USE [namedatabase]
GO


/****** Object:  StoredProcedure [dbo].[aaaa]    Script Date: 11/30/2019 7:05:46 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/****** Object:  StoredProcedure [dbo].[aaaa]    Script Date: 04/02/2018 04:12:09 ب.ظ ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


    Declare @procName varchar(500)
    Declare cur cursor 

    For select [name] from sys.objects where type = 'p'
        open cur
                    fetch next from cur into @procName
                    While @@fetch_status = 0
                    begin
                        exec('drop procedure [' + @procName + ']')
                        fetch next from cur into @procName
                    End
                    close cur
                    deallocate cur
GO

Declare @sql NVARCHAR(MAX) = N'';

SELECT @sql = @sql + N' DROP FUNCTION ' 
                   + QUOTENAME(SCHEMA_NAME(schema_id)) 
                   + N'.' + QUOTENAME(name)
FROM sys.objects
WHERE type_desc LIKE '%FUNCTION%';

Exec sp_executesql @sql
GO


--Drop trigger
DECLARE @TriggerName AS VARCHAR(500)
 -- Drop or Delete All Triggers in a Database in SQL Server 
DECLARE DropTrigger CURSOR FOR
  SELECT TRG.name AS TriggerName
  FROM   sys.triggers TRG
         INNER JOIN sys.tables TBL
                 ON TBL.OBJECT_ID = TRG.parent_id 
OPEN DropTrigger
 FETCH Next FROM DropTrigger INTO @TriggerName 
WHILE @@FETCH_STATUS = 0
  BEGIN
      DECLARE @SQL VARCHAR(MAX)=NULL
      SET @SQL='Drop Trigger ' + @TriggerName
      PRINT 'Trigger ::' + @TriggerName
            + ' Droped Successfully'
      EXEC (@SQL)
      PRINT @SQL
      FETCH Next FROM DropTrigger INTO @TriggerName
  END
CLOSE DropTrigger 
DEALLOCATE DropTrigger
GO

ALTER TABLE tblListeDastrasi
ALTER COLUMN fldNameGroup nvarchar(40) COLLATE Persian_100_CI_AS

DELETE FROM [dbo].[tblListeDastrasi]   
GO

print 'Processed 100 total records'
SET IDENTITY_INSERT [dbo].[tblListeDastrasi] OFF


DROP PROCEDURE [dah].[DateToShow]
GO

DROP PROCEDURE [dah].[DateToShow6Va9Ragham]
GO

DROP PROCEDURE [dah].[DeleteSuperVizor]
GO

DROP PROCEDURE [dah].[HasAmalkardSuperVizor]
GO

DROP PROCEDURE [dah].[HasAmalkardVizitor]
GO

DROP PROCEDURE [dah].[InfoPoorsantVizitor]
GO

DROP PROCEDURE [dah].[LoadSuperVisor]
GO

DROP PROCEDURE [dah].[LoadVaseteArshiveBargashtAzForoosh]
GO

DROP PROCEDURE [dah].[LoadVaseteArshiveForoosh]
GO

DROP PROCEDURE [dah].[LoadVaseteArshiveSanad]
GO

DROP PROCEDURE [dah].[LoadVizitor]
GO

DROP PROCEDURE [dah].[MinMaxDateAnbar]
GO

DROP PROCEDURE [dah].[NameKolMoin]
GO

DROP PROCEDURE [dah].[typeSanad]
GO

DROP PROCEDURE [dah].[Update_SuperVizor]
GO

DROP PROCEDURE [dah].[Update_Vizitor]
GO




CREATE TYPE [dbo].[dtAghsat] AS TABLE(
	[fldRadif] [bigint] NOT NULL,
	[fldShomareVam] [bigint] NOT NULL,
	[fldDateSarResid] [bigint] NULL,
	[fldMablaghGhest] [decimal](18, 0) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtAghsat-Multi]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtAghsat-Multi] AS TABLE(
	[fldRadif] [bigint] NOT NULL,
	[fldShomareVam] [bigint] NOT NULL,
	[fldDateSarResid] [bigint] NULL,
	[fldMablaghGhest] [decimal](18, 4) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtBastanFactorTasvieNashode]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtBastanFactorTasvieNashode] AS TABLE(
	[fldShomareFactor] [bigint] NULL,
	[fldMablagheFactor] [bigint] NOT NULL,
	[fldMandeTalabAzFactor] [bigint] NOT NULL,
	[fldMablagheDaryafti] [bigint] NOT NULL,
	[fldMMablagheBargashti] [bigint] NOT NULL,
	[fldMandeTasfieNashode] [bigint] NOT NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtCheckBazPasShode]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtCheckBazPasShode] AS TABLE(
	[id] [bigint] NULL,
	[fldRadif] [bigint] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL,
	[fldCodeSahebeCheck] [bigint] NULL,
	[fldNameSahebeCheck] [nvarchar](100) NULL,
	[fldDateSarResid] [nvarchar](100) NULL,
	[fldCodeBank] [bigint] NULL,
	[fldNameBank] [nvarchar](100) NULL,
	[fldShobe] [nvarchar](100) NULL,
	[fldMablagh] [decimal](18, 0) NULL,
	[fldShomarePass] [bigint] NULL,
	[fldSanad] [bigint] NULL,
	[fldShomareBazPas] [bigint] NULL,
	[fldSanadBazPas] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtCheckPass]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtCheckPass] AS TABLE(
	[ID] [bigint] NULL,
	[fldShomarePas] [bigint] NULL,
	[fldTozihat] [nvarchar](50) NULL,
	[fldSanad] [bigint] NULL,
	[fldMablagh] [decimal](18, 0) NULL,
	[fldCodeSahebeCheck] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtCheckPassMain]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtCheckPassMain] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldSHomareHesab] [bigint] NULL,
	[fldShomarePas] [bigint] NULL,
	[fldTozihat] [nvarchar](50) NULL,
	[fldSanad] [bigint] NULL,
	[fldMablagh] [decimal](18, 0) NULL,
	[fldCodeSahebeCheck] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtCheckPassMain2]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtCheckPassMain2] AS TABLE(
	[ID] [int] NOT NULL,
	[fldShomarePas] [bigint] NULL,
	[fldTozihat] [nvarchar](50) NULL,
	[fldSanad] [bigint] NULL,
	[fldMablagh] [decimal](18, 0) NULL,
	[fldCodeSahebeCheck] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtCheckPassMain3]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtCheckPassMain3] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldSHomareHesab] [bigint] NULL,
	[fldShomarePas] [bigint] NULL,
	[fldTozihat] [nvarchar](50) NULL,
	[fldSanad] [bigint] NULL,
	[fldMablagh] [decimal](18, 0) NULL,
	[fldCodeSahebeCheck] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtCheckPassMain-Multi]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtCheckPassMain-Multi] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldSHomareHesab] [bigint] NULL,
	[fldShomarePas] [bigint] NULL,
	[fldTozihat] [nvarchar](50) NULL,
	[fldSanad] [bigint] NULL,
	[fldMablagh] [decimal](18, 4) NULL,
	[fldCodeSahebeCheck] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtCheckVosool]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtCheckVosool] AS TABLE(
	[fldIDVosool] [bigint] NULL,
	[fldShomareVosool] [bigint] NULL,
	[fldDateVosool] [int] NULL,
	[fldTozihateVosool] [nvarchar](50) NULL,
	[fldVaziat] [nvarchar](15) NULL,
	[fldSanadVosool] [bigint] NULL,
	[fldCodeHesabeVosool] [nvarchar](10) NULL,
	[fldMablagh] [decimal](18, 0) NULL,
	[fldCodeSahebeCheck] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtCheckVosool-Multi]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtCheckVosool-Multi] AS TABLE(
	[fldIDVosool] [bigint] NULL,
	[fldShomareVosool] [bigint] NULL,
	[fldDateVosool] [int] NULL,
	[fldTozihateVosool] [nvarchar](50) NULL,
	[fldVaziat] [nvarchar](15) NULL,
	[fldSanadVosool] [bigint] NULL,
	[fldCodeHesabeVosool] [nvarchar](10) NULL,
	[fldMablagh] [decimal](18, 4) NULL,
	[fldCodeSahebeCheck] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtKala]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtKala] AS TABLE(
	[ID] [bigint] IDENTITY(1,1) NOT NULL,
	[fldNameKala] [nvarchar](100) NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSerial] [nvarchar](20) NULL,
	[fldCodeGroup] [bigint] NOT NULL,
	[fldCodeVahed1] [bigint] NOT NULL,
	[fldCodeVahed2] [bigint] NULL,
	[fldNogheSefaresh] [decimal](18, 0) NULL,
	[fldSharh] [nvarchar](100) NULL,
	[fldTedadJoz] [decimal](18, 0) NULL,
	[fldFeeDarsadKharid] [float] NULL,
	[fldFeeBadAzTakhfif] [money] NULL,
	[fldNerkhePorsantKol] [float] NULL,
	[fldNerkhePorsantJoz] [float] NULL,
	[fldFeeForoosh] [money] NULL,
	[fldPathPic] [nvarchar](max) NULL,
	[fldGhymatMasrafkonande] [decimal](18, 0) NULL,
	[fldIDPrinter] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtListeForoosh]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtListeForoosh] AS TABLE(
	[fldShomareFactor] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtTasfie]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtTasfie] AS TABLE(
	[fldShomareFactor] [bigint] NULL,
	[fldShomareDaryaft] [bigint] NULL,
	[fldMablagh] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtTasfie-Multi]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtTasfie-Multi] AS TABLE(
	[fldShomareFactor] [bigint] NULL,
	[fldShomareDaryaft] [bigint] NULL,
	[fldMablagh] [decimal](18, 4) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtTasfieNew]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtTasfieNew] AS TABLE(
	[fldShomareFactor] [bigint] NULL,
	[fldShomareDaryaft] [bigint] NULL,
	[fldMablagh] [decimal](18, 2) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtTasfieVam]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtTasfieVam] AS TABLE(
	[fldShomareVam] [bigint] NULL,
	[fldShomareGhest] [bigint] NULL,
	[fldShomareDaryaft] [bigint] NULL,
	[fldMablagh] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtTasfieVam-Multi]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtTasfieVam-Multi] AS TABLE(
	[fldShomareVam] [bigint] NULL,
	[fldShomareGhest] [bigint] NULL,
	[fldShomareDaryaft] [bigint] NULL,
	[fldMablagh] [decimal](18, 4) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[dtTasfieVamNew]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[dtTasfieVamNew] AS TABLE(
	[fldShomareVam] [bigint] NULL,
	[fldShomareGhest] [bigint] NULL,
	[fldShomareDaryaft] [bigint] NULL,
	[fldMablagh] [decimal](18, 2) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[LocationTableType]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[LocationTableType] AS TABLE(
	[LocationName] [nvarchar](50) NULL,
	[CostRate] [int] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tbl1]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tbl1] AS TABLE(
	[fld1] [varchar](50) NULL,
	[fld2] [varchar](50) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tblCheckDaryafti]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tblCheckDaryafti] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL,
	[fldDateSarResid] [int] NULL,
	[fldCodeBank] [int] NULL,
	[fldShobe] [nvarchar](40) NULL,
	[fldMablagh] [decimal](18, 0) NULL,
	[fldCodeSahebeCheck] [nchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tblCheckDaryafti-Multi]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tblCheckDaryafti-Multi] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL,
	[fldDateSarResid] [int] NULL,
	[fldCodeBank] [int] NULL,
	[fldShobe] [nvarchar](40) NULL,
	[fldMablagh] [decimal](18, 4) NULL,
	[fldCodeSahebeCheck] [nchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tblCheckDaryaftiNew]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tblCheckDaryaftiNew] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL,
	[fldDateSarResid] [nvarchar](15) NULL,
	[fldCodeBank] [int] NULL,
	[fldShobe] [nvarchar](40) NULL,
	[fldMablagh] [decimal](18, 0) NULL,
	[fldCodeSahebeCheck] [nchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tblCheckDaryaftiNewer]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tblCheckDaryaftiNewer] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL,
	[fldDateSarResid] [int] NULL,
	[fldCodeBank] [int] NULL,
	[fldShobe] [nvarchar](40) NULL,
	[fldSharhedaryaft] [nvarchar](70) NULL,
	[fldMablagh] [decimal](18, 0) NULL,
	[fldCodeSahebeCheck] [nchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tblCheckDaryaftiNewer-Multi]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tblCheckDaryaftiNewer-Multi] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL,
	[fldDateSarResid] [int] NULL,
	[fldCodeBank] [int] NULL,
	[fldShobe] [nvarchar](40) NULL,
	[fldSharhedaryaft] [nvarchar](70) NULL,
	[fldMablagh] [decimal](18, 4) NULL,
	[fldCodeSahebeCheck] [nchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tblCheckDaryaftiNewerNew]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tblCheckDaryaftiNewerNew] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL,
	[fldDateSarResid] [int] NULL,
	[fldCodeBank] [int] NULL,
	[fldShobe] [nvarchar](40) NULL,
	[fldSharhedaryaft] [nvarchar](70) NULL,
	[fldMablagh] [decimal](18, 2) NULL,
	[fldCodeSahebeCheck] [nchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tblCheckPardakhti]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tblCheckPardakhti] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL,
	[fldDateSarResid] [int] NULL,
	[fldCodeBank] [int] NULL,
	[fldShobe] [nvarchar](40) NULL,
	[fldMablagh] [decimal](18, 0) NULL,
	[fldCodeSahebeCheck] [nchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tblCheckPardakhti-Multi]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tblCheckPardakhti-Multi] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL,
	[fldDateSarResid] [int] NULL,
	[fldCodeBank] [int] NULL,
	[fldShobe] [nvarchar](40) NULL,
	[fldMablagh] [decimal](18, 4) NULL,
	[fldCodeSahebeCheck] [nchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tblCheckPardakhtiNew]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tblCheckPardakhtiNew] AS TABLE(
	[ID] [int] NULL,
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL,
	[fldDateSarResid] [int] NULL,
	[fldCodeBank] [int] NULL,
	[fldShobe] [nvarchar](40) NULL,
	[fldMablagh] [decimal](18, 2) NULL,
	[fldCodeSahebeCheck] [nchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tblCheckVagozari]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tblCheckVagozari] AS TABLE(
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tblDaftarCheck]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tblDaftarCheck] AS TABLE(
	[fldShomareCheck] [bigint] NULL,
	[fldShomareHesab] [bigint] NULL,
	[fldShobe] [nvarchar](40) NULL,
	[fldCodeBank] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[TestEmpType]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[TestEmpType] AS TABLE(
	[ID] [int] NULL,
	[Name] [varchar](3000) NULL,
	[Address] [varchar](8000) NULL,
	[Operation] [smallint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[TestEmpType2]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[TestEmpType2] AS TABLE(
	[ID] [int] NULL,
	[Name] [varchar](3000) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeBargashtAzForoosh]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeBargashtAzForoosh] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 2) NULL,
	[fldFeeYekCarton] [decimal](18, 2) NULL,
	[fldFeeKol] [decimal](18, 2) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 2) NULL,
	[fldArzeshAfzode] [decimal](18, 2) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 2) NULL,
	[fldMablaghKhales] [decimal](18, 2) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForoosh]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForoosh] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForoosh_Amani]    Script Date: 09/25/2022 02:23:38 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForoosh_Amani] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL,
	[fldCodeAnbar] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForoosh_Amani2]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForoosh_Amani2] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL,
	[fldCodeAnbar] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForoosh_Enteghal]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForoosh_Enteghal] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForoosh_EnteghalNew]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForoosh_EnteghalNew] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForoosh_EnteghalNew-Multi]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForoosh_EnteghalNew-Multi] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 4) NULL,
	[fldFeeJoz] [decimal](18, 4) NULL,
	[fldFeeYekCarton] [decimal](18, 4) NULL,
	[fldFeeKol] [decimal](18, 4) NULL,
	[fldDarsadTakhfif] [decimal](18, 4) NULL,
	[fldMablaghTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 4) NULL,
	[fldArzeshAfzode] [decimal](18, 4) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 4) NULL,
	[fldMablaghKhales] [decimal](18, 4) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForoosh-Multi]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForoosh-Multi] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 4) NULL,
	[fldFeeJoz] [decimal](18, 4) NULL,
	[fldFeeYekCarton] [decimal](18, 4) NULL,
	[fldFeeKol] [decimal](18, 4) NULL,
	[fldDarsadTakhfif] [decimal](18, 4) NULL,
	[fldMablaghTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 4) NULL,
	[fldArzeshAfzode] [decimal](18, 4) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 4) NULL,
	[fldMablaghKhales] [decimal](18, 4) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForooshNew]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForooshNew] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForooshNew-Multi]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForooshNew-Multi] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 4) NULL,
	[fldFeeJoz] [decimal](18, 4) NULL,
	[fldFeeYekCarton] [decimal](18, 4) NULL,
	[fldFeeKol] [decimal](18, 4) NULL,
	[fldDarsadTakhfif] [decimal](18, 4) NULL,
	[fldMablaghTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 4) NULL,
	[fldArzeshAfzode] [decimal](18, 4) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 4) NULL,
	[fldMablaghKhales] [decimal](18, 4) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForooshNewNew]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForooshNewNew] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL,
	[fldEshantion] [nvarchar](13) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForooshNewNewNew]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForooshNewNewNew] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 2) NULL,
	[fldFeeYekCarton] [decimal](18, 2) NULL,
	[fldFeeKol] [decimal](18, 2) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 2) NULL,
	[fldArzeshAfzode] [decimal](18, 2) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 2) NULL,
	[fldMablaghKhales] [decimal](18, 2) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL,
	[fldEshantion] [nvarchar](13) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeForooshNewNewNew-Multi]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeForooshNewNewNew-Multi] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 4) NULL,
	[fldFeeJoz] [decimal](18, 4) NULL,
	[fldFeeYekCarton] [decimal](18, 4) NULL,
	[fldFeeKol] [decimal](18, 4) NULL,
	[fldDarsadTakhfif] [decimal](18, 4) NULL,
	[fldMablaghTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 4) NULL,
	[fldArzeshAfzode] [decimal](18, 4) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 4) NULL,
	[fldMablaghKhales] [decimal](18, 4) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL,
	[fldEshantion] [nvarchar](13) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid] AS TABLE(
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_Amani]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_Amani] AS TABLE(
	[fldShomareSanad] [bigint] NULL,
	[fldShomareFactor] [bigint] NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldCodeTafzili] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_Enteghal]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_Enteghal] AS TABLE(
	[fldShomareSanad] [bigint] NULL,
	[fldShomareFactor] [bigint] NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_EnteghalNew]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_EnteghalNew] AS TABLE(
	[fldShomareSanad] [bigint] NULL,
	[fldShomareFactor] [bigint] NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_EnteghalNew-Multi]    Script Date: 09/25/2022 02:23:39 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_EnteghalNew-Multi] AS TABLE(
	[fldShomareSanad] [bigint] NULL,
	[fldShomareFactor] [bigint] NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 4) NULL,
	[fldFeeJoz] [decimal](18, 4) NULL,
	[fldFeeYekCarton] [decimal](18, 4) NULL,
	[fldFeeKol] [decimal](18, 4) NULL,
	[fldDarsadTakhfif] [decimal](18, 4) NULL,
	[fldMablaghTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 4) NULL,
	[fldArzeshAfzode] [decimal](18, 4) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 4) NULL,
	[fldMablaghKhales] [decimal](18, 4) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_EnteghalNewNew]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_EnteghalNewNew] AS TABLE(
	[fldShomareSanad] [bigint] NULL,
	[fldShomareFactor] [bigint] NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDateEngheza] [bigint] NULL,
	[fldCodeAnbar] [int] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_FactorKharid]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_FactorKharid] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldShomareEnteghal] [bigint] NULL,
	[fldHamloNaghl] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_FactorKharid_Final]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_FactorKharid_Final] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 2) NULL,
	[fldFeeYekCarton] [decimal](18, 2) NULL,
	[fldFeeKol] [decimal](18, 2) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 2) NULL,
	[fldArzeshAfzode] [decimal](18, 2) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 2) NULL,
	[fldMablaghKhales] [decimal](18, 2) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldHamloNaghl] [bigint] NULL,
	[fldGhymatMasrafkonande] [decimal](18, 2) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_FactorKharid_Final2]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_FactorKharid_Final2] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldHamloNaghl] [bigint] NULL,
	[fldGhymatMasrafkonande] [decimal](18, 0) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_FactorKharid_FinalNew]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_FactorKharid_FinalNew] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 2) NULL,
	[fldFeeYekCarton] [decimal](18, 2) NULL,
	[fldFeeKol] [decimal](18, 2) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 2) NULL,
	[fldArzeshAfzode] [decimal](18, 2) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 2) NULL,
	[fldMablaghKhales] [decimal](18, 2) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldHamloNaghl] [bigint] NULL,
	[fldGhymatMasrafkonande] [decimal](18, 2) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_FactorKharid_FinalNewMulti]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_FactorKharid_FinalNewMulti] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 2) NULL,
	[fldFeeYekCarton] [decimal](18, 2) NULL,
	[fldFeeKol] [decimal](18, 2) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 2) NULL,
	[fldArzeshAfzode] [decimal](18, 2) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 2) NULL,
	[fldMablaghKhales] [decimal](18, 2) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldHamloNaghl] [decimal](18, 2) NULL,
	[fldGhymatMasrafkonande] [decimal](18, 2) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_FactorKharid_FinalNew-Multi]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_FactorKharid_FinalNew-Multi] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 4) NULL,
	[fldFeeJoz] [decimal](18, 4) NULL,
	[fldFeeYekCarton] [decimal](18, 4) NULL,
	[fldFeeKol] [decimal](18, 4) NULL,
	[fldDarsadTakhfif] [decimal](18, 4) NULL,
	[fldMablaghTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 4) NULL,
	[fldArzeshAfzode] [decimal](18, 4) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 4) NULL,
	[fldMablaghKhales] [decimal](18, 4) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldHamloNaghl] [decimal](18, 4) NULL,
	[fldGhymatMasrafkonande] [decimal](18, 4) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_FactorKharid_FinalNewNew]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_FactorKharid_FinalNewNew] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 2) NULL,
	[fldFeeYekCarton] [decimal](18, 2) NULL,
	[fldFeeKol] [decimal](18, 2) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 2) NULL,
	[fldArzeshAfzode] [decimal](18, 2) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 2) NULL,
	[fldMablaghKhales] [decimal](18, 2) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldHamloNaghl] [decimal](18, 2) NULL,
	[fldGhymatMasrafkonande] [decimal](18, 2) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_MojodiEbtedaDoreh]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_MojodiEbtedaDoreh] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldShomareEnteghal] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeKharid_MojodiEbtedaDorehMulti]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeKharid_MojodiEbtedaDorehMulti] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 2) NULL,
	[fldFeeYekCarton] [decimal](18, 2) NULL,
	[fldFeeKol] [decimal](18, 2) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 2) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 2) NULL,
	[fldArzeshAfzode] [decimal](18, 2) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 2) NULL,
	[fldMablaghKhales] [decimal](18, 2) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldShomareEnteghal] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizePishForoosh]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizePishForoosh] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [decimal](18, 2) NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldEshantion] [nvarchar](13) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizePishForoosh-Multi]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizePishForoosh-Multi] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 4) NULL,
	[fldFeeJoz] [decimal](18, 4) NULL,
	[fldFeeYekCarton] [decimal](18, 4) NULL,
	[fldFeeKol] [decimal](18, 4) NULL,
	[fldDarsadTakhfif] [decimal](18, 4) NULL,
	[fldMablaghTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 4) NULL,
	[fldArzeshAfzode] [decimal](18, 4) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 4) NULL,
	[fldMablaghKhales] [decimal](18, 4) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldEshantion] [nvarchar](13) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeTolid]    Script Date: 09/25/2022 02:23:40 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeTolid] AS TABLE(
	[fldShomareSanad] [bigint] NULL,
	[fldShomareTolid] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCartonAvalie] [nvarchar](10) NULL,
	[fldTedadJozAvalie] [decimal](18, 2) NULL,
	[fldTedadCartonNahaei] [nvarchar](10) NULL,
	[fldTedadJozNahaei] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 2) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDarsdeTakhfif] [decimal](18, 0) NULL,
	[fldMablagheTakhfif] [decimal](18, 0) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeTolid2]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeTolid2] AS TABLE(
	[fldShomareSanad] [bigint] NULL,
	[fldShomareTolid] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCartonAvalie] [nvarchar](10) NULL,
	[fldTedadJozAvalie] [decimal](18, 2) NULL,
	[fldTedadCartonNahaei] [nvarchar](10) NULL,
	[fldTedadJozNahaei] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 2) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDarsdeTakhfif] [decimal](18, 0) NULL,
	[fldMablagheTakhfif] [decimal](18, 0) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeTolid2-Multi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeTolid2-Multi] AS TABLE(
	[fldShomareSanad] [bigint] NULL,
	[fldShomareTolid] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCartonAvalie] [nvarchar](10) NULL,
	[fldTedadJozAvalie] [decimal](18, 4) NULL,
	[fldTedadCartonNahaei] [nvarchar](10) NULL,
	[fldTedadJozNahaei] [decimal](18, 4) NULL,
	[fldFeeJoz] [decimal](18, 4) NULL,
	[fldFeeYekCarton] [decimal](18, 4) NULL,
	[fldFeeKol] [decimal](18, 4) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 4) NULL,
	[fldMablaghKhales] [decimal](18, 4) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDarsdeTakhfif] [decimal](18, 4) NULL,
	[fldMablagheTakhfif] [decimal](18, 4) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeTolidNew]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeTolidNew] AS TABLE(
	[fldShomareSanad] [bigint] NULL,
	[fldShomareTolid] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCartonAvalie] [nvarchar](10) NULL,
	[fldTedadJozAvalie] [decimal](18, 2) NULL,
	[fldTedadCartonNahaei] [nvarchar](10) NULL,
	[fldTedadJozNahaei] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 2) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldHazineTabdil] [decimal](18, 2) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeTolidNewNew]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeTolidNewNew] AS TABLE(
	[fldShomareSanad] [bigint] NULL,
	[fldShomareTolid] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCartonAvalie] [nvarchar](10) NULL,
	[fldTedadJozAvalie] [decimal](18, 2) NULL,
	[fldTedadCartonNahaei] [nvarchar](10) NULL,
	[fldTedadJozNahaei] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 2) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDarsdeTakhfif] [decimal](18, 0) NULL,
	[fldMablagheTakhfif] [decimal](18, 0) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpRizeTolidNewNew-Multi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
CREATE TYPE [dbo].[tpRizeTolidNewNew-Multi] AS TABLE(
	[fldShomareSanad] [bigint] NULL,
	[fldShomareTolid] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCartonAvalie] [nvarchar](10) NULL,
	[fldTedadJozAvalie] [decimal](18, 4) NULL,
	[fldTedadCartonNahaei] [nvarchar](10) NULL,
	[fldTedadJozNahaei] [decimal](18, 4) NULL,
	[fldFeeJoz] [decimal](18, 4) NULL,
	[fldFeeYekCarton] [decimal](18, 4) NULL,
	[fldFeeKol] [decimal](18, 4) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 4) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 4) NULL,
	[fldMablaghKhales] [decimal](18, 4) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldDarsdeTakhfif] [decimal](18, 4) NULL,
	[fldMablagheTakhfif] [decimal](18, 4) NULL,
	[fldDateEngheza] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpSanadEbteda_Ashkas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
CREATE TYPE [dbo].[tpSanadEbteda_Ashkas] AS TABLE(
	[fldSanad] [varchar](50) NULL,
	[fldDay] [varchar](50) NULL,
	[fldMonth] [varchar](50) NULL,
	[fldYear] [varchar](50) NULL,
	[fldCodeKol] [varchar](50) NULL,
	[fldCodeMoin] [varchar](50) NULL,
	[fldCodeTafzili] [varchar](50) NULL,
	[fldBedehkar] [decimal](18, 0) NULL,
	[fldBestankar] [decimal](18, 0) NULL,
	[fldRadif] [varchar](50) NULL,
	[fldDate] [int] NULL,
	[fldSharh] [varchar](50) NULL,
	[fldMoinKamel] [varchar](50) NULL,
	[fldCodeNoeTafzili] [varchar](50) NULL,
	[fldType] [varchar](50) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[tpSanadEbteda_AshkasMulti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
CREATE TYPE [dbo].[tpSanadEbteda_AshkasMulti] AS TABLE(
	[fldSanad] [varchar](50) NULL,
	[fldDay] [varchar](50) NULL,
	[fldMonth] [varchar](50) NULL,
	[fldYear] [varchar](50) NULL,
	[fldCodeKol] [varchar](50) NULL,
	[fldCodeMoin] [varchar](50) NULL,
	[fldCodeTafzili] [varchar](50) NULL,
	[fldBedehkar] [decimal](18, 4) NULL,
	[fldBestankar] [decimal](18, 4) NULL,
	[fldRadif] [varchar](50) NULL,
	[fldDate] [int] NULL,
	[fldSharh] [varchar](50) NULL,
	[fldMoinKamel] [varchar](50) NULL,
	[fldCodeNoeTafzili] [varchar](50) NULL,
	[fldType] [varchar](50) NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[typeKharidtest]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
CREATE TYPE [dbo].[typeKharidtest] AS TABLE(
	[fldShomareSanad] [bigint] NOT NULL,
	[fldShomareFactor] [bigint] NOT NULL,
	[fldCodeVasete] [bigint] NULL,
	[fldCodeTafsili] [bigint] NULL,
	[fldCodeUser] [bigint] NULL,
	[fldRadif] [int] NOT NULL,
	[fldCodeAnbar] [bigint] NOT NULL,
	[fldCodeKala] [bigint] NOT NULL,
	[fldSharhKala] [nvarchar](250) NULL,
	[fldTedadCarton] [nvarchar](10) NULL,
	[fldTedadJoz] [decimal](18, 2) NULL,
	[fldFeeJoz] [decimal](18, 0) NULL,
	[fldFeeYekCarton] [decimal](18, 0) NULL,
	[fldFeeKol] [decimal](18, 0) NULL,
	[fldDarsadTakhfif] [smallint] NULL,
	[fldMablaghTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed1BadAzTakhfif] [decimal](18, 0) NULL,
	[fldFeeVahed2BadAzTakhfif] [decimal](18, 0) NULL,
	[fldArzeshAfzode] [decimal](18, 0) NULL,
	[fldFeeKolBadAzTakhfif] [decimal](18, 0) NULL,
	[fldMablaghKhales] [decimal](18, 0) NULL,
	[fldType] [nvarchar](20) NULL,
	[fldDate] [bigint] NOT NULL,
	[fldTime] [nvarchar](10) NULL,
	[fldShomareEnteghal] [bigint] NULL
)
GO
/****** Object:  UserDefinedTableType [dbo].[ZT_CodeNumbers]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
CREATE TYPE [dbo].[ZT_CodeNumbers] AS TABLE(
	[CodeNumber] [bigint] NOT NULL
)
GO
/****** Object:  UserDefinedFunction [dbo].[fn_TablesLastUpdateDate]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTIOn [dbo].[fn_TablesLastUpdateDate](@Date NVARCHAR(20))

RETURNS @table TABLE(TableName NVARCHAR(40), LastUpdated Datetime)

AS

BEGIN


IF(@Date='') OR (@Date Is Null) OR (@Date='0')

    BEGIN
        INSERT INTO @table
        SELECT TOP 100 PERCENT TABLENAME,LASTUPDATED FROM 
        (
            SELECT  B.NAME AS 'TABLENAME', MAX(STATS_DATE (ID,INDID)) AS LASTUPDATED
            FROM    SYS.SYSINDEXES AS A
                    INNER JOIN SYS.OBJECTS AS B ON A.ID = B.OBJECT_ID
            WHERE   B.TYPE = 'U'  AND STATS_DATE (ID,INDID) IS NOT NULL 
            GROUP BY B.NAME
        ) AS A
        ORDER BY LASTUPDATED DESC
    END
ELSE

    BEGIN
        INSERT INTO @table
        SELECT TOP 100 PERCENT TABLENAME,LASTUPDATED FROM 
        (
            SELECT  B.NAME AS 'TABLENAME', MAX(STATS_DATE (ID,INDID)) AS LASTUPDATED,
                    CONVERT(VARCHAR, MAX(STATS_DATE (ID,INDID)), 103) as Date
            FROM    SYS.SYSINDEXES AS A
                    INNER JOIN SYS.OBJECTS AS B ON A.ID = B.OBJECT_ID
            WHERE   B.TYPE = 'U'  AND STATS_DATE (ID,INDID) IS NOT NULL 
            GROUP BY B.NAME
        ) AS A
        WHERE Date=@Date
        ORDER BY LASTUPDATED DESC
    END
RETURN

END

GO
/****** Object:  UserDefinedFunction [dbo].[ZF_GetAccessableAshkhasCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create FUNCTION [dbo].[ZF_GetAccessableAshkhasCode]
(	
	-- Add the parameters for the function here
	@userCode bigint
)
RETURNS @output TABLE(fldCode bigint 
) 
begin
  --Declare @value nvarchar(100)
  --set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPASHKHAS')

 Declare @value nvarchar(100)
  --set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPASHKHAS')
  if ( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPASHKHAS') = 'True'
  Begin -- Limitation Active

    declare @groups table(fldCode bigint)
	 insert into @groups(fldCode)
	 select fldGroupCode from tblGroupAshkhasAccess where fldUserCode = @usercode

	 insert into @output(fldCode)
	 select fldCodeTafzili from tblTafzili as t
	 inner join @groups as g on convert(nvarchar(100),t.fldCodeGroup) like convert(nvarchar(100),g.fldCode) + '%'
	 group by fldCodeTafzili
	 order by CONVERT(varchar,fldCodeTafzili )


  End
  else
  Begin -- Limitation DeActive
	insert into @output(fldCode)
	select fldCodeTafzili from tblTafzili
	order by CONVERT(varchar,fldCodeTafzili )
  End

  RETURN 
end

GO
/****** Object:  UserDefinedFunction [dbo].[ZF_GetAccessableGroupAshkhasCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create FUNCTION [dbo].[ZF_GetAccessableGroupAshkhasCode]
(	
	-- Add the parameters for the function here
	@userCode bigint
)
RETURNS @output TABLE(fldCode bigint 
) 
begin
  --Declare @value nvarchar(100)
  --set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPASHKHAS')

  if (( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPASHKHAS') = 'True')
  Begin -- Limitation Active

		declare @tbl table(fldCode bigint) -- in fact -> fldCodeGroupKala 
		insert @tbl(fldCode)
		select fldGroupCode from tblGroupAshkhasAccess where fldUserCode = @userCode 
		
		insert @output(fldCode)
		select fldGroupId from tblGroupAshkhas 
		where  fldGroupId in(select fldCode from @tbl)
		order by CONVERT(varchar,fldGroupId )

  End
  else
  Begin -- Limitation DeActive

		insert @output(fldCode)
		select fldGroupId from tblGroupAshkhas
		order by CONVERT(varchar,fldGroupId )

  End

  RETURN 
end

GO
/****** Object:  UserDefinedFunction [dbo].[ZF_SplitString]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[ZF_SplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(number bigint 
) 
BEGIN 
 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1
       
        INSERT INTO @output (number)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)
        
    END 
 
    RETURN 
 
END

GO
/****** Object:  UserDefinedFunction [dbo].[SplitString]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[SplitString]
(
  @List     nvarchar(max),
  @Delim    nvarchar(255)
)
RETURNS TABLE WITH SCHEMABINDING
AS
   RETURN ( WITH n(n) AS (SELECT 1 UNION ALL SELECT n+1 
       FROM n WHERE n <= LEN(@List))
       SELECT [Value] = SUBSTRING(@List, n, 
       CHARINDEX(@Delim, @List + @Delim, n) - n)
       FROM n WHERE n <= LEN(@List)
      AND SUBSTRING(@Delim + @List, n, DATALENGTH(@Delim)/2) = @Delim
   );

GO
/****** Object:  UserDefinedFunction [dbo].[translateFunction]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE FUNCTION [dbo].[translateFunction]
(	
	-- Add the parameters for the function here
	@Language nvarchar(10),
	@CodeMessage bigint
)
RETURNS TABLE 
AS
RETURN 
(
	-- Add the SELECT statement with parameter references here
	SELECT fldMatn
					FROM  tblTranslateMain 
						where fldLanguage=@Language and fldCode=@CodeMessage
)

GO
/****** Object:  View [dbo].[Join_TblTell_TblTafzili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE VIEW [dbo].[Join_TblTell_TblTafzili]
AS
SELECT        TOP (100) PERCENT dbo.tblTafzili.fldSharheTafzili, dbo.tblTafzili.fldCodeTafzili, dbo.tblDaftarcheTell.fldShomare, dbo.tblTafzili.fldCodeMeli, 
                         dbo.tblTafzili.fldAddress
FROM            dbo.tblDaftarcheTell INNER JOIN
                         dbo.tblTafzili ON dbo.tblDaftarcheTell.fldCodeTafzili = dbo.tblTafzili.fldCodeTafzili
GROUP BY dbo.tblTafzili.fldSharheTafzili, dbo.tblTafzili.fldCodeTafzili, dbo.tblDaftarcheTell.fldShomare, dbo.tblTafzili.fldCodeMeli, dbo.tblTafzili.fldAddress



GO
/****** Object:  View [dbo].[lstlistKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[lstlistKala]
AS
SELECT        fldNameKala, fldCodeKala
FROM            dbo.tblKala



GO
/****** Object:  View [dbo].[View_2]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[View_2]
AS
SELECT DISTINCT 
                         dbo.tblTafzili.fldCodeGroup, dbo.tblGroupAshkhas.fldGroupId, dbo.tblGroupAshkhas.fldGroupName, dbo.tblDaftarcheTell.fldShomare, 
                         dbo.tblTafzili.fldCodeTafzili
FROM            dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup INNER JOIN
                         dbo.tblDaftarcheTell ON CONVERT(int, dbo.tblTafzili.fldCodeTafzili) = CONVERT(int, dbo.tblDaftarcheTell.fldCodeTafzili)



GO
/****** Object:  View [dbo].[View_ListAshkhas_In_ReportFactorForosh_AliJahangard]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[View_ListAshkhas_In_ReportFactorForosh_AliJahangard]
AS
SELECT        dbo.tblTafzili.fldSharheTafzili, dbo.tblTafzili.fldCodeTafzili, dbo.tblTafzili.fldCodeGroup, dbo.tblTafzili.fldOstan, dbo.tblTafzili.fldCity, dbo.tblTafzili.fldAddress, 
                         dbo.tblTafzili.fldShomareHesab, dbo.tblTafzili.fldCodeMeli, dbo.tblTafzili.fldCodeEghtesadi, dbo.tblTafzili.fldCodePosti, dbo.tblTafzili.fldShomareSabt, 
                         dbo.tblTafzili.fldSaghfeEtebar, dbo.tblTafzili.fldBlackList, dbo.tblTafzili.fldSharhBlackList, dbo.tblTafzili.fldDate1, dbo.tblTafzili.fldDate2, dbo.tblDaftarcheTell.fldSms, 
                         dbo.tblDaftarcheTell.fldShomare
FROM            dbo.tblTafzili LEFT OUTER JOIN
                         dbo.tblDaftarcheTell ON dbo.tblTafzili.fldCodeTafzili = dbo.tblDaftarcheTell.fldCodeTafzili



GO
/****** Object:  View [dbo].[View_TblChap]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[View_TblChap]
AS
SELECT        fldNameMoassese, fldTozihateBala, fldAdress, fldPasafhe, fldCodeEghtesadi, fldShomareSabt, fldCodePosti, fldTell
FROM            dbo.tblChap



GO
/****** Object:  View [dbo].[viewFilter_Tbl_Tafsili_in_Form_Enteghal_Kala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewFilter_Tbl_Tafsili_in_Form_Enteghal_Kala]
AS
SELECT DISTINCT fldCodeTafzili, fldSharheTafzili,fldAddress ,fldGroupName 
FROM            dbo.tblTafzili
inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId = tblTafzili.fldCodeGroup


GO
/****** Object:  View [dbo].[viewGetAnbarForComboBox]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewGetAnbarForComboBox]
AS
SELECT        fldCode, fldNameAnbar
FROM            dbo.tblAnbar



GO
/****** Object:  View [dbo].[viewGetListEshantionGroupTabalaghtis]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewGetListEshantionGroupTabalaghtis]
AS
SELECT        dbo.tblEshantionGroupTabaghati.fldCodeGroupKala, dbo.tblEshantionGroupTabaghati.fldShomareTabaghe, dbo.tblEshantionGroupTabaghati.fldTedadForoosh, 
                         dbo.tblEshantionGroupTabaghati.fldCodeKalaEshantion, dbo.tblEshantionGroupTabaghati.fldTedadEshantion, dbo.tblKala.fldNameKala
FROM            dbo.tblEshantionGroupTabaghati INNER JOIN
                         dbo.tblKala ON dbo.tblEshantionGroupTabaghati.fldCodeKalaEshantion = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblGroupKala ON dbo.tblEshantionGroupTabaghati.fldCodeGroupKala = dbo.tblGroupKala.fldGroupId



GO
/****** Object:  View [dbo].[viewGetListEshantionTabalaghti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewGetListEshantionTabalaghti]
AS
SELECT        dbo.tblEshantionTabaghati.fldShomareTabaghe, dbo.tblEshantionTabaghati.fldTedadForoosh, dbo.tblEshantionTabaghati.fldTedadEshantion, 
                         dbo.tblEshantionTabaghati.fldCodeKalaEshantion, dbo.tblKala.fldNameKala AS fldNameKalaEshantion, tblKala_1.fldCodeKala AS fldCodeKalaForoosh, 
                         tblKala_1.fldNameKala AS fldNameKalaForoosh
FROM            dbo.tblEshantionTabaghati INNER JOIN
                         dbo.tblKala ON dbo.tblEshantionTabaghati.fldCodeKalaEshantion = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblKala AS tblKala_1 ON dbo.tblEshantionTabaghati.fldCodeKalaForoosh = tblKala_1.fldCodeKala



GO
/****** Object:  View [dbo].[viewGridForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewGridForoosh]
AS
SELECT        dbo.tblRizeForoosh.fldRadif, dbo.tblRizeForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblRizeForoosh.fldSharhKala, dbo.tblRizeForoosh.fldTedadCarton, 
                         dbo.tblRizeForoosh.fldTedadJoz, dbo.tblRizeForoosh.fldFeeJoz, dbo.tblRizeForoosh.fldFeeYekCarton, dbo.tblRizeForoosh.fldFeeKol, 
                         dbo.tblRizeForoosh.fldDarsadTakhfif, dbo.tblRizeForoosh.fldMablaghTakhfif, dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, 
                         dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif, 
                         dbo.tblRizeForoosh.fldMablaghKhales, dbo.tblRizeForoosh.fldCodeAnbar, tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2, 
                         dbo.tblAnbar.fldNameAnbar, tblVahed_2.ID AS fldVahed1Code, dbo.tblVahed.ID AS fldVahed2Code, dbo.tblKala.fldGhymatMasrafkonande
FROM            dbo.tblAnbar INNER JOIN
                         dbo.tblRizeForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeForoosh.fldCodeAnbar INNER JOIN
                         dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
WHERE        (dbo.tblRizeForoosh.fldShomareFactor = 30)



GO
/****** Object:  View [dbo].[viewListBarcodeKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewListBarcodeKala]
AS
SELECT        fldCodeKala, fldSerial
FROM            dbo.tblKala
WHERE        (fldSerial <> '0') AND (fldSerial <> '')



GO
/****** Object:  View [dbo].[viewListKalaInFactorForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewListKalaInFactorForoosh]
AS
SELECT        dbo.tblKala.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2, dbo.tblKala.fldFeeForoosh
FROM            dbo.tblKala INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed1 = dbo.tblVahed.ID INNER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID



GO
/****** Object:  View [dbo].[viewListKalaInFactorForooshByFldCodeGroup]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewListKalaInFactorForooshByFldCodeGroup]
AS
SELECT        dbo.tblKala.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2, dbo.tblKala.fldFeeForoosh, 
                         dbo.tblKala.fldCodeGroup
FROM            dbo.tblKala INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed1 = dbo.tblVahed.ID INNER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID



GO
/****** Object:  View [dbo].[viewListSmallKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewListSmallKala]
AS
SELECT        fldNameKala, fldCodeKala
FROM            dbo.tblKala



GO
/****** Object:  View [dbo].[viewLoadVizitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewLoadVizitor]
AS
SELECT        dbo.tblTafzili.fldCodeTafzili, dbo.tblTafzili.fldSharheTafzili, dbo.tblTafzili.fldPoorsant, dbo.tblTafzili.fldNoeMohasebePoorsant, 
                         CASE WHEN tblTafzili.fldNoeMohasebePoorsant = 1 THEN 'فروش' WHEN tblTafzili.fldNoeMohasebePoorsant = 2 THEN 'واریز' WHEN tblTafzili.fldNoeMohasebePoorsant
                          = 3 THEN 'هدفمند' END AS fldSharheMohasebe, dbo.tblTafzili.fldCodeSuperVisor AS CodeSuperVisor, tblTafzili_1.fldSharheTafzili AS fldNameSuperVizor, 
                         dbo.tblTafzili.ID
FROM            dbo.tblTafzili INNER JOIN
                         dbo.tblTafzili AS tblTafzili_1 ON dbo.tblTafzili.fldCodeSuperVisor = tblTafzili_1.fldCodeTafzili
WHERE        (dbo.tblTafzili.fldNoeVizitor = 2)



GO
/****** Object:  View [dbo].[viewReportListAshkhasByIN]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewReportListAshkhasByIN]
AS
SELECT DISTINCT 
                         TOP (100) PERCENT dbo.tblTafzili.fldCodeGroup, dbo.tblGroupAshkhas.fldGroupName, dbo.tblDaftarcheTell.fldShomare, dbo.tblTafzili.fldCodeTafzili, 
                         dbo.tblTafzili.fldSharheTafzili
FROM            dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup LEFT OUTER JOIN
                         dbo.tblDaftarcheTell ON CONVERT(int, dbo.tblTafzili.fldCodeTafzili) = CONVERT(int, dbo.tblDaftarcheTell.fldCodeTafzili)
GROUP BY dbo.tblTafzili.fldCodeGroup, dbo.tblGroupAshkhas.fldGroupId, dbo.tblGroupAshkhas.fldGroupName, dbo.tblDaftarcheTell.fldShomare, dbo.tblTafzili.fldCodeTafzili, 
                         dbo.tblTafzili.fldSharheTafzili
HAVING        (dbo.tblTafzili.fldCodeGroup IN (108, 100, 105))
ORDER BY dbo.tblGroupAshkhas.fldGroupName, dbo.tblTafzili.fldSharheTafzili



GO
/****** Object:  View [dbo].[viewRizeForooshEhsan]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewRizeForooshEhsan]
AS
SELECT        dbo.tblRizeForoosh.fldShomareSanad, dbo.tblRizeForoosh.fldShomareFactor, dbo.tblRizeForoosh.fldCodeVasete, dbo.tblRizeForoosh.fldCodeTafsili, 
                         dbo.tblTafzili.fldSharheTafzili, dbo.tblRizeForoosh.fldCodeAnbar, dbo.tblAnbar.fldNameAnbar, dbo.tblRizeForoosh.fldCodeKala, dbo.tblKala.fldNameKala, 
                         dbo.tblKala.fldGhymatMasrafkonande, dbo.tblRizeForoosh.fldSharhKala, dbo.tblRizeForoosh.fldTedadCarton, dbo.tblRizeForoosh.fldTedadJoz, 
                         dbo.tblRizeForoosh.fldFeeJoz, dbo.tblRizeForoosh.fldFeeYekCarton, dbo.tblRizeForoosh.fldFeeKol, dbo.tblRizeForoosh.fldDarsadTakhfif, 
                         dbo.tblRizeForoosh.fldMablaghTakhfif, dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, 
                         dbo.tblRizeForoosh.fldArzeshAfzode, dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif, dbo.tblRizeForoosh.fldMablaghKhales, dbo.tblRizeForoosh.fldType, 
                         dbo.tblRizeForoosh.fldDate, dbo.tblGroupKala.fldGroupId, dbo.tblGroupKala.fldGroupName, dbo.tblGroupAshkhas.fldGroupName AS fldGroupNameAshkas2
FROM            dbo.tblRizeForoosh INNER JOIN
                         dbo.tblAnbar ON dbo.tblRizeForoosh.fldCodeAnbar = dbo.tblAnbar.fldCode INNER JOIN
                         dbo.tblTafzili ON dbo.tblRizeForoosh.fldCodeTafsili = dbo.tblTafzili.fldCodeTafzili INNER JOIN
                         dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblGroupKala ON dbo.tblTafzili.fldCodeGroup = dbo.tblGroupKala.fldGroupId LEFT OUTER JOIN
                         dbo.tblGroupAshkhas ON dbo.tblRizeForoosh.fldCodeVasete = dbo.tblGroupAshkhas.fldGroupId



GO
/****** Object:  View [dbo].[viewShowListKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewShowListKala]
AS
SELECT        dbo.tblKala.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2, dbo.tblKala.fldFeeForoosh, 
                         dbo.tblKala.fldCodeGroup
FROM            dbo.tblKala INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed1 = dbo.tblVahed.ID INNER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID



GO
/****** Object:  View [dbo].[viewShowListSmallGroupKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewShowListSmallGroupKala]
AS
SELECT        fldGroupId, fldGroupName
FROM            dbo.tblGroupKala



GO
/****** Object:  View [dbo].[viewtestforoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewtestforoosh]
AS
SELECT        dbo.tblRizeForoosh.fldCodeTafsili, tblTafzili_1.fldSharheTafzili, dbo.tblRizeForoosh.fldCodeVasete, dbo.tblTafzili.fldSharheTafzili AS fldNameVasete
FROM            dbo.tblRizeForoosh LEFT OUTER JOIN
                         dbo.tblTafzili ON dbo.tblRizeForoosh.fldCodeVasete = dbo.tblTafzili.fldCodeTafzili LEFT OUTER JOIN
                         dbo.tblTafzili AS tblTafzili_1 ON dbo.tblRizeForoosh.fldCodeTafsili = tblTafzili_1.fldCodeTafzili



GO
/****** Object:  View [dbo].[viewtestShomaretel]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viewtestShomaretel]
AS
SELECT        dbo.tblTafzili.fldCodeTafzili, dbo.tblDaftarcheTell.fldCodeTafzili AS Expr1, dbo.tblDaftarcheTell.fldName, dbo.tblDaftarcheTell.fldShomare, 
                         dbo.tblDaftarcheTell.fldSms, dbo.tblDaftarcheTell.fldSharh, dbo.tblTafzili.fldOstan
FROM            dbo.tblTafzili INNER JOIN
                         dbo.tblDaftarcheTell ON dbo.tblTafzili.fldCodeTafzili = dbo.tblDaftarcheTell.fldCodeTafzili



GO
/****** Object:  View [dbo].[viwListeCheckHayePasNashode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[viwListeCheckHayePasNashode]
AS
SELECT        TOP (100) PERCENT ID, fldShomareCheck, fldShomareHesab, fldDateSarResid, SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 1, 4) 
                         + '/' + SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 7, 2) AS fldDateSarResidShow, 
                         fldCodeBank, fldShobe, fldMablagh, fldDatePardakht, SUBSTRING(CONVERT(nvarchar, fldDatePardakht), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, 
                         fldDatePardakht), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, fldDatePardakht), 7, 2) AS fldDatePardakhtShow, fldCodeSahebeCheck, fldShomarePardakht, 
                         fldCodeSahebeHesab, fldVasete, fldSanad, fldShomarePass, fldDatePass, fldSharhePass
FROM            dbo.tblCheckePardakhti
WHERE        (fldShomarePass IS NULL)
ORDER BY fldDateSarResid



GO
/****** Object:  StoredProcedure [dbo].[aaaa]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




CREATE proc [dbo].[aaaa]

@NameVahed nvarChar(70),
@mmm nvarchar(30),
@IDOutPut int outPut
as 

begin

	insert into tblTanzimatDynamic 
		values(@NameVahed,@mmm)	
	set @IDOutPut=@@IDENTITY
	
end

GO
/****** Object:  StoredProcedure [dbo].[AddRecordTanzimatForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[AddRecordTanzimatForoosh]
	       @fldKalaTekrary bit
	       ,@fldTypeInputKalaInGrid nvarchar(10)
           ,@fldAllowForooshKalaBishAzMojodi bit
           ,@fldShowMotalebatInFactorForoosh bit
           ,@fldForooshNaghdi bit
           ,@fldShowSodKala bit
           ,@fldShowSodFactor bit
           ,@fldEshantion bit
           ,@fldChandGheymate bit
           ,@fldArzeshAfzode decimal(18,4)
           ,@fldTedadChapInForoosh smallint
           ,@fldSearchNumberingInKala bit
           ,@fldBeginDateTakhfifat bigint
           ,@fldEndDateTakhfifat bigint
		   ,@intResult smallint output
AS
BEGIN
	begin transaction AddRecord
		begin try
		--Delete 
		delete from tblTanzimatForoosh
		--insert
			INSERT INTO [dbo].[tblTanzimatForoosh]
           ([fldKalaTekrary]
           ,[fldTypeInputKalaInGrid]
           ,[fldAllowForooshKalaBishAzMojodi]
           ,[fldShowMotalebatInFactorForoosh]
           ,[fldForooshNaghdi]
           ,[fldShowSodKala]
           ,[fldShowSodFactor]
           ,[fldEshantion]
           ,[fldChandGheymate]
           ,[fldArzeshAfzode]
           ,[fldTedadChapInForoosh]
           ,[fldSearchNumberingInKala]
           ,[fldBeginDateTakhfifat]
           ,[fldEndDateTakhfifat])
     VALUES
           (@fldKalaTekrary
	       ,@fldTypeInputKalaInGrid
           ,@fldAllowForooshKalaBishAzMojodi
           ,@fldShowMotalebatInFactorForoosh
           ,@fldForooshNaghdi
           ,@fldShowSodKala
           ,@fldShowSodFactor
           ,@fldEshantion
           ,@fldChandGheymate
           ,@fldArzeshAfzode
           ,@fldTedadChapInForoosh
           ,@fldSearchNumberingInKala
           ,@fldBeginDateTakhfifat
           ,@fldEndDateTakhfifat)
			set @intResult=1
			commit transaction AddRecord
		end try
		begin catch
			if @@TranCount>0
			begin
				set @intResult=0
				rollback transaction AddRecord;
			end
		end catch
END

GO
/****** Object:  StoredProcedure [dbo].[AmalkardBankInTblCheckPardakhti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[AmalkardBankInTblCheckPardakhti] 
	-- Add the parameters for the stored procedure here
	@CodeBank bigint,
	@Amalkard nvarchar(10) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	declare @intCount bigint

	set @intCount=(Select Count(fldCodeBank) from tblCheckePardakhti Where fldCodeBank=@CodeBank)
	if @intCount>0
		set @Amalkard='true'
	else
		set @Amalkard='false'
END

GO
/****** Object:  StoredProcedure [dbo].[AmalkardCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[AmalkardCheck] 
	-- Add the parameters for the stored procedure here
	@ShomareCheck bigint,
	@ShomareHesab bigint,	
	@ShomareDaryaft bigint,
	@Amalkard nvarchar(20) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @cnt bigint
    -- Insert statements for procedure here
	set @cnt= (Select count(fldSHomareCheck) from tblCheckeDaryafti Where fldShomareCheck=@ShomareCheck and fldShomareHesab=@ShomareHesab and fldShomareDaryaft=@ShomareDaryaft and fldVaziat is not null)
	
	IF @cnt>0 
		set @Amalkard ='true'
	else
		begin			
			set @cnt= (Select count(fldSHomareCheck) from tblCheckeDaryafti Where fldShomareCheck=@ShomareCheck and fldShomareHesab=@ShomareHesab and fldShomareDaryaft=@ShomareDaryaft and (fldShomareVosool  is not null or fldShomarePardakht  is not null))
			if @cnt>0 
				set @Amalkard ='true'
			else
				set @Amalkard ='false'
		end
END

GO
/****** Object:  StoredProcedure [dbo].[AmalkardCheckForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[AmalkardCheckForoosh]
	-- Add the parameters for the stored procedure here
	@ShomareDaryaft bigint,
	@Amalkard nvarchar(15) output

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
		if @ShomareDaryaft=0
			begin
				set @amalkard='false'
				return
			end

	declare @cnt bigint
    -- Insert statements for procedure here
	set @cnt= (Select count(fldSHomareCheck) from tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft and fldVaziat is not null)
	
	IF @cnt>0 
		set @Amalkard ='true'
	else
		begin			
			set @cnt= (Select count(fldSHomareCheck) from tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft and (fldShomareVosool  is not null or fldShomarePardakht  is not null))
			if @cnt>0 
				set @Amalkard ='true'
			else
				set @Amalkard ='false'
		end
END

GO
/****** Object:  StoredProcedure [dbo].[AmalkardCheckkharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[AmalkardCheckkharid]
	-- Add the parameters for the stored procedure here
	@ShomarePardakht bigint,
	@Amalkard nvarchar(15) output

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
		if @ShomarePardakht=0
		begin
			set @amalkard='false'
			return
		end


	declare @cnt bigint
    -- Insert statements for procedure here
	set @cnt= (Select count(fldSHomareCheck) from tblCheckePardakhti Where fldShomarePardakht=@ShomarePardakht and isnumeric(fldShomarePass)=1 )
	
	IF @cnt>0 
		set @Amalkard ='true'
	else		
		set @Amalkard ='false'
END

GO
/****** Object:  StoredProcedure [dbo].[BahayeTamamShodeForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[BahayeTamamShodeForoosh]
--چندزبانه
@lngDateFrom bigint,
@lngDateUntil bigint

as 
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END

begin

create table #TempMojoodieAnbar(

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 4),
fldTedadeKhorooj decimal(18, 4),
fldTedadeJoz decimal(18, 4), 
fldTedadeKol decimal(18, 4), 
fldTedadDarKarton decimal(18, 4),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 4),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20) COLLATE Persian_100_CI_AS
)


create table #TempMojoodieAnbarForoosh(

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 4),
fldTedadeKhorooj decimal(18, 4),
fldTedadeJoz decimal(18, 4), 
fldTedadeKol decimal(18, 4), 
fldTedadDarKarton decimal(18, 4),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 4),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20) COLLATE Persian_100_CI_AS
)

create table #TempShowMojoodiForoosh(

fldCodeKala bigint,
SumVorood decimal(18, 4),
SumKhorooj decimal(18, 4),
SumMande decimal(18, 4),
BahayeTamamShode decimal(18, 4),
fldTedadDarKarton decimal(18, 4),
fldTedadDarKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadVoroodKarton decimal(18, 4),
fldTedadVoroodKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadKhoroojKarton decimal(18, 4),
fldTedadKhoroojKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh decimal(18, 4),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldKharid decimal (18, 4),
fldArzesheMojoodi decimal (18, 4)
)

create table #TempShowMojoodi(

fldCodeKala bigint,
SumVorood decimal(18, 4),
SumKhorooj decimal(18, 4),
SumMande decimal(18, 4),
fldTedadDarKarton decimal(18, 4),
fldTedadDarKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadVoroodKarton decimal(18, 4),
fldTedadVoroodKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadKhoroojKarton decimal(18, 4),
fldTedadKhoroojKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh decimal(18, 4),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldKharid decimal (18, 4),
fldArzesheMojoodi decimal (18, 4)
)

delete #TempMojoodieAnbar
delete #TempMojoodieAnbarForoosh
delete #TempShowMojoodiForoosh
delete #TempShowMojoodi

INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeKharid

INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzForoosh


INSERT INTO #TempMojoodieAnbarForoosh
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzForoosh

INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeForoosh



INSERT INTO #TempMojoodieAnbarForoosh
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeForoosh

INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzKharid


------------Insert into #TempMojoodieAnbar
------------	(fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
------------		fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
------------SELECT        'NameGroup',fldCodeGroup, fldCodeKala, fldNameKala ,0,0 , - 1 , - 1 , 0, '' , fldNogheSefaresh, fldFeeForoosh, 
------------                         0, 0, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,0,''
------------FROM            tblKala
------------	where fldCodeKala Not In (Select fldCodeKala from #TempMojoodieAnbar)



------------UPDATE       #TempMojoodieAnbar
------------SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
------------FROM            tblKala INNER JOIN
------------                         #TempMojoodieAnbar ON tblKala.fldCodeKala = #TempMojoodieAnbar.fldCodeKala



------------UPDATE       #TempMojoodieAnbar
------------SET                #TempMojoodieAnbar.fldGroupName = tblGroupKala.fldGroupName
------------FROM            tblGroupKala INNER JOIN
------------                         #TempMojoodieAnbar ON #TempMojoodieAnbar.fldCodeGroup = tblGroupKala.fldGroupId


------------UPDATE       #TempMojoodieAnbar
------------SET                fldNameAnbar = tblAnbar.fldNameAnbar
------------FROM            tblAnbar INNER JOIN
------------                         #TempMojoodieAnbar ON tblAnbar.fldCode = #TempMojoodieAnbar.fldCodeAnbar



----------update #tempMojoodieAnbar set #tempMojoodieAnbar.fldNameVahed=tbl2.fldNameVahed,#tempMojoodieAnbar.fldNameVahed2=tbl2.fldNameVahed2
----------from
----------(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
----------FROM            tblVahed INNER JOIN
----------                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
----------                                FROM            tblKala INNER JOIN
----------                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

----------where tbl2.fldCodeKala=#tempMojoodieAnbar.fldCodeKala




Insert into #TempShowMojoodi
	(SumVorood,SumKhorooj,SumMande, fldCodeKala,    fldKharid)
SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, SUM(fldTedadeVorood - fldTedadeKhorooj) AS sumMande, 
                          fldCodeKala,0
						 FROM            #TempMojoodieAnbar
GROUP BY fldCodeKala



delete #TempMojoodieAnbarForoosh where fldDate<@lngDateFrom 
delete #TempMojoodieAnbarForoosh where fldDate>@lngDateUntil


Insert into #TempShowMojoodiForoosh
	(SumVorood,SumKhorooj,SumMande, fldCodeKala,fldKharid)
SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, SUM(fldTedadeVorood - fldTedadeKhorooj) AS sumMande,fldCodeKala,0
FROM            #TempMojoodieAnbarForoosh
GROUP BY fldCodeKala




--set @MiangineKharid=0


update #TempShowMojoodi set fldKharid=tbl1.Miangin from
(SELECT        ROUND(SUM(fldFeeVahed1BadAzTakhfif*fldTedadJoz) / SUM(fldTedadJoz), 0) AS Miangin, fldCodeKala
FROM            tblRizeKharid
GROUP BY fldCodeKala
having  SUM(fldTedadJoz)>0
) as tbl1
where tbl1.fldCodeKala=#TempShowMojoodi.fldCodeKala



update #TempShowMojoodiForoosh set BahayeTamamShode=-1*(#TempShowMojoodi.fldKharid*#TempShowMojoodiForoosh.SumMande) from #TempShowMojoodi
	where #TempShowMojoodiForoosh.fldCodeKala=#TempShowMojoodi.fldCodeKala


Select SUM(BahayeTamamShode) as SumBahayeTamamShodeForooshrafte from #TempShowMojoodiForoosh

--select * from #TempShowMojoodiForoosh
----موجودی مثبت
--if @Filter=2
--		delete from #TempShowMojoodi Where  SumMande <=0
		

----موجودی منفی
--if @Filter=3
--		delete from #TempShowMojoodi Where  SumMande >=0

----موجودی صفر
--if @Filter=4
--		delete from #TempShowMojoodi Where  SumMande <>0


--محاسبه ارزش موجودی هر کالا
--update #TempShowMojoodi set fldArzesheMojoodi=0
--update #TempShowMojoodi set fldArzesheMojoodi=SumMande*fldKharid where SumMande>0

----محاسبه میانگین خرید هرکالا
--set @MiangineKharid= (Select Sum(fldKharid*SumMande) as SumMiangin from #TempShowMojoodi Where SumMande>0)


--if   @MiangineKharid is null
--	set @MiangineKharid=0

	

	

--Set @JameTedad=(Select Sum(SumMande)  from #TempShowMojoodi Where SumMande>0)

--if   @JameTedad is null
--	set @JameTedad=0

--			--if @ListGroupAshkhas<>''
--		 --     begin
--		 --      declare @StrDeleteGroupAshkhas varchar(5000)
--		 --       set  @StrDeleteGroupAshkhas='delete from #TempRizeForoosh 
--		 --           where '+ @ListGroupAshkhas + ''
--			--		execute(@StrDeleteGroupAshkhas)
--		 --     end


-- declare @StrOrder nvarchar(50)
-- declare @Select nvarchar(500)



-- Set @Select='Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
--	fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid,fldArzesheMojoodi from #TempShowMojoodi '

----Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
--	--fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid ,@strorder
--	--from #TempShowMojoodi

--if @Order=0 
--	set @StrOrder=' order by fldCodeKala'
--else if @order=1 	
--	set @StrOrder=' order by fldNameKala'
--else if @order=2 
--	set @StrOrder=' order by SumMande desc'
--else if @order=3 
--	set @StrOrder=' order by SumMande asc'
--else if @order=4 
--	set @StrOrder=' order by fldKharid desc'	
--else if @order=5 
--	set @StrOrder=' order by fldKharid asc'
--else if @Order=6
--	set @StrOrder=' order by fldGroupName desc'
--else if @Order=7
--	set @StrOrder=' order by fldCodeGroup asc'

--execute(@select + @StrOrder)

	
end

GO
/****** Object:  StoredProcedure [dbo].[bbbb]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[bbbb]
--@NameHesab nvarChar(50),
@Meghdar bigint OutPut


as

begin
	--set @Meghdar=1020
	Select *
		Into   #Temp
		From   tblSodooreSanad

		Declare @Id int,@Jam bigint
		set @Jam=0

		While (Select Count(*) From #Temp) > 0
		Begin

			Select Top 1 @Id = Id From #Temp

			--Do some processing here

			--Delete #Temp Where Id = @Id
			set @jam=@Jam+( Select fldBedehkar from #Temp Where Id = @Id)
		End
		set @Meghdar=@jam

end

GO
/****** Object:  StoredProcedure [dbo].[ChapSanad2]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ChapSanad2]
@Shomaresanad nvarchar(50)

as
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END

	create  table #Sanad
		(
		[fldCodeKol] [nvarchar](50) COLLATE Persian_100_CI_AS NULL,
		[SharheKol]  [nvarchar](50) COLLATE Persian_100_CI_AS NULL,
		[fldCodeMoin] [nvarchar](10) COLLATE Persian_100_CI_AS NULL,
		[SharheMoin]  [nvarchar](50) COLLATE Persian_100_CI_AS NULL,
		[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS NOT NULL,
		[SharheTafzili]  [nvarchar](200) COLLATE Persian_100_CI_AS NULL,
		[fldCodeMarkaz1] [nvarchar](50) COLLATE Persian_100_CI_AS NULL,
		[SharheMarkaz1] [nvarchar](50) COLLATE Persian_100_CI_AS NULL,
		[fldCodeMarkaz2] [nvarchar](50) COLLATE Persian_100_CI_AS NULL,
		[SharheMarkaz2] [nvarchar](50) COLLATE Persian_100_CI_AS NULL,
		[fldBedehkar] [decimal](18, 4) NULL,
		[fldBestankar] [decimal](18, 4) NULL,
		[fldSharh] [nvarchar](150) NULL,
		[fldDate] [int] NULL,
		[fldCodeNoeTafzili] [nvarchar](50) COLLATE Persian_100_CI_AS NULL,
		)

begin
	
	insert into #Sanad
		([fldCodeKol],[fldCodeMoin] ,[fldCodeTafzili],[fldCodeMarkaz1],[fldCodeMarkaz2],[fldBedehkar],[fldBestankar],[fldSharh],fldDate,[fldCodeNoeTafzili] )
		Select [fldCodeKol],[fldCodeMoin] ,[fldCodeTafzili],[fldCodeMarkaz1],[fldCodeMarkaz2],[fldBedehkar],[fldBestankar],[fldSharh],fldDate,[fldCodeNoeTafzili]  from tblSodooreSanad Where fldSanad=@Shomaresanad

	update #sanad set SharheTafzili=tblTafzili.fldSharheTafzili from tblTafzili Where #Sanad.fldCodeTafzili=tblTafzili.fldCodeTafzili And #Sanad.fldCodeNoeTafzili=tblTafzili.fldCodeNoe
	update #sanad set SharheKol=tblKolMoin.fldSharheHesab from tblKolMoin Where #Sanad.fldCodeKol=tblKolMoin.fldCodeHesab
	update #sanad set SharheMoin=tblKolMoin.fldSharheHesab from tblKolMoin Where #Sanad.fldCodeKol+ #Sanad.fldCodeMoin =tblKolMoin.fldCodeHesab
	update #sanad set SharheMarkaz1=tblMarkaz1.fldSharh from tblMarkaz1 Where #Sanad.fldCodeMarkaz1=tblMarkaz1.fldSharh
	update #sanad set SharheMarkaz1=tblMarkaz2.fldSharh from tblMarkaz2 Where #Sanad.fldCodeMarkaz2=tblMarkaz2.fldSharh

	Select  [fldCodeKol],[SharheKol],[fldCodeMoin],[SharheMoin],[fldCodeTafzili],[SharheTafzili],[fldCodeMarkaz1],[SharheMarkaz1],[fldCodeMarkaz2],[SharheMarkaz2],[fldBedehkar],[fldBestankar],[fldSharh],[fldDate],[fldCodeNoeTafzili] from #Sanad
	--select * from #Sanad

end
GO
/****** Object:  StoredProcedure [dbo].[Check_AmalKard_IDGroupAshkhas_TblAshkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Check_AmalKard_IDGroupAshkhas_TblAshkhas]
@fldGroupAshkhasId bigint,

@flagCheckIdInTblAshkhas int output  --چک کردن ای دی وارد شده ایا در عمکرد کالا سابقه دارد یا خیر new

as


begin
-------------------------------------------------------------------new 95-09-03
declare @hasRowInTblAshkhas int --new

set @hasRowInTblAshkhas=0 --new 
set @flagCheckIdInTblAshkhas=0 -- new 
set @hasRowInTblAshkhas=(select count(fldCodeGroup) from tblTafzili where fldCodeGroup=@fldGroupAshkhasId)-- new 
if (@hasRowInTblAshkhas >= 1) --new 
begin
set @flagCheckIdInTblAshkhas=1
return 
end 
------------------------------------------------------ new 95-09-03


			 
end

GO
/****** Object:  StoredProcedure [dbo].[Check_AmalKard_IDGroupKalaIN_TblKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Check_AmalKard_IDGroupKalaIN_TblKala]
@fldGroupKalaId bigint,

@flagCheckIdInTblKala int output  --چک کردن ای دی وارد شده ایا در عمکرد کالا سابقه دارد یا خیر new

as


begin
-------------------------------------------------------------------new 95-09-03
declare @hasRowInTblKala int --new

set @hasRowInTblKala=0 --new 
set @flagCheckIdInTblKala=0 -- new 
set @hasRowInTblKala=(select count(fldCodeGroup) from tblKala where fldCodeGroup=@fldGroupKalaId)-- new 
if (@hasRowInTblKala >= 1) --new 
begin
set @flagCheckIdInTblKala=1
return 
end 
------------------------------------------------------ new 95-09-03


			 
end

GO
/****** Object:  StoredProcedure [dbo].[Check_AmlKard_CodeTafzili_For_Delete_In_Tbl_Tafzili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Check_AmlKard_CodeTafzili_For_Delete_In_Tbl_Tafzili]
--چندزبانه
@fldCodeTafzili	nvarchar(10),
@flagDelete_Tafzili int output	 
as
begin
declare @hasrowinSanad int

set @hasrowinSanad=(select count(fldCodeTafzili) from tblSodooreSanad  where fldCodeTafzili=@fldCodeTafzili)

if (@hasrowinSanad > 0)
	begin
		set @flagDelete_Tafzili=1
		return
	end
else
	begin
		set @hasrowinSanad=(select count(fldCodeTafsili) from tblRizePishForoosh  where fldCodeTafsili=@fldCodeTafzili)
		if (@hasrowinSanad > 0)
			begin
				set @flagDelete_Tafzili=1
				return
			end
		else
			begin
				set @hasrowinSanad=(select count(fldCodeTafsili) from tblRizePishPishForoosh  where fldCodeTafsili=@fldCodeTafzili)
				if (@hasrowinSanad > 0)
					begin
						set @flagDelete_Tafzili=1
						return
					end
			end

	end
end

GO
/****** Object:  StoredProcedure [dbo].[Check_CodeTafzili_Delete_Tbl_Tafzili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Check_CodeTafzili_Delete_Tbl_Tafzili]
@fldCodeTafzili	nvarchar(10),
@flagDelete_Tafzili int output	 
as
begin
declare @hasrowinSanad int

set @hasrowinSanad=(select count(fldCodeTafzili) from tblSodooreSanad  where fldCodeTafzili=@fldCodeTafzili)

if (@hasrowinSanad > 0)
begin
set @flagDelete_Tafzili=1
return
end
end

GO
/****** Object:  StoredProcedure [dbo].[checkIdNumber_GruopAshkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[checkIdNumber_GruopAshkhas]
@LenidNumber nvarchar(20),
@idNumber nvarchar(20)
--@ReturnID bigint output
	
AS
BEGIN
	               select Max(fldGroupId) from tblGroupAshkhas
	               where LEN(convert(nvarchar(20),fldGroupId))=@LenidNumber
				   and   convert(nvarchar(20),fldGroupId) like @idNumber + '%'
     --Return @ReturnID
END

GO
/****** Object:  StoredProcedure [dbo].[checkIdNumber_GruopKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[checkIdNumber_GruopKala]
@LenidNumber nvarchar(20),
@idNumber nvarchar(20)
--@ReturnID bigint output
	
AS
BEGIN
	               select Max(fldGroupId) from tblGroupKala
	               where LEN(convert(nvarchar(20),fldGroupId))=@LenidNumber
				   and   convert(nvarchar(20),fldGroupId) like @idNumber + '%'
     --Return @ReturnID
END

GO
/****** Object:  StoredProcedure [dbo].[CodeKolHesab]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[CodeKolHesab]
@NameHesab nvarChar(50),
@Meghdar nvarChar(100) OutPut

as

begin
	if @NameHesab='bank' 
		begin
			set @Meghdar=(Select  fldCodeKolBank from tblTanzimat )
		end
	else if @NameHesab='sandoogh' 
		begin
			set @Meghdar=(Select  fldCodeKolSandoogh from tblTanzimat )
		end
	else if @NameHesab='check' 
		begin
			set @Meghdar=(Select  fldCodeKolCheck from tblTanzimat )
		end
	else if @NameHesab='checkpardakhti' 
		begin
			set @Meghdar=(Select  fldCodeKolCheckPardakhti from tblTanzimat )
		end
	else if @NameHesab='hesabhayedaryaftani' 
		begin
			set @Meghdar=(Select  fldCodeMoinHesabhayeDaryaftani from tblTanzimat )
		end		
	else if @NameHesab='kolhesabhayedaryaftani' 
		begin
			set @Meghdar=(Select  fldCodeKolHesabhayeDaryaftani from tblTanzimat )
		end	
	else if @NameHesab='kolhesabhayepardakhtani' 
		begin
			set @Meghdar=(Select  fldCodeKolHesabhayePardakhtani from tblTanzimat )
		end		
	else if @NameHesab='daramad' 
		begin
			set @Meghdar=(Select  fldCodeKolDaramad from tblTanzimat )
		end	
	else if @NameHesab='hazine' 
		begin
			set @Meghdar=(Select  fldCodeKolHazine from tblTanzimat )
		end	
	else if @NameHesab='sarmaye'
		begin
			set @Meghdar=(Select  fldKolSarmaye from tblTanzimat )
		end	
	else if @NameHesab='tarafeentezami'
		begin
			set @Meghdar=(Select  fldkolTarafEntezami from tblTanzimat )		
		end 

end

GO
/****** Object:  StoredProcedure [dbo].[CodeMoinHesab]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[CodeMoinHesab]
@NameHesab nvarChar(50),
@Meghdar nvarChar(100) OutPut

as

begin
	if @NameHesab='check' 
		begin
			set @Meghdar=(Select  fldCodeMoinCheck from tblTanzimat )
		end
	else if @NameHesab='checkvagozaripersonel' 
		begin
			set @Meghdar=(Select  fldMoinCheckVagozariPersonel from tblTanzimat )
		end
	else if @NameHesab='checkpardakhti' 
		begin
			set @Meghdar=(Select  fldCodeMoinCheckPardakhti from tblTanzimat )
		end
	else if @NameHesab='takhfifatedaryaft' 
		begin
			set @Meghdar=(Select  fldCodeMoinTakhfifateDaryaft from tblTanzimat )								  
		end
	else if @NameHesab='hesabhayedaryaftani' 
		begin
			set @Meghdar=(Select  fldCodeMoinHesabhayeDaryaftani from tblTanzimat )
		end
	else if @NameHesab='hesabhayepardakhtani' 
		begin
			set @Meghdar=(Select  fldCodeMoinHesabhayePardakhtani from tblTanzimat )
		end
	else if @NameHesab='hesabhayepardakhtanipoorsant' 
		begin
			set @Meghdar=(Select  fldCodeMoinHesabhayePardakhtaniPoorsant from tblTanzimat )
		end		
	else if @NameHesab='takhfifatepardakht' 
		begin
			set @Meghdar=(Select  fldCodeMoinTakhfifatePardakht from tblTanzimat )								  
		end
	else if @NameHesab='daramad' 
		begin
			set @Meghdar=(Select  fldCodeMoinDaramad from tblTanzimat )								  
		end
	else if @NameHesab='foroosh'
		begin
			set @Meghdar=(Select  fldCodeMoinForoosh from tblTanzimat )		
		end 
	else if @NameHesab='bargashtazforoosh'
		begin
			set @Meghdar=(Select  fldCodeMoinBargashtAzForoosh from tblTanzimat )		
		end 		
	else if @NameHesab='hamlvanaghl'
		begin
			set @Meghdar=(Select  fldMoinDaramadeHamlVaNaghl from tblTanzimat )		
		end 
	else if @NameHesab='arzesheafzoode'
		begin
			set @Meghdar=(Select  fldMoinArzesheAfzoode from tblTanzimat )		
		end 
	else if @NameHesab='mojoodieebteda'
		begin
			set @Meghdar=(Select  fldMoinMojoodieEbteda from tblTanzimat )
		end	
	else if @NameHesab='arzesheafzoodekharid'
		begin
			set @Meghdar=(Select  fldMoinArzesheAfzoodeKharid from tblTanzimat )		
		end 
	else if @NameHesab='kharid'
		begin
			set @Meghdar=(Select  fldCodeMoinKharid from tblTanzimat )		
		end 
	else if @NameHesab='hazinehamlvanaghlekharid'
		begin
			set @Meghdar=(Select  fldCodeMoinHazineHamlVaNaghl from tblTanzimat )		
		end 
	else if @NameHesab='hoghoogh'
		begin
			set @Meghdar=(Select  fldCodeHoghoogh from tblTanzimat )		
		end 
	else if @NameHesab='forooshamani'
		begin
			set @Meghdar=(Select  fldMoinAmani from tblTanzimat )
		end		
	else if @NameHesab='checkpersonel'
		begin
			set @Meghdar=(Select  fldMoinCheckPersonel from tblTanzimat )
		end 
	else if @NameHesab='karmozdevam'
		begin
			set @Meghdar=(Select  fldMoinKarmozdeVam from tblTanzimat )
		end 
	else if @NameHesab='hoghooghesabet'
		begin
			set @Meghdar=(Select  fldMoinHoghoogh from tblTanzimat )
		end 	
end

GO
/****** Object:  StoredProcedure [dbo].[dastrasiUser]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[dastrasiUser] 
	@CodeUser bigint
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	
	create  table #TempTable2(
		fldCodeUser bigint,
		fldUserName nvarchar(25) COLLATE Persian_100_CI_AS,
		fldCodeDastrasi bigint ,
		fldName nvarchar(150) COLLATE Persian_100_CI_AS,
		fldGroup bigint,
		fldOrder bigint,
		fldNameGroup nvarchar(40) COLLATE Persian_100_CI_AS,
		fldCheck int


	)

	insert into #TempTable2
		(fldCodeUser,fldCodeDastrasi,fldName,fldGroup,fldOrder,fldUserName,fldNameGroup,fldCheck)	
	SELECT        tblDastrasi.fldCodeUser, tblDastrasi.fldCodeDastrasi, tblListeDastrasi.fldName, tblListeDastrasi.fldGroup, tblListeDastrasi.fldOrder, tblSecurity.fldUser,tblListeDastrasi.fldNameGroup,1
		FROM            tblDastrasi INNER JOIN
								 tblSecurity ON tblDastrasi.fldCodeUser = tblSecurity.fldCode INNER JOIN
								 tblListeDastrasi ON tblDastrasi.fldCodeDastrasi = tblListeDastrasi.fldCode
										where tblDastrasi.fldCodeUser=@CodeUser


	insert into #TempTable2
		(fldCodeUser,fldCodeDastrasi,fldName,fldGroup,fldOrder,fldUserName,fldNameGroup, fldCheck)
		Select @CodeUser,fldCode,fldName,fldGroup,fldOrder,'',fldNamegroup, 0
			from tblListeDastrasi 
				Where fldCode not in (Select fldCodedastrasi from tblDastrasi Where fldCodeUser =@CodeUser)
		
		
		
			--SELECT        tblDastrasi.fldCodeUser, tblDastrasi.fldCodeDastrasi, tblListeDastrasi.fldName, tblListeDastrasi.fldGroup, tblListeDastrasi.fldOrder, tblSecurity.fldUser,0
			--FROM            tblDastrasi INNER JOIN
			--					 tblSecurity ON tblDastrasi.fldCodeUser = tblSecurity.fldCode INNER JOIN
			--					 tblListeDastrasi ON tblDastrasi.fldCodeDastrasi = tblListeDastrasi.fldCode
			--							where fldCodeDastrasi Not In (Select fldCodeDastrasi from tblDastrasi Where fldCodeUser=@CodeUser)
				

	select fldCodeUser, fldCodeDastrasi, fldName, fldGroup, fldOrder,fldNameGroup, fldCheck from #TempTable2

END

GO
/****** Object:  StoredProcedure [dbo].[DataMokhtalef]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[DataMokhtalef] 
--چندزبانه
@namedarkhast nvarchar(50),
@data nvarchar(50),
@Meghdarstring nvarchar(50) output

as

begin
	if @namedarkhast='nametafzili'
		begin
			set @Meghdarstring	 =	(select fldSharheTafzili from tblTafzili Where fldCodeTafzili=@data)
		end
end

GO
/****** Object:  StoredProcedure [dbo].[Date6Raghami]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Date6Raghami]

@DateKamel bigint,
@DateSmall bigint Output
as

begin	
	declare @strDateKamel varchar(10),@strDateSmall varchar(10)
	set @strDateKamel=convert(varchar,@DateKamel)

	set @strDateSmall=substring( @strDateKamel,3,6)
	set @DateSmall=CONVERT(bigint,@strDateSmall)
	--select @strDateSmall from tblsodooreSanad
end

GO
/****** Object:  StoredProcedure [dbo].[Date9Raghami]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Date9Raghami]

@DateSmall int,
@DateKamel int Output
as

begin	
	declare @strDateSmall varchar(10),@strDateKamel varchar(10)
	set @strDateSmall=convert(varchar,@DateSmall)


	declare @PishSal nvarchar(70)
	set @PishSal=(Select fldMeghdar from tblTanzimateKol Where fldType='pishsal')
	if isnumeric(@PishSal )=0
		set @PishSal=13

	if len(convert(nvarchar(20),@DateSmall))<8
		begin
			set @strDateKamel=@PishSal + @strDateSmall
			set @DateKamel=CONVERT(int,@strDateKamel)
		end 
	else
		set @DateKamel=@DateSmall

end

GO
/****** Object:  StoredProcedure [dbo].[DateToShow]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DateToShow]
	-- Add the parameters for the stored procedure here
	
	@strDate nvarchar(10),
	@NewDate nvarchar(15) output
AS
BEGIN
	--declare @str1 nvarchar(15),@strTest nvarchar(15)
	--set @strTest ='960601'
	--set @str =(Select '/' + SUBSTRING('960601', 2,2))


	if len(@strDate)=6 -- بدون 13 اول
		begin
			set @NewDate = (Select Substring(@strDate,1,2) + '/')
			set @NewDate =(select @NewDate +  Substring(@strDate,3,2) + '/' )


		end

END

GO
/****** Object:  StoredProcedure [dbo].[DateToShow6Va9Ragham]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[DateToShow6Va9Ragham]
	-- Add the parameters for the stored procedure here
	
	@strDate nvarchar(10),
	@NewDate nvarchar(15) output
AS
BEGIN
	--declare @str1 nvarchar(15),@strTest nvarchar(15)
	--set @strTest ='960601'
	--set @str =(Select '/' + SUBSTRING('960601', 2,2))


	if len(@strDate)=6 -- بدون 13 اول
		begin
			set @NewDate = (Select Substring(@strDate,1,2) + '/')
			set @NewDate =(select @NewDate +  Substring(@strDate,3,2) + '/' )
			set @NewDate =(select @NewDate +  Substring(@strDate,5,2) )

		end

END

GO
/****** Object:  StoredProcedure [dbo].[Delete_Tbl_DaftarcheTell]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Delete_Tbl_DaftarcheTell]
@fldIdTell	bigint,
@flagDelete int output	
as
begin


Delete  from tblDaftarcheTell
where fldIdDaftarche=@fldIdTell
set @flagDelete=1
end

GO
/****** Object:  StoredProcedure [dbo].[Delete_Tbl_Tafzili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Delete_Tbl_Tafzili]
@fldCodeTafzili	nvarchar(10),
@flagDelete_Tafzili int output	 
as
begin
declare @hasrowinSanad int

set @hasrowinSanad=(select count(fldCodeTafzili) from tblSodooreSanad  where fldCodeTafzili=@fldCodeTafzili)

if (@hasrowinSanad > 0)
begin
set @flagDelete_Tafzili=1
return
end
 
if(@hasrowinSanad=0)
begin

Delete from tblTafzili 
where fldCodeTafzili=convert(bigint,@fldCodeTafzili)

Delete  from tblDaftarcheTell
where fldCodeTafzili=CONVERT(bigint,@fldCodeTafzili)
set @flagDelete_Tafzili=0
end
end

GO
/****** Object:  StoredProcedure [dbo].[DeleteAllInfo]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[DeleteAllInfo] 
	-- Add the parameters for the stored procedure here
	@Anbar int,
	@Kala int,
	@DaftarcheTell int,
	@Ashkhas int,	
	@BankeMoshtari int
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	Delete from tblSodooreSanad
	delete from tblForoosh
	Delete from tblRizeForoosh
	delete from tblBargashtAzForoosh
	delete from tblRizeBargashtAzForoosh
	delete from tblKharid
	delete from tblRizeKharid
	delete from tblBargashtAzKharid
	delete from tblRizeBargashtAzKharid
	delete from tblCheckeDaryafti
	delete from tblCheckePardakhti
	delete from tblDaryaft
	delete from tblPardakht
	delete from DasteCheck
	delete from tblFish
	delete from tblPas
	delete from tblPrinters
	delete from tblRizePishPishForoosh	
	delete from tblPishForoosh
	delete from tblRizePishForoosh
	delete from tblVosool
	delete from tblDasteCheck
	delete from tblTasfie	
	delete from tblRizeTasfie
	delete from tblRizeTolid
	delete from tblRizeFormooleTolid
	delete from tblHavaleGoroohi
	delete from tblRizeForooshFastFood
	delete from tblMultiFeeDynamic
	delete from tblSharheFeeDynamic
	delete from tblPishPishForoosh	
	delete from tblEtesalRanandeBeAnbar
	delete from tblSmsRizeSms
	delete from tblVamInfo
	delete from tblRizeTasfieVam
	delete from tblDaftarcheAghsat
	

	if @Anbar=1 
		Delete from tblAnbar Where fldCode<>1000

	if @Kala=1 
		begin			
			delete from tblEshantionTabaghati
			delete from tblEshantionTekrarShavande
			delete from tblKala 
			delete from tblGroupKala Where fldGroupId<>100 
			delete from tblFee
			delete from tblImageKala	
			delete from tblEshantionGroupTabaghati
			delete from tblEshantionGroupTekrarShavande		
		end

	if @DaftarcheTell=1 
		begin
			delete tblDaftarcheTell where fldCodeTafzili=0	
		end

	if @Ashkhas=1
		begin
			delete from tblTafzili Where fldCodeTafzili<>10000 and fldCodeTafzili<>10001
			delete from tblGroupAshkhas Where fldGroupId<>100 and fldGroupId<>101
			delete from tblListPorsantVaset
			delete from  tblPorsantVisitor

			if @DaftarcheTell=1 
				Delete  tblDaftarcheTell 
			else
				update tblDaftarcheTell set fldCodeTafzili=0
		end

	if @BankeMoshtari=1
		delete from tblBankeMoshtari


	declare @cnt bigint

	set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='deleteall' )
	
	if @cnt is null
		set @cnt=1
	else
		set @cnt+=1


	delete from tblDeleteList
	delete from tblAmalkardekarbar

	insert into tblDeleteList(id,fldCode,fldtype)
				values(@cnt,0,'deleteall')

END

GO
/****** Object:  StoredProcedure [dbo].[DeleteChapgar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[DeleteChapgar]
	-- Add the parameters for the stored procedure here
	@IDPrinter bigint,
	@strResult nvarchar(30) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @bigCount bigint
	set @bigCount = (Select Count(fldIDPrinter)  from tblKala where fldIDPrinter=@IDPrinter)
	
	if @bigCount>0 
		set @strResult ='amalkard'
	else
		begin
			Delete from tblPrinters Where id=@IDPrinter
			set @strResult ='delete'
		end

END

GO
/****** Object:  StoredProcedure [dbo].[DeleteDaryaft]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[DeleteDaryaft]
--چندزبانه
@ShomareDaryaft bigint,
@NoeKhata nvarchar(30) output

as

begin
	begin transaction transact
		begin try
			declare @ShomareSanad nvarchar(50)
			set @ShomareSanad =(Select fldShomareSanad from tblDaryaft Where fldShomareDaryaft=@ShomareDaryaft)
			delete from tblDaryaft Where fldShomareSanad=@shomareSanad	
			delete from tblSodooreSanad Where fldSanad=@shomareSanad
			delete from tblCheckeDaryafti Where fldSanad=@shomareSanad		
			delete from tblRizeTasfie Where fldShomareDaryaft=@ShomareDaryaft	
			delete from tblRizeTasfieVam Where fldShomareDaryaft=@ShomareDaryaft	

			set @NoeKhata='deleteok'
	commit transaction
			end try

		begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='rolback'

		end catch
end

GO
/****** Object:  StoredProcedure [dbo].[DeleteDasteCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[DeleteDasteCheck]
@Serial bigint,
@CodeBank bigint
as

begin
	Delete from tblDasteCheck Where fldShomareCheck=@serial and fldCodeBank=@CodeBank
end

GO
/****** Object:  StoredProcedure [dbo].[DeleteFactorAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  PROCEDURE [dbo].[DeleteFactorAmani]
   @fldShomareSanad bigint,
   @fldShomareFactor bigint,
   @strResult nvarchar(25) output
AS
BEGIN
	
	SET NOCOUNT ON;
	begin transaction DelRecord
		begin try
	--delete
					delete from tblRizeForoosh
						where fldShomareSanad=@fldShomareSanad --and fldShomareFactor=@fldShomareFactor
							IF @@ROWCOUNT = 0  
								begin
										set @strResult='NotDeletedRizeForoosh'
										rollback transaction DelRecord;
								end
					delete from tblRizeKharid
						where fldShomareSanad=@fldShomareSanad --and fldShomareFactor=@fldShomareFactor
							IF @@ROWCOUNT = 0  
								begin
										set @strResult='NotDeletedRizeForoosh'
										rollback transaction DelRecord;
								end
					delete from tblSodooreSanad
						where fldSanad=@fldShomareSanad
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedtblSodooreSanad'
										rollback transaction DelRecord;
									end
					set @strResult='deleted'
					commit transaction DelRecord;
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				rollback transaction DelRecord;
			end
		end catch
   
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteFactorBargashtAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create  PROCEDURE [dbo].[DeleteFactorBargashtAmani]
   @fldShomareSanad bigint,
   @fldShomareFactor bigint,
   @strResult nvarchar(25) output
AS
BEGIN
	
	SET NOCOUNT ON;
	begin transaction DelRecord
		begin try
	--delete
					delete from tblRizeBargashtAzForoosh
						where fldShomareSanad=@fldShomareSanad --and fldShomareFactor=@fldShomareFactor
							IF @@ROWCOUNT = 0  
								begin
										set @strResult='NotDeletedRizeForoosh'
										rollback transaction DelRecord;
								end
					delete from tblRizeKharid
						where fldShomareSanad=@fldShomareSanad --and fldShomareFactor=@fldShomareFactor
							IF @@ROWCOUNT = 0  
								begin
										set @strResult='NotDeletedRizeForoosh'
										rollback transaction DelRecord;
								end
					delete from tblSodooreSanad
						where fldSanad=@fldShomareSanad
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedtblSodooreSanad'
										rollback transaction DelRecord;
									end
					set @strResult='deleted'
					commit transaction DelRecord;
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				rollback transaction DelRecord;
			end
		end catch
   
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteFactorbARGASHTaZForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  PROCEDURE [dbo].[DeleteFactorbARGASHTaZForoosh]
   @fldShomareSanad bigint,
   @fldShomareFactor bigint,
   @strResult nvarchar(25) output
AS
BEGIN
	
	SET NOCOUNT ON;
	begin transaction DelRecord
		begin try
	--delete
					declare @ShomarePardakht as bigint

					delete from tblRizeBargashtAzForoosh
						where fldShomareSanad=@fldShomareSanad and fldShomareFactor=@fldShomareFactor
							IF @@ROWCOUNT = 0  
								begin
										set @strResult='NotDeletedRizeForoosh'
										rollback transaction DelRecord;
								end
					delete from tblBargashtAzForoosh
						where fldShomareSanad=@fldShomareSanad and fldShomareFactorForoosh=@fldShomareFactor
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedForoosh'
										rollback transaction DelRecord;
									end
					delete from tblSodooreSanad
						where fldSanad=@fldShomareSanad
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedtblSodooreSanad'
										rollback transaction DelRecord;
									end

					--#############################
					--خودم اضافه کردم
					set @ShomarePardakht =(Select fldShomarePardakht from tblPardakht
												where fldShomareSanad=@fldShomareSanad)
					--#############################

					delete from tblPardakht
						where fldShomareSanad=@fldShomareSanad
							--IF @@ROWCOUNT = 0  
							--	begin
							--		set @strResult='NotDeletedtblDaryaft'
							--		rollback transaction DelRecord;
							--	end

					--#############################
					--خودم اضافه کردم
					delete from tblCheckePardakhti
						where fldSanad=@fldShomareSanad
					
					update tblCheckeDaryafti
						set fldShomarePardakht = NULL
						where fldShomarePardakht=@ShomarePardakht
					--##############################

							--IF @@ROWCOUNT = 0  
							--	begin
							--		set @strResult='NotDeletedtblCheckeDaryafti'
							--		rollback transaction DelRecord;
							--	end
					set @strResult='deleted'
					commit transaction DelRecord;
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				rollback transaction DelRecord;
			end
		end catch
   
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteFactorbargashtazkharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--کامل نیست  (واگذاری چک تعیین نشده)
CREATE  PROCEDURE [dbo].[DeleteFactorbargashtazkharid]
   @fldShomareSanad bigint,
   @fldShomareFactor bigint,
   @CheckeVagozari tblCheckVagozari  readonly,
   @ShomarePardakht bigint,
   @strResult nvarchar(25) output
AS
BEGIN
	
	SET NOCOUNT ON;
	begin transaction DelRecord
		begin try
	--delete
					delete from tblRizebargashtazkharid
						where fldShomareSanad=@fldShomareSanad and fldShomareFactor=@fldShomareFactor
							IF @@ROWCOUNT = 0  
								begin
										set @strResult='NotDeletedRizeBargashtAzKharid'
										rollback transaction DelRecord;
								end
					delete from tblbargashtazkharid
						where fldShomareSanad=@fldShomareSanad and fldShomareFactorKharid=@fldShomareFactor
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedBargashtAzKharid'
										rollback transaction DelRecord;
									end
					delete from tblSodooreSanad
						where fldSanad=@fldShomareSanad
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedtblSodooreSanad'
										rollback transaction DelRecord;
									end
					delete from tblDaryaft
						where fldShomareSanad=@fldShomareSanad
							--IF @@ROWCOUNT = 0  
							--	begin
							--		set @strResult='NotDeletedtblDaryaft'
							--		rollback transaction DelRecord;
							--	end
					delete from tblCheckeDaryafti
						where fldSanad=@fldShomareSanad
							--IF @@ROWCOUNT = 0  
							--	begin
							--		set @strResult='NotDeletedtblCheckeDaryafti'
							--		rollback transaction DelRecord;
							--	end
					



					set @strResult='deleted'
					commit transaction DelRecord;
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				rollback transaction DelRecord;
			end
		end catch
   
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteFactorForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  PROCEDURE [dbo].[DeleteFactorForoosh]
   @fldShomareSanad bigint,
   @fldShomareFactor bigint,
   @strResult nvarchar(25) output
AS
BEGIN
	
	SET NOCOUNT ON;
	begin transaction DelRecord
		begin try
	--delete
					delete from tblRizeForoosh
						where fldShomareSanad=@fldShomareSanad and fldShomareFactor=@fldShomareFactor
							IF @@ROWCOUNT = 0  
								begin
										set @strResult='NotDeletedRizeForoosh'
										rollback transaction DelRecord;
								end
					delete from tblForoosh
						where fldShomareSanad=@fldShomareSanad and fldShomareFactorForoosh=@fldShomareFactor
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedForoosh'
										rollback transaction DelRecord;
									end
					delete from tblSodooreSanad
						where fldSanad=@fldShomareSanad
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedtblSodooreSanad'
										rollback transaction DelRecord;
									end
					delete from tblDaryaft
						where fldShomareSanad=@fldShomareSanad
							--IF @@ROWCOUNT = 0  
							--	begin
							--		set @strResult='NotDeletedtblDaryaft'
							--		rollback transaction DelRecord;
							--	end
					delete from tblCheckeDaryafti
						where fldSanad=@fldShomareSanad
							--IF @@ROWCOUNT = 0  
							--	begin
							--		set @strResult='NotDeletedtblCheckeDaryafti'
							--		rollback transaction DelRecord;
							--	end
					set @strResult='deleted'
					commit transaction DelRecord;
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				rollback transaction DelRecord;
			end
		end catch
   
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteFactorKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--کامل نیست  (واگذاری چک تعیین نشده)
CREATE  PROCEDURE [dbo].[DeleteFactorKharid]
   @fldShomareSanad bigint,
   @fldShomareFactor bigint,
   @CheckeVagozari tblCheckVagozari  readonly,
   @ShomarePardakht bigint,
   @strResult nvarchar(25) output
AS
BEGIN
	
	SET NOCOUNT ON;
	begin transaction DelRecord
		begin try
	--delete
					delete from tblRizeKharid
						where fldShomareSanad=@fldShomareSanad and fldShomareFactor=@fldShomareFactor
							IF @@ROWCOUNT = 0  
								begin
										set @strResult='NotDeletedRizeKharid'
										rollback transaction DelRecord;
								end
					delete from tblKharid
						where fldShomareSanad=@fldShomareSanad and fldShomareFactorKharid=@fldShomareFactor
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedKharid'
										rollback transaction DelRecord;
									end
					delete from tblSodooreSanad
						where fldSanad=@fldShomareSanad
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedtblSodooreSanad'
										rollback transaction DelRecord;
									end
					delete from tblPardakht
						where fldShomareSanad=@fldShomareSanad
							--IF @@ROWCOUNT = 0  
							--	begin
							--		set @strResult='NotDeletedtblDaryaft'
							--		rollback transaction DelRecord;
							--	end
					delete from tblCheckePardakhti
						where fldSanad=@fldShomareSanad
							--IF @@ROWCOUNT = 0  
							--	begin
							--		set @strResult='NotDeletedtblCheckeDaryafti'
							--		rollback transaction DelRecord;
							--	end

					UPDATE tblCheckeDaryafti Set fldShomarePardakht=NULL Where fldShomarePardakht=@ShomarePardakht 



					set @strResult='deleted'
					commit transaction DelRecord;
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				rollback transaction DelRecord;
			end
		end catch
   
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteFactorPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  PROCEDURE [dbo].[DeleteFactorPishForoosh]   
   @fldShomareFactor bigint,
   @strResult nvarchar(25) output
AS
BEGIN
	
	SET NOCOUNT ON;
	begin transaction DelRecord
		begin try
	--delete
					delete from tblRizePishForoosh
						where  fldShomareFactor=@fldShomareFactor
							IF @@ROWCOUNT = 0  
								begin
										set @strResult='NotDeletedRizeForoosh'
										rollback transaction DelRecord;
								end
					delete from tblPishForoosh
						where  fldShomareFactorForoosh=@fldShomareFactor
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedForoosh'
										rollback transaction DelRecord;
									end		
					update tblForoosh set fldShomarePishFactor=NULL Where fldShomarePishFactor=@fldShomareFactor													
					set @strResult='deleted'
					commit transaction DelRecord;
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				rollback transaction DelRecord;
			end
		end catch
   
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteFactorPishPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create  PROCEDURE [dbo].[DeleteFactorPishPishForoosh]   
   @fldShomareFactor bigint,
   @strResult nvarchar(25) output
AS
BEGIN
	
	SET NOCOUNT ON;
	begin transaction DelRecord
		begin try
	--delete
					delete from tblRizePishPishForoosh
						where  fldShomareFactor=@fldShomareFactor
							IF @@ROWCOUNT = 0  
								begin
										set @strResult='NotDeletedRizeForoosh'
										rollback transaction DelRecord;
								end
					delete from tblPishPishForoosh
						where  fldShomareFactorForoosh=@fldShomareFactor
								IF @@ROWCOUNT = 0  
									begin
										set @strResult='NotDeletedForoosh'
										rollback transaction DelRecord;
									end															
					set @strResult='deleted'
					commit transaction DelRecord;
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				rollback transaction DelRecord;
			end
		end catch
   
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteFullKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DeleteFullKala]
	@IdKalaForoosh bigint,
	@fldResult int output
AS
BEGIN
	begin transaction transact
		begin try
			delete from tblEshantionTabaghati 
				where fldCodeKalaForoosh=@IdKalaForoosh
		    delete from tblEshantionTekrarshavande
				where fldCodeKalaForoosh=@IdKalaForoosh
		    delete from tblFee 
				where fldCodeKala=@IdKalaForoosh

			delete tblImageKala Where fldCodeKala = @IdKalaForoosh

			delete from tblKala
				where fldCodeKala=@IdKalaForoosh

			set @fldResult=1
	commit transaction
		end try

	begin catch
		if @@trancount>0 
			rollback transaction ;
			set @fldResult=0

	end catch


END

GO
/****** Object:  StoredProcedure [dbo].[DeleteGroupAshkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DeleteGroupAshkhas]
@fldGroupAshkhasId bigint,
@res int output,
@flagCheckIdInTblAshkhas int output
	
AS
set @flagCheckIdInTblAshkhas=0
declare @hasrow int
declare @hasrowin_tblAshkhas int

BEGIN
set @hasrowin_tblAshkhas=(select count(fldCodeGroup) from tblTafzili where fldCodeGroup=@fldGroupAshkhasId)
if (@hasrowin_tblAshkhas >=1) 
begin
set @flagCheckIdInTblAshkhas=1
return 
end

set @hasrow=(select count(fldGroupId) from tblGroupAshkhas where CONVERT(nvarchar(50),fldGroupId) like CONVERT(nvarchar(50), @fldGroupAshkhasId) + '%')
if @hasrow > 1
begin

set @res=1


end
else
begin
set @res=0
delete from tblGroupAshkhas
where fldGroupId=@fldGroupAshkhasId
  end
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteGroupKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DeleteGroupKala]
@fldGroupKalaId bigint,
@res int output,
@flagCheckIdInTblKala int output
	
AS
set @flagCheckIdInTblKala=0
declare @hasrow int
declare @hasrowin_tblKala int

BEGIN
set @hasrowin_tblKala=(select count(fldCodeGroup) from tblKala where fldCodeGroup=@fldGroupKalaId)
if (@hasrowin_tblKala >=1) 
begin
set @flagCheckIdInTblKala=1
return 
end

set @hasrow=(select count(fldGroupId) from tblGroupKala where CONVERT(nvarchar(50),fldGroupId) like CONVERT(nvarchar(50), @fldGroupKalaId) + '%')
if @hasrow > 1
begin

set @res=1


end
else
begin
set @res=0
delete from tblGroupKala
where fldGroupId=@fldGroupKalaId
  end
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteInfo_Ebteda_Doreh_Ashkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DeleteInfo_Ebteda_Doreh_Ashkhas] 
@fldShomareSanad bigint,
@strResult varchar(25) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @HasRow int
	set @HasRow=0
	set @HasRow=(select count(fldSanad) from tblSodooreSanad where fldSanad=@fldShomareSanad And fldType='EbtedaAshkhas')
    -- Insert statements for procedure here
	If @HasRow > 0 
	 Begin
		Delete from tblSodooreSanad where fldSanad=@fldShomareSanad And fldType='EbtedaAshkhas'
		
		set @strResult='Delete Is OK'
     End
	Else
	 Begin
	    set @strResult='Delete Is Error'
	 End
	
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteInfo_MojodiEbteda_Doreh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DeleteInfo_MojodiEbteda_Doreh] 
@fldShomareSanad bigint,
@strResult varchar(25) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @HasRow int
	set @HasRow=0
	set @HasRow=(select count(fldSanad) from tblSodooreSanad where fldSanad=@fldShomareSanad )
    -- Insert statements for procedure here
	If @HasRow > 0 
	 Begin
		Delete from tblSodooreSanad where fldSanad=@fldShomareSanad And fldType='mojoodieebteda'
		Delete from tblRizeKharid where fldShomareSanad=@fldShomareSanad And fldType='mojodi_ebteda_doreh'
		set @strResult='Delete Is OK'
     End
	Else
	 Begin
	    set @strResult='Delete Is Error'
	 End
	
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteInfoEnteghal]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DeleteInfoEnteghal] 
	-- Add the parameters for the stored procedure here
	@fldShomareEnteghal bigint,
	@strResult nvarchar(25) output
  
AS
BEGIN
	
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	delete from tblRizeForoosh where fldShomareEnteghal=@fldShomareEnteghal and fldType='enteghal'
    delete from tblRizeKharid where fldShomareEnteghal=@fldShomareEnteghal and fldType='enteghal'

END

GO
/****** Object:  StoredProcedure [dbo].[DeleteInfoKhabanidanCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DeleteInfoKhabanidanCheck] 
--چندزبانه
@fldIdCheck bigint,
@Result nvarchar(50) output
    
AS
BEGIN
	
	SET NOCOUNT ON;

   UPDATE [dbo].[tblCheckeDaryafti]
   SET 
     fldDateVagozariBeBank=Null, 
     fldTozihateVagozariBeBank=Null,
     fldVaziat =Null,
     fldCodeBankVagozari=Null 
    
 WHERE ID=@fldIdCheck 
    set @Result='DeleteOK'
	
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteInfoVosoolCheck_Final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DeleteInfoVosoolCheck_Final] 
@fldShomareVosool bigint,
@strResult nvarchar(25) output
as            
BEGIN
	begin transaction transact
	begin try
	
		SET NOCOUNT ON;                                                                                          
		declare @StrShomareSanadVosool1 nvarchar(20);
		set @StrShomareSanadVosool1=(select fldShomareSanad from tblVosool where fldShomareVosool=@fldShomareVosool)
		delete from tblSodooreSanad where fldSanad=@StrShomareSanadVosool1 
		delete from tblVosool where fldShomareVosool=@fldShomareVosool 
						 
			-- /update tblchkDaryafti Reset For Update
			update tblCheckeDaryafti 
			set fldShomareVosool=NULL,
			fldDateVosool=NULL,
			fldTozihateVosool=NULL,
			fldVaziat=NULL,
			fldSanadVosool=NULL,
			fldCodeHesabeVosool=NULL
			where fldShomareVosool=@fldShomareVosool 

				----///
				--baraye halati ke check be bank vagozar shode					
				update tblCheckeDaryafti 
					set fldShomareVosool=NULL,
					fldDateVosool=NULL,
					fldTozihateVosool=NULL,
					fldVaziat='vagozari',
					fldSanadVosool=NULL,
					fldCodeHesabeVosool=NULL
					where fldShomareVosool=@fldShomareVosool and fldDateVagozariBeBank IS not NULL
				----///
	commit transaction
	end try

	begin catch
		if @@trancount>0 
			rollback transaction ;
			set @strResult='rolback'

	end catch
END
GO
/****** Object:  StoredProcedure [dbo].[DeletePardakht]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[DeletePardakht]

@ShomarePardakht bigint,
@NoeKhata nvarchar(30) output

as

begin
	begin transaction transact
		begin try
			declare @ShomareSanad nvarchar(50)
			set @ShomareSanad =(Select fldShomareSanad from tblPardakht Where fldShomarePardakht=@ShomarePardakht)
			delete from tblPardakht Where fldShomareSanad=@shomareSanad	
			delete from tblSodooreSanad Where fldSanad=@shomareSanad
			delete from tblCheckePardakhti Where fldSanad=@shomareSanad			
			UPDATE tblCheckeDaryafti Set fldShomarePardakht=NULL ,fldVaziat=NULL  Where fldShomarePardakht=@ShomarePardakht 
			set @NoeKhata='deleteok'
	commit transaction
			end try

		begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='rolback'

		end catch
end

GO
/****** Object:  StoredProcedure [dbo].[DeletePaseCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[DeletePaseCheck] 
	-- Add the parameters for the stored procedure here
	@fldShomarePas bigint,
	@NoeKhata nvarchar(30) output 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	begin transaction transact
	begin try
		declare @ShomareSanad bigint

		set @ShomareSanad =(Select top(1) fldSanadPas  from tblCheckePardakhti Where fldShomarePass=@fldShomarePas)
		UPDATE       tblCheckePardakhti
			SET         																					
				fldShomarePass=NULL,
				fldDatePass =NULL,
				fldSharhePass =NULL,
				fldSanadPas =NUll																	
				where fldShomarePass=@fldShomarePas

		--set @NoeKhata=100/0
		delete from tblSodooreSanad Where fldSanad=@ShomareSanad
		delete from tblPas Where fldShomarePas=@fldShomarePas
	commit transaction
	end try

	begin catch
		if @@trancount>0 
			rollback transaction ;
			set @NoeKhata='rolback'

	end catch
							
END
GO
/****** Object:  StoredProcedure [dbo].[DeleteSuperVizor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[DeleteSuperVizor]
	-- Add the parameters for the stored procedure here
	@ID bigint
AS
BEGIN
	Delete from tblTafzili Where ID=@Id
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteTblAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc  [dbo].[DeleteTblAnbar]
@ID as int

as
begin
Delete from tblAnbar Where ID=@ID
end

GO
/****** Object:  StoredProcedure [dbo].[DeleteTblBankeMoshtari]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc  [dbo].[DeleteTblBankeMoshtari]
@ID as int

as
begin
Delete from tblBankeMoshtari Where ID=@ID
end

GO
/****** Object:  StoredProcedure [dbo].[DeleteTblKolMoin]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc  [dbo].[DeleteTblKolMoin]
@ID as int

as
begin
Delete from tblKolMoin Where ID=@ID
end

GO
/****** Object:  StoredProcedure [dbo].[DeleteTblTarheTakhfif]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc  [dbo].[DeleteTblTarheTakhfif]
@ID as int

as
begin
Delete from tblTarheTakhfif Where ID=@ID
end

GO
/****** Object:  StoredProcedure [dbo].[DeleteTblVahed]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc  [dbo].[DeleteTblVahed]
@ID as int

as
begin
Delete from tblVahed Where ID=@ID
end

GO
/****** Object:  StoredProcedure [dbo].[DeleteUser]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[DeleteUser] 
	-- Add the parameters for the stored procedure here
	@Code bigint,
	@NoeKhata nvarchar(10) output
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	
	begin transaction transact
		begin try
			declare @mmm bigint
			Delete from tblSecurity Where fldCode=@Code
			execute InsertDastrasi @Code,0,2

			delete from tblDastrasiSandoogh Where fldCodeUser=@Code

			
		commit transaction
			end try

		begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='rolback'

		end catch
		
END

GO
/****** Object:  StoredProcedure [dbo].[DeleteVam]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[DeleteVam]

@ShomareVam bigint
as 

begin


begin try
begin transaction

declare @code bigint = (select fldShomareSanad from tblVamInfo where fldShomareVam = @ShomareVam)

	delete from tblVamInfo Where fldShomareVam=@ShomareVam
	delete from tblDaftarcheAghsat Where fldShomareVam=@ShomareVam
	delete from tblSodooreSanad where fldSanad = @code

commit
end try

begin catch

	rollback

end catch

end
GO
/****** Object:  StoredProcedure [dbo].[ExistCodeAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ExistCodeAnbar]
@CodeAnbar bigint

AS
BEGIN
	select fldCode from tblAnbar
	where fldCode=@CodeAnbar
END

GO
/****** Object:  StoredProcedure [dbo].[ExistFactorBargashtAzForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ExistFactorBargashtAzForoosh]
	@fldShomareFactor bigint,
	@fldMax bigint output
AS
BEGIN
	set @fldMax=( SELECT top 1 fldShomareFactorForoosh  from tblBargashtAzForoosh where fldShomareFactorForoosh=@fldShomareFactor)
	if @fldMax is null
		set @fldMax=0
	
END

GO
/****** Object:  StoredProcedure [dbo].[ExistFactorBargashtAzKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[ExistFactorBargashtAzKharid]
	@fldShomareFactor bigint,
	@fldMax bigint output
AS
BEGIN
	set @fldMax=( SELECT top 1 fldShomareFactorKharid  from tblBargashtAzKharid where fldShomareFactorKharid=@fldShomareFactor)
	if @fldMax is null
		set @fldMax=0
	
END

GO
/****** Object:  StoredProcedure [dbo].[ExistFactorForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ExistFactorForoosh]
	@fldShomareFactor bigint,
	@fldMax bigint output
AS
BEGIN
	set @fldMax=( SELECT top 1 fldShomareFactorForoosh  from tblForoosh where fldShomareFactorForoosh=@fldShomareFactor)
	if @fldMax is null
		set @fldMax=0
	
END

GO
/****** Object:  StoredProcedure [dbo].[ExistFactorKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ExistFactorKharid]
--چندزبانه
	@fldShomareFactor bigint,
	@fldMax bigint output
AS
BEGIN
	set @fldMax=( SELECT top 1 fldShomareFactorKharid  from tblKharid where fldShomareFactorKharid=@fldShomareFactor)
	if @fldMax is null
		set @fldMax=0
	
END

GO
/****** Object:  StoredProcedure [dbo].[ExistFactorPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ExistFactorPishForoosh]
--چندزبانه
	@fldShomareFactor bigint,
	@fldMax bigint output
AS
BEGIN
	set @fldMax=( SELECT top 1 fldShomareFactorForoosh  from tblPishForoosh where fldShomareFactorForoosh=@fldShomareFactor)
	if @fldMax is null
		set @fldMax=0
	
END

GO
/****** Object:  StoredProcedure [dbo].[ExistFactorPishPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[ExistFactorPishPishForoosh]
	@fldShomareFactor bigint,
	@fldMax bigint output
AS
BEGIN
	set @fldMax=( SELECT top 1 fldShomareFactorForoosh  from tblPishPishForoosh where fldShomareFactorForoosh=@fldShomareFactor)
	if @fldMax is null
		set @fldMax=0
	
END

GO
/****** Object:  StoredProcedure [dbo].[ExistShomareAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[ExistShomareAmani]
	@fldShomareAmani bigint,
	@fldMax bigint output
AS
BEGIN
	set @fldMax=( SELECT top 1 fldShomareAmani  from tblRizeForoosh where fldShomareAmani=@fldShomareAmani)
	if @fldMax is null
		set @fldMax=0
	
END

GO
/****** Object:  StoredProcedure [dbo].[ExistShomareBargashtAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ExistShomareBargashtAmani]
	@fldShomareAmani bigint,
	@fldMax bigint output
AS
BEGIN
	set @fldMax=( SELECT top 1 fldShomareBargashtAmani  from tblRizeBargashtAzForoosh where fldShomareBargashtAmani=3)
	if @fldMax is null
		set @fldMax=0
	
END

GO
/****** Object:  StoredProcedure [dbo].[ExistShomareEnteghal]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[ExistShomareEnteghal]
	@fldShomareHavale bigint,
	@fldMax bigint output
AS
BEGIN
	set @fldMax=( SELECT top 1 fldShomareEnteghal  from tblRizeForoosh where fldShomareEnteghal=@fldShomareHavale)
	if @fldMax is null
		set @fldMax=0
	
END

GO
/****** Object:  StoredProcedure [dbo].[ExistShomareVosool]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ExistShomareVosool]
	@fldShomareVosool bigint,
	@fldMax bigint output
AS
BEGIN
	set @fldMax=( SELECT top 1 fldShomareVosool  from tblVosool where fldShomareVosool=@fldShomareVosool)
	if @fldMax is null
		set @fldMax=0
	
END

GO
/****** Object:  StoredProcedure [dbo].[Fillter_tbl_DaftarcheTell_ByName_ByTell_NotHas_CodeTafzili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Fillter_tbl_DaftarcheTell_ByName_ByTell_NotHas_CodeTafzili]
@itemSearch_name_or_Tell nvarchar(45),
@flagFillter int
as
if (@flagFillter=0)
begin
select fldIdDaftarche,fldCodeTafzili,fldName,fldShomare,fldSms,fldSharh  from tblDaftarcheTell 
where  fldShomare Like '%' +  @itemSearch_name_or_Tell  + '%' 
end
else if (@flagFillter=1)
begin
select fldIdDaftarche,fldCodeTafzili,fldName,fldShomare,fldSms,fldSharh  from tblDaftarcheTell 
where fldName  Like '%' +  @itemSearch_name_or_Tell  + '%' 
end

GO
/****** Object:  StoredProcedure [dbo].[Filter_Tbl_Tafsili_in_Form_Enteghal_Kala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[Filter_Tbl_Tafsili_in_Form_Enteghal_Kala]
@codeTafzili nvarchar(20),
@flagSearch int
as
begin
if (@flagSearch=0)
   begin
            SELECT DISTINCT fldCodeTafzili, fldSharheTafzili
               
				
            FROM            dbo.tblTafzili

            where fldCodeTafzili like '%' + @codeTafzili + '%'
       end

 else if (@flagSearch=1)
 begin
            SELECT DISTINCT fldCodeTafzili,fldSharheTafzili
				
            FROM            dbo.tblTafzili

		    where fldSharheTafzili like '%' + @codeTafzili + '%'

 end
end

GO
/****** Object:  StoredProcedure [dbo].[Filter_Tbl_Tafsili_in_FormAshkhas_ByCodeTafzili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Filter_Tbl_Tafsili_in_FormAshkhas_ByCodeTafzili]
@codeTafzili nvarchar(20),
@flagSearch int
as
begin
if (@flagSearch=0)
   begin
            SELECT DISTINCT fldCodeTafzili, fldCodeGroup, fldSharheTafzili,fldAddress,fldBlackList,
                fldCity,fldCodeEghtesadi,fldCodeMeli,fldCodePosti,fldDate1,fldDate2,
                fldPic,fldOstan,fldShomareHesab,fldShomareSabt,
				fldVisitor,fldSharhBlackList,fldSaghfeEtebar 
				
            FROM            dbo.tblTafzili 

            where fldCodeTafzili like '%' + @codeTafzili + '%' and fldCodeNoe=1
       end

 else if (@flagSearch=1)
 begin
            SELECT DISTINCT fldCodeTafzili, fldCodeGroup, fldSharheTafzili,fldAddress,fldBlackList,
                fldCity,fldCodeEghtesadi,fldCodeMeli,fldCodePosti,fldDate1,fldDate2,
                fldPic,fldOstan,fldShomareHesab,fldShomareSabt,
				fldVisitor,fldSharhBlackList,fldSaghfeEtebar 
				
            FROM            dbo.tblTafzili

		    where fldSharheTafzili like '%' + @codeTafzili + '%' and fldCodeNoe=1

 end
end

GO
/****** Object:  StoredProcedure [dbo].[Filter_Tbl_Tafsili_in_FormAshkhas_ByCodeTafzili_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Filter_Tbl_Tafsili_in_FormAshkhas_ByCodeTafzili_WithUserCode]
--چندزبانه
@codeTafzili nvarchar(20),
@flagSearch int,
@userCode bigint
as
begin

-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################
-- ##########################################################

if (@flagSearch=0)
   begin
            SELECT DISTINCT fldCodeTafzili, fldCodeGroup, fldSharheTafzili,fldAddress,fldBlackList,
                fldCity,fldCodeEghtesadi,fldCodeMeli,fldCodePosti,fldDate1,fldDate2,
                fldPic,fldOstan,fldShomareHesab,fldShomareSabt,
				fldVisitor,fldSharhBlackList,fldSaghfeEtebar 
				
            FROM            dbo.tblTafzili 

            where fldCodeTafzili like '%' + @codeTafzili + '%' and fldCodeNoe=1
			and fldCodeTafzili in (select code from @accessCodes)
       end

 else if (@flagSearch=1)
 begin
            SELECT DISTINCT fldCodeTafzili, fldCodeGroup, fldSharheTafzili,fldAddress,fldBlackList,
                fldCity,fldCodeEghtesadi,fldCodeMeli,fldCodePosti,fldDate1,fldDate2,
                fldPic,fldOstan,fldShomareHesab,fldShomareSabt,
				fldVisitor,fldSharhBlackList,fldSaghfeEtebar 
				
            FROM            dbo.tblTafzili

		    where fldSharheTafzili like '%' + @codeTafzili + '%' and fldCodeNoe=1
			and fldCodeTafzili in (select code from @accessCodes)

 end
end

GO
/****** Object:  StoredProcedure [dbo].[FilterByCodeAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[FilterByCodeAnbar]
@fldCodeAnbar bigint
AS
BEGIN
	select fldCode,fldNameAnbar from tblAnbar
	where convert(nvarchar(50),fldCode) like '%'+convert(nvarchar(50),@fldCodeAnbar)+'%'
END

GO
/****** Object:  StoredProcedure [dbo].[FilterByNameAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[FilterByNameAnbar]
@fldNameAnbar nvarchar(50)

AS
BEGIN
	select fldCode,fldNameAnbar from tblAnbar
	where fldNameAnbar like '%'+@fldNameAnbar+'%'
END

GO
/****** Object:  StoredProcedure [dbo].[FilterDasteCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[FilterDasteCheck]
@ShomareCheck nvarchar(50)
--@CodeBank int
as

begin
	--SELECT        tblDasteCheck.fldCodeBank as fldCodeBank, tblDasteCheck.fldShobe as fldShobe, tblDasteCheck.fldShomareCheck as fldShomareCheck, tblDasteCheck.fldShomareHesab as fldShomareHesab, tblKolMoin.fldSharheHesab as fldNameBank
	--	FROM            tblDasteCheck INNER JOIN
	--							 tblKolMoin ON tblDasteCheck.fldCodeBank = tblKolMoin.fldCodeHesab
	--	WHERE        (Convert(nvarchar,tblDasteCheck.fldShomareCheck) LIKE '%' +@ShomareCheck + '%')
	--	order by fldNameBank,fldShomareCheck
	--if @CodeBank=0 
	--	begin
			--SELECT        fldCodeBank, fldShobe, fldShomareCheck, fldShomareHesab, fldNameBank
			--	FROM            (SELECT        tblDasteCheck.fldCodeBank, tblDasteCheck.fldShobe, tblDasteCheck.fldShomareCheck, tblDasteCheck.fldShomareHesab, 
			--														tblKolMoin.fldSharheHesab AS fldNameBank
			--							   FROM            tblDasteCheck INNER JOIN
			--														tblKolMoin ON tblDasteCheck.fldCodeBank = tblKolMoin.fldCodeHesab
			--							   WHERE        (CONVERT(nvarchar, tblDasteCheck.fldShomareCheck) LIKE '%' +  +@ShomareCheck + + '%')) AS tbl1
			--	WHERE        (NOT (fldCodeBank =@codeBank)) OR
			--							 (NOT (fldShomareCheck = @ShomareCheck))
			--	ORDER BY fldNameBank, fldShomareCheck
				SELECT        fldCodeBank, fldShobe, fldShomareCheck, fldShomareHesab, fldNameBank
				FROM            (SELECT        tblDasteCheck.fldCodeBank, tblDasteCheck.fldShobe, tblDasteCheck.fldShomareCheck, tblDasteCheck.fldShomareHesab, 
																	tblKolMoin.fldSharheHesab AS fldNameBank
										   FROM            tblDasteCheck INNER JOIN
																	tblKolMoin ON tblDasteCheck.fldCodeBank = tblKolMoin.fldCodeHesab
										   WHERE        (CONVERT(nvarchar, tblDasteCheck.fldShomareCheck) LIKE '' + @ShomareCheck + '%')) AS tbl1
				WHERE        ((CONVERT(nvarchar, fldCodeBank) + CONVERT(nvarchar, fldShomareCheck)) NOT IN
											 (SELECT        CONVERT(nvarchar, fldCodeBank) + CONVERT(nvarchar, fldShomareCheck) AS Expr1
												FROM            tblCheckePardakhti))
				ORDER BY fldNameBank, fldShomareCheck
			--end
	--else if @CodeBank>0 
	--	begin
	--		SELECT        fldCodeBank, fldShobe, fldShomareCheck, fldShomareHesab, fldNameBank
	--			FROM            (SELECT        tblDasteCheck.fldCodeBank, tblDasteCheck.fldShobe, tblDasteCheck.fldShomareCheck, tblDasteCheck.fldShomareHesab, 
	--																tblKolMoin.fldSharheHesab AS fldNameBank
	--									   FROM            tblDasteCheck INNER JOIN
	--																tblKolMoin ON tblDasteCheck.fldCodeBank = tblKolMoin.fldCodeHesab
	--									   WHERE        (CONVERT(nvarchar, tblDasteCheck.fldShomareCheck) LIKE '%' +  +@ShomareCheck + + '%')) AS tbl1
	--			WHERE        (NOT (fldCodeBank =@codeBank)) OR
	--									 (NOT (fldShomareCheck = @ShomareCheck))
	--			ORDER BY fldNameBank, fldShomareCheck
	--		end
	--	end
end

GO
/****** Object:  StoredProcedure [dbo].[FindMaxCodeGroupAshkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[FindMaxCodeGroupAshkhas](@id nvarchar(50))
AS
BEGIN
if(len(@id)=3)
begin
	select max(fldGroupId) from tblGroupAshkhas
	where len(fldGroupId)=3 
	
end
else if(len(@id)>=6)
begin
declare @idSub nvarchar(50)

set @idSub=SUBSTRING(@id,0,len(@id)-2)
	select max(fldGroupId) from tblGroupAshkhas
	where len(fldGroupId)=len(@id)
	and convert(nvarchar,fldGroupId) like @idSub +'%'
end
END

GO
/****** Object:  StoredProcedure [dbo].[FindMaxCodeGroupKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[FindMaxCodeGroupKala](@id nvarchar(50))
AS
BEGIN
if(len(@id)=3)
begin
	select max(fldGroupId) from tblGroupKala
	where len(fldGroupId)=3 
	--and @id like +'%'
end
else if(len(@id)>=6)
begin
declare @idSub nvarchar(50)

set @idSub=SUBSTRING(@id,0,len(@id)-2)
	select max(fldGroupId) from tblGroupKala
	where len(fldGroupId)=len(@id)
	and convert(nvarchar,fldGroupId) like @idSub +'%'
end
END

GO
/****** Object:  StoredProcedure [dbo].[GetCodeVahedByName]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetCodeVahedByName]
--چندزبانه
@strNameVahed nvarchar(50),
@lngCodeVahed bigint output
AS
BEGIN

	set @lngCodeVahed=( SELECT Id from tblVahed
	where fldNameVahed = N''+@strNameVahed+'')

	if @lngCodeVahed is null
		set @lngCodeVahed=0
END

GO
/****** Object:  StoredProcedure [dbo].[GetCodeVaseteByCodeTafsili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetCodeVaseteByCodeTafsili]
--چندزبانه
	@strCodeTafsili nvarchar(25),
	@strCodeVasete nvarchar(25) output
AS
BEGIN
	
	SET NOCOUNT ON;
	--set  @strCodeVasete=(SELECT        dbo.tblTafzili.fldVisitor
	--			FROM            dbo.tblTafzili INNER JOIN
	--									 dbo.tblGroupAshkhas ON dbo.tblTafzili.fldVisitor = dbo.tblGroupAshkhas.fldGroupId
	--			WHERE        (dbo.tblTafzili.fldCodeTafzili = @strCodeTafsili)
	--			       )

	set  @strCodeVasete  = (Select fldVisitor from tblTafzili Where fldCodeTafzili = @strCodeTafsili)
	set @strCodeVasete= ISNULL(@strCodeVasete,'0')	
END

GO
/****** Object:  StoredProcedure [dbo].[GetfldTedadInYekCarton]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetfldTedadInYekCarton]
--چندزبانه
	@fldCodeKala bigint,
	@fldValue decimal(18,4) output
AS
BEGIN
	SET NOCOUNT ON;
	declare @Value decimal(18,4)
	set @Value=(	SELECT        fldTedadJoz AS fldTedadInYekCarton
		FROM            dbo.tblKala
		where fldCodeKala=@fldCodeKala)

		if isnumeric(@Value)=0
			set @fldValue=0
		else
			set @fldValue=@Value


END

GO
/****** Object:  StoredProcedure [dbo].[GetGhymatMasrafkonande]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  PROCEDURE [dbo].[GetGhymatMasrafkonande]
--چندزبانه
@lngCodeKala bigint,
@decPrice decimal(18,4) output
AS
BEGIN
	
		set @decPrice=(select isnull(fldGhymatMasrafkonande,0) from tblKala 
		where fldCodeKala=@lngCodeKala)
		
END

GO
/****** Object:  StoredProcedure [dbo].[GetInfoKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[GetInfoKala]
@CodeKala bigint
AS
BEGIN

	SELECT         ID, fldNameKala, fldCodeKala, fldSerial, fldCodeGroup, fldCodeVahed1, fldCodeVahed2, fldNogheSefaresh, fldSharh, fldTedadJoz, fldFeeDarsadKharid, 
                         fldFeeBadAzTakhfif, fldNerkhePorsantKol, fldNerkhePorsantJoz, fldFeeForoosh, fldPathPic, fldGhymatMasrafkonande
		FROM            dbo.tblKala
		Where fldCodeKala=@CodeKala

END

GO
/****** Object:  StoredProcedure [dbo].[GetListEshantionGroupTabalaghtisMulti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[GetListEshantionGroupTabalaghtisMulti]
	@language nvarchar(10)
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    create table #tblTemp
	(
	fldCodeGroupKala bigint,
	fldShomareTabaghe bigint,
	fldTedadForoosh decimal(18,4),
	fldCodeKalaEshantion bigint,
	fldTedadEshantion decimal(18,4),
	fldNameKala nvarchar(100)
	)

	insert into #tblTemp
		(fldCodeGroupKala,fldShomareTabaghe,fldTedadForoosh,fldCodeKalaEshantion,fldTedadEshantion,fldNameKala)
		SELECT        dbo.tblEshantionGroupTabaghati.fldCodeGroupKala, dbo.tblEshantionGroupTabaghati.fldShomareTabaghe, dbo.tblEshantionGroupTabaghati.fldTedadForoosh, dbo.tblEshantionGroupTabaghati.fldCodeKalaEshantion, 
									 dbo.tblEshantionGroupTabaghati.fldTedadEshantion, dbo.tblKala.fldNameKala
			FROM            dbo.tblEshantionGroupTabaghati INNER JOIN
									 dbo.tblKala ON dbo.tblEshantionGroupTabaghati.fldCodeKalaEshantion = dbo.tblKala.fldCodeKala INNER JOIN
									 dbo.tblGroupKala ON dbo.tblEshantionGroupTabaghati.fldCodeGroupKala = dbo.tblGroupKala.fldGroupId

	 update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKalaEshantion and tblkalatranslate.fldLanguage=@Language

	 select fldCodeGroupKala,fldShomareTabaghe,fldTedadForoosh,fldCodeKalaEshantion,fldTedadEshantion,fldNameKala from #tbltemp
END

GO
/****** Object:  StoredProcedure [dbo].[GetListFullInfoKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[GetListFullInfoKala]
--چندزبانه
@language nvarchar(10),
@fldCodeKala bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
begin
create table #tblTemp
(
fldCodeKala bigint,
fldNameKala nvarchar(100),
fldNameVahed nvarchar(100),
fldNameVahed2 nvarchar(100),
fldFeeForoosh decimal(18,4), 
fldSerial nvarchar(20), 
fldCodeGroup bigint,
fldNogheSefaresh decimal(18,4),
fldSharh nvarchar(100),
fldTedadJoz decimal(18,4),
fldFeeBadAzTakhfif decimal(18,4), 
fldFeeDarsadKharid decimal(18,4),
fldNerkhePorsantKol decimal(18,4), 
fldNerkhePorsantJoz decimal(18,4),
fldPathPic nvarchar(max)
)

insert into #tblTemp
	(fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldFeeForoosh,fldSerial,fldCodeGroup,fldNogheSefaresh,fldSharh,fldTedadJoz,fldFeeBadAzTakhfif,fldFeeDarsadKharid,fldNerkhePorsantKol,fldNerkhePorsantJoz,fldPathPic)
		SELECT        dbo.tblKala.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2, dbo.tblKala.fldFeeForoosh, 
								 dbo.tblKala.fldSerial, dbo.tblKala.fldCodeGroup, dbo.tblKala.fldNogheSefaresh, dbo.tblKala.fldSharh, dbo.tblKala.fldTedadJoz, dbo.tblKala.fldFeeBadAzTakhfif, 
								 dbo.tblKala.fldFeeDarsadKharid, dbo.tblKala.fldNerkhePorsantKol, dbo.tblKala.fldNerkhePorsantJoz, dbo.tblKala.fldPathPic
		FROM            dbo.tblKala INNER JOIN
								 dbo.tblVahed ON dbo.tblKala.fldCodeVahed1 = dbo.tblVahed.ID INNER JOIN
								 dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID
		where dbo.tblKala.fldCodeKala =@fldCodeKala

update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language

select fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldFeeForoosh,fldSerial,fldCodeGroup,fldNogheSefaresh,fldSharh,fldTedadJoz,fldFeeBadAzTakhfif,fldFeeDarsadKharid,fldNerkhePorsantKol,fldNerkhePorsantJoz,fldPathPic from #tblTemp


end
GO
/****** Object:  StoredProcedure [dbo].[GetListMultiPrice]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetListMultiPrice]
--چندزبانه
	@lngCodeKala bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
		select fldName,convert(decimal(18,4),fldFee) as fldFee from tblFee
			where fldCodeKala=@lngCodeKala
END

GO
/****** Object:  StoredProcedure [dbo].[GetMablaghHamloNaghl]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetMablaghHamloNaghl]
--چندزبانه

 @fldSanad bigint,
 @fldMablagh decimal(18,4) output

AS
BEGIN

	SET NOCOUNT ON;
		declare  @fldCodeHamloNaghl bigint
		set @fldCodeHamloNaghl=(select dbo.tblTanzimat.fldMoinDaramadeHamlVaNaghl from tblTanzimat)
	set @fldMablagh=	(SELECT        fldBestankar
		FROM            tblSodooreSanad
		WHERE        (fldSanad = @fldSanad) AND (fldMoinKamel = CONVERT(nvarchar(10),@fldCodeHamloNaghl)))
		if (@fldMablagh is null)
			set @fldMablagh=0
END

GO
/****** Object:  StoredProcedure [dbo].[GetMablaghHamloNaghlBargashtAzKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetMablaghHamloNaghlBargashtAzKharid]
--چندزبانه
 @fldSanad bigint,
 @fldMablagh bigint output

AS
BEGIN

	SET NOCOUNT ON;
		declare  @fldCodeHamloNaghl bigint
		set @fldCodeHamloNaghl=(select dbo.tblTanzimat.fldCodeMoinHazineHamlVaNaghl from tblTanzimat)
	set @fldMablagh=	(SELECT        fldBestankar
		FROM            tblSodooreSanad
		WHERE        (fldSanad = @fldSanad) AND (fldMoinKamel = CONVERT(nvarchar(10),@fldCodeHamloNaghl)))
		if (@fldMablagh is null)
			set @fldMablagh=0
END

GO
/****** Object:  StoredProcedure [dbo].[GetMablaghHamloNaghlKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetMablaghHamloNaghlKharid]
--چندزبانه
 @fldSanad bigint,
 @fldMablagh decimal(18,4) output

AS
BEGIN

	SET NOCOUNT ON;
		declare  @fldCodeHamloNaghl decimal(18,4)
		set @fldCodeHamloNaghl=(select dbo.tblTanzimat.fldCodeMoinHazineHamlVaNaghl from tblTanzimat)
	set @fldMablagh=	(SELECT        fldBedehkar
		FROM            tblSodooreSanad
		WHERE        (fldSanad = @fldSanad) AND (fldMoinKamel = CONVERT(nvarchar(10),@fldCodeHamloNaghl)))
		if (@fldMablagh is null)
			set @fldMablagh=0
END

GO
/****** Object:  StoredProcedure [dbo].[GetMaxShomareEnteghal]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[GetMaxShomareEnteghal]
	@fldMax bigint output
AS
BEGIN
	
	set @fldMax=( SELECT isnull(max(fldShomareEnteghal),0) as fldMax from tblRizeForoosh)
END

GO
/****** Object:  StoredProcedure [dbo].[GetMaxShomareFactorBargashtAzForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[GetMaxShomareFactorBargashtAzForoosh]
	@fldMax bigint output
AS
BEGIN
	SET NOCOUNT ON;
	set @fldMax=( SELECT isnull(max(fldShomareFactorForoosh),0) as fldMax from tblBargashtAzForoosh)
END

GO
/****** Object:  StoredProcedure [dbo].[GetMaxShomareFactorBargashtAzKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetMaxShomareFactorBargashtAzKharid]
	@fldMax bigint output
AS
BEGIN
	SET NOCOUNT ON;
	set @fldMax=( SELECT isnull(max(fldShomareFactorKharid),0) as fldMax from tblBargashtAzKharid)
END
GO
/****** Object:  StoredProcedure [dbo].[GetMaxShomareFactorForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetMaxShomareFactorForoosh]
	@fldMax bigint output
AS
BEGIN
	SET NOCOUNT ON;
	set @fldMax=( SELECT isnull(max(fldShomareFactorForoosh),0) as fldMax from tblForoosh)
END

GO
/****** Object:  StoredProcedure [dbo].[GetMaxShomareFactorKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetMaxShomareFactorKharid]
	@fldMax bigint output
AS
BEGIN
	SET NOCOUNT ON;
	set @fldMax=( SELECT isnull(max(fldShomareFactorKharid),0) as fldMax from tblKharid)
END
GO
/****** Object:  StoredProcedure [dbo].[GetMaxShomareFactorPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetMaxShomareFactorPishForoosh]
--چندزبانه
	@fldMax bigint output
AS
BEGIN
	SET NOCOUNT ON;
	set @fldMax=( SELECT isnull(max(fldShomareFactorForoosh),0) as fldMax from tblPishForoosh)
END

GO
/****** Object:  StoredProcedure [dbo].[GetMaxShomareFactorPishPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[GetMaxShomareFactorPishPishForoosh]
	@fldMax bigint output
AS
BEGIN
	SET NOCOUNT ON;
	set @fldMax=( SELECT isnull(max(fldShomareFactorForoosh),0) as fldMax from tblPishPishForoosh)
END

GO
/****** Object:  StoredProcedure [dbo].[GetMaxShomarePishFactorForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[GetMaxShomarePishFactorForoosh]
	@fldMax bigint output
AS
BEGIN
	SET NOCOUNT ON;
	set @fldMax=( SELECT isnull(max(fldShomareFactorForoosh),0) as fldMax from tblPishForoosh)
END

GO
/****** Object:  StoredProcedure [dbo].[GetNameGroupByCodeTafzili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetNameGroupByCodeTafzili]
	@fldCodeHesab bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

  SELECT        dbo.tblTafzili.fldCodeGroup, dbo.tblGroupAshkhas.fldGroupName
FROM            dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup
WHERE        (dbo.tblTafzili.fldCodeTafzili = @fldCodeHesab)
END

GO
/****** Object:  StoredProcedure [dbo].[GetOtherInfoBargashtAzFactor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetOtherInfoBargashtAzFactor] 
--چندزبانه
	@fldShomareFactor bigint
AS
BEGIN
	
	SET NOCOUNT ON;

		SELECT        TOP (1) dbo.tblRizeBargashtAzForoosh.fldCodeVasete, dbo.tblRizeBargashtAzForoosh.fldCodeUser, dbo.tblRizeBargashtAzForoosh.fldTime, dbo.tblRizeBargashtAzForoosh.fldDate, 
                         dbo.tblbargashtAzForoosh.fldSharhFactor, dbo.tblRizeBargashtAzForoosh.fldShomareFactor, dbo.tblRizeBargashtAzForoosh.fldShomareSanad, dbo.tblTafzili.fldSharheTafzili, 
                         dbo.tblRizeBargashtAzForoosh.fldCodeTafsili
FROM            dbo.tblRizeBargashtAzForoosh INNER JOIN
                         dbo.tblbargashtAzForoosh ON dbo.tblRizeBargashtAzForoosh.fldShomareSanad = dbo.tblbargashtAzForoosh.fldShomareSanad INNER JOIN
                         dbo.tblTafzili ON dbo.tblRizeBargashtAzForoosh.fldCodeTafsili = dbo.tblTafzili.fldCodeTafzili
WHERE        (dbo.tblRizeBargashtAzForoosh.fldShomareFactor = @fldShomareFactor)
END

GO
/****** Object:  StoredProcedure [dbo].[GetOtherInfoFactor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetOtherInfoFactor] 
--چندزبانه
	@fldShomareFactor bigint
AS
BEGIN
	
	SET NOCOUNT ON;

		SELECT        TOP (1) dbo.tblRizeForoosh.fldCodeVasete, dbo.tblRizeForoosh.fldCodeUser, dbo.tblRizeForoosh.fldTime, dbo.tblRizeForoosh.fldDate, 
                         dbo.tblForoosh.fldSharhFactor,dbo.tblForoosh.fldShomarePishFactor, dbo.tblRizeForoosh.fldShomareFactor, dbo.tblRizeForoosh.fldShomareSanad, dbo.tblTafzili.fldSharheTafzili, 
                         dbo.tblRizeForoosh.fldCodeTafsili
FROM            dbo.tblRizeForoosh INNER JOIN
                         dbo.tblForoosh ON dbo.tblRizeForoosh.fldShomareSanad = dbo.tblForoosh.fldShomareSanad INNER JOIN
                         dbo.tblTafzili ON dbo.tblRizeForoosh.fldCodeTafsili = dbo.tblTafzili.fldCodeTafzili
WHERE        (dbo.tblRizeForoosh.fldShomareFactor =@fldShomareFactor)
END

GO
/****** Object:  StoredProcedure [dbo].[GetOtherInfoFactorAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetOtherInfoFactorAmani] 
	@fldShomareFactor bigint
AS
BEGIN
	
	SET NOCOUNT ON;

		SELECT        TOP (1) dbo.tblRizeForoosh.fldCodeVasete, dbo.tblRizeForoosh.fldCodeUser, dbo.tblRizeForoosh.fldTime, dbo.tblRizeForoosh.fldDate, 
                         dbo.tblRizeForoosh.fldShomareFactor, dbo.tblRizeForoosh.fldShomareSanad, dbo.tblTafzili.fldSharheTafzili, 
                         dbo.tblRizeForoosh.fldCodeTafsili
FROM            dbo.tblRizeForoosh  INNER JOIN
                         dbo.tblTafzili ON dbo.tblRizeForoosh.fldCodeTafsili = dbo.tblTafzili.fldCodeTafzili
WHERE        (dbo.tblRizeForoosh.fldShomareAmani = @fldShomareFactor)
END

GO
/****** Object:  StoredProcedure [dbo].[GetOtherInfoFactorBargashtAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[GetOtherInfoFactorBargashtAmani] 
	@fldShomareFactor bigint
AS
BEGIN
	
	SET NOCOUNT ON;

		SELECT        TOP (1) dbo.tblRizeBargashtAzForoosh.fldCodeVasete, dbo.tblRizeBargashtAzForoosh.fldCodeUser, dbo.tblRizeBargashtAzForoosh.fldTime, dbo.tblRizeBargashtAzForoosh.fldDate, 
                         dbo.tblRizeBargashtAzForoosh.fldShomareBargashtAmani, dbo.tblRizeBargashtAzForoosh.fldShomareSanad, dbo.tblTafzili.fldSharheTafzili, 
                         dbo.tblRizeBargashtAzForoosh.fldCodeTafsili
FROM            dbo.tblRizeBargashtAzForoosh  INNER JOIN
                         dbo.tblTafzili ON dbo.tblRizeBargashtAzForoosh.fldCodeTafsili = dbo.tblTafzili.fldCodeTafzili
WHERE        (dbo.tblRizeBargashtAzForoosh.fldShomareBargashtAmani = @fldShomareFactor)
END

GO
/****** Object:  StoredProcedure [dbo].[GetOtherInfoFactorBargashtAzKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[GetOtherInfoFactorBargashtAzKharid] 
	@fldShomareFactor bigint
AS
BEGIN
	
	SET NOCOUNT ON;

		SELECT        TOP (1) dbo.tblRizeBargashtAzKharid.fldCodeVasete, dbo.tblRizeBargashtAzKharid.fldCodeUser, dbo.tblRizeBargashtAzKharid.fldTime, dbo.tblRizeBargashtAzKharid.fldDate, 
                         dbo.tblBargashtAzKharid.fldSharhFactor, dbo.tblRizeBargashtAzKharid.fldShomareFactor, dbo.tblRizeBargashtAzKharid.fldShomareSanad, dbo.tblTafzili.fldSharheTafzili, 
                         dbo.tblRizeBargashtAzKharid.fldCodeTafsili
FROM            dbo.tblRizeBargashtAzKharid INNER JOIN
                         dbo.tblBargashtAzKharid ON dbo.tblRizeBargashtAzKharid.fldShomareSanad = dbo.tblBargashtAzKharid.fldShomareSanad INNER JOIN
                         dbo.tblTafzili ON dbo.tblRizeBargashtAzKharid.fldCodeTafsili = dbo.tblTafzili.fldCodeTafzili
WHERE        (dbo.tblRizeBargashtAzKharid.fldShomareFactor = @fldShomareFactor)
END

GO
/****** Object:  StoredProcedure [dbo].[GetOtherInfoFactorKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetOtherInfoFactorKharid] 
--چندزبانه
	@fldShomareFactor bigint
AS
BEGIN
	
	SET NOCOUNT ON;

		SELECT        TOP (1) dbo.tblRizeKharid.fldCodeVasete, dbo.tblRizeKharid.fldCodeUser, dbo.tblRizeKharid.fldTime, dbo.tblRizeKharid.fldDate, 
                         dbo.tblKharid.fldSharhFactor, dbo.tblRizeKharid.fldShomareFactor, dbo.tblRizeKharid.fldShomareSanad, dbo.tblTafzili.fldSharheTafzili, 
                         dbo.tblRizeKharid.fldCodeTafsili
FROM            dbo.tblRizeKharid INNER JOIN
                         dbo.tblKharid ON dbo.tblRizeKharid.fldShomareSanad = dbo.tblKharid.fldShomareSanad INNER JOIN
                         dbo.tblTafzili ON dbo.tblRizeKharid.fldCodeTafsili = dbo.tblTafzili.fldCodeTafzili
WHERE        (dbo.tblRizeKharid.fldShomareFactor = @fldShomareFactor)
END

GO
/****** Object:  StoredProcedure [dbo].[GetOtherInfoPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetOtherInfoPishForoosh] 
--چندزبانه
	@fldShomareFactor bigint
AS
BEGIN
	
	SET NOCOUNT ON;

		SELECT        TOP (1) dbo.tblRizePishForoosh.fldCodeVasete, dbo.tblRizePishForoosh.fldCodeUser, dbo.tblRizePishForoosh.fldTime, dbo.tblRizePishForoosh.fldDate, 
                         dbo.tblPishForoosh.fldSharhFactor, dbo.tblRizePishForoosh.fldShomareFactor,  dbo.tblTafzili.fldSharheTafzili, 
                         dbo.tblRizePishForoosh.fldCodeTafsili
FROM            dbo.tblRizePishForoosh INNER JOIN
                         dbo.tblPishForoosh ON dbo.tblRizePishForoosh.fldShomareFactor = dbo.tblPishForoosh.fldShomareFactorForoosh INNER JOIN
                         dbo.tblTafzili ON dbo.tblRizePishForoosh.fldCodeTafsili = dbo.tblTafzili.fldCodeTafzili
WHERE        (dbo.tblRizePishForoosh.fldShomareFactor = @fldShomareFactor)
END

GO
/****** Object:  StoredProcedure [dbo].[GetOtherInfoPishPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetOtherInfoPishPishForoosh] 
--چندزبانه
	@fldShomareFactor bigint
AS
BEGIN
	
	SET NOCOUNT ON;

		SELECT        TOP (1) dbo.tblRizePishPishForoosh.fldCodeVasete, dbo.tblRizePishPishForoosh.fldCodeUser, dbo.tblRizePishPishForoosh.fldTime, dbo.tblRizePishPishForoosh.fldDate, 
                         dbo.tblPishPishForoosh.fldSharhFactor, dbo.tblRizePishPishForoosh.fldShomareFactor,  dbo.tblTafzili.fldSharheTafzili, 
                         dbo.tblRizePishPishForoosh.fldCodeTafsili
FROM            dbo.tblRizePishPishForoosh INNER JOIN
                         dbo.tblPishPishForoosh ON dbo.tblRizePishPishForoosh.fldShomareFactor = dbo.tblPishPishForoosh.fldShomareFactorForoosh INNER JOIN
                         dbo.tblTafzili ON dbo.tblRizePishPishForoosh.fldCodeTafsili = dbo.tblTafzili.fldCodeTafzili
WHERE        (dbo.tblRizePishPishForoosh.fldShomareFactor = @fldShomareFactor)
END

GO
/****** Object:  StoredProcedure [dbo].[GetPorsantSuperVisor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetPorsantSuperVisor]
--چندزبانه
	-- Add the parameters for the stored procedure here
	@fldCodeKala bigint,
	@fldPorsant float output
AS
BEGIN
	
	set @fldPorsant=(SELECT fldNerkhePorsantJoz from tblKala 
		where fldCodeKala=@fldCodeKala)

END

GO
/****** Object:  StoredProcedure [dbo].[GetPorsantVisitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetPorsantVisitor]
--چندزبانه
	-- Add the parameters for the stored procedure here
	@fldCodeKala bigint,
	@fldPorsant float output
AS
BEGIN
	
	set @fldPorsant=(SELECT fldNerkhePorsantKol from tblKala 
		where fldCodeKala=@fldCodeKala)

END

GO
/****** Object:  StoredProcedure [dbo].[GetRowEshantionTabaghati]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetRowEshantionTabaghati]
--چندزبانه
	@lngCodeKala bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    
		SELECT 
			  [fldCodeKalaForoosh]
			  ,[fldShomareTabaghe]
			  ,[fldTedadForoosh]
			  ,[fldCodeKalaEshantion]
			  ,[fldTedadEshantion]
		  FROM tblEshantionTabaghati
			  where [fldCodeKalaForoosh]=@lngCodeKala

END

GO
/****** Object:  StoredProcedure [dbo].[GetRowEshantionTekrarShavande]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetRowEshantionTekrarShavande]
--چندزبانه
	@lngCodeKala bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    
		SELECT 
		  [fldCodeKalaForoosh]
		  ,[fldTedadKala]
		  ,[fldCodeKalaEshantion]
		  ,[fldTedadEshantion]
			  FROM tblEshantionTekrarShavande
				  where [fldCodeKalaForoosh]=@lngCodeKala

END

GO
/****** Object:  StoredProcedure [dbo].[GetShomareDaryaftByfldSanad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetShomareDaryaftByfldSanad]
	@fldShomareSanad bigint,
	@fldShomareDaryaft bigint output
AS
BEGIN

	SET NOCOUNT ON;
				set @fldShomareDaryaft=(	SELECT        TOP (1) dbo.tblDaryaft.fldShomareDaryaft
						FROM            dbo.tblSodooreSanad INNER JOIN
												 dbo.tblDaryaft ON dbo.tblSodooreSanad.fldSanad = dbo.tblDaryaft.fldShomareSanad
						WHERE        (dbo.tblDaryaft.fldShomareSanad = @fldShomareSanad))
				if @fldShomareDaryaft is null
					set @fldShomareDaryaft=0
END

GO
/****** Object:  StoredProcedure [dbo].[GetShomarePardakhtByfldSanad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[GetShomarePardakhtByfldSanad]
	@fldShomareSanad bigint,
	@fldShomarePardakht bigint output
AS
BEGIN

	SET NOCOUNT ON;
				set @fldShomarePardakht=(	SELECT        TOP (1) dbo.tblPardakht.fldShomarePardakht
						FROM            dbo.tblSodooreSanad INNER JOIN
												 dbo.tblPardakht ON dbo.tblSodooreSanad.fldSanad = dbo.tblPardakht.fldShomareSanad
						WHERE        (dbo.tblPardakht.fldShomareSanad = @fldShomareSanad))
				if @fldShomarePardakht is null
					set @fldShomarePardakht=0
END

GO
/****** Object:  StoredProcedure [dbo].[GetStuffGroupBenefitFifo]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[GetStuffGroupBenefitFifo](@DateFROM BIGINT,@DateTo BIGINT, @ListCodeAnbar VARCHAR(5000),@fldCodekala bigint, @fldCodeGroup bigint)
AS
BEGIN
	DECLARE	@LastDurationResult Table([CodeKala] bigint,[CodeGroup] bigint, SumBuyAmount decimal,SumBuyCount bigint,
		SumSellAmount decimal, SumSellCount bigint, SumReSellAmount decimal, SumReSellCount decimal, RemaindCount bigint,
		RemaindAtEnd bigint, StuffRemaindAtEnd bigint, BahayeTamamShodeFifo decimal, Benefit decimal);

	DECLARE	@CurrentDurationResult Table([CodeKala] bigint,[CodeGroup] bigint, SumBuyAmount decimal,SumBuyCount bigint,
	SumSellAmount decimal, SumSellCount bigint, SumReSellAmount decimal, SumReSellCount decimal, RemaindCount bigint,
	RemaindAtEnd bigint, StuffRemaindAtEnd bigint, BahayeTamamShodeFifo decimal, Benefit decimal);

	DECLARE	@AllDurationResult Table([CodeKala] bigint,[CodeGroup] bigint, SumBuyAmount decimal,SumBuyCount bigint,
		SumSellAmount decimal, SumSellCount bigint, SumReSellAmount decimal, SumReSellCount decimal, RemaindCount bigint,
		RemaindAtEnd bigint, StuffRemaindAtEnd bigint, BahayeTamamShodeFifo decimal, Benefit decimal);


	INSERT INTO @CurrentDurationResult
	EXEC	[SoodeGroupFifo] @DateFrom, @DateTo, @ListCodeAnbar,@fldCodekala, @fldCodeGroup
	
	DECLARE @endDate bigint = @DateFROM -1

	INSERT INTO @LastDurationResult
	EXEC	[SoodeGroupFifo] 0, @endDate, @ListCodeAnbar,@fldCodekala, @fldCodeGroup

		
	INSERT INTO @AllDurationResult
	EXEC	[SoodeGroupFifo] 0,@DateTo , @ListCodeAnbar,@fldCodekala, @fldCodeGroup

	SELECT	Row_NUMBER() Over (ORDER BY [Current].[CodeKala]) [RowNo],[Current].[CodeKala], [STUFF].fldNameKala StuffName, [Current].[CodeGroup],  [StuffGroup].fldGroupName GroupName,[Current].SumBuyAmount,
			[Current].SumBuyCount, [Current].SumSellAmount,
			[Current].SumSellCount, [Current].SumReSellAmount, [Current].SumReSellCount, [Current].RemaindCount,
			isnull([Current].RemaindAtEnd,0) RemaindAtEnd, [Current].StuffRemaindAtEnd, [Current].BahayeTamamShodeFifo,
			(ISNULL([ALL].Benefit,0) - ISNULL([Last].Benefit,0)) Benefit
	FROM	@CurrentDurationResult [Current]
			LEFT JOIN @AllDurationResult [All] ON [Current].CodeKala=[All].CodeKala
			LEFT JOIN @LastDurationResult [Last] ON [Current].CodeKala=[Last].CodeKala
			INNER JOIN tblKala [Stuff] ON [Current].CodeKala=[Stuff].fldCodeKala
			INNER JOIN tblGroupKala [StuffGroup] ON [Stuff].fldCodeGroup=[StuffGroup].fldGroupId
	ORDER BY [Current].[CodeKala]
END
GO
/****** Object:  StoredProcedure [dbo].[GetTanzimatForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetTanzimatForoosh]
--چندزبانه
AS
BEGIN
	SELECT top 1 [ID]
      ,[fldKalaTekrary]
      ,[fldTypeInputKalaInGrid]
      ,[fldAllowForooshKalaBishAzMojodi]
      ,[fldShowMotalebatInFactorForoosh]
      ,[fldForooshNaghdi]
      ,[fldShowSodKala]
      ,[fldShowSodFactor]
      ,[fldEshantion]
      ,[fldChandGheymate]
      ,[fldArzeshAfzode]
      ,[fldTedadChapInForoosh]
      ,[fldSearchNumberingInKala]
      ,[fldBeginDateTakhfifat]
      ,[fldEndDateTakhfifat]
  FROM [dbo].[tblTanzimatForoosh]
  order by ID DESC
END

GO
/****** Object:  StoredProcedure [dbo].[GetTedadJozKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetTedadJozKala]
@fldCodeKala bigint,
@fldTedadJoz decimal(18,2) output
AS
BEGIN
	
	set @fldTedadJoz=(SELECT fldTedadJoz from tblKala
	 where fldCodeKala=@fldCodeKala)
end

GO
/****** Object:  StoredProcedure [dbo].[HasAmalkard]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[HasAmalkard]

@Meghdar bigint,
@NameTable nvarchar(20),
@TedadTekrari int output
as

begin
	declare @RowCount int
	if @NameTable='Vahed' 
		begin
			set @RowCount =(Select Count(fldCodeVahed1) from tblKala Where fldCodeVahed1=@meghdar)
			if (@RowCount>0 )
				begin
					set @Tedadtekrari=@Rowcount
				end
			else
				begin
					set @RowCount =(Select Count(fldCodeVahed2) from tblKala Where fldCodeVahed2=@meghdar)
					set @Tedadtekrari=@Rowcount
				end
		end


end

GO
/****** Object:  StoredProcedure [dbo].[HasAmalkardAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[HasAmalkardAnbar]
	@CodeAnbar bigint,
	@Amalkard nvarchar(10) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	declare @intCount bigint

	set @Amalkard ='false'

	set @intCount =(select Count(fldCodeKala) from tblRizeForoosh Where fldCodeAnbar=@CodeAnbar)	
	if @intCount>0 
		begin
			set @Amalkard ='true'
			return
		end

	set @intCount =(select Count(fldCodeKala) from tblRizeKharid Where fldCodeAnbar=@CodeAnbar)	
	if @intCount>0 
		begin
			set @Amalkard ='true'
			return
		end

	set @intCount =(select Count(fldCodeKala) from tblRizeBargashtAzForoosh Where fldCodeAnbar=@CodeAnbar)	
	if @intCount>0 
		begin
			set @Amalkard ='true'
			return
		end

	set @intCount =(select Count(fldCodeKala) from tblRizeBargashtAzKharid Where fldCodeAnbar=@CodeAnbar)	
	if @intCount>0 
		begin
			set @Amalkard ='true'
			return
		end
END

GO
/****** Object:  StoredProcedure [dbo].[HasAmalkardAnbarNew]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[HasAmalkardAnbarNew]
	@CodeAnbar bigint,
	@Amalkard nvarchar(10) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	declare @intCount bigint

	set @Amalkard ='false'

	--set @intCount =(select Count(fldCodeKala) from tblRizeForoosh Where fldCodeAnbar=@CodeAnbar)	
	--if @intCount>0 
	--	begin
	--		set @Amalkard ='true'
	--		return
	--	end

	--set @intCount =(select Count(fldCodeKala) from tblRizeKharid Where fldCodeAnbar=@CodeAnbar)	
	--if @intCount>0 
	--	begin
	--		set @Amalkard ='true'
	--		return
	--	end

	--set @intCount =(select Count(fldCodeKala) from tblRizeBargashtAzForoosh Where fldCodeAnbar=@CodeAnbar)	
	--if @intCount>0 
	--	begin
	--		set @Amalkard ='true'
	--		return
	--	end

	--set @intCount =(select Count(fldCodeKala) from tblRizeBargashtAzKharid Where fldCodeAnbar=@CodeAnbar)	
	--if @intCount>0 
	--	begin
	--		set @Amalkard ='true'
	--		return
	--	end
END

GO
/****** Object:  StoredProcedure [dbo].[HasAmalkardBankMoshtari]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[HasAmalkardBankMoshtari]
	-- Add the parameters for the stored procedure here
	@CodeBank bigint,
	@Amalkard nvarchar(10) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	declare @intCount bigint

	set @intCount =(Select Count(fldCodeBank) from tblCheckeDaryafti Where fldCodeBank =@CodeBank)
	if @intCount>0 
		set @Amalkard='true'
	else
		set @Amalkard='false'
END

GO
/****** Object:  StoredProcedure [dbo].[HasAmalkardeDaryaftMain]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[HasAmalkardeDaryaftMain]
@ShomareDaryaft bigint,
@Khorooji nvarchar(50) output
as
begin
	declare @ShomareSanad nvarchar(50),@VaziatVosool nvarchar(15)

	set @ShomareSanad=(Select fldShomareSanad from tblDaryaft Where fldShomareDaryaft=@ShomareDaryaft)
	if isnumeric(@shomareSanad)=1
		begin
			set @Khorooji=(Select fldType from tblSodooreSanad Where fldSanad=@ShomareSanad and fldRadif='1')
		end

	set @VaziatVosool=(Select top(1) fldVaziat from tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft and fldVaziat is not null)
	
	if @VaziatVosool is not null
		set @Khorooji=	@VaziatVosool
	else
		begin
			set @VaziatVosool=(Select top(1) fldShomarePardakht from tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft and fldShomarePardakht is not null)	
			if @VaziatVosool is not null
				set @Khorooji=	'forooshecheck'
		end

		
		
end

GO
/****** Object:  StoredProcedure [dbo].[HasAmalkardePardakhtMain]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[HasAmalkardePardakhtMain]
@ShomarePardakht bigint,
@Khorooji nvarchar(50) output
as
begin
	declare @ShomareSanad nvarchar(50)

	set @ShomareSanad=(Select fldShomareSanad from tblPardakht Where fldShomarePardakht=@ShomarePardakht)
	if isnumeric(@shomareSanad)=1
		begin
			set @Khorooji=(Select fldType from tblSodooreSanad Where fldSanad=@ShomareSanad and fldRadif='1')
		end
end

GO
/****** Object:  StoredProcedure [dbo].[HasAmalkardKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[HasAmalkardKala] 
	@CodeKala bigint,
	@Amalkard nvarchar(10) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @lngCount bigint
	
	set @lngcount=(Select Count(fldCodeKala) from tblRizeForoosh Where fldCodeKala=@CodeKala )

	set @Amalkard='false'
	if @lngCount>0
		begin
			set @Amalkard='true'
			return
		end

	set @lngcount=(Select Count(fldCodeKala) from tblRizeKharid Where fldCodeKala=@CodeKala )
	if @lngCount>0
		begin
			set @Amalkard='true'
			return
		end

	set @lngcount=(Select Count(fldCodeKala) from tblRizeBargashtAzForoosh Where fldCodeKala=@CodeKala )
	if @lngCount>0
		begin
			set @Amalkard='true'
			return
		end

	set @lngcount=(Select Count(fldCodeKala) from tblRizeBargashtAzKharid Where fldCodeKala=@CodeKala )	
	if @lngCount>0
		begin
			set @Amalkard='true'
			return
		end

	set @lngcount=(Select Count(fldCodeKala) from tblRizePishForoosh Where fldCodeKala=@CodeKala )	
	if @lngCount>0
		begin
			set @Amalkard='true'
			return
		end

	set @lngcount=(Select Count(fldCodeKala) from tblRizePishPishForoosh Where fldCodeKala=@CodeKala )	
	if @lngCount>0
		begin
			set @Amalkard='true'
			return
		end

	set @lngcount=(Select Count(fldCodeKalaForoosh) from tblEshantionTabaghati Where fldCodeKalaForoosh=@CodeKala )	
	if @lngCount>0
		begin
			set @Amalkard='true'
			return
		end

	set @lngcount=(Select Count(fldCodeKalaEshantion) from tblEshantionTabaghati Where fldCodeKalaEshantion=@CodeKala )	
	if @lngCount>0
		begin
			set @Amalkard='true'
			return
		end

END

GO
/****** Object:  StoredProcedure [dbo].[HasAmalkardKolMoin]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[HasAmalkardKolMoin]

@CodeKol nvarchar(50),
@CountRecord int output
as
begin
	declare @RowCountBank int
	begin
		set @RowCountBank=(Select Count(fldMoinKamel) from tblSodooreSanad  Where fldMoinKamel=@CodeKol)
		set @CountRecord=@RowCountBank
	end
end

GO
/****** Object:  StoredProcedure [dbo].[HasAmalkardSuperVizor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[HasAmalkardSuperVizor] 
	-- Add the parameters for the stored procedure here
	@CodeTafzili nvarchar(10),
	@strHasAmalkard nvarchar(20) output
AS
BEGIN
	declare @bigCount bigint
	set @bigCount=(Select count(id) from tblTafzili Where fldCodeSuperVisor=@CodeTafzili)
	
	set @strHasAmalkard='false'
	if @bigCount>0 		
		set @strHasAmalkard='true'
	else
		set @bigCount=(Select count(id) from tblSodooreSanad Where fldCodeTafzili=@CodeTafzili)
			if @bigCount>0 				
				set @strHasAmalkard='true'			
				



END

GO
/****** Object:  StoredProcedure [dbo].[HasAmalkardVizitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[HasAmalkardVizitor] 
	-- Add the parameters for the stored procedure here
	@CodeTafzili nvarchar(10),
	@strHasAmalkard nvarchar(20) output
AS
BEGIN
	declare @bigCount bigint
	set @bigCount=(Select count(id) from tblRizeForoosh Where fldCodeVasete=@CodeTafzili)
	
	set @strHasAmalkard='false'
	if @bigCount>0 		
		set @strHasAmalkard='true'
	else
		set @bigCount=(Select count(id) from tblSodooreSanad Where fldCodeTafzili=@CodeTafzili or fldVasete=@CodeTafzili)
			if @bigCount>0 				
				set @strHasAmalkard='true'						
				



END

GO
/****** Object:  StoredProcedure [dbo].[HesabeHazinePardakht]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[HesabeHazinePardakht] 
	@ShomareSanad bigint,
	@CodeKolHazine nvarchar(10),
	@CodeKolMoinHazine nvarchar(10) output,
	@SharheHesab nvarchar(50) output

AS
BEGIN
	set @CodeKolMoinHazine=(Select  top(1)fldMoinKamel from tblSodooreSanad Where fldSanad=@ShomareSanad and fldCodeKol like @CodeKolHazine + '%')
	set @SharheHesab=(Select  top(1)fldSharheHesab from tblKolMoin Where fldCodeHesab=@CodeKolMoinHazine)
END

GO
/****** Object:  StoredProcedure [dbo].[HesabeSandooghBankPardakht]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[HesabeSandooghBankPardakht] 
	@ShomareSanad bigint,	
	@CodeKolMoinHesab nvarchar(10) output,
	@SharheHesab nvarchar(50) output

AS
BEGIN
	
	declare @CodeKolSandoogh  nvarchar(10),@CodeKolBank nvarchar(10)

	execute CodeKolHesab 'bank',@CodeKolBank output
	execute CodeKolHesab 'sandoogh',@CodeKolSandoogh output

	set @CodeKolMoinHesab=(Select fldMoinKamel from tblSodooreSanad Where fldSanad=@ShomareSanad and (fldCodeKol=@CodeKolSandoogh or fldCodeKol=@CodeKolBank) and fldBedehkar>0)
	set @SharheHesab=(Select fldSharheHesab from tblKolMoin Where fldCodeHesab=@CodeKolMoinHesab)
END

GO
/****** Object:  StoredProcedure [dbo].[IDOfPrinter]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[IDOfPrinter] 
	-- Add the parameters for the stored procedure here
	@NamePrinter nvarchar(max),
	@IDPrinter bigint output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @ID1  bigint
	
	set @id1 =(Select top(1) id from tblPrinters Where fldNamePrinter=@NamePrinter )
    if @id1 is null
		set @IDPrinter=0
	else
		set @IDPrinter =@ID1
END

GO
/****** Object:  StoredProcedure [dbo].[InfoPoorsantVizitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InfoPoorsantVizitor] 
--چندزبانه
	@CodeTafzili nvarchar(10),
	@fldNoeMohasebePoorsant int output,
	@fldCodeSuperVisor nvarchar(10) output,
	@fldPoorsant decimal(18,4) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	set @fldNoeMohasebePoorsant=(Select fldNoeMohasebePoorsant from tblTafzili Where fldCodeTafzili = @CodeTafzili)
	set @fldCodeSuperVisor=(Select isnull(fldCodeSuperVisor ,'') from tblTafzili Where fldCodeTafzili = @CodeTafzili)
	set @fldPoorsant=(Select fldPoorsant from tblTafzili Where fldCodeTafzili = @CodeTafzili)
	
END

GO
/****** Object:  StoredProcedure [dbo].[Insert_Tbl_DaftarcheTell]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  StoredProcedure [dbo].[checkIdNumber_GruopAshkhas]    Script Date: 09/30/2019 11:52:52 ق.ظ ******/
CREATE proc [dbo].[Insert_Tbl_DaftarcheTell]
@fldCodeTafzili	nvarchar(10),
@fldName	nvarchar(50),
@fldShomare	nvarchar(50),
@fldSms	nvarchar(20),
@fldSharh	nvarchar(100),
@flagSabt int output	
as
begin

insert into tblDaftarcheTell (fldCodeTafzili, fldName, fldShomare, fldSms, fldSharh)
values(
 @fldCodeTafzili,
 @fldName,
 @fldShomare,
 @fldSms,
 @fldSharh) 
 
set @flagsabt=1
end

GO
/****** Object:  StoredProcedure [dbo].[Insert_Tbl_DaftarcheTell_has_CodeTafsili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Insert_Tbl_DaftarcheTell_has_CodeTafsili]
@fldCodeTafzili	nvarchar(10),
@fldName nvarchar(50),
@fldShomare	nvarchar(50),
@fldSms	nvarchar(20),
@fldSharh	nvarchar(100),
@flagSabt int output	
as
begin

insert into tblDaftarcheTell( fldCodeTafzili, fldName, fldShomare, fldSms, fldSharh)
values(
 @fldCodeTafzili,
 @fldName,
 @fldShomare,
 @fldSms,
 @fldSharh) 
 
set @flagsabt=1
end

GO
/****** Object:  StoredProcedure [dbo].[Insert_Tbl_Tafzili_final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Insert_Tbl_Tafzili_final]
--چندزبانه
@fldNoeHesab	nvarchar(15),	
@fldSharheTafzili	nvarchar(200),
--@fldCodeTafzili	nvarchar(10),	
@fldCodeNoe	nvarchar(5),	--set "1"
@fldNoe	nvarchar(10),	
@fldCodeGroup	bigint,	        
@fldOstan	nvarchar(50),	
@fldCity	nvarchar(50),	
@fldAddress	nvarchar(200),	
@fldShomareHesab	nvarchar(20),	
@fldCodeMeli	nvarchar(20),	
@fldCodeEghtesadi	nvarchar(20),	
@fldCodePosti	nvarchar(20),	
@fldShomareSabt	nvarchar(20),	
@fldSaghfeEtebar	decimal(18,4),
@fldBlackList	nvarchar(5),	
@fldSharhBlackList	nvarchar(100),	
@fldDate1	bigint,	
@fldDate2	bigint,	
@fldVisitor	bigint,	
@fldPic	nvarchar(300),
@fldNoeVizitor int,
@fldPoorsant decimal(18,4),
@fldCodeSuperVisor nvarchar(10),
@fldNoeMohasebePoorsant int,
@flagSabtOK int output,
@iDTafsiliReturned bigint output,
@flagReturnChkCodeTafzili int output 

as
begin
set @flagSabtOK=0
declare @LenNumber int
declare @TafziliCode_Create bigint
declare @CountTafzili_in_TblTafzili int
declare @MaxCodeTafzili bigint
declare @chkCodeTafzili bigint 
Declare @Convert_TafziliCode_Create nvarchar(10)
Declare @Convert_TafziliCode_Create2 nvarchar(10)

set @fldVisitor=isnull(@fldVisitor,0)



set @LenNumber=(select fldTafzili  from tblPaye)

set @CountTafzili_in_TblTafzili=(select COUNT(CONVERT(bigint,fldCodeTafzili)) from tblTafzili)

if(@CountTafzili_in_TblTafzili =0)
 begin 
  if(@LenNumber >0)
    begin
      set @TafziliCode_Create=POWER(10,@lennumber-1)
        set @TafziliCode_Create=@TafziliCode_Create+1

		--
		  set  @Convert_TafziliCode_Create=CONVERT(nvarchar(10),@TafziliCode_Create)

		insert into tblTafzili(
				fldNoeHesab,fldSharheTafzili,fldCodeTafzili,fldCodeNoe,
				fldNoe,fldCodeGroup,fldOstan,fldCity,fldAddress,
				fldShomareHesab,fldCodeMeli,fldCodeEghtesadi,
				fldCodePosti,fldShomareSabt,fldSaghfeEtebar,
				fldBlackList,fldSharhBlackList,
				fldDate1,fldDate2,
				fldVisitor,
				fldPic,fldNoeVizitor,fldPoorsant,fldCodeSuperVisor,fldNoeMohasebePoorsant)

				values (@fldNoeHesab,@fldSharheTafzili,@Convert_TafziliCode_Create,@fldCodeNoe,
				@fldNoe,@fldCodeGroup,@fldOstan,@fldCity,@fldAddress,
				@fldShomareHesab,@fldCodeMeli,@fldCodeEghtesadi,
				@fldCodePosti,@fldShomareSabt,@fldSaghfeEtebar,
				@fldBlackList,@fldSharhBlackList,
				@fldDate1,@fldDate2,
				@fldVisitor,
				@fldPic,@fldNoeVizitor,@fldPoorsant,@fldCodeSuperVisor,@fldNoeMohasebePoorsant)
              
			  set @flagSabtOK=1
			  set @iDTafsiliReturned=@TafziliCode_Create
		--
    end
  else
    begin
      return
    end 
 end
else if @CountTafzili_in_TblTafzili > 0 ---یعنی کد قبل از  ایجاد شده و باید یه رقم به آن اضافه شود	 
begin

  set @MaxCodeTafzili=(select MAX(CONVERT(bigint,fldCodeTafzili)) from tblTafzili)

------------------------------------------------------------ باید چک شود کد تفیلی خارج از محدوده نباشد
 set @chkCodeTafzili=POWER(10,@lennumber)-1  -- orginal
                                                   
 --if  @chkCodeTafzili <=  @MaxCodeTafzili  --orginal Error
 if  @MaxCodeTafzili >=  @chkCodeTafzili -- Fixed 96-03-01 Is Worked OK!
 begin
    set @flagReturnChkCodeTafzili=1
     return
 end
------------------------------------------------------------
set @MaxCodeTafzili=@MaxCodeTafzili+1

set  @Convert_TafziliCode_Create2=CONVERT(nvarchar(10),@MaxCodeTafzili)
insert into tblTafzili
(
				fldNoeHesab,fldSharheTafzili,fldCodeTafzili,fldCodeNoe,
				fldNoe,fldCodeGroup,fldOstan,fldCity,fldAddress,
				fldShomareHesab,fldCodeMeli,fldCodeEghtesadi,
				fldCodePosti,fldShomareSabt,fldSaghfeEtebar,
				fldBlackList,fldSharhBlackList,
				fldDate1,fldDate2,
				fldVisitor,
				fldPic,fldNoeVizitor,fldPoorsant,fldCodeSuperVisor,fldNoeMohasebePoorsant)
values (@fldNoeHesab,@fldSharheTafzili,@Convert_TafziliCode_Create2,@fldCodeNoe,
@fldNoe,@fldCodeGroup,@fldOstan,@fldCity,@fldAddress,
@fldShomareHesab,@fldCodeMeli,@fldCodeEghtesadi,
@fldCodePosti,@fldShomareSabt,@fldSaghfeEtebar,
@fldBlackList,@fldSharhBlackList,
@fldDate1,@fldDate2,
@fldVisitor,
@fldPic,@fldNoeVizitor,@fldPoorsant,@fldCodeSuperVisor,@fldNoeMohasebePoorsant)

                   set @flagSabtOK=1
				     set @iDTafsiliReturned=@MaxCodeTafzili
end 

END

GO
/****** Object:  StoredProcedure [dbo].[Insert_Tbl_Tafzili_Personel]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[Insert_Tbl_Tafzili_Personel]
@fldNoeHesab	nvarchar(15),	
@fldSharheTafzili	nvarchar(200),
--@fldCodeTafzili	nvarchar(10),	
@fldCodeNoe	nvarchar(5),	--set "1"
@fldNoe	nvarchar(10),	
@fldCodeGroup	bigint,	        
@fldOstan	nvarchar(50),	
@fldCity	nvarchar(50),	
@fldAddress	nvarchar(200),	
@fldShomareHesab	nvarchar(20),	
@fldCodeMeli	nvarchar(20),	
@fldCodeEghtesadi	nvarchar(20),	
@fldCodePosti	nvarchar(20),	
@fldShomareSabt	nvarchar(20),	
@fldSaghfeEtebar	decimal,
@fldBlackList	nvarchar(5),	
@fldSharhBlackList	nvarchar(100),	
@fldDate1	bigint,	
@fldDate2	bigint,	
@fldVisitor	bigint,	
@fldPic	nvarchar(300),
@fldNoeVizitor int,
@fldPoorsant int,
@fldCodeSuperVisor nvarchar(10),
@fldNoeMohasebePoorsant int,
@fldNoePersonel nvarchar(20),
@flagSabtOK int output,
@iDTafsiliReturned bigint output,
@flagReturnChkCodeTafzili int output 

as
begin
set @flagSabtOK=0
declare @LenNumber int
declare @TafziliCode_Create bigint
declare @CountTafzili_in_TblTafzili int
declare @MaxCodeTafzili bigint
declare @chkCodeTafzili bigint 
Declare @Convert_TafziliCode_Create nvarchar(10)
Declare @Convert_TafziliCode_Create2 nvarchar(10)

set @fldVisitor=isnull(@fldVisitor,0)



set @LenNumber=(select fldTafzili  from tblPaye)

set @CountTafzili_in_TblTafzili=(select COUNT(CONVERT(bigint,fldCodeTafzili)) from tblTafzili)

if(@CountTafzili_in_TblTafzili =0)
 begin 
  if(@LenNumber >0)
    begin
      set @TafziliCode_Create=POWER(10,@lennumber-1)
        set @TafziliCode_Create=@TafziliCode_Create+1

		--
		  set  @Convert_TafziliCode_Create=CONVERT(nvarchar(10),@TafziliCode_Create)

		insert into tblTafzili(
				fldNoeHesab,fldSharheTafzili,fldCodeTafzili,fldCodeNoe,
				fldNoe,fldCodeGroup,fldOstan,fldCity,fldAddress,
				fldShomareHesab,fldCodeMeli,fldCodeEghtesadi,
				fldCodePosti,fldShomareSabt,fldSaghfeEtebar,
				fldBlackList,fldSharhBlackList,
				fldDate1,fldDate2,
				fldVisitor,
				fldPic,fldNoeVizitor,fldPoorsant,fldCodeSuperVisor,fldNoeMohasebePoorsant,fldNoePersonel)

				values (@fldNoeHesab,@fldSharheTafzili,@Convert_TafziliCode_Create,@fldCodeNoe,
				@fldNoe,@fldCodeGroup,@fldOstan,@fldCity,@fldAddress,
				@fldShomareHesab,@fldCodeMeli,@fldCodeEghtesadi,
				@fldCodePosti,@fldShomareSabt,@fldSaghfeEtebar,
				@fldBlackList,@fldSharhBlackList,
				@fldDate1,@fldDate2,
				@fldVisitor,
				@fldPic,@fldNoeVizitor,@fldPoorsant,@fldCodeSuperVisor,@fldNoeMohasebePoorsant,@fldNoePersonel)
              
			  set @flagSabtOK=1
			  set @iDTafsiliReturned=@TafziliCode_Create
		--
    end
  else
    begin
      return
    end 
 end
else if @CountTafzili_in_TblTafzili > 0 ---یعنی کد قبل از  ایجاد شده و باید یه رقم به آن اضافه شود	 
begin

  set @MaxCodeTafzili=(select MAX(CONVERT(bigint,fldCodeTafzili)) from tblTafzili)

------------------------------------------------------------ باید چک شود کد تفیلی خارج از محدوده نباشد
 set @chkCodeTafzili=POWER(10,@lennumber)-1  -- orginal
                                                   
 --if  @chkCodeTafzili <=  @MaxCodeTafzili  --orginal Error
 if  @MaxCodeTafzili >=  @chkCodeTafzili -- Fixed 96-03-01 Is Worked OK!
 begin
    set @flagReturnChkCodeTafzili=1
     return
 end
------------------------------------------------------------
set @MaxCodeTafzili=@MaxCodeTafzili+1

set  @Convert_TafziliCode_Create2=CONVERT(nvarchar(10),@MaxCodeTafzili)
insert into tblTafzili
(
				fldNoeHesab,fldSharheTafzili,fldCodeTafzili,fldCodeNoe,
				fldNoe,fldCodeGroup,fldOstan,fldCity,fldAddress,
				fldShomareHesab,fldCodeMeli,fldCodeEghtesadi,
				fldCodePosti,fldShomareSabt,fldSaghfeEtebar,
				fldBlackList,fldSharhBlackList,
				fldDate1,fldDate2,
				fldVisitor,
				fldPic,fldNoeVizitor,fldPoorsant,fldCodeSuperVisor,fldNoeMohasebePoorsant,fldNoePersonel)
values (@fldNoeHesab,@fldSharheTafzili,@Convert_TafziliCode_Create2,@fldCodeNoe,
@fldNoe,@fldCodeGroup,@fldOstan,@fldCity,@fldAddress,
@fldShomareHesab,@fldCodeMeli,@fldCodeEghtesadi,
@fldCodePosti,@fldShomareSabt,@fldSaghfeEtebar,
@fldBlackList,@fldSharhBlackList,
@fldDate1,@fldDate2,
@fldVisitor,
@fldPic,@fldNoeVizitor,@fldPoorsant,@fldCodeSuperVisor,@fldNoeMohasebePoorsant,@fldNoePersonel)

                   set @flagSabtOK=1
				     set @iDTafsiliReturned=@MaxCodeTafzili
end 

END

GO
/****** Object:  StoredProcedure [dbo].[InsertAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertAnbar]

@IDAnbar bigInt,
@NameAnbar nvarChar(70),
@flagSabt int output,
@IDOutPut int outPut
as 

begin
	set @IDAnbar= (Select Max(fldCode) from tblAnbar where fldCode<2000)

	if ISNUMERIC(@IDAnbar)=1
		Set @IDAnbar+=1
	else
		Set @IDAnbar=1000

	insert into tblAnbar 
		values(@IDAnbar,@NameAnbar)
	set @flagSabt=@IDAnbar
	set @IDOutPut=@@IDENTITY
end

GO
/****** Object:  StoredProcedure [dbo].[InsertAnbarAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertAnbarAmani]

@IDAnbar bigInt,
@NameAnbar nvarChar(70),
@CodeTafzili bigint,
@flagSabt int output,
@IDOutPut int outPut
as 

begin
	set @IDAnbar= (Select Max(fldCode) from tblAnbar where fldCode>=2000)

	if ISNUMERIC(@IDAnbar)=1
		Set @IDAnbar+=1
	else
		Set @IDAnbar=2000

	insert into tblAnbar 
		values(@IDAnbar,@NameAnbar)

	insert into tblAnbarAmani
		(fldCodeAnbar,fldCodeTafzili)
		values	(@IDAnbar,@CodeTafzili)

	set @flagSabt=@IDAnbar
	set @IDOutPut=@@IDENTITY
end

GO
/****** Object:  StoredProcedure [dbo].[InsertBankeMoshtari]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[InsertBankeMoshtari]

@NameBank nvarChar(70),
@IDOutPut int outPut
as 

begin

	insert into tblBankeMoshtari 
		values(@NameBank)	
	set @IDOutPut=@@IDENTITY
end

GO
/****** Object:  StoredProcedure [dbo].[insertDasteCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[insertDasteCheck]
@daste tblDaftarCheck ReadOnly

as

begin
	insert into tblDasteCheck (fldCodeBank,fldShobe,fldShomareCheck,fldShomareHesab)
		Select fldCodeBank,fldShobe,fldShomareCheck,fldShomareHesab from @daste
end

GO
/****** Object:  StoredProcedure [dbo].[InsertDastrasi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger: SQLQuery7.sql|7|0|C:\Users\marashi2\AppData\Local\Temp\~vsD567.sql
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertDastrasi] 
	-- Add the parameters for the stored procedure here
	@CodeUser bigint,
	@CodeDastrasi bigint,
	@Check int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


    -- Insert statements for procedure here
	if @check=0 --یعنی پاک بشه
		delete from tblDastrasi Where fldCodeUser=@CodeUser and fldCodeDastrasi=@CodeDastrasi
	else if @check=1
		begin
			insert into tblDastrasi
				(fldCodeUser,fldCodeDastrasi)
				values
					(@CodeUser,@CodeDastrasi)
		end
	else if @check=2 --UnCheckaLL
		begin
			DELETE from tbldastrasi	
				where fldCodeUser=@CodeUser
		end
	else if @Check=3 --CheckAll
		begin
			DELETE from tbldastrasi	
				where fldCodeUser=@CodeUser
			insert into tbldastrasi	
				(fldCodeUser,fldCodeDastrasi)
				Select @CodeUser,fldCode from  tblListeDastrasi
		end
END

GO
/****** Object:  StoredProcedure [dbo].[InsertDastrasiSandoogh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertDastrasiSandoogh] 
	@CodeUser bigint,
	@CodeSandoogh bigint,
	@CodeBank bigint,	
	@ElzamiSandoogh  nvarchar(5) ,
	@ElzamiBank nvarchar(5) ,
	@NoeKhata nvarchar(10) output
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END

BEGIN
	begin transaction transact
	begin try

		Delete from tblDastrasiSandoogh Where fldCodeUser=@CodeUser
		
		insert tblDastrasiSandoogh
			(fldCodeUser,fldSandoogh,fldType,fldElzamiSandoogh)
			values
			(@CodeUser,@CodeSandoogh,'sandoogh',@ElzamiSandoogh)

		insert tblDastrasiSandoogh
			(fldCodeUser,fldSandoogh,fldType,fldElzamiBank)
			values
			(@CodeUser,@CodeBank,'bank',@ElzamiBank)

	commit transaction
	end try

	begin catch
		if @@trancount>0 
			rollback transaction ;
			set @NoeKhata='rolback'

	end catch
		
END

GO
/****** Object:  StoredProcedure [dbo].[InsertEbtedaDoreh_Ashkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertEbtedaDoreh_Ashkhas]
--چندزبانه
	@fldCodeArz int,
	@fldNerkheArz decimal(18,4),
    @dtSanadEbteda_Ashkas tpSanadEbteda_AshkasMulti  Readonly,
	@strResult  nvarchar(25) output,
    @InsertType nvarchar(50),
	@fldBedehkar decimal (18, 4),
	@fldBestankar  decimal (18, 4), 
	@fldRadif varchar(50),
	@fldCodeKolSarmaye varchar(50),
	@fldMoinKamelSarmaye varchar(50),
	@fldShmareSanad varchar(50)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	
	SET NOCOUNT ON;
	   Begin Transaction InsertRow
	     declare @lngCountMojodi bigint
    -- Insert statements for procedure here
	        Begin Try
			declare @DateSanad nvarchar(20),@day  nvarchar(20),@month  nvarchar(20),@year nvarchar(20)
			set @DateSanad=(select top(1) flddate from @dtSanadEbteda_Ashkas )
			set @year=SUBSTRING(@DateSanad,1,4)
			set @month=SUBSTRING(@DateSanad,5,2)
			set @day=SUBSTRING(@DateSanad,7,2)

		    set @lngCountmojodi=(Select Count(fldSanad) from tblSodooreSanad Where  fldType='EbtedaAshkhas')
			 if (@lngCountMojodi = 0)
			   Begin			   
			   
		        if @InsertType='insert'
					begin						
						INSERT INTO [dbo].[tblSodooreSanad]
								(fldCodeArz
								,fldNerkheArz
								,[fldSanad]
								,[fldDay] 
								,[fldMonth]
								,[fldYear]
								,[fldCodeKol] 
								,[fldCodeMoin] 
								,[fldCodeTafzili] 
								,[fldBedehkar] 
								,[fldBestankar] 
								,[fldRadif] 
								,[fldDate]  
								,[fldSharh] 
								,[fldMoinKamel]
								,[fldCodeNoeTafzili]
								,[fldType])
								
								SELECT
								@fldCodeArz
								,@fldNerkheArz
								 ,[fldSanad]
								,[fldDay] 
								,[fldMonth]
								,[fldYear]
								,[fldCodeKol] 
								,[fldCodeMoin] 
								,[fldCodeTafzili] 
								,[fldBedehkar] 
								,[fldBestankar] 
								,[fldRadif] 
								,[fldDate]  
								,[fldSharh] 
								,[fldMoinKamel]
								,[fldCodeNoeTafzili]
								,[fldType]
								output from @dtSanadEbteda_Ashkas;

								
								
									INSERT INTO tblSodooreSanad                      --sarmaye
												(fldCodeArz,fldNerkheArz, fldSanad,fldCodeKol,fldCodeMoin,
													fldBedehkar,fldBestankar,fldRadif, 
													fldType,fldCodeTafzili,fldDate,fldDay,fldMonth,fldYear)
									values (@fldCodeArz,@fldNerkheArz, @fldShmareSanad,@fldCodeKolSarmaye,@fldMoinKamelSarmaye,
											@fldBedehkar,@fldBestankar,@fldRadif,'EbtedaAshkhas','',@DateSanad,@day,@month,@year)


							     set @strResult='insertOk'
                       End
                  End
                
				 if (@lngCountMojodi > 0)
			       Begin
				   Delete from tblSodooreSanad where fldType='EbtedaAshkhas'
		              if @InsertType='insert'
					begin
						INSERT INTO [dbo].[tblSodooreSanad]
								(fldCodeArz
								,fldNerkheArz
								,[fldSanad]
								,[fldDay] 
								,[fldMonth]
								,[fldYear]
								,[fldCodeKol] 
								,[fldCodeMoin] 
								,[fldCodeTafzili] 
								,[fldBedehkar] 
								,[fldBestankar] 
								,[fldRadif] 
								,[fldDate]  
								,[fldSharh] 
								,[fldMoinKamel]
								,[fldCodeNoeTafzili]
								,[fldType])
								
								SELECT
								@fldCodeArz
								,@fldNerkheArz
								 ,[fldSanad]
								,[fldDay] 
								,[fldMonth]
								,[fldYear]
								,[fldCodeKol] 
								,[fldCodeMoin] 
								,[fldCodeTafzili] 
								,[fldBedehkar] 
								,[fldBestankar] 
								,[fldRadif] 
								,[fldDate]  
								,[fldSharh] 
								,[fldMoinKamel]
								,[fldCodeNoeTafzili]
								,[fldType]
								output from @dtSanadEbteda_Ashkas;
								
								
									INSERT INTO tblSodooreSanad                      --sarmaye
												(fldCodeArz,fldNerkheArz, fldSanad,fldCodeKol,fldCodeMoin,
													fldBedehkar,fldBestankar,fldRadif, 
													fldType,fldCodeTafzili,fldDate,fldDay,fldMonth,fldYear)
									values (@fldCodeArz,@fldNerkheArz, @fldShmareSanad,@fldCodeKolSarmaye,@fldMoinKamelSarmaye,
											@fldBedehkar,@fldBestankar,@fldRadif,'EbtedaAshkhas','',@DateSanad,@day,@month,@year)

				
							     set @strResult='UpdateArchiveOk'
                          End
				   End

		     Commit Transaction InsertRow; 
		   End Try

			 Begin Catch
			   if @@TRANCOUNT > 0 
			     begin 
				  RollBack Transaction InsertRow;
				     set @strResult='Error'
				 end

			  End Catch
			  
END

GO
/****** Object:  StoredProcedure [dbo].[insertGroupAshkhas_m]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger: SQLQuery16.sql|7|0|C:\Users\ALISER~1\AppData\Local\Temp\~vsF5E.sql
CREATE proc [dbo].[insertGroupAshkhas_m]
@fldGroupAshkhasId bigint,
@fldGroupAshkhasName nvarchar(50),
@codlen int,
@flag int output,
@flagroot int  -- این فلگ مشخص میکنه که کدام دستورات باید اجرا شود

as


begin
declare @idnumber bigint
declare @idnumberconvert nvarchar(30)
declare @GroupName nvarchar(50)


set @flag=0

 

 if (@codlen=1)  --وقتی از دیتا گرید انتخاب نشود  ایجاد سرگروه سه رقمی
  begin
    set @idnumber=(select max(fldGroupId) from tblGroupAshkhas where LEN(fldGroupId)=3 )
    if not Exists(select fldGroupName  from tblGroupAshkhas where fldGroupName=@fldGroupAshkhasName and LEN(fldGroupId)=3)
     begin
		 set @idnumber=@idnumber+1
		 
		 insert into tblGroupAshkhas(fldGroupId,fldGroupName)
		     values(@idnumber,@fldGroupAshkhasName)
			 
			
	  end
	   else
	     begin
		  set @flag=1
		  return
		 end
		
   end

   -- زیر گروه اول

   --if (@codlen=3)
   if ((@codlen<>1) and (@flagroot=1 ))
  begin
   
	set @idnumber=(select max(fldGroupId) from tblGroupAshkhas where LEN(convert(nvarchar(50),fldGroupId))> = 3 and convert(nvarchar(50),fldGroupId) like  convert(nvarchar(50),@fldGroupAshkhasId) + '%')
   
		if not Exists(select fldGroupName  from tblGroupAshkhas where fldGroupName=@fldGroupAshkhasName and LEN(fldGroupId) > =3 and convert(nvarchar(50),fldGroupId) like  convert(nvarchar(50),@fldGroupAshkhasId) + '%')-- نام تکراری نباشد در هر سطح
	     begin
	     set @idnumberconvert=convert(nvarchar(30),@idnumber)+ '001'
		 set @idnumber=CONVERT(bigint,@idnumberconvert)
		 
		 insert into tblGroupAshkhas(fldGroupId,fldGroupName)
		     values(@idnumber,@fldGroupAshkhasName)
			 
			

	  end
	   else
	     begin
		  set @flag=1
		  return
		  end
		
   end

		   --
		  -- if (@codlen=6)
		   if ((@codlen <> 1) and (@flagroot=2)) --تولید زیر گروه جز
				begin
				 
				  set @idnumber=(select max(fldGroupId) from tblGroupAshkhas where LEN(convert(nvarchar(50),fldGroupId)) = len(@fldGroupAshkhasId)
							 and convert(nvarchar(50),fldGroupId) like  convert(nvarchar(50),@fldGroupAshkhasId) + '%' )

				 
				 declare @Name nvarchar(100)
				 set @Name=(select top(1) fldGroupName  from tblGroupAshkhas where fldGroupName=@fldGroupAshkhasName And Convert(varchar,fldGroupId) like  left(convert(varchar,@fldGroupAshkhasId),len(convert(varchar,@fldGroupAshkhasId))-3) + '%')  -- len(fldGroupId)=len(@fldGroupAshkhasId))
				 --if not Exists(select fldGroupName  from tblGroupAshkhas where fldGroupName=@fldGroupAshkhasName And len(fldGroupId)=len(@fldGroupAshkhasId) )
				 if @name is  null
					 begin
						set @idnumber=@idnumber+1
						
						
							 insert into tblGroupAshkhas(fldGroupId,fldGroupName)
								 values(@idnumber,@fldGroupAshkhasName)
								 
					 end
				 else
					 begin
					  set @flag=1 -- نام کالا در آن سطح مشابه میباشد
					  return
					 end
		
		   end
		 

		      end

GO
/****** Object:  StoredProcedure [dbo].[insertGroupKala_m]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger: SQLQuery16.sql|7|0|C:\Users\ALISER~1\AppData\Local\Temp\~vsF5E.sql
CREATE proc [dbo].[insertGroupKala_m]
@fldGroupKalaId bigint,
@fldGroupKalaName nvarchar(50),
@codlen int,
@flag int output,
@flagroot int  -- این فلگ مشخص میکنه که کدام دستورات باید اجرا شود
--@flagCheckIdInTblKala int output  --چک کردن ای دی وارد شده ایا در عمکرد کالا سابقه دارد یا خیر new

as


begin
declare @idnumber bigint
declare @idnumberconvert nvarchar(30)
declare @GroupName nvarchar(50)
--------------------------------------------------------------------new 95-09-03
--declare @hasRowInTblKala int --new

--set @hasRowInTblKala=0 --new 
--set @flagCheckIdInTblKala=0 -- new 
--set @hasRowInTblKala=(select count(fldCodeGroup) from tblKala where fldCodeGroup=@fldGroupKalaId)-- new 
--if (@hasRowInTblKala >= 1) --new 
--begin
--set @flagCheckIdInTblKala=1
--return 
--end 
------------------------------------------------------ new 95-09-03

set @flag=0

 --set @GroupName=(select fldGroupName  from tblGroupKala where fldGroupName=@fldGroupKalaName)
 --  selectیه سلکت از جدول کالا برای مشخص شدن اینکه این شماره کالا دارد یا ندارد
 if (@codlen=1)  --وقتی از دیتا گرید انتخاب نشود  ایجاد سرگروه سه رقمی
  begin
    set @idnumber=(select max(fldGroupId) from tblGroupKala where LEN(fldGroupId)=3 )
    if not Exists(select fldGroupName  from tblGroupKala where fldGroupName=@fldGroupKalaName and LEN(fldGroupId)=3)
     begin
		 set @idnumber=@idnumber+1
		 
		 insert into tblGroupKala(fldGroupId,fldGroupName)
		     values(@idnumber,@fldGroupKalaName)
			 
			 

	  end
	   else
	     begin
		  set @flag=1
		  return
		 end
		
   end

   -- زیر گروه اول

   --if (@codlen=3)
   if ((@codlen<>1) and (@flagroot=1 ))
  begin
   
	set @idnumber=(select max(fldGroupId) from tblGroupKala where LEN(convert(nvarchar(50),fldGroupId))> = 3 and convert(nvarchar(50),fldGroupId) like  convert(nvarchar(50),@fldGroupKalaId) + '%')
   
		if not Exists(select fldGroupName  from tblGroupKala where fldGroupName=@fldGroupKalaName and LEN(fldGroupId) > =3 and convert(nvarchar(50),fldGroupId) like  convert(nvarchar(50),@fldGroupKalaId) + '%')-- نام تکراری نباشد در هر سطح
	
     begin
	     set @idnumberconvert=convert(nvarchar(30),@idnumber)+ '001'
		 set @idnumber=CONVERT(bigint,@idnumberconvert)
		 
		 insert into tblGroupKala(fldGroupId,fldGroupName)
		     values(@idnumber,@fldGroupKalaName)
			 
			

	  end
	   else
	     begin
		  set @flag=1
		  return
		  end
		
   end

		   --
		  -- if (@codlen=6)
		   if ((@codlen <> 1) and (@flagroot=2)) --تولید زیر گروه جز
				begin
				 --set @idnumber=(select max(fldGroupId) from tblGroupKala where LEN(fldGroupId)> =3 and fldGroupId=@fldGroupKalaId)
				 --set @idnumber=(select max(fldGroupId) from tblGroupKala where LEN(convert(nvarchar(50),fldGroupId))> = 3 and convert(nvarchar(50),fldGroupId) like  convert(nvarchar(50),@fldGroupKalaId) + '%')

				  set @idnumber=(select max(fldGroupId) from tblGroupKala where LEN(convert(nvarchar(50),fldGroupId)) = len(@fldGroupKalaId)
							 and convert(nvarchar(50),fldGroupId) like  convert(nvarchar(50),@fldGroupKalaId) + '%' )

				 if not Exists(select fldGroupName  from tblGroupKala where fldGroupName=@fldGroupKalaName)
				  --and not Exists(select fldGroupId  from tblGroupKala where fldGroupId=@fldGroupKalaId)
					 begin
						set @idnumber=@idnumber+1
						
						
							 insert into tblGroupKala(fldGroupId,fldGroupName)
								 values(@idnumber,@fldGroupKalaName)
								 
					 end
				 else
					 begin
					  set @flag=1 -- نام کالا در آن سطح مشابه میباشد
					  return
					 end
		
		   end
		   --
		   --if(LEN(@idnumber) < 15)
		   -- begin
		   --set @returnIdInsert=@idnumber
		  
		   --return @returnIdInsert
		   --end
		 


     end

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertInfoAmani]
	@dtForoosh tpRizeForoosh_Amani2 readonly,
	@dtKharid tpRizeKharid_EnteghalNew readonly,
	@fldShomareAmani bigint,
	@fldCodeAnbar_in bigint,
	@fldCodeAshkhas_in bigint,
	@InsertType nvarchar(50),
	@strResult nvarchar(25) output  
	
AS
BEGIN

	SET NOCOUNT ON;

        begin transaction AddRecord
             declare @lngCountAmani bigint
			 declare @Date bigint, @CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@CodeMoin nvarchar(10),@decMablaghKolFactor decimal(18,0),@intRadif bigint,@dateSmall nvarchar(10),@day nvarchar(10),@month nvarchar(10),@year nvarchar(10),@TooleKol bigint,@Toolemoin bigint,@fldCodeVasete bigint,@strSharhKoli nvarchar(250),@fldShomareSanad bigint
             begin try  --try
							  set @fldCodeVasete=(select top(1) fldCodeVasete from @dtForoosh)	
			                  set @lngCountAmani=(Select Count(fldShomareAmani) from tblRizeForoosh Where fldShomareAmani=@fldShomareAmani)
							  set @decMablaghKolFactor=(Select Sum(fldMablaghKhales ) from @dtForoosh)
					          if ( @lngCountAmani = 0)
							    begin
									--INSERT INTO tblTest7(fld1, fld2, fld3)
									--					VALUES        ('2',2,2)

									if @InsertType='insert'
									  begin

									    execute  ToolePaye 'kol',@TooleKol output
										execute  ToolePaye 'moin',@Toolemoin output
										execute codemoinhesab 'forooshamani' ,@CodeMoin output
										set @CodeKolkham =substring(@CodeMoin, 1, @TooleKol)
										set @CodeMoinKham =substring(@CodeMoin,@TooleKol+1,@Toolemoin)											
										--set @dateSmall=(Select Top(1) substring(Convert(varchar,fldDate),3,6)  from @dtForoosh)
										set @Date=(Select Top(1) fldDate  from @dtForoosh)
										set @year=substring(convert(nvarchar(20),@Date),3,2)
										--set @yearFull=substring(convert(nvarchar(20),@Date),0,5)
										set @month=substring(convert(nvarchar(20),@Date),5,2)
										set @day=substring(convert(nvarchar(20),@Date),7,2)
										set @dateSmall=substring(convert(nvarchar(15),@Date),3,6)


										execute InsertSanadFirst @CodeKolkham,@CodeMoinKham,@CodeMoin,@fldCodeAshkhas_in,'1',@decMablaghKolFactor,0,1,@dateSmall,@strSharhKoli,@day,@month,@year,'amani',@fldCodeVasete,@fldShomareSanad output 

										execute CodeKolHesab'tarafeentezami' ,@CodeMoin output
										set @CodeKolkham =substring(@CodeMoin, 1, @TooleKol)
										set @CodeMoinKham =substring(@CodeMoin,@TooleKol+1,@Toolemoin)
										execute InsertSanadLast  @CodeKolkham,@CodeMoinKham,@CodeMoin,NULL,NULL,0,@decMablaghKolFactor,2,@dateSmall,@strSharhKoli,@day,@month,@year,'amani',@fldCodeVasete,@fldShomareSanad
									  INSERT INTO [dbo].[tblRizeForoosh]
										   ([fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,[fldCodeTafsili]
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,[fldCodeAnbar]
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,[fldShomareAmani]
										   ,[fldDateEngheza])
											SELECT
											@fldShomareSanad
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]	
										   ,@fldCodeAshkhas_in								   
										   ,[fldCodeUser]
										   ,[fldRadif]		
										   ,[fldCodeAnbar]							   
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]										   
										   ,@fldShomareAmani 
										   ,[fldDateEngheza]
										   output from @dtForoosh


									--ثبت انبار وارده ریز خرید
									   INSERT INTO [dbo].[tblRizeKharid]
												(
												--[fldCodeTafsili]
												--,[fldCodeUser]
												--,[fldRadif]
												--,[fldCodeAnbar]
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,[fldShomareAmani]
											[fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,[fldCodeTafsili]
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,[fldCodeAnbar]
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,[fldShomareAmani]
										   ,[fldDateEngheza])
												select
												--@fldCodeAshkhas_in
												--,[fldCodeUser]
												--,[fldRadif]
												--,@fldCodeAnbar_in
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,@fldShomareAmani
											@fldShomareSanad
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,@fldCodeAshkhas_in
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,@fldCodeAnbar_in
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,@fldShomareAmani 
										   ,[fldDateEngheza]
												output from @dtKharid;
											
											--execute  ToolePaye 'kol',@TooleKol output
											--execute  ToolePaye 'moin',@Toolemoin output
											--execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
											--set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
											--set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)											

											set @strResult='insertOk'

										end --flag insert
									end --count rowShomare Amani

							--set @lngCountAmani=(Select Count(fldShomareAmani) from tblRizeForoosh Where fldShomareAmani=@fldShomareAmani)
							if ( @lngCountAmani > 0)
									begin
										if @InsertType='arshive'
										  begin

											set @fldShomareSanad=(Select top(1) fldShomareSanad from tblRizeForoosh where fldShomareAmani=@fldShomareAmani and fldType='amani')
											delete from tblRizeForoosh where fldShomareAmani=@fldShomareAmani and fldType='amani'
											delete from tblRizeKharid where fldShomareAmani=@fldShomareAmani and fldType='amani'
											delete from tblSodooreSanad where @fldShomareSanad=@fldShomareSanad and fldType='amani'

											--INSERT       INTO              tblTest7(fld1, fld2, fld3)
											--			VALUES        ('1',1,1)

											execute  ToolePaye 'kol',@TooleKol output
											execute  ToolePaye 'moin',@Toolemoin output
											execute codemoinhesab 'forooshamani' ,@CodeMoin output
											set @CodeKolkham =substring(@CodeMoin, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoin,@TooleKol+1,@Toolemoin)											
											
											set @Date=(Select Top(1) fldDate  from @dtForoosh)
											set @year=substring(convert(nvarchar(20),@Date),3,2)
											
											set @month=substring(convert(nvarchar(20),@Date),5,2)
											set @day=substring(convert(nvarchar(20),@Date),7,2)
											set @dateSmall=substring(convert(nvarchar(15),@Date),3,6)


											execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoin,@fldCodeAshkhas_in,'1',@decMablaghKolFactor,0,1,@dateSmall,@strSharhKoli,@day,@month,@year,'amani',@fldCodeVasete,@fldShomareSanad 

											execute CodeKolHesab'tarafeentezami' ,@CodeMoin output
											set @CodeKolkham =substring(@CodeMoin, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoin,@TooleKol+1,@Toolemoin)
											execute InsertSanadLast  @CodeKolkham,@CodeMoinKham,@CodeMoin,NULL,NULL,0,@decMablaghKolFactor,2,@dateSmall,@strSharhKoli,@day,@month,@year,'amani',@fldCodeVasete,@fldShomareSanad

										INSERT INTO [dbo].[tblRizeForoosh]
												([fldShomareSanad]
												,[fldShomareFactor]
												,[fldCodeVasete]
												,[fldCodeTafsili]
												,[fldCodeUser]
												,[fldRadif]
												,[fldCodeAnbar]
												,[fldCodeKala]
												,[fldSharhKala]
												,[fldTedadCarton]
												,[fldTedadJoz]
												,[fldFeeJoz]
												,[fldFeeYekCarton]
												,[fldFeeKol]
												,[fldDarsadTakhfif]
												,[fldMablaghTakhfif]
												,[fldFeeVahed1BadAzTakhfif]
												,[fldFeeVahed2BadAzTakhfif]
												,[fldArzeshAfzode]
												,[fldFeeKolBadAzTakhfif]
												,[fldMablaghKhales]
												,[fldType]
												,[fldDate]
												,[fldTime]
												,[fldShomareAmani]
												,[fldDateEngheza])
												SELECT
												@fldShomareSanad
												,[fldShomareFactor]
												,[fldCodeVasete]												
												,@fldCodeAshkhas_in
												,[fldCodeUser]
												,[fldRadif]		
												,[fldCodeAnbar]										
												,[fldCodeKala]
												,[fldSharhKala]
												,[fldTedadCarton]
												,[fldTedadJoz]
												,[fldFeeJoz]
												,[fldFeeYekCarton]
												,[fldFeeKol]
												,[fldDarsadTakhfif]
												,[fldMablaghTakhfif]
												,[fldFeeVahed1BadAzTakhfif]
												,[fldFeeVahed2BadAzTakhfif]
												,[fldArzeshAfzode]
												,[fldFeeKolBadAzTakhfif]
												,[fldMablaghKhales]
												,[fldType]
												,[fldDate]
												,[fldTime]
												,@fldShomareAmani 
												,[fldDateEngheza]
												output from @dtForoosh


								--ثبت انبار وارده ریز خرید
												INSERT INTO [dbo].[tblRizeKharid]
															(
												--[fldCodeTafsili]
												--,[fldCodeUser]
												--,[fldRadif]
												--,[fldCodeAnbar]
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,[fldShomareAmani]
											[fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,[fldCodeTafsili]
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,[fldCodeAnbar]
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,[fldShomareAmani]
										   ,[fldDateEngheza])
												select
												--@fldCodeAshkhas_in
												--,[fldCodeUser]
												--,[fldRadif]
												--,@fldCodeAnbar_in
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,@fldShomareAmani
											@fldShomareSanad
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,@fldCodeAshkhas_in
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,@fldCodeAnbar_in
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,@fldShomareAmani 
										   ,[fldDateEngheza]
												output from @dtKharid;


												set @strResult='insertArchiveOk'
												end

										else 
											begin
												if @InsertType='insert'
													begin
													set @strResult='error_tekrari_insert'
														rollback transaction 
														return
													end
												end


										end

																							

				              commit transaction AddRecord;

           end try    --try

		  begin catch
		   if @@TranCount > 0
			 begin
				set @strResult='error'
				rollback transaction AddRecord;
			  end
			else
				set @strResult='mmm'
		   end catch
   
		END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoBargashAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertInfoBargashAmani]
	@dtForoosh tpRizeForoosh_Amani2 readonly,
	@dtKharid tpRizeKharid_EnteghalNewNew readonly,
	@fldShomareAmani bigint,
	@fldCodeAnbar_in bigint,
	@fldCodeAshkhas_in bigint,
	@InsertType nvarchar(50),
	@strResult nvarchar(25) output  
	
AS
BEGIN

	SET NOCOUNT ON;

        begin transaction AddRecord
             declare @lngCountAmani bigint
			 declare @Date bigint, @CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@CodeMoin nvarchar(10),@decMablaghKolFactor decimal(18,0),@intRadif bigint,@dateSmall nvarchar(10),@day nvarchar(10),@month nvarchar(10),@year nvarchar(10),@TooleKol bigint,@Toolemoin bigint,@fldCodeVasete bigint,@strSharhKoli nvarchar(250),@fldShomareSanad bigint
             begin try  --try
							  set @fldCodeVasete=(select top(1) fldCodeVasete from @dtForoosh)	
			                  set @lngCountAmani=(Select Count(fldShomareBargashtAmani) from tblRizeBargashtAzForoosh Where fldShomareBargashtAmani=@fldShomareAmani)
							  set @decMablaghKolFactor=(Select Sum(fldMablaghKhales ) from @dtForoosh)
					          if ( @lngCountAmani = 0)
							    begin
									--INSERT INTO tblTest7(fld1, fld2, fld3)
									--					VALUES        ('2',2,2)

									if @InsertType='insert'
									  begin

									    execute  ToolePaye 'kol',@TooleKol output
										execute  ToolePaye 'moin',@Toolemoin output
										execute codemoinhesab 'forooshamani' ,@CodeMoin output
										set @CodeKolkham =substring(@CodeMoin, 1, @TooleKol)
										set @CodeMoinKham =substring(@CodeMoin,@TooleKol+1,@Toolemoin)											
										--set @dateSmall=(Select Top(1) substring(Convert(varchar,fldDate),3,6)  from @dtForoosh)
										set @Date=(Select Top(1) fldDate  from @dtForoosh)
										set @year=substring(convert(nvarchar(20),@Date),3,2)
										--set @yearFull=substring(convert(nvarchar(20),@Date),0,5)
										set @month=substring(convert(nvarchar(20),@Date),5,2)
										set @day=substring(convert(nvarchar(20),@Date),7,2)
										set @dateSmall=substring(convert(nvarchar(15),@Date),3,6)

										
										--execute InsertSanadFirst @CodeKolkham,@CodeMoinKham,@CodeMoin,NULL,NULL,@decMablaghKolFactor,0,1,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtamani',@fldCodeVasete,@fldShomareSanad output

										--execute CodeKolHesab'tarafeentezami' ,@CodeMoin output
										--set @CodeKolkham =substring(@CodeMoin, 1, @TooleKol)
										--set @CodeMoinKham =substring(@CodeMoin,@TooleKol+1,@Toolemoin)
										--execute InsertSanadLast  @CodeKolkham,@CodeMoinKham,@CodeMoin,@fldCodeAshkhas_in,'1',0,@decMablaghKolFactor,2,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtamani',@fldCodeVasete,@fldShomareSanad  
										
										execute InsertSanadFirst @CodeKolkham,@CodeMoinKham,@CodeMoin,@fldCodeAshkhas_in,'1',0,@decMablaghKolFactor,2,@dateSmall,@strSharhKoli,@day,@month,@year,'amani',@fldCodeVasete,@fldShomareSanad output 

										execute CodeKolHesab'tarafeentezami' ,@CodeMoin output
										set @CodeKolkham =substring(@CodeMoin, 1, @TooleKol)
										set @CodeMoinKham =substring(@CodeMoin,@TooleKol+1,@Toolemoin)
										execute InsertSanadLast  @CodeKolkham,@CodeMoinKham,@CodeMoin,NULL,NULL,@decMablaghKolFactor,0,1,@dateSmall,@strSharhKoli,@day,@month,@year,'amani',@fldCodeVasete,@fldShomareSanad

									  INSERT INTO [dbo].[tblRizeBargashtAzForoosh]
										   ([fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,[fldCodeTafsili]
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,[fldCodeAnbar]
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,[fldShomareBargashtAmani]
										   ,[fldDateEngheza])
											SELECT
											@fldShomareSanad
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]	
										   ,@fldCodeAshkhas_in								   
										   ,[fldCodeUser]
										   ,[fldRadif]		
										   ,@fldCodeAnbar_in						   
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]										   
										   ,@fldShomareAmani 
										   ,[fldDateEngheza]
										   output from @dtForoosh


									--ثبت انبار وارده ریز خرید
									   INSERT INTO [dbo].[tblRizeKharid]
												(
												--[fldCodeTafsili]
												--,[fldCodeUser]
												--,[fldRadif]
												--,[fldCodeAnbar]
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,[fldShomareAmani]
											[fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,[fldCodeTafsili]
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,[fldCodeAnbar]
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,[fldShomareBargashtAmani]
										   ,[fldDateEngheza])
												select
												--@fldCodeAshkhas_in
												--,[fldCodeUser]
												--,[fldRadif]
												--,@fldCodeAnbar_in
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,@fldShomareAmani
											@fldShomareSanad
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,@fldCodeAshkhas_in
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,[fldCodeAnbar]
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,@fldShomareAmani 
										   ,[fldDateEngheza]
												output from @dtKharid;
											
											--execute  ToolePaye 'kol',@TooleKol output
											--execute  ToolePaye 'moin',@Toolemoin output
											--execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
											--set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
											--set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)											

											set @strResult='insertOk'

										end --flag insert
									end --count rowShomare Amani

							--set @lngCountAmani=(Select Count(fldShomareAmani) from tblRizeForoosh Where fldShomareAmani=@fldShomareAmani)
							if ( @lngCountAmani > 0)
									begin
										if @InsertType='arshive'
										  begin

											set @fldShomareSanad=(Select top(1) fldShomareSanad from tblRizeBargashtAzForoosh where fldShomareBargashtAmani=@fldShomareAmani and fldType='bargashtamani')
											delete from tblRizeBargashtAzForoosh where fldShomareBargashtAmani=@fldShomareAmani and fldType='bargashtamani'
											delete from tblRizeKharid where fldShomareBargashtAmani=@fldShomareAmani and fldType='bargashtamani'
											delete from tblSodooreSanad where @fldShomareSanad=@fldShomareSanad and fldType='bargashtamani'

											--INSERT       INTO              tblTest7(fld1, fld2, fld3)
											--			VALUES        ('1',1,1)

											execute  ToolePaye 'kol',@TooleKol output
											execute  ToolePaye 'moin',@Toolemoin output
											execute codemoinhesab 'forooshamani' ,@CodeMoin output
											set @CodeKolkham =substring(@CodeMoin, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoin,@TooleKol+1,@Toolemoin)											
											
											set @Date=(Select Top(1) fldDate  from @dtForoosh)
											set @year=substring(convert(nvarchar(20),@Date),3,2)
											
											set @month=substring(convert(nvarchar(20),@Date),5,2)
											set @day=substring(convert(nvarchar(20),@Date),7,2)
											set @dateSmall=substring(convert(nvarchar(15),@Date),3,6)


											execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoin,@fldCodeAshkhas_in,'1',0,@decMablaghKolFactor,2,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtamani',@fldCodeVasete,@fldShomareSanad 

											execute CodeKolHesab'tarafeentezami' ,@CodeMoin output
											set @CodeKolkham =substring(@CodeMoin, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoin,@TooleKol+1,@Toolemoin)
											execute InsertSanadLast  @CodeKolkham,@CodeMoinKham,@CodeMoin,NULL,NULL,@decMablaghKolFactor,0,1,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtamani',@fldCodeVasete,@fldShomareSanad

										INSERT INTO [dbo].[tblRizeBargashtAzForoosh]
												([fldShomareSanad]
												,[fldShomareFactor]
												,[fldCodeVasete]
												,[fldCodeTafsili]
												,[fldCodeUser]
												,[fldRadif]
												,[fldCodeAnbar]
												,[fldCodeKala]
												,[fldSharhKala]
												,[fldTedadCarton]
												,[fldTedadJoz]
												,[fldFeeJoz]
												,[fldFeeYekCarton]
												,[fldFeeKol]
												,[fldDarsadTakhfif]
												,[fldMablaghTakhfif]
												,[fldFeeVahed1BadAzTakhfif]
												,[fldFeeVahed2BadAzTakhfif]
												,[fldArzeshAfzode]
												,[fldFeeKolBadAzTakhfif]
												,[fldMablaghKhales]
												,[fldType]
												,[fldDate]
												,[fldTime]
												,[fldShomareBargashtAmani]
												,[fldDateEngheza])
												SELECT
												@fldShomareSanad
												,[fldShomareFactor]
												,[fldCodeVasete]												
												,@fldCodeAshkhas_in
												,[fldCodeUser]
												,[fldRadif]		
												,[fldCodeAnbar]										
												,[fldCodeKala]
												,[fldSharhKala]
												,[fldTedadCarton]
												,[fldTedadJoz]
												,[fldFeeJoz]
												,[fldFeeYekCarton]
												,[fldFeeKol]
												,[fldDarsadTakhfif]
												,[fldMablaghTakhfif]
												,[fldFeeVahed1BadAzTakhfif]
												,[fldFeeVahed2BadAzTakhfif]
												,[fldArzeshAfzode]
												,[fldFeeKolBadAzTakhfif]
												,[fldMablaghKhales]
												,[fldType]
												,[fldDate]
												,[fldTime]
												,@fldShomareAmani 
												,[fldDateEngheza]
												output from @dtForoosh


								--ثبت انبار وارده ریز خرید
												INSERT INTO [dbo].[tblRizeKharid]
															(
												--[fldCodeTafsili]
												--,[fldCodeUser]
												--,[fldRadif]
												--,[fldCodeAnbar]
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,[fldShomareAmani]
											[fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,[fldCodeTafsili]
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,[fldCodeAnbar]
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,[fldShomareBargashtAmani]
										   ,[fldDateEngheza])
												select
												--@fldCodeAshkhas_in
												--,[fldCodeUser]
												--,[fldRadif]
												--,@fldCodeAnbar_in
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,@fldShomareAmani
											@fldShomareSanad
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,@fldCodeAshkhas_in
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,fldCodeAnbar
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,@fldShomareAmani 
										   ,[fldDateEngheza]
												output from @dtKharid;


												set @strResult='insertArchiveOk'
												end

										else 
											begin
												if @InsertType='insert'
													begin
													set @strResult='error_tekrari_insert'
														rollback transaction 
														return
													end
												end


										end

																							

				              commit transaction AddRecord;

           end try    --try

		  begin catch
		   if @@TranCount > 0
			 begin
				set @strResult='error'
				rollback transaction AddRecord;
			  end
			else
				set @strResult='mmm'
		   end catch
   
		END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoBargashtAzForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertInfoBargashtAzForoosh]
	@fldCodeArz int,
	@fldNerkheArz decimal(18,4),
	@language nvarchar(10),
	@flagForoosh nvarchar(10),

	@intHasRowCheckPardakhti int,
	@intHasRowCheckMoshtari int,
	@lngMablaghKolCheckHaPardakhti decimal(18,4),
	@lngMablaghKolCheckMoshtari decimal(18,4),



	@lngMablaghNaghdPardakht decimal(18,4),
	@lngMablaghBankPardakht decimal(18,4),
	@lngMablaghTakhfifPardakht decimal(18,4),

	@dtForoosh [tpRizeForooshNew-Multi]  readonly,	
	@fldSharhFactor nvarchar(250),
	@fldShomareFactor bigint,
    @fldShomareSanad bigint,
    @fldDate bigint,
	@fldCodeTafsili bigint,

	@fldCodeVasete nvarchar(10),
	@lngMablaghPorsant decimal(18,4),

	@fldCodeSuperVizor nvarchar(10),
	@lngMablaghPorsantSuperVizor decimal(18,4),

	@lngHazineHamlVaNaghl decimal(18,4),
	@lngArzeshAfzode decimal(18,4),
	@lngMablaghKolFactor decimal(18,4),


	--پارامترهای صندوق
@CodeKolSandoogh nvarchar(10),
@CodeMoinSandoogh nvarchar(10),
--@Naghd decimal,
--@SNaghd nvarchar(200),

--'پارامترهای بانک
@CodeKolBank nvarchar(10),
@CodeMoinBank nvarchar(10),
--@Bank decimal,
--@SBank nvarchar(200),

--'پارامترهای چک
@tblCheck [tblCheckPardakhti-Multi] readonly,
@CodeKolCheck nvarchar(10),
@CodeMoinCheck nvarchar(10),
--@MablaghCheck decimal,
--@SCheck nvarchar(200),

--'پارامترهای چک واگذاری
@CheckeVagozari tblCheckVagozari readonly,
@CodeKolCheckVagozari nvarchar(10),
@CodeMoinCheckVagozari nvarchar(10),
--@MablaghCheckVagozari decimal,
--@SCheckVagozari nvarchar(200),

--پارامترهای تخفیفات
@CodeKoltakhfifat nvarchar(10),
@CodeMoinTakhfifatePardakht nvarchar(10),
--@Takhfif decimal,
--@STakhfif nvarchar(200),

-----پایان کد مرعشی
--شرح پرداخت
@strSharhNaghd nvarchar(50),
@strSharhBank nvarchar(50),
@strSharhCheck nvarchar(50),
@strSharhCheckMoshtari nvarchar(50),
@strSharhTakhfif nvarchar(50),
@strSharhKoli nvarchar(50),



	@strResult nvarchar(25) output,
	@strResult2 nvarchar(25) output,
	@flagInsert nvarchar(10)
	--@ErrorLine bigint output
	
AS
create table #tmpColor (
	MoinKamel nvarchar(50) COLLATE Persian_100_CI_AS,
	fldColorBank int,
	fldColorSandoogh int , 
	fldColorSooratHesab int

)
BEGIN

	begin transaction AddRecord
		begin try
			declare @intRadif int,@CodeMoinHesabhayePardakhtani nvarchar(100),@CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@TooleKol bigint,@Toolemoin bigint, @lngsum decimal(18,4)
			declare @day nvarchar(4),@Month nvarchar(4),@year nvarchar(5),@ShomarePardakht bigint,@yearFull nvarchar(5),@dateSmall bigint,@strMoinKamel nvarchar(50),@lngSumPardakht bigint
			declare @CodeMoinHoghoogh nvarchar(10),@SharhePoorsant nvarchar(100)
			--set @year=substring(convert(nvarchar(20),@fldDate),3,2)
			set @yearFull=substring(convert(nvarchar(20),@fldDate),0,5)
			set @year=@yearFull
			set @month=substring(convert(nvarchar(20),@fldDate),5,2)
			set @day=substring(convert(nvarchar(20),@fldDate),7,2)
			--set @dateSmall=substring(convert(nvarchar(15),@fldDate),3,6)
			set @dateSmall=@fldDate
			declare @lngSumDaryaft decimal(18,4)
			set @intRadif=0


			--set @SharhePoorsant='معکوس پورسانت برگشت ' + Convert(varchar,@fldShomareFactor)
			set @SharhePoorsant =(select * from dbo.translateFunction(@language,18267)) + ' ' + Convert(varchar,@fldShomareFactor)
			
		
			if @fldSharhFactor=''
					set @fldSharhFactor='none'

			if @flagForoosh='arshive'
				begin
					--arishive
					--update(delete and insert)
					--delete
					delete from tblRizeBargashtAzForoosh
						where fldShomareSanad=@fldShomareSanad

					delete from tblBargashtAzForoosh
						where fldShomareSanad=@fldShomareSanad

					insert into #tmpColor
								(MoinKamel,fldColorBank ,fldColorSandoogh , fldColorSooratHesab )
								Select fldMoinKamel,fldColorBank,fldColorSandoogh,fldColorSooratHesab from tblSodooreSanad Where fldSanad=@fldShomareSanad

					delete from tblSodooreSanad
						where fldSanad=@fldShomareSanad

					declare @ShomarePardakhtArshive bigint
					set @ShomarePardakhtArshive=(Select fldShomarePardakht from tblPardakht Where fldShomareSanad=@fldShomareSanad)
					if @ShomarePardakhtArshive is not null
						begin
							update tblCheckeDaryafti 
								set fldShomarePardakht= null
								where fldShomarePardakht=@ShomarePardakhtArshive
						end

					delete from tblPardakht
						where fldShomareSanad=@fldShomareSanad

					delete from tblCheckePardakhti
						where fldSanad=Convert(nvarchar(50),@fldShomareSanad)


					


						--where fldSanad=@fldShomareSanad  

					--insert

							set @intRadif=@intRadif+1
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output

							execute codemoinhesab 'bargashtazforoosh' ,@CodeMoinHesabhayePardakhtani output
							set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
							set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngsum,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad

						
							if @lngHazineHamlVaNaghl>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'hamlvanaghl' ,@CodeMoinHesabhayePardakhtani output
								set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngHazineHamlVaNaghl,0,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
							end
							
							if @lngArzeshAfzode>0
							begin 
								set @intRadif=@intRadif+1
								execute codemoinhesab 'arzesheafzoode' ,@CodeMoinHesabhayePardakhtani output
								set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngArzeshAfzode,0,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
							end 

							set @intRadif=@intRadif+1
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayePardakhtani output
							set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
							--چون در حالت ویرایش میباشد از لست استفاده کردم
							
							execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',0,@lngMablaghKolFactor,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
	

							--بررسی شرح های پرداخت
							if (@strSharhNaghd is null or @strSharhNaghd='')
							begin
								set @strSharhNaghd='nonePardakht'
							end
							if (@strSharhBank is null or @strSharhBank='')
							begin
								set @strSharhBank='nonePardakht'
							end
							if (@strSharhCheck is null or @strSharhCheck='')
							begin
								set @strSharhCheck='nonePardakht'
							end
							if (@strSharhTakhfif is null or @strSharhTakhfif='')
							begin
								set @strSharhTakhfif='nonePardakht'
							end
							--شرح دریافت اصلی فروش
							if (@strSharhKoli is null or @strSharhKoli='')
							begin
								set @strSharhKoli='nonePardakht'
							end

							--پایان بررسی شرح های پرداخت
							set @lngSumPardakht=(@lngMablaghNaghdPardakht+@lngMablaghBankPardakht+@lngMablaghKolCheckHaPardakhti+@lngMablaghKolCheckMoshtari+@lngMablaghTakhfifPardakht)

							if(@lngSumPardakht>0)
							begin
								set @intRadif=@intRadif+1
								execute ToolePaye 'kol',@TooleKol output
								execute ToolePaye 'moin',@Toolemoin output
								execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayePardakhtani output
								set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
								--بدهکار
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngSumPardakht,0,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
							end

							if @lngMablaghNaghdPardakht>0
							begin
								set @intRadif =@intRadif+1
								set @CodeMoinKham=substring(@CodeMoinSandoogh,@TooleKol+1,@Toolemoin)
								set @strMoinKamel=@CodeKolSandoogh + @CodeMoinSandoogh
								execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,0,@lngMablaghNaghdPardakht,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
							end
							--INSERT INTO tblTest
							--	(adad)
							--		VALUES        (@lngMablaghBankPardakht)
							--سند بانک
							if @lngMablaghBankPardakht>0
							begin
								--INSERT INTO tblTest
								--(adad)
								--	VALUES        (66)
								set @intRadif =@intRadif+1

								set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
								set @strMoinKamel=@CodeKolBank + @CodeMoinBank
								execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,0,@lngMablaghBankPardakht,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
							end
							--سند چک
							if @lngMablaghKolCheckHaPardakhti>0
							begin
								set @intRadif =@intRadif+1

								set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)
								set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck
								execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckHaPardakhti,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
							end
							--سند حسابداری واگذاری چک
							if @lngMablaghKolCheckMoshtari>0
							begin
								set @intRadif =@intRadif+1
								set @CodeMoinKham=substring(@CodeMoinCheckVagozari,@TooleKol+1,@Toolemoin)
								set @strMoinKamel=@CodeKolCheckVagozari + @CodeMoinCheckVagozari
								execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckVagozari,@CodeMoinCheckVagozari,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckMoshtari,@intRadif,@dateSmall,@strSharhCheckMoshtari,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;

							end

							if @lngMablaghTakhfifPardakht>0
							begin
								set @intRadif =@intRadif+1
								set @CodeMoinKham=substring(@CodeMoinTakhfifatePardakht,@TooleKol+1,@Toolemoin)
								set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifatePardakht
								execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,0,@lngMablaghTakhfifPardakht,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
							end
							if (@lngSumPardakht>0)
							begin
								set @ShomarePardakht=(select isnull(max(fldShomarePardakht),0) as fldMax from tblPardakht)
								set @ShomarePardakht=@ShomarePardakht+1
								INSERT INTO tblPardakht( fldShomarePardakht, fldShomareSanad, fldCodeTafzili,fldType)
								VALUES (@ShomarePardakht,@fldShomareSanad,@fldCodeTafsili,'bargashtazforoosh')

								--ثبت ریز اطلاعات چک
								Insert into tblCheckePardakhti (fldcodearz,fldNerkheArz, fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDatePardakht,fldShomarePardakht,fldVasete,fldSanad)
								Select @fldcodearz,@fldNerkheArz, fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@dateSmall,@ShomarePardakht, @fldCodeVasete,@fldShomareSanad output from @tblCheck;

								--ثبت واگذاری چک
								UPDATE tblCheckeDaryafti Set fldShomarePardakht=@ShomarePardakht Where Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) in (Select Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) from @CheckeVagozari)

							end



								--ح س پرداختنی
							
							--execute  ToolePaye 'kol',@TooleKol output
							--execute  ToolePaye 'moin',@Toolemoin output
							--execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output
							--set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
							--set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
							----declare @lngSumDaryaft bigint
							--set @lngSumPardakht=(@lngMablaghTakhfifPardakht+@lngMablaghBankPardakht+@lngMablaghNaghdPardakht+@lngMablaghKolCheckHaPardakhti+@lngMablaghKolCheckMoshtari)
							--if(@lngSumPardakht>0)
							--begin
							--	set @intRadif=@intRadif+1
							--	execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',0,@lngSumPardakht,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad 
							--end 
							--سند پورسانت واسطه ها و ویزیتور ها
							if isnumeric(@fldCodeVasete )=1  and Convert(decimal(18,4),@fldCodeVasete)>0
								begin									
									set @intRadif=@intRadif+1
									
									execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
									set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									if @lngMablaghPorsant>0 
										begin
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeVasete,'0',0,@lngMablaghPorsant,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeVasete,'0',@lngMablaghPorsant,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad
										end

									--### سند سوپروایزر####
									if @lngMablaghPorsantSuperVizor>0
										begin								
											set @intRadif=@intRadif+1
									
											execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
											set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
												
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeSuperVizor,'0',0,@lngMablaghPorsantSuperVizor,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeSuperVizor,'0',@lngMablaghPorsantSuperVizor,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad
										end 
									
								end 
								--پایان سند پورسانت واسطه ها و ویزیتور ها 

								--if @lngSumPardakht>0
								--begin
								--	set @ShomarePardakht=(select isnull(max(fldShomarePardakht),0) as fldMax from tblPardakht)
								--	set @ShomarePardakht=@ShomarePardakht+1
								--	--ثبت دریافت اصلی فروش
								--	INSERT  INTO   tblpardakht( fldShomarePardakht, fldShomareSanad, fldCodeTafzili,fldType)
								--					VALUES        (@ShomarePardakht,@fldShomareSanad,@fldCodeTafsili,'5bargashtazforoosh')

								--	--ثبت ریز اطلاعات چک	
								--	--Insert into  tblCheckeDaryafti (fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
								--	--		Select fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,ShomarePardakht,@fldCodeVasete,@fldShomareSanad output from @dtCheckDaryafti;	
								--end 
							INSERT INTO tblbargashtazforoosh
							   ([fldShomareFactorForoosh]
							   ,[fldShomareSanad]
							   ,[fldDate]
							   ,[fldSharhFactor]
							   )
								 values(@fldShomareFactor,@fldShomareSanad,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end )

							 INSERT INTO tblRizeBargashtAzForoosh
						   ([fldShomareSanad]
						   ,[fldShomareFactor]
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime]
						   ,[fldDateEngheza])
							select @fldShomareSanad
						   ,@fldShomareFactor
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime]
						   ,[fldDateEngheza] output  from @dtForoosh;






					set @strResult='update'
					set @strResult2='update'
					
					--commit transaction AddRecord;
					--return
				end
				--تا اینجا تفصیلی ها به روز  شدند
			else if @flagForoosh='insert'
				begin
				--insert
						
					declare @resultExist bigint
					execute ExistFactorBargashtazForoosh @fldShomareFactor,@resultExist output
					declare @maxFactor bigint
					execute GetMaxShomareFactorBargashtAzForoosh @maxFactor output
					set @maxFactor=@maxFactor+1
						
					if(@resultExist>0)
						begin
						--کاربر دیگری در همین لحظه شماره فاکتور را گرفت
							
							set @strResult=convert(nvarchar(17),@maxFactor)
							set @strResult2='New_Enter'
							rollback transaction AddRecord;
							return
							--در قسمت ویندوزی به کاربر کادری را نشان دهد که max+1 می باشد
							--yes
								--insert
							--no
								--inputbox در برنامه ویندوز که کاربر خودش شماره فاکتور را مشخص کند
								--باید اینجا چکار کنم؟؟؟؟
						end 
					--ehsan added
					 else if(@resultExist=0 and @flagInsert='yes')
					 --زمانی که کاربر از کادر پیام رویه دکمه بله کلیک میکنه
					     begin
						
							set @intRadif=@intRadif+1
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayePardakhtani output
							set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
							execute InsertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',0,@lngMablaghKolFactor,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad output 
								
							if @lngHazineHamlVaNaghl>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'hamlvanaghl' ,@CodeMoinHesabhayePardakhtani output
								set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngHazineHamlVaNaghl,0,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
							end 
							if @lngArzeshAfzode>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'arzesheafzoode' ,@CodeMoinHesabhayePardakhtani output
								set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngArzeshAfzode,0,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
							end 

							set @intRadif=@intRadif+1
							execute codemoinhesab 'bargashtazforoosh' ,@CodeMoinHesabhayePardakhtani output
							set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
							set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngsum,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
							--بررسی شرح های دریافت
							if (@strSharhNaghd is null or @strSharhNaghd='')
								begin
									set @strSharhNaghd='nonepardakht'
								end 
							if (@strSharhBank is null or @strSharhBank='')
								begin
									set @strSharhBank='nonepardakht'
								end 
							if (@strSharhCheck is null or @strSharhCheck='')
								begin
									set @strSharhCheck='nonepardakht'
								end 
							if (@strSharhTakhfif is null or  @strSharhTakhfif='')
								begin
									set @strSharhTakhfif='nonepardakht'
								end 
							--شرح دریافت اصلی فروش
							if (@strSharhKoli is null or @strSharhKoli='')
								begin
									set @strSharhKoli='none'
								end 
	
							--پایان بررسی شرح های دریافت

							set @lngSumPardakht=(@lngMablaghNaghdPardakht+@lngMablaghBankPardakht+@lngMablaghKolCheckHaPardakhti+@lngMablaghKolCheckMoshtari+@lngMablaghTakhfifPardakht)

									if(@lngSumPardakht>0)
									begin
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									--بدهکار
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngSumPardakht,0,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
									end

									if @lngMablaghNaghdPardakht>0
									begin
									set @intRadif =@intRadif+1
									set @CodeMoinKham=substring(@CodeMoinSandoogh,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolSandoogh + @CodeMoinSandoogh
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,0,@lngMablaghNaghdPardakht,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
									end
									--سند بانک
									if @lngMablaghBankPardakht>0
									begin
									set @intRadif =@intRadif+1

									set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolBank + @CodeMoinBank
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,0,@lngMablaghBankPardakht,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
									end
									--سند چک
									if @lngMablaghKolCheckHaPardakhti>0
									begin
									set @intRadif =@intRadif+1

									set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckHaPardakhti,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
									end
									--سند حسابداری واگذاری چک
									if @lngMablaghKolCheckMoshtari>0
									begin
									set @intRadif =@intRadif+1
									set @CodeMoinKham=substring(@CodeMoinCheckVagozari,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolCheckVagozari + @CodeMoinCheckVagozari
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckVagozari,@CodeMoinCheckVagozari,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckMoshtari,@intRadif,@dateSmall,@strSharhCheckMoshtari,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;

									end

									if @lngMablaghTakhfifPardakht>0
									begin
									set @intRadif =@intRadif+1
									set @CodeMoinKham=substring(@CodeMoinTakhfifatePardakht,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifatePardakht
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,0,@lngMablaghTakhfifPardakht,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
									end
									if (@lngSumPardakht>0)
									begin
									set @ShomarePardakht=(select isnull(max(fldShomarePardakht),0) as fldMax from tblPardakht)
									set @ShomarePardakht=@ShomarePardakht+1
									INSERT INTO tblPardakht( fldShomarePardakht, fldShomareSanad, fldCodeTafzili,fldType)
									VALUES (@ShomarePardakht,@fldShomareSanad,@fldCodeTafsili,'bargashtazforoosh')

									--ثبت ریز اطلاعات چک
									Insert into tblCheckePardakhti (fldcodearz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDatePardakht,fldShomarePardakht,fldVasete,fldSanad)
									Select @fldcodearz,@fldNerkheArz, fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@dateSmall,@ShomarePardakht, @fldCodeVasete,@fldShomareSanad output from @tblCheck;

									--ثبت واگذاری چک
									UPDATE tblCheckeDaryafti Set fldShomarePardakht=@ShomarePardakht Where Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) in (Select Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) from @CheckeVagozari)

									end





							--سند پورسانت واسطه ها و ویزیتور ها
							if isnumeric(@fldCodeVasete )=1  and Convert(decimal(18,4),@fldCodeVasete)>0
								begin									
									set @intRadif=@intRadif+1
									
									execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
									set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									if @lngMablaghPorsant>0 
										begin
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeVasete,'0',0,@lngMablaghPorsant,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeVasete,'0',@lngMablaghPorsant,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad
										end

									--### سند سوپروایزر####
									if @lngMablaghPorsantSuperVizor>0
										begin								
											set @intRadif=@intRadif+1
									
											execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
											set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
												
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeSuperVizor,'0',0,@lngMablaghPorsantSuperVizor,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeSuperVizor,'0',@lngMablaghPorsantSuperVizor,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad
										end 
									
								end 
								--پایان سند پورسانت واسطه ها و ویزیتور ها 

								if @lngSumPardakht>0
								begin
									set @ShomarePardakht=(select isnull(max(fldShomarePardakht),0) as fldMax from tblPardakht)
									set @ShomarePardakht=@ShomarePardakht+1
									--ثبت دریافت اصلی فروش
									INSERT  INTO   tblPardakht( fldShomarePardakht, fldShomareSanad, fldCodeTafzili,fldType)
													VALUES        (@ShomarePardakht,@fldShomareSanad,@fldCodeTafsili,'bargashtazforoosh')

									--ثبت ریز اطلاعات چک	
									--Insert into  tblCheckeDaryafti (fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
									--		Select fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,ShomarePardakht,@fldCodeVasete,@fldShomareSanad output from @dtCheckDaryafti;	
								end
							INSERT INTO tblBargashtAzForoosh
							   ([fldShomareFactorForoosh]
							   ,[fldShomareSanad]
							   ,[fldDate]
							   ,[fldSharhFactor]
							   )
								 values(@maxFactor,@fldShomareSanad,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end )

							 INSERT INTO tblRizeBargashtAzForoosh
						   ([fldShomareSanad]
						   ,[fldShomareFactor]
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime]
						   ,[fldDateEngheza])
							select @fldShomareSanad
						   ,@maxFactor
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime]
						   ,[fldDateEngheza] output  from @dtForoosh;
							set @strResult=convert(nvarchar(17),@maxFactor)
							set @strResult2='insert'
							--commit transaction AddRecord;
						end
					else if(@resultExist=0 and @flagInsert='no')
						 begin
						
							
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output

							set @intRadif=@intRadif+1							
							execute codemoinhesab 'bargashtazforoosh' ,@CodeMoinHesabhayePardakhtani output
							set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
							set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							execute InsertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngsum,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad output 
							--execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngsum,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
						   

							

							if @lngHazineHamlVaNaghl>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'hamlvanaghl' ,@CodeMoinHesabhayePardakhtani output
								set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngHazineHamlVaNaghl,0,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
							end
							if @lngArzeshAfzode>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'arzesheafzoode' ,@CodeMoinHesabhayePardakhtani output
								set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngArzeshAfzode,0,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
							end 


							
							--############################################################################################
							-----------------------pardakht----------------------------------------
									--بررسی شرح های پرداخت
									if (@strSharhNaghd is null or @strSharhNaghd='')
									begin
									set @strSharhNaghd='nonePardakht'
									end
									if (@strSharhBank is null or @strSharhBank='')
									begin
									set @strSharhBank='nonePardakht'
									end
									if (@strSharhCheck is null or @strSharhCheck='')
									begin
									set @strSharhCheck='nonePardakht'
									end
									if (@strSharhTakhfif is null or @strSharhTakhfif='')
									begin
									set @strSharhTakhfif='nonePardakht'
									end
									--شرح دریافت اصلی فروش
									if (@strSharhKoli is null or @strSharhKoli='')
									begin
									set @strSharhKoli='nonePardakht'
									end

									--پایان بررسی شرح های پرداخت
									set @lngSumPardakht=(@lngMablaghNaghdPardakht+@lngMablaghBankPardakht+@lngMablaghKolCheckHaPardakhti+@lngMablaghKolCheckMoshtari+@lngMablaghTakhfifPardakht)

									if(@lngSumPardakht>0)
									begin
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									--بدهکار
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngSumPardakht,0,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
									end

									if @lngMablaghNaghdPardakht>0
									begin
									set @intRadif =@intRadif+1
									set @CodeMoinKham=substring(@CodeMoinSandoogh,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolSandoogh + @CodeMoinSandoogh
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,0,@lngMablaghNaghdPardakht,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
									end
									--سند بانک
									if @lngMablaghBankPardakht>0
									begin
									set @intRadif =@intRadif+1

									set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolBank + @CodeMoinBank
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,0,@lngMablaghBankPardakht,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
									end
									--سند چک
									if @lngMablaghKolCheckHaPardakhti>0
									begin
									set @intRadif =@intRadif+1

									set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckHaPardakhti,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
									end
									--سند حسابداری واگذاری چک
									if @lngMablaghKolCheckMoshtari>0
									begin
									set @intRadif =@intRadif+1
									set @CodeMoinKham=substring(@CodeMoinCheckVagozari,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolCheckVagozari + @CodeMoinCheckVagozari
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckVagozari,@CodeMoinCheckVagozari,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckMoshtari,@intRadif,@dateSmall,@strSharhCheckMoshtari,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;

									end

									if @lngMablaghTakhfifPardakht>0
									begin
										set @intRadif =@intRadif+1
										set @CodeMoinKham=substring(@CodeMoinTakhfifatePardakht,@TooleKol+1,@Toolemoin)
										set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifatePardakht
										execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,0,@lngMablaghTakhfifPardakht,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad;
									end
									
									if (@lngSumPardakht>0)
									begin
										set @ShomarePardakht=(select isnull(max(fldShomarePardakht),0) as fldMax from tblPardakht)
										set @ShomarePardakht=@ShomarePardakht+1
										INSERT INTO tblPardakht( fldShomarePardakht, fldShomareSanad, fldCodeTafzili,fldType)
										VALUES (@ShomarePardakht,@fldShomareSanad,@fldCodeTafsili,'bargashtazforoosh')

										--ثبت ریز اطلاعات چک
										Insert into tblCheckePardakhti (fldcodearz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDatePardakht,fldShomarePardakht,fldVasete,fldSanad)
										Select @fldcodearz,@fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@dateSmall,@ShomarePardakht, @fldCodeVasete,@fldShomareSanad output from @tblCheck;

										--ثبت واگذاری چک
										UPDATE tblCheckeDaryafti Set fldShomarePardakht=@ShomarePardakht Where Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) in (Select Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) from @CheckeVagozari)

									end

									set @intRadif=@intRadif+1
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayePardakhtani output
							set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
							--execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',0,@lngMablaghKolFactor,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
							--execute InsertSanadFirst @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',0,@lngMablaghKolFactor,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad output 
							 execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',0,@lngMablaghKolFactor,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazforoosh',@fldCodeVasete,@fldShomareSanad
							--############################################################################################



							--سند پورسانت واسطه ها و ویزیتور ها
							if isnumeric(@fldCodeVasete )=1  and Convert(decimal(18,4),@fldCodeVasete)>0
								begin									
									set @intRadif=@intRadif+1
									
									execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
									set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									if @lngMablaghPorsant>0 
										begin
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeVasete,'0',0,@lngMablaghPorsant,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeVasete,'0',@lngMablaghPorsant,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad
										end

									--### سند سوپروایزر####
									if @lngMablaghPorsantSuperVizor>0
										begin								
											set @intRadif=@intRadif+1
									
											execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
											set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
												
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeSuperVizor,'0',0,@lngMablaghPorsantSuperVizor,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeSuperVizor,'0',@lngMablaghPorsantSuperVizor,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'bargashtazforoosh','',@fldShomareSanad
										end 
									
								end 
								--پایان سند پورسانت واسطه ها و ویزیتور ها 



								if @lngSumPardakht>0
								begin
									set @ShomarePardakht=(select isnull(max(fldShomarePardakht),0) as fldMax from tblPardakht)
									set @ShomarePardakht=@ShomarePardakht+1
									--ثبت دریافت اصلی فروش

									--control
									--INSERT  INTO   tblpardakht( fldShomarepardakht, fldShomareSanad, fldCodeTafzili,fldType)
									--				VALUES        (ShomarePardakht,@fldShomareSanad,@fldCodeTafsili,'bargashtazforoosh')

									--ثبت ریز اطلاعات چک	
									--Insert into  tblCheckepardakhti (fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDatepardakht,fldShomarepardakht,fldVasete,fldSanad)
									--		Select fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,ShomarePardakht,@fldCodeVasete,@fldShomareSanad output from @dtCheckpardakhti;	
								end
							INSERT INTO tblBargashtAzForoosh
							   ([fldShomareFactorForoosh]
							   ,[fldShomareSanad]
							   ,[fldDate]
							   ,[fldSharhFactor]
							   )
								 values(@fldShomareFactor,@fldShomareSanad,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end )--iif (@fldSharhFactor='none','',@fldSharhFactor)

							 INSERT INTO tblRizeBargashtAzForoosh
													   ([fldShomareSanad]
													   ,[fldShomareFactor]
													   ,[fldCodeVasete]
													   ,[fldCodeTafsili]
													   ,[fldCodeUser]
													   ,[fldRadif]
													   ,[fldCodeAnbar]
													   ,[fldCodeKala]
													   ,[fldSharhKala]
													   ,[fldTedadCarton]
													   ,[fldTedadJoz]
													   ,[fldFeeJoz]
													   ,[fldFeeYekCarton]
													   ,[fldFeeKol]
													   ,[fldDarsadTakhfif]
													   ,[fldMablaghTakhfif]
													   ,[fldFeeVahed1BadAzTakhfif]
													   ,[fldFeeVahed2BadAzTakhfif]
													   ,[fldArzeshAfzode]
													   ,[fldFeeKolBadAzTakhfif]
													   ,[fldMablaghKhales]
													   ,[fldType]
													   ,[fldDate]
													   ,[fldTime]
													   ,[fldDateEngheza])
														select @fldShomareSanad
														--edit
													   ,@fldShomareFactor
													   ,[fldCodeVasete]
													   ,[fldCodeTafsili]
													   ,[fldCodeUser]
													   ,[fldRadif]
													   ,[fldCodeAnbar]
													   ,[fldCodeKala]
													   ,[fldSharhKala]
													   ,[fldTedadCarton]
													   ,[fldTedadJoz]
													   ,[fldFeeJoz]
													   ,[fldFeeYekCarton]
													   ,[fldFeeKol]
													   ,[fldDarsadTakhfif]
													   ,[fldMablaghTakhfif]
													   ,[fldFeeVahed1BadAzTakhfif]
													   ,[fldFeeVahed2BadAzTakhfif]
													   ,[fldArzeshAfzode]
													   ,[fldFeeKolBadAzTakhfif]
													   ,[fldMablaghKhales]
													   ,[fldType]
													   ,[fldDate]
													   ,[fldTime]
													   ,[fldDateEngheza] output  from @dtForoosh;
							set @strResult=convert(nvarchar(17),@fldShomareFactor)
							set @strResult2='insertByNo'
							--commit transaction AddRecord
						end
						
				end
			--set @strResult='errorx'
			--set @strResult2='errorx'
			--update sharh Ha
			--@fldShomarefactor dar khat zir bayad avaz beshe (shayad =>@fldShomarefactorNew )
			update tblSodooreSanad set fldColorBank=#tmpColor.fldColorBank,fldColorSandoogh=#tmpColor.fldColorSandoogh,fldColorSooratHesab=#tmpColor.fldColorSooratHesab from #tmpColor Where tblSodooreSanad.fldSanad=@fldShomareSanad and fldMoinKamel=#tmpColor.MoinKamel

			 if @flagInsert='yes'
				begin
					update  tblSodooreSanad
							--set fldSharh ='برگشت از فروش به شماره ' + convert(nvarchar(17),@maxFactor)
							set fldSharh ='' 
							where fldSanad=@fldShomareSanad and fldSharh like 'none'
					update  tblSodooreSanad
							--set fldSharh ='برگشت از فروش به شماره ' + convert(nvarchar(17),@maxFactor) + ' - شماره پرداخت ' +  convert(nvarchar(17),@ShomarePardakht) 
							set fldSharh =''  
							where fldSanad=@fldShomareSanad and fldSharh like 'nonepardakht'
				end
			else
				begin
					 update  tblSodooreSanad
							--set fldSharh ='برگشت از فروش به شماره ' + convert(nvarchar(17),@fldShomarefactor)
							set fldSharh ='' 
							where fldSanad=@fldShomareSanad and fldSharh like 'none'

					update  tblSodooreSanad
							--set fldSharh ='برگشت از فروش به شماره ' + convert(nvarchar(17),@fldShomarefactor) + ' - شماره پرداخت ' +  convert(nvarchar(17),@ShomarePardakht) 
							set fldSharh =''  
							where fldSanad=@fldShomareSanad and fldSharh like 'nonepardakht'
				end


				commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult= 'error' 
				set @strResult2='error'
				 --set @ErrorLine=( Select ERROR_LINE() as errln)
				--set @ErrorLine=1
				--select  ERROR_LINE() as mm 
				--SELECT
				--	 ERROR_NUMBER() AS ErrorNumber,
				--	 ERROR_SEVERITY() AS ErrorSeverity,
				--	 ERROR_STATE() AS ErrorState,
				--	 ERROR_PROCEDURE() AS ErrorProcedure,
				--	 ERROR_LINE() AS ErrorLine,
				--	 ERROR_MESSAGE() AS ErrorMessage;

				rollback transaction AddRecord;
			end
		end catch
END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoBargashtAzForooshTest]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertInfoBargashtAzForooshTest]
	@flagForoosh nvarchar(10),
	@fldCodeTafsili bigint,
	@fldCodeVasete nvarchar(10),
	@lngMablaghPorsant bigint,
    @fldShomareFactor bigint,
    @fldShomareSanad bigint,
    @fldDate bigint,
	@fldSharhFactor nvarchar(250),
	@dtForoosh tpRizeForoosh  readonly,
	@dtCheckDaryafti tblCheckDaryafti readonly,
	@lngHazineHamlVaNaghl bigint,
	@lngArzeshAfzode bigint,
	@lngMablaghKolFactor bigint,
	@lngMablaghKolCheckHa bigint,
	@intHasRowCheckDaryafti int,
	@lngMablaghNaghdDaryaft bigint,
    @lngMablaghBankDaryaft bigint,
	@lngMablaghTakhfifDaryaft bigint,
	@lngCodeSandoghDaryaft bigint,
	@lngCodeBankDaryaft bigint,
	--شرح دریافت
	@strSharhNaghd nvarchar(50), 
	@strSharhBank nvarchar(50),
	@strSharhCheck nvarchar(50),
	@strSharhTakhfif nvarchar(50),
	@strSharhKoli nvarchar(50),
	@strResult nvarchar(25) output,
	@strResult2 nvarchar(25) output,
	@flagInsert nvarchar(10),
	@ErrorLine bigint output
AS
BEGIN

	begin transaction AddRecord
		begin try
			declare @intRadif int,@CodeMoinHesabhayeDaryaftani nvarchar(100),@CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@TooleKol bigint,@Toolemoin bigint, @lngsum bigint
			declare @day nvarchar(4),@Month nvarchar(4),@year nvarchar(5),@ShomareDaryaft bigint,@yearFull nvarchar(5),@dateSmall bigint
			set @year=substring(convert(nvarchar(20),@fldDate),3,2)
			set @yearFull=substring(convert(nvarchar(20),@fldDate),0,5)
			set @month=substring(convert(nvarchar(20),@fldDate),5,2)
			set @day=substring(convert(nvarchar(20),@fldDate),7,2)
			set @dateSmall=substring(convert(nvarchar(15),@fldDate),3,6)
			declare @lngSumDaryaft bigint
			set @intRadif=0
		
			if @flagForoosh='arshive'
				begin
					--arishive
					--update(delete and insert)
					--delete
					delete from tblRizeBargashtAzForoosh
						where fldShomareSanad=@fldShomareSanad

					delete from tblBargashtAzForoosh
						where fldShomareSanad=@fldShomareSanad

					delete from tblSodooreSanad
						where fldSanad=@fldShomareSanad

					delete from tblDaryaft
						where fldShomareSanad=@fldShomareSanad

					delete from tblCheckeDaryafti
						where fldSanad=@fldShomareSanad and (fldShomareVosool  is  null and fldShomarePardakht  is  null)
						--where fldSanad=@fldShomareSanad  

					--insert
							set @intRadif=@intRadif+1
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							--چون در حالت ویرایش میباشد از لست استفاده کردم
							
							execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolFactor,0,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							
							if @lngHazineHamlVaNaghl>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'hamlvanaghl' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngHazineHamlVaNaghl,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end
							
							if @lngArzeshAfzode>0
							begin 
								set @intRadif=@intRadif+1
								execute codemoinhesab 'arzesheafzoode' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngArzeshAfzode,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end 

							set @intRadif=@intRadif+1
							execute codemoinhesab 'foroosh' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
							set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngsum,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							--بررسی شرح های دریافت
							if (@strSharhNaghd is null or @strSharhNaghd='')
								begin
									set @strSharhNaghd='noneDaryaft'
								end 
							if (@strSharhBank is null or @strSharhBank='')
								begin
									set @strSharhBank='noneDaryaft'
								end 
							if (@strSharhCheck is null or @strSharhCheck='')
								begin
									set @strSharhCheck='noneDaryaft'
								end 
							if (@strSharhTakhfif is null or @strSharhTakhfif='')
								begin
									set @strSharhTakhfif='noneDaryaft'
								end 
							--شرح دریافت اصلی فروش
							if (@strSharhKoli is null or @strSharhKoli='')
								begin
									set @strSharhKoli='noneDaryaft'
								end 
	
							--پایان بررسی شرح های دریافت

							if @lngMablaghNaghdDaryaft>0
								begin
								--صندوق
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeSandoghDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghNaghdDaryaft,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 
							if @lngMablaghBankDaryaft>0
								begin
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeBankDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghBankDaryaft,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 
							if @intHasRowCheckDaryafti>0
								begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'check' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
					
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolCheckHa,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

							if @lngMablaghTakhfifDaryaft>0
								begin
									set @intRadif=@intRadif+1

									execute codemoinhesab 'takhfifatedaryaft' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

								--ح س دریافتنی
							
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							--declare @lngSumDaryaft bigint
							set @lngSumDaryaft=(@lngMablaghTakhfifDaryaft+@lngMablaghBankDaryaft+@lngMablaghNaghdDaryaft+@lngMablaghKolCheckHa)
							if(@lngSumDaryaft>0)
							begin
								set @intRadif=@intRadif+1
								execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngSumDaryaft,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad 
							end 
							--سند پورسانت واسطه ها و ویزیتور ها
							if @lngMablaghPorsant>0
								begin
									set @intRadif=@intRadif+1

									--execute codemoinhesab '' ,@CodeMoinHesabhayeDaryaftani output
									--set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									--set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									--execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 
								--پایان سند پورسانت واسطه ها و ویزیتور ها 

								if @lngSumDaryaft>0
								begin
									set @ShomareDaryaft=(select isnull(max(fldShomareDaryaft),0) as fldMax from tblDaryaft)
									set @ShomareDaryaft=@ShomareDaryaft+1
									--ثبت دریافت اصلی فروش
									INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
													VALUES        (@ShomareDaryaft,@fldShomareSanad,@fldCodeTafsili,'foroosh')

									--ثبت ریز اطلاعات چک	
									Insert into  tblCheckeDaryafti (fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
											Select fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,@ShomareDaryaft,@fldCodeVasete,@fldShomareSanad output from @dtCheckDaryafti;	
								end 
							INSERT INTO tblBargashtAzForoosh
							   ([fldShomareFactorForoosh]
							   ,[fldShomareSanad]
							   ,[fldDate]
							   ,[fldSharhFactor]
							   )
								 values(@fldShomareFactor,@fldShomareSanad,@fldDate,@fldSharhFactor)

							 INSERT INTO tblRizeBargashtAzForoosh
						   ([fldShomareSanad]
						   ,[fldShomareFactor]
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime])
							select @fldShomareSanad
						   ,@fldShomareFactor
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime] output  from @dtForoosh;






					set @strResult='update'
					set @strResult2='update'
					
					--commit transaction AddRecord;
					--return
				end
				--تا اینجا تفصیلی ها به روز  شدند
			else if @flagForoosh='insert'
				begin
				--insert
						
					declare @resultExist bigint
					execute ExistFactorBargashtAzForoosh @fldShomareFactor,@resultExist output
					declare @maxFactor bigint
					execute GetMaxShomareFactorBargashtAzForoosh @maxFactor output
					set @maxFactor=@maxFactor+1
						
					if(@resultExist>0)
						begin
						--کاربر دیگری در همین لحظه شماره فاکتور را گرفت
							
							set @strResult=convert(nvarchar(17),@maxFactor)
							set @strResult2='New_Enter'
							rollback transaction AddRecord;
							return
							--در قسمت ویندوزی به کاربر کادری را نشان دهد که max+1 می باشد
							--yes
								--insert
							--no
								--inputbox در برنامه ویندوز که کاربر خودش شماره فاکتور را مشخص کند
								--باید اینجا چکار کنم؟؟؟؟
						end 
					--ehsan added
					 else if(@resultExist=0 and @flagInsert='yes')
					 --زمانی که کاربر از کادر پیام رویه دکمه بله کلیک میکنه
					     begin
						
							set @intRadif=@intRadif+1
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							execute InsertSanadFirst @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolFactor,0,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad output 
								
							if @lngHazineHamlVaNaghl>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'hamlvanaghl' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngHazineHamlVaNaghl,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end 
							if @lngArzeshAfzode>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'arzesheafzoode' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngArzeshAfzode,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end 

							set @intRadif=@intRadif+1
							execute codemoinhesab 'foroosh' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
							set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngsum,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							--بررسی شرح های دریافت
							if (@strSharhNaghd is null or @strSharhNaghd='')
								begin
									set @strSharhNaghd='noneDaryaft'
								end 
							if (@strSharhBank is null or @strSharhBank='')
								begin
									set @strSharhBank='noneDaryaft'
								end 
							if (@strSharhCheck is null or @strSharhCheck='')
								begin
									set @strSharhCheck='noneDaryaft'
								end 
							if (@strSharhTakhfif is null or  @strSharhTakhfif='')
								begin
									set @strSharhTakhfif='noneDaryaft'
								end 
							--شرح دریافت اصلی فروش
							if (@strSharhKoli is null or @strSharhKoli='')
								begin
									set @strSharhKoli='none'
								end 
	
							--پایان بررسی شرح های دریافت

							if @lngMablaghNaghdDaryaft>0
								begin
								--صندوق
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeSandoghDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghNaghdDaryaft,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 
							if @lngMablaghBankDaryaft>0
								begin
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeBankDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghBankDaryaft,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

							if @intHasRowCheckDaryafti>0
								begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'check' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
					
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolCheckHa,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

							if @lngMablaghTakhfifDaryaft>0
								begin
									set @intRadif=@intRadif+1

									execute codemoinhesab 'takhfifatedaryaft' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

								--ح س دریافتنی
							
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							--declare @lngSumDaryaft bigint
							set @lngSumDaryaft=(@lngMablaghTakhfifDaryaft+@lngMablaghBankDaryaft+@lngMablaghNaghdDaryaft+@lngMablaghKolCheckHa)
							if @lngSumDaryaft>0
							begin
								set @intRadif=@intRadif+1
								execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngSumDaryaft,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad 
							end
							--سند پورسانت واسطه ها و ویزیتور ها
							if @lngMablaghPorsant>0
								begin
									set @intRadif=@intRadif+1

									--execute codemoinhesab '' ,@CodeMoinHesabhayeDaryaftani output
									--set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									--set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									--execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 
								--پایان سند پورسانت واسطه ها و ویزیتور ها 

								if @lngSumDaryaft>0
								begin
									set @ShomareDaryaft=(select isnull(max(fldShomareDaryaft),0) as fldMax from tblDaryaft)
									set @ShomareDaryaft=@ShomareDaryaft+1
									--ثبت دریافت اصلی فروش
									INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
													VALUES        (@ShomareDaryaft,@fldShomareSanad,@fldCodeTafsili,'foroosh')

									--ثبت ریز اطلاعات چک	
									Insert into  tblCheckeDaryafti (fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
											Select fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,@ShomareDaryaft,@fldCodeVasete,@fldShomareSanad output from @dtCheckDaryafti;	
								end
							INSERT INTO tblBargashtAzForoosh
							   ([fldShomareFactorForoosh]
							   ,[fldShomareSanad]
							   ,[fldDate]
							   ,[fldSharhFactor]
							   )
								 values(@maxFactor,@fldShomareSanad,@fldDate,@fldSharhFactor)

							 INSERT INTO tblRizeBargashtAzForoosh
						   ([fldShomareSanad]
						   ,[fldShomareFactor]
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime])
							select @fldShomareSanad
						   ,@maxFactor
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime] output  from @dtForoosh;
							set @strResult=convert(nvarchar(17),@maxFactor)
							set @strResult2='insert'
							--commit transaction AddRecord;
						end
					else if(@resultExist=0 and @flagInsert='no')
						 begin
						
							set @intRadif=@intRadif+1
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							execute InsertSanadFirst @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolFactor,0,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad output 
							if @lngHazineHamlVaNaghl>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'hamlvanaghl' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngHazineHamlVaNaghl,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end
							if @lngArzeshAfzode>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'arzesheafzoode' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngArzeshAfzode,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end 

							set @intRadif=@intRadif+1
							execute codemoinhesab 'foroosh' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
							set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngsum,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							--بررسی شرح های دریافت
							if (@strSharhNaghd is null or @strSharhNaghd='')
								begin
									set @strSharhNaghd='noneDaryaft'
								end 
							if (@strSharhBank is null or @strSharhBank='')
								begin
									set @strSharhBank='noneDaryaft'
								end 
							if (@strSharhCheck is null or @strSharhCheck='')
								begin
									set @strSharhCheck='noneDaryaft'
								end 
							if (@strSharhTakhfif is null or @strSharhTakhfif='')
								begin
									set @strSharhTakhfif='noneDaryaft'
								end 
							--شرح دریافت اصلی فروش
							if (@strSharhKoli is null or @strSharhKoli='')
								begin
									set @strSharhKoli='noneDaryaft'
								end 
	
							--پایان بررسی شرح های دریافت

							if @lngMablaghNaghdDaryaft>0
								begin
								--صندوق
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeSandoghDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghNaghdDaryaft,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 
							if @lngMablaghBankDaryaft>0
								begin
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeBankDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghBankDaryaft,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

							if @intHasRowCheckDaryafti>0
								begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'check' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
					
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolCheckHa,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

							if @lngMablaghTakhfifDaryaft>0
								begin
									set @intRadif=@intRadif+1

									execute codemoinhesab 'takhfifatedaryaft' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

								--ح س دریافتنی
							
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							
							set @lngSumDaryaft=(@lngMablaghTakhfifDaryaft+@lngMablaghBankDaryaft+@lngMablaghNaghdDaryaft+@lngMablaghKolCheckHa)
							if @lngSumDaryaft>0
							begin
								set @intRadif=@intRadif+1
								execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngSumDaryaft,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad 
							end 
							--سند پورسانت واسطه ها و ویزیتور ها
							if @lngMablaghPorsant>0
								begin
									set @intRadif=@intRadif+1

									--execute codemoinhesab '' ,@CodeMoinHesabhayeDaryaftani output
									--set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									--set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									--execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 
								--پایان سند پورسانت واسطه ها و ویزیتور ها 
								if @lngSumDaryaft>0
								begin
									set @ShomareDaryaft=(select isnull(max(fldShomareDaryaft),0) as fldMax from tblDaryaft)
									set @ShomareDaryaft=@ShomareDaryaft+1
									--ثبت دریافت اصلی فروش
									INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
													VALUES        (@ShomareDaryaft,@fldShomareSanad,@fldCodeTafsili,'foroosh')

									--ثبت ریز اطلاعات چک	
									Insert into  tblCheckeDaryafti (fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
											Select fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,@ShomareDaryaft,@fldCodeVasete,@fldShomareSanad output from @dtCheckDaryafti;	
								end
							INSERT INTO tblBargashtAzForoosh
							   ([fldShomareFactorForoosh]
							   ,[fldShomareSanad]
							   ,[fldDate]
							   ,[fldSharhFactor]
							   )
								 values(@fldShomareFactor,@fldShomareSanad,@fldDate,@fldSharhFactor)

							 INSERT INTO tblRizeBargashtAzForoosh
													   ([fldShomareSanad]
													   ,[fldShomareFactor]
													   ,[fldCodeVasete]
													   ,[fldCodeTafsili]
													   ,[fldCodeUser]
													   ,[fldRadif]
													   ,[fldCodeAnbar]
													   ,[fldCodeKala]
													   ,[fldSharhKala]
													   ,[fldTedadCarton]
													   ,[fldTedadJoz]
													   ,[fldFeeJoz]
													   ,[fldFeeYekCarton]
													   ,[fldFeeKol]
													   ,[fldDarsadTakhfif]
													   ,[fldMablaghTakhfif]
													   ,[fldFeeVahed1BadAzTakhfif]
													   ,[fldFeeVahed2BadAzTakhfif]
													   ,[fldArzeshAfzode]
													   ,[fldFeeKolBadAzTakhfif]
													   ,[fldMablaghKhales]
													   ,[fldType]
													   ,[fldDate]
													   ,[fldTime])
														select @fldShomareSanad
														--edit
													   ,@fldShomareFactor
													   ,[fldCodeVasete]
													   ,[fldCodeTafsili]
													   ,[fldCodeUser]
													   ,[fldRadif]
													   ,[fldCodeAnbar]
													   ,[fldCodeKala]
													   ,[fldSharhKala]
													   ,[fldTedadCarton]
													   ,[fldTedadJoz]
													   ,[fldFeeJoz]
													   ,[fldFeeYekCarton]
													   ,[fldFeeKol]
													   ,[fldDarsadTakhfif]
													   ,[fldMablaghTakhfif]
													   ,[fldFeeVahed1BadAzTakhfif]
													   ,[fldFeeVahed2BadAzTakhfif]
													   ,[fldArzeshAfzode]
													   ,[fldFeeKolBadAzTakhfif]
													   ,[fldMablaghKhales]
													   ,[fldType]
													   ,[fldDate]
													   ,[fldTime] output  from @dtForoosh;
							set @strResult=convert(nvarchar(17),@fldShomareFactor)
							set @strResult2='insertByNo'
							--commit transaction AddRecord
						end
						
				end
			--set @strResult='errorx'
			--set @strResult2='errorx'
			--update sharh Ha
			--@fldShomarefactor dar khat zir bayad avaz beshe (shayad =>@fldShomarefactorNew )
			 if @flagInsert='yes'
				begin
					update  tblSodooreSanad
							set fldSharh ='فاکتور شماره ' + convert(nvarchar(17),@maxFactor)
							where fldSanad=@fldShomareSanad and fldSharh like 'none'
					update  tblSodooreSanad
							set fldSharh ='فاکتور شماره ' + convert(nvarchar(17),@maxFactor) + ' - شماره دریافت ' +  convert(nvarchar(17),@ShomareDaryaft) 
							where fldSanad=@fldShomareSanad and fldSharh like 'noneDaryaft'
				end
			else
				begin
					 update  tblSodooreSanad
							set fldSharh ='فاکتور شماره ' + convert(nvarchar(17),@fldShomarefactor)
							where fldSanad=@fldShomareSanad and fldSharh like 'none'

					update  tblSodooreSanad
							set fldSharh ='فاکتور شماره ' + convert(nvarchar(17),@fldShomarefactor) + ' - شماره دریافت ' +  convert(nvarchar(17),@ShomareDaryaft) 
							where fldSanad=@fldShomareSanad and fldSharh like 'noneDaryaft'
				end


				commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				set @strResult2='error'
				set @ErrorLine=( Select ERROR_LINE() as errln)
				if @ErrorLine is null 
					set @errorline=0
				rollback transaction AddRecord;
			end
		end catch
END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoBargashtAzKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertInfoBargashtAzKharid]
--چندزبانه
@fldCodeArz int,
@fldNerkheArz decimal(18,4),
@flagKharid nvarchar(10),
@dtCheckDaryafti [tblCheckDaryafti-Multi] readonly,
@intHasRowCheckDaryafti int,
@lngMablaghKolCheckHa decimal(18,4),
@lngMablaghNaghdDaryaft decimal(18,4),
@lngMablaghBankDaryaft decimal(18,4),
@lngMablaghTakhfifDaryaft decimal(18,4),


@dtKharid [tpRizeKharid_FactorKharid_FinalNew-Multi] readonly,
@fldSharhfactor nvarchar(250),
@fldShomarefactor bigint,
@fldShomaresanad bigint,
@fldDate bigint,
@fldCodeTafsili bigint,


@lngHazineHamlVaNaghl decimal(18,4),
@lngArzeshAfzode decimal(18,4),
@lngMablaghKolFactor decimal(18,4),

@lngCodeSandoghDaryaft bigint,
@lngCodeBankDaryaft bigint,

--شرح دریافت
@strSharhNaghd nvarchar(50), 
@strSharhBank nvarchar(50),
@strSharhCheck nvarchar(50),
@strSharhTakhfif nvarchar(50),
@strSharhKoli nvarchar(50),

@strResult nvarchar(25) output,
@strResult2 NVarChar (25) output,
@flagInsert nvarchar(10)
AS
create table #tmpColor (
	MoinKamel nvarchar(50) COLLATE Persian_100_CI_AS,
	fldColorBank int,
	fldColorSandoogh int , 
	fldColorSooratHesab int
)

BEGIN
begin transaction AddRecord
			begin try
					--set @MablaghCheckVagozari=@lngMablaghKolCheckMoshtari
					--set @MablaghCheck=@lngMablaghKolCheckHaPardakhti
					declare @intRadif int,@CodeMoinHesabhayeDaryaftani nvarchar(100),@CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@TooleKol bigint,@Toolemoin bigint, @lngsum decimal(18,4)
					declare @day nvarchar(4),@Month nvarchar(4),@year nvarchar(5),@yearFull nvarchar(5),@dateSmall bigint
					declare @fldCodeVasete bigint,@strMoinKamel nvarchar(50),@lngSumPardakht decimal(18,4),@lngSumDaryaft decimal(18,4),@ShomareDaryaft bigint
					--,@ShomarePardakht bigint,@lngSumPardakht bigint
					--set @year=substring(convert(nvarchar(20),@fldDate),3,2)
					set @yearFull=substring(convert(nvarchar(20),@fldDate),0,5)
					set @year=@yearFull
					set @month=substring(convert(nvarchar(20),@fldDate),5,2)
					set @day=substring(convert(nvarchar(20),@fldDate),7,2)
					--set @dateSmall=substring(convert(nvarchar(15),@fldDate),3,6)
					set @dateSmall=@fldDate
					declare @maxFactor bigint
					set @intRadif=0
					set @fldCodeVasete=0
					----------End Declare and set Init Values----------

			if(@flagKharid='arshive')
				begin
				--delete
					delete from tblRizeBargashtAzKharid
					where fldShomareSanad=@fldShomareSanad and fldShomareFactor=@fldShomareFactor
					IF @@ROWCOUNT = 0
					begin
					set @strResult='NotDeletedRizeBargashtAzKharid'
					rollback transaction DelRecord;
					end
					delete from tblBargashtAzKharid
					where fldShomareSanad=@fldShomareSanad and fldShomareFactorKharid=@fldShomareFactor
					IF @@ROWCOUNT = 0
					begin
					set @strResult='NotDeletedBargashtAzKharid'
					rollback transaction DelRecord;
					end

					insert into #tmpColor
								(MoinKamel,fldColorBank ,fldColorSandoogh , fldColorSooratHesab )
								Select fldMoinKamel,fldColorBank,fldColorSandoogh,fldColorSooratHesab from tblSodooreSanad Where fldSanad=@fldShomareSanad
					delete from tblSodooreSanad
					where fldSanad=@fldShomareSanad
					IF @@ROWCOUNT = 0
					begin
					set @strResult='NotDeletedtblSodooreSanad'
					rollback transaction DelRecord;
					end
					delete from tblDaryaft
					where fldShomareSanad=@fldShomareSanad

					delete from tblCheckeDaryafti
							where fldSanad=@fldShomareSanad and (fldShomareVosool  is  null and fldShomarePardakht  is  null) and fldVaziat is  null
					


				--insert
				----------hamlonaghl and arzeshafzode and BargashtAzKharid--------------
				set @intRadif=@intRadif+1
				execute ToolePaye 'kol',@TooleKol output
				execute ToolePaye 'moin',@Toolemoin output
				execute codemoinhesab 'kharid' ,@CodeMoinHesabhayeDaryaftani output
				set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
				set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
				set @lngsum=(@lngMablaghKolFactor-(@lngHazineHamlVaNaghl+@lngArzeshAfzode))
				--upd
				execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngsum,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad

				if @lngHazineHamlVaNaghl>0
				begin
					set @intRadif=@intRadif+1
					execute codemoinhesab 'hazinehamlvanaghlekharid' ,@CodeMoinHesabhayeDaryaftani output
					set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
					--upd
					execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngHazineHamlVaNaghl,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
				end

				if @lngArzeshAfzode>0
				begin
					set @intRadif=@intRadif+1
					execute codemoinhesab 'arzesheafzoodekharid' ,@CodeMoinHesabhayeDaryaftani output
					set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
					--upd
					execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngArzeshAfzode,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
				end
				-----------------end hamlonaghl and arzeshafzode and kharid-------------
				set @intRadif=@intRadif+1
				execute ToolePaye 'kol',@TooleKol output
				execute ToolePaye 'moin',@Toolemoin output
				execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayeDaryaftani output
				set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
				set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
				--بدهکار
				--upd
				execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolFactor,0,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
				-----------------------daryaft----------------------------------------
				--بررسی شرح های پرداخت
				if (@strSharhNaghd is null or @strSharhNaghd='')
				begin
					set @strSharhNaghd='noneDaryaft'
				end
				if (@strSharhBank is null or @strSharhBank='')
				begin
					set @strSharhBank='noneDaryaft'
				end
				if (@strSharhCheck is null or @strSharhCheck='')
				begin
					set @strSharhCheck='noneDaryaft'
				end
				if (@strSharhTakhfif is null or @strSharhTakhfif='')
				begin
					set @strSharhTakhfif='noneDaryaft'
				end
				--شرح دریافت اصلی فروش
				if (@strSharhKoli is null or @strSharhKoli='')
				begin
					set @strSharhKoli='noneDaryaft'
				end

				--پایان بررسی شرح های دریافت
				set @lngSumDaryaft=(@lngMablaghNaghdDaryaft+@lngMablaghBankDaryaft+@lngMablaghKolCheckHa+@lngMablaghTakhfifDaryaft)

				if(@lngSumDaryaft>0)
				begin
					set @intRadif=@intRadif+1
					execute ToolePaye 'kol',@TooleKol output
					execute ToolePaye 'moin',@Toolemoin output
					execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayeDaryaftani output
					set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
					--بدهکار
					--upd
					execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngSumDaryaft,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
				end

				if @lngMablaghNaghdDaryaft>0
				begin
									--صندوق
					set @intRadif=@intRadif+1
					set @CodeMoinHesabhayeDaryaftani=@lngCodeSandoghDaryaft
					set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
					set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
					execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghNaghdDaryaft,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad								
					--execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,@lngMablaghNaghdPardakht,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
				end
				--سند بانک
				if @lngMablaghBankDaryaft>0
				--begin
				--	set @intRadif =@intRadif+1

				--	set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
				--	set @strMoinKamel=@CodeKolBank + @CodeMoinBank
				--	--upd
				--	execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@lngMablaghBankPardakht,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
				--end
				begin
					set @intRadif=@intRadif+1
					set @CodeMoinHesabhayeDaryaftani=@lngCodeBankDaryaft
					set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
					set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
					execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghBankDaryaft,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
					--	execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@lngMablaghBankPardakht,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
				end 
				--سند چک
				if @lngMablaghKolCheckHa>0
				--begin
				--	set @intRadif =@intRadif+1

				--	set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)
				--	set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck
				--	--upd
				--	execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@fldCodeTafsili,'1',@lngMablaghKolCheckHaPardakhti,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
				--end
				begin
					set @intRadif=@intRadif+1
					execute codemoinhesab 'check' ,@CodeMoinHesabhayeDaryaftani output
					set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
					
					execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolCheckHa,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
					--	execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@fldCodeTafsili,'1',@lngMablaghKolCheckHaPardakhti,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
				end 
				

				if @lngMablaghTakhfifDaryaft>0
				--begin
				--	set @intRadif =@intRadif+1
				--	set @CodeMoinKham=substring(@CodeMoinTakhfifatePardakht,@TooleKol+1,@Toolemoin)
				--	set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifatePardakht
				--	--upd
				--	execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,@lngMablaghTakhfifPardakht,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
				--end
				begin
					set @intRadif=@intRadif+1

					execute codemoinhesab 'takhfifatedaryaft' ,@CodeMoinHesabhayeDaryaftani output
					set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
					--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
					execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
					--	execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,@lngMablaghTakhfifPardakht,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
				end 

				set @lngSumDaryaft=(@lngMablaghTakhfifDaryaft+@lngMablaghBankDaryaft+@lngMablaghNaghdDaryaft+@lngMablaghKolCheckHa)
				if @lngSumDaryaft>0
					begin
						set @ShomareDaryaft=(select isnull(max(fldShomareDaryaft),0) as fldMax from tblDaryaft)
						set @ShomareDaryaft=@ShomareDaryaft+1
						--ثبت دریافت 
						INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
										VALUES        (@ShomareDaryaft,@fldShomareSanad,@fldCodeTafsili,'bargashtazkharid')

						--ثبت ریز اطلاعات چک	
						Insert into  tblCheckeDaryafti (fldCodeArz,fldNerkheArz, fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
								Select  @fldCodeArz,@fldNerkheArz,  fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,@ShomareDaryaft,@fldCodeVasete,@fldShomareSanad output from @dtCheckDaryafti;	
						
						delete tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft and  fldvaziat is null and fldShomareCheck in (Select fldShomareCheck from tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft and fldVaziat is not null)
					end

				-----------------------factor--------------------------------------

				set @maxFactor=@fldShomarefactor


				INSERT INTO tblBargashtAzKharid ([fldShomareFactorKharid],[fldShomareSanad],[fldDate],[fldSharhFactor])
					values(@maxFactor,@fldShomareSanad,@fldDate,@fldSharhFactor)
					INSERT INTO tblRizeBargashtAzKharid([fldShomareSanad],[fldShomareFactor]
					,[fldCodeVasete],[fldCodeTafsili],[fldCodeUser],[fldRadif],[fldCodeAnbar]
					,[fldCodeKala],[fldSharhKala],[fldTedadCarton],[fldTedadJoz],[fldFeeJoz]
					,[fldFeeYekCarton],[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif]
					,[fldFeeVahed1BadAzTakhfif],[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode]
					,[fldFeeKolBadAzTakhfif],[fldMablaghKhales],[fldType],[fldDate],[fldTime]
					,[fldHamloNaghl],[fldGhymatMasrafkonande],[fldDateEngheza])
					select @fldShomareSanad,@maxFactor,[fldCodeVasete],[fldCodeTafsili]
					,[fldCodeUser],[fldRadif],[fldCodeAnbar],[fldCodeKala],[fldSharhKala]
					,[fldTedadCarton],[fldTedadJoz],[fldFeeJoz],[fldFeeYekCarton]
					,[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif],[fldFeeVahed1BadAzTakhfif]
					,[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode],[fldFeeKolBadAzTakhfif]
					,[fldMablaghKhales],[fldType],[fldDate],[fldTime],[fldHamloNaghl]
					,[fldGhymatMasrafkonande],[fldDateEngheza]
					output from @dtKharid;
					set @strResult=convert(nvarchar(17),@maxFactor)
					set @strResult2='update'
					set @flagInsert='yes'
				end
			else if(@flagKharid='insert')
				begin
				    declare @resultExist bigint
					execute ExistFactorBargashtAzKharid @fldShomareFactor,@resultExist output
					declare @maxFactorKharidForKarbar bigint
					execute GetMaxShomareFactorBargashtAzkharid @maxFactorKharidForKarbar output
					set @maxFactorKharidForKarbar=@maxFactorKharidForKarbar+1
						
					if(@resultExist>0)
						begin
						--کاربر دیگری در همین لحظه شماره فاکتور را گرفت
							
							set @strResult=convert(nvarchar(17),@maxFactorKharidForKarbar)
							set @strResult2='New_Enter'
							rollback transaction AddRecord;
							return
							--در قسمت ویندوزی به کاربر کادری را نشان دهد که max+1 می باشد
							--yes
								--insert
							--no
								--inputbox در برنامه ویندوز که کاربر خودش شماره فاکتور را مشخص کند
								--باید اینجا چکار کنم؟؟؟؟
						end 
					else if(@resultExist=0 and @flagInsert='yes')
					    begin
						 --زمانی که کاربر از کادر پیام رویه دکمه بله کلیک میکنه
						--ehsan test
							--declare @resultExist bigint
							execute ExistFactorBargashtAzKharid @fldShomareFactor,@resultExist output
							--declare @maxFactorKharidForKarbar bigint
							--execute GetMaxShomareFactorBargashtAzKharid @maxFactorKharidForKarbar output
							set @maxFactorKharidForKarbar=@fldShomarefactor
							--set @maxFactorKharidForKarbar=@maxFactorKharidForKarbar+1
						
							if(@resultExist>0)
								begin
								--کاربر دیگری در همین لحظه شماره فاکتور را گرفت
							
									set @strResult=convert(nvarchar(17),@maxFactorKharidForKarbar)
									set @strResult2='New_Enter'
									rollback transaction AddRecord;
									return
									--در قسمت ویندوزی به کاربر کادری را نشان دهد که max+1 می باشد
									--yes
										--insert
									--no
										--inputbox در برنامه ویندوز که کاربر خودش شماره فاکتور را مشخص کند
								end 
						--ehsan test end
									----------hamlonaghl and arzeshafzode and kharid--------------
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output									
									execute codemoinhesab 'kharid' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
									set @lngsum=(@lngMablaghKolFactor-(@lngHazineHamlVaNaghl+@lngArzeshAfzode))									
									execute InsertSanadfirstMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngsum,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad output

									if @lngHazineHamlVaNaghl>0
									begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'hazinehamlvanaghlekharid' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
									--upd
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngHazineHamlVaNaghl,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
									end

									if @lngArzeshAfzode>0
									begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'arzesheafzoodekharid' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
									--upd
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngArzeshAfzode,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
									end
									-----------------end hamlonaghl and arzeshafzode and kharid-------------
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
									--بستانکار
									--upd
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolFactor,0,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
									-----------------------pardakht----------------------------------------
									--بررسی شرح های پرداخت
									if (@strSharhNaghd is null or @strSharhNaghd='')
									begin
									set @strSharhNaghd='noneDaryaft'
									end
									if (@strSharhBank is null or @strSharhBank='')
									begin
									set @strSharhBank='noneDaryaft'
									end
									if (@strSharhCheck is null or @strSharhCheck='')
									begin
									set @strSharhCheck='noneDaryaft'
									end
									if (@strSharhTakhfif is null or @strSharhTakhfif='')
									begin
									set @strSharhTakhfif='noneDaryaft'
									end
									--شرح دریافت اصلی فروش
									if (@strSharhKoli is null or @strSharhKoli='')
									begin
									set @strSharhKoli='noneDaryaft'
									end

									--پایان بررسی شرح های پرداخت
									set @lngSumDaryaft=(@lngMablaghNaghdDaryaft+@lngMablaghBankDaryaft+@lngMablaghKolCheckHa+@lngMablaghTakhfifDaryaft)

									if(@lngSumDaryaft>0)
									begin
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
									--بدهکار
									--upd
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngSumDaryaft,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
									end

									if @lngMablaghNaghdDaryaft>0
									begin
									--صندوق
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeSandoghDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghNaghdDaryaft,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad								
									--execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,@lngMablaghNaghdPardakht,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
								end
									--سند بانک
									if @lngMablaghBankDaryaft>0
										begin
											--set @intRadif =@intRadif+1

											--set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
											--set @strMoinKamel=@CodeKolBank + @CodeMoinBank
											set @intRadif=@intRadif+1
											set @CodeMoinHesabhayeDaryaftani=@lngCodeBankDaryaft
											set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
											set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											--upd
											--execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@strMoinKamel,NUll,NULL,@lngMablaghBankdaryaft,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghBankDaryaft,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
										end
									--سند چک
									if @lngMablaghKolCheckHa>0
										begin
											set @intRadif=@intRadif+1
											execute codemoinhesab 'check' ,@CodeMoinHesabhayeDaryaftani output
											set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
											----upd
											--execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@fldCodeTafsili,'1',@lngMablaghKolCheckHaPardakhti,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;							
											  execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolCheckHa,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
										end
									

									if @lngMablaghTakhfifDaryaft>0
									begin
										--set @intRadif =@intRadif+1
										--set @CodeMoinKham=substring(@CodeMoinTakhfifatePardakht,@TooleKol+1,@Toolemoin)
										--set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifatePardakht
										----upd
										--execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,@lngMablaghTakhfifPardakht,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
										set @intRadif=@intRadif+1

										execute codemoinhesab 'takhfifatedaryaft' ,@CodeMoinHesabhayeDaryaftani output
										set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
										set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)												
										execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
									end
									

									if @lngSumDaryaft>0
										begin
											set @ShomareDaryaft=(select isnull(max(fldShomareDaryaft),0) as fldMax from tblDaryaft)
											set @ShomareDaryaft=@ShomareDaryaft+1
											--ثبت دریافت
											INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
															VALUES        (@ShomareDaryaft,@fldShomareSanad,@fldCodeTafsili,'bargashtazkharid')

											--ثبت ریز اطلاعات چک	
											Insert into  tblCheckeDaryafti (fldCodeArz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
													Select @fldCodeArz,@fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,@ShomareDaryaft,@fldCodeVasete,@fldShomareSanad output from @dtCheckDaryafti;	
										end 

									-----------------------factor--------------------------------------

									--execute GetMaxShomareFactorKharid @maxFactor output
									set @maxFactor=@maxFactorKharidForKarbar

									INSERT INTO tblBargashtAzKharid ([fldShomareFactorKharid],[fldShomareSanad],[fldDate],[fldSharhFactor])
									values(@maxFactor,@fldShomareSanad,@fldDate,@fldSharhFactor)
									INSERT INTO tblRizeBargashtAzKharid([fldShomareSanad],[fldShomareFactor]
									,[fldCodeVasete],[fldCodeTafsili],[fldCodeUser],[fldRadif],[fldCodeAnbar]
									,[fldCodeKala],[fldSharhKala],[fldTedadCarton],[fldTedadJoz],[fldFeeJoz]
									,[fldFeeYekCarton],[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif]
									,[fldFeeVahed1BadAzTakhfif],[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode]
									,[fldFeeKolBadAzTakhfif],[fldMablaghKhales],[fldType],[fldDate],[fldTime]
									,[fldHamloNaghl],[fldGhymatMasrafkonande],[fldDateEngheza])
									select @fldShomareSanad,@maxFactor,[fldCodeVasete],[fldCodeTafsili]
									,[fldCodeUser],[fldRadif],[fldCodeAnbar],[fldCodeKala],[fldSharhKala]
									,[fldTedadCarton],[fldTedadJoz],[fldFeeJoz],[fldFeeYekCarton]
									,[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif],[fldFeeVahed1BadAzTakhfif]
									,[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode],[fldFeeKolBadAzTakhfif]
									,[fldMablaghKhales],[fldType],[fldDate],[fldTime],[fldHamloNaghl]
									,[fldGhymatMasrafkonande],[fldDateEngheza]
									output from @dtKharid;
									set @strResult=convert(nvarchar(17),@maxFactor)
									set @strResult2='insert'
									set @flagInsert='yes_byKarbar'
						end
					else if(@resultExist=0 and @flagInsert='no')
					    begin
						
									----------hamlonaghl and arzeshafzode and kharid--------------
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'kharid' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
									set @lngsum=(@lngMablaghKolFactor-(@lngHazineHamlVaNaghl+@lngArzeshAfzode))
									--upd
									execute InsertSanadfirstMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngsum,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad output

									if @lngHazineHamlVaNaghl>0
									begin
										set @intRadif=@intRadif+1
										execute codemoinhesab 'hazinehamlvanaghlekharid' ,@CodeMoinHesabhayeDaryaftani output
										set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
										set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
										--upd
										execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngHazineHamlVaNaghl,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
									end

									if @lngArzeshAfzode>0
										begin
											set @intRadif=@intRadif+1
											execute codemoinhesab 'arzesheafzoodekharid' ,@CodeMoinHesabhayeDaryaftani output
											set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
											--upd
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngArzeshAfzode,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
										end
									-----------------end hamlonaghl and arzeshafzode and kharid-------------
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
									--بستانکار
									--upd
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolFactor,0,@intRadif,@dateSmall,'none',@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
									-----------------------pardakht----------------------------------------
									--بررسی شرح های پرداخت
									if (@strSharhNaghd is null or @strSharhNaghd='')
										begin
										set @strSharhNaghd='noneDaryaft'
										end
									if (@strSharhBank is null or @strSharhBank='')
										begin
										set @strSharhBank='noneDaryaft'
										end
									if (@strSharhCheck is null or @strSharhCheck='')
										begin
										set @strSharhCheck='noneDaryaft'
										end
									if (@strSharhTakhfif is null or @strSharhTakhfif='')
										begin
										set @strSharhTakhfif='noneDaryaft'
										end
									--شرح دریافت اصلی فروش
									if (@strSharhKoli is null or @strSharhKoli='')
										begin
										set @strSharhKoli='noneDaryaft'
										end

									--پایان بررسی شرح های پرداخت
									set @lngSumDaryaft=(@lngMablaghNaghdDaryaft+@lngMablaghBankDaryaft+@lngMablaghKolCheckHa+@lngMablaghTakhfifDaryaft)

									if(@lngSumDaryaft>0)
									begin
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
									--بدهکار
									--upd
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngSumDaryaft,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
									end

									if @lngMablaghNaghdDaryaft>0
										begin
											--set @intRadif =@intRadif+1
											--set @CodeMoinKham=substring(@CodeMoinSandoogh,@TooleKol+1,@Toolemoin)
											--set @strMoinKamel=@CodeKolSandoogh + @CodeMoinSandoogh
											----upd
											--execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,@lngMablaghNaghdPardakht,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
											set @intRadif=@intRadif+1
											set @CodeMoinHesabhayeDaryaftani=@lngCodeSandoghDaryaft
											set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
											set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghNaghdDaryaft,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
										end
									--سند بانک
									if @lngMablaghBankDaryaft>0
										begin
											--set @intRadif =@intRadif+1

											--set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
											--set @strMoinKamel=@CodeKolBank + @CodeMoinBank
											----upd
											--execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@lngMablaghBankPardakht,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
											set @intRadif=@intRadif+1
											set @CodeMoinHesabhayeDaryaftani=@lngCodeBankDaryaft
											set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
											set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghBankDaryaft,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
										end
									--سند چک
									--if @lngMablaghKolCheckHaPardakhti>0
									--begin
									--set @intRadif =@intRadif+1

									--set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)
									--set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck
									----upd
									--execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@fldCodeTafsili,'1',@lngMablaghKolCheckHaPardakhti,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
									--end
									if @intHasRowCheckDaryafti>0
										begin
											set @intRadif=@intRadif+1
											execute codemoinhesab 'check' ,@CodeMoinHesabhayeDaryaftani output
											set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
					
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolCheckHa,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
										end 
									

									--if @lngMablaghTakhfifPardakht>0
									--begin
									--set @intRadif =@intRadif+1
									--set @CodeMoinKham=substring(@CodeMoinTakhfifatePardakht,@TooleKol+1,@Toolemoin)
									--set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifatePardakht
									----upd
									--execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,@lngMablaghTakhfifPardakht,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad;
									--end
									if @lngMablaghTakhfifDaryaft>0
										begin
											set @intRadif=@intRadif+1
											
											execute codemoinhesab 'takhfifatedaryaft' ,@CodeMoinHesabhayeDaryaftani output
											set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'bargashtazkharid',@fldCodeVasete,@fldShomareSanad
										end 
									if @lngSumDaryaft>0
										begin
											set @ShomareDaryaft=(select isnull(max(fldShomareDaryaft),0) as fldMax from tblDaryaft)
											set @ShomareDaryaft=@ShomareDaryaft+1
											--ثبت دریافت اصلی 
											INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
															VALUES        (@ShomareDaryaft,@fldShomareSanad,@fldCodeTafsili,'bargashtazkharid')

											--ثبت ریز اطلاعات چک	
											Insert into  tblCheckeDaryafti (fldCodeArz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
													Select @fldCodeArz,@fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,@ShomareDaryaft,@fldCodeVasete,@fldShomareSanad output from @dtCheckDaryafti;	
										end

									-----------------------factor--------------------------------------

									--execute GetMaxShomareFactorKharid @maxFactor output
									set @maxFactor=@fldShomareFactor

									INSERT INTO tblBargashtAzKharid ([fldShomareFactorKharid],[fldShomareSanad],[fldDate],[fldSharhFactor])
									values(@maxFactor,@fldShomareSanad,@fldDate,@fldSharhFactor)
									INSERT INTO tblRizeBargashtAzKharid([fldShomareSanad],[fldShomareFactor]
									,[fldCodeVasete],[fldCodeTafsili],[fldCodeUser],[fldRadif],[fldCodeAnbar]
									,[fldCodeKala],[fldSharhKala],[fldTedadCarton],[fldTedadJoz],[fldFeeJoz]
									,[fldFeeYekCarton],[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif]
									,[fldFeeVahed1BadAzTakhfif],[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode]
									,[fldFeeKolBadAzTakhfif],[fldMablaghKhales],[fldType],[fldDate],[fldTime]
									,[fldHamloNaghl],[fldGhymatMasrafkonande],[fldDateEngheza])
									select @fldShomareSanad,@maxFactor,[fldCodeVasete],[fldCodeTafsili]
									,[fldCodeUser],[fldRadif],[fldCodeAnbar],[fldCodeKala],[fldSharhKala]
									,[fldTedadCarton],[fldTedadJoz],[fldFeeJoz],[fldFeeYekCarton]
									,[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif],[fldFeeVahed1BadAzTakhfif]
									,[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode],[fldFeeKolBadAzTakhfif]
									,[fldMablaghKhales],[fldType],[fldDate],[fldTime],[fldHamloNaghl]
									,[fldGhymatMasrafkonande],[fldDateEngheza]
									output from @dtKharid;
									set @strResult=convert(nvarchar(17),@maxFactor)
									set @strResult2='insert'
									set @flagInsert='yes_byNo'
						end
			

			end--aaaa


			update tblSodooreSanad set fldColorBank=#tmpColor.fldColorBank,fldColorSandoogh=#tmpColor.fldColorSandoogh,fldColorSooratHesab=#tmpColor.fldColorSooratHesab from #tmpColor Where tblSodooreSanad.fldSanad=@fldShomareSanad and fldMoinKamel=#tmpColor.MoinKamel

			if @flagInsert='yes'
				begin
					update tblSodooreSanad
					--set fldSharh ='برگشت از خرید شماره ' + convert(nvarchar(17),@maxFactor)
					set fldSharh =''
					where fldSanad=@fldShomareSanad and fldSharh like 'none'
					update tblSodooreSanad
					--set fldSharh ='برگشت از خرید شماره ' + convert(nvarchar(17),@maxFactor) + ' - شماره دریافت ' + convert(nvarchar(17),@ShomareDaryaft)
					set fldSharh =''
					where fldSanad=@fldShomareSanad and fldSharh like 'noneDaryaft'
				end
			else
				begin
					update tblSodooreSanad
					--set fldSharh ='برگشت از خرید شماره ' + convert(nvarchar(17),@fldShomarefactor)
					set fldSharh =''
					where fldSanad=@fldShomareSanad and fldSharh like 'none'

					update tblSodooreSanad
					--set fldSharh ='برگشت از خرید شماره ' + convert(nvarchar(17),@fldShomarefactor) + ' - شماره دریافت ' + convert(nvarchar(17),@ShomareDaryaft)
					set fldSharh =''
					where fldSanad=@fldShomareSanad and fldSharh like 'noneDaryaft'
				end

			commit transaction AddRecord;
   end try
		begin catch
			if @@TranCount>0
				begin
					set @strResult='error'
					set @strResult2='error'
					rollback transaction AddRecord;
				end
		end catch


END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoEnteghal]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertInfoEnteghal]
--چندزبانه
	@dtForoosh [tpRizeForoosh_EnteghalNew-Multi] readonly,
	@dtKharid [tpRizeKharid_EnteghalNew-Multi] readonly,
	@fldShomareEnteghal bigint,
	@fldCodeAnbar_out bigint,
	@fldCodeAshkhas_out bigint,
	@fldCodeAnbar_in bigint,
	@fldCodeAshkhas_in bigint,
	@strResult nvarchar(25) output,
    @InsertType nvarchar(50)
	
AS
BEGIN

	SET NOCOUNT ON;

        begin transaction AddRecord
             declare @lngCountEnteghal decimal(18,4)
             begin try  --try

			                  set @lngCountEnteghal=(Select Count(fldShomareEnteghal) from tblRizeForoosh Where fldShomareEnteghal=@fldShomareEnteghal)
					          if ( @lngCountEnteghal = 0)
							    begin
									if @InsertType='insert'
									  begin
									INSERT INTO [dbo].[tblRizeForoosh]
										   ([fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,[fldCodeTafsili]
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,[fldCodeAnbar]
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,[fldShomareEnteghal])
											SELECT
											[fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,@fldCodeAshkhas_out
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,@fldCodeAnbar_out
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,@fldShomareEnteghal 
										   output from @dtForoosh


																		   --ثبت انبار وارده ریز خرید
									INSERT INTO [dbo].[tblRizeKharid]
												(
												--[fldCodeTafsili]
												--,[fldCodeUser]
												--,[fldRadif]
												--,[fldCodeAnbar]
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,[fldShomareEnteghal]
											[fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,[fldCodeTafsili]
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,[fldCodeAnbar]
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,[fldShomareEnteghal])
												select
												--@fldCodeAshkhas_in
												--,[fldCodeUser]
												--,[fldRadif]
												--,@fldCodeAnbar_in
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,@fldShomareEnteghal
											[fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,@fldCodeAshkhas_in
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,@fldCodeAnbar_in
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,@fldShomareEnteghal 

												output from @dtKharid;

										set @strResult='insertOk'

											end --flag insert
									end --count rowShomare Enteghal

							--set @lngCountEnteghal=(Select Count(fldShomareEnteghal) from tblRizeForoosh Where fldShomareEnteghal=@fldShomareEnteghal)
							if ( @lngCountEnteghal > 0)
									begin
										if @InsertType='archive'
										  begin
											delete from tblRizeForoosh where fldShomareEnteghal=@fldShomareEnteghal and fldType='enteghal'
											delete from tblRizeKharid where fldShomareEnteghal=@fldShomareEnteghal and fldType='enteghal'

										INSERT INTO [dbo].[tblRizeForoosh]
												([fldShomareSanad]
												,[fldShomareFactor]
												,[fldCodeVasete]
												,[fldCodeTafsili]
												,[fldCodeUser]
												,[fldRadif]
												,[fldCodeAnbar]
												,[fldCodeKala]
												,[fldSharhKala]
												,[fldTedadCarton]
												,[fldTedadJoz]
												,[fldFeeJoz]
												,[fldFeeYekCarton]
												,[fldFeeKol]
												,[fldDarsadTakhfif]
												,[fldMablaghTakhfif]
												,[fldFeeVahed1BadAzTakhfif]
												,[fldFeeVahed2BadAzTakhfif]
												,[fldArzeshAfzode]
												,[fldFeeKolBadAzTakhfif]
												,[fldMablaghKhales]
												,[fldType]
												,[fldDate]
												,[fldTime]
												,[fldShomareEnteghal])
												SELECT
												[fldShomareSanad]
												,[fldShomareFactor]
												,[fldCodeVasete]
												,@fldCodeAshkhas_out
												,[fldCodeUser]
												,[fldRadif]
												,@fldCodeAnbar_out
												,[fldCodeKala]
												,[fldSharhKala]
												,[fldTedadCarton]
												,[fldTedadJoz]
												,[fldFeeJoz]
												,[fldFeeYekCarton]
												,[fldFeeKol]
												,[fldDarsadTakhfif]
												,[fldMablaghTakhfif]
												,[fldFeeVahed1BadAzTakhfif]
												,[fldFeeVahed2BadAzTakhfif]
												,[fldArzeshAfzode]
												,[fldFeeKolBadAzTakhfif]
												,[fldMablaghKhales]
												,[fldType]
												,[fldDate]
												,[fldTime]
												,@fldShomareEnteghal 
												output from @dtForoosh


								--ثبت انبار وارده ریز خرید
												INSERT INTO [dbo].[tblRizeKharid]
															(
												--[fldCodeTafsili]
												--,[fldCodeUser]
												--,[fldRadif]
												--,[fldCodeAnbar]
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,[fldShomareEnteghal]
											[fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,[fldCodeTafsili]
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,[fldCodeAnbar]
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,[fldShomareEnteghal])
												select
												--@fldCodeAshkhas_in
												--,[fldCodeUser]
												--,[fldRadif]
												--,@fldCodeAnbar_in
												--,[fldCodeKala]
												--,[fldSharhKala]
												--,[fldTedadJoz]
												--,[fldType]
												--,[fldDate]
												--,[fldTime]
												--,@fldShomareEnteghal
											[fldShomareSanad]
										   ,[fldShomareFactor]
										   ,[fldCodeVasete]
										   ,@fldCodeAshkhas_in
										   ,[fldCodeUser]
										   ,[fldRadif]
										   ,@fldCodeAnbar_in
										   ,[fldCodeKala]
										   ,[fldSharhKala]
										   ,[fldTedadCarton]
										   ,[fldTedadJoz]
										   ,[fldFeeJoz]
										   ,[fldFeeYekCarton]
										   ,[fldFeeKol]
										   ,[fldDarsadTakhfif]
										   ,[fldMablaghTakhfif]
										   ,[fldFeeVahed1BadAzTakhfif]
										   ,[fldFeeVahed2BadAzTakhfif]
										   ,[fldArzeshAfzode]
										   ,[fldFeeKolBadAzTakhfif]
										   ,[fldMablaghKhales]
										   ,[fldType]
										   ,[fldDate]
										   ,[fldTime]
										   ,@fldShomareEnteghal 

												output from @dtKharid;


															set @strResult='insertArchiveOk'
															end

												else 
													begin
														if @InsertType='insert'
														   begin
														    set @strResult='error_tekrari_insert'
														      rollback transaction 
														       return
														   end
													 end
												end

																							

				              commit transaction AddRecord;

           end try    --try

		  begin catch
		   if @@TranCount > 0
			 begin
				set @strResult='error'
				rollback transaction AddRecord;
			  end
		   end catch
   
		END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertInfoForoosh]
	--چندزبانه
	@fldCodeArz int,
	@fldNerkheArz decimal(18,4),
	@language nvarchar(10),
	@typeNetwork nvarchar(10),
	@flagForoosh nvarchar(10),
	@MultiFee bigint,
	@fldCodeTafsili bigint,

	@fldCodeVasete nvarchar(10),
	@lngMablaghPorsant [decimal](18, 4),

	@fldCodeSuperVizor nvarchar(10),
	@lngMablaghPorsantSuperVizor [decimal](18, 4),

    @fldShomareFactor bigint,
    @fldShomareSanad bigint,
    @fldDate bigint,
	@fldSharhFactor nvarchar(250),
	@dtForoosh [tpRizeForooshNewNewNew-Multi]  readonly,
	@dtCheckDaryafti [tblCheckDaryaftiNewer-Multi] readonly,
	@lngHazineHamlVaNaghl [decimal](18, 4),
	@lngArzeshAfzode [decimal](18, 4),
	@lngMablaghKolFactor [decimal](18, 4),
	@lngMablaghKolCheckHa [decimal](18, 4),
	@intHasRowCheckDaryafti int,
	@lngMablaghNaghdDaryaft [decimal](18, 4),
    @lngMablaghBankDaryaft [decimal](18, 4),
	@lngMablaghTakhfifDaryaft [decimal](18, 4),
	@lngCodeSandoghDaryaft bigint,
	@lngCodeBankDaryaft bigint,
	@ShomarePeigiri nvarchar(50),
	--شرح دریافت
	@strSharhNaghd nvarchar(50), 
	@strSharhBank nvarchar(50),
	@strSharhCheck nvarchar(50),
	@strSharhTakhfif nvarchar(50),
	@strSharhKoli nvarchar(50),

	@ShomarepishFactor bigint,

	@strResult nvarchar(25) output,
	@strResult2 nvarchar(25) output,
	@flagInsert nvarchar(10)
AS

create table #tmpColor (
	MoinKamel nvarchar(50) COLLATE Persian_100_CI_AS,
	fldColorBank int,
	fldColorSandoogh int , 
	fldColorSooratHesab int

)

BEGIN

	begin transaction AddRecord
		begin try
			declare @intRadif int,@CodeMoinHesabhayeDaryaftani nvarchar(100),@CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@TooleKol bigint,@Toolemoin bigint, @lngsum [decimal](18, 4)
			declare @day nvarchar(4),@Month nvarchar(4),@year nvarchar(5),@ShomareDaryaft bigint,@yearFull nvarchar(5),@dateSmall bigint
			declare @CodeMoinHoghoogh nvarchar(10),@CodeMoinHesabhayePardakhtani nvarchar(10), @SharhePoorsant nvarchar(50)

			--set @year=substring(convert(nvarchar(20),@fldDate),3,2)
			set @yearFull=substring(convert(nvarchar(20),@fldDate),0,5)
			set @year=@yearFull
			set @month=substring(convert(nvarchar(20),@fldDate),5,2)
			set @day=substring(convert(nvarchar(20),@fldDate),7,2)
			--set @dateSmall=substring(convert(nvarchar(15),@fldDate),3,6)
			set @dateSmall=@fldDate
			declare @lngSumDaryaft [decimal](18, 4)
			set @intRadif=0
		
			if @fldSharhFactor=''
					set @fldSharhFactor='none'


					--######برای اصلاح سند حسابداری مریوط به پورانتهاییه که اشتباها کدنوعشون 1 بوده
					UPDATE       tblSodooreSanad
					SET                fldCodeNoeTafzili = '0'
					WHERE        (fldMoinKamel = '31303') and fldCodeNoeTafzili='1'

					UPDATE       tblSodooreSanad
					SET                fldCodeNoeTafzili = '0'
					WHERE        (fldMoinKamel = '11301') and fldCodeNoeTafzili='1'


					UPDATE       tblSodooreSanad
					SET                fldCodeNoeTafzili = '0'
					WHERE        (fldMoinKamel = '71301') and fldCodeNoeTafzili='1'
					--####################################
			set @SharhePoorsant=(Select * from dbo.translatefunction(@language,18268)) + Convert(varchar,@fldShomareFactor)
			if @flagForoosh='arshive'
				begin
					--arishive
					--update(delete and insert)
					--delete
					delete from tblRizeForoosh
						where fldShomareSanad=@fldShomareSanad

					delete from tblForoosh
						where fldShomareSanad=@fldShomareSanad

					insert into #tmpColor
								(MoinKamel,fldColorBank ,fldColorSandoogh , fldColorSooratHesab )
								Select fldMoinKamel,fldColorBank,fldColorSandoogh,fldColorSooratHesab from tblSodooreSanad Where fldSanad=@fldShomareSanad

					delete from tblSodooreSanad
						where fldSanad=@fldShomareSanad

					set @ShomareDaryaft=(Select fldShomareDaryaft from tblDaryaft Where fldShomareSanad= @fldShomareSanad)
					delete from tblDaryaft
						where fldShomareSanad=@fldShomareSanad

					delete from tblCheckeDaryafti
						where fldSanad=@fldShomareSanad and (fldShomareVosool  is  null and fldShomarePardakht  is  null) and fldVaziat is  null
						--where fldSanad=@fldShomareSanad  

					--insert
							set @intRadif=@intRadif+1
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							--چون در حالت ویرایش میباشد از لست استفاده کردم
							
							execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolFactor,0,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							
							if @lngHazineHamlVaNaghl>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'hamlvanaghl' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngHazineHamlVaNaghl,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end
							
							if @lngArzeshAfzode>0
							begin 
								set @intRadif=@intRadif+1
								execute codemoinhesab 'arzesheafzoode' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngArzeshAfzode,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end 

							set @intRadif=@intRadif+1
							execute codemoinhesab 'foroosh' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
							set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngsum,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							--بررسی شرح های دریافت
							if (@strSharhNaghd is null or @strSharhNaghd='')
								begin
									set @strSharhNaghd='noneDaryaft'
								end 
							if (@strSharhBank is null or @strSharhBank='')
								begin
									set @strSharhBank='noneDaryaft'
								end 
							if (@strSharhCheck is null or @strSharhCheck='')
								begin
									set @strSharhCheck='noneDaryaft'
								end 
							if (@strSharhTakhfif is null or @strSharhTakhfif='')
								begin
									set @strSharhTakhfif='noneDaryaft'
								end 
							--شرح دریافت اصلی فروش
							if (@strSharhKoli is null or @strSharhKoli='')
								begin
									set @strSharhKoli='noneDaryaft'
								end 
	
							--پایان بررسی شرح های دریافت

							if @lngMablaghNaghdDaryaft>0
								begin
								--صندوق
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeSandoghDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghNaghdDaryaft,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 
							if @lngMablaghBankDaryaft>0
								begin
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeBankDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghBankDaryaft,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad									
									
									
									
									if isnumeric(@ShomarePeigiri)=1 
										update tblSodooreSanad set fldShomarepeigiri=@ShomarePeigiri where fldMoinKamel= @CodeMoinHesabhayeDaryaftani And fldSanad=@fldShomareSanad
								end 
							if @intHasRowCheckDaryafti>0
								begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'check' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
					
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolCheckHa,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

							if @lngMablaghTakhfifDaryaft>0
								begin
									set @intRadif=@intRadif+1

									execute codemoinhesab 'takhfifatedaryaft' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

								--ح س دریافتنی
							
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							--declare @lngSumDaryaft bigint
							set @lngSumDaryaft=(@lngMablaghTakhfifDaryaft+@lngMablaghBankDaryaft+@lngMablaghNaghdDaryaft+@lngMablaghKolCheckHa)
							if(@lngSumDaryaft>0)
							begin
								set @intRadif=@intRadif+1
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngSumDaryaft,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad 
							end 
							--سند پورسانت واسطه ها و ویزیتور ها
							--if @lngMablaghPorsant>0
							
							
							if isnumeric(@fldCodeVasete )=1  and Convert(decimal(18, 4),@fldCodeVasete)>0
								begin									
									set @intRadif=@intRadif+1									

									execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
									set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									if @lngMablaghPorsant>0 
										begin																						
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeVasete,'0',@lngMablaghPorsant,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeVasete,'0',0,@lngMablaghPorsant,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad
										end

									--### سند سوپروایزر####
									if @lngMablaghPorsantSuperVizor>0
										begin								
											set @intRadif=@intRadif+1
									
											execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
											set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
												
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeSuperVizor,'0',@lngMablaghPorsantSuperVizor,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeSuperVizor,'0',0,@lngMablaghPorsantSuperVizor,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad
										end 
									
								end 

							

								--پایان سند پورسانت واسطه ها و ویزیتور ها 

								if @lngSumDaryaft>0
								begin
									--set @ShomareDaryaft=(Select fldShomareDaryaft from tblDaryaft Where fldShomareSanad= @fldShomareSanad)
									--INSERT     INTO            tblTest7(fld1, fld3)
									--VALUES     (@fldShomareSanad,@ShomareDaryaft)
									if @ShomareDaryaft is null
										begin
											--INSERT     INTO            tblTest7( fld3)
											--	VALUES     (0)
											set @ShomareDaryaft=(select isnull(max(fldShomareDaryaft),0) as fldMax from tblDaryaft)										
											set @ShomareDaryaft=@ShomareDaryaft+1
										end 
									--ثبت دریافت اصلی فروش
									INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
													VALUES        (@ShomareDaryaft,@fldShomareSanad,@fldCodeTafsili,'foroosh')

									--ثبت ریز اطلاعات چک	
									Insert into  tblCheckeDaryafti (fldcodearz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldSharheDaryaft,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
											Select @fldcodearz,@fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldSharheDaryaft,fldMablagh,@fldCodeTafsili,@fldDate,@ShomareDaryaft,@fldCodeVasete,@fldShomareSanad output from @dtCheckDaryafti;	

									delete tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft and  fldvaziat is null and fldShomareCheck in (Select fldShomareCheck from tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft and fldVaziat is not null)
								end 

							
							INSERT INTO tblForoosh
							   ([fldShomareFactorForoosh]
							   ,[fldShomareSanad]
							   ,[fldDate]
							   ,[fldSharhFactor]
							   ,[fldShomarepishFactor]
							   ,[fldCodeFeeDynamic]
							   
							   )
								 values(@fldShomareFactor,@fldShomareSanad,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end ,@ShomarepishFactor,@MultiFee)
								 
							 INSERT INTO tblRizeForoosh
						   ([fldShomareSanad]
						   ,[fldShomareFactor]
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime]
						   ,[fldEshantion]
						   ,[fldDateEngheza])
							select @fldShomareSanad
						   ,@fldShomareFactor
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime]
						   ,[fldEshantion]
						   ,[fldDateEngheza] output  from @dtForoosh;






					set @strResult='update'
					set @strResult2='update'
					
					--commit transaction AddRecord;
					--return
				end
				--تا اینجا تفصیلی ها به روز  شدند
			else if @flagForoosh='insert'
				begin
				--insert
						
					declare @resultExist bigint
					execute ExistFactorForoosh @fldShomareFactor,@resultExist output
					declare @maxFactor bigint
					execute GetMaxShomareFactorForoosh @maxFactor output
					set @maxFactor=@maxFactor+1
						
						
					if(@resultExist>0)
						begin
						--کاربر دیگری در همین لحظه شماره فاکتور را گرفت
							if @typeNetwork='yes'
								begin
									set @fldShomareFactor=@maxFactor
									set @resultExist = 0 
								end
							else
								begin
									set @strResult=convert(nvarchar(17),@maxFactor)
									set @strResult2='New_Enter'
									rollback transaction AddRecord;
									return
								end
							--در قسمت ویندوزی به کاربر کادری را نشان دهد که max+1 می باشد
							--yes
								--insert
							--no
								--inputbox در برنامه ویندوز که کاربر خودش شماره فاکتور را مشخص کند
								--باید اینجا چکار کنم؟؟؟؟
						end 
					--ehsan added
					if(@resultExist=0 and @flagInsert='yes')
					 --زمانی که کاربر از کادر پیام رویه دکمه بله کلیک میکنه
					     begin
						
							set @intRadif=@intRadif+1
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							execute InsertSanadFirstMultiArz @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolFactor,0,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad output 
								
							if @lngHazineHamlVaNaghl>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'hamlvanaghl' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngHazineHamlVaNaghl,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end 
							if @lngArzeshAfzode>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'arzesheafzoode' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngArzeshAfzode,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end 

							set @intRadif=@intRadif+1
							execute codemoinhesab 'foroosh' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
							set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							
							execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngsum,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							--بررسی شرح های دریافت
							if (@strSharhNaghd is null or @strSharhNaghd='')
								begin
									set @strSharhNaghd='noneDaryaft'
								end 
							if (@strSharhBank is null or @strSharhBank='')
								begin
									set @strSharhBank='noneDaryaft'
								end 
							if (@strSharhCheck is null or @strSharhCheck='')
								begin
									set @strSharhCheck='noneDaryaft'
								end 
							if (@strSharhTakhfif is null or  @strSharhTakhfif='')
								begin
									set @strSharhTakhfif='noneDaryaft'
								end 
							--شرح دریافت اصلی فروش
							if (@strSharhKoli is null or @strSharhKoli='')
								begin
									set @strSharhKoli='none'
								end 
	
							--پایان بررسی شرح های دریافت

							if @lngMablaghNaghdDaryaft>0
								begin
								--صندوق
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeSandoghDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghNaghdDaryaft,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 
							if @lngMablaghBankDaryaft>0
								begin
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeBankDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghBankDaryaft,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad

									
									if isnumeric(@ShomarePeigiri)=1 
										update tblSodooreSanad set fldShomarepeigiri=@ShomarePeigiri where fldMoinKamel= @CodeMoinHesabhayeDaryaftani And fldSanad=@fldShomareSanad
								end 

							if @intHasRowCheckDaryafti>0
								begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'check' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
					
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolCheckHa,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

							if @lngMablaghTakhfifDaryaft>0
								begin
									set @intRadif=@intRadif+1

									execute codemoinhesab 'takhfifatedaryaft' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

								--ح س دریافتنی
							
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							--declare @lngSumDaryaft bigint
							set @lngSumDaryaft=(@lngMablaghTakhfifDaryaft+@lngMablaghBankDaryaft+@lngMablaghNaghdDaryaft+@lngMablaghKolCheckHa)
							if @lngSumDaryaft>0
							begin
								set @intRadif=@intRadif+1
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngSumDaryaft,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad 
							end

							--سند پورسانت واسطه ها و ویزیتور ها
							--if @lngMablaghPorsant>0
							--	begin									
							--		set @intRadif=@intRadif+1
									
							--		execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
							--		set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
							--		set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
			
							--		--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							--		execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeVasete,'1',@lngMablaghPorsant,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh','',@fldShomareSanad


							--		set @intRadif=@intRadif+1									
							--		execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output
							--		set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
							--		set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
							--		--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							--		execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeVasete,'1',0,@lngMablaghPorsant,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh','',@fldShomareSanad
									
							--	end 

							----### سند سوپروایزر####
							--if @lngMablaghPorsantSuperVizor>0
							--	begin								
							--		set @intRadif=@intRadif+1
									
							--		execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
							--		set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
							--		set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
												
							--		execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeSuperVizor,'1',@lngMablaghPorsantSuperVizor,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh','',@fldShomareSanad


							--		set @intRadif=@intRadif+1									
							--		execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output
							--		set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
							--		set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
							--		--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							--		execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeSuperVizor,'1',0,@lngMablaghPorsantSuperVizor,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh','',@fldShomareSanad
							--	end 

							if isnumeric(@fldCodeVasete )=1  and Convert(decimal(18, 4),@fldCodeVasete)>0
								begin									
									set @intRadif=@intRadif+1
									
									execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
									set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									if @lngMablaghPorsant>0 
										begin
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeVasete,'0',@lngMablaghPorsant,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeVasete,'0',0,@lngMablaghPorsant,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad
										end

									--### سند سوپروایزر####
									if @lngMablaghPorsantSuperVizor>0
										begin								
											set @intRadif=@intRadif+1
									
											execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
											set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)

											declare @SharhePoorsant2 nvarchar(50)
											set @SharhePoorsant2='پورسانت فاکتور' + Convert(varchar,@fldShomareFactor)
												
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeSuperVizor,'0',@lngMablaghPorsantSuperVizor,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeSuperVizor,'0',0,@lngMablaghPorsantSuperVizor,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad
										end 
									
								end 

								--پایان سند پورسانت واسطه ها و ویزیتور ها 

								if @lngSumDaryaft>0
								begin
									set @ShomareDaryaft=(select isnull(max(fldShomareDaryaft),0) as fldMax from tblDaryaft)
									set @ShomareDaryaft=@ShomareDaryaft+1
									--ثبت دریافت اصلی فروش
									INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
													VALUES        (@ShomareDaryaft,@fldShomareSanad,@fldCodeTafsili,'foroosh')

									--ثبت ریز اطلاعات چک	
									Insert into  tblCheckeDaryafti (fldcodearz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldSharheDaryaft,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
											Select @fldcodearz,@fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldSharheDaryaft,fldMablagh,@fldCodeTafsili,@fldDate,@ShomareDaryaft,@fldCodeVasete,@fldShomareSanad output from @dtCheckDaryafti;	
								end
							INSERT INTO tblForoosh
							   ([fldShomareFactorForoosh]
							   ,[fldShomareSanad]
							   ,[fldDate]
							   ,[fldSharhFactor]
							   ,[fldShomarepishFactor]
							   ,[fldCodeFeeDynamic]
							   
							   )
								 values(@maxFactor,@fldShomareSanad,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end,@ShomarepishFactor,@MultiFee )

							 INSERT INTO tblRizeForoosh
						   ([fldShomareSanad]
						   ,[fldShomareFactor]
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime]
						   ,[fldEshantion]
						   ,[fldDateEngheza])
							select @fldShomareSanad
						   ,@maxFactor
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime]
						   ,[fldEshantion]
						   ,[fldDateEngheza] output  from @dtForoosh;
							set @strResult=convert(nvarchar(17),@maxFactor)
							set @strResult2='insert'
							--commit transaction AddRecord;
						end
					else if(@resultExist=0 and @flagInsert='no')
						 begin
						
							set @intRadif=@intRadif+1
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							execute InsertSanadFirstMultiArz @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolFactor,0,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad output 
							if @lngHazineHamlVaNaghl>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'hamlvanaghl' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngHazineHamlVaNaghl,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end
							if @lngArzeshAfzode>0
							begin
								set @intRadif=@intRadif+1
								execute codemoinhesab 'arzesheafzoode' ,@CodeMoinHesabhayeDaryaftani output
								set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
								set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,0,@lngArzeshAfzode,@intRadif,@dateSmall,'none',@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							end 

							set @intRadif=@intRadif+1
							execute codemoinhesab 'foroosh' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
							set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngsum,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
							--بررسی شرح های دریافت
							if (@strSharhNaghd is null or @strSharhNaghd='')
								begin
									set @strSharhNaghd='noneDaryaft'
								end 
							if (@strSharhBank is null or @strSharhBank='')
								begin
									set @strSharhBank='noneDaryaft'
								end 
							if (@strSharhCheck is null or @strSharhCheck='')
								begin
									set @strSharhCheck='noneDaryaft'
								end 
							if (@strSharhTakhfif is null or @strSharhTakhfif='')
								begin
									set @strSharhTakhfif='noneDaryaft'
								end 
							--شرح دریافت اصلی فروش
							if (@strSharhKoli is null or @strSharhKoli='')
								begin
									set @strSharhKoli='noneDaryaft'
								end 
	
							--پایان بررسی شرح های دریافت

							if @lngMablaghNaghdDaryaft>0
								begin
								--صندوق
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeSandoghDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghNaghdDaryaft,0,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 
							if @lngMablaghBankDaryaft>0
								begin
									set @intRadif=@intRadif+1
									set @CodeMoinHesabhayeDaryaftani=@lngCodeBankDaryaft
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghBankDaryaft,0,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								
									
									if isnumeric(@ShomarePeigiri)=1 
										update tblSodooreSanad set fldShomarepeigiri=@ShomarePeigiri where fldMoinKamel= @CodeMoinHesabhayeDaryaftani And fldSanad=@fldShomareSanad
								end 

							if @intHasRowCheckDaryafti>0
								begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'check' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
					
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',@lngMablaghKolCheckHa,0,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

							if @lngMablaghTakhfifDaryaft>0
								begin
									set @intRadif=@intRadif+1

									execute codemoinhesab 'takhfifatedaryaft' ,@CodeMoinHesabhayeDaryaftani output
									set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,null,null,@lngMablaghTakhfifDaryaft,0,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad
								end 

								--ح س دریافتنی
							
							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
							set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
							set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
							
							set @lngSumDaryaft=(@lngMablaghTakhfifDaryaft+@lngMablaghBankDaryaft+@lngMablaghNaghdDaryaft+@lngMablaghKolCheckHa)
							if @lngSumDaryaft>0
							begin
								set @intRadif=@intRadif+1
								execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafsili,'1',0,@lngSumDaryaft,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'foroosh',@fldCodeVasete,@fldShomareSanad 
							end 



							--سند پورسانت واسطه ها و ویزیتور ها
							--if @lngMablaghPorsant>0

							--	begin									
							--		set @intRadif=@intRadif+1
									
							--		execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
							--		set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
							--		set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
			
							--		--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							--		execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeVasete,'1',@lngMablaghPorsant,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh','',@fldShomareSanad


							--		set @intRadif=@intRadif+1									
							--		execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output
							--		set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
							--		set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
							--		--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							--		execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeVasete,'1',0,@lngMablaghPorsant,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh','',@fldShomareSanad
									
							--	end 

							----### سند سوپروایزر####
							--if @lngMablaghPorsantSuperVizor>0
							--	begin								
							--		set @intRadif=@intRadif+1
									
							--		execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
							--		set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
							--		set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
												
							--		execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeSuperVizor,'1',@lngMablaghPorsantSuperVizor,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh','',@fldShomareSanad


							--		set @intRadif=@intRadif+1									
							--		execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output
							--		set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
							--		set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
							--		--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
							--		execute InsertSanadLast @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeSuperVizor,'1',0,@lngMablaghPorsantSuperVizor,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'foroosh','',@fldShomareSanad
							--	end 
							if isnumeric(@fldCodeVasete )=1  and Convert(decimal(18, 4),@fldCodeVasete)>0
								begin									
									set @intRadif=@intRadif+1
									
									execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
									set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									if @lngMablaghPorsant>0 
										begin
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeVasete,'1',@lngMablaghPorsant,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeVasete,'0',0,@lngMablaghPorsant,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad
										end

									--### سند سوپروایزر####
									if @lngMablaghPorsantSuperVizor>0
										begin								
											set @intRadif=@intRadif+1
									
											execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
											set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
												
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeSuperVizor,'0',@lngMablaghPorsantSuperVizor,0,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeSuperVizor,'0',0,@lngMablaghPorsantSuperVizor,@intRadif,@dateSmall,@SharhePoorsant,@day,@month,@year,'foroosh','',@fldShomareSanad
										end 
									
								end 



								--پایان سند پورسانت واسطه ها و ویزیتور ها 
								if @lngSumDaryaft>0
								begin
									set @ShomareDaryaft=(select isnull(max(fldShomareDaryaft),0) as fldMax from tblDaryaft)
									set @ShomareDaryaft=@ShomareDaryaft+1
									--ثبت دریافت اصلی فروش
									INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
													VALUES        (@ShomareDaryaft,@fldShomareSanad,@fldCodeTafsili,'foroosh')

									--ثبت ریز اطلاعات چک	
									Insert into  tblCheckeDaryafti (fldcodearz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldSharheDaryaft,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
											Select @fldcodearz,@fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldSharheDaryaft,fldMablagh,@fldCodeTafsili,@fldDate,@ShomareDaryaft,@fldCodeVasete,@fldShomareSanad output from @dtCheckDaryafti;	
								end
							INSERT INTO tblForoosh
							   ([fldShomareFactorForoosh]
							   ,[fldShomareSanad]
							   ,[fldDate]
							   ,[fldSharhFactor]
							   ,[fldShomarepishFactor]
							   ,[fldCodeFeeDynamic]
							   
							   )
								 values(@fldShomareFactor,@fldShomareSanad,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end,@ShomarepishFactor,@MultiFee )

							 INSERT INTO tblRizeForoosh
													   ([fldShomareSanad]
													   ,[fldShomareFactor]
													   ,[fldCodeVasete]
													   ,[fldCodeTafsili]
													   ,[fldCodeUser]
													   ,[fldRadif]
													   ,[fldCodeAnbar]
													   ,[fldCodeKala]
													   ,[fldSharhKala]
													   ,[fldTedadCarton]
													   ,[fldTedadJoz]
													   ,[fldFeeJoz]
													   ,[fldFeeYekCarton]
													   ,[fldFeeKol]
													   ,[fldDarsadTakhfif]
													   ,[fldMablaghTakhfif]
													   ,[fldFeeVahed1BadAzTakhfif]
													   ,[fldFeeVahed2BadAzTakhfif]
													   ,[fldArzeshAfzode]
													   ,[fldFeeKolBadAzTakhfif]
													   ,[fldMablaghKhales]
													   ,[fldType]
													   ,[fldDate]
													   ,[fldTime]
													   ,[fldEshantion]
													   ,[fldDateEngheza])
														select @fldShomareSanad
														--edit
													   ,@fldShomareFactor
													   ,[fldCodeVasete]
													   ,[fldCodeTafsili]
													   ,[fldCodeUser]
													   ,[fldRadif]
													   ,[fldCodeAnbar]
													   ,[fldCodeKala]
													   ,[fldSharhKala]
													   ,[fldTedadCarton]
													   ,[fldTedadJoz]
													   ,[fldFeeJoz]
													   ,[fldFeeYekCarton]
													   ,[fldFeeKol]
													   ,[fldDarsadTakhfif]
													   ,[fldMablaghTakhfif]
													   ,[fldFeeVahed1BadAzTakhfif]
													   ,[fldFeeVahed2BadAzTakhfif]
													   ,[fldArzeshAfzode]
													   ,[fldFeeKolBadAzTakhfif]
													   ,[fldMablaghKhales]
													   ,[fldType]
													   ,[fldDate]
													   ,[fldTime]
													   ,[fldEshantion]
													   ,[fldDateEngheza] output  from @dtForoosh;
							set @strResult=convert(nvarchar(17),@fldShomareFactor)
							set @strResult2='insertByNo'
							--commit transaction AddRecord
						end
						
				end
			--set @strResult='errorx'
			--set @strResult2='errorx'
			--update sharh Ha
			--@fldShomarefactor dar khat zir bayad avaz beshe (shayad =>@fldShomarefactorNew )
			update tblSodooreSanad set fldColorBank=#tmpColor.fldColorBank,fldColorSandoogh=#tmpColor.fldColorSandoogh,fldColorSooratHesab=#tmpColor.fldColorSooratHesab from #tmpColor Where tblSodooreSanad.fldSanad=@fldShomareSanad and fldMoinKamel=#tmpColor.MoinKamel

			 if @flagInsert='yes'
				begin
					update  tblSodooreSanad
							--set fldSharh ='فاکتور شماره ' + convert(nvarchar(17),@maxFactor)
							set fldSharh ='' 
							where fldSanad=@fldShomareSanad and fldSharh like 'none'
					update  tblSodooreSanad
							--set fldSharh ='فاکتور شماره ' + convert(nvarchar(17),@maxFactor) + ' - شماره دریافت ' +  convert(nvarchar(17),@ShomareDaryaft) 
							set fldSharh ='' 
							where fldSanad=@fldShomareSanad and fldSharh like 'noneDaryaft'
				end
			else
				begin
					 update  tblSodooreSanad
							--set fldSharh ='فاکتور شماره ' + convert(nvarchar(17),@fldShomarefactor)
							set fldSharh =''
							where fldSanad=@fldShomareSanad and fldSharh like 'none'

					update  tblSodooreSanad
							--set fldSharh ='فاکتور شماره ' + convert(nvarchar(17),@fldShomarefactor) + ' - شماره دریافت ' +  convert(nvarchar(17),@ShomareDaryaft) 
							set fldSharh =''
							where fldSanad=@fldShomareSanad and fldSharh like 'noneDaryaft'
				end


				commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				set @strResult2='error'
				rollback transaction AddRecord;
			end
		end catch
END


GO
/****** Object:  StoredProcedure [dbo].[InsertInfoInDaramad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertInfoInDaramad]
--پارامترهای کلی
@fldCodeArz int,
@fldNerkheArz decimal(18,4),
@InsertType nvarchar(10),
@DateDaryaft int,
@CodeTafzili nvarchar(10),
@ShomareDaryaft bigint,
@ShomareSanadKarbar nvarchar(50),
@SharheKol nvarchar(200),
@CodeMoinDaramad nvarchar(10),
@day nvarchar(50),
@month nvarchar(50),
@year nvarchar(50),
@TypeDaryaft nvarchar(10),
@CodeNoeTafzili nvarchar(10),

--پارامترهای صندوق
@CodeKolSandoogh nvarchar(10),
@CodeMoinSandoogh nvarchar(10),
@Naghd decimal(18,4),
@SNaghd nvarchar(200),

--'پارامترهای بانک
@CodeKolBank nvarchar(10),
@CodeMoinBank nvarchar(10),
@Bank decimal(18,4),
@SBank nvarchar(200),

--'پارامترهای چک
@tblCheck [tblCheckDaryafti-Multi] readonly,
@CodeKolCheck nvarchar(10),
@CodeMoinCheck nvarchar(10),
@MablaghCheck decimal(18,4),
@SCheck nvarchar(200),

--پارامترهای تخفیفات
@CodeKoltakhfifat nvarchar(10),
@CodeMoinTakhfifateDaryaft nvarchar(10),
@Takhfif decimal(18,4),
@STakhfif nvarchar(200),


--پارامترهای واسطه
@CodeVasete nvarchar(10),

@lngCodeUser bigint,
@lngDateNow bigint,



@NoeKhata nvarchar(30) output 


--@NoeKhata int output
as
create table #tmpColor (
	MoinKamel nvarchar(50) COLLATE Persian_100_CI_AS,
	fldColorBank int,
	fldColorSandoogh int , 
	fldColorSooratHesab int

)

begin
	begin transaction transact
		begin try
			declare @MaxSanad bigint,@lngI bigint,@lngCount bigint,@strShomareSanad nvarchar(50),@lngCountDaryaft int,@IDOutPut bigint,@decJam decimal(18,4)	,@strNameUser nvarchar(25),@strTime nvarchar(30)
			declare @TooleKol bigint,@TooleMoin bigint,@ShomareSanadArchive nvarchar(50),@DateKamel int
	 
			 execute  ToolePaye 'kol',@TooleKol output
			 execute  ToolePaye 'moin',@Toolemoin output

			 execute  Date9raghami @datedaryaft,@datekamel output

			 set @lngCountDaryaft=(Select Count(fldShomareDaryaft) from tblDaryaft Where fldShomareDaryaft=@ShomareDaryaft);

			 set @decjam=@naghd+@bank +@MablaghCheck+ @Takhfif ;

			 set @strNameUser=(Select fldUser from tblSecurity Where fldCode=@lngCodeUser)

			 set @strtime=(SELECT ltrim(right(convert(varchar(25), getdate(), 100), 7)))

			 if @lngCountDaryaft>0 
				begin
					if @InsertType='archive'  --برای حالتی که میخاد بروزرسانی کنه
						begin
							--Insert into tblTest5(name1,name2)
							--	values ('a','a')
							--declare @ShomareSanadArchive nvarchar(50)

							set @ShomareSanadArchive=(select fldShomareSanad from tblDaryaft where fldShomareDaryaft=@shomaredaryaft)
							--Insert into tblTest7(fld2)
							--	values (@ShomareSanadArchive)
							insert into #tmpColor
								(MoinKamel,fldColorBank ,fldColorSandoogh , fldColorSooratHesab )
								Select fldMoinKamel,fldColorBank,fldColorSandoogh,fldColorSooratHesab from tblSodooreSanad Where fldSanad=@ShomareSanadArchive

							delete from tblSodooreSanad Where fldSanad=@ShomareSanadArchive
							delete from tblCheckeDaryafti where fldSanad=@ShomareSanadArchive  and fldVaziat is null
							delete from tblDaryaft where fldShomareSanad=@ShomareSanadArchive
							set @noekhata='update_archive'
							--set @noekhata=3

							INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
								VALUES        (@lngCodeUser,@strNameUser,'درآمد','ویرایش',@lngDateNow,@strtime,@shomaredaryaft,@ShomareSanadArchive)

						end
					else
						--set @NoeKhata='error_tekrari_insert'
						--return
					--else
						begin
							if @InsertType='insert' -- برای حالتی که جدید میخاد ثبت کنه ولی یکم قبلش یک کاربر دیگه ثبت میکنه
								begin
									--Insert into tblTest7(fld1)
									--	values ('er in')
									set @NoeKhata='error_tekrari_insert'
									rollback transaction 
									return
									--set @NoeKhata=2
								end
						end
				end
			--else -- برای حالتی که شماره دریافت جدیده 
			--	begin
					declare @CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@intRadif int,@strRadif nvarchar(50),@strMoinKamel nvarchar(12)
					declare @ResultSanad nvarchar(10)
					--set @CodeKolHesabDaryaftani= substring('11301',2,2)
					--insert into tblTest7 (fld2,fld3)
					--	values(@TooleKol,@Toolemoin)


					if  @CodeTafzili=0
					begin
						set @CodeTafzili=''
						set @CodeNoeTafzili=''
					end
			
					set @CodeKolkham=left(@CodeMoinDaramad,@TooleKol)	
					set @CodeMoinKham=substring(@CodeMoinDaramad,@TooleKol+1,@Toolemoin)
			
					set @intRadif=0;

					--سند نقد
			
					execute IsArchiveShomareSanad @ShomareSanadKarbar,@ResultSanad output

					--Insert into tblTest7(fld1)
					--			values (@ResultSanad)

					if @naghd>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinSandoogh,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKolSandoogh + @CodeMoinSandoogh
							
							
							if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
								begin
									execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz ,   @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,@Naghd,0,@strRadif,@DateDaryaft,@SNaghd,@day,@month,@year,'daramad',@CodeVasete,@strShomareSanad output	;
								end
							else	
								begin							
								  execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,@Naghd,0,@strRadif,@DateDaryaft,@SNaghd,@day,@month,@year,'daramad',@CodeVasete,@ShomareSanadKarbar;
								  set @strShomareSanad=@ShomareSanadKarbar
								end
							--insert into tblTest7 (fld1)
								--values(@strShomareSanad)
						end
					--سند بانک
					if @Bank>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKolBank + @CodeMoinBank
					
							--insert into tblTest7 (fld1)
							--	values(@CodeKolBank + @CodeMoinBank)
							--execute insertSanadFirst  @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@Bank,0,@strRadif,@DateDaryaft,@SBank,'daramad',@strShomareSanad output	;
							if @intRadif=1 
								begin
									if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
										execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@Bank,0,@strRadif,@DateDaryaft,@SBank,@day,@month,@year,'daramad',@CodeVasete,@strShomareSanad output	;
									else
										begin
											execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@Bank,0,@strRadif,@DateDaryaft,@SBank,@day,@month,@year,'daramad',@CodeVasete,@ShomareSanadKarbar;
											set @strShomareSanad=@ShomareSanadKarbar
										end
								end
							else
								begin
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@Bank,0,@strRadif,@DateDaryaft,@SBank,@day,@month,@year,'daramad',@CodeVasete,@strShomareSanad;
								end
						end
					--سند چک
					if @MablaghCheck>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck
							if @intRadif=1
								begin
									if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
										begin
											execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DateDaryaft,@SCheck,@day,@month,@year,'daramad',@CodeVasete,@strShomareSanad output;
										end
									else
										begin
											execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DateDaryaft,@SCheck,@day,@month,@year,'daramad',@CodeVasete,@ShomareSanadKarbar;
											set @strShomareSanad=@ShomareSanadKarbar
										end
								end
							else
								begin
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DateDaryaft,@SCheck,@day,@month,@year,'daramad',@CodeVasete,@strShomareSanad;
								end
							--execute insertSanadFirst  @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DateDaryaft,@SCheck,'daramad',@strShomareSanad output	;
						end
					--سند تخفیفات
					if @Takhfif>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinTakhfifateDaryaft,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifateDaryaft

							if @intRadif=1 
								begin
									if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
										begin
											execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKoltakhfifat,@CodeMoinTakhfifateDaryaft,@strMoinKamel,NUll,NULL,@Takhfif,0,@strRadif,@DateDaryaft,@STakhfif,@day,@month,@year,'daramad',@CodeVasete,@strShomareSanad output;
										end
									else
										begin
											execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKoltakhfifat,@CodeMoinTakhfifateDaryaft,@strMoinKamel,NUll,NULL,@Takhfif,0,@strRadif,@DateDaryaft,@STakhfif,@day,@month,@year,'daramad',@CodeVasete,@ShomareSanadKarbar;										
											set @strShomareSanad=@ShomareSanadKarbar
										end
								end
							else
								begin
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKoltakhfifat,@CodeMoinTakhfifateDaryaft,@strMoinKamel,NUll,NULL,@Takhfif,0,@strRadif,@DateDaryaft,@STakhfif,@day,@month,@year,'daramad',@CodeVasete,@strShomareSanad;
								end
							--execute insertSanadFirst  @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DateDaryaft,@SCheck,'daramad',@strShomareSanad output	;
						end
					--سند بستانکاری
					set @intRadif =@intRadif+1			
					set @strRadif=convert(nvarchar(50),@intRadif)
					set @CodeKolkham =substring(@CodeMoinDaramad, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinDaramad,@TooleKol+1,@Toolemoin)
					if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
						begin
							execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKolkham,@CodeMoinKham ,@CodeMoinDaramad,@CodeTafzili,@CodeNoeTafzili,0,@decjam,@strRadif,@DateDaryaft,@SharheKol,@day,@month,@year,'daramad',@CodeVasete,@strShomareSanad;
							if @InsertType<>'archive'								
								INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
									VALUES        (@lngCodeUser,@strNameUser,'درآمد','جدید',@lngDateNow,@strtime,@shomaredaryaft,@strShomareSanad)
						end
					else
						begin
							execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKolkham,@CodeMoinKham ,@CodeMoinDaramad,@CodeTafzili,@CodeNoeTafzili,0,@decjam,@strRadif,@DateDaryaft,@SharheKol,@day,@month,@year,'daramad',@CodeVasete,@ShomareSanadKarbar;
							if @InsertType<>'archive'								
								INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
									VALUES        (@lngCodeUser,@strNameUser,'درآمد','جدید',@lngDateNow,@strtime,@shomaredaryaft,@ShomareSanadKarbar)
						end
					--execute insertSanadLast @CodeKolkham,@CodeMoinKham ,@CodeMoinDaramad,NUll,NUll,@decjam,0,@strRadif,@DateDaryaft,@SharheKol,'daramad';

					update tblSodooreSanad set fldColorBank=#tmpColor.fldColorBank,fldColorSandoogh=#tmpColor.fldColorSandoogh,fldColorSooratHesab=#tmpColor.fldColorSooratHesab from #tmpColor Where tblSodooreSanad.fldSanad=@ShomareSanadArchive and fldMoinKamel=#tmpColor.MoinKamel
					if @Resultsanad='notarchive' 
						begin
							set @strShomareSanad=@ShomareSanadKarbar
						end


					--ثبت ریز اطلاعات چک					
					Insert into  tblCheckeDaryafti (fldCodeArz,fldNerkheArz, fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
						Select @fldCodeArz,@fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@CodeTafzili,@Datekamel,@ShomareDaryaft, @CodeVasete,@strShomareSanad output from @tblCheck;	

					delete tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft and  fldvaziat is null and fldShomareCheck in (Select fldShomareCheck from tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft and fldVaziat is not null)

					--ثبت اطلاعات دریافت
					if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
						begin
							INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
								VALUES        (@ShomareDaryaft,@strShomareSanad,@CodeTafzili,@TypeDaryaft)
						end
					else
						begin
							INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
								VALUES        (@ShomareDaryaft,@ShomareSanadKarbar,@CodeTafzili,@TypeDaryaft)
						end

					set @NoeKhata='Insert_New'
		commit transaction
			end try

		begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='rolback'

		end catch
					--set @NoeKhata=1
				


			--set @MaxSanad=(Select Max(Convert(dec,fldSanad)) from tblSodooreSanad);	
			--set @MaxSanad+=1;
	
			--INSERT INTO tblSodooreSanad
			--                     (fldSanad)
			--	SELECT        MAX(CONVERT(dec, fldSanad)) + 1 AS Expr1
			--	FROM            tblSodooreSanad AS tblSodooreSanad_1
	


	
			--INSERT INTO tblSodooreSanad
		 --                        (fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldType)
			--	SELECT        MAX(CONVERT(dec, fldSanad)) + 1 AS MaxSanad,'111','01','11101',@CodeTafzili,'1',@decjam,0,'1',@DateDaryaft,@SharheKol,'daramad'
			--	FROM            tblSodooreSanad AS tblSodooreSanad_1

	
			--set @IDOutPut=@@IDENTITY;	

			--set @lngShomareSanad=(Select fldSanad from tblSodooreSanad   where id=@IDOutPut)



                         
	

	
	

end

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoInDaryaft]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertInfoInDaryaft]
--چندزبانه
--پارامترهای کلی
@fldCodeArz int,
@fldNerkheArz decimal(18,4),
@language nvarchar(10),
@InsertType nvarchar(10),
@DateDaryaft int,
@CodeTafzili nvarchar(10),
@ShomareDaryaft bigint,
@ShomareSanadKarbar nvarchar(50),
@SharheKol nvarchar(200),
@CodeMoinHesabhayeDaryaftani nvarchar(10),
@day nvarchar(50),
@month nvarchar(50),
@year nvarchar(50),
@TypeDaryaft nvarchar(10),


--پارامترهای صندوق
@CodeKolSandoogh nvarchar(10),
@CodeMoinSandoogh nvarchar(10),
@Naghd decimal(18,4),
@SNaghd nvarchar(200),

--'پارامترهای بانک
@CodeKolBank nvarchar(10),
@CodeMoinBank nvarchar(10),
@Bank decimal(18,4),
@SBank nvarchar(200),
@ShomarePeigiri nvarchar(50),

--'پارامترهای چک
@tblCheck [tblCheckDaryaftiNewer-Multi] readonly,
@CodeKolCheck nvarchar(10),
@CodeMoinCheck nvarchar(10),
@MablaghCheck decimal(18,4),
@SCheck nvarchar(200),

--پارامترهای تخفیفات
@CodeKoltakhfifat nvarchar(10),
@CodeMoinTakhfifateDaryaft nvarchar(10),
@Takhfif decimal(18,4),
@STakhfif nvarchar(200),


--پارامترهای واسطه
@CodeVasete nvarchar(10),
@lngMablaghPorsant decimal(18,4),
@fldCodeSuperVizor nvarchar(10),
@lngMablaghPorsantSuperVizor decimal(18,4),

@lngCodeUser bigint,
@lngDateNow bigint,

--'پارامتر تسویه
@dtTasfie [dtTasfie-Multi] readonly,

--'پارامتر تسویه وام
@dtTasfieVam [dtTasfieVam-Multi] readonly,

@NoeKhata nvarchar(30) output 


--@NoeKhata int output
as

create table #tmpColor (
	MoinKamel nvarchar(50) COLLATE Persian_100_CI_AS,
	fldColorBank int,
	fldColorSandoogh int , 
	fldColorSooratHesab int

)




begin
	begin transaction transact
		begin try
			declare @MaxSanad bigint,@lngI bigint,@lngCount bigint,@strShomareSanad nvarchar(50),@lngCountDaryaft int,@IDOutPut bigint,@decJam decimal(18,4)	,@strNameUser nvarchar(25),@strTime nvarchar(30)
			declare @TooleKol bigint,@TooleMoin bigint,@ShomareSanadArchive nvarchar(50),@DateKamel int
			declare @CodeMoinHoghoogh nvarchar(10),@CodeMoinHesabhayePardakhtani nvarchar(10), @SharhePoorsant nvarchar(100)

			 execute  ToolePaye 'kol',@TooleKol output
			 execute  ToolePaye 'moin',@Toolemoin output

			 execute  Date9raghami @datedaryaft,@datekamel output

			 set @lngCountDaryaft=(Select Count(fldShomareDaryaft) from tblDaryaft Where fldShomareDaryaft=@ShomareDaryaft);

			 set @decjam=@naghd+@bank +@MablaghCheck+ @Takhfif ;

			 set @SharhePoorsant=(Select * from dbo.translatefunction(@LANGUAGE,18269)) + ' ' + Convert(varchar,@ShomareDaryaft)

			 set @strNameUser=(Select fldUser from tblSecurity Where fldCode=@lngCodeUser)

			 set @strtime=(SELECT ltrim(right(convert(varchar(25), getdate(), 100), 7)))

			 if @lngCountDaryaft>0 
				begin
					if @InsertType='archive'  --برای حالتی که میخاد بروزرسانی کنه
						begin
							--Insert into tblTest5(name1,name2)
							--	values ('a','a')
							--declare @ShomareSanadArchive nvarchar(50)

							set @ShomareSanadArchive=(select fldShomareSanad from tblDaryaft where fldShomareDaryaft=@shomaredaryaft)
							--Insert into tblTest7(fld2)
							--	values (@ShomareSanadArchive)
							insert into #tmpColor
								(MoinKamel,fldColorBank ,fldColorSandoogh , fldColorSooratHesab )
								Select fldMoinKamel,fldColorBank,fldColorSandoogh,fldColorSooratHesab from tblSodooreSanad Where fldSanad=@ShomareSanadArchive

							delete from tblSodooreSanad Where fldSanad=@ShomareSanadArchive
							--delete from tblCheckeDaryafti where fldSanad=@ShomareSanadArchive and fldVaziat is null
							--delete from tblCheckeDaryafti
							--	where fldSanad=@ShomareSanadArchive and (fldShomareVosool  is  null and fldShomarePardakht  is  null) and fldVaziat is  null

							delete from tblCheckeDaryafti
								where fldSanad=@ShomareSanadArchive and (fldShomareVosool  is  null and fldShomarePardakht  is  null and fldVaziat is  null)

							delete from tblDaryaft where fldShomareSanad=@ShomareSanadArchive
							if @ShomareDaryaft>0 
								delete from tblRizeTasfie Where fldShomareDaryaft=@ShomareDaryaft
							set @noekhata='update_archive'

							
							
							--set @noekhata=3


							INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
								VALUES        (@lngCodeUser,@strNameUser,'دریافت','ویرایش',@lngDateNow,@strtime,@shomaredaryaft,@ShomareSanadArchive)

						end
					else
						--set @NoeKhata='error_tekrari_insert'
						--return
					--else
						begin
							if @InsertType='insert' -- برای حالتی که جدید میخاد ثبت کنه ولی یکم قبلش یک کاربر دیگه ثبت میکنه
								begin
									--Insert into tblTest7(fld1)
									--	values ('er in')
									set @NoeKhata='error_tekrari_insert'
									rollback transaction 
									return
									--set @NoeKhata=2
								end
						end
				end
			--else -- برای حالتی که شماره دریافت جدیده 
			--	begin
					declare @CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@intRadif int,@strRadif nvarchar(50),@strMoinKamel nvarchar(12)
					declare @ResultSanad nvarchar(10)
					--set @CodeKolHesabDaryaftani= substring('11301',2,2)
					--insert into tblTest7 (fld2,fld3)
					--	values(@TooleKol,@Toolemoin)


			
					set @CodeKolkham=left(@codemoinhesabhayedaryaftani,@TooleKol)	
					set @CodeMoinKham=substring(@codemoinhesabhayedaryaftani,@TooleKol+1,@Toolemoin)
			
					set @intRadif=0;

					--سند نقد
			
					execute IsArchiveShomareSanad @ShomareSanadKarbar,@ResultSanad output

					--Insert into tblTest7(fld1)
					--			values (@ResultSanad)

					if @naghd>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinSandoogh,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKolSandoogh + @CodeMoinSandoogh
							
							
							if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
								begin
									execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,@Naghd,0,@strRadif,@DateDaryaft,@SNaghd,@day,@month,@year,'daryaft',@CodeVasete,@strShomareSanad output	;
								end
							else	
								begin							
								  execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,@Naghd,0,@strRadif,@DateDaryaft,@SNaghd,@day,@month,@year,'daryaft',@CodeVasete,@ShomareSanadKarbar;
								  set @strShomareSanad=@ShomareSanadKarbar
								end
							--insert into tblTest7 (fld1)
								--values(@strShomareSanad)
						end
					--سند بانک
					if @Bank>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKolBank + @CodeMoinBank
					
							--insert into tblTest7 (fld1)
							--	values(@CodeKolBank + @CodeMoinBank)
							--execute insertSanadFirst  @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@Bank,0,@strRadif,@DateDaryaft,@SBank,'daryaft',@strShomareSanad output	;
							if @intRadif=1 
								begin
									if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
										execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@Bank,0,@strRadif,@DateDaryaft,@SBank,@day,@month,@year,'daryaft',@CodeVasete,@strShomareSanad output	;
									else
										begin
											execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@Bank,0,@strRadif,@DateDaryaft,@SBank,@day,@month,@year,'daryaft',@CodeVasete,@ShomareSanadKarbar;
											set @strShomareSanad=@ShomareSanadKarbar
										end
								end
							else
								begin
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@Bank,0,@strRadif,@DateDaryaft,@SBank,@day,@month,@year,'daryaft',@CodeVasete,@strShomareSanad;
								end
								if isnumeric(@ShomarePeigiri)=1 
									update tblSodooreSanad set fldShomarepeigiri=@ShomarePeigiri where fldMoinKamel= @strMoinKamel And fldSanad=@strShomareSanad
						end
					--سند چک
					if @MablaghCheck>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck
							if @intRadif=1
								begin
									if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
										begin
											execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DateDaryaft,@SCheck,@day,@month,@year,'daryaft',@CodeVasete,@strShomareSanad output;
										end
									else
										begin
											execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DateDaryaft,@SCheck,@day,@month,@year,'daryaft',@CodeVasete,@ShomareSanadKarbar;
											set @strShomareSanad=@ShomareSanadKarbar
										end
								end
							else
								begin
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DateDaryaft,@SCheck,@day,@month,@year,'daryaft',@CodeVasete,@strShomareSanad;
								end
							--execute insertSanadFirst  @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DateDaryaft,@SCheck,'daryaft',@strShomareSanad output	;
						end
					--سند تخفیفات
					if @Takhfif>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinTakhfifateDaryaft,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifateDaryaft

							if @intRadif=1 
								begin
									if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
										begin
											execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKoltakhfifat,@CodeMoinTakhfifateDaryaft,@strMoinKamel,NUll,NULL,@Takhfif,0,@strRadif,@DateDaryaft,@STakhfif,@day,@month,@year,'daryaft',@CodeVasete,@strShomareSanad output;
										end
									else
										begin
											execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKoltakhfifat,@CodeMoinTakhfifateDaryaft,@strMoinKamel,NUll,NULL,@Takhfif,0,@strRadif,@DateDaryaft,@STakhfif,@day,@month,@year,'daryaft',@CodeVasete,@ShomareSanadKarbar;										
											set @strShomareSanad=@ShomareSanadKarbar
										end
								end
							else
								begin
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKoltakhfifat,@CodeMoinTakhfifateDaryaft,@strMoinKamel,NUll,NULL,@Takhfif,0,@strRadif,@DateDaryaft,@STakhfif,@day,@month,@year,'daryaft',@CodeVasete,@strShomareSanad;
								end
							--execute insertSanadFirst  @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DateDaryaft,@SCheck,'daryaft',@strShomareSanad output	;
						end
					--سند بستانکاری
					set @intRadif =@intRadif+1			
					set @strRadif=convert(nvarchar(50),@intRadif)
					set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)
					if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
						begin
							execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham ,@CodeMoinHesabhayeDaryaftani,@CodeTafzili,'1',0,@decjam,@strRadif,@DateDaryaft,@SharheKol,@day,@month,@year,'daryaft',@CodeVasete,@strShomareSanad;
							if @InsertType<>'archive'								
								INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
									VALUES        (@lngCodeUser,@strNameUser,'دریافت','جدید',@lngDateNow,@strtime,@shomaredaryaft,@strShomareSanad)
						end
					else
						begin
							execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham ,@CodeMoinHesabhayeDaryaftani,@CodeTafzili,'1',0,@decjam,@strRadif,@DateDaryaft,@SharheKol,@day,@month,@year,'daryaft',@CodeVasete,@ShomareSanadKarbar;
							if @InsertType<>'archive'								
								INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
									VALUES        (@lngCodeUser,@strNameUser,'دریافت','جدید',@lngDateNow,@strtime,@shomaredaryaft,@ShomareSanadKarbar)
						end
					--execute insertSanadLast @CodeKolkham,@CodeMoinKham ,@codemoinhesabhayedaryaftani,NUll,NUll,@decjam,0,@strRadif,@DateDaryaft,@SharheKol,'daryaft';




					--سند پورسانت واسطه ها و ویزیتور ها
							if isnumeric(@CodeVasete )=1  and Convert(decimal(18,4),@CodeVasete)>0
								begin									
									set @intRadif=@intRadif+1
									
									execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
									set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
			
									--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
									if @lngMablaghPorsant>0 
										begin
											if @ResultSanad='archive'
												execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@CodeVasete,'0',@lngMablaghPorsant,0,@intRadif,@DateDaryaft,@SharhePoorsant,@day,@month,@year,'daryaft','',@strShomareSanad
											else
												execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@CodeVasete,'0',@lngMablaghPorsant,0,@intRadif,@DateDaryaft,@SharhePoorsant,@day,@month,@year,'daryaft','',@ShomareSanadKarbar


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											if @ResultSanad='archive'
												execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@CodeVasete,'0',0,@lngMablaghPorsant,@intRadif,@DateDaryaft,@SharhePoorsant,@day,@month,@year,'daryaft','',@strShomareSanad
											else
												execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@CodeVasete,'0',0,@lngMablaghPorsant,@intRadif,@DateDaryaft,@SharhePoorsant,@day,@month,@year,'daryaft','',@ShomareSanadKarbar
										end

									--### سند سوپروایزر####
									if @lngMablaghPorsantSuperVizor>0
										begin								
											set @intRadif=@intRadif+1
									
											execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
											set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
											
											if @ResultSanad='archive'
												execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeSuperVizor,'0',@lngMablaghPorsantSuperVizor,0,@intRadif,@DateDaryaft,@SharhePoorsant,@day,@month,@year,'daryaft','',@strShomareSanad
											else
												execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@fldCodeSuperVizor,'0',@lngMablaghPorsantSuperVizor,0,@intRadif,@DateDaryaft,@SharhePoorsant,@day,@month,@year,'daryaft','',@ShomareSanadKarbar


											set @intRadif=@intRadif+1									
											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											--set @lngsum=(@lngMablaghKolFactor-@lngHazineHamlVaNaghl-@lngArzeshAfzode)
											if @ResultSanad='archive'
												execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeSuperVizor,'0',0,@lngMablaghPorsantSuperVizor,@intRadif,@DateDaryaft,@SharhePoorsant,@day,@month,@year,'daryaft','',@strShomareSanad
											else
												execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeSuperVizor,'0',0,@lngMablaghPorsantSuperVizor,@intRadif,@DateDaryaft,@SharhePoorsant,@day,@month,@year,'daryaft','',@ShomareSanadKarbar
										end 
									
								end 
								--پایان سند پورسانت واسطه ها و ویزیتور ها 


					update tblSodooreSanad set fldColorBank=#tmpColor.fldColorBank,fldColorSandoogh=#tmpColor.fldColorSandoogh,fldColorSooratHesab=#tmpColor.fldColorSooratHesab from #tmpColor Where tblSodooreSanad.fldSanad=@ShomareSanadArchive and fldMoinKamel=#tmpColor.MoinKamel

					if @Resultsanad='notarchive' 
						begin
							set @strShomareSanad=@ShomareSanadKarbar
						end


					--ثبت ریز اطلاعات چک					
					Insert into  tblCheckeDaryafti (fldCodeArz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldSharheDaryaft,fldMablagh,fldCodeSahebeCheck,fldDateDaryaft,fldShomareDaryaft,fldVasete,fldSanad)
						Select @fldCodeArz,@fldNerkheArz, fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldSharheDaryaft,fldMablagh,@CodeTafzili,@Datekamel,@ShomareDaryaft,  @CodeVasete,@strShomareSanad output from @tblCheck;	

					delete tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft  and (fldShomareVosool  is  null and fldShomarePardakht  is  null and fldVaziat is  null) and fldShomareCheck in (Select fldShomareCheck from tblCheckeDaryafti Where fldShomareDaryaft=@ShomareDaryaft and (fldShomareVosool  is not null or fldShomarePardakht  is not null or fldVaziat is not null))

					--ثبت اطلاعات دریافت
					if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
						begin
							INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
								VALUES        (@ShomareDaryaft,@strShomareSanad,@CodeTafzili,@TypeDaryaft)
						end
					else
						begin
							INSERT  INTO   tblDaryaft( fldShomareDaryaft, fldShomareSanad, fldCodeTafzili,fldType)
								VALUES        (@ShomareDaryaft,@ShomareSanadKarbar,@CodeTafzili,@TypeDaryaft)
						end

					
					INSERT       INTO              tblRizeTasfie( fldShomareFactor, fldShomareDaryaft, fldMablagh,fldShomareBargashtAzForoosh)
						Select fldShomareFactor, @ShomareDaryaft, fldMablagh ,0   from  @dtTasfie


					INSERT       INTO              tblRizeTasfieVam( fldShomareVam,fldShomareGhest, fldShomareDaryaft, fldMablagh)
						Select fldShomareVam,fldShomareGhest, @ShomareDaryaft, fldMablagh    from  @dtTasfieVam
									

					set @NoeKhata='Insert_New'
		commit transaction
			end try

		begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='rolback'

		end catch
					--set @NoeKhata=1
				


			--set @MaxSanad=(Select Max(Convert(dec,fldSanad)) from tblSodooreSanad);	
			--set @MaxSanad+=1;
	
			--INSERT INTO tblSodooreSanad
			--                     (fldSanad)
			--	SELECT        MAX(CONVERT(dec, fldSanad)) + 1 AS Expr1
			--	FROM            tblSodooreSanad AS tblSodooreSanad_1
	


	
			--INSERT INTO tblSodooreSanad
		 --                        (fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldType)
			--	SELECT        MAX(CONVERT(dec, fldSanad)) + 1 AS MaxSanad,'111','01','11101',@CodeTafzili,'1',@decjam,0,'1',@DateDaryaft,@SharheKol,'daryaft'
			--	FROM            tblSodooreSanad AS tblSodooreSanad_1

	
			--set @IDOutPut=@@IDENTITY;	

			--set @lngShomareSanad=(Select fldSanad from tblSodooreSanad   where id=@IDOutPut)



                         
	

	
	

end

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoInPardakht]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertInfoInPardakht]
--چندزبانه شد
--پارامترهای کلی
@fldCodeArz int,
@fldNerkheArz decimal(18,4),
@InsertType nvarchar(10),
@DatePardakht int,
@CodeTafzili nvarchar(10),
@ShomarePardakht bigint,
@ShomareSanadKarbar nvarchar(50),
@SharheKol nvarchar(200),
@CodeMoinHesabhayePardakhtani nvarchar(10),
@day nvarchar(50),
@month nvarchar(50),
@year nvarchar(50),
@TypePardakht nvarchar(20),
@CodeHesabeHazine nvarchar(10),
@CodeNoeTafzili nvarchar(10),


--پارامترهای صندوق
@CodeKolSandoogh nvarchar(10),
@CodeMoinSandoogh nvarchar(10),
@Naghd decimal(18,4),
@SNaghd nvarchar(200),

--'پارامترهای بانک
@CodeKolBank nvarchar(10),
@CodeMoinBank nvarchar(10),
@Bank decimal(18,4),
@SBank nvarchar(200),

--'پارامترهای چک
@tblCheck [tblCheckPardakhti-Multi] readonly,
@CodeKolCheck nvarchar(10),
@CodeMoinCheck nvarchar(10),
@MablaghCheck decimal(18,4),
@SCheck nvarchar(200),

--'پارامترهای چک واگذاری
@CheckeVagozari tblCheckVagozari  readonly,
@CodeKolCheckVagozari nvarchar(10),
@CodeMoinCheckVagozari nvarchar(10),
@MablaghCheckVagozari decimal(18,4),
@SCheckVagozari nvarchar(200),

--پارامترهای تخفیفات
@CodeKoltakhfifat nvarchar(10),
@CodeMoinTakhfifatePardakht nvarchar(10),
@Takhfif decimal(18,4),
@STakhfif nvarchar(200),


@lngCodeUser bigint,
@lngDateNow bigint,

--پارامترهای واسطه
@CodeVasete nvarchar(10),





@NoeKhata nvarchar(30) output 


--@NoeKhata int output
as


create table #tmpColor (
	MoinKamel nvarchar(50) COLLATE Persian_100_CI_AS,
	fldColorBank int,
	fldColorSandoogh int , 
	fldColorSooratHesab int

)

begin
	begin transaction transact
		begin try
			declare @MaxSanad bigint,@lngI bigint,@lngCount bigint,@strShomareSanad nvarchar(50),@lngCountPardakht int,@IDOutPut bigint,@decJam decimal(18,4)	,@strNameUser nvarchar(25),@strTime nvarchar(30)
			declare @TooleKol bigint,@TooleMoin bigint,@ShomareSanadArchive nvarchar(50),@LngCountVagozari bigint,@lngIVagozari bigint,@DateKamel int
	 
			 execute  ToolePaye 'kol',@TooleKol output
			 execute  ToolePaye 'moin',@Toolemoin output

			 execute  Date9raghami @datePardakht,@datekamel output

			 set @lngCountPardakht=(Select Count(fldShomarePardakht) from tblPardakht Where fldShomarePardakht=@ShomarePardakht);

			 set @decjam=@naghd+@bank +@MablaghCheck+ @MablaghCheckVagozari+ @Takhfif ;

			 if @TypePardakht='hazine' and @CodeTafzili=0
				begin
					set @CodeTafzili=''
					set @CodeNoeTafzili=''
				end
			if @TypePardakht='pardakhtpersonel' and @CodeTafzili=0
				begin
					set @CodeTafzili=''
					set @CodeNoeTafzili=''
				end
				

			if @TypePardakht='enteghalevajh' 
				begin
					set @CodeTafzili=''
					set @CodeNoeTafzili=''
				end

			 set @strNameUser=(Select fldUser from tblSecurity Where fldCode=@lngCodeUser)

			 set @strtime=(SELECT ltrim(right(convert(varchar(25), getdate(), 100), 7)))

			 if @lngCountPardakht>0 
				begin
					if @InsertType='archive'  --برای حالتی که میخاد بروزرسانی کنه
						begin
							--Insert into tblTest5(name1,name2)
							--	values ('a','a')
							--declare @ShomareSanadArchive nvarchar(50)

							set @ShomareSanadArchive=(select fldShomareSanad from tblPardakht where fldShomarePardakht=@shomarePardakht)
							--Insert into tblTest7(fld2)
							--	values (@ShomareSanadArchive)
							insert into #tmpColor
								(MoinKamel,fldColorBank ,fldColorSandoogh , fldColorSooratHesab )
								Select fldMoinKamel,fldColorBank,fldColorSandoogh,fldColorSooratHesab from tblSodooreSanad Where fldSanad=@ShomareSanadArchive


							--INSERT INTO tblTest7
							--		(fld1, fld2, fld3)
							--Select fldMoinKamel,fldColorBank,@ShomareSanadArchive from tblSodooreSanad Where fldSanad=@ShomareSanadArchive

							delete from tblSodooreSanad Where fldSanad=@ShomareSanadArchive
							delete from tblCheckePardakhti where fldSanad=@ShomareSanadArchive
							update tblCheckeDaryafti set fldShomarePardakht=null,fldVaziat=NULL Where fldShomarePardakht=@ShomarePardakht
							delete from tblPardakht where fldShomareSanad=@ShomareSanadArchive
							
							set @noekhata='update_archive'

							if @TypePardakht='enteghalevajh' 
								INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
									VALUES        (@lngCodeUser,@strNameUser,'انتقال وجه','ویرایش',@lngDateNow,@strtime,@shomarePardakht,@ShomareSanadArchive)
							
							else if @TypePardakht='hazine' 
								INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
									VALUES        (@lngCodeUser,@strNameUser,'ثبت هزینه','ویرایش',@lngDateNow,@strtime,@shomarePardakht,@ShomareSanadArchive)
							else
								INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
									VALUES        (@lngCodeUser,@strNameUser,'پرداخت','ویرایش',@lngDateNow,@strtime,@shomarePardakht,@ShomareSanadArchive)

							--set @noekhata=3

						end
					else
						--set @NoeKhata='error_tekrari_insert'
						--return
					--else
						begin
							if @InsertType='insert' -- برای حالتی که جدید میخاد ثبت کنه ولی یکم قبلش یک کاربر دیگه ثبت میکنه
								begin
									--Insert into tblTest7(fld1)
									--	values ('er in')
									set @NoeKhata='error_tekrari_insert'
									rollback transaction 
									return
									--set @NoeKhata=2
								end
						end
				end
			--else -- برای حالتی که شماره دریافت جدیده 
			--	begin
					declare @CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@intRadif int,@strRadif nvarchar(50),@strMoinKamel nvarchar(12)
					declare @ResultSanad nvarchar(10)
					--set @CodeKolHesabPardakhtani= substring('11301',2,2)
					--insert into tblTest7 (fld2,fld3)
					--	values(@TooleKol,@Toolemoin)
			
					set @CodeKolkham=left(@codemoinhesabhayePardakhtani,@TooleKol)	
					set @CodeMoinKham=substring(@codemoinhesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
					set @intRadif=0;

					--سند نقد
			
					execute IsArchiveShomareSanad @ShomareSanadKarbar,@ResultSanad output

					--Insert into tblTest7(fld1)
					--			values (@ResultSanad)

					if @naghd>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinSandoogh,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKolSandoogh + @CodeMoinSandoogh
							
							
							if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
								begin
									execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,0,@Naghd,@strRadif,@DatePardakht,@SNaghd,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad output	;
								end
							else	
								begin							
								  execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,0,@Naghd,@strRadif,@DatePardakht,@SNaghd,@day,@month,@year,@TypePardakht,@CodeVasete,@ShomareSanadKarbar;
								  set @strShomareSanad=@ShomareSanadKarbar
								end
							--insert into tblTest7 (fld1)
								--values(@strShomareSanad)
						end
					--سند بانک
					if @Bank>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKolBank + @CodeMoinBank
					
							--insert into tblTest7 (fld1)
							--	values(@CodeKolBank + @CodeMoinBank)
							--execute insertSanadFirst  @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,@Bank,0,@strRadif,@DatePardakht,@SBank,@TypePardakht,@strShomareSanad output	;
							if @intRadif=1 
								begin
									if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
										execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,0,@Bank,@strRadif,@DatePardakht,@SBank,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad output	;
									else
										begin
											execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,0,@Bank,@strRadif,@DatePardakht,@SBank,@day,@month,@year,@TypePardakht,@CodeVasete,@ShomareSanadKarbar;
											set @strShomareSanad=@ShomareSanadKarbar
										end
								end
							else
								begin
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,0,@Bank,@strRadif,@DatePardakht,@SBank,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad;
								end
						end
					--سند چک
					if @MablaghCheck>0 
						begin							
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck

							declare @CodeKolCheckPersonel nvarchar(10),@CodeMoinKhamPersonel nvarchar(10),@strMoinKamelPersonel nvarchar(10)

							if @intRadif=1
								begin
									if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
										begin
											if @CodeNoeTafzili='0' --پرداختی به پرسنل
												begin
													execute codemoinhesab 'checkpersonel' ,@strMoinKamelPersonel output
													set @CodeMoinKhamPersonel=substring(@strMoinKamelPersonel,@TooleKol+1,@Toolemoin)
													set @CodeKolCheckPersonel=substring(@strMoinKamelPersonel,1,@TooleKol)
													execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckPersonel,@CodeMoinKhamPersonel,@strMoinKamelPersonel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheck,@strRadif,@DatePardakht,@SCheck,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad output;
												end
											else --پرداختی به اشخاص																	
													execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheck,@strRadif,@DatePardakht,@SCheck,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad output;												
										end
									else
										begin
											if @CodeNoeTafzili='0' --پرداختی به پرسنل
												begin
													execute codemoinhesab 'checkpersonel' ,@strMoinKamelPersonel output
													set @CodeMoinKhamPersonel=substring(@strMoinKamelPersonel,@TooleKol+1,@Toolemoin)
													set @CodeKolCheckPersonel=substring(@strMoinKamelPersonel,1,@TooleKol)
													--set @strMoinKamel=@CodeMoinCheck
													execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckPersonel,@CodeMoinKhamPersonel,@strMoinKamelPersonel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheck,@strRadif,@DatePardakht,@SCheck,@day,@month,@year,@TypePardakht,@CodeVasete,@ShomareSanadKarbar;												
													--execute insertSanadLast '317','02','31702',@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheck,@strRadif,@DatePardakht,@SCheck,@day,@month,@year,@TypePardakht,@CodeVasete,@ShomareSanadKarbar;												
												end
											else
												begin									
													execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheck,@strRadif,@DatePardakht,@SCheck,@day,@month,@year,@TypePardakht,@CodeVasete,@ShomareSanadKarbar;
												end


											
											set @strShomareSanad=@ShomareSanadKarbar
										end
								end
							else
								begin
									if @CodeNoeTafzili='0' --پرداختی به پرسنل
										begin
											execute codemoinhesab 'checkpersonel' ,@strMoinKamelPersonel output
											set @CodeMoinKhamPersonel=substring(@strMoinKamelPersonel,@TooleKol+1,@Toolemoin)
											set @CodeKolCheckPersonel=substring(@strMoinKamelPersonel,1,@TooleKol)
											execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckPersonel,@CodeMoinKhamPersonel,@strMoinKamelPersonel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheck,@strRadif,@DatePardakht,@SCheck,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad;
										end 
									else
										begin
											execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheck,@CodeMoinCheck,@CodeMoinKhamPersonel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheck,@strRadif,@DatePardakht,@SCheck,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad;
										end

								end
							--execute insertSanadFirst  @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DatePardakht,@SCheck,@TypePardakht,@strShomareSanad output	;
						end
						

					--@CheckeVagozari tblCheckVagozari  readonly,
					--@CodeKolCheckVagozari nvarchar(10),
					--@CodeMoinCheckVagozari nvarchar(10),
					--@MablaghCheckVagozari decimal,
					--@SCheckVagozari nvarchar(200),
					--سند واگذاری چک
						--While @lngIVagozari< @LngCountVagozari-1
						--	begin
								--update tblCheckeDaryafti set fldShomarePardakht=@ShomarePardakht  Where fldShomareCheck= @CheckeVagozari.fldShomarecheck And fldShomareHesab=@CheckeVagozari.fldShomareHesab 
								--UPDATE tblCheckeDaryafti Set fldShomarePardakht=@ShomarePardakht Where fldShomareCheck =@CheckeVagozari.fldShomareCheck
					--ثبت واگذاری چک
					--UPDATE tblCheckeDaryafti Set fldShomarePardakht=@ShomarePardakht Where Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) in (Select Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) from @CheckeVagozari)
					UPDATE tblCheckeDaryafti Set fldShomarePardakht=@ShomarePardakht,fldVaziat='forooshecheck' Where Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) in (Select Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) from @CheckeVagozari)


					--سند حسابداری واگذاری چک
					if @MablaghCheckVagozari>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinCheckVagozari,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKolCheckVagozari + @CodeMoinCheckVagozari

							declare @CodeKolCheckVagozariPersonel nvarchar(10),@CodeMoinKhamVagozariPersonel nvarchar(10),@strMoinKamelVagozariPersonel nvarchar(10)

							if @intRadif=1
								begin
									if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
										begin
											if @CodeNoeTafzili='0' --پرداختی به پرسنل
												begin
													execute codemoinhesab 'checkvagozaripersonel' ,@strMoinKamelVagozariPersonel output
													set @CodeMoinKhamVagozariPersonel=substring(@strMoinKamelVagozariPersonel,@TooleKol+1,@Toolemoin)
													set @CodeKolCheckVagozariPersonel=substring(@strMoinKamelVagozariPersonel,1,@TooleKol)													
													execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckVagozariPersonel,@CodeMoinKhamVagozariPersonel,@strMoinKamelVagozariPersonel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheckVagozari,@strRadif,@DatePardakht,@SCheckVagozari,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad output;
												end
											else--پرداختی به اشخاص
												execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckVagozari,@CodeMoinCheckVagozari,@strMoinKamel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheckVagozari,@strRadif,@DatePardakht,@SCheckVagozari,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad output;
										end
									else
										begin
											if @CodeNoeTafzili='0' --پرداختی به پرسنل
												begin												
													execute codemoinhesab 'checkvagozaripersonel' ,@strMoinKamelVagozariPersonel output
													set @CodeMoinKhamVagozariPersonel=substring(@strMoinKamelVagozariPersonel,@TooleKol+1,@Toolemoin)
													set @CodeKolCheckVagozariPersonel=substring(@strMoinKamelVagozariPersonel,1,@TooleKol)

													execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckVagozariPersonel,@CodeMoinKhamVagozariPersonel,@strMoinKamelVagozariPersonel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheckVagozari,@strRadif,@DatePardakht,@SCheckVagozari,@day,@month,@year,@TypePardakht,@CodeVasete,@ShomareSanadKarbar;
												end
											else
												execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckVagozari,@CodeMoinCheckVagozari,@strMoinKamel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheckVagozari,@strRadif,@DatePardakht,@SCheckVagozari,@day,@month,@year,@TypePardakht,@CodeVasete,@ShomareSanadKarbar;

											set @strShomareSanad=@ShomareSanadKarbar
										end
								end
							else
								begin
									if @CodeNoeTafzili='0' --پرداختی به پرسنل
										begin												
											execute codemoinhesab 'checkvagozaripersonel' ,@strMoinKamelVagozariPersonel output
											set @CodeMoinKhamVagozariPersonel=substring(@strMoinKamelVagozariPersonel,@TooleKol+1,@Toolemoin)
											set @CodeKolCheckVagozariPersonel=substring(@strMoinKamelVagozariPersonel,1,@TooleKol)
											execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckVagozariPersonel,@CodeMoinKhamVagozariPersonel,@strMoinKamelVagozariPersonel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheckVagozari,@strRadif,@DatePardakht,@SCheckVagozari,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad;
										end
									else
										execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolCheckVagozari,@CodeMoinCheckVagozari,@strMoinKamel,@CodeTafzili,@CodeNoeTafzili,0,@MablaghCheckVagozari,@strRadif,@DatePardakht,@SCheckVagozari,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad;
								end
							--execute insertSanadFirst  @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DatePardakht,@SCheck,@TypePardakht,@strShomareSanad output	;
						end
						--		--Set @lngiVagozari+=1
						--	end	
						--update tblCheckeDaryafti set fldShomarePardakht=@ShomarePardakht Where fldShomareCheck= @CheckeVagozari.fldShomarecheck And fldShomareHesab=@CheckeVagozari.fldShomareHesab 
						--insert into tblCheckePardakhti (fldShomareCheck) values 
					--سند تخفیفات
					if @Takhfif>0 
						begin
							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)
							set @CodeMoinKham=substring(@CodeMoinTakhfifatePardakht,@TooleKol+1,@Toolemoin)
							set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifatePardakht

							if @intRadif=1 
								begin
									if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
										begin
											execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,0,@Takhfif,@strRadif,@DatePardakht,@STakhfif,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad output;
										end
									else
										begin
											execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,0,@Takhfif,@strRadif,@DatePardakht,@STakhfif,@day,@month,@year,@TypePardakht,@CodeVasete,@ShomareSanadKarbar;										
											set @strShomareSanad=@ShomareSanadKarbar
										end
								end
							else
								begin
									execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,0,@Takhfif,@strRadif,@DatePardakht,@STakhfif,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad;
								end
							--execute insertSanadFirst  @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',@MablaghCheck,0,@strRadif,@DatePardakht,@SCheck,@TypePardakht,@strShomareSanad output	;
						end
					--سند بدهکاری
					set @intRadif =@intRadif+1			
					set @strRadif=convert(nvarchar(50),@intRadif)

					if @TypePardakht='hazine' 
						begin
							set @CodeMoinHesabhayePardakhtani=@CodeHesabeHazine
						end
					if @TypePardakht='pardakhtpersonel'  or @TypePardakht='pardakhtsayer'
						begin
							set @CodeMoinHesabhayePardakhtani=@CodeHesabeHazine
						end

					if @TypePardakht='enteghalevajh'
						begin
							--Insert into tblTest5(name1,name2)
							--		values (@CodeMoinHesabhayePardakhtani,@CodeHesabeHazine)
							set @CodeMoinHesabhayePardakhtani=@CodeHesabeHazine
							--Insert into tblTest5(name1,name2)
							--		values (@CodeMoinHesabhayePardakhtani,@CodeHesabeHazine)
						end
					 
				

					set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
					if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
						begin
							execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham ,@CodeMoinHesabhayePardakhtani,@CodeTafzili,@CodeNoeTafzili,@decjam,0,@strRadif,@DatePardakht,@SharheKol,@day,@month,@year,@TypePardakht,@CodeVasete,@strShomareSanad;

							if @InsertType<>'archive'	
								BEGIN
									if @TypePardakht='enteghalevajh' 
										INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
											VALUES        (@lngCodeUser,@strNameUser,'انتقال وجه','جدید',@lngDateNow,@strtime,@shomarePardakht,@strShomareSanad)
							
									else if @TypePardakht='hazine' 
										INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
											VALUES        (@lngCodeUser,@strNameUser,'ثبت هزینه','جدید',@lngDateNow,@strtime,@shomarePardakht,@strShomareSanad)

									else
										INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
											VALUES        (@lngCodeUser,@strNameUser,'پرداخت','جدید',@lngDateNow,@strtime,@shomarePardakht,@strShomareSanad)
								END

						end
					else
						begin
							execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham,@CodeMoinKham ,@CodeMoinHesabhayePardakhtani,@CodeTafzili,@CodeNoeTafzili,@decjam,0,@strRadif,@DatePardakht,@SharheKol,@day,@month,@year,@TypePardakht,@CodeVasete,@ShomareSanadKarbar;
							if @InsertType<>'archive'	
								BEGIN
									if @TypePardakht='enteghalevajh' 
										INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
											VALUES        (@lngCodeUser,@strNameUser,'انتقال وجه','جدید',@lngDateNow,@strtime,@shomarePardakht,@ShomareSanadKarbar)
							
									else if @TypePardakht='hazine' 
										INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
											VALUES        (@lngCodeUser,@strNameUser,'ثبت هزینه','جدید',@lngDateNow,@strtime,@shomarePardakht,@ShomareSanadKarbar)
									else
										INSERT  INTO  tblAmalkardekarbar( fldCodeKarbar, fldNameKarbar, fldNameForm, NoeAmaliat, fldDate, fldTime, fldShomare, fldSanad)
											VALUES        (@lngCodeUser,@strNameUser,'پرداخت','جدید',@lngDateNow,@strtime,@shomarePardakht,@ShomareSanadKarbar)
								END
						end
					--execute insertSanadLast @CodeKolkham,@CodeMoinKham ,@codemoinhesabhayePardakhtani,NUll,NUll,@decjam,0,@strRadif,@DatePardakht,@SharheKol,@TypePardakht;


					
					update tblSodooreSanad set fldColorBank=#tmpColor.fldColorBank,fldColorSandoogh=#tmpColor.fldColorSandoogh,fldColorSooratHesab=#tmpColor.fldColorSooratHesab from #tmpColor Where tblSodooreSanad.fldSanad=@ShomareSanadArchive and fldMoinKamel=#tmpColor.MoinKamel

					if @Resultsanad='notarchive' 
						begin
							set @strShomareSanad=@ShomareSanadKarbar
						end

						 
					--ثبت ریز اطلاعات چک					
					Insert into  tblCheckePardakhti (fldCodeArz,fldNerkheArz, fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDatePardakht,fldShomarePardakht,fldVasete,fldSanad)
						Select @fldCodeArz,@fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@CodeTafzili,@DateKamel,@ShomarePardakht, @CodeVasete,@strShomareSanad output from @tblCheck;	

					--update tblCheckePardakhti set fldShomareCheck=10 where @tblCheck.fld 

					--if ISNUMERIC(@CodeTafzili)=0
					--	set @CodeTafzili='0'

					--ثبت اطلاعات دریافت
					if @ResultSanad='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
						begin
							INSERT  INTO   tblPardakht( fldShomarePardakht, fldShomareSanad, fldCodeTafzili,fldType)
								VALUES        (@ShomarePardakht,@strShomareSanad,@CodeTafzili,@TypePardakht)							 
						end
					else
						begin
							INSERT INTO tblPardakht
								(fldShomarePardakht, fldShomareSanad, fldCodeTafzili, fldType)
							VALUES        (@ShomarePardakht, @ShomareSanadKarbar,@CodeTafzili,@TypePardakht)
								--INSERT  INTO   tblPardakht( fldShomarePardakht, fldShomareSanad, fldCodeTafzili,fldType)
								--	VALUES        (@ShomarePardakht,@ShomareSanadKarbar,@CodeTafzili,@TypePardakht)
								--INSERT       INTO              tblTest7(fld1, fld2, fld3)
								--		VALUES        (@TypePardakht,@ShomarePardakht,@ShomareSanadKarbar)
						end

					set @NoeKhata='Insert_New'
		commit transaction
			end try

		begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='rolback'

		end catch
					--set @NoeKhata=1
				


			--set @MaxSanad=(Select Max(Convert(dec,fldSanad)) from tblSodooreSanad);	
			--set @MaxSanad+=1;
	
			--INSERT INTO tblSodooreSanad
			--                     (fldSanad)
			--	SELECT        MAX(CONVERT(dec, fldSanad)) + 1 AS Expr1
			--	FROM            tblSodooreSanad AS tblSodooreSanad_1
	


	
			--INSERT INTO tblSodooreSanad
		 --                        (fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldType)
			--	SELECT        MAX(CONVERT(dec, fldSanad)) + 1 AS MaxSanad,'111','01','11101',@CodeTafzili,'1',@decjam,0,'1',@DatePardakht,@SharheKol,@TypePardakht
			--	FROM            tblSodooreSanad AS tblSodooreSanad_1

	
			--set @IDOutPut=@@IDENTITY;	

			--set @lngShomareSanad=(Select fldSanad from tblSodooreSanad   where id=@IDOutPut)



                         
	

	
	

end

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoInTblTanzimatDynamic]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[InsertInfoInTblTanzimatDynamic]
	@strType nvarchar(30),
	@strMeghdar nvarchar(30)
as 

begin
	INSERT       INTO              tblTanzimatDynamic(fldType, fldMeghdar)
				VALUES        (@strtype,@strmeghdar)
end

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoIntblTanzimatKol]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertInfoIntblTanzimatKol] 
--چندزبانه
	@Type nvarchar(50),
	@Meghdar nvarchar(70)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	Delete tblTanzimateKol Where fldtype=@Type

	Insert into tblTanzimateKol
		(fldType,fldMeghdar)
		values
		(@Type,@Meghdar)
END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoKhabanidanCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertInfoKhabanidanCheck] 
--چندزبانه
@fldIdCheck bigint,
@fldDateVagozariBeBank  int,
@fldTozihateVagozariBeBank  nvarchar(50),
@fldCodeBankVagozari  nvarchar(50),
@Result nvarchar(50) output
    
AS
BEGIN
	
	SET NOCOUNT ON;

   UPDATE [dbo].[tblCheckeDaryafti]
   SET 
     fldDateVagozariBeBank=@fldDateVagozariBeBank, 
     fldTozihateVagozariBeBank=@fldTozihateVagozariBeBank,
     fldVaziat ='Vagozari',
     fldCodeBankVagozari=@fldCodeBankVagozari 
    
 WHERE ID=@fldIdCheck 
    set @Result='UpdateOk'
	
END
GO
/****** Object:  StoredProcedure [dbo].[InsertInfoKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertInfoKharid]
--چندزبانه
@fldCodeArz int,
@fldNerkheArz decimal(18,4),
@flagKharid nvarchar(10),

--@dtCheckPardakhti tblCheckPardakhti readonly,
--@dtCheckMoshtari tblCheckVagozari readonly,
@intHasRowCheckPardakhti int,
@intHasRowCheckMoshtari int,
@lngMablaghKolCheckHaPardakhti decimal(18,4),
@lngMablaghKolCheckMoshtari decimal(18,4),



@lngMablaghNaghdPardakht decimal(18,4),
@lngMablaghBankPardakht decimal(18,4),
@lngMablaghTakhfifPardakht decimal(18,4),

@dtKharid [tpRizeKharid_FactorKharid_FinalNew-Multi] readonly,
@fldSharhfactor nvarchar(250),
@fldShomarefactor bigint,
@fldShomaresanad bigint,
@fldDate bigint,
@fldDateSarResid bigint,
@fldCodeTafsili bigint,


@lngHazineHamlVaNaghl decimal(18,4),
@lngArzeshAfzode decimal(18,4),
@lngMablaghKolFactor decimal(18,4),

--@lngCodeSandoghPardakht bigint,
--@lngCodeBankPardakht bigint,


---مرعشی

--پارامترهای صندوق
@CodeKolSandoogh nvarchar(10),
@CodeMoinSandoogh nvarchar(10),
--@Naghd decimal,
--@SNaghd nvarchar(200),

--'پارامترهای بانک
@CodeKolBank nvarchar(10),
@CodeMoinBank nvarchar(10),
--@Bank decimal,
--@SBank nvarchar(200),

--'پارامترهای چک
@tblCheck [tblCheckPardakhti-Multi] readonly,
@CodeKolCheck nvarchar(10),
@CodeMoinCheck nvarchar(10),
--@MablaghCheck decimal,
--@SCheck nvarchar(200),

--'پارامترهای چک واگذاری
@CheckeVagozari tblCheckVagozari readonly,
@CodeKolCheckVagozari nvarchar(10),
@CodeMoinCheckVagozari nvarchar(10),
--@MablaghCheckVagozari decimal,
--@SCheckVagozari nvarchar(200),

--پارامترهای تخفیفات
@CodeKoltakhfifat nvarchar(10),
@CodeMoinTakhfifatePardakht nvarchar(10),
--@Takhfif decimal,
--@STakhfif nvarchar(200),

-----پایان کد مرعشی
--شرح پرداخت
@strSharhNaghd nvarchar(50),
@strSharhBank nvarchar(50),
@strSharhCheck nvarchar(50),
@strSharhCheckMoshtari nvarchar(50),
@strSharhTakhfif nvarchar(50),
@strSharhKoli nvarchar(50),
@strResult nvarchar(25) output,
@strResult2 NVarChar (25) output,
@flagInsert nvarchar(10)
AS

create table #tmpColor (
	MoinKamel nvarchar(50) COLLATE Persian_100_CI_AS,
	fldColorBank int,
	fldColorSandoogh int , 
	fldColorSooratHesab int
)
BEGIN
begin transaction AddRecord
			begin try
					--set @MablaghCheckVagozari=@lngMablaghKolCheckMoshtari
					--set @MablaghCheck=@lngMablaghKolCheckHaPardakhti
					declare @intRadif int,@CodeMoinHesabhayePardakhtani nvarchar(100),@CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@TooleKol bigint,@Toolemoin bigint, @lngsum decimal(18,4)
					declare @day nvarchar(4),@Month nvarchar(4),@year nvarchar(5),@ShomarePardakht bigint,@yearFull nvarchar(5),@dateSmall bigint
					declare @fldCodeVasete bigint,@strMoinKamel nvarchar(50),@lngSumPardakht decimal(18,4)
					--,@ShomarePardakht bigint,@lngSumPardakht bigint
					--set @year=substring(convert(nvarchar(20),@fldDate),0,5)
					set @yearFull=substring(convert(nvarchar(20),@fldDate),0,5)
					set @year=@yearFull
					set @month=substring(convert(nvarchar(20),@fldDate),5,2)
					set @day=substring(convert(nvarchar(20),@fldDate),7,2)
					--set @dateSmall=substring(convert(nvarchar(15),@fldDate),3,6)
					set @dateSmall=@fldDate

					declare @maxFactor bigint
					set @intRadif=0
					set @fldCodeVasete=0
					----------End Declare and set Init Values----------

			if(@flagKharid='arshive')
				begin
					--delete
						delete from tblRizeKharid
						where fldShomareSanad=@fldShomareSanad and fldShomareFactor=@fldShomareFactor
						IF @@ROWCOUNT = 0
						begin
						set @strResult='NotDeletedRizeKharid'
						rollback transaction DelRecord;
						end
						delete from tblKharid
						where fldShomareSanad=@fldShomareSanad and fldShomareFactorKharid=@fldShomareFactor
						IF @@ROWCOUNT = 0
						begin
						set @strResult='NotDeletedKharid'
						rollback transaction DelRecord;
						end

						insert into #tmpColor
								(MoinKamel,fldColorBank ,fldColorSandoogh , fldColorSooratHesab )
								Select fldMoinKamel,fldColorBank,fldColorSandoogh,fldColorSooratHesab from tblSodooreSanad Where fldSanad=@fldShomareSanad

						delete from tblSodooreSanad
						where fldSanad=@fldShomareSanad
						IF @@ROWCOUNT = 0
						begin
						set @strResult='NotDeletedtblSodooreSanad'
						rollback transaction DelRecord;
						end							
						
						set @ShomarePardakht =(Select  fldShomarePardakht from tblPardakht Where fldShomareSanad = @fldShomareSanad)
						if @ShomarePardakht is not null
							begin
								UPDATE tblCheckeDaryafti Set fldShomarePardakht=NULL Where fldShomarePardakht=@ShomarePardakht 
							end
						
						delete from tblPardakht
						where fldShomareSanad=@fldShomareSanad

						delete from tblCheckePardakhti
						where fldSanad=@fldShomareSanad

						--declare @mm bigint
						--set @mm=(Select  fldShomarePardakht from tblPardakht Where fldShomareSanad = 384)
						

						--INSERT INTO dbo.tblTest5
						--				(name1, name2, name3)
						--				VALUES        (@mm,@fldShomareSanad,'aa')
						


						--INSERT INTO dbo.tblTest5
						--				(name1, name2, name3)
						--				VALUES        (@ShomarePardakht,@fldShomareSanad,'hh')

					--insert
					----------hamlonaghl and arzeshafzode and kharid--------------
					set @intRadif=@intRadif+1
					execute ToolePaye 'kol',@TooleKol output
					execute ToolePaye 'moin',@Toolemoin output
					execute codemoinhesab 'kharid' ,@CodeMoinHesabhayePardakhtani output
					set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
					set @lngsum=(@lngMablaghKolFactor-(@lngHazineHamlVaNaghl+@lngArzeshAfzode))
					execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngsum,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad

					if @lngHazineHamlVaNaghl>0
					begin
						set @intRadif=@intRadif+1
						execute codemoinhesab 'hazinehamlvanaghlekharid' ,@CodeMoinHesabhayePardakhtani output
						set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
						set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
						execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngHazineHamlVaNaghl,0,@intRadif,@dateSmall,'none',@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
					end

					if @lngArzeshAfzode>0
					begin
						set @intRadif=@intRadif+1
						execute codemoinhesab 'arzesheafzoodekharid' ,@CodeMoinHesabhayePardakhtani output
						set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
						set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
						execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngArzeshAfzode,0,@intRadif,@dateSmall,'none',@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
					end
					-----------------end hamlonaghl and arzeshafzode and kharid-------------
					set @intRadif=@intRadif+1
					execute ToolePaye 'kol',@TooleKol output
					execute ToolePaye 'moin',@Toolemoin output
					execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output
					set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
					set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
					--بستانکار
					execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',0,@lngMablaghKolFactor,@intRadif,@dateSmall,'none',@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
					-----------------------pardakht----------------------------------------
					--بررسی شرح های پرداخت
					if (@strSharhNaghd is null or @strSharhNaghd='')
					begin
						set @strSharhNaghd='nonePardakht'
					end
					if (@strSharhBank is null or @strSharhBank='')
					begin
						set @strSharhBank='nonePardakht'
					end
					if (@strSharhCheck is null or @strSharhCheck='')
					begin
						set @strSharhCheck='nonePardakht'
					end
					if (@strSharhTakhfif is null or @strSharhTakhfif='')
					begin
						set @strSharhTakhfif='nonePardakht'
					end
					--شرح دریافت اصلی فروش
					if (@strSharhKoli is null or @strSharhKoli='')
					begin
						set @strSharhKoli='nonePardakht'
					end

					--پایان بررسی شرح های پرداخت
					set @lngSumPardakht=(@lngMablaghNaghdPardakht+@lngMablaghBankPardakht+@lngMablaghKolCheckHaPardakhti+@lngMablaghKolCheckMoshtari+@lngMablaghTakhfifPardakht)

					if(@lngSumPardakht>0)
					begin
						set @intRadif=@intRadif+1
						execute ToolePaye 'kol',@TooleKol output
						execute ToolePaye 'moin',@Toolemoin output
						execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output
						set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
						set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
						--بدهکار
						execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngSumPardakht,0,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
					end

					if @lngMablaghNaghdPardakht>0
					begin
						set @intRadif =@intRadif+1
						set @CodeMoinKham=substring(@CodeMoinSandoogh,@TooleKol+1,@Toolemoin)
						set @strMoinKamel=@CodeKolSandoogh + @CodeMoinSandoogh
						execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,0,@lngMablaghNaghdPardakht,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
					end
					--سند بانک
					if @lngMablaghBankPardakht>0
					begin
						set @intRadif =@intRadif+1

						set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
						set @strMoinKamel=@CodeKolBank + @CodeMoinBank
						execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,0,@lngMablaghBankPardakht,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
					end
					--سند چک
					if @lngMablaghKolCheckHaPardakhti>0
					begin
						set @intRadif =@intRadif+1

						set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)
						set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck
						execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckHaPardakhti,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
					end
					--سند حسابداری واگذاری چک
					if @lngMablaghKolCheckMoshtari>0
					begin
						set @intRadif =@intRadif+1
						set @CodeMoinKham=substring(@CodeMoinCheckVagozari,@TooleKol+1,@Toolemoin)
						set @strMoinKamel=@CodeKolCheckVagozari + @CodeMoinCheckVagozari
						execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheckVagozari,@CodeMoinCheckVagozari,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckMoshtari,@intRadif,@dateSmall,@strSharhCheckMoshtari,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;

					end

					if @lngMablaghTakhfifPardakht>0
					begin
						set @intRadif =@intRadif+1
						set @CodeMoinKham=substring(@CodeMoinTakhfifatePardakht,@TooleKol+1,@Toolemoin)
						set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifatePardakht
						execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,0,@lngMablaghTakhfifPardakht,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
					end
					if (@lngSumPardakht>0)
					begin
						set @ShomarePardakht=(select isnull(max(fldShomarePardakht),0) as fldMax from tblPardakht)
						set @ShomarePardakht=@ShomarePardakht+1
						INSERT INTO tblPardakht( fldShomarePardakht, fldShomareSanad, fldCodeTafzili,fldType)
						VALUES (@ShomarePardakht,@fldShomareSanad,@fldCodeTafsili,'kharid')

						--ثبت ریز اطلاعات چک
						Insert into tblCheckePardakhti (fldCodeArz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDatePardakht,fldShomarePardakht,fldVasete,fldSanad)
						Select @fldCodeArz,@fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,@ShomarePardakht, @fldCodeVasete,@fldShomareSanad output from @tblCheck;

						--ثبت واگذاری چک
						UPDATE tblCheckeDaryafti Set fldShomarePardakht=@ShomarePardakht Where Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) in (Select Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) from @CheckeVagozari)

					end

					-----------------------factor--------------------------------------

					set @maxFactor=@fldShomarefactor


					INSERT INTO tblKharid ([fldShomareFactorKharid],[fldShomareSanad],[fldDate],[fldDateSarResid],[fldSharhFactor])
						values(@maxFactor,@fldShomareSanad,@fldDate,@fldDateSarResid, @fldSharhFactor)
						INSERT INTO tblRizeKharid([fldShomareSanad],[fldShomareFactor]
						,[fldCodeVasete],[fldCodeTafsili],[fldCodeUser],[fldRadif],[fldCodeAnbar]
						,[fldCodeKala],[fldSharhKala],[fldTedadCarton],[fldTedadJoz],[fldFeeJoz]
						,[fldFeeYekCarton],[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif]
						,[fldFeeVahed1BadAzTakhfif],[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode]
						,[fldFeeKolBadAzTakhfif],[fldMablaghKhales],[fldType],[fldDate],[fldTime]
						,[fldHamloNaghl],[fldGhymatMasrafkonande],[fldDateEngheza])
						select @fldShomareSanad,@maxFactor,[fldCodeVasete],[fldCodeTafsili]
						,[fldCodeUser],[fldRadif],[fldCodeAnbar],[fldCodeKala],[fldSharhKala]
						,[fldTedadCarton],[fldTedadJoz],[fldFeeJoz],[fldFeeYekCarton]
						,[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif],[fldFeeVahed1BadAzTakhfif]
						,[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode],[fldFeeKolBadAzTakhfif]
						,[fldMablaghKhales],[fldType],[fldDate],[fldTime],[fldHamloNaghl]
						,[fldGhymatMasrafkonande],[fldDateEngheza]
						output from @dtKharid;
						set @strResult=convert(nvarchar(17),@maxFactor)
						set @strResult2='update'
						set @flagInsert='yes'
					end


			--########

			else if(@flagKharid='insert')
			begin
				    declare @resultExist bigint
					execute ExistFactorKharid @fldShomareFactor,@resultExist output
					declare @maxFactorKharidForKarbar bigint
					execute GetMaxShomareFactorKharid @maxFactorKharidForKarbar output
					set @maxFactorKharidForKarbar=@maxFactorKharidForKarbar+1
						
					if(@resultExist>0)
						begin
						--کاربر دیگری در همین لحظه شماره فاکتور را گرفت
							
							set @strResult=convert(nvarchar(17),@maxFactorKharidForKarbar)
							set @strResult2='New_Enter'
							rollback transaction AddRecord;
							return
							--در قسمت ویندوزی به کاربر کادری را نشان دهد که max+1 می باشد
							--yes
								--insert
							--no
								--inputbox در برنامه ویندوز که کاربر خودش شماره فاکتور را مشخص کند
								--باید اینجا چکار کنم؟؟؟؟
						end 
					else if(@resultExist=0 and @flagInsert='yes')
					    begin
						 --زمانی که کاربر از کادر پیام رویه دکمه بله کلیک میکنه
						--ehsan test
							--declare @resultExist bigint
							execute ExistFactorKharid @fldShomareFactor,@resultExist output
							--declare @maxFactorKharidForKarbar bigint
							--execute GetMaxShomareFactorKharid @maxFactorKharidForKarbar output
							set @maxFactorKharidForKarbar=@fldShomarefactor
							--set @maxFactorKharidForKarbar=@maxFactorKharidForKarbar+1
						
							if(@resultExist>0)
								begin
								--کاربر دیگری در همین لحظه شماره فاکتور را گرفت
							
									set @strResult=convert(nvarchar(17),@maxFactorKharidForKarbar)
									set @strResult2='New_Enter'
									rollback transaction AddRecord;
									return
									--در قسمت ویندوزی به کاربر کادری را نشان دهد که max+1 می باشد
									--yes
										--insert
									--no
										--inputbox در برنامه ویندوز که کاربر خودش شماره فاکتور را مشخص کند
								end 
						--ehsan test end
									----------hamlonaghl and arzeshafzode and kharid--------------
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'kharid' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									set @lngsum=(@lngMablaghKolFactor-(@lngHazineHamlVaNaghl+@lngArzeshAfzode))
									execute InsertSanadfirstMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngsum,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad out

									if @lngHazineHamlVaNaghl>0
									begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'hazinehamlvanaghlekharid' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngHazineHamlVaNaghl,0,@intRadif,@dateSmall,'none',@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
									end

									if @lngArzeshAfzode>0
									begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'arzesheafzoodekharid' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngArzeshAfzode,0,@intRadif,@dateSmall,'none',@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
									end
									-----------------end hamlonaghl and arzeshafzode and kharid-------------
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									--بستانکار
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',0,@lngMablaghKolFactor,@intRadif,@dateSmall,'none',@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
									-----------------------pardakht----------------------------------------
									--بررسی شرح های پرداخت
									if (@strSharhNaghd is null or @strSharhNaghd='')
									begin
									set @strSharhNaghd='nonePardakht'
									end
									if (@strSharhBank is null or @strSharhBank='')
									begin
									set @strSharhBank='nonePardakht'
									end
									if (@strSharhCheck is null or @strSharhCheck='')
									begin
									set @strSharhCheck='nonePardakht'
									end
									if (@strSharhTakhfif is null or @strSharhTakhfif='')
									begin
									set @strSharhTakhfif='nonePardakht'
									end
									--شرح دریافت اصلی فروش
									if (@strSharhKoli is null or @strSharhKoli='')
									begin
									set @strSharhKoli='nonePardakht'
									end

									--پایان بررسی شرح های پرداخت
									set @lngSumPardakht=(@lngMablaghNaghdPardakht+@lngMablaghBankPardakht+@lngMablaghKolCheckHaPardakhti+@lngMablaghKolCheckMoshtari+@lngMablaghTakhfifPardakht)

									if(@lngSumPardakht>0)
									begin
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									--بدهکار
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngSumPardakht,0,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
									end

									if @lngMablaghNaghdPardakht>0
									begin
									set @intRadif =@intRadif+1
									set @CodeMoinKham=substring(@CodeMoinSandoogh,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolSandoogh + @CodeMoinSandoogh
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,0,@lngMablaghNaghdPardakht,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
									end
									--سند بانک
									if @lngMablaghBankPardakht>0
									begin
									set @intRadif =@intRadif+1

									set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolBank + @CodeMoinBank
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,0,@lngMablaghBankPardakht,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
									end
									--سند چک
									if @lngMablaghKolCheckHaPardakhti>0
									begin
									set @intRadif =@intRadif+1

									set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckHaPardakhti,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
									end
									--سند حسابداری واگذاری چک
									if @lngMablaghKolCheckMoshtari>0
									begin
									set @intRadif =@intRadif+1
									set @CodeMoinKham=substring(@CodeMoinCheckVagozari,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolCheckVagozari + @CodeMoinCheckVagozari
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheckVagozari,@CodeMoinCheckVagozari,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckMoshtari,@intRadif,@dateSmall,@strSharhCheckMoshtari,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;

									end

									if @lngMablaghTakhfifPardakht>0
									begin
									set @intRadif =@intRadif+1
									set @CodeMoinKham=substring(@CodeMoinTakhfifatePardakht,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifatePardakht
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,0,@lngMablaghTakhfifPardakht,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
									end
									if (@lngSumPardakht>0)
									begin
									set @ShomarePardakht=(select isnull(max(fldShomarePardakht),0) as fldMax from tblPardakht)
									set @ShomarePardakht=@ShomarePardakht+1
									INSERT INTO tblPardakht( fldShomarePardakht, fldShomareSanad, fldCodeTafzili,fldType)
									VALUES (@ShomarePardakht,@fldShomareSanad,@fldCodeTafsili,'kharid')

									--ثبت ریز اطلاعات چک
									Insert into tblCheckePardakhti (fldCodeArz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDatePardakht,fldShomarePardakht,fldVasete,fldSanad)
									Select @fldCodeArz,@fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,@ShomarePardakht, @fldCodeVasete,@fldShomareSanad output from @tblCheck;

									--ثبت واگذاری چک
									UPDATE tblCheckeDaryafti Set fldShomarePardakht=@ShomarePardakht Where Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) in (Select Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) from @CheckeVagozari)

									end

									-----------------------factor--------------------------------------

									--execute GetMaxShomareFactorKharid @maxFactor output
									set @maxFactor=@maxFactorKharidForKarbar

									INSERT INTO tblKharid ([fldShomareFactorKharid],[fldShomareSanad],[fldDate],[fldDateSarResid],[fldSharhFactor])
									values(@maxFactor,@fldShomareSanad,@fldDate,@flddateSarResid, @fldSharhFactor)
									INSERT INTO tblRizeKharid([fldShomareSanad],[fldShomareFactor]
									,[fldCodeVasete],[fldCodeTafsili],[fldCodeUser],[fldRadif],[fldCodeAnbar]
									,[fldCodeKala],[fldSharhKala],[fldTedadCarton],[fldTedadJoz],[fldFeeJoz]
									,[fldFeeYekCarton],[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif]
									,[fldFeeVahed1BadAzTakhfif],[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode]
									,[fldFeeKolBadAzTakhfif],[fldMablaghKhales],[fldType],[fldDate],[fldTime]
									,[fldHamloNaghl],[fldGhymatMasrafkonande],[fldDateEngheza])
									select @fldShomareSanad,@maxFactor,[fldCodeVasete],[fldCodeTafsili]
									,[fldCodeUser],[fldRadif],[fldCodeAnbar],[fldCodeKala],[fldSharhKala]
									,[fldTedadCarton],[fldTedadJoz],[fldFeeJoz],[fldFeeYekCarton]
									,[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif],[fldFeeVahed1BadAzTakhfif]
									,[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode],[fldFeeKolBadAzTakhfif]
									,[fldMablaghKhales],[fldType],[fldDate],[fldTime],[fldHamloNaghl]
									,[fldGhymatMasrafkonande],[fldDateEngheza]
									output from @dtKharid;
									set @strResult=convert(nvarchar(17),@maxFactor)
									set @strResult2='insert'
									set @flagInsert='yes_byKarbar'
						end
					else if(@resultExist=0 and @flagInsert='no')
					    begin
						
									----------hamlonaghl and arzeshafzode and kharid--------------
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'kharid' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									set @lngsum=(@lngMablaghKolFactor-(@lngHazineHamlVaNaghl+@lngArzeshAfzode))
									execute InsertSanadfirstMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngsum,0,@intRadif,@dateSmall,@fldSharhFactor,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad out

									if @lngHazineHamlVaNaghl>0
									begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'hazinehamlvanaghlekharid' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngHazineHamlVaNaghl,0,@intRadif,@dateSmall,'none',@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
									end

									if @lngArzeshAfzode>0
									begin
									set @intRadif=@intRadif+1
									execute codemoinhesab 'arzesheafzoodekharid' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,null,null,@lngArzeshAfzode,0,@intRadif,@dateSmall,'none',@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
									end
									-----------------end hamlonaghl and arzeshafzode and kharid-------------
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									--بستانکار
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',0,@lngMablaghKolFactor,@intRadif,@dateSmall,'none',@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
									-----------------------pardakht----------------------------------------
									--بررسی شرح های پرداخت
									if (@strSharhNaghd is null or @strSharhNaghd='')
									begin
									set @strSharhNaghd='nonePardakht'
									end
									if (@strSharhBank is null or @strSharhBank='')
									begin
									set @strSharhBank='nonePardakht'
									end
									if (@strSharhCheck is null or @strSharhCheck='')
									begin
									set @strSharhCheck='nonePardakht'
									end
									if (@strSharhTakhfif is null or @strSharhTakhfif='')
									begin
									set @strSharhTakhfif='nonePardakht'
									end
									--شرح دریافت اصلی فروش
									if (@strSharhKoli is null or @strSharhKoli='')
									begin
									set @strSharhKoli='nonePardakht'
									end

									--پایان بررسی شرح های پرداخت
									set @lngSumPardakht=(@lngMablaghNaghdPardakht+@lngMablaghBankPardakht+@lngMablaghKolCheckHaPardakhti+@lngMablaghKolCheckMoshtari+@lngMablaghTakhfifPardakht)

									if(@lngSumPardakht>0)
									begin
									set @intRadif=@intRadif+1
									execute ToolePaye 'kol',@TooleKol output
									execute ToolePaye 'moin',@Toolemoin output
									execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output
									set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
									set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
									--بدهکار
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@fldCodeTafsili,'1',@lngSumPardakht,0,@intRadif,@dateSmall,@strSharhKoli,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad
									end

									if @lngMablaghNaghdPardakht>0
									begin
									set @intRadif =@intRadif+1
									set @CodeMoinKham=substring(@CodeMoinSandoogh,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolSandoogh + @CodeMoinSandoogh
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolSandoogh,@CodeMoinSandoogh,@strMoinKamel,NUll,NULL,0,@lngMablaghNaghdPardakht,@intRadif,@dateSmall,@strSharhNaghd,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
									end
									--سند بانک
									if @lngMablaghBankPardakht>0
									begin
									set @intRadif =@intRadif+1

									set @CodeMoinKham=substring(@CodeMoinBank,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolBank + @CodeMoinBank
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolBank,@CodeMoinBank,@strMoinKamel,NUll,NULL,0,@lngMablaghBankPardakht,@intRadif,@dateSmall,@strSharhBank,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
									end
									--سند چک
									if @lngMablaghKolCheckHaPardakhti>0
									begin
									set @intRadif =@intRadif+1

									set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckHaPardakhti,@intRadif,@dateSmall,@strSharhCheck,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
									end
									--سند حسابداری واگذاری چک
									if @lngMablaghKolCheckMoshtari>0
									begin
									set @intRadif =@intRadif+1
									set @CodeMoinKham=substring(@CodeMoinCheckVagozari,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKolCheckVagozari + @CodeMoinCheckVagozari
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheckVagozari,@CodeMoinCheckVagozari,@strMoinKamel,@fldCodeTafsili,'1',0,@lngMablaghKolCheckMoshtari,@intRadif,@dateSmall,@strSharhCheckMoshtari,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;

									end

									if @lngMablaghTakhfifPardakht>0
									begin
									set @intRadif =@intRadif+1
									set @CodeMoinKham=substring(@CodeMoinTakhfifatePardakht,@TooleKol+1,@Toolemoin)
									set @strMoinKamel=@CodeKoltakhfifat + @CodeMoinTakhfifatePardakht
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKoltakhfifat,@CodeMoinTakhfifatePardakht,@strMoinKamel,NUll,NULL,0,@lngMablaghTakhfifPardakht,@intRadif,@dateSmall,@strSharhTakhfif,@day,@month,@year,'kharid',@fldCodeVasete,@fldShomareSanad;
									end
									if (@lngSumPardakht>0)
									begin
									set @ShomarePardakht=(select isnull(max(fldShomarePardakht),0) as fldMax from tblPardakht)
									set @ShomarePardakht=@ShomarePardakht+1
									INSERT INTO tblPardakht( fldShomarePardakht, fldShomareSanad, fldCodeTafzili,fldType)
									VALUES (@ShomarePardakht,@fldShomareSanad,@fldCodeTafsili,'kharid')

									--ثبت ریز اطلاعات چک
									Insert into tblCheckePardakhti (fldCodeArz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,fldCodeSahebeCheck,fldDatePardakht,fldShomarePardakht,fldVasete,fldSanad)
									Select @fldCodeArz,@fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,@fldCodeTafsili,@fldDate,@ShomarePardakht, @fldCodeVasete,@fldShomareSanad output from @tblCheck;

									--ثبت واگذاری چک
									UPDATE tblCheckeDaryafti Set fldShomarePardakht=@ShomarePardakht Where Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) in (Select Convert(nvarchar,fldShomareCheck) + Convert(nvarchar,fldShomareHesab) from @CheckeVagozari)

									end

									-----------------------factor--------------------------------------

									--execute GetMaxShomareFactorKharid @maxFactor output
									set @maxFactor=@fldShomareFactor

									INSERT INTO tblKharid ([fldShomareFactorKharid],[fldShomareSanad],[fldDate],[fldDateSarResid],[fldSharhFactor])
									values(@maxFactor,@fldShomareSanad,@fldDate,@fldDateSarResid,@fldSharhFactor)
									INSERT INTO tblRizeKharid([fldShomareSanad],[fldShomareFactor]
									,[fldCodeVasete],[fldCodeTafsili],[fldCodeUser],[fldRadif],[fldCodeAnbar]
									,[fldCodeKala],[fldSharhKala],[fldTedadCarton],[fldTedadJoz],[fldFeeJoz]
									,[fldFeeYekCarton],[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif]
									,[fldFeeVahed1BadAzTakhfif],[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode]
									,[fldFeeKolBadAzTakhfif],[fldMablaghKhales],[fldType],[fldDate],[fldTime]
									,[fldHamloNaghl],[fldGhymatMasrafkonande],[fldDateEngheza])
									select @fldShomareSanad,@maxFactor,[fldCodeVasete],[fldCodeTafsili]
									,[fldCodeUser],[fldRadif],[fldCodeAnbar],[fldCodeKala],[fldSharhKala]
									,[fldTedadCarton],[fldTedadJoz],[fldFeeJoz],[fldFeeYekCarton]
									,[fldFeeKol],[fldDarsadTakhfif],[fldMablaghTakhfif],[fldFeeVahed1BadAzTakhfif]
									,[fldFeeVahed2BadAzTakhfif],[fldArzeshAfzode],[fldFeeKolBadAzTakhfif]
									,[fldMablaghKhales],[fldType],[fldDate],[fldTime],[fldHamloNaghl]
									,[fldGhymatMasrafkonande],[fldDateEngheza]
									output from @dtKharid;
									set @strResult=convert(nvarchar(17),@maxFactor)
									set @strResult2='insert'
									set @flagInsert='yes_byNo'
						end
			

			end


			update tblSodooreSanad set fldColorBank=#tmpColor.fldColorBank,fldColorSandoogh=#tmpColor.fldColorSandoogh,fldColorSooratHesab=#tmpColor.fldColorSooratHesab from #tmpColor Where tblSodooreSanad.fldSanad=@fldShomareSanad and fldMoinKamel=#tmpColor.MoinKamel

			if @flagInsert='yes'
				begin
					update tblSodooreSanad
					--set fldSharh ='فاکتور خرید شماره ' + convert(nvarchar(17),@maxFactor)
					set fldSharh ='' 
					where fldSanad=@fldShomareSanad and fldSharh like 'none'
					update tblSodooreSanad
					--set fldSharh ='فاکتور خرید شماره ' + convert(nvarchar(17),@maxFactor) + ' - شماره پرداخت ' + convert(nvarchar(17),@ShomarePardakht)
					set fldSharh =''
					where fldSanad=@fldShomareSanad and fldSharh like 'nonePardakht'
				end
			else
				begin
					update tblSodooreSanad
					--set fldSharh ='فاکتور خرید شماره ' + convert(nvarchar(17),@fldShomarefactor)
					set fldSharh =''
					where fldSanad=@fldShomareSanad and fldSharh like 'none'

					update tblSodooreSanad
					--set fldSharh ='فاکتور خرید شماره ' + convert(nvarchar(17),@fldShomarefactor) + ' - شماره پرداخت ' + convert(nvarchar(17),@ShomarePardakht)
					set fldSharh =''
					where fldSanad=@fldShomareSanad and fldSharh like 'nonePardakht'
				end

			commit transaction AddRecord;
   end try
		begin catch
			if @@TranCount>0
				begin
					set @strResult='error'
					set @strResult2='error'
					rollback transaction AddRecord;
				end
		end catch


END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoMojodiEbtedaDoreh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertInfoMojodiEbtedaDoreh]
    @dtRizekharid tpRizeKharid_MojodiEbtedaDoreh Readonly,
	@strResult  nvarchar(25) output,
    @InsertType nvarchar(50),

	@fldCodeKol nvarchar(50),
	@fldCodeMoin nvarchar(50),
	@fldMoinKamel nvarchar(50),
	--@fldCodeTafzili nvarchar(50),
	--@fldCodeNoeTafzili nvarchar(50),
	@fldBedehkar decimal,
	@fldBestankar  decimal, 
	--@fldRadif nvarchar(50),
	@fldDate int, 
	@fldSharh nvarchar(100),
	@day nvarchar(50),
	@month nvarchar(50),
	@year nvarchar(50),
	@fldType nvarchar(20),
	@vasete nvarchar(10),
	@ShomareSanad nvarchar(50), 
	@fldCodeKolSarmaye nvarchar(50),
	@fldMoinKamelSarmaye nvarchar(50)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	   Begin Transaction InsertRow
	     declare @lngCountMojodi bigint
    -- Insert statements for procedure here
	      Begin Try
		          set @lngCountmojodi=(Select Count(fldShomareSanad) from tblRizeKharid Where  fldType='mojodi_ebteda_doreh')
			 if (@lngCountMojodi = 0)
			   Begin
		        if @InsertType='insert'
					begin
						INSERT INTO [dbo].[tblRizeKharid]
								([fldShomareSanad]
								,[fldShomareFactor]
								,[fldCodeVasete]
								,[fldCodeTafsili]
								,[fldCodeUser]
								,[fldRadif]
								,[fldCodeAnbar]
								,[fldCodeKala]
								,[fldSharhKala]
								,[fldTedadCarton]
								,[fldTedadJoz]
								,[fldFeeJoz]
								,[fldFeeYekCarton]
								,[fldFeeKol]
								,[fldDarsadTakhfif]
								,[fldMablaghTakhfif]
								,[fldFeeVahed1BadAzTakhfif]
								,[fldFeeVahed2BadAzTakhfif]
								,[fldArzeshAfzode]
								,[fldFeeKolBadAzTakhfif]
								,[fldMablaghKhales]
								,[fldType]
								,[fldDate]
								,[fldTime]
								,[fldShomareEnteghal])
								SELECT
								 [fldShomareSanad]
								,[fldShomareFactor]
								,[fldCodeVasete]
								,[fldCodeTafsili]
								,[fldCodeUser]
								,[fldRadif]
								,[fldCodeAnbar]
								,[fldCodeKala]
								,[fldSharhKala]
								,[fldTedadCarton]
								,[fldTedadJoz]
								,[fldFeeJoz]
								,[fldFeeYekCarton]
								,[fldFeeKol]
								,[fldDarsadTakhfif]
								,[fldMablaghTakhfif]
								,[fldFeeVahed1BadAzTakhfif]
								,[fldFeeVahed2BadAzTakhfif]
								,[fldArzeshAfzode]
								,[fldFeeKolBadAzTakhfif]
								,[fldMablaghKhales]
								,[fldType]
								,[fldDate]
								,[fldTime]
								,[fldShomareEnteghal]
								output from @dtRizekharid;

								INSERT INTO tblSodooreSanad
												(fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel,
													fldBedehkar, fldBestankar , fldRadif,fldDate ,
													fldSharh,fldday,fldmonth,fldyear,fldvasete,fldType,fldCodeTafzili)
						        values (@ShomareSanad,@fldCodeKol,@fldCodeMoin,@fldMoinKamel,
									    @fldBedehkar,0,'1',@fldDate,@fldSharh,@day,@month,@year,@vasete, @fldType,' ')

								
									INSERT INTO tblSodooreSanad                      --sarmaye
												(fldSanad,fldCodeKol,fldCodeMoin,fldMoinKamel,
													fldBedehkar,fldBestankar,fldRadif,fldDate, 
													fldSharh,fldday,fldmonth,fldyear,fldvasete,fldType,fldCodeTafzili)
									values (@ShomareSanad,@fldCodeKolSarmaye,'',@fldMoinKamelSarmaye,
											0,@fldBestankar,'2',@fldDate,@fldSharh,@day,@month,@year,@vasete,@fldType,' ')


							     set @strResult='insertOk'
                       End
                  End
                
				 if (@lngCountMojodi > 0)
			       Begin
				   Delete from tblSodooreSanad where fldSanad=@ShomareSanad And fldType='mojoodieebteda'
		           Delete from tblRizeKharid where fldShomareSanad=@ShomareSanad And fldType='mojodi_ebteda_doreh'
				      if @InsertType='insert'
					    begin
						INSERT INTO [dbo].[tblRizeKharid]
								([fldShomareSanad]
								,[fldShomareFactor]
								,[fldCodeVasete]
								,[fldCodeTafsili]
								,[fldCodeUser]
								,[fldRadif]
								,[fldCodeAnbar]
								,[fldCodeKala]
								,[fldSharhKala]
								,[fldTedadCarton]
								,[fldTedadJoz]
								,[fldFeeJoz]
								,[fldFeeYekCarton]
								,[fldFeeKol]
								,[fldDarsadTakhfif]
								,[fldMablaghTakhfif]
								,[fldFeeVahed1BadAzTakhfif]
								,[fldFeeVahed2BadAzTakhfif]
								,[fldArzeshAfzode]
								,[fldFeeKolBadAzTakhfif]
								,[fldMablaghKhales]
								,[fldType]
								,[fldDate]
								,[fldTime]
								,[fldShomareEnteghal])
								SELECT
								 [fldShomareSanad]
								,[fldShomareFactor]
								,[fldCodeVasete]
								,[fldCodeTafsili]
								,[fldCodeUser]
								,[fldRadif]
								,[fldCodeAnbar]
								,[fldCodeKala]
								,[fldSharhKala]
								,[fldTedadCarton]
								,[fldTedadJoz]
								,[fldFeeJoz]
								,[fldFeeYekCarton]
								,[fldFeeKol]
								,[fldDarsadTakhfif]
								,[fldMablaghTakhfif]
								,[fldFeeVahed1BadAzTakhfif]
								,[fldFeeVahed2BadAzTakhfif]
								,[fldArzeshAfzode]
								,[fldFeeKolBadAzTakhfif]
								,[fldMablaghKhales]
								,[fldType]
								,[fldDate]
								,[fldTime]
								,[fldShomareEnteghal]
								output from @dtRizekharid;

								INSERT INTO tblSodooreSanad
												(fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel,
													fldBedehkar, fldBestankar , fldRadif,fldDate ,
													fldSharh,fldday,fldmonth,fldyear,fldvasete,fldType,fldCodeTafzili)
						        values (@ShomareSanad,@fldCodeKol,@fldCodeMoin,@fldMoinKamel,
									    @fldBedehkar,0,'1',@fldDate,@fldSharh,@day,@month,@year,@vasete, @fldType,' ')

								
									INSERT INTO tblSodooreSanad                      --sarmaye
												(fldSanad,fldCodeKol,fldCodeMoin,fldMoinKamel,
													fldBedehkar,fldBestankar,fldRadif,fldDate, 
													fldSharh,fldday,fldmonth,fldyear,fldvasete,fldType,fldCodeTafzili)
									values (@ShomareSanad,@fldCodeKolSarmaye,'',@fldMoinKamelSarmaye,
											0,@fldBestankar,'2',@fldDate,@fldSharh,@day,@month,@year,@vasete,@fldType,' ')


							     set @strResult='UpdateArchiveOk'
                       End
				   End

		     Commit Transaction InsertRow; 
		   End Try

			 Begin Catch
			   if @@TRANCOUNT > 0 
			     begin 
				  RollBack Transaction InsertRow;
				     set @strResult='Error'
				 end

			  End Catch

END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoMojodiEbtedaDorehNew]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertInfoMojodiEbtedaDorehNew]
    @dtRizekharid tpRizeKharid_MojodiEbtedaDoreh Readonly,
	@strResult  nvarchar(25) output,
    @InsertType nvarchar(50),

	@fldCodeKol nvarchar(50),
	@fldCodeMoin nvarchar(50),
	@fldMoinKamel nvarchar(50),
	--@fldCodeTafzili nvarchar(50),
	--@fldCodeNoeTafzili nvarchar(50),
	@fldBedehkar decimal,
	@fldBestankar  decimal, 
	--@fldRadif nvarchar(50),
	@fldDate int, 
	@fldSharh nvarchar(100),
	@day nvarchar(50),
	@month nvarchar(50),
	@year nvarchar(50),
	@fldType nvarchar(20),
	@vasete nvarchar(10),
	@ShomareSanad nvarchar(50), 
	@fldCodeKolSarmaye nvarchar(50),
	@fldMoinKamelSarmaye nvarchar(50)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	   Begin Transaction InsertRow
	     declare @lngCountMojodi bigint
    -- Insert statements for procedure here
	      Begin Try			
				set @ShomareSanad=(Select top 1(fldSanad)  from tblSodooreSanad Where fldType='mojoodieebteda')
				if isnumeric(@ShomareSanad )=0
					begin

						set @ShomareSanad =(Select Max(Convert(bigint,fldSanad)) from tblSodooreSanad)
						if ISNUMERIC(@ShomareSanad )=1
							set @ShomareSanad=@ShomareSanad + 1
						else
							set @ShomareSanad=1

					end 
					
		     set @lngCountmojodi=(Select Count(fldShomareSanad) from tblRizeKharid Where  fldType='mojodi_ebteda_doreh')
			 if (@lngCountMojodi = 0)
			   Begin
		        if @InsertType='insert'
					begin
						--INSERT INTO [dbo].[tblRizeKharid]
						--		([fldShomareSanad]
						--		,[fldShomareFactor]
						--		,[fldCodeVasete]
						--		,[fldCodeTafsili]
						--		,[fldCodeUser]
						--		,[fldRadif]
						--		,[fldCodeAnbar]
						--		,[fldCodeKala]
						--		,[fldSharhKala]
						--		,[fldTedadCarton]
						--		,[fldTedadJoz]
						--		,[fldFeeJoz]
						--		,[fldFeeYekCarton]
						--		,[fldFeeKol]
						--		,[fldDarsadTakhfif]
						--		,[fldMablaghTakhfif]
						--		,[fldFeeVahed1BadAzTakhfif]
						--		,[fldFeeVahed2BadAzTakhfif]
						--		,[fldArzeshAfzode]
						--		,[fldFeeKolBadAzTakhfif]
						--		,[fldMablaghKhales]
						--		,[fldType]
						--		,[fldDate]
						--		,[fldTime]
						--		,[fldShomareEnteghal])
						--		SELECT
						--		 [fldShomareSanad]
						--		,[fldShomareFactor]
						--		,[fldCodeVasete]
						--		,[fldCodeTafsili]
						--		,[fldCodeUser]
						--		,[fldRadif]
						--		,[fldCodeAnbar]
						--		,[fldCodeKala]
						--		,[fldSharhKala]
						--		,[fldTedadCarton]
						--		,[fldTedadJoz]
						--		,[fldFeeJoz]
						--		,[fldFeeYekCarton]
						--		,[fldFeeKol]
						--		,[fldDarsadTakhfif]
						--		,[fldMablaghTakhfif]
						--		,[fldFeeVahed1BadAzTakhfif]
						--		,[fldFeeVahed2BadAzTakhfif]
						--		,[fldArzeshAfzode]
						--		,[fldFeeKolBadAzTakhfif]
						--		,[fldMablaghKhales]
						--		,[fldType]
						--		,[fldDate]
						--		,[fldTime]
						--		,[fldShomareEnteghal]
						--		output from @dtRizekharid;


								INSERT INTO tblSodooreSanad
												(fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel,
													fldBedehkar, fldBestankar , fldRadif,fldDate ,
													fldSharh,fldday,fldmonth,fldyear,fldvasete,fldType,fldCodeTafzili)
						        values (@ShomareSanad,@fldCodeKol,@fldCodeMoin,@fldMoinKamel,
									    @fldBedehkar,0,'1',@fldDate,@fldSharh,@day,@month,@year,@vasete, @fldType,' ')


									INSERT INTO tblSodooreSanad                      --sarmaye
												(fldSanad,fldCodeKol,fldCodeMoin,fldMoinKamel,
													fldBedehkar,fldBestankar,fldRadif,fldDate, 
													fldSharh,fldday,fldmonth,fldyear,fldvasete,fldType,fldCodeTafzili)
									values (@ShomareSanad,@fldCodeKolSarmaye,'',@fldMoinKamelSarmaye,
											0,@fldBestankar,'2',@fldDate,@fldSharh,@day,@month,@year,@vasete,@fldType,' ')


							     set @strResult='insertOk'
                       End
                  End
                
				 if (@lngCountMojodi > 0)
			       Begin

				   Delete from tblSodooreSanad where fldSanad=@ShomareSanad And fldType='mojoodieebteda'

		           --Delete from tblRizeKharid where fldShomareSanad=@ShomareSanad And fldType='mojodi_ebteda_doreh'
				      if @InsertType='insert'
					    begin
						--INSERT INTO [dbo].[tblRizeKharid]
						--		([fldShomareSanad]
						--		,[fldShomareFactor]
						--		,[fldCodeVasete]
						--		,[fldCodeTafsili]
						--		,[fldCodeUser]
						--		,[fldRadif]
						--		,[fldCodeAnbar]
						--		,[fldCodeKala]
						--		,[fldSharhKala]
						--		,[fldTedadCarton]
						--		,[fldTedadJoz]
						--		,[fldFeeJoz]
						--		,[fldFeeYekCarton]
						--		,[fldFeeKol]
						--		,[fldDarsadTakhfif]
						--		,[fldMablaghTakhfif]
						--		,[fldFeeVahed1BadAzTakhfif]
						--		,[fldFeeVahed2BadAzTakhfif]
						--		,[fldArzeshAfzode]
						--		,[fldFeeKolBadAzTakhfif]
						--		,[fldMablaghKhales]
						--		,[fldType]
						--		,[fldDate]
						--		,[fldTime]
						--		,[fldShomareEnteghal])
						--		SELECT
						--		 [fldShomareSanad]
						--		,[fldShomareFactor]
						--		,[fldCodeVasete]
						--		,[fldCodeTafsili]
						--		,[fldCodeUser]
						--		,[fldRadif]
						--		,[fldCodeAnbar]
						--		,[fldCodeKala]
						--		,[fldSharhKala]
						--		,[fldTedadCarton]
						--		,[fldTedadJoz]
						--		,[fldFeeJoz]
						--		,[fldFeeYekCarton]
						--		,[fldFeeKol]
						--		,[fldDarsadTakhfif]
						--		,[fldMablaghTakhfif]
						--		,[fldFeeVahed1BadAzTakhfif]
						--		,[fldFeeVahed2BadAzTakhfif]
						--		,[fldArzeshAfzode]
						--		,[fldFeeKolBadAzTakhfif]
						--		,[fldMablaghKhales]
						--		,[fldType]
						--		,[fldDate]
						--		,[fldTime]
						--		,[fldShomareEnteghal]
						--		output from @dtRizekharid;
								INSERT INTO tblSodooreSanad
												(fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel,
													fldBedehkar, fldBestankar , fldRadif,fldDate ,
													fldSharh,fldday,fldmonth,fldyear,fldvasete,fldType,fldCodeTafzili)
						        values (@ShomareSanad,@fldCodeKol,@fldCodeMoin,@fldMoinKamel,
									    @fldBedehkar,0,'1',@fldDate,@fldSharh,@day,@month,@year,@vasete, @fldType,' ')

									INSERT INTO tblSodooreSanad                      --sarmaye
												(fldSanad,fldCodeKol,fldCodeMoin,fldMoinKamel,
													fldBedehkar,fldBestankar,fldRadif,fldDate, 
													fldSharh,fldday,fldmonth,fldyear,fldvasete,fldType,fldCodeTafzili)
									values (@ShomareSanad,@fldCodeKolSarmaye,'',@fldMoinKamelSarmaye,
											0,@fldBestankar,'2',@fldDate,@fldSharh,@day,@month,@year,@vasete,@fldType,' ')

							     set @strResult='UpdateArchiveOk'
                       End
				   End

		     Commit Transaction InsertRow; 
		   End Try

			 Begin Catch
			   if @@TRANCOUNT > 0 
			     begin 
				  RollBack Transaction InsertRow;
				     set @strResult='Error'
				 end

			  End Catch

END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoPaseCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertInfoPaseCheck]
--چندزبانه
	@fldCodeArz int,
	@fldNerkheArz decimal(18,4),
	@language nvarchar(10),
	@fldShomarePas bigint,
	@fldSanadPas bigint,
	@strResult nvarchar(25) output,
	@InsertType nvarchar(50)  ,	
	@Date bigint,
	@tblCheck [dtCheckPassMain-multi] readonly

AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	begin transaction AddRecord
			declare @lngCountShomarePas bigint,@EmpId bigint,@intRadif bigint,@strRadif nvarchar(50),@CodeMoinKham nvarchar(10),@TooleKol bigint,@TooleMoin bigint,@CodeMoinCheck nvarchar(10),@CodeKolCheck nvarchar(10),@strMoinKamel nvarchar(50),@MablaghCheck decimal(18,4),@smallDate nvarchar(15),@strSharheCheck nvarchar(50),@day nvarchar(50),@month nvarchar(50),@year nvarchar(50),@strCodeTafzili nvarchar(50),@CodeNoe nvarchar(10),@CodeVasete nvarchar(50)
			declare @lngCountShomareSanad bigint,@strMoinKamelBank nvarchar(50),@CodeKolBank nvarchar(50),@CodeMoinBank nvarchar(50),@ShomareCheck nvarchar(50) ,@Sharh  nvarchar(70)--,@NoeMohasebePoorsant int,@DarsadePoorsant int,@MablaghePoorsant bigint
			declare @bigShomareSanad bigint
			begin try
				 set @lngCountShomarePas=(select COUNT(fldShomarePas) from tblPas where fldShomarePas=@fldShomarePas) 
				 set @lngCountShomareSanad=(select COUNT(convert(bigint,fldSanad)) from tblSodooreSanad where convert(bigint,fldSanad)=@fldSanadPas)

				 --###############################################################################################
				  if @lngCountShomarePas > 0  --یعنی در حالت اینسرت هستیم و شماره رو زودتر یک نفر ذخیره کرد
				  	 Begin					 
						if @InsertType='insert'
							begin
							set @strResult='error_tekrari_insert'
								rollback transaction 
								return
							end --End Insert
				      end --@lngCoun

				 --###############################################################################################
				if @InsertType='arshive'
					begin
						UPDATE       tblCheckePardakhti
							SET         																					
										fldShomarePass=NULL,
										fldDatePass =NULL,
										fldSharhePass =NULL,
										fldSanadPas =NUll
																	
								where fldShomarePass=@fldShomarePas

							delete from tblSodooreSanad Where fldSanad=@fldSanadPas
							delete from tblPas Where fldShomarePas=@fldShomarePas

							set @bigShomareSanad=@fldSanadPas
							
					end

				 --حالت اینسرت و شماره آزاده
				if @InsertType='insert'
					begin
						
						IF (@lngCountShomareSanad=0) 
							set @bigShomareSanad =@fldSanadPas
						else
							begin
								declare @MaxSanad bigint
								set @MaxSanad=(select max(convert(bigint,fldSanad)) from tblSodooreSanad)
								set @MaxSanad=@MaxSanad+1
								set @bigShomareSanad =@MaxSanad
							end
					 	
							--update tblCheckePardakhti
							--set fldShomareCheck=mm.fldShomareCheck ,
							--	fldShomareHesab=mm.fldShomareHesab,
							--	@fldShomarePas=@fldShomarePas
							--	from @tblCheck as mm
							--	where fldShomareCheck=mm.fldShomareCheck and fldShomareHesab=mm.fldShomareHesab

						

								--UPDATE       tblCheckePardakhti
								--	SET                fldShomareCheck = mm.fldShomareCheck
								--	FROM            @tblCheck as mm CROSS JOIN
								--							 tblCheckePardakhti
						

								--from @dtCheckPas
								--where fldShomareCheck=@dtCheckPas.fldShomareCheck and fldShomareHesab=@dtCheckPas.fldShomareHesab
						
						--update tblCheckePardakhti
						--		set fldShomarePass=@fldShomarePas,
						--		fldSharhePass=@tblCheck.fldTozihat,
						--		fldSanad=@bigShomareSanad
						--	from @tblCheck 
						--		where fldShomareCheck=@tblCheck.fldShomareCheck and fldShomareHesab=@tblCheck.fldShomareHesab


									
							--select * from @tblCheck
									--FROM tblCheckeDaryafti  INNER JOIN @dtCheckPas as P
										--ON tblCheckePardakhti.fldShomareCheck = p.fldShomareCheck
					end


					UPDATE       tblCheckePardakhti
							SET         fldShomareCheck = mm.fldShomareCheck,										
										fldShomarePass=@fldShomarePas,
										fldSanadPas =@bigShomareSanad,
										fldDatePass=@Date,
										fldSharhePass=mm.fldTozihat

							FROM            @tblCheck AS mm INNER JOIN
													 tblCheckePardakhti ON mm.fldShomareCheck = tblCheckePardakhti.fldShomareCheck
			


			declare EmpCursor Cursor For
				SELECT ID  FROM @tblCheck
			--Select Id from tblSodooreSanad With (NoLock) where fldCodeTafzili='1000' ORDER BY CONVERT(dec, fldSanad), CONVERT(dec, fldRadif)
				open EmpCursor
				fetch Next From EmpCursor into @EmpId
				While @@Fetch_Status = 0
					Begin	

						--INSERT INTO dbo.tblTest5
						--	(name1, name2, name3)
						--	VALUES        ('','',Convert(varchar,@EmpId))

							execute  ToolePaye 'kol',@TooleKol output
							execute  ToolePaye 'moin',@Toolemoin output

							set @intRadif =@intRadif+1			
							set @strRadif=convert(nvarchar(50),@intRadif)							
							set @CodeMoinKham=substring(@CodeMoinCheck,@TooleKol+1,@Toolemoin)

							execute codemoinhesab 'checkpardakhti' ,@strMoinKamel output

							set @CodeKolCheck =substring(@strMoinKamel, 1, @TooleKol)
							set @CodeMoinCheck =substring(@strMoinKamel,@TooleKol+1,@Toolemoin)

							set @strMoinKamel=@CodeKolCheck + @CodeMoinCheck
							set @MablaghCheck = (Select fldmablagh from @tblCheck  Where id=@EmpId)
							set @strSharheCheck=(Select fldTozihat from @tblCheck  Where id=@EmpId)
							set @smallDate=@Date--SUBSTRING(CONVERT(nvarchar, @Date), 3, 6)
							set @day=SUBSTRING(CONVERT(nvarchar, @Date), 7, 2)
							set @month=SUBSTRING(CONVERT(nvarchar, @Date), 5, 2)
							set @year=SUBSTRING(CONVERT(nvarchar, @Date), 1, 4)

							set @strCodeTafzili =(Select fldCodeSahebeCheck from @tblCheck Where id=@EmpId)
							set @CodeNoe =(Select fldCodeNoe from tblTafzili Where fldCodeTafzili=@strCodeTafzili)
							set @CodeVasete = (Select fldVasete from tblCheckePardakhti Where id=@EmpId)


							set @strMoinKamelBank = (Select fldCodeBank from tblCheckePardakhti Where id =@EmpId)
							set @ShomareCheck= (Select fldShomareCheck from tblCheckePardakhti Where id =@EmpId)
							set @Sharh = (Select * from dbo.translatefunction(@LANGUAGE,18270)) + ' ' + @ShomareCheck

							set @CodeKolBank =substring(@strMoinKamelBank, 1, @TooleKol)
							set @CodeMoinBank =substring(@strMoinKamelBank,@TooleKol+1,@Toolemoin)
							--if @intRadif=1
							--	begin
									--if @InsertType='archive' --یعنی این شماره سند که از سمت کاربر اومده تکراریه
									--	begin
											--execute InsertSanadfirstMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,NUll,NULL,0,@MablaghCheck,@strRadif,strsmalDate ,@strSharheCheck,@day,@month,@year,'pardakht',@CodeVasete,@strShomareSanad;

							--INSERT INTO dbo.tblTest5
							--	(name1, name2, name3)
							--	VALUES        (@CodeKolCheck,@CodeMoinCheck,@strMoinKamel)

							--execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,NUll,NULL,0,@MablaghCheck,@strRadif,strsmalDate ,@strSharheCheck,01,01,96,'pas',0,105;
							if @InsertType='insert' and @intRadif=1
								begin
									execute InsertSanadfirstMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@strCodeTafzili,@CodeNoe,@MablaghCheck,0,@intRadif,@smallDate,@strSharheCheck,@day,@month,@year,'pasecheck',@CodeVasete,@bigShomareSanad output;
									set @intRadif=2
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolBank,@CodeMoinBank,@strMoinKamelBank,NULL,NULL,0,@MablaghCheck,@intRadif,@smallDate,@Sharh,@day,@month,@year,'pasecheck',@CodeVasete,@bigShomareSanad;
								end
							else
								begin
									set @intRadif = @intRadif +1
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@strCodeTafzili,@CodeNoe,@MablaghCheck,0,@intRadif,@smallDate,@strSharheCheck,@day,@month,@year,'pasecheck',@CodeVasete,@bigShomareSanad;
									set @intRadif = @intRadif +1
									execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolBank,@CodeMoinBank,@strMoinKamelBank,NULL,NULL,0,@MablaghCheck,@intRadif,@smallDate,@Sharh,@day,@month,@year,'pasecheck',@CodeVasete,@bigShomareSanad;
								end

							--set @NoeMohasebePoorsant = (Select fldNoeMohasebePoorsant from tblTafzili Where fldCodeTafzili=@strCodeTafzili)
							--if isnumeric(@NoeMohasebePoorsant )=1
							--	begin
							--		set @DarsadePoorsant=(Select fldPoorsant from tblTafzili Where fldCodeTafzili=@strCodeTafzili)
							--		if @DarsadePoorsant>0  and @NoeMohasebePoorsant =2  --محاسبه براساس واریز
							--			begin
							--				set @MablaghePoorsant = @MablaghCheck * @DarsadePoorsant / 100

							--			end
							--	end

							--			end
							--		else
							--			begin
							--				execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',0,@MablaghCheck,@strRadif,@DatePardakht,@SCheck,@day,@month,@year,'pardakht',@CodeVasete,@ShomareSanadKarbar;
							--				set @strShomareSanad=@ShomareSanadKarbar
							--			end
							--	end
							--else
							--	begin
							--		execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolCheck,@CodeMoinCheck,@strMoinKamel,@CodeTafzili,'1',0,@MablaghCheck,@strRadif,@DatePardakht,@SCheck,@day,@month,@year,'pardakht',@CodeVasete,@strShomareSanad;
							--	end

						
						Fetch Next from EmpCursor
						into @EmpId
					end
			Close EmpCurSor
			Deallocate EmpCursor


			insert into tblPas
				(fldshomarepas,fldShomareSanad)
				values
				(@fldShomarePas,@bigShomareSanad)
			
			commit transaction AddRecord;
           end try
		begin catch
			if @@TranCount > 0
				begin
					set @strResult='error'
					
					rollback transaction AddRecord;
				end
		end catch
END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertInfoPishForoosh]
	@flagForoosh nvarchar(10),
	@MultiFee decimal(18,4),
	@fldCodeTafsili bigint,

	@fldCodeVasete nvarchar(10),
	@lngMablaghPorsant decimal(18,4),

	@fldCodeSuperVizor nvarchar(10),
	@lngMablaghPorsantSuperVizor decimal(18,4),

    @fldShomareFactor bigint,
    --@fldShomareSanad bigint,
    @fldDate bigint,
	@fldSharhFactor nvarchar(250),
	@dtForoosh [tpRizePishForoosh-Multi]  readonly,
	@dtCheckDaryafti [tblCheckDaryafti-Multi] readonly,
	@lngHazineHamlVaNaghl decimal(18,4),
	@lngArzeshAfzode decimal(18,4),
	@lngMablaghKolFactor decimal(18,4),
	@lngMablaghKolCheckHa decimal(18,4),
	@intHasRowCheckDaryafti int,
	@lngMablaghNaghdDaryaft decimal(18,4),
    @lngMablaghBankDaryaft decimal(18,4),
	@lngMablaghTakhfifDaryaft decimal(18,4),
	@lngCodeSandoghDaryaft bigint,
	@lngCodeBankDaryaft bigint,
	--شرح دریافت
	@strSharhNaghd nvarchar(50), 
	@strSharhBank nvarchar(50),
	@strSharhCheck nvarchar(50),
	@strSharhTakhfif nvarchar(50),
	@strSharhKoli nvarchar(50),
	@strResult nvarchar(25) output,
	@strResult2 nvarchar(25) output,
	@flagInsert nvarchar(10)
AS
BEGIN

	begin transaction AddRecord
		begin try
			declare @intRadif int,@CodeMoinHesabhayeDaryaftani nvarchar(100),@CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@TooleKol bigint,@Toolemoin bigint, @lngsum decimal(18,4)
			declare @day nvarchar(4),@Month nvarchar(4),@year nvarchar(5),@ShomareDaryaft bigint,@yearFull nvarchar(5),@dateSmall bigint
			declare @CodeMoinHoghoogh nvarchar(10),@CodeMoinHesabhayePardakhtani nvarchar(10)

			--set @year=substring(convert(nvarchar(20),@fldDate),3,2)
			set @yearFull=substring(convert(nvarchar(20),@fldDate),0,5)
			SEt @year=@yearFull
			set @month=substring(convert(nvarchar(20),@fldDate),5,2)
			set @day=substring(convert(nvarchar(20),@fldDate),7,2)
			--set @dateSmall=substring(convert(nvarchar(15),@fldDate),3,6)
			set @datesmall=@flddate
			declare @lngSumDaryaft decimal(18,4)
			set @intRadif=0
		
			if @fldSharhFactor=''
					set @fldSharhFactor='none'

			if @flagForoosh='arshive'
				begin
					--arishive
					--update(delete and insert)
					--delete
					delete from tblRizePishForoosh
						where fldShomareFactor =@fldShomareFactor

					delete from tblPishForoosh
						where fldShomareFactorForoosh =@fldShomareFactor

							INSERT INTO tblPishForoosh
							   ([fldShomareFactorForoosh]							   
							   ,[fldDate]
							   ,[fldSharhFactor]
							   ,[fldCodeFeeDynamic]
							   )
								 values(@fldShomareFactor,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end,@MultiFee )
								 
							 INSERT INTO tblRizePishForoosh						   
						    ([fldShomareFactor]
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldEshantion]
						   ,[fldTime])
							select
							@fldShomareFactor
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldEshantion]
						   ,[fldTime] output  from @dtForoosh;






					set @strResult='update'
					set @strResult2='update'
					
					--commit transaction AddRecord;
					--return
				end
				--تا اینجا تفصیلی ها به روز  شدند
			else if @flagForoosh='insert'
				begin
				--insert
						
					declare @resultExist bigint
					execute ExistFactorPishForoosh @fldShomareFactor,@resultExist output
					declare @maxFactor bigint
					execute GetMaxShomareFactorPishForoosh @maxFactor output
					set @maxFactor=@maxFactor+1
						
					if(@resultExist>0)
						begin
						--کاربر دیگری در همین لحظه شماره فاکتور را گرفت
							
							set @strResult=convert(nvarchar(17),@maxFactor)
							set @strResult2='New_Enter'
							rollback transaction AddRecord;
							return
							--در قسمت ویندوزی به کاربر کادری را نشان دهد که max+1 می باشد
							--yes
								--insert
							--no
								--inputbox در برنامه ویندوز که کاربر خودش شماره فاکتور را مشخص کند
								--باید اینجا چکار کنم؟؟؟؟
						end 
					--ehsan added
					 else if(@resultExist=0 and @flagInsert='yes')
					 --زمانی که کاربر از کادر پیام رویه دکمه بله کلیک میکنه
					     begin
						
							
							INSERT INTO tblPishForoosh
							   ([fldShomareFactorForoosh]							   
							   ,[fldDate]
							   ,[fldSharhFactor]
							   ,[fldCodeFeeDynamic]
							   )							   
								 values(@maxFactor,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end ,@MultiFee)

							 INSERT INTO tblRizePishForoosh						   
						    ([fldShomareFactor]
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldEshantion]
						   ,[fldTime])
							select 
						    @maxFactor
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldEshantion]
						   ,[fldTime] output  from @dtForoosh;
							set @strResult=convert(nvarchar(17),@maxFactor)
							set @strResult2='insert'
							commit transaction AddRecord;
						end
					else if(@resultExist=0 and @flagInsert='no')
						 begin
						
							
							INSERT INTO tblPishForoosh
							   ([fldShomareFactorForoosh]							   
							   ,[fldDate]
							   ,[fldSharhFactor]
							   ,[fldCodeFeeDynamic]
							   )
								 values(@fldShomareFactor,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end,@MultiFee )

							 INSERT INTO tblRizePishForoosh
													   ([fldShomareFactor]
													   ,[fldCodeVasete]
													   ,[fldCodeTafsili]
													   ,[fldCodeUser]
													   ,[fldRadif]
													   ,[fldCodeAnbar]
													   ,[fldCodeKala]
													   ,[fldSharhKala]
													   ,[fldTedadCarton]
													   ,[fldTedadJoz]
													   ,[fldFeeJoz]
													   ,[fldFeeYekCarton]
													   ,[fldFeeKol]
													   ,[fldDarsadTakhfif]
													   ,[fldMablaghTakhfif]
													   ,[fldFeeVahed1BadAzTakhfif]
													   ,[fldFeeVahed2BadAzTakhfif]
													   ,[fldArzeshAfzode]
													   ,[fldFeeKolBadAzTakhfif]
													   ,[fldMablaghKhales]
													   ,[fldType]
													   ,[fldDate]
													   ,[fldEshantion]
													   ,[fldTime])
														select 
														--edit
													    @fldShomareFactor
													   ,[fldCodeVasete]
													   ,[fldCodeTafsili]
													   ,[fldCodeUser]
													   ,[fldRadif]
													   ,[fldCodeAnbar]
													   ,[fldCodeKala]
													   ,[fldSharhKala]
													   ,[fldTedadCarton]
													   ,[fldTedadJoz]
													   ,[fldFeeJoz]
													   ,[fldFeeYekCarton]
													   ,[fldFeeKol]
													   ,[fldDarsadTakhfif]
													   ,[fldMablaghTakhfif]
													   ,[fldFeeVahed1BadAzTakhfif]
													   ,[fldFeeVahed2BadAzTakhfif]
													   ,[fldArzeshAfzode]
													   ,[fldFeeKolBadAzTakhfif]
													   ,[fldMablaghKhales]
													   ,[fldType]
													   ,[fldDate]
													   ,[fldEshantion]
													   ,[fldTime] output  from @dtForoosh;
							set @strResult=convert(nvarchar(17),@fldShomareFactor)
							set @strResult2='insertByNo'
							--commit transaction AddRecord
						end
						
				end
		


				commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				set @strResult2='error'
				rollback transaction AddRecord;
			end
		end catch
END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoPishPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertInfoPishPishForoosh]
--چندزبانه
	@flagForoosh nvarchar(10),
	@fldCodeTafsili bigint,

	@fldCodeVasete nvarchar(10),
	@lngMablaghPorsant decimal(18,4),

	@fldCodeSuperVizor nvarchar(10),
	@lngMablaghPorsantSuperVizor decimal(18,4),

    @fldShomareFactor bigint,
    --@fldShomareSanad bigint,
    @fldDate bigint,
	@fldSharhFactor nvarchar(250),
	@dtForoosh [tpRizeForoosh-multi]  readonly,
	@dtCheckDaryafti [tblCheckDaryafti-Multi] readonly,
	@lngHazineHamlVaNaghl decimal(18,4),
	@lngArzeshAfzode decimal(18,4),
	@lngMablaghKolFactor decimal(18,4),
	@lngMablaghKolCheckHa decimal(18,4),
	@intHasRowCheckDaryafti int,
	@lngMablaghNaghdDaryaft decimal(18,4),
    @lngMablaghBankDaryaft decimal(18,4),
	@lngMablaghTakhfifDaryaft decimal(18,4),
	@lngCodeSandoghDaryaft bigint,
	@lngCodeBankDaryaft bigint,
	--شرح دریافت
	@strSharhNaghd nvarchar(50), 
	@strSharhBank nvarchar(50),
	@strSharhCheck nvarchar(50),
	@strSharhTakhfif nvarchar(50),
	@strSharhKoli nvarchar(50),
	@strResult nvarchar(25) output,
	@strResult2 nvarchar(25) output,
	@flagInsert nvarchar(10)
AS
BEGIN

	begin transaction AddRecord
		begin try
			declare @intRadif int,@CodeMoinHesabhayeDaryaftani nvarchar(100),@CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10),@TooleKol bigint,@Toolemoin bigint, @lngsum decimal(18,4)
			declare @day nvarchar(4),@Month nvarchar(4),@year nvarchar(5),@ShomareDaryaft bigint,@yearFull nvarchar(5),@dateSmall bigint
			declare @CodeMoinHoghoogh nvarchar(10),@CodeMoinHesabhayePardakhtani nvarchar(10)
			
			set @yearFull=substring(convert(nvarchar(20),@fldDate),0,5)
			set @year=@yearFull
			set @month=substring(convert(nvarchar(20),@fldDate),5,2)
			set @day=substring(convert(nvarchar(20),@fldDate),7,2)
			set @dateSmall=@fldDate --substring(convert(nvarchar(15),@fldDate),3,6)
			declare @lngSumDaryaft decimal(18,4)
			set @intRadif=0
		
			if @fldSharhFactor=''
					set @fldSharhFactor='none'

			if @flagForoosh='arshive'
				begin
					--arishive
					--update(delete and insert)
					--delete
					delete from tblRizePishPishForoosh
						where fldShomareFactor =@fldShomareFactor

					delete from tblPishPishForoosh
						where fldShomareFactorForoosh =@fldShomareFactor

							INSERT INTO tblPishPishForoosh
							   ([fldShomareFactorForoosh]							   
							   ,[fldDate]
							   ,[fldSharhFactor]
							   )
								 values(@fldShomareFactor,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end )
								 
							 INSERT INTO tblRizePishPishForoosh						   
						    ([fldShomareFactor]
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime])
							select
							@fldShomareFactor
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime] output  from @dtForoosh;






					set @strResult='update'
					set @strResult2='update'
					
					--commit transaction AddRecord;
					--return
				end
				--تا اینجا تفصیلی ها به روز  شدند
			else if @flagForoosh='insert'
				begin
				--insert
						
					declare @resultExist bigint
					execute ExistFactorPishPishForoosh @fldShomareFactor,@resultExist output
					declare @maxFactor bigint
					execute GetMaxShomareFactorPishPishForoosh @maxFactor output
					set @maxFactor=@maxFactor+1
						
					if(@resultExist>0)
						begin
						--کاربر دیگری در همین لحظه شماره فاکتور را گرفت
							
							set @strResult=convert(nvarchar(17),@maxFactor)
							set @strResult2='New_Enter'
							rollback transaction AddRecord;
							return
							--در قسمت ویندوزی به کاربر کادری را نشان دهد که max+1 می باشد
							--yes
								--insert
							--no
								--inputbox در برنامه ویندوز که کاربر خودش شماره فاکتور را مشخص کند
								--باید اینجا چکار کنم؟؟؟؟
						end 
					--ehsan added
					 else if(@resultExist=0 and @flagInsert='yes')
					 --زمانی که کاربر از کادر پیام رویه دکمه بله کلیک میکنه
					     begin
						
							
							INSERT INTO tblPishPishForoosh
							   ([fldShomareFactorForoosh]							   
							   ,[fldDate]
							   ,[fldSharhFactor]
							   )							   
								 values(@maxFactor,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end )

							 INSERT INTO tblRizePishPishForoosh						   
						    ([fldShomareFactor]
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime])
							select 
						    @maxFactor
						   ,[fldCodeVasete]
						   ,[fldCodeTafsili]
						   ,[fldCodeUser]
						   ,[fldRadif]
						   ,[fldCodeAnbar]
						   ,[fldCodeKala]
						   ,[fldSharhKala]
						   ,[fldTedadCarton]
						   ,[fldTedadJoz]
						   ,[fldFeeJoz]
						   ,[fldFeeYekCarton]
						   ,[fldFeeKol]
						   ,[fldDarsadTakhfif]
						   ,[fldMablaghTakhfif]
						   ,[fldFeeVahed1BadAzTakhfif]
						   ,[fldFeeVahed2BadAzTakhfif]
						   ,[fldArzeshAfzode]
						   ,[fldFeeKolBadAzTakhfif]
						   ,[fldMablaghKhales]
						   ,[fldType]
						   ,[fldDate]
						   ,[fldTime] output  from @dtForoosh;
							set @strResult=convert(nvarchar(17),@maxFactor)
							set @strResult2='insert'
							commit transaction AddRecord;
						end
					else if(@resultExist=0 and @flagInsert='no')
						 begin
						
							
							INSERT INTO tblPishPishForoosh
							   ([fldShomareFactorForoosh]							   
							   ,[fldDate]
							   ,[fldSharhFactor]
							   )
								 values(@fldShomareFactor,@fldDate,case when @fldSharhFactor='none' then '' else @fldSharhFactor end )

							 INSERT INTO tblRizePishPishForoosh
													   ([fldShomareFactor]
													   ,[fldCodeVasete]
													   ,[fldCodeTafsili]
													   ,[fldCodeUser]
													   ,[fldRadif]
													   ,[fldCodeAnbar]
													   ,[fldCodeKala]
													   ,[fldSharhKala]
													   ,[fldTedadCarton]
													   ,[fldTedadJoz]
													   ,[fldFeeJoz]
													   ,[fldFeeYekCarton]
													   ,[fldFeeKol]
													   ,[fldDarsadTakhfif]
													   ,[fldMablaghTakhfif]
													   ,[fldFeeVahed1BadAzTakhfif]
													   ,[fldFeeVahed2BadAzTakhfif]
													   ,[fldArzeshAfzode]
													   ,[fldFeeKolBadAzTakhfif]
													   ,[fldMablaghKhales]
													   ,[fldType]
													   ,[fldDate]
													   ,[fldTime])
														select 
														--edit
													    @fldShomareFactor
													   ,[fldCodeVasete]
													   ,[fldCodeTafsili]
													   ,[fldCodeUser]
													   ,[fldRadif]
													   ,[fldCodeAnbar]
													   ,[fldCodeKala]
													   ,[fldSharhKala]
													   ,[fldTedadCarton]
													   ,[fldTedadJoz]
													   ,[fldFeeJoz]
													   ,[fldFeeYekCarton]
													   ,[fldFeeKol]
													   ,[fldDarsadTakhfif]
													   ,[fldMablaghTakhfif]
													   ,[fldFeeVahed1BadAzTakhfif]
													   ,[fldFeeVahed2BadAzTakhfif]
													   ,[fldArzeshAfzode]
													   ,[fldFeeKolBadAzTakhfif]
													   ,[fldMablaghKhales]
													   ,[fldType]
													   ,[fldDate]
													   ,[fldTime] output  from @dtForoosh;
							set @strResult=convert(nvarchar(17),@fldShomareFactor)
							set @strResult2='insertByNo'
							--commit transaction AddRecord
						end
						
				end
		


				commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				set @strResult2='error'
				rollback transaction AddRecord;
			end
		end catch
END

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoTblTanzimatDynamic]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[InsertInfoTblTanzimatDynamic]
	@strType nvarchar(30),
	@strMeghdar nvarchar(30)
as 

begin
	INSERT       INTO              tblTanzimatDynamic(fldType, fldMeghdar)
				VALUES        (@strtype,@strmeghdar)
end

GO
/****** Object:  StoredProcedure [dbo].[InsertInfoVosoolCheck_Final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertInfoVosoolCheck_Final] 
--چندزبانه
@fldCodeArz int,
@fldNerkheArz decimal(18,4),
@language nvarchar(10),
@fldShomareVosool bigint,
@fldSanadVosool bigint,
@strResult nvarchar(25) output,
@InsertType nvarchar(50),
@dtChecksVosools [dbo].[dtCheckVosool-Multi] readonly

as            
BEGIN
	
	SET NOCOUNT ON;
   begin transaction AddRecord
            declare @lngCountShomareVosool bigint
			declare @lngCountShomareSanad bigint,@SPoorsant nvarchar(50)

			set @SPoorsant=(Select * from dbo.translatefunction(@LANGUAGE,18272))

			begin try
			     
				 set @lngCountShomareVosool=(select COUNT(fldShomareVosool) from tblVosool where fldShomareVosool=@fldShomareVosool) 
				 set @lngCountShomareSanad=(select COUNT(convert(bigint,fldSanad)) from tblSodooreSanad where convert(bigint,fldSanad)=@fldSanadVosool)
				  if @lngCountShomareVosool > 0
				  	 Begin
						if @InsertType='insert'
							begin
							set @strResult='error_tekrari_insert'
								rollback transaction 
								return
							end --End Insert
				      end --@lngCountShomareVosool > 0
					  
                 --*************************************
	       
					  if @InsertType='insert'
					   begin

					 IF (@lngCountShomareSanad=0) --@lngcountShomareVosool = 0 and sanad =0  Shomare Sanad Tekrari Nist
					 Begin
					   -- /update tblchkDaryafti
					  update tblCheckeDaryafti 
					  set  fldShomareVosool=P.fldShomareVosool,
						   fldDateVosool=P.fldDateVosool,
						   fldTozihateVosool=P.fldTozihateVosool,
						   fldVaziat=P.fldVaziat,
						   fldSanadVosool=@fldSanadVosool,
						   fldCodeHesabeVosool=P.fldCodeHesabeVosool,
						   fldDateBargasht=Null
					  FROM tblCheckeDaryafti  INNER JOIN @dtChecksVosools as P
					  ON tblCheckeDaryafti.ID =P.fldIDVosool;
			   
						--/update tblchkDaryafti

					


						--*/*----------------------------------Sanad hesab dari
						        IF OBJECT_ID('tempdb..#TempCheck1') IS NOT NULL
									 begin
										delete from  #TempCheck1
									 end
									else 
									 begin
									create table #TempCheck1(
									fldIDVosool1 bigint NULL,
									fldDateVosool1 int NULL,
									fldCodeHesabeVosool1 nvarchar(10)NULL,
									fldMablagh1      decimal(18,4) NULL,
									fldTozihateVosool1 Nvarchar(50)NULL,
									[fldCodeSahebeCheck1] nvarchar(10)NULL
									)
									 end

									insert into #TempCheck1(fldIDVosool1,fldDateVosool1,fldCodeHesabeVosool1,fldMablagh1,fldTozihateVosool1,[fldCodeSahebeCheck1]
									)
									select fldIDVosool,fldDateVosool,fldCodeHesabeVosool,fldMablagh,fldTozihateVosool,fldCodeSahebeCheck from
									@dtChecksVosools output




								DECLARE @Mablagh dec(18,4);
								Declare @CodeHesab nvarchar(10);
								Declare @Tarikh int;
								Declare @Tozihate Nvarchar(50);
								Declare @IDVosool Bigint;
								Declare @CodeSahebeCheck nvarchar(10);

								
								declare @TooleKol bigint,@TooleMoin bigint,@fldCodhesabKol Nvarchar(10),@fldCodHesabMoin nvarchar(10)
								declare @intRow int=1,@day nvarchar(4),@Month nvarchar(4),@year nvarchar(5),@dateSmall bigint;
								
								declare @OutCountMoin nvarchar(100);
								declare @strTozihateVShomareCheck Nvarchar(100);
								Declare @strShomareCheckByID Nvarchar(50),@CodeVasete Nvarchar(50);
								--//cursor
								DECLARE intListCursor CURSOR LOCAL FAST_FORWARD
								FOR
								SELECT [fldIDVosool1],[fldDateVosool1],[fldCodeHesabeVosool1],[fldMablagh1],[fldTozihateVosool1],[fldCodeSahebeCheck1] 
								FROM #TempCheck1;

								
								OPEN intListCursor


								FETCH NEXT FROM intListCursor INTO @IDVosool,@Tarikh,@CodeHesab,@Mablagh,@Tozihate,@CodeSahebeCheck;

								WHILE @@FETCH_STATUS = 0
									BEGIN

									--// Get Shomare Check By ID from Tbl CheckDaryafti
									set @strShomareCheckByID=(select fldShomareCheck  from tblCheckeDaryafti where ID=@IDVosool)
									set @CodeVasete=(select fldVasete  from tblCheckeDaryafti where ID=@IDVosool)
									--// Get Shomare Check By ID from Tbl CheckDaryafti
									execute  ToolePaye 'kol',@TooleKol output
									execute  ToolePaye 'moin',@Toolemoin output
									set @fldCodhesabKol=SUBSTRING(@CodeHesab,1,@TooleKol)
									set @fldCodHesabMoin=SUBSTRING(@CodeHesab,@TooleKol +1,@TooleMoin)
								
									--//Tarikh
									set @year=substring(convert(nvarchar(20),@Tarikh),0,5)
									set @month=substring(convert(nvarchar(20),@Tarikh),5,2)
									set @day=substring(convert(nvarchar(20),@Tarikh),7,2)
									set @dateSmall=@Tarikh--substring(convert(nvarchar(15),@Tarikh),3,6)
									--//Tarikh
								
								
							

								   		--/*insert to tblVosool
										if @intRow=1
										 begin
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol,@fldCodHesabMoin,@CodeHesab,NUll,NULL,
										   @Mablagh,0,@intRow,@dateSmall,@Tozihate,@day,@month,@year,'Vosool',@CodeVasete,@fldSanadVosool;
									  

										   --//Asanad Daryaftani
									 
											execute CodeMoinHesab 'check', @OutCountMoin output
											set @fldCodhesabKol=SUBSTRING(@OutCountMoin,1,@TooleKol)
											set @fldCodHesabMoin=SUBSTRING(@OutCountMoin,@TooleKol +1,@TooleMoin)
										
										
											set @strTozihateVShomareCheck=(Select * from dbo.translatefunction(@LANGUAGE,18271)) + ' ' + Convert(nvarchar(100),@strShomareCheckByID) 

											set @intRow =@intRow + 1

											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol ,@fldCodHesabMoin,@OutCountMoin,@CodeSahebeCheck,'0',
											0,@Mablagh,@intRow,@dateSmall,@strTozihateVShomareCheck,@day,@month,@year,'Vosool',NULL,@fldSanadVosool; 
										   -- // Asnad Daryaftani


										  insert into tblVosool(fldShomareSanad,fldShomareVosool)values(@fldSanadVosool,@fldShomareVosool)
											


											----سند پورسانت
											declare @CodeKolkham nvarchar(50),@CodeMoinKham nvarchar(50),@CodeMoinHesabhayePardakhtani nvarchar(50)



											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											set @intRow =@intRow + 1

											declare @NoeMohasebePoorsant int,@decDarsadPoorsant decimal(18,4),@DecMablaghePoorsant decimal(18,4),@CodeMoinHoghoogh nvarchar(50)
											set @NoeMohasebePoorsant=(Select fldNoeMohasebePoorsant  from tblTafzili Where fldCodeTafzili= @CodeVasete)
											if @NoeMohasebePoorsant=2 --نوع محاسبه واریزی
												begin
													set @decDarsadPoorsant=(Select fldPoorsant from tblTafzili Where fldCodeTafzili= @CodeVasete)
													set @DecMablaghePoorsant=@Mablagh*@decDarsadPoorsant/100


													set @intRow=@intRow+1
									
													execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
													set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
													set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
												
													--سند بدهکاری پورسانت
													execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@CodeVasete,'1',@DecMablaghePoorsant,0,@intRow,@dateSmall,@SPoorsant,@day,@month,@year,'Vosool','',@fldSanadVosool


													execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
													set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
													set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
													--سند بستانکاری پورسانت
													set @intRow=@intRow+1
													execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@CodeVasete,'0',0,@DecMablaghePoorsant,@intRow,@dateSmall,@SPoorsant,@day,@month,@year,'Vosool','',@fldSanadVosool
												end

										 -- execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol ,@fldCodHesabMoin,@OutCountMoin,@CodeSahebeCheck,'0',
											--0,@Mablagh,@intRow,@dateSmall,@strTozihateVShomareCheck,@day,@month,@year,'Vosool',NULL,@fldSanadVosool; 
										  End

										else
										 begin
											  set @intRow =@intRow + 1
											  execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol ,@fldCodHesabMoin,@CodeHesab,NULL,'1',
											  @Mablagh,0,@intRow,@dateSmall,@Tozihate,@day,@month,@year,'Vosool',NULL,@fldSanadVosool;

											  --///// Asnad Daryafti
											execute CodeMoinHesab 'check', @OutCountMoin output
											set @fldCodhesabKol=SUBSTRING(@OutCountMoin,1,@TooleKol)
											set @fldCodHesabMoin=SUBSTRING(@OutCountMoin,@TooleKol +1,@TooleMoin)
											set @strTozihateVShomareCheck=(Select * from dbo.translatefunction(@LANGUAGE,18271)) + ' ' +  Convert(nvarchar(100),@strShomareCheckByID)
											  set @intRow =@intRow + 1
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol ,@fldCodHesabMoin,@OutCountMoin,@CodeSahebeCheck,'0',
											0,@Mablagh,@intRow,@dateSmall,@strTozihateVShomareCheck,@day,@month,@year,'Vosool',@CodeVasete,@fldSanadVosool; 

											
											----سند پورسانت
											--declare @CodeKolkham nvarchar(50),@CodeMoinKham nvarchar(50),@CodeMoinHesabhayePardakhtani nvarchar(50)



											execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
											set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
											set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
											set @intRow =@intRow + 1

											--declare @NoeMohasebePoorsant int,@decDarsadPoorsant decimal(18,2),@DecMablaghePoorsant decimal(18,2),@CodeMoinHoghoogh nvarchar(50)
											set @NoeMohasebePoorsant=(Select fldNoeMohasebePoorsant  from tblTafzili Where fldCodeTafzili= @CodeVasete)
											if @NoeMohasebePoorsant=2 --نوع محاسبه واریزی
												begin
													set @decDarsadPoorsant=(Select fldPoorsant from tblTafzili Where fldCodeTafzili= @CodeVasete)
													set @DecMablaghePoorsant=@Mablagh*@decDarsadPoorsant/100


													set @intRow=@intRow+1
									
													execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output
													set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
													set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
												
													--سند بدهکاری پورسانت
													execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@CodeVasete,'1',@DecMablaghePoorsant,0,@intRow,@dateSmall,@SPoorsant,@day,@month,@year,'Vosool','',@fldSanadVosool


													execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
													set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
													set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
													--سند بستانکاری پورسانت
													set @intRow=@intRow+1
													execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@CodeVasete,'0',0,@DecMablaghePoorsant,@intRow,@dateSmall,@SPoorsant,@day,@month,@year,'Vosool','',@fldSanadVosool
												end
											 --//// asnad Daryafti
										 end

										 --/* insert to tblvosool

									   -- Attempt to fetch next row from cursor
									   FETCH NEXT FROM intListCursor INTO @IDVosool,@Tarikh,@CodeHesab,@Mablagh,@Tozihate,@CodeSahebeCheck;
									END;

								CLOSE intListCursor;
								DEALLOCATE intListCursor;


							

						--*/*------------------------------------sanad hesab dari

						    set @strResult='insertOk1'
							
						
					End -- @lngCountSanadVosool = 0
				 --****************************************
				 
				else if @lngCountShomareSanad > 0 --@sanad > 0 Shomare Sanad Tekrari Bod
				    begin					
					 declare @MaxSanad bigint
					 set @MaxSanad=(select max(convert(bigint,fldSanad)) from tblSodooreSanad)
					 set @MaxSanad=@MaxSanad+1
					  
					   -- /update tblchkDaryafti
					  update tblCheckeDaryafti 
					  set  fldShomareVosool=P.fldShomareVosool,
						   fldDateVosool=P.fldDateVosool,
						   fldTozihateVosool=P.fldTozihateVosool,
						   fldVaziat=P.fldVaziat,
						   fldSanadVosool=@MaxSanad,
						   fldCodeHesabeVosool=P.fldCodeHesabeVosool
					  FROM tblCheckeDaryafti  INNER JOIN @dtChecksVosools as P
					  ON tblCheckeDaryafti.ID =P.fldIDVosool;
			   
						--/update tblchkDaryafti
						     
					    --/insert Sanad Hesabdari
						  IF OBJECT_ID('tempdb..#TempCheck3') IS NOT NULL
									 begin
										delete from  #TempCheck3
									 end
									else 
									 begin
									create table #TempCheck3(
									fldIDVosool1 bigint NULL,
									fldDateVosool1 int NULL,
									fldCodeHesabeVosool1 nvarchar(10)NULL,
									fldMablagh1      decimal(18,4) NULL,
									fldTozihateVosool1 Nvarchar(50)NULL,
									[fldCodeSahebeCheck1] nvarchar(10)NULL
									)
									 end

									insert into #TempCheck3(fldIDVosool1,fldDateVosool1,fldCodeHesabeVosool1,fldMablagh1,fldTozihateVosool1,[fldCodeSahebeCheck1])
									
									select fldIDVosool,fldDateVosool,fldCodeHesabeVosool,fldMablagh,fldTozihateVosool,fldCodeSahebeCheck from
									@dtChecksVosools output

								DECLARE @Mablagh3 dec(18,4);
								Declare @CodeHesab3 nvarchar(10);
								Declare @Tarikh3 int;
								Declare @Tozihate3 Nvarchar(50);
								
								Declare @IDVosool3 Bigint;
								
								Declare @CodeSahebeCheck3 nvarchar(10);
								declare @OutCountMoin3 nvarchar(100);
						        declare @strTozihateVShomareCheck3 Nvarchar(100);
								Declare @strShomareCheckByID3 Nvarchar(50);
								
								declare @TooleKol3 bigint,@TooleMoin3 bigint,@fldCodhesabKol3 Nvarchar(10),@fldCodHesabMoin3 nvarchar(10)
								
								declare @intRow3 int=1,@day3 nvarchar(4),@Month3 nvarchar(4),@year3 nvarchar(5),@dateSmall3 bigint;

								DECLARE intListCursor CURSOR LOCAL FAST_FORWARD
								FOR
								SELECT [fldIDVosool1],[fldDateVosool1],[fldCodeHesabeVosool1],[fldMablagh1],[fldTozihateVosool1],[fldCodeSahebeCheck1] 
								FROM #TempCheck3;

								
								OPEN intListCursor


								FETCH NEXT FROM intListCursor INTO @IDVosool3,@Tarikh3,@CodeHesab3,@Mablagh3,@Tozihate3,@CodeSahebeCheck3;

								WHILE @@FETCH_STATUS = 0
								BEGIN



								--// Get Shomare Check By ID from Tbl CheckDaryafti
								set @strShomareCheckByID3=(select fldShomareCheck  from tblCheckeDaryafti where ID=@IDVosool3)
								--// Get Shomare Check By ID from Tbl CheckDaryafti

								-- Kol v Moin
							
	                            execute  ToolePaye 'kol',@TooleKol3 output
								execute  ToolePaye 'moin',@Toolemoin3 output
								set @fldCodhesabKol3=SUBSTRING(@CodeHesab3,1,@TooleKol3)
								set @fldCodHesabMoin3=SUBSTRING(@CodeHesab3,@TooleKol3 +1,@TooleMoin3)
								-- Kol V moin
								
								--//Tarikh
								set @year3=substring(convert(nvarchar(20),@Tarikh3),0,5)
								set @month3=substring(convert(nvarchar(20),@Tarikh3),5,2)
								set @day3=substring(convert(nvarchar(20),@Tarikh3),7,2)
								set @dateSmall3=@Tarikh3--substring(convert(nvarchar(15),@Tarikh3),3,6)
								--//Tarikh
								
								
								
							
						             
									
								

								   	--/*insert to tblVosool
									if @intRow3=1
									 begin
									       --/// First Sanad
		                                execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol3,@fldCodHesabMoin3,@CodeHesab3,NUll,NULL,
                                       @Mablagh3,0, @intRow3,@dateSmall3,@Tozihate3,@day3,@month3,@year3,'Vosool',@CodeVasete,@MaxSanad;
									 
									        -- /// First Sanad
											set @intRow3 =@intRow3 +1
											 --/Asnad Daryaftani
						  
										   execute CodeMoinHesab 'check', @OutCountMoin3 output
										   set @fldCodhesabKol3=SUBSTRING(@OutCountMoin3,1,@TooleKol3)
										   set @fldCodHesabMoin3=SUBSTRING(@OutCountMoin3,@TooleKol3 +1,@TooleMoin3)
						  
						   
										   set @strTozihateVShomareCheck3=(Select * from dbo.translatefunction(@LANGUAGE,18271)) + ' ' +  Convert(nvarchar(100),@strShomareCheckByID3)

										   execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol3 ,@fldCodHesabMoin3,@OutCountMoin3,@CodeSahebeCheck3,'0',
										   0,@Mablagh3,@intRow3,@dateSmall3,@strTozihateVShomareCheck3,@day3,@Month3,@year3,'Vosool',NULL,@MaxSanad; 

						                  --/Asanad Daryaftani
						              insert into tblVosool(fldShomareSanad,fldShomareVosool)values(@MaxSanad,@fldShomareVosool)
									  end
									 else
									    begin
										        -- ///Last Sanad Insert 
												 set @intRow3 =@intRow3 +1
										     execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol3 ,@fldCodHesabMoin3,@CodeHesab3,NULL,'1',
                                             @Mablagh3,0,@intRow3,@dateSmall3,@Tozihate3,@day3,@month3,@year3,'Vosool',@CodeVasete,@MaxSanad; 
											   -- ///Last Sanad Insert
									     
										 --/Asnad Daryaftani
						  
										   execute CodeMoinHesab 'check', @OutCountMoin3 output
										   set @fldCodhesabKol3=SUBSTRING(@OutCountMoin3,1,@TooleKol3)
										   set @fldCodHesabMoin3=SUBSTRING(@OutCountMoin3,@TooleKol3 +1,@TooleMoin3)
						  
						   
										   set @strTozihateVShomareCheck3=(Select * from dbo.translatefunction(@LANGUAGE,18271)) + ' ' +  Convert(nvarchar(100),@strShomareCheckByID3)

										   set @intRow3 =@intRow3 +1
										   execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol3 ,@fldCodHesabMoin3,@OutCountMoin3,@CodeSahebeCheck3,'0',
										   0,@Mablagh3,@intRow3,@dateSmall3,@strTozihateVShomareCheck3,@day3,@Month3,@year3,'Vosool',NULL,@MaxSanad; 

						                  --/Asanad Daryaftani

									  End
						             --/* insert to tblvosool

								   -- Attempt to fetch next row from cursor
								   FETCH NEXT FROM intListCursor INTO @IDVosool3,@Tarikh3,@CodeHesab3,@Mablagh3,@Tozihate3,@CodeSahebeCheck3;
								END;

								CLOSE intListCursor;
								DEALLOCATE intListCursor;


						--/insert Sanad Hesabdari

					
						 
						
						

						    set @strResult='insertOk2'
						
						 end ---- else @lngCountShomareVosool = 0
					End --Type Insert
			 		
					--// 
					--insert into tbltest2 (name) values ('2')
						if @InsertType='archive'
							begin
							   --insert into tbltest2 (name) values ('3')
							   declare @MaxNewSanad1 nvarchar(10)
							   declare @StrShomareSanadVosool nvarchar(20);
							   set @StrShomareSanadVosool=(select fldShomareSanad from tblVosool where fldShomareVosool=@fldShomareVosool)
							   --insert into tbltest2 (name) values (@StrShomareSanadVosool)
							   delete from tblSodooreSanad where fldSanad=@StrShomareSanadVosool 
							   delete from tblVosool where fldShomareVosool=@fldShomareVosool 
				  	        
							   ---/// sanad az karbar check mishavad tekrari nabashad
								declare @SanadVosoolCount1 bigint;
								set @SanadVosoolCount1=(select COUNT(convert(bigint,fldSanad)) from tblSodooreSanad where convert(bigint,fldSanad)=@fldSanadVosool)
								if @SanadVosoolCount1 > 0 -- //////in yani karbar zodtar sabt kardeh
								 begin
								 --insert into tbltest2 (name) values ('4')
								 set @MaxNewSanad1=(select max(convert(bigint,fldSanad)) from tblSodooreSanad)
								 set @MaxNewSanad1=@MaxNewSanad1+1

								  update tblCheckeDaryafti 
								   set fldShomareVosool=NULL,
								   fldDateVosool=NULL,
								   fldTozihateVosool=NULL,
								   fldVaziat=NULL,
								   fldSanadVosool=NULL,
								   fldCodeHesabeVosool=NULL
								   where fldShomareVosool=@fldShomareVosool 
								  --insert into tbltest2 (name) values ('1')
								--------///
							------baraye halati ke check be bank vagozar shode					
								update tblCheckeDaryafti 
								   set fldShomareVosool=NULL,
								   fldDateVosool=NULL,
								   fldTozihateVosool=NULL,
								   fldVaziat='vagozari',
								   fldSanadVosool=NULL,
								   fldCodeHesabeVosool=NULL
								   where fldShomareVosool=@fldShomareVosool and fldDateVagozariBeBank IS not NULL
							--------///

					 


							--*/*----------------------------------Sanad hesab dari
									IF OBJECT_ID('tempdb..#TempCheck4') IS NOT NULL
										 begin
											delete from  #TempCheck4
										 end
										else 
										 begin
										create table #TempCheck4(
										fldIDVosool1 bigint NULL,
										fldDateVosool1 int NULL,
										fldCodeHesabeVosool1 nvarchar(10)NULL,
										fldMablagh1      decimal(18,4) NULL,
										fldTozihateVosool1 Nvarchar(50)NULL,
										[fldCodeSahebeCheck1] nvarchar(10)NULL
										)
										 end

										insert into #TempCheck4(fldIDVosool1,fldDateVosool1,fldCodeHesabeVosool1,fldMablagh1,fldTozihateVosool1,[fldCodeSahebeCheck1]
										)
										select fldIDVosool,fldDateVosool,fldCodeHesabeVosool,fldMablagh,fldTozihateVosool,fldCodeSahebeCheck from
										@dtChecksVosools output




										DECLARE @Mablagh4 dec(18,4);
										Declare @CodeHesab4 nvarchar(10);
										Declare @Tarikh4 int;
										Declare @Tozihate4 Nvarchar(50);
								
										Declare @IDVosool4 Bigint;
								
										Declare @CodeSahebeCheck4 nvarchar(10);

								
										declare @TooleKol4 bigint,@TooleMoin4 bigint,@fldCodhesabKol4 Nvarchar(10),@fldCodHesabMoin4 nvarchar(10)
										declare @intRow4 int=1,@day4 nvarchar(4),@Month4 nvarchar(4),@year4 nvarchar(5),@dateSmall4 bigint;
								
										declare @OutCountMoin4 nvarchar(100);
										declare @strTozihateVShomareCheck4 Nvarchar(100);
										Declare @strShomareCheckByID4 Nvarchar(50);
										--//cursor
										DECLARE intListCursor CURSOR LOCAL FAST_FORWARD
										FOR
										SELECT [fldIDVosool1],[fldDateVosool1],[fldCodeHesabeVosool1],[fldMablagh1],[fldTozihateVosool1],[fldCodeSahebeCheck1] 
										FROM #TempCheck4;

								
										OPEN intListCursor


										FETCH NEXT FROM intListCursor INTO @IDVosool4,@Tarikh4,@CodeHesab4,@Mablagh4,@Tozihate4,@CodeSahebeCheck4;

										WHILE @@FETCH_STATUS = 0
										BEGIN

										--// Get Shomare Check By ID from Tbl CheckDaryafti
										set @strShomareCheckByID4=(select fldShomareCheck  from tblCheckeDaryafti where ID=@IDVosool4)
										--// Get Shomare Check By ID from Tbl CheckDaryafti
										execute  ToolePaye 'kol',@TooleKol4 output
										execute  ToolePaye 'moin',@Toolemoin4 output
										set @fldCodhesabKol4=SUBSTRING(@CodeHesab4,1,@TooleKol4)
										set @fldCodHesabMoin4=SUBSTRING(@CodeHesab4,@TooleKol4 +1,@TooleMoin4)
								
										--//Tarikh
										set @year4=substring(convert(nvarchar(20),@Tarikh4),0,5)
										set @month4=substring(convert(nvarchar(20),@Tarikh4),5,2)
										set @day4=substring(convert(nvarchar(20),@Tarikh4),7,2)
										set @dateSmall4=@Tarikh4--substring(convert(nvarchar(15),@Tarikh4),3,6)
										--//Tarikh
								
								
								
								
							

								   		--/*insert to tblVosool
										if @intRow4=1
										 begin
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol4,@fldCodHesabMoin4,@CodeHesab4,NUll,NULL,
										   @Mablagh4,0,@intRow4,@dateSmall4,@Tozihate4,@day4,@month4,@year4,'Vosool',@CodeVasete,@MaxNewSanad1
									

										   --//Asanad Daryaftani
											 set @intRow4 =@intRow4 +1
											execute CodeMoinHesab 'check', @OutCountMoin4 output
											set @fldCodhesabKol4=SUBSTRING(@OutCountMoin4,1,@TooleKol4)
											set @fldCodHesabMoin4=SUBSTRING(@OutCountMoin4,@TooleKol4 +1,@TooleMoin4)
										
										
											set @strTozihateVShomareCheck4=(Select * from dbo.translatefunction(@LANGUAGE,18271)) + ' ' +  Convert(nvarchar(100),@strShomareCheckByID4)

											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol4 ,@fldCodHesabMoin4,@OutCountMoin4,@CodeSahebeCheck4,'0',
											0,@Mablagh4,@intRow4,@dateSmall4,@strTozihateVShomareCheck4,@day4,@month4,@year4,'Vosool',NULL,@MaxNewSanad1; 
										   -- // Asnad Daryaftani


										  insert into tblVosool(fldShomareSanad,fldShomareVosool)values(@MaxNewSanad1,@fldShomareVosool)									  	
										  End

										else
										 begin
									      
											  set @intRow4 =@intRow4 +1
											  execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol4 ,@fldCodHesabMoin4,@CodeHesab4,NULL,'1',
											  @Mablagh4,0,@intRow4,@dateSmall4,@Tozihate4,@day4,@month4,@year4,'Vosool',@CodeVasete,@MaxNewSanad1;

											  --// Asnad Daryafti
											execute CodeMoinHesab 'check', @OutCountMoin4 output
											set @fldCodhesabKol4=SUBSTRING(@OutCountMoin4,1,@TooleKol4)
											set @fldCodHesabMoin4=SUBSTRING(@OutCountMoin4,@TooleKol4 +1,@TooleMoin4)
											set @strTozihateVShomareCheck4=(Select * from dbo.translatefunction(@LANGUAGE,18271)) + ' ' +  Convert(nvarchar(100),@strShomareCheckByID4)

											set @intRow4 =@intRow4 +1
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol4 ,@fldCodHesabMoin4,@OutCountMoin4,@CodeSahebeCheck4,'0',
											0,@Mablagh4,@intRow4,@dateSmall4,@strTozihateVShomareCheck4,@day4,@month4,@year4,'Vosool',NULL,@MaxNewSanad1; 
											  --// asnad Daryafti

										  	
										   end



										 --/* insert to tblvosool

									   -- Attempt to fetch next row from cursor
									   FETCH NEXT FROM intListCursor INTO @IDVosool4,@Tarikh4,@CodeHesab4,@Mablagh4,@Tozihate4,@CodeSahebeCheck4;
									END;

									CLOSE intListCursor;
									DEALLOCATE intListCursor;

									  -- /update tblchkDaryafti
						  update tblCheckeDaryafti 
						  set  fldShomareVosool=P.fldShomareVosool,
							   fldDateVosool=P.fldDateVosool,
							   fldTozihateVosool=P.fldTozihateVosool,
							   fldVaziat=P.fldVaziat,
							   fldSanadVosool=@MaxNewSanad1,
							   fldCodeHesabeVosool=P.fldCodeHesabeVosool
						  FROM tblCheckeDaryafti  INNER JOIN @dtChecksVosools as P
						  ON tblCheckeDaryafti.ID =P.fldIDVosool;
			   
							--/update tblchkDaryafti
							

							--*/*------------------------------------sanad hesab dari

								set @strResult='ArchiveUpdateOK1'
							
								 End -- end count

								else   -- lngCountsanad =0
								 begin
								 --insert into tbltest2 (name) values ('5')
							   declare @StrShomareSanadVosool1 nvarchar(20);
							   set @StrShomareSanadVosool1=(select fldShomareSanad from tblVosool where fldShomareVosool=@fldShomareVosool)
							   delete from tblSodooreSanad where fldSanad=@StrShomareSanadVosool1 
							   delete from tblVosool where fldShomareVosool=@fldShomareVosool 

								 set @MaxNewSanad1=@fldSanadVosool     -- // yani sanad tekrari nist

							 
							 -- /update tblchkDaryafti Reset For Update
								update tblCheckeDaryafti 
							   set fldShomareVosool=NULL,
							   fldDateVosool=NULL,
							   fldTozihateVosool=NULL,
							   fldVaziat=NULL,
							   fldSanadVosool=NULL,
							   fldCodeHesabeVosool=NULL
							   where fldShomareVosool=@fldShomareVosool 
						  
							--------///
							------baraye halati ke check be bank vagozar shode					
							 update tblCheckeDaryafti 
							   set fldShomareVosool=NULL,
							   fldDateVosool=NULL,
							   fldTozihateVosool=NULL,
							   fldVaziat='vagozari',
							   fldSanadVosool=NULL,
							   fldCodeHesabeVosool=NULL
							   where fldShomareVosool=@fldShomareVosool and fldDateVagozariBeBank IS not NULL
							--------///

						   --/update tblchkDaryafti Reset For Update

							  -- /update tblchkDaryafti
						  update tblCheckeDaryafti 
						  set  fldShomareVosool=P.fldShomareVosool,
							   fldDateVosool=P.fldDateVosool,
							   fldTozihateVosool=P.fldTozihateVosool,
							   fldVaziat=P.fldVaziat,
							   fldSanadVosool=@MaxNewSanad1,
							   fldCodeHesabeVosool=P.fldCodeHesabeVosool
						  FROM tblCheckeDaryafti  INNER JOIN @dtChecksVosools as P
						  ON tblCheckeDaryafti.ID =P.fldIDVosool;
			   
							--/update tblchkDaryafti
						
							   -- TempTable
								  --*/*----------------------------------Sanad hesab dari
									IF OBJECT_ID('tempdb..#TempCheck5') IS NOT NULL
										 begin
											delete from  #TempCheck5
										 end
										else 
										 begin
										create table #TempCheck5(
										fldIDVosool1 bigint NULL,
										fldDateVosool1 int NULL,
										fldCodeHesabeVosool1 nvarchar(10)NULL,
										fldMablagh1      decimal(18,4) NULL,
										fldTozihateVosool1 Nvarchar(50)NULL,
										[fldCodeSahebeCheck1] nvarchar(10)NULL
										)
										 end

									   insert into #TempCheck5(fldIDVosool1,fldDateVosool1,fldCodeHesabeVosool1,fldMablagh1,fldTozihateVosool1,[fldCodeSahebeCheck1]
										)
										select fldIDVosool,fldDateVosool,fldCodeHesabeVosool,fldMablagh,fldTozihateVosool,fldCodeSahebeCheck from
										@dtChecksVosools output

									DECLARE @Mablagh5 dec(18,4);
									Declare @CodeHesab5 nvarchar(10);
									Declare @Tarikh5 int;
									Declare @Tozihate5 Nvarchar(50);
									Declare @IDVosool5 Bigint;
									Declare @CodeSahebeCheck5 nvarchar(10);

								
									declare @TooleKol5 bigint,@TooleMoin5 bigint,@fldCodhesabKol5 Nvarchar(10),@fldCodHesabMoin5 nvarchar(10)
									declare @intRow5 int=1,@day5 nvarchar(4),@Month5 nvarchar(4),@year5 nvarchar(5),@dateSmall5 bigint;
								
									declare @OutCountMoin5 nvarchar(100);
									declare @strTozihateVShomareCheck5 Nvarchar(100);
									Declare @strShomareCheckByID5 Nvarchar(50);
							   -- TempTable

							   --//cursor
									DECLARE intListCursor CURSOR LOCAL FAST_FORWARD
									FOR
									SELECT [fldIDVosool1],[fldDateVosool1],[fldCodeHesabeVosool1],[fldMablagh1],[fldTozihateVosool1],[fldCodeSahebeCheck1] 
									FROM #TempCheck5;

								
									OPEN intListCursor


									FETCH NEXT FROM intListCursor INTO @IDVosool5,@Tarikh5,@CodeHesab5,@Mablagh5,@Tozihate5,@CodeSahebeCheck5;

									WHILE @@FETCH_STATUS = 0
									BEGIN

									--// Get Shomare Check By ID from Tbl CheckDaryafti
									set @strShomareCheckByID5=(select fldShomareCheck  from tblCheckeDaryafti where ID=@IDVosool5)
									--// Get Shomare Check By ID from Tbl CheckDaryafti
									execute  ToolePaye 'kol',@TooleKol5 output
									execute  ToolePaye 'moin',@Toolemoin5 output
									set @fldCodhesabKol5=SUBSTRING(@CodeHesab5,1,@TooleKol5)
									set @fldCodHesabMoin5=SUBSTRING(@CodeHesab5,@TooleKol5 +1,@TooleMoin5)
								
									--//Tarikh
									set @year5=substring(convert(nvarchar(20),@Tarikh5),0,5)
									set @month5=substring(convert(nvarchar(20),@Tarikh5),5,2)
									set @day5=substring(convert(nvarchar(20),@Tarikh5),7,2)
									set @dateSmall5=@Tarikh5--substring(convert(nvarchar(15),@Tarikh5),3,6)
									--//Tarikh

										
								set @CodeVasete=(select fldVasete  from tblCheckeDaryafti where ID=@IDVosool5)
												   		--/*insert to tblVosool
										if @intRow5=1
										 begin
											--insert into tbltest2 (name) values ('7')
											--insert into tbltest2 (name) values (@IDVosool5)
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol5,@fldCodHesabMoin5,@CodeHesab5,NUll,NULL,
										   @Mablagh5,0,@intRow5,@dateSmall5,@Tozihate5,@day5,@month5,@year5,'Vosool',@codevasete,@MaxNewSanad1;
									 

										   --//Asanad Daryaftani
									 
											execute CodeMoinHesab 'check', @OutCountMoin5 output
											set @fldCodhesabKol5=SUBSTRING(@OutCountMoin5,1,@TooleKol5)
											set @fldCodHesabMoin5=SUBSTRING(@OutCountMoin5,@TooleKol5 +1,@TooleMoin5)
										
										
											set @strTozihateVShomareCheck5=(Select * from dbo.translatefunction(@LANGUAGE,18271)) + ' ' +  Convert(nvarchar(100),@strShomareCheckByID5)
											set @intRow5 =@intRow5 +1
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol5 ,@fldCodHesabMoin5,@OutCountMoin5,@CodeSahebeCheck5,'0',
											0,@Mablagh5,@intRow5,@dateSmall5,@strTozihateVShomareCheck5,@day5,@month5,@year5,'Vosool',NULL,@MaxNewSanad1; 
										   -- // Asnad Daryaftani


										  insert into tblVosool(fldShomareSanad,fldShomareVosool)values(@MaxNewSanad1,@fldShomareVosool)
										  
												----سند پورسانت
												--declare @CodeKolkham nvarchar(50),@CodeMoinKham nvarchar(50),@CodeMoinHesabhayePardakhtani nvarchar(50)
											

												execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
												set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
												set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
												set @intRow =@intRow + 1

												--declare @NoeMohasebePoorsant int,@decDarsadPoorsant decimal(18,2),@DecMablaghePoorsant decimal(18,2),@CodeMoinHoghoogh nvarchar(50)
												set @NoeMohasebePoorsant=(Select fldNoeMohasebePoorsant  from tblTafzili Where fldCodeTafzili= @CodeVasete)
												--insert into tbltest2 (name) values (@CodeVasete)
												--set @CodeVasete=(select *  from tblCheckeDaryafti where ID=24)
												--insert into tbltest2 (name) values (@CodeVasete)
												if @NoeMohasebePoorsant=2 --نوع محاسبه واریزی
														begin
															--insert into tbltest2 (name) values ('8')
															set @decDarsadPoorsant=(Select fldPoorsant from tblTafzili Where fldCodeTafzili= @CodeVasete)
															--insert into tbltest2 (name) values (@CodeVasete)
															set @DecMablaghePoorsant=@Mablagh5*@decDarsadPoorsant/100
															
															set @intRow5=@intRow5+1
															
															execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output

															execute  ToolePaye 'kol',@TooleKol output
															execute  ToolePaye 'moin',@Toolemoin output
															set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
															set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
												
															declare @MoinKamel2 nvarchar(20)

															set @moinKamel2=( @CodeKolkham  +  @CodeMoinKham)
															--سند بدهکاری پورسانت
															execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@CodeVasete,'1',@DecMablaghePoorsant,0,@intRow5,@dateSmall5,@SPoorsant,@day5,@month5,@year5,'Vosool','',@fldSanadVosool

															execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
															set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
															set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)

															--سند بستانکاری پورسانت
															set @intRow5=@intRow5+1
															execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@CodeVasete,'0',0,@DecMablaghePoorsant,@intRow5,@dateSmall5,@SPoorsant,@day5,@month5,@year5,'Vosool','',@fldSanadVosool
														end

										  End

										else
										 begin
											--insert into tbltest2 (name) values ('8')
											   set @intRow5 =@intRow5 +1
											  execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol5 ,@fldCodHesabMoin5,@CodeHesab5,NULL,'1',
											  @Mablagh5,0,@intRow5,@dateSmall5,@Tozihate5,@day5,@month5,@year5,'Vosool',@CodeVasete,@MaxNewSanad1;

											  --// Asnad Daryafti
											execute CodeMoinHesab 'check', @OutCountMoin5 output
											set @fldCodhesabKol5=SUBSTRING(@OutCountMoin5,1,@TooleKol5)
											set @fldCodHesabMoin5=SUBSTRING(@OutCountMoin5,@TooleKol5 +1,@TooleMoin5)
											set @strTozihateVShomareCheck5=(Select * from dbo.translatefunction(@LANGUAGE,18271)) + ' ' + Convert(nvarchar(100),@strShomareCheckByID5)

											set @intRow5 =@intRow5 +1
											execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @fldCodhesabKol5 ,@fldCodHesabMoin5,@OutCountMoin5,@CodeSahebeCheck5,'0',
											0,@Mablagh5,@intRow5,@dateSmall5,@strTozihateVShomareCheck5,@day5,@month5,@year5,'Vosool',NULL,@MaxNewSanad1; 


											
												----سند پورسانت
												--declare @CodeKolkham nvarchar(50),@CodeMoinKham nvarchar(50),@CodeMoinHesabhayePardakhtani nvarchar(50)
												execute  ToolePaye 'kol',@TooleKol output
												execute  ToolePaye 'moin',@Toolemoin output

												execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
												set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
												set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)
			
												set @intRow =@intRow + 1

												--declare @NoeMohasebePoorsant int,@decDarsadPoorsant decimal(18,2),@DecMablaghePoorsant decimal(18,2),@CodeMoinHoghoogh nvarchar(50)
												set @NoeMohasebePoorsant=(Select fldNoeMohasebePoorsant  from tblTafzili Where fldCodeTafzili= @CodeVasete)
												if @NoeMohasebePoorsant=2 --نوع محاسبه واریزی
													begin
														set @decDarsadPoorsant=(Select fldPoorsant from tblTafzili Where fldCodeTafzili= @CodeVasete)
														set @DecMablaghePoorsant=@Mablagh5*@decDarsadPoorsant/100


														set @intRow5=@intRow5+1
									
														execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output

														execute  ToolePaye 'kol',@TooleKol output
														execute  ToolePaye 'moin',@Toolemoin output
														set @CodeKolkham =substring(@CodeMoinHoghoogh, 1, @TooleKol)
														set @CodeMoinKham =substring(@CodeMoinHoghoogh,@TooleKol+1,@Toolemoin)
												
														--سند بدهکاری پورسانت
														execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHoghoogh,@CodeVasete,'1',@DecMablaghePoorsant,0,@intRow5,@dateSmall5,@SPoorsant,@day5,@month5,@year5,'Vosool','',@fldSanadVosool

														execute codemoinhesab 'hesabhayepardakhtanipoorsant' ,@CodeMoinHesabhayePardakhtani output
														set @CodeKolkham =substring(@CodeMoinHesabhayePardakhtani, 1, @TooleKol)
														set @CodeMoinKham =substring(@CodeMoinHesabhayePardakhtani,@TooleKol+1,@Toolemoin)

														--سند بستانکاری پورسانت
														set @intRow5=@intRow5+1
														execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayePardakhtani,@CodeVasete,'0',0,@DecMablaghePoorsant,@intRow5,@dateSmall5,@SPoorsant,@day5,@month5,@year5,'Vosool','',@fldSanadVosool
													end
											  --// asnad Daryafti
										 end


										 --/* insert to tblvosool

									   -- Attempt to fetch next row from cursor
									   FETCH NEXT FROM intListCursor INTO @IDVosool5,@Tarikh5,@CodeHesab5,@Mablagh5,@Tozihate5,@CodeSahebeCheck5;
									END;

									CLOSE intListCursor;
									DEALLOCATE intListCursor;


							

							--*/*------------------------------------sanad hesab dari

								set @strResult='ArchiveUpdateOK2'


                         

								 end
								 ---/// sanad az karbar check mishavad tekrari nabashad

				
					   End --/// ArchiveUpdate End
                        
				
																																												                    
   commit transaction AddRecord;
           end try
		begin catch
			if @@TranCount > 0
				begin
					set @strResult='error'
					
					rollback transaction AddRecord;
				end
		end catch
		
END

GO
/****** Object:  StoredProcedure [dbo].[InsertInTblPrinters]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertInTblPrinters] 
	-- Add the parameters for the stored procedure here
	@type nvarchar(10),
	@ID bigint,
	@NameMahal nvarchar(50),
	@NamePrinter nvarchar(MAX)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	if @type='update'
		update tblPrinters
			set fldNameMahal=@NameMahal,fldNamePrinter=@NamePrinter  
				where ID=@ID

	if @type='insert'
	INSERT        INTO              tblPrinters( fldNameMahal, fldNamePrinter)
			VALUES        (@NameMahal,@NamePrinter)


END

GO
/****** Object:  StoredProcedure [dbo].[InsertIntoTblIDChapgarKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertIntoTblIDChapgarKala] 
--چندزبانه
	-- Add the parameters for the stored procedure here
	@CodeKala bigint,
	@IDChapgar bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	delete tblIDChapgarKala Where fldCodeKala=@CodeKala

   insert into tblIDChapgarKala
		(fldCodeKala,fldIDChapgarKala)
	Values
		(@CodeKala,@IDChapgar)
END

GO
/****** Object:  StoredProcedure [dbo].[InsertKolMoin]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertKolMoin]

@IDHesab bigInt,
@NameHesab nvarChar(70),
@CodeKolHesab nvarChar(8),
@CodeType nvarchar(5),
@SharheNoeTafzili nvarchar(20),
@flagSabt int output,
@IDOutPut int outPut


as 

begin
	set @IDHesab= (Select Max(fldCodeHesab) from tblKolMoin Where left(fldCodeHesab,len(@CodekolHesab))=@CodeKolHesab )
	Set @IDHesab+=1
	insert into tblKolMoin 
		values(@IDHesab,@NameHesab,@SharheNoeTafzili,@CodeType,'غير فعال','غير فعال',null)
	set @flagSabt=@IDHesab
	set @IDOutPut=@@IDENTITY
end

GO
/****** Object:  StoredProcedure [dbo].[InsertSanad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[InsertSanad]
	@fldCodeKol nvarchar(50),
	@fldCodeMoin nvarchar(50),
	@fldMoinKamel nvarchar(50),
	@fldCodeTafzili nvarchar(50),
	@fldCodeNoeTafzili nvarchar(50),
	@fldBedehkar decimal,
	@fldBestankar  decimal, 
	@fldRadif nvarchar(50),
	@fldDate int, 
	@fldSharh nvarchar(100),
	@fldType nvarchar(20)
as

begin
		INSERT INTO tblSodooreSanad
                         (fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldType)
		SELECT        MAX(CONVERT(dec, fldSanad)) + 1 AS MaxSanad,@fldCodeKol,@fldCodeMoin,@fldMoinKamel,@fldCodeTafzili,@fldCodeNoeTafzili,@fldBedehkar,@fldBestankar,@fldRadif,@fldDate,@fldSharh,'daryaft'
		FROM            tblSodooreSanad AS tblSodooreSanad_1
end

GO
/****** Object:  StoredProcedure [dbo].[InsertSanadFirst]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertSanadFirst]
--چندزبانه
	@fldCodeKol nvarchar(50),
	@fldCodeMoin nvarchar(50),
	@fldMoinKamel nvarchar(50),
	@fldCodeTafzili nvarchar(50),
	@fldCodeNoeTafzili nvarchar(50),
	@fldBedehkar decimal(18, 4),
	@fldBestankar  decimal(18, 4), 
	@fldRadif nvarchar(50),
	@fldDate int, 
	@fldSharh nvarchar(100),	
	@day nvarchar(50),
	@month nvarchar(50),
	@year nvarchar(50),
	@fldType nvarchar(20),
	@fldVasete nvarchar(10),
	@ShomareSanad nvarchar(50) output
as

begin
		declare @lngShomareSanad nvarchar(50),@IDOutPut bigint,@lngCount bigint,@yearSmall nvarchar(10)
		if @fldSharh is null
			set @fldSharh=''
		--if len(@year)>2
		--	set @yearSmall=substring(@year,3,2)
		--else
		set @yearSmall=@year
		
		set @lngCount= (Select Count(fldSanad) from tblSodooreSanad)

		if @lngCount>0 
			begin
				INSERT INTO tblSodooreSanad
								 (fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldCodeMarkaz1,fldCodeMarkaz2, fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldDay,fldMonth,fldYear,fldVasete, fldType)
				SELECT        MAX(CONVERT(dec, fldSanad)) + 1 AS MaxSanad,@fldCodeKol,@fldCodeMoin,@fldMoinKamel,ISNULL(@fldCodeTafzili,''),ISNULL(@fldCodeNoeTafzili,''),'','',@fldBedehkar,@fldBestankar,@fldRadif,@fldDate,@fldSharh,@day,@month,@yearSmall,@fldVasete,@fldType
				FROM            tblSodooreSanad AS tblSodooreSanad_1
			end
		else
			begin
				INSERT INTO tblSodooreSanad
								 (fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldCodeMarkaz1,fldCodeMarkaz2,fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldDay,fldMonth,fldYear,fldVasete, fldType)
					Values ('1',@fldCodeKol,@fldCodeMoin,@fldMoinKamel,ISNULL(@fldCodeTafzili,''),ISNULL(@fldCodeNoeTafzili,''),'','',@fldBedehkar,@fldBestankar,@fldRadif,@fldDate,@fldSharh,@day,@month,@yearSmall,@fldVasete,@fldType)
				--FROM            tblSodooreSanad AS tblSodooreSanad_1

				--INSERT INTO tblSodooreSanad
				--				 (fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldDay,fldMonth,fldYear,fldVasete, fldType)
				--values        ('66','110','01','1101','','',10000,0,'2',950101,'ss','01','01','95','100','daryaft')
				
			end

		set @IDOutPut=@@IDENTITY;
	
		--set @lngShomareSanad=(Select fldSanad from tblSodooreSanad   where id=@IDOutPut)

		

		set @lngShomareSanad =(select fldSanad from tblSodooreSanad Where ID=@IDOutPut)

		--insert into tblTest7 (fld1)
		--values(@IDOutPut)

		set @ShomareSanad=@lngShomareSanad

		--INSERT INTO tblTest7(fld1)
		--VALUES        (@lngShomareSanad )

	 --  	set @ShomareSanad=@lngShomareSanad
		--EXECUTE [TestInsert]  @IDOutPut
end

GO
/****** Object:  StoredProcedure [dbo].[InsertSanadFirstMultiArz]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertSanadFirstMultiArz]
--چندزبانه
	@fldCodeArz int,
	@fldNerkheArz decimal(18,4),
	@fldCodeKol nvarchar(50),
	@fldCodeMoin nvarchar(50),
	@fldMoinKamel nvarchar(50),
	@fldCodeTafzili nvarchar(50),
	@fldCodeNoeTafzili nvarchar(50),
	@fldBedehkar decimal(18, 4),
	@fldBestankar  decimal(18, 4), 
	@fldRadif nvarchar(50),
	@fldDate int, 
	@fldSharh nvarchar(100),	
	@day nvarchar(50),
	@month nvarchar(50),
	@year nvarchar(50),
	@fldType nvarchar(20),
	@fldVasete nvarchar(10),
	@ShomareSanad nvarchar(50) output
as

begin
		declare @lngShomareSanad nvarchar(50),@IDOutPut bigint,@lngCount bigint,@yearSmall nvarchar(10)
		if @fldSharh is null
			set @fldSharh=''
		--if len(@year)>2
		--	set @yearSmall=substring(@year,3,2)
		--else
		set @yearSmall=@year
			 
		--if len(@fldDate)=8
		--	set @fldDate=RIGHT(@fldDate,6)
		
		set @lngCount= (Select Count(fldSanad) from tblSodooreSanad)

		if @lngCount>0 
			begin
				INSERT INTO tblSodooreSanad
								 (fldCodeArz,fldNerkheArz, fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldCodeMarkaz1,fldCodeMarkaz2, fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldDay,fldMonth,fldYear,fldVasete, fldType)
				SELECT        @fldCodeArz,@fldNerkheArz, MAX(CONVERT(dec, fldSanad)) + 1 AS MaxSanad,@fldCodeKol,@fldCodeMoin,@fldMoinKamel,ISNULL(@fldCodeTafzili,''),ISNULL(@fldCodeNoeTafzili,''),'','',@fldBedehkar,@fldBestankar,@fldRadif,@fldDate,@fldSharh,@day,@month,@yearSmall,@fldVasete,@fldType
				FROM            tblSodooreSanad AS tblSodooreSanad_1
			end
		else
			begin
				INSERT INTO tblSodooreSanad
								 (fldCodeArz,fldNerkheArz,fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldCodeMarkaz1,fldCodeMarkaz2,fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldDay,fldMonth,fldYear,fldVasete, fldType)
					Values ( @fldCodeArz,@fldNerkheArz,'1',@fldCodeKol,@fldCodeMoin,@fldMoinKamel,ISNULL(@fldCodeTafzili,''),ISNULL(@fldCodeNoeTafzili,''),'','',@fldBedehkar,@fldBestankar,@fldRadif,@fldDate,@fldSharh,@day,@month,@yearSmall,@fldVasete,@fldType)
				--FROM            tblSodooreSanad AS tblSodooreSanad_1

				--INSERT INTO tblSodooreSanad
				--				 (fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldDay,fldMonth,fldYear,fldVasete, fldType)
				--values        ('66','110','01','1101','','',10000,0,'2',950101,'ss','01','01','95','100','daryaft')
				
			end

		set @IDOutPut=@@IDENTITY;
	
		--set @lngShomareSanad=(Select fldSanad from tblSodooreSanad   where id=@IDOutPut)

		

		set @lngShomareSanad =(select fldSanad from tblSodooreSanad Where ID=@IDOutPut)

		--insert into tblTest7 (fld1)
		--values(@IDOutPut)

		set @ShomareSanad=@lngShomareSanad

		--INSERT INTO tblTest7(fld1)
		--VALUES        (@lngShomareSanad )

	 --  	set @ShomareSanad=@lngShomareSanad
		--EXECUTE [TestInsert]  @IDOutPut
end

GO
/****** Object:  StoredProcedure [dbo].[InsertSanadLast]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertSanadLast]
--چندزبانه
	@fldCodeKol nvarchar(50),
	@fldCodeMoin nvarchar(50),
	@fldMoinKamel nvarchar(50),
	@fldCodeTafzili nvarchar(50),
	@fldCodeNoeTafzili nvarchar(50),
	@fldBedehkar decimal(18,4),
	@fldBestankar  decimal(18, 4), 
	@fldRadif nvarchar(50),
	@fldDate int, 
	@fldSharh nvarchar(100),
	@day nvarchar(50),
	@month nvarchar(50),
	@year nvarchar(50),
	@fldType nvarchar(20),
	@vasete nvarchar(10),
	@ShomareSanad nvarchar(50) 
as

begin
		declare @lngShomareSanad nvarchar(50),@IDOutPut bigint,@yearSmall nvarchar(10)
		if @fldSharh is null
			set @fldSharh=''
		--if len(@year)>2
		--	set @yearSmall=substring(@year,3,2)
		--else
		set @yearSmall=@year
		
		INSERT INTO tblSodooreSanad
                         (fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldCodeMarkaz1,fldCodeMarkaz2,fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldday,fldmonth,fldyear,fldvasete,fldType)
		values (@ShomareSanad,@fldCodeKol,@fldCodeMoin,@fldMoinKamel,isnull(@fldCodeTafzili,''),isnull(@fldCodeNoeTafzili,''),'','',@fldBedehkar,@fldBestankar,@fldRadif,@fldDate,@fldSharh,@day,@month,@yearSmall,@vasete, @fldType)

		--insert into tblTest7
		--	(fld4)
		-- values	(@fldBestankar)

		--set @IDOutPut=@@IDENTITY;	

		--set @lngShomareSanad =(select fldSanad from tblSodooreSanad Where ID=@IDOutPut)

		--set @ShomareSanad=@lngShomareSanad

		--INSERT INTO tblTest7(fld1)
		--VALUES        (@lngShomareSanad )

	 --  	set @ShomareSanad=@lngShomareSanad
		--EXECUTE [TestInsert]  @IDOutPut
end

GO
/****** Object:  StoredProcedure [dbo].[InsertSanadLastMultiArz]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[InsertSanadLastMultiArz]
--چندزبانه
	@fldCodeArz int,
	@fldNerkheArz decimal(18,4),
	@fldCodeKol nvarchar(50),
	@fldCodeMoin nvarchar(50),
	@fldMoinKamel nvarchar(50),
	@fldCodeTafzili nvarchar(50),
	@fldCodeNoeTafzili nvarchar(50),
	@fldBedehkar decimal(18,4),
	@fldBestankar  decimal(18, 4), 
	@fldRadif nvarchar(50),
	@fldDate int, 
	@fldSharh nvarchar(100),
	@day nvarchar(50),
	@month nvarchar(50),
	@year nvarchar(50),
	@fldType nvarchar(20),
	@vasete nvarchar(10),
	@ShomareSanad nvarchar(50) 
as

begin
		declare @lngShomareSanad nvarchar(50),@IDOutPut bigint,@yearSmall nvarchar(10)
		if @fldSharh is null
			set @fldSharh=''
		--if len(@year)>2
		--	set @yearSmall=substring(@year,3,2)
		--else
		set @yearSmall=@year
		
		INSERT INTO tblSodooreSanad
                         (fldCodeArz,fldNerkheArz,fldSanad, fldCodeKol, fldCodeMoin, fldMoinKamel, fldCodeTafzili, fldCodeNoeTafzili,fldCodeMarkaz1,fldCodeMarkaz2,fldBedehkar, fldBestankar , fldRadif,fldDate , fldSharh,fldday,fldmonth,fldyear,fldvasete,fldType)
		values (@fldCodeArz,@fldNerkheArz,@ShomareSanad,@fldCodeKol,@fldCodeMoin,@fldMoinKamel,isnull(@fldCodeTafzili,''),isnull(@fldCodeNoeTafzili,''),'','',@fldBedehkar,@fldBestankar,@fldRadif,@fldDate,@fldSharh,@day,@month,@yearSmall,@vasete, @fldType)

		--insert into tblTest7
		--	(fld4)
		-- values	(@fldBestankar)

		--set @IDOutPut=@@IDENTITY;	

		--set @lngShomareSanad =(select fldSanad from tblSodooreSanad Where ID=@IDOutPut)

		--set @ShomareSanad=@lngShomareSanad

		--INSERT INTO tblTest7(fld1)
		--VALUES        (@lngShomareSanad )

	 --  	set @ShomareSanad=@lngShomareSanad
		--EXECUTE [TestInsert]  @IDOutPut
end

GO
/****** Object:  StoredProcedure [dbo].[InsertShomareFish]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertShomareFish] 
	-- Add the parameters for the stored procedure here
	@ShomareFactor bigint,
	@ShomareFish bigint
AS
BEGIN
	
	SET NOCOUNT ON;
	declare @ShomareForSave bigint
	if @ShomareFish=-1 --یعنی شماره ماکس باید دربیادیا بعبارتی زمانی که حالت آرشیو نیست
		execute MaxShomareFish @ShomareForSave output
	else
		set @ShomareForSave =@ShomareFish
		
	Delete tblFish Where fldShomareFactor=@ShomareFactor or fldShomareFish=@ShomareForSave
    --set @ShomareForSave=1
	Insert into tblFish
		(fldShomarefactor,fldShomareFish)
		values
		(@ShomareFactor,@ShomareForSave)
END

GO
/****** Object:  StoredProcedure [dbo].[InsertTanzimatNew]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertTanzimatNew]

	@Name1 nvarChar(30),
	@Meghdar nvarChar(30),
	@NoeKhata nvarchar(30) output ,
	@IDOutPut int outPut
as 

begin
	begin transaction transact
		begin try
			delete tblTanzimatNew where fldName=@Name1
			insert into tblTanzimatNew 
				values(@Name1,@Meghdar)	
			set @IDOutPut=@@IDENTITY
			set @noekhata='insert'
		commit transaction
		end try

	begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='rolback'				
	end catch
end

GO
/****** Object:  StoredProcedure [dbo].[InsertTarheTakhfif]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertTarheTakhfif]
--چندزبانه
@ID bigint,
@fldMablagh decimal(18,4),
@fldDarsad decimal(18,4),
@countRow bigint

as 

begin

	insert into tblTarheTakhfif 
		values(@ID,@fldMablagh,@fldDarsad,@countRow)

end

GO
/****** Object:  StoredProcedure [dbo].[inserttblPorsantVisitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[inserttblPorsantVisitor]
--چندزبانه
@ID bigint,
@fldCodeVisitor bigint,
@fldRadif bigint,
@fldMablagh decimal (18, 4),
@fldDarsad decimal(18,4)


as 

begin

	insert into tblPorsantVisitor 
		values(@ID,@fldCodeVisitor,@fldRadif,@fldMablagh,@fldDarsad)

end

GO
/****** Object:  StoredProcedure [dbo].[inserttest]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[inserttest]
as
DECLARE @LocationTVP AS LocationTableType;  
  
/* Add data to the table variable. */  
INSERT INTO @LocationTVP (LocationName, CostRate)  
    SELECT Name1, 0.00  
    FROM tblTest5;  
  
/* Pass the table variable data to a stored procedure. */  
EXEC usp_InsertProductionLocation @LocationTVP;

GO
/****** Object:  StoredProcedure [dbo].[InsertUser]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertUser]
	@Code bigint,
	@UserName nvarchar(25),
	@Password nvarchar(MAX),
	@Type int,
	@TypeInsert nvarchar(15),
	@Result nvarchar(30) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	if @TypeInsert='update'
		begin
			update tblSecurity set fldUser=@UserName ,fldPassword=@Password Where fldCode=@Code
			set @Result='true'
		end

	if @TypeInsert='insert'
		begin
			declare @MaxCode bigint

			set @MaxCode=(Select Max(fldCode) from tblSecurity)
			if @MaxCode is null 
				set @MaxCode=1000
			else
				set @MaxCode=@MaxCode+1

			Insert into tblSecurity
				(fldCode, fldUser,fldPassword,fldType)
				values
				(@MaxCode, @UserName,@Password,@Type)

				set @Result='true'
		end
END

GO
/****** Object:  StoredProcedure [dbo].[InsertVahed]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[InsertVahed]

@NameVahed nvarChar(70),
@IDOutPut int outPut
as 

begin

	insert into tblVahed  (fldNameVahed)
		values(@NameVahed)	
	set @IDOutPut=@@IDENTITY
end

GO
/****** Object:  StoredProcedure [dbo].[IsArchiveShomaredaryaft]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[IsArchiveShomaredaryaft]
--چندزبانه
@Shomaredaryaft bigint,
@Result nvarchar(20) output

as
begin
	declare @lngCount bigint,@strType nvarchar(50)

	set @lngCount=(Select count(fldShomareDaryaft) from tblDaryaft Where fldShomareDaryaft=@Shomaredaryaft)
	if @lngCount>0 
		begin
			set @Result=(Select fldType from tblDaryaft Where fldShomareDaryaft=@Shomaredaryaft)
		end
	else
		begin
			set @Result='notarchive'
		end
end

GO
/****** Object:  StoredProcedure [dbo].[IsArchiveShomarePardakht]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[IsArchiveShomarePardakht]
@ShomarePardakht bigint,
@Result nvarchar(10) output

as
begin
	declare @lngCount bigint

	set @lngCount=(Select count(fldShomarePardakht) from tblPardakht Where fldShomarePardakht=@ShomarePardakht)
	if @lngCount>0 
		begin
			set @Result='archive'
		end
	else
		begin
			set @Result='notarchive'
		end
end

GO
/****** Object:  StoredProcedure [dbo].[IsArchiveShomareSanad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[IsArchiveShomareSanad]
@ShomareSanad nvarchar(50),
@Result nvarchar(10) output

as
begin
	declare @lngCount bigint

	set @lngCount=(Select count(fldSanad) from tblSodooreSanad Where fldSanad=@ShomareSanad)							
						
	if @lngCount>0 
		begin
			set @Result='archive'
		end
	else
		begin
			set @Result='notarchive'
		end
end

GO
/****** Object:  StoredProcedure [dbo].[IsArchiveShomareVam]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[IsArchiveShomareVam]
@ShomareVam bigint,
@Result nvarchar(20) output

as
begin
	declare @lngCount bigint,@strType nvarchar(50)

	set @lngCount=(Select count(fldShomareVam) from tblVamInfo Where fldShomareVam=@ShomareVam)
	if @lngCount>0 
		begin
			set @Result='archive'
		end
	else
		begin
			set @Result='notarchive'
		end
end

GO
/****** Object:  StoredProcedure [dbo].[IsArshiveShomarePas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[IsArshiveShomarePas]
	@ShomarePas bigint,
	@Result nvarchar(30) output
AS
BEGIN
	declare @bigCount bigint

	set @bigCount =(Select Count(fldShomarePas) from tblPas Where fldShomarePas=@ShomarePas)
	if @bigCount>0 
		set @Result='arshive'
	else
		set @Result='insert'
END

GO
/****** Object:  StoredProcedure [dbo].[ISTekrariCheckeDaryafti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ISTekrariCheckeDaryafti]
@intShomareCheck bigint,
@ID Int,
@intOutPut int output 
as

begin
	declare @RowCount int
	if @ID>0 --برای حالتی که مقایسه در حالت ویرایشه یعنی مقایسه بغیر خودش  
		begin
			set @RowCount=(Select count(fldShomareCheck) from tblCheckeDaryafti Where fldShomareCheck=@intShomareCheck and ID<>@ID);			
		end
	else -- برای حالتی که مقایسه در حالت درج جدیده یعنی با کل رکوردها مقایسه انجام میشه
		begin
			set @RowCount=(Select count(fldShomareCheck) from tblCheckeDaryafti Where fldShomareCheck=@intShomareCheck );			
		end
	
	set @intOutPut=@Rowcount

end

GO
/****** Object:  StoredProcedure [dbo].[ISTekrariCheckePardakhti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ISTekrariCheckePardakhti]
@intShomareCheck bigint,
@ID Int,
@intOutPut int output 
as

begin
	declare @RowCount int
	if @ID>0 --برای حالتی که مقایسه در حالت ویرایشه یعنی مقایسه بغیر خودش  
		begin
			set @RowCount=(Select count(fldShomareCheck) from tblCheckePardakhti Where fldShomareCheck=@intShomareCheck and ID<>@ID);			
		end
	else -- برای حالتی که مقایسه در حالت درج جدیده یعنی با کل رکوردها مقایسه انجام میشه
		begin
			set @RowCount=(Select count(fldShomareCheck) from tblCheckePardakhti Where fldShomareCheck=@intShomareCheck );			
		end
	
	set @intOutPut=@Rowcount

end

GO
/****** Object:  StoredProcedure [dbo].[isTekrariDasteCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[isTekrariDasteCheck]
@CodeBank nvarchar(50),
@SerialStart bigint,
@SerialEnd bigint,
@NoeKhata nvarchar(20) output
as

begin
	declare @lngCount bigint
 	set @lngcount=(SELECT        Count(fldShomareCheck)
			FROM            tblDasteCheck
			WHERE        (fldCodeBank = @codebank) AND (fldShomareCheck BETWEEN @SerialStart AND @SerialEnd))

	if @lngcount>0 
		begin
			set @NoeKhata='tekrari'
		end
	else if @lngcount<=0 
		begin
			set @NoeKhata='new'
		end
end

GO
/****** Object:  StoredProcedure [dbo].[Kardeks]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Kardeks]

--@mm nvarchar(10),
--@nn nvarchar(10)

as
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END

begin
 

create table #TempMojoodieAnbar5(

fldGroupName nvarchar(50)  COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100)  COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 0),
fldTedadeKhorooj decimal(18, 0),
fldTedadeJoz decimal(18, 0), 
fldTedadeKol decimal(18, 0), 
fldTedadDarKarton decimal(18, 0),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200)  COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 0),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100)  COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100)  COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20)  COLLATE Persian_100_CI_AS
)



--insert into #TempMojoodieAnbar5 (
--fldCodeGroup,fldCodeKala,fldNameKala,fldTedadeVorood,fldTedadeKhorooj,
--fldTedadeJoz,fldTedadeKol,fldCodeAnbar,fldNameAnbar,fldNogheSefaresh,fldFeeForoosh,
--fldLastFee,fldMianginFee,fldNameVahed,fldNameVahed2 )
--values (1,2,'3',4,5,6,7,7.1,'8',9,10,11,12,'13','14')


INSERT INTO #TempMojoodieAnbar5
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeKharid



INSERT INTO #TempMojoodieAnbar5
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeForoosh


UPDATE       #TempMojoodieAnbar5
SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
FROM            tblKala INNER JOIN
                         #TempMojoodieAnbar5 ON tblKala.fldCodeKala = #TempMojoodieAnbar5.fldCodeKala


UPDATE       #TempMojoodieAnbar5
SET                #TempMojoodieAnbar5.fldGroupName = tblGroupKala.fldGroupName
FROM            tblGroupKala INNER JOIN
                         #TempMojoodieAnbar5 ON #TempMojoodieAnbar5.fldCodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempMojoodieAnbar5
SET                fldNameAnbar = tblAnbar.fldNameAnbar
FROM            tblAnbar INNER JOIN
                         #TempMojoodieAnbar5 ON tblAnbar.fldCode = #TempMojoodieAnbar5.fldCodeAnbar


update #TempMojoodieAnbar5 set #TempMojoodieAnbar5.fldNameVahed=tbl2.fldNameVahed,#TempMojoodieAnbar5.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

where tbl2.fldCodeKala=#TempMojoodieAnbar5.fldCodeKala

delete  from #TempMojoodieAnbar5  where fldCodeKala<>11001
Select * from #TempMojoodieAnbar5 
end

GO
/****** Object:  StoredProcedure [dbo].[Kardeks2]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[Kardeks2]
--چندزبانه
	-- Add the parameters for the stored procedure here
	@fldCodeArz bigint,
	@language nvarchar(10),
	@Codekala bigint,
	@ListCodeAnbar varchar(5000)=null,
	@DateFrom varchar(8),
	@DateTo varchar(8)
	--@CodeTa bigint,
	--@GroupAz bigint,
	--@GroupTa bigInt,
	--@Type nvarchar(10)
AS
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END
BEGIN
	
	Create table #TempKardeks(
		fldCodeArz bigint,
		fldNerkheArz decimal(18,4),
		[ID] bigint,
		[fldCodeTafsili] bigint,
		[CodeKala] bigint null,
		[NameKala] nvarchar(100) collate Persian_100_CI_AS,
		[CodeGroup] bigint,
		[NameGroup] nvarchar(80) collate Persian_100_CI_AS,
		[fldShomareFactor] bigint,
		[fldShomareEnteghal] bigint,
		[fldShomareSanad] bigint,
		[fldDate] bigint,
		[fldDateShow] nvarchar(25),
		[fldSharh] nvarchar(200) collate Persian_100_CI_AS,
		[fldCodeAnbar] bigint,
		[fldNameAnbar] nvarchar(50) collate Persian_100_CI_AS,
		[fldFeeJoz] decimal(18,4),
		[fldFeeYekKarton] decimal(18,4),
		[fldMablaghKol] decimal(18,4),
		[fldTedadeJozVorood] decimal(18,4),
		[fldTedadeJozVoroodT] nvarchar(30),
		[fldTedadeJozKhorooj] decimal(18,4),
		[fldTedadeJozKhoroojT] nvarchar(30),
		[fldTedadDarKartonShow] nvarchar(100) collate Persian_100_CI_AS,
		[SumMandeJoz] decimal(18,4),
		[SumMandeJozT] nvarchar(100),
		[fldTedadDarKarton] decimal(18,4),
		[fldJozMande] decimal(18,4),
		[fldTedadVoroodKartonShow] nvarchar(100) collate Persian_100_CI_AS,
		[fldTedadKhoroojKartonShow] nvarchar(100) collate Persian_100_CI_AS,
		[fldKolMande] nvarchar(100) collate Persian_100_CI_AS,
		[fldtype] nvarchar(20) collate Persian_100_CI_AS,
		[fldtypeShow] nvarchar(40) collate Persian_100_CI_AS,
		[fldNameVahed] NVarchar(100) collate Persian_100_CI_AS,
		[fldNameVahed2] Nvarchar(100) collate Persian_100_CI_AS,
		[ConditionSum] bigint,
		[IdOrder] bigint,
		[fldRadif] bigint
	)
	delete from #TempKardeks
	--Create table #TempKardeksShow(
	--	[CodeKala] bigint null,
	--	[NameKala] nvarchar(100) collate Persian_100_CI_AS,
	--	[CodeGroup] bigint,
	--	[NameGroup] nvarchar(80) collate Persian_100_CI_AS,
	--	[fldShomareFactor] bigint,
	--	[fldShomareSanad] bigint,
	--	[fldDate] bigint,
	--	[fldSharh] nvarchar(200) collate Persian_100_CI_AS,
	--	[fldCodeAnbar] bigint,
	--	[fldNameAnbar] nvarchar(50) collate Persian_100_CI_AS,
	--	[fldFeeJoz] bigint,
	--	[fldFeeYekKarton] bigint,
	--	[fldMablaghKol] bigint,
	--	[fldTedadeJozVorood] bigint,
	--	[fldTedadeJozKhorooj] bigint,
	--	[fldTedadDarKarton] bigint,
	--	[fldJozMande] bigint,
	--	[fldTedadVoroodKartonShow] nvarchar(100) collate Persian_100_CI_AS,
	--	[fldKolKhorooj] nvarchar(100) collate Persian_100_CI_AS,
	--	[fldKolMande] nvarchar(100) collate Persian_100_CI_AS,
	--	[fldtype] nvarchar(20) collate Persian_100_CI_AS,
	--	[fldNameVahed] NVarchar(100) collate Persian_100_CI_AS,
	--	[fldNameVahed2] Nvarchar(100) collate Persian_100_CI_AS
	--)

	declare @SumMandeGhablVorood decimal(18,4),@SumGhablKhorooj decimal(18,4),@SumTedad decimal(18,4)





	insert into #TempKardeks
 	 (id,fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
Select id,fldCodeTafsili,fldCodeKala,'NameKala',-1,'namegroup',fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldTedadJoz,0,-1,'','','',fldtype,'namevahed','namevahed2' from tblRizeKharid
	where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo

	insert into #TempKardeks
 	 (fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
		Select 0,fldCodeKala,'NameKala',-1,'namegroup',fldShomareTolid,0,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldTedadJozNahaei,0,-1,'','','',fldtype,'namevahed','namevahed2' from tblRizeTolid
		where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo and fldType='mahsool'




	--insert into #TempKardeks
	--	(CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
	--	Select fldCodeKala,'NameKala',-1,'namegroup',fldShomareFactor,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldTedadJoz,0,-1,'kolvorood','kolkhorooj','kolmande','kharid','namevahed','namevahed2' from tblRizeKharid


	insert into #TempKardeks
		(fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
		Select fldCodeTafsili, fldCodeKala,'NameKala',-1,'namegroup',fldShomareFactor,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldTedadJoz,0,-1,'','','','bargashtazforoosh','namevahed','namevahed2' from tblRizeBargashtAzForoosh
		where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo


	insert into #TempKardeks
		(id,fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2,fldRadif)
		Select id,fldCodeTafsili,fldCodeKala,'NameKala',-1,'namegroup',fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeYekCarton,fldFeeKol,0,fldTedadJoz,-1,'','','',fldtype,'namevahed','namevahed2',fldRadif from tblRizeForoosh
		where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo
		
	insert into #TempKardeks
		(fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
		Select 0,fldCodeKala,'NameKala',-1,'namegroup',fldShomareTolid,0,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeYekCarton,fldFeeKol,0,fldTedadJozAvalie,-1,'','','',fldtype,'namevahed','namevahed2' from tblRizeTolid
		where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo and fldType='avalie'


	insert into #TempKardeks
		(fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
		Select fldCodeTafsili,fldCodeKala,'NameKala',-1,'namegroup',fldShomareFactor,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeYekCarton,fldFeeKol,0,fldTedadJoz,-1,'','','','bargashtazkharid','namevahed','namevahed2' from tblRizeBargashtAzKharid
		where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo
	--Select * from #TempKardeks



--	--مانده قبل

--	set @SumTedad=(Select Sum(fldTedadJoz) as SumMande from tblRizeKharid where fldCodeKala=@Codekala and fldDate < @DateFrom and fldCodeAnbar  )	
--	if isnumeric(@SumTedad)=1
--		set @SumMandeGhablVorood=@SumTedad

--	set @SumTedad=(Select Sum(fldTedadJoz) as SumMande from tblRizeBargashtAzForoosh where fldCodeKala=@Codekala and fldDate < @DateFrom  )	
--		if isnumeric(@SumTedad)=1
--			set @SumMandeGhablVorood=@SumTedad

--	set @SumTedad=(Select Sum(fldTedadJoz) as SumMande from tblRizeForoosh where fldCodeKala=@Codekala and fldDate < @DateFrom  )	
--		if isnumeric(@SumTedad)=1
--			set @SumGhablKhorooj=@SumTedad

--	set @SumTedad=(Select Sum(fldTedadJoz) as SumMande from tblRizeBargashtAzKharid where fldCodeKala=@Codekala and fldDate < @DateFrom  )	
--		if isnumeric(@SumTedad)=1
--			set @SumGhablKhorooj=@SumTedad
--insert into #TempKardeks
--		(fldCodeTafsili,fldShomareFactor,fldShomareSanad,fldSharh,fldCodeAnbar,fldNameAnbar,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
--		(0,0,0,'نقل مانده',
----####################
	

	if @ListCodeAnbar <>''
		begin
		declare @StrDeleteAnbar varchar(5000)
		set  @StrDeleteAnbar='delete from #TempKardeks 
		    where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
			execute(@StrDeleteAnbar)
		end

	UPDATE       #TempKardeks
		SET                CodeGroup = tblKala.fldCodeGroup, NameKala = tblKala.fldNameKala,fldTedadDarKarton=tblKala.fldTedadJoz
		FROM            tblKala INNER JOIN
								 #TempKardeks ON tblKala.fldCodeKala = #TempKardeks.CodeKala


	UPDATE       #TempKardeks
		SET                #TempKardeks.NameGroup = tblGroupKala.fldGroupName
		FROM            tblGroupKala INNER JOIN
								 #TempKardeks ON #TempKardeks.CodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempKardeks
	SET                fldNameAnbar = tblAnbar.fldNameAnbar
	FROM            tblAnbar INNER JOIN
							 #TempKardeks ON tblAnbar.fldCode = #TempKardeks.fldCodeAnbar




update #TempKardeks set #TempKardeks.fldNameVahed=tbl2.fldNameVahed,#TempKardeks.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2
														 where tbl2.fldCodeKala=#TempKardeks.CodeKala

update #TempKardeks set fldNameVahed2='' where fldTedadDarKarton=0

update #TempKardeks set fldSharh='' Where fldSharh='Sharh'

--برای جمع ستونها مجبور شدم این کار رو بکنم
update #TempKardeks set ConditionSum=1

----##########
--update #TempKardeks set IdOrder=1 where fldType='kharid'
--update #TempKardeks set IdOrder=2 where fldType='bargashtazforoosh'

----##########
    --declare @EmpCodeTafzili nvarchar(10),@SumJam bigint    
    --declare EmpCursor Cursor For
    --        Select fldCodetafzili from #TempKardeks  With (NoLock) 
    --    		open EmpCursor
    --    		fetch Next From EmpCursor into @EmpCodeTafzili
    --    		While @@Fetch_Status = 0
    --    			Begin	
    --    				set @SumJam=Select SUM(fldTedadeJozVorood-fldTedadeJozKhorooj) from  #TempKardeks   where fldCodetafzili =@EmpCodeTafzili
    --    				update #TempKardeks  set  SumMandeJoz=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
    --    						where fldCodetafzili =@EmpCodeTafzili
    --    				Fetch Next from EmpCursor
    --    			into @EmpCodeTafzili
    --    		end
    --    Close EmpCurSor
    --Deallocate EmpCursor
--update  #TempKardeks set SumMandeJoz = tbl3.RunningTotal from
--	(Select fldShomareSanad,SUM(fldTedadeJozVorood-fldTedadeJozKhorooj) OVER (PARTITION BY ConditionSum   ) AS RunningTotal from #TempKardeks) as tbl3
--	where #TempKardeks.fldShomareSanad=tbl3.fldShomareSanad

--update  #TempKardeks set SumMandeJoz = tblS.b from
--SELECT     fldShomareSanad, fldTedadeJozVorood-fldTedadeJozKhorooj,
--              (SELECT     SUM(fldTedadeJozVorood-fldTedadeJozKhorooj) AS Expr1
--                 FROM         #TempKardeks AS b
--                 WHERE     (fldShomareSanad <= a.fldShomareSanad)) AS b
--FROM         #TempKardeks AS a


--update set      fld1, fld2,
--          (SELECT     SUM(fld2) AS Expr1
--             FROM         tblTest7 AS b
--             WHERE     (fld1 <= a.fld1)) AS b
--FROM         tblTest7 AS a


--UPDATE    #TempKardeks
--SET              SumMandeJoz = tblss.b
--FROM         (SELECT      fldShomareSanad, fldTedadeJozVorood-fldTedadeJozKhorooj,
--                                                  (SELECT     SUM(fldTedadeJozVorood-fldTedadeJozKhorooj) AS Expr1
--                                                     FROM         #TempKardeks AS b
--                                                     WHERE     (CONVERT(dec, fldShomareSanad) <= CONVERT(dec, a.fldShomareSanad))) AS b
--                        FROM         #TempKardeks AS a) AS tblss INNER JOIN
--                      #TempKardeks ON tblss.fldShomareSanad = #TempKardeks.fldShomareSanad
--where #TempKardeks.fldShomareSanad=tbls.fldShomareSanad 

update #TempKardeks set IdOrder=convert(bigint, Convert(varchar,fldDate) + convert(varchar, fldShomareSanad))
update #TempKardeks set IdOrder=convert(varchar,IdOrder) + Convert(varchar,fldShomareEnteghal) where fldtype='enteghal'
update #TempKardeks set IdOrder=convert(varchar,IdOrder) + '1' where len(IdOrder)=9 and fldTedadeJozVorood>0
update #TempKardeks set IdOrder=convert(varchar,IdOrder) + '2' where len(IdOrder)=9 and fldTedadeJozKhorooj>0


---جمع ورود و خروج
UPDATE    #TempKardeks
SET              SumMandeJoz = tblss.b
FROM         (SELECT     44 AS Expr1, IdOrder, fldTedadeJozVorood - fldTedadeJozKhorooj AS Expr2,
                                                  (SELECT     SUM(fldTedadeJozVorood - fldTedadeJozKhorooj) AS Expr1
                                                     FROM         #TempKardeks AS b
                                                     WHERE     (CONVERT(varchar, IdOrder) <= CONVERT(varchar, a.IdOrder))) AS b
                        FROM         #TempKardeks AS a) AS tblss INNER JOIN
                      #TempKardeks ON tblss.IdOrder = #TempKardeks.IdOrder
        

--update #TempKardeks set SumMandeJozT=SumMandeJoz,fldTedadeJozVoroodT=fldTedadeJozVorood,fldTedadeJozKhoroojT=fldTedadeJozKhorooj
--update #TempKardeks set SumMandeJozT=LEFT(Convert(varchar,SumMandeJoz),len(Convert(varchar,SumMandeJoz))-3) where right(convert(varchar,SumMandeJoz),2)='00'
--update #TempKardeks set fldTedadeJozVoroodT=LEFT(Convert(varchar,fldTedadeJozVorood),len(Convert(varchar,fldTedadeJozVorood))-3) where right(convert(varchar,fldTedadeJozVorood),2)='00'
--update #TempKardeks set fldTedadeJozKhoroojT=LEFT(Convert(varchar,fldTedadeJozKhorooj),len(Convert(varchar,fldTedadeJozKhorooj))-3) where right(convert(varchar,fldTedadeJozKhorooj),2)='00'
              
--select Convert(varchar,floor(fldTedadeJozKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Convert(decimal(18,4),fldTedadeJozKhorooj), fldTedadDarKarton) } ) + ' ' + fldNameVahed from #TempKardeks where { fn MOD(Convert(decimal(18,4),fldTedadeJozKhorooj), fldTedadDarKarton) }<>0

--select * from #TempKardeks
update #TempKardeks Set fldTedadVoroodKartonShow=Convert(varchar,floor(fldTedadeJozVorood / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Convert(decimal(18,4),fldTedadeJozVorood), fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempKardeks Set fldTedadKhoroojKartonShow=Convert(varchar,floor(fldTedadeJozKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Convert(decimal(18,4),fldTedadeJozKhorooj), fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0  and { fn MOD(Convert(decimal(18,4),fldTedadeJozKhorooj), fldTedadDarKarton) }<>0

update #TempKardeks Set fldKolMande=Convert(varchar,floor(SumMandeJoz / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Convert(decimal(18,4),SumMandeJoz), fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempKardeks Set fldTedadVoroodKartonShow='' where fldTedadeJozVorood=0
update #TempKardeks Set fldTedadKhoroojKartonShow='' where fldTedadeJozKhorooj=0



--update #TempKardeks Set fldTedadKhoroojKartonShow=Convert(varchar,floor(fldTedadeJozKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldTedadeJozKhorooj, fldTedadDarKarton) } ) + ' ' + fldNameVahed
--	where fldTedadDarKarton>0

--جمع ستونها
--SELECT
--  AccountNumber,
--  flddate,
--  bill,
--  SUM(bill) OVER (PARTITION BY AccountNumber ORDER BY flddate) AS RunningTotal
--FROM
--  dbo.Table1 


----فیلتر تا قبل از تاریخ 
--Insert into #TempKardeksShow
--	(fldTedadeJozVorood,fldTedadeJozKhorooj,fldTedadDarKarton, NameGroup, CodeGroup, CodeKala, NameKala, fldNameVahed, fldNameVahed2)
--SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, 
--                         fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,0
--FROM            #TempKardeks
--GROUP BY CodeKala, NameGroup, CodeGroup, NameKala, fldNameVahed, fldNameVahed2, fldTedadDarKarton, fldTedadeKol




	--Select CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2
	--	from #TempKardeks
	--update 

	--Select fldTedadeJozVorood,fldTedadeJozKhorooj,fldDate,fldShomareSanad,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldTedadDarKartonShow,SUM(fldTedadeJozVorood-fldTedadeJozKhorooj) OVER (PARTITION BY ConditionSum ORDER BY fldDate,fldShomareSanad) AS RunningTotal from #TempKardeks
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18273))  where fldType='kharid'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18274)) where fldType='mojodi_ebteda_doreh'	
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18275)) where fldType='enteghal'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18276)) where fldType='foroosh'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18277)) where fldType='bargashtazforoosh'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18278)) where fldType='bargashtazkharid'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18279)) where fldType='avalie'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18280)) where fldType='mahsool'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18281)),fldCodeTafsili=0,fldSharh=(Select * from dbo.translatefunction(@LANGUAGE,18281)) where fldType='Moghayrat'

	
	update #TempKardeks set fldShomareFactor=fldShomareEnteghal where fldType='enteghal'

	update #TempKardeks set fldSharh= tblTafzili.fldSharheTafzili + ' ' + fldSharh from tblTafzili 
		Where #TempKardeks.fldCodeTafsili=tblTafzili.fldCodeTafzili



	
	if @fldCodeArz=1	 --واحد ارزی پایه
	begin
		update #TempKardeks set fldCodeArz=tblSodooreSanad.fldCodeArz,fldNerkheArz=tblSodooreSanad.fldNerkheArz from tblSodooreSanad Where #TempKardeks.fldShomareSanad=tblSodooreSanad.fldSanad

		update #TempKardeks set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null		

		update #TempKardeks set fldFeeJoz=fldFeeJoz/ fldNerkheArz,fldFeeJoz=fldFeeJoz/ fldNerkheArz,fldFeeYekKarton=fldFeeYekKarton/ fldNerkheArz,fldMablaghKol=fldMablaghKol/ fldNerkheArz --from tblCheckeDaryafti Where #TempReportCheck.fldShomareSanad=tblCheckeDaryafti.fldSanad
	end

if @fldCodeArz>1
	begin
			update #TempKardeks set fldCodeArz=tblSodooreSanad.fldCodeArz,fldNerkheArz=tblSodooreSanad.fldNerkheArz from tblSodooreSanad Where #TempKardeks.fldShomareSanad=tblSodooreSanad.fldSanad

			update #TempKardeks set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

			declare @Nerkh decimal(18,4)

			set @Nerkh =(Select fldNerkheArz from tblVahedeArzi Where fldCodeArz=(@fldCodeArz))


			--تبدیل به نرخ پایه
			update #TempKardeks set fldFeeJoz=fldFeeJoz/ fldNerkheArz,fldFeeYekKarton=fldFeeYekKarton/ fldNerkheArz,fldMablaghKol=fldMablaghKol/ fldNerkheArz--from tblSodooreSanad Where #TempReportCheck.fldShomareSanad=tblSodooreSanad.fldSanad --and fldCodeArz<>@fldCodeArz

				--تبدیل به نرخ روز ارز انتخابی
			update #TempKardeks set fldFeeJoz=fldFeeJoz* @Nerkh,fldFeeYekKarton=fldFeeYekKarton* @Nerkh,fldMablaghKol=fldMablaghKol* @Nerkh
	end

	
	update #TempKardeks set flddateshow =substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2)
	Select  ROW_NUMBER() OVER(ORDER BY flddate,fldshomaresanad ) AS Row,id,fldSharh,fldtypeShow,fldShomareFactor,flddateshow, fldTedadeJozVorood, fldTedadeJozKhorooj, SumMandeJoz,fldNameVahed,fldFeeJoz,fldFeeYekKarton,fldMablaghKol,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldTedadDarKarton,fldNameVahed2,fldKolMande,fldShomareSanad,fldNameAnbar,fldDate,fldtype,fldRadif from #TempKardeks order by Convert(varchar,IdOrder)
	
--FROM
--  dbo.Table1 

END

GO
/****** Object:  StoredProcedure [dbo].[KardeksForSood]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[KardeksForSood]
--چندزبانه
	-- Add the parameters for the stored procedure here
	@language nvarchar(10),
	@Codekala bigint,
	@ListCodeAnbar varchar(5000)=null,
	@DateFrom varchar(8),
	@DateTo varchar(8)
	--@CodeTa bigint,
	--@GroupAz bigint,
	--@GroupTa bigInt,
	--@Type nvarchar(10)
AS
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END
BEGIN
	
	Create table #TempKardeks(
		[ID] bigint,
		[fldCodeTafsili] bigint,
		[CodeKala] bigint null,
		[NameKala] nvarchar(100) collate Persian_100_CI_AS,
		[CodeGroup] bigint,
		[NameGroup] nvarchar(80) collate Persian_100_CI_AS,
		[fldShomareFactor] bigint,
		[fldShomareEnteghal] bigint,
		[fldShomareSanad] bigint,
		[fldDate] bigint,
		[fldDateShow] nvarchar(25),
		[fldSharh] nvarchar(200) collate Persian_100_CI_AS,
		[fldCodeAnbar] bigint,
		[fldNameAnbar] nvarchar(50) collate Persian_100_CI_AS,
		[fldFeeJoz] decimal (18, 4),
		[fldFeeVahed1BadAzTakhfif] decimal (18, 4),
		[fldFeeYekKarton] decimal (18, 4),
		[fldMablaghKol] decimal (18, 4),
		[fldTedadeJozVorood] decimal(18,4),
		[fldTedadeJozVoroodT] nvarchar(10),
		[fldTedadeJozKhorooj] decimal(18,4),
		[fldTedadeJozKhoroojT] nvarchar(10),
		[fldTedadDarKartonShow] nvarchar(100) collate Persian_100_CI_AS,
		[SumMandeJoz] decimal(18,4),
		[SumMandeJozT] nvarchar(100),
		[fldTedadDarKarton] decimal (18, 4),
		[fldJozMande] decimal (18, 4),
		[fldTedadVoroodKartonShow] nvarchar(100) collate Persian_100_CI_AS,
		[fldTedadKhoroojKartonShow] nvarchar(100) collate Persian_100_CI_AS,
		[fldKolMande] nvarchar(100) collate Persian_100_CI_AS,
		[fldtype] nvarchar(20) collate Persian_100_CI_AS,
		[fldtypeShow] nvarchar(40) collate Persian_100_CI_AS,
		[fldNameVahed] NVarchar(100) collate Persian_100_CI_AS,
		[fldNameVahed2] Nvarchar(100) collate Persian_100_CI_AS,
		[ConditionSum] decimal (18, 4),
		[IdOrder] bigint,
		[fldRadif] bigint
	)



	declare @SumMandeGhablVorood decimal (18, 4),@SumGhablKhorooj decimal (18, 4),@SumTedad decimal (18, 4)





	insert into #TempKardeks
 	 (id,fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
Select id,fldCodeTafsili,fldCodeKala,'NameKala',-1,'namegroup',fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekCarton,fldMablaghKhales,fldTedadJoz,0,-1,'','','',fldtype,'namevahed','namevahed2' from tblRizeKharid
	where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo

	insert into #TempKardeks
 	 (fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
		Select 0,fldCodeKala,'NameKala',-1,'namegroup',fldShomareTolid,0,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekCarton,fldFeeKol,fldTedadJozNahaei,0,-1,'','','',fldtype,'namevahed','namevahed2' from tblRizeTolid
		where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo and fldType='mahsool'




	--insert into #TempKardeks
	--	(CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
	--	Select fldCodeKala,'NameKala',-1,'namegroup',fldShomareFactor,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldTedadJoz,0,-1,'kolvorood','kolkhorooj','kolmande','kharid','namevahed','namevahed2' from tblRizeKharid


	insert into #TempKardeks
		(fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
		Select fldCodeTafsili, fldCodeKala,'NameKala',-1,'namegroup',fldShomareFactor,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekCarton,fldMablaghKhales,fldTedadJoz,0,-1,'','','','bargashtazforoosh','namevahed','namevahed2' from tblRizeBargashtAzForoosh
		where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo


	insert into #TempKardeks
		(id,fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2,fldRadif)
		Select id,fldCodeTafsili,fldCodeKala,'NameKala',-1,'namegroup',fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekCarton,fldMablaghKhales,0,fldTedadJoz,-1,'','','',fldtype,'namevahed','namevahed2',fldRadif from tblRizeForoosh
		where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo
		
	insert into #TempKardeks
		(fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
		Select 0,fldCodeKala,'NameKala',-1,'namegroup',fldShomareTolid,0,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekCarton,fldFeeKol,0,fldTedadJozAvalie,-1,'','','',fldtype,'namevahed','namevahed2' from tblRizeTolid
		where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo and fldType='avalie'


	insert into #TempKardeks
		(fldCodeTafsili,CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
		Select fldCodeTafsili,fldCodeKala,'NameKala',-1,'namegroup',fldShomareFactor,fldShomareSanad,fldDate,'Sharh',fldCodeAnbar,'nameanbar',fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekCarton,fldMablaghKhales,0,fldTedadJoz,-1,'','','','bargashtazkharid','namevahed','namevahed2' from tblRizeBargashtAzKharid
		where fldCodeKala=@Codekala and fldDate BETWEEN  @DateFrom  AND  @DateTo
	--Select * from #TempKardeks



--	--مانده قبل

--	set @SumTedad=(Select Sum(fldTedadJoz) as SumMande from tblRizeKharid where fldCodeKala=@Codekala and fldDate < @DateFrom and fldCodeAnbar  )	
--	if isnumeric(@SumTedad)=1
--		set @SumMandeGhablVorood=@SumTedad

--	set @SumTedad=(Select Sum(fldTedadJoz) as SumMande from tblRizeBargashtAzForoosh where fldCodeKala=@Codekala and fldDate < @DateFrom  )	
--		if isnumeric(@SumTedad)=1
--			set @SumMandeGhablVorood=@SumTedad

--	set @SumTedad=(Select Sum(fldTedadJoz) as SumMande from tblRizeForoosh where fldCodeKala=@Codekala and fldDate < @DateFrom  )	
--		if isnumeric(@SumTedad)=1
--			set @SumGhablKhorooj=@SumTedad

--	set @SumTedad=(Select Sum(fldTedadJoz) as SumMande from tblRizeBargashtAzKharid where fldCodeKala=@Codekala and fldDate < @DateFrom  )	
--		if isnumeric(@SumTedad)=1
--			set @SumGhablKhorooj=@SumTedad
--insert into #TempKardeks
--		(fldCodeTafsili,fldShomareFactor,fldShomareSanad,fldSharh,fldCodeAnbar,fldNameAnbar,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2)
--		(0,0,0,'نقل مانده',
----####################
	

	if @ListCodeAnbar <>''
		begin
		declare @StrDeleteAnbar varchar(5000)
		set  @StrDeleteAnbar='delete from #TempKardeks 
		    where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
			execute(@StrDeleteAnbar)
		end

	UPDATE       #TempKardeks
		SET                CodeGroup = tblKala.fldCodeGroup, NameKala = tblKala.fldNameKala,fldTedadDarKarton=tblKala.fldTedadJoz
		FROM            tblKala INNER JOIN
								 #TempKardeks ON tblKala.fldCodeKala = #TempKardeks.CodeKala


	UPDATE       #TempKardeks
		SET                #TempKardeks.NameGroup = tblGroupKala.fldGroupName
		FROM            tblGroupKala INNER JOIN
								 #TempKardeks ON #TempKardeks.CodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempKardeks
	SET                fldNameAnbar = tblAnbar.fldNameAnbar
	FROM            tblAnbar INNER JOIN
							 #TempKardeks ON tblAnbar.fldCode = #TempKardeks.fldCodeAnbar




update #TempKardeks set #TempKardeks.fldNameVahed=tbl2.fldNameVahed,#TempKardeks.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2
														 where tbl2.fldCodeKala=#TempKardeks.CodeKala

update #TempKardeks set fldNameVahed2='' where fldTedadDarKarton=0

update #TempKardeks set fldSharh='' Where fldSharh='Sharh'

--برای جمع ستونها مجبور شدم این کار رو بکنم
update #TempKardeks set ConditionSum=1

----##########
--update #TempKardeks set IdOrder=1 where fldType='kharid'
--update #TempKardeks set IdOrder=2 where fldType='bargashtazforoosh'

----##########
    --declare @EmpCodeTafzili nvarchar(10),@SumJam bigint    
    --declare EmpCursor Cursor For
    --        Select fldCodetafzili from #TempKardeks  With (NoLock) 
    --    		open EmpCursor
    --    		fetch Next From EmpCursor into @EmpCodeTafzili
    --    		While @@Fetch_Status = 0
    --    			Begin	
    --    				set @SumJam=Select SUM(fldTedadeJozVorood-fldTedadeJozKhorooj) from  #TempKardeks   where fldCodetafzili =@EmpCodeTafzili
    --    				update #TempKardeks  set  SumMandeJoz=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
    --    						where fldCodetafzili =@EmpCodeTafzili
    --    				Fetch Next from EmpCursor
    --    			into @EmpCodeTafzili
    --    		end
    --    Close EmpCurSor
    --Deallocate EmpCursor
--update  #TempKardeks set SumMandeJoz = tbl3.RunningTotal from
--	(Select fldShomareSanad,SUM(fldTedadeJozVorood-fldTedadeJozKhorooj) OVER (PARTITION BY ConditionSum   ) AS RunningTotal from #TempKardeks) as tbl3
--	where #TempKardeks.fldShomareSanad=tbl3.fldShomareSanad

--update  #TempKardeks set SumMandeJoz = tblS.b from
--SELECT     fldShomareSanad, fldTedadeJozVorood-fldTedadeJozKhorooj,
--              (SELECT     SUM(fldTedadeJozVorood-fldTedadeJozKhorooj) AS Expr1
--                 FROM         #TempKardeks AS b
--                 WHERE     (fldShomareSanad <= a.fldShomareSanad)) AS b
--FROM         #TempKardeks AS a


--update set      fld1, fld2,
--          (SELECT     SUM(fld2) AS Expr1
--             FROM         tblTest7 AS b
--             WHERE     (fld1 <= a.fld1)) AS b
--FROM         tblTest7 AS a


--UPDATE    #TempKardeks
--SET              SumMandeJoz = tblss.b
--FROM         (SELECT      fldShomareSanad, fldTedadeJozVorood-fldTedadeJozKhorooj,
--                                                  (SELECT     SUM(fldTedadeJozVorood-fldTedadeJozKhorooj) AS Expr1
--                                                     FROM         #TempKardeks AS b
--                                                     WHERE     (CONVERT(dec, fldShomareSanad) <= CONVERT(dec, a.fldShomareSanad))) AS b
--                        FROM         #TempKardeks AS a) AS tblss INNER JOIN
--                      #TempKardeks ON tblss.fldShomareSanad = #TempKardeks.fldShomareSanad
--where #TempKardeks.fldShomareSanad=tbls.fldShomareSanad 

update #TempKardeks set IdOrder=convert(bigint, Convert(varchar,fldDate) + convert(varchar, fldShomareSanad))
update #TempKardeks set IdOrder=convert(varchar,IdOrder) + fldShomareEnteghal where fldtype='enteghal'
update #TempKardeks set IdOrder=convert(varchar,IdOrder) + '1' where len(IdOrder)=9 and fldTedadeJozVorood>0
update #TempKardeks set IdOrder=convert(varchar,IdOrder) + '2' where len(IdOrder)=9 and fldTedadeJozKhorooj>0


---جمع ورود و خروج
UPDATE    #TempKardeks
SET              SumMandeJoz = tblss.b
FROM         (SELECT     44 AS Expr1, IdOrder, fldTedadeJozVorood - fldTedadeJozKhorooj AS Expr2,
                                                  (SELECT     SUM(fldTedadeJozVorood - fldTedadeJozKhorooj) AS Expr1
                                                     FROM         #TempKardeks AS b
                                                     WHERE     (CONVERT(varchar, IdOrder) <= CONVERT(varchar, a.IdOrder))) AS b
                        FROM         #TempKardeks AS a) AS tblss INNER JOIN
                      #TempKardeks ON tblss.IdOrder = #TempKardeks.IdOrder
        

update #TempKardeks set SumMandeJozT=SumMandeJoz,fldTedadeJozVoroodT=fldTedadeJozVorood,fldTedadeJozKhoroojT=fldTedadeJozKhorooj
update #TempKardeks set SumMandeJozT=LEFT(Convert(varchar,SumMandeJoz),len(Convert(varchar,SumMandeJoz))-3) where right(convert(varchar,SumMandeJoz),2)='00'
update #TempKardeks set fldTedadeJozVoroodT=LEFT(Convert(varchar,fldTedadeJozVorood),len(Convert(varchar,fldTedadeJozVorood))-3) where right(convert(varchar,fldTedadeJozVorood),2)='00'
update #TempKardeks set fldTedadeJozKhoroojT=LEFT(Convert(varchar,fldTedadeJozKhorooj),len(Convert(varchar,fldTedadeJozKhorooj))-3) where right(convert(varchar,fldTedadeJozKhorooj),2)='00'
              

update #TempKardeks Set fldTedadVoroodKartonShow=Convert(varchar,floor(fldTedadeJozVorood / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Convert(decimal (18, 4),fldTedadeJozVorood), fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempKardeks Set fldTedadKhoroojKartonShow=Convert(varchar,floor(fldTedadeJozKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Convert(decimal (18, 4),fldTedadeJozKhorooj), fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempKardeks Set fldKolMande=Convert(varchar,floor(SumMandeJoz / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Convert(decimal (18, 4),SumMandeJoz), fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempKardeks Set fldTedadVoroodKartonShow='' where fldTedadeJozVorood=0
update #TempKardeks Set fldTedadKhoroojKartonShow='' where fldTedadeJozKhorooj=0



--update #TempKardeks Set fldTedadKhoroojKartonShow=Convert(varchar,floor(fldTedadeJozKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldTedadeJozKhorooj, fldTedadDarKarton) } ) + ' ' + fldNameVahed
--	where fldTedadDarKarton>0

--جمع ستونها
--SELECT
--  AccountNumber,
--  flddate,
--  bill,
--  SUM(bill) OVER (PARTITION BY AccountNumber ORDER BY flddate) AS RunningTotal
--FROM
--  dbo.Table1 


----فیلتر تا قبل از تاریخ 
--Insert into #TempKardeksShow
--	(fldTedadeJozVorood,fldTedadeJozKhorooj,fldTedadDarKarton, NameGroup, CodeGroup, CodeKala, NameKala, fldNameVahed, fldNameVahed2)
--SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, 
--                         fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,0
--FROM            #TempKardeks
--GROUP BY CodeKala, NameGroup, CodeGroup, NameKala, fldNameVahed, fldNameVahed2, fldTedadDarKarton, fldTedadeKol




	--Select CodeKala,NameKala,CodeGroup,NameGroup,fldShomareFactor,fldShomareSanad,fldDate,fldSharh,fldCodeAnbar,fldNameAnbar,fldFeeJoz,fldFeeYekKarton,fldMablaghKol,fldTedadeJozVorood,fldTedadeJozKhorooj,fldJozMande,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldKolMande,fldtype,fldNameVahed,fldNameVahed2
	--	from #TempKardeks
	--update 

	--Select fldTedadeJozVorood,fldTedadeJozKhorooj,fldDate,fldShomareSanad,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldTedadDarKartonShow,SUM(fldTedadeJozVorood-fldTedadeJozKhorooj) OVER (PARTITION BY ConditionSum ORDER BY fldDate,fldShomareSanad) AS RunningTotal from #TempKardeks
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18273))  where fldType='kharid'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18274)) where fldType='mojodi_ebteda_doreh'	
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18275)) where fldType='enteghal'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18276)) where fldType='foroosh'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18277)) where fldType='bargashtazforoosh'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18278)) where fldType='bargashtazkharid'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18279)) where fldType='avalie'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18280)) where fldType='mahsool'
	update #TempKardeks set fldTypeShow=(Select * from dbo.translatefunction(@LANGUAGE,18281)),fldCodeTafsili=0,fldSharh=(Select * from dbo.translatefunction(@LANGUAGE,18281)) where fldType='Moghayrat'

	
	update #TempKardeks set fldShomareFactor=fldShomareEnteghal where fldType='enteghal'

	update #TempKardeks set fldSharh= tblTafzili.fldSharheTafzili + ' ' + fldSharh from tblTafzili 
		Where #TempKardeks.fldCodeTafsili=tblTafzili.fldCodeTafzili

	
	update #TempKardeks set flddateshow =substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2)
	Select  ROW_NUMBER() OVER(ORDER BY flddate,fldshomaresanad ) AS Row,id,fldSharh,fldtypeShow,fldShomareFactor,flddateshow,fldTedadeJozVoroodT As fldTedadeJozVorood,fldTedadeJozKhoroojT as fldTedadeJozKhorooj,SumMandeJozT as SumMandeJoz,fldNameVahed,fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekKarton,fldMablaghKol,fldTedadVoroodKartonShow,fldTedadKhoroojKartonShow,fldTedadDarKarton,fldNameVahed2,fldKolMande,fldShomareSanad,fldNameAnbar,fldDate,fldtype,fldRadif from #TempKardeks order by Convert(varchar,IdOrder)
	
--FROM
--  dbo.Table1 

END

GO
/****** Object:  StoredProcedure [dbo].[KardeksNew]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KardeksNew]

--@mm nvarchar(10),
--@nn nvarchar(10)

as
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END

begin
 

create table #TempMojoodieAnbar5(

fldGroupName nvarchar(50)  COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100)  COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 0),
fldTedadeKhorooj decimal(18, 0),
fldTedadeJoz decimal(18, 0), 
fldTedadeKol decimal(18, 0), 
fldTedadDarKarton decimal(18, 0),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200)  COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 0),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100)  COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100)  COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20)  COLLATE Persian_100_CI_AS
)



--insert into #TempMojoodieAnbar5 (
--fldCodeGroup,fldCodeKala,fldNameKala,fldTedadeVorood,fldTedadeKhorooj,
--fldTedadeJoz,fldTedadeKol,fldCodeAnbar,fldNameAnbar,fldNogheSefaresh,fldFeeForoosh,
--fldLastFee,fldMianginFee,fldNameVahed,fldNameVahed2 )
--values (1,2,'3',4,5,6,7,7.1,'8',9,10,11,12,'13','14')


INSERT INTO #TempMojoodieAnbar5
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeKharid



INSERT INTO #TempMojoodieAnbar5
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeForoosh


UPDATE       #TempMojoodieAnbar5
SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
FROM            tblKala INNER JOIN
                         #TempMojoodieAnbar5 ON tblKala.fldCodeKala = #TempMojoodieAnbar5.fldCodeKala


UPDATE       #TempMojoodieAnbar5
SET                #TempMojoodieAnbar5.fldGroupName = tblGroupKala.fldGroupName
FROM            tblGroupKala INNER JOIN
                         #TempMojoodieAnbar5 ON #TempMojoodieAnbar5.fldCodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempMojoodieAnbar5
SET                fldNameAnbar = tblAnbar.fldNameAnbar
FROM            tblAnbar INNER JOIN
                         #TempMojoodieAnbar5 ON tblAnbar.fldCode = #TempMojoodieAnbar5.fldCodeAnbar


update #TempMojoodieAnbar5 set #TempMojoodieAnbar5.fldNameVahed=tbl2.fldNameVahed,#TempMojoodieAnbar5.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

where tbl2.fldCodeKala=#TempMojoodieAnbar5.fldCodeKala

delete  from #TempMojoodieAnbar5  where fldCodeKala<>11001
Select * from #TempMojoodieAnbar5 
end

GO
/****** Object:  StoredProcedure [dbo].[KardeksNew2]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KardeksNew2]

--@mm nvarchar(10),
--@nn nvarchar(10)

as
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END

begin
 

create table #TempMojoodieAnbar5(

fldGroupName nvarchar(50)  COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100)  COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 0),
fldTedadeKhorooj decimal(18, 0),
fldTedadeJoz decimal(18, 0), 
fldTedadeKol decimal(18, 0), 
fldTedadDarKarton decimal(18, 0),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200)  COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 0),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100)  COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100)  COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20)  COLLATE Persian_100_CI_AS
)



--insert into #TempMojoodieAnbar5 (
--fldCodeGroup,fldCodeKala,fldNameKala,fldTedadeVorood,fldTedadeKhorooj,
--fldTedadeJoz,fldTedadeKol,fldCodeAnbar,fldNameAnbar,fldNogheSefaresh,fldFeeForoosh,
--fldLastFee,fldMianginFee,fldNameVahed,fldNameVahed2 )
--values (1,2,'3',4,5,6,7,7.1,'8',9,10,11,12,'13','14')


INSERT INTO #TempMojoodieAnbar5
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeKharid



INSERT INTO #TempMojoodieAnbar5
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeForoosh


UPDATE       #TempMojoodieAnbar5
SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
FROM            tblKala INNER JOIN
                         #TempMojoodieAnbar5 ON tblKala.fldCodeKala = #TempMojoodieAnbar5.fldCodeKala


UPDATE       #TempMojoodieAnbar5
SET                #TempMojoodieAnbar5.fldGroupName = tblGroupKala.fldGroupName
FROM            tblGroupKala INNER JOIN
                         #TempMojoodieAnbar5 ON #TempMojoodieAnbar5.fldCodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempMojoodieAnbar5
SET                fldNameAnbar = tblAnbar.fldNameAnbar
FROM            tblAnbar INNER JOIN
                         #TempMojoodieAnbar5 ON tblAnbar.fldCode = #TempMojoodieAnbar5.fldCodeAnbar


update #TempMojoodieAnbar5 set #TempMojoodieAnbar5.fldNameVahed=tbl2.fldNameVahed,#TempMojoodieAnbar5.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

where tbl2.fldCodeKala=#TempMojoodieAnbar5.fldCodeKala

delete  from #TempMojoodieAnbar5  where fldCodeKala<>11001
Select * from #TempMojoodieAnbar5 
end

GO
/****** Object:  StoredProcedure [dbo].[Kardex]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[Kardex] 
@CodeKala bigint,
@DateFrom bigint,
@DateUntil bigint

AS
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END
BEGIN

create table #TempMojoodieAnbar6(

fldGroupName nvarchar(50)  COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100)  COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 0),
fldTedadeKhorooj decimal(18, 0),
fldTedadeJoz decimal(18, 0), 
fldTedadeKol decimal(18, 0), 
fldTedadDarKarton decimal(18, 0),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200)  COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 0),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100)  COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100)  COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20)  COLLATE Persian_100_CI_AS
)



--insert into #TempMojoodieAnbar6 (
--fldCodeGroup,fldCodeKala,fldNameKala,fldTedadeVorood,fldTedadeKhorooj,
--fldTedadeJoz,fldTedadeKol,fldCodeAnbar,fldNameAnbar,fldNogheSefaresh,fldFeeForoosh,
--fldLastFee,fldMianginFee,fldNameVahed,fldNameVahed2 )
--values (1,2,'3',4,5,6,7,7.1,'8',9,10,11,12,'13','14')


INSERT INTO #TempMojoodieAnbar6
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeKharid 
	where fldCodeKala=@CodeKala And fldDate between @Datefrom And @DateUntil




INSERT INTO #TempMojoodieAnbar6
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeForoosh
	where fldCodeKala=@CodeKala And fldDate between @Datefrom And @DateUntil


INSERT INTO #TempMojoodieAnbar6
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz,0 , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzForoosh
	where fldCodeKala=@CodeKala And fldDate between @Datefrom And @DateUntil


INSERT INTO #TempMojoodieAnbar6
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0, fldTedadJoz , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzKharid
	where fldCodeKala=@CodeKala And fldDate between @Datefrom And @DateUntil


UPDATE       #TempMojoodieAnbar6
SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
FROM            tblKala INNER JOIN
                         #TempMojoodieAnbar6 ON tblKala.fldCodeKala = #TempMojoodieAnbar6.fldCodeKala


UPDATE       #TempMojoodieAnbar6
SET                #TempMojoodieAnbar6.fldGroupName = tblGroupKala.fldGroupName
FROM            tblGroupKala INNER JOIN
                         #TempMojoodieAnbar6 ON #TempMojoodieAnbar6.fldCodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempMojoodieAnbar6
SET                fldNameAnbar = tblAnbar.fldNameAnbar
FROM            tblAnbar INNER JOIN
                         #TempMojoodieAnbar6 ON tblAnbar.fldCode = #TempMojoodieAnbar6.fldCodeAnbar


update #TempMojoodieAnbar6 set #TempMojoodieAnbar6.fldNameVahed=tbl2.fldNameVahed,#TempMojoodieAnbar6.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

where tbl2.fldCodeKala=#TempMojoodieAnbar6.fldCodeKala

--delete  from #TempMojoodieAnbar6  where fldCodeKala<>1001
Select * from #TempMojoodieAnbar6 

END

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareBazPas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomareBazPas]
@Type nvarchar(50),
@ShomareBazPasJari bigint,
@NewShomareBazPas bigint output

as

begin
	if @Type='koochak'
		begin
		 set @NewShomareBazPas=(	SELECT        ISNULL( max(fldShomareBazPas),@ShomareBazPasJari) AS Expr1
				FROM            tblCheckePardakhti
				WHERE        (fldShomareBazPas < @ShomareBazPasJari))
		 --insert into tbltest7	(fld2)
			--values(@ShomarePardakhteJari)
		end

	if @Type='bozorg'
		begin
		set @NewShomareBazPas=(	SELECT        isnull( min(fldShomareBazPas),@ShomareBazPasJari) AS Expr1
				FROM            tblCheckePardakhti
				WHERE        (fldShomareBazPas > @ShomareBazPasJari))
		end
end
GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareDaryaft]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[KoochakBozorgShomareDaryaft]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareDaryafteJari bigint,
@NewShomareDaryaft bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 --set @NewShomareDaryaft=(	SELECT        ISNULL( max(fldShomareDaryaft),@ShomareDaryafteJari) AS Expr1
			--	FROM            tblDaryaft
			--	WHERE        (fldShomareDaryaft < @ShomareDaryafteJari and fldType=@TypeForm))
			set @NewShomareDaryaft=(	SELECT        ISNULL( max(fldShomareDaryaft),@ShomareDaryafteJari) AS Expr1
				FROM            tblDaryaft
				WHERE        (fldShomareDaryaft < @ShomareDaryafteJari ))
		
		end

	if @Type='bozorg'
		begin
		--set @NewShomareDaryaft=(	SELECT        isnull( min(fldShomareDaryaft),@ShomareDaryafteJari) AS Expr1
		--		FROM            tblDaryaft
		--		WHERE        (fldShomareDaryaft > @ShomareDaryafteJari  and fldType=@TypeForm))
		set @NewShomareDaryaft=(	SELECT        isnull( min(fldShomareDaryaft),@ShomareDaryafteJari) AS Expr1
				FROM            tblDaryaft
				WHERE        (fldShomareDaryaft > @ShomareDaryafteJari ))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareEnteghal]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[KoochakBozorgShomareEnteghal]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareEnteghalJari bigint,
@NewShomareEnteghal bigint output

as

begin
      
	   SET NOCOUNT ON;
	if @Type='koochak'
		begin
		 set @NewShomareEnteghal=(	SELECT        ISNULL( max(fldShomareEnteghal),@ShomareEnteghalJari) AS Expr1
				FROM            tblRizeForoosh
				WHERE        (fldShomareEnteghal < @ShomareEnteghalJari and fldType=@TypeForm))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareEnteghal=(	SELECT        isnull( min(fldShomareEnteghal),@ShomareEnteghalJari) AS Expr1
				FROM            tblRizeForoosh
				WHERE        (fldShomareEnteghal > @ShomareEnteghalJari  and fldType=@TypeForm))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareFactorAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomareFactorAmani]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareFactorForooshJari bigint,
@NewShomareFactorForoosh bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 set @NewShomareFactorForoosh=(	SELECT        ISNULL( max(fldShomareAmani),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizeForoosh
				WHERE        (fldShomareAmani < @ShomareFactorForooshJari and fldType=@TypeForm))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareFactorForoosh=(	SELECT        isnull( min(fldShomareAmani),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizeForoosh
				WHERE        (fldShomareAmani > @ShomareFactorForooshJari  and fldType=@TypeForm))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareFactorBargashtAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[KoochakBozorgShomareFactorBargashtAmani]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareFactorForooshJari bigint,
@NewShomareFactorForoosh bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 set @NewShomareFactorForoosh=(	SELECT        ISNULL( max(fldShomareBargashtAmani),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizeBargashtAzForoosh
				WHERE        (fldShomareBargashtAmani < @ShomareFactorForooshJari and fldType=@TypeForm))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareFactorForoosh=(	SELECT        isnull( min(fldShomareBargashtAmani),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizeBargashtAzForoosh
				WHERE        (fldShomareBargashtAmani > @ShomareFactorForooshJari  and fldType=@TypeForm))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareFactorBargashtAzForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[KoochakBozorgShomareFactorBargashtAzForoosh]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareFactorBargashtAzForooshJari bigint,
@NewShomareFactorBargashtAzForoosh bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 set @NewShomareFactorBargashtAzForoosh=(	SELECT        ISNULL( max(fldShomareFactor),@ShomareFactorBargashtAzForooshJari) AS Expr1
				FROM            tblRizeBargashtAzForoosh
				WHERE        (fldShomareFactor < @ShomareFactorBargashtAzForooshJari))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareFactorBargashtAzForoosh=(	SELECT        isnull( min(fldShomareFactor),@ShomareFactorBargashtAzForooshJari) AS Expr1
				FROM            tblRizeBargashtAzForoosh
				WHERE        (fldShomareFactor > @ShomareFactorBargashtAzForooshJari ))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareFactorBargashtAzKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomareFactorBargashtAzKharid]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareFactorKharidJari bigint,
@NewShomareFactorKharid bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 set @NewShomareFactorKharid=(	SELECT        ISNULL( max(fldShomareFactor),@ShomareFactorKharidJari) AS Expr1
				FROM            tblRizeBargashtAzKharid
				WHERE        (fldShomareFactor < @ShomareFactorKharidJari and fldType=@TypeForm))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareFactorKharid=(	SELECT        isnull( min(fldShomareFactor),@ShomareFactorKharidJari) AS Expr1
				FROM            tblRizeBargashtAzKharid
				WHERE        (fldShomareFactor > @ShomareFactorKharidJari  and fldType=@TypeForm))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareFactorForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomareFactorForoosh]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareFactorForooshJari bigint,
@NewShomareFactorForoosh bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 set @NewShomareFactorForoosh=(	SELECT        ISNULL( max(fldShomareFactor),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizeForoosh
				WHERE        (fldShomareFactor < @ShomareFactorForooshJari and fldType=@TypeForm))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareFactorForoosh=(	SELECT        isnull( min(fldShomareFactor),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizeForoosh
				WHERE        (fldShomareFactor > @ShomareFactorForooshJari  and fldType=@TypeForm))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareFactorKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomareFactorKharid]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareFactorKharidJari bigint,
@NewShomareFactorKharid bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 set @NewShomareFactorKharid=(	SELECT        ISNULL( max(fldShomareFactor),@ShomareFactorKharidJari) AS Expr1
				FROM            tblRizeKharid
				WHERE        (fldShomareFactor < @ShomareFactorKharidJari and fldType=@TypeForm))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareFactorKharid=(	SELECT        isnull( min(fldShomareFactor),@ShomareFactorKharidJari) AS Expr1
				FROM            tblRizeKharid
				WHERE        (fldShomareFactor > @ShomareFactorKharidJari  and fldType=@TypeForm))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareFactorPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomareFactorPishForoosh]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareFactorForooshJari bigint,
@NewShomareFactorForoosh bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 set @NewShomareFactorForoosh=(	SELECT        ISNULL( max(fldShomareFactor),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizePishForoosh
				WHERE        (fldShomareFactor < @ShomareFactorForooshJari and fldType=@TypeForm))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareFactorForoosh=(	SELECT        isnull( min(fldShomareFactor),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizePishForoosh
				WHERE        (fldShomareFactor > @ShomareFactorForooshJari  and fldType=@TypeForm))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareFactorPishPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomareFactorPishPishForoosh]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareFactorForooshJari bigint,
@NewShomareFactorForoosh bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 set @NewShomareFactorForoosh=(	SELECT        ISNULL( max(fldShomareFactor),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizePishPishForoosh
				WHERE        (fldShomareFactor < @ShomareFactorForooshJari and fldType=@TypeForm))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareFactorForoosh=(	SELECT        isnull( min(fldShomareFactor),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizePishPishForoosh
				WHERE        (fldShomareFactor > @ShomareFactorForooshJari  and fldType=@TypeForm))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareHavale]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomareHavale]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareHavaleJari bigint,
@NewShomareHavale bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 --set @NewShomareHavale=(	SELECT        ISNULL( max(fldShomareHavale),@ShomareHavaleeJari) AS Expr1
			--	FROM            tblHavale
			--	WHERE        (fldShomareHavale < @ShomareHavaleeJari and fldType=@TypeForm))
			set @NewShomareHavale=(	SELECT        ISNULL( max(fldShomareHavale),@ShomareHavaleJari) AS Expr1
				FROM            tblHavaleGoroohi
				WHERE        (fldShomareHavale < @ShomareHavaleJari ))
		
		end

	if @Type='bozorg'
		begin
		--set @NewShomareHavale=(	SELECT        isnull( min(fldShomareHavale),@ShomareHavaleeJari) AS Expr1
		--		FROM            tblHavale
		--		WHERE        (fldShomareHavale > @ShomareHavaleeJari  and fldType=@TypeForm))
		set @NewShomareHavale=(	SELECT        isnull( min(fldShomareHavale),@ShomareHavaleJari) AS Expr1
				FROM            tblHavaleGoroohi
				WHERE        (fldShomareHavale > @ShomareHavaleJari ))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomarePardakht]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[KoochakBozorgShomarePardakht]
@Type nvarchar(50),
@ShomarePardakhteJari bigint,
@NewShomarePardakht bigint output

as

begin
	if @Type='koochak'
		begin
		 set @NewShomarePardakht=(	SELECT        ISNULL( max(fldShomarePardakht),@ShomarePardakhteJari) AS Expr1
				FROM            tblPardakht
				WHERE        (fldShomarePardakht < @ShomarePardakhteJari) and fldType<>'hazine'  and fldType<>'enteghalevajh' and fldType<>'pardakhtpersonel' and fldType<>'pardakhtsayer') 
		 --insert into tbltest7	(fld2)
			--values(@ShomarePardakhteJari)
		end

	if @Type='bozorg'
		begin
		set @NewShomarePardakht=(	SELECT        isnull( min(fldShomarePardakht),@ShomarePardakhteJari) AS Expr1
				FROM            tblPardakht
				WHERE        (fldShomarePardakht > @ShomarePardakhteJari) and fldType<>'hazine'  and fldType<>'enteghalevajh' and fldType<>'pardakhtpersonel' and fldType<>'pardakhtsayer') 
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomarePardakhtEnteghaleVajh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomarePardakhtEnteghaleVajh]
@Type nvarchar(50),
@ShomarePardakhteJari bigint,
@NewShomarePardakht bigint output

as

begin
	if @Type='koochak'
		begin
		 set @NewShomarePardakht=(	SELECT        ISNULL( max(fldShomarePardakht),@ShomarePardakhteJari) AS Expr1
				FROM            tblPardakht
				WHERE        (fldShomarePardakht < @ShomarePardakhteJari) and fldType='enteghalevajh') 
		 --insert into tbltest7	(fld2)
			--values(@ShomarePardakhteJari)
		end

	if @Type='bozorg'
		begin
		set @NewShomarePardakht=(	SELECT        isnull( min(fldShomarePardakht),@ShomarePardakhteJari) AS Expr1
				FROM            tblPardakht
				WHERE        (fldShomarePardakht > @ShomarePardakhteJari) and fldType='enteghalevajh') 
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomarePardakhtHazine]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomarePardakhtHazine]
@Type nvarchar(50),
@ShomarePardakhteJari bigint,
@NewShomarePardakht bigint output

as

begin
	if @Type='koochak'
		begin
		 set @NewShomarePardakht=(	SELECT        ISNULL( max(fldShomarePardakht),@ShomarePardakhteJari) AS Expr1
				FROM            tblPardakht
				WHERE        (fldShomarePardakht < @ShomarePardakhteJari) and fldType='hazine') 
		 --insert into tbltest7	(fld2)
			--values(@ShomarePardakhteJari)
		end

	if @Type='bozorg'
		begin
		set @NewShomarePardakht=(	SELECT        isnull( min(fldShomarePardakht),@ShomarePardakhteJari) AS Expr1
				FROM            tblPardakht
				WHERE        (fldShomarePardakht > @ShomarePardakhteJari) and fldType='hazine') 
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomarePardakhtPersonel]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomarePardakhtPersonel]
@Type nvarchar(50),
@ShomarePardakhteJari bigint,
@NewShomarePardakht bigint output

as

begin
	if @Type='koochak'
		begin
		 set @NewShomarePardakht=(	SELECT        ISNULL( max(fldShomarePardakht),@ShomarePardakhteJari) AS Expr1
				FROM            tblPardakht
				WHERE        (fldShomarePardakht < @ShomarePardakhteJari) and fldType='pardakhtpersonel') 
		 --insert into tbltest7	(fld2)
			--values(@ShomarePardakhteJari)
		end

	if @Type='bozorg'
		begin
		set @NewShomarePardakht=(	SELECT        isnull( min(fldShomarePardakht),@ShomarePardakhteJari) AS Expr1
				FROM            tblPardakht
				WHERE        (fldShomarePardakht > @ShomarePardakhteJari) and fldType='pardakhtpersonel') 
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomarePardakhtSayer]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomarePardakhtSayer]
@Type nvarchar(50),
@ShomarePardakhteJari bigint,
@NewShomarePardakht bigint output

as

begin
	if @Type='koochak'
		begin
		 set @NewShomarePardakht=(	SELECT        ISNULL( max(fldShomarePardakht),@ShomarePardakhteJari) AS Expr1
				FROM            tblPardakht
				WHERE        (fldShomarePardakht < @ShomarePardakhteJari) and fldType='pardakhtsayer') 
		 --insert into tbltest7	(fld2)
			--values(@ShomarePardakhteJari)
		end

	if @Type='bozorg'
		begin
		set @NewShomarePardakht=(	SELECT        isnull( min(fldShomarePardakht),@ShomarePardakhteJari) AS Expr1
				FROM            tblPardakht
				WHERE        (fldShomarePardakht > @ShomarePardakhteJari) and fldType='pardakhtsayer') 
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomarePasCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomarePasCheck]
@Type nvarchar(50),
--@TypeForm nvarchar(50),
@ShomarePasJari bigint,
@NewShomarePas bigint output

as

begin
      
	   SET NOCOUNT ON;
	if @Type='koochak'
		begin
		 set @NewShomarePas=(SELECT   ISNULL(max(fldShomarePas),@ShomarePasJari) AS Expr1
				FROM            tblPas
				WHERE        (fldShomarePas < @ShomarePasJari))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomarePas=(	SELECT   isnull(min(fldShomarePas),@ShomarePasJari) AS Expr1
				FROM            tblPas
				WHERE        (fldShomarePas > @ShomarePasJari ))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareSanad_MojodiEbteda_doreh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[KoochakBozorgShomareSanad_MojodiEbteda_doreh]
@Type nvarchar(50),
@TypeForm nvarchar(50),
@ShomareSanadJari bigint,
@NewShomareSanad bigint output

as

begin
      
	   SET NOCOUNT ON;
	if @Type='koochak'
		begin
		 set @NewShomareSanad=(	SELECT        ISNULL( max(fldShomareSanad),@ShomareSanadJari) AS Expr1
				FROM            tblRizeKharid
				WHERE        (fldShomareSanad < @ShomareSanadJari and fldType=@TypeForm))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareSanad=(	SELECT        isnull( min(fldShomareSanad),@ShomareSanadJari) AS Expr1
				FROM            tblRizeKharid
				WHERE        (fldShomareSanad > @ShomareSanadJari  and fldType=@TypeForm))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareTolid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[KoochakBozorgShomareTolid]
@Type nvarchar(50),
@ShomareTolidJari bigint,
@NewShomareTolid bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 set @NewShomareTolid=(	SELECT        ISNULL( max(fldShomareTolid),@ShomareTolidJari) AS Expr1
				FROM            tblRizeTolid
				WHERE        (fldShomareTolid < @ShomareTolidJari ))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareTolid=(	SELECT        isnull( min(fldShomareTolid),@ShomareTolidJari) AS Expr1
				FROM            tblRizeTolid
				WHERE        (fldShomareTolid > @ShomareTolidJari  ))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareVam]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[KoochakBozorgShomareVam]
@Type nvarchar(50),
@ShomareVamJari bigint,
@NewShomareVam bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='koochak'
		begin
		 set @NewShomareVam=(	SELECT        ISNULL( max(fldShomareVam),@ShomareVamJari) AS Expr1
				FROM            tblVamInfo
				WHERE        (fldShomareVam < @ShomareVamJari ))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareVam=(	SELECT        isnull( min(fldShomareVam),@ShomareVamJari) AS Expr1
				FROM            tblVamInfo
				WHERE        (fldShomareVam > @ShomareVamJari  ))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[KoochakBozorgShomareVosoolCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[KoochakBozorgShomareVosoolCheck]
@Type nvarchar(50),
--@TypeForm nvarchar(50),
@ShomareVosoolJari bigint,
@NewShomareVosool bigint output

as

begin
      
	   SET NOCOUNT ON;
	if @Type='koochak'
		begin
		 set @NewShomareVosool=(SELECT   ISNULL(max(fldShomareVosool),@ShomareVosoolJari) AS Expr1
				FROM            tblVosool
				WHERE        (fldShomareVosool < @ShomareVosoolJari))
		
		end

	if @Type='bozorg'
		begin
		set @NewShomareVosool=(	SELECT   isnull(min(fldShomareVosool),@ShomareVosoolJari) AS Expr1
				FROM            tblVosool
				WHERE        (fldShomareVosool > @ShomareVosoolJari ))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[ListeAghsateTasfieNashode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeAghsateTasfieNashode] 
--چندزبانه
	@CodeTafzili nvarchar(10),
	@ShomareDaryaft bigint
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


CREATE TABLE #tmpDaryaftTasfie2(
	[fldShomareVam] [bigint]  null,
	[fldShomareGhest] [bigint]  null,
	[SumDaryafti] decimal(18,4) null,
	[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldMablagheMande]decimal(18,4),	
	[fldMablagheDaryaftiJari] decimal(18,4),
	[fldDateSarResid][bigint],
	[ShomareDaryaft][bigint]

	)

		

	insert into #tmpDaryaftTasfie2
		(fldShomareVam,fldShomareGhest,SumDaryafti,fldCodeTafzili,fldMablagheMande,fldDateSarResid)
		Select  tblDaftarcheAghsat.fldShomareVam,fldRadif,0,fldCodeTafzili, tblDaftarcheAghsat.fldMablaghGhest,fldDateSarResid
			from tblDaftarcheAghsat
			inner join tblVamInfo
			on tblDaftarcheAghsat.fldShomareVam =tblVamInfo.fldShomareVam
			where fldCodeTafzili = @CodeTafzili
	--insert into #tmpDaryaftTasfie2
	--	(fldShomarePeigiri,fldShomareVam,fldShomareGhest,SumDaryafti,fldCodeTafzili,fldMablagheMande,fldDateSarResid)
	--	Select Convert(varchar,tblDaftarcheAghsat.fldShomareVam) +  Convert(varchar,tblDaftarcheAghsat.fldRadif), tblDaftarcheAghsat.fldShomareVam,fldRadif,0,fldCodeTafzili, tblDaftarcheAghsat.fldMablaghGhest,fldDateSarResid
	--		from tblDaftarcheAghsat
	--		inner join tblVamInfo
	--		on tblDaftarcheAghsat.fldShomareVam =tblVamInfo.fldShomareVam
	--		where fldCodeTafzili = @CodeTafzili
	--select * from #tmpDaryaftTasfie2
	update #tmpDaryaftTasfie2
		set SumDaryafti=tbl1.sumDaryafti from		
		(Select fldShomareVam,fldShomareGhest, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfieVam group by fldShomareVam,fldShomareGhest ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie2.fldShomareFactor) as tbl1
		where #tmpDaryaftTasfie2.fldShomareVam=tbl1.fldShomareVam and  #tmpDaryaftTasfie2.fldShomareGhest=tbl1.fldShomareGhest


		update #tmpDaryaftTasfie2 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
			(Select fldShomareVam,fldShomareGhest, fldMablagh from tblRizeTasfieVam Where fldShomareDaryaft=@ShomareDaryaft) as tbl2
			where tbl2.fldShomareVam=#tmpDaryaftTasfie2.fldShomareVam and tbl2.fldShomareGhest=#tmpDaryaftTasfie2.fldShomareGhest
	--update #tmpDaryaftTasfie2 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
	--	(Select (Convert(varchar,fldShomareVam) +  Convert(varchar,fldShomareGhest)) As fldShomarePeigiri, fldMablagh from tblRizeTasfieVam Where fldShomareDaryaft=@ShomareDaryaft) as tbl2
	--	where tbl2.fldShomareVam=#tmpDaryaftTasfie2.fldShomareVam

	update #tmpDaryaftTasfie2 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null

	Select fldShomareVam,fldShomareGhest,SumDaryafti,fldMablagheMande,fldMablagheDaryaftiJari,
		substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid
		from #tmpDaryaftTasfie2
			where fldMablagheMande>SumDaryafti or fldMablagheDaryaftiJari>0
		order by fldDateSarResid,fldShomareVam

END

GO
/****** Object:  StoredProcedure [dbo].[ListeAshkhasBaMande]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeAshkhasBaMande]
	-- Add the parameters for the stored procedure here	
	@Filter nvarchar(15) output,
	@BetafkikeVizitor int,
	@Mobile int,
	@Date int,
	@ListGroupAshkhas varchar(5000)=null
AS

IF 1=0 BEGIN
 SET FMTONLY OFF
END

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	create table #TempListeAshkhas3(

		fldCodetafzili nvarchar(10)  COLLATE Persian_100_CI_AS,
		fldSharheTafzili nvarchar(200) COLLATE Persian_100_CI_AS,
		fldBedehkar bigint,
		fldBestankar Bigint,
		fldMande bigint,
		fldAdress nvarchar(200) COLLATE Persian_100_CI_AS,
		fldTell nvarchar(50) COLLATE Persian_100_CI_AS,
		fldCodeGroup nvarchar(100) COLLATE Persian_100_CI_AS,
		fldNameGroup nvarchar(50) COLLATE Persian_100_CI_AS,
		fldVisitor bigint,
		fldNameVisitor nvarchar(200) COLLATE Persian_100_CI_AS
	)
	
    declare @CodeKolDaryaftani nvarchar(100),@CodeKolPardakhtani nvarchar(100)

	execute CodeKolHesab 'kolhesabhayedaryaftani',@CodeKolDaryaftani output
	execute CodeKolHesab 'kolhesabhayepardakhtani',@CodeKolPardakhtani output
	--declare @BetafkikeVizitor int,@Filter nvarchar(15)
	--set @BetafkikeVizitor=1
	--set @Filter ='bedehkaran'
	--delete #TempListeAshkhas3
	if @BetafkikeVizitor=1
		Insert into #TempListeAshkhas3 (fldCodetafzili,fldBedehkar,fldBestankar,fldMande,fldVisitor)	
			SELECT        fldCodeTafzili, SUM(fldBedehkar) AS sumBed, SUM(fldBestankar) AS SumBes, SUM(fldBedehkar - fldBestankar) AS Mande,fldVasete
				FROM            tblSodooreSanad
				WHERE   ( Convert(bigint,fldDate)<=@Date) and (     (fldCodeKol = @CodeKolDaryaftani) OR
										 (fldCodeKol= @CodeKolPardakhtani) and fldCodeNoeTafzili='1' )
				GROUP BY fldCodeTafzili,fldVasete
	else		
		Insert into #TempListeAshkhas3 (fldCodetafzili,fldBedehkar,fldBestankar,fldMande)	
			SELECT        fldCodeTafzili, SUM(fldBedehkar) AS sumBed, SUM(fldBestankar) AS SumBes, SUM(fldBedehkar - fldBestankar) AS Mande
				FROM            tblSodooreSanad
				WHERE   ( Convert(bigint,fldDate)<=@Date) and (     (fldCodeKol = @CodeKolDaryaftani) OR
										 (fldCodeKol= @CodeKolPardakhtani) and fldCodeNoeTafzili='1' )
				GROUP BY fldCodeTafzili


	Insert into #TempListeAshkhas3 (fldCodetafzili,fldBedehkar,fldBestankar,fldMande)
		Select fldCodeTafzili, 0, 0, 0 from tblTafzili
			Where fldCodeNoe='1' and fldCodeTafzili Not In (Select fldCodeTafzili from #TempListeAshkhas3)

	if @BetafkikeVizitor=1
		update #TempListeAshkhas3  set fldSharheTafzili=tblTafzili.fldSharheTafzili,  fldAdress=tblTafzili.fldAddress  from tblTafzili
			where #TempListeAshkhas3.fldCodetafzili=tblTafzili.fldCodeTafzili
	else
		update #TempListeAshkhas3  set fldSharheTafzili=tblTafzili.fldSharheTafzili,  fldAdress=tblTafzili.fldAddress , fldVisitor=tblTafzili.fldVisitor from tblTafzili
			where #TempListeAshkhas3.fldCodetafzili=tblTafzili.fldCodeTafzili


	--declare @EmpCodeTafzili nvarchar(10)
	--declare EmpCursor Cursor For
	--	Select fldCodetafzili from #TempListeAshkhas3  With (NoLock) 
	--		open EmpCursor
	--		fetch Next From EmpCursor into @EmpCodeTafzili
	--		While @@Fetch_Status = 0
	--			Begin	
					

									
	--				update #TempListeAshkhas3  set  fldTell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
	--						where fldCodetafzili=@EmpCodeTafzili
	--				Fetch Next from EmpCursor
	--			into @EmpCodeTafzili
	--		end
	--Close EmpCurSor
	--Deallocate EmpCursor

	--update #TempListeAshkhas3  set  fldTell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
	--						where fldCodetafzili=@EmpCodeTafzili

	--update #TempListeAshkhas3  set  fldTell=
	--(select fldShomare from
	--	(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
	--	FROM            [tblDaftarcheTell]) DB_Address
	--	where RowNo=1) as tbl2
	--	where #TempListeAshkhas3.fldCodetafzili=tbl1.fldCodetafzili


		update #TempListeAshkhas3 set fldTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempListeAshkhas3.fldCodetafzili=tbl5.fldCodeTafzili


	--if @mobile=1 
	--	begin
	--		declare @EmpCodeTafzili nvarchar(10)
	--		declare EmpCursor Cursor For
	--			Select fldCodetafzili from #TempListeAshkhas3  With (NoLock) 
	--				open EmpCursor
	--				fetch Next From EmpCursor into @EmpCodeTafzili
	--				While @@Fetch_Status = 0
	--					Begin	
					

									
	--						update #TempListeAshkhas3  set  fldTell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
	--								where fldCodetafzili=@EmpCodeTafzili
	--						Fetch Next from EmpCursor
	--					into @EmpCodeTafzili
	--				end
	--		Close EmpCurSor
	--		Deallocate EmpCursor
	--	end


	--Select fldShomare from tblTafzili
	update #TempListeAshkhas3 set fldCodeGroup=tblTafzili.fldCodeGroup from tblTafzili Where #TempListeAshkhas3.fldCodetafzili=tblTafzili.fldCodeTafzili

	update #TempListeAshkhas3 set fldNameGroup=tblGroupAshkhas.fldGroupName from tblGroupAshkhas Where #TempListeAshkhas3.fldCodeGroup=tblGroupAshkhas.fldGroupId

	update #TempListeAshkhas3 set fldNameVisitor=tblTafzili.fldSharheTafzili from tblTafzili Where #TempListeAshkhas3.fldVisitor=tblTafzili.fldCodeTafzili
	update #TempListeAshkhas3 set fldNameVisitor='بدون واسطه'  where fldNameVisitor is NUll

	if @Filter='bedehkaran' 
		delete #TempListeAshkhas3 Where fldMande<=0
	
	if @Filter='bestankaran'
		begin
			delete #TempListeAshkhas3 Where fldMande>=0
			update #TempListeAshkhas3 set fldMande=-1*fldMande
		end
	
	--برای حسابهایی که تفصیلی ندارن،برای سندهای دستی
	delete from #TempListeAshkhas3 Where fldCodetafzili =''

	if @ListGroupAshkhas<>''
		begin
		declare @StrDeleteGroupAshkhas varchar(5000)
		set  @StrDeleteGroupAshkhas='delete from #TempListeAshkhas3 
		    where '+ @ListGroupAshkhas + ''
			execute(@StrDeleteGroupAshkhas)
		end
	
	select fldNameGroup,fldCodetafzili,fldSharheTafzili, fldBedehkar,fldBestankar,fldMande,fldVisitor,fldTell,fldAdress,fldCodeGroup,fldNameVisitor from #TempListeAshkhas3


END

GO
/****** Object:  StoredProcedure [dbo].[ListeAshkhasBaMande_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeAshkhasBaMande_WithUserCode]
--چندزبانه
	-- Add the parameters for the stored procedure here	
	@fldCodeArz bigint,	
	@language nvarchar(10),
	@Filter nvarchar(15) output,
	@BetafkikeVizitor int,
	@Mobile int,
	@Date int,
	@ListGroupAshkhas varchar(5000)=null,
	@userCode bigint
AS

IF 1=0 BEGIN
 SET FMTONLY OFF
END

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	create table #TempListeAshkhas3(
		fldCodeArz bigint,
		fldNerkheArz decimal(18,4),
		fldCodetafzili nvarchar(10)  COLLATE Persian_100_CI_AS,
		fldSharheTafzili nvarchar(200) COLLATE Persian_100_CI_AS,
		fldBedehkar decimal(18,4),
		fldBestankar decimal(18,4),
		fldMande decimal(18,4),
		fldAdress nvarchar(200) COLLATE Persian_100_CI_AS,
		fldTell nvarchar(50) COLLATE Persian_100_CI_AS,
		fldCodeGroup nvarchar(100) COLLATE Persian_100_CI_AS,
		fldNameGroup nvarchar(50) COLLATE Persian_100_CI_AS,
		fldVisitor bigint,
		fldNameVisitor nvarchar(200) COLLATE Persian_100_CI_AS
	)
	
    declare @CodeKolDaryaftani nvarchar(100),@CodeKolPardakhtani nvarchar(100)

	execute CodeKolHesab 'kolhesabhayedaryaftani',@CodeKolDaryaftani output
	execute CodeKolHesab 'kolhesabhayepardakhtani',@CodeKolPardakhtani output
	--declare @BetafkikeVizitor int,@Filter nvarchar(15)
	--set @BetafkikeVizitor=1
	--set @Filter ='bedehkaran'
	--delete #TempListeAshkhas3
	update tblSodooreSanad set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null
	if @BetafkikeVizitor=1
		Insert into #TempListeAshkhas3 (fldCodeArz,fldNerkheArz, fldCodetafzili,fldBedehkar,fldBestankar,fldMande,fldVisitor)	
			SELECT       fldCodeArz,fldNerkheArz,  fldCodeTafzili, SUM(fldBedehkar) AS sumBed, SUM(fldBestankar) AS SumBes, SUM(fldBedehkar - fldBestankar) AS Mande,fldVasete
				FROM            tblSodooreSanad
				WHERE   ( Convert(bigint,fldDate)<=@Date) and (     (fldCodeKol = @CodeKolDaryaftani) OR
										 (fldCodeKol= @CodeKolPardakhtani) and fldCodeNoeTafzili='1' )
				GROUP BY fldCodeTafzili,fldVasete,fldCodeArz,fldNerkheArz
	else		
		Insert into #TempListeAshkhas3 (fldCodeArz,fldNerkheArz, fldCodetafzili,fldBedehkar,fldBestankar,fldMande)	
			SELECT      fldCodeArz,fldNerkheArz,   fldCodeTafzili, SUM(fldBedehkar) AS sumBed, SUM(fldBestankar) AS SumBes, SUM(fldBedehkar - fldBestankar) AS Mande
				FROM            tblSodooreSanad
				WHERE   ( Convert(bigint,fldDate)<=@Date) and (     (fldCodeKol = @CodeKolDaryaftani) OR
										 (fldCodeKol= @CodeKolPardakhtani) and fldCodeNoeTafzili='1' )
				GROUP BY fldCodeTafzili,fldCodeArz,fldNerkheArz


	Insert into #TempListeAshkhas3 (fldCodetafzili,fldBedehkar,fldBestankar,fldMande)
		Select fldCodeTafzili, 0, 0, 0 from tblTafzili
			Where fldCodeNoe='1' and fldCodeTafzili Not In (Select fldCodeTafzili from #TempListeAshkhas3)

	if @BetafkikeVizitor=1
		update #TempListeAshkhas3  set fldSharheTafzili=tblTafzili.fldSharheTafzili,  fldAdress=tblTafzili.fldAddress  from tblTafzili
			where #TempListeAshkhas3.fldCodetafzili=tblTafzili.fldCodeTafzili
	else
		update #TempListeAshkhas3  set fldSharheTafzili=tblTafzili.fldSharheTafzili,  fldAdress=tblTafzili.fldAddress , fldVisitor=tblTafzili.fldVisitor from tblTafzili
			where #TempListeAshkhas3.fldCodetafzili=tblTafzili.fldCodeTafzili


	--declare @EmpCodeTafzili nvarchar(10)
	--declare EmpCursor Cursor For
	--	Select fldCodetafzili from #TempListeAshkhas3  With (NoLock) 
	--		open EmpCursor
	--		fetch Next From EmpCursor into @EmpCodeTafzili
	--		While @@Fetch_Status = 0
	--			Begin	
					

									
	--				update #TempListeAshkhas3  set  fldTell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
	--						where fldCodetafzili=@EmpCodeTafzili
	--				Fetch Next from EmpCursor
	--			into @EmpCodeTafzili
	--		end
	--Close EmpCurSor
	--Deallocate EmpCursor

	--update #TempListeAshkhas3  set  fldTell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
	--						where fldCodetafzili=@EmpCodeTafzili

	--update #TempListeAshkhas3  set  fldTell=
	--(select fldShomare from
	--	(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
	--	FROM            [tblDaftarcheTell]) DB_Address
	--	where RowNo=1) as tbl2
	--	where #TempListeAshkhas3.fldCodetafzili=tbl1.fldCodetafzili


		update #TempListeAshkhas3 set fldTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempListeAshkhas3.fldCodetafzili=tbl5.fldCodeTafzili


	--if @mobile=1 
	--	begin
	--		declare @EmpCodeTafzili nvarchar(10)
	--		declare EmpCursor Cursor For
	--			Select fldCodetafzili from #TempListeAshkhas3  With (NoLock) 
	--				open EmpCursor
	--				fetch Next From EmpCursor into @EmpCodeTafzili
	--				While @@Fetch_Status = 0
	--					Begin	
					

									
	--						update #TempListeAshkhas3  set  fldTell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
	--								where fldCodetafzili=@EmpCodeTafzili
	--						Fetch Next from EmpCursor
	--					into @EmpCodeTafzili
	--				end
	--		Close EmpCurSor
	--		Deallocate EmpCursor
	--	end


	--Select fldShomare from tblTafzili
	update #TempListeAshkhas3 set fldCodeGroup=tblTafzili.fldCodeGroup from tblTafzili Where #TempListeAshkhas3.fldCodetafzili=tblTafzili.fldCodeTafzili

	update #TempListeAshkhas3 set fldNameGroup=tblGroupAshkhas.fldGroupName from tblGroupAshkhas Where #TempListeAshkhas3.fldCodeGroup=tblGroupAshkhas.fldGroupId

	update #TempListeAshkhas3 set fldNameVisitor=tblTafzili.fldSharheTafzili from tblTafzili Where #TempListeAshkhas3.fldVisitor=tblTafzili.fldCodeTafzili
	update #TempListeAshkhas3 set fldNameVisitor=(Select * from dbo.translatefunction(@LANGUAGE,18282))  where fldNameVisitor is NUll

	if @Filter='bedehkaran' 
		delete #TempListeAshkhas3 Where fldMande<=0
	
	if @Filter='bestankaran'
		begin
			delete #TempListeAshkhas3 Where fldMande>=0
			update #TempListeAshkhas3 set fldMande=-1*fldMande
		end
	
	--برای حسابهایی که تفصیلی ندارن،برای سندهای دستی
	delete from #TempListeAshkhas3 Where fldCodetafzili =''

	if @ListGroupAshkhas<>''
		begin
		declare @StrDeleteGroupAshkhas varchar(5000)
		set  @StrDeleteGroupAshkhas='delete from #TempListeAshkhas3 
		    where '+ @ListGroupAshkhas + ''
			execute(@StrDeleteGroupAshkhas)
		end
	
-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################	


if @fldCodeArz=1	 --واحد ارزی پایه
	begin
		update #TempListeAshkhas3 set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

		update #TempListeAshkhas3 set fldBedehkar=fldBedehkar/ fldNerkheArz,fldBestankar=fldBestankar/ fldNerkheArz,fldMande=fldMande/ fldNerkheArz --from tblCheckeDaryafti Where #TempReportCheck.fldShomareSanad=tblCheckeDaryafti.fldSanad
	end

if @fldCodeArz>1
	begin
			update #TempListeAshkhas3 set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

			declare @Nerkh decimal(18,4)

			set @Nerkh =(Select fldNerkheArz from tblVahedeArzi Where fldCodeArz=(@fldCodeArz))


			--تبدیل به نرخ پایه
			update #TempListeAshkhas3 set fldBedehkar=fldBedehkar/ fldNerkheArz,fldBestankar=fldBestankar/ fldNerkheArz,fldMande=fldMande/ fldNerkheArz --from tblSodooreSanad Where #TempReportCheck.fldShomareSanad=tblSodooreSanad.fldSanad --and fldCodeArz<>@fldCodeArz

				--تبدیل به نرخ روز ارز انتخابی
			update #TempListeAshkhas3 set fldBedehkar=fldBedehkar* @Nerkh,fldBestankar=fldBestankar* @Nerkh,fldMande=fldMande* @Nerkh
	end

	select fldNameGroup,fldCodetafzili,fldSharheTafzili, fldBedehkar,fldBestankar,fldMande,fldVisitor,fldTell,fldAdress,fldCodeGroup,fldNameVisitor 
	from #TempListeAshkhas3
	where fldCodetafzili in (select code from @accessCodes)


END

GO
/****** Object:  StoredProcedure [dbo].[ListeBedehkaran]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeBedehkaran]
	-- Add the parameters for the stored procedure here
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	--ناقصه باید کامل بشه
    declare @CodeKolDaryaftani nvarchar(100),@CodeKolPardakhtani nvarchar(100)

	execute CodeKolHesab 'kolhesabhayedaryaftani',@CodeKolDaryaftani output
	execute CodeKolHesab 'kolhesabhayepardakhtani',@CodeKolPardakhtani output

	SELECT        fldCodeTafzili, SUM(fldBedehkar) AS sumBed, SUM(fldBestankar) AS SumBes, SUM(fldBedehkar - fldBestankar) AS Mande
		FROM            tblSodooreSanad
		WHERE        (fldMoinKamel = @CodeKolDaryaftani) OR
								 (fldMoinKamel = @CodeKolPardakhtani)
		GROUP BY fldCodeTafzili
END

GO
/****** Object:  StoredProcedure [dbo].[ListeCheckhayePasNashode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeCheckhayePasNashode]
--چندزبانه
@fldCodeArz int
	-- Add the parameters for the stored procedure here
	
AS
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	--SELECT         ID, fldShomareCheck, fldShomareHesab, fldDateSarResid, SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 1, 4) 
 --                        + '/' + SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 7, 2) AS fldDateSarResidShow, 
 --                        fldCodeBank, fldShobe, fldMablagh, fldDatePardakht, SUBSTRING(CONVERT(nvarchar, fldDatePardakht), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, 
 --                        fldDatePardakht), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, fldDatePardakht), 7, 2) AS fldDatePardakhtShow, fldCodeSahebeCheck, fldShomarePardakht, 
 --                        fldCodeSahebeHesab, fldVasete, fldSanad, fldShomarePass, fldDatePass, fldSharhePass
	--	FROM            dbo.tblCheckePardakhti
	--		WHERE        (fldShomarePass IS NULL)
	--			ORDER BY fldDateSarResid
	create table #tblTemp
	(
		fldCodeArz bigint,
		fldNerkheArz decimal(18,4),
		ID bigint,
		fldShomareCheck bigint,
		fldShomareHesab bigint,
		fldDateSarResid int, 
	    fldDateSarResidShow nvarchar(20),
	    fldCodeBank bigint,
		fldSharheHesab nvarchar(50),
		fldCodeSahebeCheck bigint,
		fldSharheTafzili nvarchar(200),
	    fldMablagh decimal(18,4),
		fldDatePardakht int,
		fldDatePardakhtShow nvarchar(20), 
		fldShomarePardakht bigint,
		fldCodeSahebeHesab bigint,
		fldVasete bigint,
		fldSanad bigint, 
        fldShomarePass bigint,
		fldDatePass bigint,
		fldSharhePass nvarchar(200), 
		fldShobe nvarchar(40)
	)

	insert #tblTemp	
			(fldCodeArz,fldNerkheArz, ID ,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldSharheHesab,fldCodeSahebeCheck,fldSharheTafzili,fldMablagh,fldDatePardakht,fldDatePardakhtShow,fldShomarePardakht,fldCodeSahebeHesab,fldVasete,fldSanad, 
				fldShomarePass,fldDatePass,fldSharhePass,fldShobe)
			
	SELECT       fldCodeArz,fldNerkheArz, dbo.tblCheckePardakhti.ID, dbo.tblCheckePardakhti.fldShomareCheck, dbo.tblCheckePardakhti.fldShomareHesab, dbo.tblCheckePardakhti.fldDateSarResid, 
                         SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDateSarResid), 
                         5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResidShow,
						 dbo.tblCheckePardakhti.fldCodeBank,dbo.tblKolMoin.fldSharheHesab,  dbo.tblCheckePardakhti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,
                         dbo.tblCheckePardakhti.fldMablagh, dbo.tblCheckePardakhti.fldDatePardakht, SUBSTRING(CONVERT(nvarchar, 
                         dbo.tblCheckePardakhti.fldDatePardakht), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht), 5, 2) 
                         + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht), 7, 2) AS fldDatePardakhtShow, 
                         dbo.tblCheckePardakhti.fldShomarePardakht, dbo.tblCheckePardakhti.fldCodeSahebeHesab, dbo.tblCheckePardakhti.fldVasete, dbo.tblCheckePardakhti.fldSanad, 
                         dbo.tblCheckePardakhti.fldShomarePass, dbo.tblCheckePardakhti.fldDatePass, dbo.tblCheckePardakhti.fldSharhePass, 
                         dbo.tblCheckePardakhti.fldShobe
FROM            dbo.tblCheckePardakhti INNER JOIN
                         dbo.tblTafzili ON dbo.tblCheckePardakhti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
                         dbo.tblKolMoin ON dbo.tblCheckePardakhti.fldCodeBank = dbo.tblKolMoin.fldCodeHesab
WHERE        (dbo.tblCheckePardakhti.fldShomarePass IS NULL) and isnumeric(fldShomareBazPas)=0
			ORDER BY fldDateSarResid



if @fldCodeArz=1	 --واحد ارزی پایه
	begin
		update #tblTemp set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

		update #tblTemp set fldMablagh=fldMablagh/ fldNerkheArz --from tblCheckeDaryafti Where #TempReportCheck.fldShomareSanad=tblCheckeDaryafti.fldSanad
	end

if @fldCodeArz>1
	begin
			update #tblTemp set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

			declare @Nerkh decimal(18,4)

			set @Nerkh =(Select fldNerkheArz from tblVahedeArzi Where fldCodeArz=(@fldCodeArz))


			--تبدیل به نرخ پایه
			update #tblTemp set fldMablagh=fldMablagh/ fldNerkheArz --from tblSodooreSanad Where #TempReportCheck.fldShomareSanad=tblSodooreSanad.fldSanad --and fldCodeArz<>@fldCodeArz

				--تبدیل به نرخ روز ارز انتخابی
			update #tblTemp set fldMablagh=fldMablagh* @Nerkh
	end

	
	select ID ,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldSharheHesab,fldCodeSahebeCheck,fldSharheTafzili,fldMablagh,fldDatePardakht,fldDatePardakhtShow,fldShomarePardakht,fldCodeSahebeHesab,fldVasete,fldSanad, 
				fldShomarePass,fldDatePass,fldSharhePass,fldShobe
		from #tblTemp
END

GO
/****** Object:  StoredProcedure [dbo].[ListeCheckhayePasShode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeCheckhayePasShode] 
--چندزبانه
	-- Add the parameters for the stored procedure here
	@ShomarePas bigint
	
AS
BEGIN

	SELECT        dbo.tblCheckePardakhti.ID, dbo.tblCheckePardakhti.fldShomareCheck, dbo.tblCheckePardakhti.fldShomareHesab, dbo.tblCheckePardakhti.fldDateSarResid, 
                         SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDateSarResid), 
                         5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResidShow,
						 dbo.tblCheckePardakhti.fldCodeBank,dbo.tblKolMoin.fldSharheHesab,  dbo.tblCheckePardakhti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,
                         dbo.tblCheckePardakhti.fldMablagh, dbo.tblCheckePardakhti.fldDatePardakht, SUBSTRING(CONVERT(nvarchar, 
                         dbo.tblCheckePardakhti.fldDatePardakht), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht), 5, 2) 
                         + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht), 7, 2) AS fldDatePardakhtShow, 
                         dbo.tblCheckePardakhti.fldShomarePardakht, dbo.tblCheckePardakhti.fldCodeSahebeHesab, dbo.tblCheckePardakhti.fldVasete, dbo.tblCheckePardakhti.fldSanad, 
                         dbo.tblCheckePardakhti.fldShomarePass, dbo.tblCheckePardakhti.fldDatePass, dbo.tblCheckePardakhti.fldSharhePass, 
                         dbo.tblCheckePardakhti.fldShobe,fldSanadPas
FROM            dbo.tblCheckePardakhti INNER JOIN
                         dbo.tblTafzili ON dbo.tblCheckePardakhti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
                         dbo.tblKolMoin ON dbo.tblCheckePardakhti.fldCodeBank = dbo.tblKolMoin.fldCodeHesab
WHERE        (dbo.tblCheckePardakhti.fldShomarePass IS Not NULL) and dbo.tblCheckePardakhti.fldShomarePass=@ShomarePas
			ORDER BY fldDateSarResid

END

GO
/****** Object:  StoredProcedure [dbo].[ListeCodeKalaForooshBargashAzForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeCodeKalaForooshBargashAzForoosh] 
	-- Add the parameters for the stored procedure here
	@DateFrom bigint,
	@DateUntil bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	Create table #TempTable(
	fldCodeKala bigint
	)

	Insert into #TempTable
		(fldCodeKala)
		Select fldCodeKala from tblRizeForoosh Where fldDate between @DateFrom and @DateUntil

	Insert into #TempTable
		(fldCodeKala)
		Select fldCodeKala from tblRizeBargashtAzForoosh Where fldDate between @DateFrom and @DateUntil

	Select distinct fldCodekala from #TempTable
END

GO
/****** Object:  StoredProcedure [dbo].[ListeFactorhayeTasfieNashode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeFactorhayeTasfieNashode] 
--چندزبانه
	@CodeTafzili nvarchar(10),
	@ShomareDaryaft bigint
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


CREATE TABLE #tmpDaryaftTasfie2(	
	[fldShomareFactor] [bigint]  null,
	[SumDaryafti] decimal(18,4) null,
	[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldMablagheMande]decimal(18,4),
	[fldSharh][nvarchar](100) COLLATE Persian_100_CI_AS,
	[fldMablagheDaryaftiJari] decimal(18,4),
	[fldDateSarResid][bigint],
	[ShomareDaryaft][bigint]

	)

	--insert into #tmpDaryaftTasfie2
	--	(fldShomareFactor,SumDaryafti)
	--Select fldShomareFactor ,Sum(fldMablagh) as SumDaryafti from tblRizeTasfie   Group by fldShomareFactor
		


	insert into #tmpDaryaftTasfie2
		(fldShomareFactor,SumDaryafti,fldCodeTafzili,fldMablagheMande,fldSharh,fldDateSarResid)
		Select fldShomareFactor,0,fldCodeTafzili, fldMablagheMandeFactor,fldSharh,fldDateSarResid
			from tblTasfie
			where fldCodeTafzili = @CodeTafzili

	update #tmpDaryaftTasfie2
		set SumDaryafti=tbl1.sumDaryafti from		
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfie group by fldShomareFactor ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie2.fldShomareFactor) as tbl1
		where #tmpDaryaftTasfie2.fldShomareFactor=tbl1.fldShomareFactor

--FROM            tblTasfie

--	SELECT        #tmpDaryaftTasfie2.fldShomareFactor, #tmpDaryaftTasfie2.SumDaryafti, tblTasfie.fldCodeTafzili, tblTasfie.fldMablagheMande, tblTasfie.fldSharh, 
--							 tblTasfie.fldDateSarResid
--	FROM            #tmpDaryaftTasfie2 INNER JOIN
--							 tblTasfie ON #tmpDaryaftTasfie2.fldShomareFactor = tblTasfie.fldShomareFactor
--					WHERE        (tblTasfie.fldCodeTafzili = @CodeTafzili)


	update #tmpDaryaftTasfie2 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
		(Select fldShomareFactor, fldMablagh from tblRizeTasfie Where fldShomareDaryaft=@ShomareDaryaft) as tbl2
		where tbl2.fldShomareFactor=#tmpDaryaftTasfie2.fldShomareFactor

	update #tmpDaryaftTasfie2 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null

	Select fldShomareFactor,SumDaryafti,fldMablagheMande,fldSharh,fldMablagheDaryaftiJari,
		substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid
		from #tmpDaryaftTasfie2
			where fldMablagheMande>SumDaryafti or fldMablagheDaryaftiJari>0
		order by fldDateSarResid,fldShomareFactor

END

GO
/****** Object:  StoredProcedure [dbo].[ListeFactorhayeTasfieNashodeAlarm]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeFactorhayeTasfieNashodeAlarm] 
	--چندزبانه
	@fldCodeArz bigint,
	@CodeTafzili nvarchar(10),		
	@DataType varchar(40),
	@DateSarResidAz bigint,
	@DateSarResidTa bigint,
	@SumMandeTasfieNashode decimal(18,4) output
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


CREATE TABLE #tmpDaryaftTasfie4(	
	[fldShomareFactor] [bigint]  null,
	[SumDaryafti] [bigint] null,
	[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldMablagheMande] decimal(18,4),
	[fldmandeTasfienashode] decimal(18,4),
	[fldSharh][nvarchar](100) COLLATE Persian_100_CI_AS,	
	[fldDateSarResid][bigint],
	[fldDateFactor][bigint],
	[SharheTafzili][nvarchar](200) COLLATE Persian_100_CI_AS,
	[codeTafzili][nvarchar](10) COLLATE Persian_100_CI_AS,
	[Tell][nvarchar](50) COLLATE Persian_100_CI_AS,
	[Adress][nvarchar](200) COLLATE Persian_100_CI_AS

	)


	
	insert into #tmpDaryaftTasfie4
			(fldShomareFactor,fldCodeTafzili,SharheTafzili,fldMablagheMande,fldSharh,fldDateSarResid,fldDateFactor)
			Select distinct dbo.tblTasfie.fldShomareFactor,dbo.tblTafzili.fldCodeTafzili, dbo.tblTafzili.fldSharheTafzili, fldMablagheMandeFactor,fldSharh,fldDateSarResid, dbo.tblRizeForoosh.fldDate		
				from  dbo.tblTasfie INNER JOIN
				dbo.tblRizeForoosh  on dbo.tblTasfie.fldShomareFactor=dbo.tblRizeForoosh.fldShomareFactor 
				INNER JOIN dbo.tblTafzili on dbo.tblTasfie.fldCodeTafzili=dbo.tblTafzili.fldCodeTafzili
		if @Codetafzili<>0 --يعني همه افراد
			delete #tmpDaryaftTasfie4 where fldCodeTafzili<>@CodeTafzili

	update #tmpDaryaftTasfie4
		set SumDaryafti=tbl1.sumDaryafti from		
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfie group by fldShomareFactor ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor) as tbl1
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl1.fldShomareFactor





	update   #tmpDaryaftTasfie4 set fldmandeTasfienashode=fldMablagheMande-SumDaryafti

	--بروزرساني کد تفصيلي
	update #tmpDaryaftTasfie4 set codeTafzili=tbl3.fldCodeTafsili from
		(SELECT        tblForoosh.fldShomareFactorForoosh,tblRizeForoosh.fldCodeTafsili
			FROM            tblForoosh INNER JOIN
                         tblRizeForoosh ON tblForoosh.fldShomareFactorForoosh = tblRizeForoosh.fldShomareFactor) as tbl3
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl3.fldShomareFactorForoosh


	--بروزرساني تاريخ سررسيد bm
	update #tmpDaryaftTasfie4 set fldDateSarResid=tbl9.fldDateSarResid from
	(select fldDateSarResid,fldShomareFactor from tblTasfie) as tbl9
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl9.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateSarResid< @DateSarResidAz
		delete #tmpDaryaftTasfie4 Where fldDateSarResid> @DateSarResidTa



	if @DataType='tasfienashode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande=SumDaryafti
		end

	--بروزرساني تلفن
	declare @EmpCodeTafzili nvarchar(10)
	declare EmpCursor Cursor For
		Select fldCodetafzili from #tmpDaryaftTasfie4  With (NoLock) 
			open EmpCursor
			fetch Next From EmpCursor into @EmpCodeTafzili
			While @@Fetch_Status = 0
				Begin				
					update #tmpDaryaftTasfie4  set  Tell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
							where fldCodetafzili=@EmpCodeTafzili
					Fetch Next from EmpCursor
				into @EmpCodeTafzili
			end
	Close EmpCurSor
	Deallocate EmpCursor


	update #tmpDaryaftTasfie4  set Adress =tbl9.fldAddress from
		(Select fldAddress,fldCodeTafzili from tblTafzili) as tbl9			
		where #tmpDaryaftTasfie4.fldCodeTafzili=tbl9.fldCodeTafzili

	set @sumMandeTasfieNashode=(Select Sum(fldmandeTasfienashode) as SumMande from #tmpDaryaftTasfie4)
	if @sumMandeTasfieNashode is null
		set @sumMandeTasfieNashode=0




	
			Select codeTafzili,SharheTafzili,fldShomareFactor,fldMablagheMande,SumDaryafti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2)  as fldDateSarResid,	
			Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4			
				order by fldDateSarResid,fldShomareFactor
		


END

GO
/****** Object:  StoredProcedure [dbo].[ListeFactorhayeTasfieNashodeReport]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeFactorhayeTasfieNashodeReport] 
	@ListGroupAshkhas varchar(5000)=null ,
	@ListCodeAshkhas varchar(5000)=null,
	@ListCodeVisitor varchar(5000)=null,
	@CodeTafzili nvarchar(10),		
	@DataType varchar(40),
	@DateSarResidAz bigint,
	@DateSarResidTa bigint,
	@DateFactorAz bigint,
	@DateFactorTa bigint,
	@FlagSort Varchar(15),
	@SumFactorha bigint output,
	@SumMandeFactor bigint output,
	@SumMandeTasfieNashode bigint output,
	@SumKoleDaryafti bigint output
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


CREATE TABLE #tmpDaryaftTasfie4(	
	[fldShomareFactor] [bigint]  null,
	[SumDaryafti] [bigint] null,
	[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldMablagheFactor][bigint],
	[fldMablagheMande][bigint],
	[fldMablagheBargashti][bigint],
	[fldmandeTasfienashode] [bigint],
	[fldSharh][nvarchar](100) COLLATE Persian_100_CI_AS,	
	[fldDateSarResid][bigint],
	[fldDateFactor][bigint],
	[ShomareDaryaft][bigint],
	[SharheTafzili][nvarchar](200) COLLATE Persian_100_CI_AS,
	[codeTafzili][nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldCodeGroupAshkhas][bigint],
	[NameGroup][nvarchar](80) COLLATE Persian_100_CI_AS,
	[fldCodeVasete][bigint],
	[NameVasete][nvarchar](200) COLLATE Persian_100_CI_AS,
	[Tell][nvarchar](50) COLLATE Persian_100_CI_AS,
	[Adress][nvarchar](200) COLLATE Persian_100_CI_AS

	)

	--insert into #tmpDaryaftTasfie4
	--	(fldShomareFactor,SumDaryafti)
	--Select fldShomareFactor ,Sum(fldMablagh) as SumDaryafti from tblRizeTasfie   Group by fldShomareFactor
		
	
	
	insert into #tmpDaryaftTasfie4
			(fldShomareFactor,SumDaryafti,fldMablagheBargashti,fldCodeTafzili,fldMablagheFactor,fldMablagheMande,fldSharh,fldDateSarResid,fldDateFactor)
			Select distinct dbo.tblTasfie.fldShomareFactor,0,0,fldCodeTafzili,fldMablagheFactor, fldMablagheMandeFactor,fldSharh,fldDateSarResid, dbo.tblRizeForoosh.fldDate		
				from  dbo.tblTasfie INNER JOIN
				dbo.tblRizeForoosh  on dbo.tblTasfie.fldShomareFactor=dbo.tblRizeForoosh.fldShomareFactor 

		if @Codetafzili<>0 --يعني همه افراد
			delete #tmpDaryaftTasfie4 where fldCodeTafzili<>@CodeTafzili

	update #tmpDaryaftTasfie4
		set SumDaryafti=tbl1.sumDaryafti from		
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfie where fldShomareDaryaft<>0 group by fldShomareFactor ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor) as tbl1
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl1.fldShomareFactor
	
	--بروزرساني جمع مبلغ برگشتي bm
	update #tmpDaryaftTasfie4
	set fldMablagheBargashti=tbl11.fldMablagheBargashti from
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as fldMablagheBargashti from tblRizeTasfie where fldShomareBargashtAzForoosh<>0 group by fldShomareFactor ) as tbl11
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl11.fldShomareFactor

		
	
	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
	--	(Select fldShomareFactor, fldMablagh from tblRizeTasfie Where fldShomareDaryaft=@ShomareDaryaft) as tbl2
	--	where tbl2.fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor

	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null
	update   #tmpDaryaftTasfie4 set fldmandeTasfienashode=fldMablagheMande-SumDaryafti - fldMablagheBargashti

	--بروزرساني کد تفصيلي
	update #tmpDaryaftTasfie4 set codeTafzili=tbl3.fldCodeTafsili from
		(SELECT        tblForoosh.fldShomareFactorForoosh,tblRizeForoosh.fldCodeTafsili
			FROM            tblForoosh INNER JOIN
                         tblRizeForoosh ON tblForoosh.fldShomareFactorForoosh = tblRizeForoosh.fldShomareFactor) as tbl3
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl3.fldShomareFactorForoosh

	--بروزرساني نام تفصيلي
	update #tmpDaryaftTasfie4 set SharheTafzili=tbl5.fldSharheTafzili from
		(Select fldSharheTafzili,fldCodeTafzili from tblTafzili) as tbl5
		where #tmpDaryaftTasfie4.codeTafzili=tbl5.fldCodeTafzili

	--بروزرساني کدگروه 
	update #tmpDaryaftTasfie4 set fldCodeGroupAshkhas=tbl6.fldCodeGroup from
		(Select fldCodeGroup,fldCodeTafzili from tblTafzili) as tbl6
		Where #tmpDaryaftTasfie4.fldCodeTafzili=tbl6.fldCodeTafzili
	
	--بروزرساني نام گروه
	update #tmpDaryaftTasfie4  set NameGroup=tbl6.fldGroupName from
		(Select fldGroupId,fldGroupName from tblGroupAshkhas) as tbl6
		where tbl6.fldGroupId=#tmpDaryaftTasfie4.fldCodeGroupAshkhas
	

	--بروزرساني کد ويزيتورها	
	update #tmpDaryaftTasfie4  set fldCodeVasete = tbl7.fldCodeVasete from
		(SELECT        tblForoosh.fldShomareFactorForoosh, tblRizeForoosh.fldCodeVasete
			FROM            tblForoosh INNER JOIN
							 tblRizeForoosh ON tblForoosh.fldShomareFactorForoosh = tblRizeForoosh.fldShomareFactor) as tbl7
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl7.fldShomareFactorForoosh

	--بروزرساني نام ويزيتورها
	update #tmpDaryaftTasfie4  set NameVasete=tbl8.fldSharheTafzili from
		(Select fldSharheTafzili,fldCodeTafzili from tblTafzili) as tbl8
		where #tmpDaryaftTasfie4.fldCodeVasete=tbl8.fldCodeTafzili  and fldCodeVasete<>0
	
	update #tmpDaryaftTasfie4 set NameVasete='' where fldCodeVasete=0

	--بروزرساني تاريخ سررسيد bm
	update #tmpDaryaftTasfie4 set fldDateSarResid=tbl9.fldDateSarResid from
	(select fldDateSarResid,fldShomareFactor from tblTasfie) as tbl9
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl9.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateSarResid< @DateSarResidAz
		delete #tmpDaryaftTasfie4 Where fldDateSarResid> @DateSarResidTa
	--بروزرساني تاريخ فاکتور bm
	update #tmpDaryaftTasfie4 set fldDateFactor=tbl10.fldDate from
	(select fldDate,fldShomareFactor from tblRizeForoosh) as tbl10
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl10.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateFactor< @DateFactorAz
		delete #tmpDaryaftTasfie4 Where fldDateFactor> @DateFactorTa

	 if @ListCodeAshkhas <>''
		begin
			declare @StrDeleteAshkhas varchar(5000)
			set  @StrDeleteAshkhas='delete from #tmpDaryaftTasfie4 
				where fldCodeTafzili NOT IN ('+ @ListCodeAshkhas +')'
				execute(@StrDeleteAshkhas)
		end


		if @ListGroupAshkhas<>''
			begin
			declare @StrDeleteGroupAshkhas varchar(5000)
			set  @StrDeleteGroupAshkhas='delete from #tmpDaryaftTasfie4 
				where '+ @ListGroupAshkhas + ''
				execute(@StrDeleteGroupAshkhas)
			end


		if @ListCodeVisitor<>''
			begin
				if @ListCodeVisitor='-1' --ليست فاکتورهاي بدون ويزيتور
					begin
						delete from #tmpDaryaftTasfie4 where fldCodeVasete<>0
					end
				else
					begin
					declare @StrDeleteVisitor varchar(5000)
					set  @StrDeleteVisitor='delete from #tmpDaryaftTasfie4 
						where '+ @ListCodeVisitor + ''
						execute(@StrDeleteVisitor)
					end
			end


	if @DataType='tasfienashode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande=(SumDaryafti+fldMablagheBargashti)
		end
	if @DataType='tasfieshode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande<>(SumDaryafti+fldMablagheBargashti)
		end
	--بروزرساني تلفن
	declare @EmpCodeTafzili nvarchar(10)
	declare EmpCursor Cursor For
		Select fldCodetafzili from #tmpDaryaftTasfie4  With (NoLock) 
			open EmpCursor
			fetch Next From EmpCursor into @EmpCodeTafzili
			While @@Fetch_Status = 0
				Begin				
					update #tmpDaryaftTasfie4  set  Tell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
							where fldCodetafzili=@EmpCodeTafzili
					Fetch Next from EmpCursor
				into @EmpCodeTafzili
			end
	Close EmpCurSor
	Deallocate EmpCursor


	update #tmpDaryaftTasfie4  set Adress =tbl9.fldAddress from
		(Select fldAddress,fldCodeTafzili from tblTafzili) as tbl9			
		where #tmpDaryaftTasfie4.fldCodeTafzili=tbl9.fldCodeTafzili

	set @sumMandeTasfieNashode=(Select Sum(fldmandeTasfienashode) as SumMande from #tmpDaryaftTasfie4)
	if @sumMandeTasfieNashode is null
		set @sumMandeTasfieNashode=0

	set @SumFactorha=(Select Sum(fldMablagheFactor) as SumMande from #tmpDaryaftTasfie4)
	if @SumFactorha is null
		set @SumFactorha=0

	set @SumMandeFactor=(Select Sum(fldMablagheMande) as SumMande from #tmpDaryaftTasfie4)
	if @SumMandeFactor is null
		set @SumMandeFactor=0


	set @SumKoleDaryafti=(Select Sum(SumDaryafti) as SumMande from #tmpDaryaftTasfie4)
	if @SumKoleDaryafti is null
		set @SumKoleDaryafti=0

	


	if @FlagSort='date'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2)  as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameVasete,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0
				order by fldDateSarResid,fldShomareFactor
		
	if @FlagSort='factor'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameVasete,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by fldShomareFactor,fldDateSarResid
	
	if @FlagSort='bk'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameVasete,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0		
				order by fldmandeTasfienashode desc--,fldShomareFactor,fldDateSarResid

	if @FlagSort='kb'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
				NameVasete,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by Convert(bigint,fldmandeTasfienashode) asc--,fldShomareFactor,fldDateSarResid

	else if @FlagSort='nameshakhs'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameVasete,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by SharheTafzili asc,fldShomareFactor,fldDateSarResid

	else if @FlagSort='namegorooh'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) +'f' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameVasete,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameGroup asc,fldShomareFactor,fldDateSarResid

	else --@FlagSort='namevizitor'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) + 'g' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameVasete,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameVasete asc,fldShomareFactor,fldDateSarResid

	--declare @strEXE nvarchar(300),@Order nvarchar(100)

	--if @FlagSort='date'
	--	set @order=' order by fldDateSarResid,fldShomareFactor'
	--set @strEXE='Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheMande,SumDaryafti,fldmandeTasfienashode, 		
	--	fldDateSarResid ,NameVasete,Tell,Adress,fldSharh from #tmpDaryaftTasfie4' + @order			


END

GO
/****** Object:  StoredProcedure [dbo].[ListeFactorhayeTasfieNashodeReport_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeFactorhayeTasfieNashodeReport_WithUserCode] 
--چندزبانه
	@ListGroupAshkhas varchar(5000)=null ,
	@ListCodeAshkhas varchar(5000)=null,
	@ListCodeVisitor varchar(5000)=null,
	@CodeTafzili nvarchar(10),		
	@DataType varchar(40),
	@DateSarResidAz bigint,
	@DateSarResidTa bigint,
	@DateFactorAz bigint,
	@DateFactorTa bigint,
	@FlagSort Varchar(15),
	@SumFactorha decimal(18,4) output,
	@SumMandeFactor decimal(18,4) output,
	@SumMandeTasfieNashode decimal(18,4) output,
	@SumKoleDaryafti decimal(18,4) output,
	@userCode bigint
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


CREATE TABLE #tmpDaryaftTasfie4(	
	[fldShomareFactor] [bigint]  null,
	[SumDaryafti] decimal(18,4) null,
	[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldMablagheFactor]decimal(18,4),
	[fldMablagheMande]decimal(18,4),
	[fldMablagheBargashti]decimal(18,4),
	[fldmandeTasfienashode] decimal(18,4),
	[fldSharh][nvarchar](100) COLLATE Persian_100_CI_AS,	
	[fldDateSarResid][bigint],
	[fldDateFactor][bigint],
	[ShomareDaryaft]decimal(18,4),
	[SharheTafzili][nvarchar](200) COLLATE Persian_100_CI_AS,
	[codeTafzili][nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldCodeGroupAshkhas][bigint],
	[NameGroup][nvarchar](80) COLLATE Persian_100_CI_AS,
	[fldCodeVasete][bigint],
	[NameVasete][nvarchar](200) COLLATE Persian_100_CI_AS,
	[Tell][nvarchar](50) COLLATE Persian_100_CI_AS,
	[Adress][nvarchar](200) COLLATE Persian_100_CI_AS

	)

	--insert into #tmpDaryaftTasfie4
	--	(fldShomareFactor,SumDaryafti)
	--Select fldShomareFactor ,Sum(fldMablagh) as SumDaryafti from tblRizeTasfie   Group by fldShomareFactor
		
	
	
	insert into #tmpDaryaftTasfie4
			(fldShomareFactor,SumDaryafti,fldMablagheBargashti,fldCodeTafzili,fldMablagheFactor,fldMablagheMande,fldSharh,fldDateSarResid,fldDateFactor)
			Select distinct dbo.tblTasfie.fldShomareFactor,0,0,fldCodeTafzili,fldMablagheFactor, fldMablagheMandeFactor,fldSharh,fldDateSarResid, dbo.tblRizeForoosh.fldDate		
				from  dbo.tblTasfie INNER JOIN
				dbo.tblRizeForoosh  on dbo.tblTasfie.fldShomareFactor=dbo.tblRizeForoosh.fldShomareFactor 

		if @Codetafzili<>0 --يعني همه افراد
			delete #tmpDaryaftTasfie4 where fldCodeTafzili<>@CodeTafzili

	update #tmpDaryaftTasfie4
		set SumDaryafti=tbl1.sumDaryafti from		
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfie where fldShomareDaryaft<>0 group by fldShomareFactor ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor) as tbl1
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl1.fldShomareFactor
	
	--بروزرساني جمع مبلغ برگشتي bm
	update #tmpDaryaftTasfie4
	set fldMablagheBargashti=tbl11.fldMablagheBargashti from
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as fldMablagheBargashti from tblRizeTasfie where fldShomareBargashtAzForoosh<>0 group by fldShomareFactor ) as tbl11
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl11.fldShomareFactor

		
	
	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
	--	(Select fldShomareFactor, fldMablagh from tblRizeTasfie Where fldShomareDaryaft=@ShomareDaryaft) as tbl2
	--	where tbl2.fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor

	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null
	update   #tmpDaryaftTasfie4 set fldmandeTasfienashode=fldMablagheMande-SumDaryafti - fldMablagheBargashti

	--بروزرساني کد تفصيلي
	update #tmpDaryaftTasfie4 set codeTafzili=tbl3.fldCodeTafsili from
		(SELECT        tblForoosh.fldShomareFactorForoosh,tblRizeForoosh.fldCodeTafsili
			FROM            tblForoosh INNER JOIN
                         tblRizeForoosh ON tblForoosh.fldShomareFactorForoosh = tblRizeForoosh.fldShomareFactor) as tbl3
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl3.fldShomareFactorForoosh

	--بروزرساني نام تفصيلي
	update #tmpDaryaftTasfie4 set SharheTafzili=tbl5.fldSharheTafzili from
		(Select fldSharheTafzili,fldCodeTafzili from tblTafzili) as tbl5
		where #tmpDaryaftTasfie4.codeTafzili=tbl5.fldCodeTafzili

	--بروزرساني کدگروه 
	update #tmpDaryaftTasfie4 set fldCodeGroupAshkhas=tbl6.fldCodeGroup from
		(Select fldCodeGroup,fldCodeTafzili from tblTafzili) as tbl6
		Where #tmpDaryaftTasfie4.fldCodeTafzili=tbl6.fldCodeTafzili
	
	--بروزرساني نام گروه
	update #tmpDaryaftTasfie4  set NameGroup=tbl6.fldGroupName from
		(Select fldGroupId,fldGroupName from tblGroupAshkhas) as tbl6
		where tbl6.fldGroupId=#tmpDaryaftTasfie4.fldCodeGroupAshkhas
	

	--بروزرساني کد ويزيتورها	
	update #tmpDaryaftTasfie4  set fldCodeVasete = tbl7.fldCodeVasete from
		(SELECT        tblForoosh.fldShomareFactorForoosh, tblRizeForoosh.fldCodeVasete
			FROM            tblForoosh INNER JOIN
							 tblRizeForoosh ON tblForoosh.fldShomareFactorForoosh = tblRizeForoosh.fldShomareFactor) as tbl7
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl7.fldShomareFactorForoosh

	--بروزرساني نام ويزيتورها
	update #tmpDaryaftTasfie4  set NameVasete=tbl8.fldSharheTafzili from
		(Select fldSharheTafzili,fldCodeTafzili from tblTafzili) as tbl8
		where #tmpDaryaftTasfie4.fldCodeVasete=tbl8.fldCodeTafzili  and fldCodeVasete<>0
	
	update #tmpDaryaftTasfie4 set NameVasete='' where fldCodeVasete=0

	--بروزرساني تاريخ سررسيد bm
	update #tmpDaryaftTasfie4 set fldDateSarResid=tbl9.fldDateSarResid from
	(select fldDateSarResid,fldShomareFactor from tblTasfie) as tbl9
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl9.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateSarResid< @DateSarResidAz
		delete #tmpDaryaftTasfie4 Where fldDateSarResid> @DateSarResidTa
	--بروزرساني تاريخ فاکتور bm
	update #tmpDaryaftTasfie4 set fldDateFactor=tbl10.fldDate from
	(select fldDate,fldShomareFactor from tblRizeForoosh) as tbl10
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl10.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateFactor< @DateFactorAz
		delete #tmpDaryaftTasfie4 Where fldDateFactor> @DateFactorTa

	 if @ListCodeAshkhas <>''
		begin
			declare @StrDeleteAshkhas varchar(5000)
			set  @StrDeleteAshkhas='delete from #tmpDaryaftTasfie4 
				where fldCodeTafzili NOT IN ('+ @ListCodeAshkhas +')'
				execute(@StrDeleteAshkhas)
		end


		if @ListGroupAshkhas<>''
			begin
			declare @StrDeleteGroupAshkhas varchar(5000)
			set  @StrDeleteGroupAshkhas='delete from #tmpDaryaftTasfie4 
				where '+ @ListGroupAshkhas + ''
				execute(@StrDeleteGroupAshkhas)
			end


		if @ListCodeVisitor<>''
			begin
				if @ListCodeVisitor='-1' --ليست فاکتورهاي بدون ويزيتور
					begin
						delete from #tmpDaryaftTasfie4 where fldCodeVasete<>0
					end
				else
					begin
					declare @StrDeleteVisitor varchar(5000)
					set  @StrDeleteVisitor='delete from #tmpDaryaftTasfie4 
						where '+ @ListCodeVisitor + ''
						execute(@StrDeleteVisitor)
					end
			end


	if @DataType='tasfienashode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande=(SumDaryafti+fldMablagheBargashti)
		end
	if @DataType='tasfieshode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande<>(SumDaryafti+fldMablagheBargashti)
		end
	--بروزرساني تلفن
	declare @EmpCodeTafzili nvarchar(10)
	declare EmpCursor Cursor For
		Select fldCodetafzili from #tmpDaryaftTasfie4  With (NoLock) 
			open EmpCursor
			fetch Next From EmpCursor into @EmpCodeTafzili
			While @@Fetch_Status = 0
				Begin				
					update #tmpDaryaftTasfie4  set  Tell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
							where fldCodetafzili=@EmpCodeTafzili
					Fetch Next from EmpCursor
				into @EmpCodeTafzili
			end
	Close EmpCurSor
	Deallocate EmpCursor


	update #tmpDaryaftTasfie4  set Adress =tbl9.fldAddress from
		(Select fldAddress,fldCodeTafzili from tblTafzili) as tbl9			
		where #tmpDaryaftTasfie4.fldCodeTafzili=tbl9.fldCodeTafzili

	set @sumMandeTasfieNashode=(Select Sum(fldmandeTasfienashode) as SumMande from #tmpDaryaftTasfie4)
	if @sumMandeTasfieNashode is null
		set @sumMandeTasfieNashode=0

	set @SumFactorha=(Select Sum(fldMablagheFactor) as SumMande from #tmpDaryaftTasfie4)
	if @SumFactorha is null
		set @SumFactorha=0

	set @SumMandeFactor=(Select Sum(fldMablagheMande) as SumMande from #tmpDaryaftTasfie4)
	if @SumMandeFactor is null
		set @SumMandeFactor=0


	set @SumKoleDaryafti=(Select Sum(SumDaryafti) as SumMande from #tmpDaryaftTasfie4)
	if @SumKoleDaryafti is null
		set @SumKoleDaryafti=0

-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################
-- ##########################################################

-- ##################### Group Ashkhas Limitation
-- ##########################################################
delete from #tmpDaryaftTasfie4 where
fldCodeTafzili not in (select code from @accessCodes)
-- ##########################################################


	if @FlagSort='date'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2)  as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameVasete,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0
				order by fldDateSarResid,fldShomareFactor
		
	if @FlagSort='factor'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameVasete,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by fldShomareFactor,fldDateSarResid
	
	if @FlagSort='bk'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameVasete,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0		
				order by fldmandeTasfienashode desc--,fldShomareFactor,fldDateSarResid

	if @FlagSort='kb'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
				NameVasete,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by Convert(bigint,fldmandeTasfienashode) asc--,fldShomareFactor,fldDateSarResid

	else if @FlagSort='nameshakhs'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameVasete,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by SharheTafzili asc,fldShomareFactor,fldDateSarResid

	else if @FlagSort='namegorooh'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) +'f' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameVasete,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameGroup asc,fldShomareFactor,fldDateSarResid

	else --@FlagSort='namevizitor'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) + 'g' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameVasete,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameVasete asc,fldShomareFactor,fldDateSarResid

	--declare @strEXE nvarchar(300),@Order nvarchar(100)

	--if @FlagSort='date'
	--	set @order=' order by fldDateSarResid,fldShomareFactor'
	--set @strEXE='Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheMande,SumDaryafti,fldmandeTasfienashode, 		
	--	fldDateSarResid ,NameVasete,Tell,Adress,fldSharh from #tmpDaryaftTasfie4' + @order			


END

GO
/****** Object:  StoredProcedure [dbo].[ListeKalaDynamic]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeKalaDynamic] 
--چندزبانه
	@language nvarchar(10),
	@CodeGroupKala varchar(5000),
	@sort nvarchar(20)
AS
BEGIN


create Table #temp1
(
	fldCodeKala bigint,
	fldNamekala nvarchar(200) COLLATE Persian_100_CI_AS,
	fldCodeGroup bigint,
	fldNameGroup nvarchar(200) COLLATE Persian_100_CI_AS,
	fldFee1 decimal(18,4),
	fldFee2 decimal(18,4),
	fldFee3 decimal(18,4),
	fldFee4 decimal(18,4),
	fldFee5 decimal(18,4),
	fldFee6 decimal(18,4),
	fldCodeSharh1 bigint,
	fldCodeSharh2 bigint,
	fldCodeSharh3 bigint,
	fldCodeSharh4 bigint,
	fldCodeSharh5 bigint,
	fldCodeSharh6 bigint,
	fldSharhFee1 nvarchar(200) COLLATE Persian_100_CI_AS,	
	fldSharhFee2 nvarchar(200) COLLATE Persian_100_CI_AS,
	fldSharhFee3 nvarchar(200) COLLATE Persian_100_CI_AS,
	fldSharhFee4 nvarchar(200) COLLATE Persian_100_CI_AS,
	fldSharhFee5 nvarchar(200) COLLATE Persian_100_CI_AS,
	fldSharhFee6 nvarchar(200) COLLATE Persian_100_CI_AS,
	fldGheimatMasrafkonande decimal(18,4),
	fldCodeVahed1       varchar(100) COLLATE Persian_100_CI_AS,
	fldCodeVahed2       varchar(100) COLLATE Persian_100_CI_AS, 
	fldNameVahed1       nvarchar(100) COLLATE Persian_100_CI_AS, 
	FldNameVahed2       nvarchar(100) COLLATE Persian_100_CI_AS
)

create Table #tempOrder
(
Row1 bigint,
fldCodeSharh bigint,
fldFee decimal(18,4),
fldCodeKala bigint,
)

insert into #temporder
	(Row1,fldCodeSharh,fldFee,fldCodeKala)			
(SELECT 
   ROW_NUMBER() OVER (partition by fldCodeKala order by fldCodeKala)  RowNo,fldCodeSharh ,fldfee,fldCodeKala    
FROM 
   tblMultiFeeDynamic)



insert into #temp1
	(fldCodeKala,fldFee1,fldfee2,fldfee3,fldfee4,fldfee5,fldfee6,fldCodeSharh1,fldCodeSharh2,fldCodeSharh3,fldCodeSharh4,fldCodeSharh5,fldCodeSharh6)
   SELECT DISTINCT fldCodeKala
,(SELECT fldFee FROM #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=1) AS Fee1
,(SELECT fldFee FROM #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=2) AS Fee2
,(SELECT fldFee FROM #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=3) AS Fee3
,(SELECT fldFee FROM #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=4) AS Fee4
,(SELECT fldFee FROM #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=5) AS Fee5
,(SELECT fldFee FROM #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=6) AS Fee6
,(Select fldCodeSharh from #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=1) AS fldCodeSharh1
,(Select fldCodeSharh from #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=2) AS fldCodeSharh2
,(Select fldCodeSharh from #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=3) AS fldCodeSharh3
,(Select fldCodeSharh from #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=4) AS fldCodeSharh4
,(Select fldCodeSharh from #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=5) AS fldCodeSharh5
,(Select fldCodeSharh from #tempOrder s2 WHERE s2.fldCodeKala=s1.fldCodeKala AND s2.Row1=6) AS fldCodeSharh6
FROM #tempOrder s1

	--create table #TempListKala1(
	--fldNameKala         nvarchar(300) COLLATE Persian_100_CI_AS, 
	--fldCodeKala         bigint,
	--fldCodeVahed1       varchar(100) COLLATE Persian_100_CI_AS,
	--fldCodeVahed2       varchar(100) COLLATE Persian_100_CI_AS, 
	--fldNameVahed        nvarchar(100) COLLATE Persian_100_CI_AS, 
	--FldNameVahed2       nvarchar(100) COLLATE Persian_100_CI_AS,
	--fldCodeGroup        bigint,
	--fldGroupName        nvarchar(300) COLLATE Persian_100_CI_AS,
	--fldFeeForoosh1    decimal(18,4) , 
	--fldFeeForoosh2    decimal(18,4) , 
	--fldFeeForoosh3    decimal(18,4) , 
	--fldFeeForoosh4    decimal(18,4) , 
	--fldFeeForoosh5    decimal(18,4) , 
	--fldFeeForoosh6    decimal(18,4) , 
	--fldFeeForoosh7    decimal(18,4) , 
	--fldFeeForoosh8    decimal(18,4) )


	
	--insert into #TempListKala1(fldNameKala,fldCodeKala,fldCodeVahed1,fldCodeVahed2,fldNameVahed,FldNameVahed2,
	--						  fldCodeGroup,fldGroupName)
	--select   dbo.tblKala.fldNameKala, dbo.tblKala.fldCodeKala,  dbo.tblKala.fldCodeVahed1, dbo.tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed, 
	--						 dbo.tblVahed.fldNameVahed AS FldNameVahed2, dbo.tblKala.fldCodeGroup, dbo.tblGroupKala.fldGroupName
	--FROM     dbo.tblKala LEFT OUTER JOIN
	--						 dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId LEFT OUTER JOIN
	--						 dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed1 = tblVahed_1.ID LEFT OUTER JOIN
	--						 dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID


	


	update #temp1 set fldGheimatMasrafkonande =tblKala.fldGhymatMasrafkonande,fldNamekala=tblkala.fldNameKala,fldCodeGroup=tblKala.fldCodeGroup,fldCodeVahed1=tblKala.fldCodeVahed1,fldCodeVahed2=tblkala.fldCodeVahed2 from tblkala Where tblkala.fldCodekala=#temp1.fldCodeKala


	if @CodeGroupKala<>''
	begin
		declare @StrDeleteGroupKala varchar(5000)
		set  @StrDeleteGroupKala='delete from #temp1
			where '+ @CodeGroupKala + ''
			execute(@StrDeleteGroupKala)
	end


	update #temp1 set fldNameGroup=tblGroupKala.fldGroupName from tblGroupKala Where #temp1.fldCodeGroup=tblGroupKala.fldGroupId
	update #temp1 set fldNamevahed1=tblVahed.fldNameVahed from tblVahed Where fldCodeVahed1=tblVahed.ID
	update #temp1 set fldNamevahed2=tblVahed.fldNameVahed from tblVahed Where fldCodeVahed2=tblVahed.id

	update #temp1 set fldSharhFee1=tblSharheFeeDynamic.fldSharh from tblSharheFeeDynamic Where fldCodeSharh1=tblSharheFeeDynamic.fldCodeSharh
	update #temp1 set fldSharhFee2=tblSharheFeeDynamic.fldSharh from tblSharheFeeDynamic Where fldCodeSharh2=tblSharheFeeDynamic.fldCodeSharh
	update #temp1 set fldSharhFee3=tblSharheFeeDynamic.fldSharh from tblSharheFeeDynamic Where fldCodeSharh3=tblSharheFeeDynamic.fldCodeSharh
	update #temp1 set fldSharhFee4=tblSharheFeeDynamic.fldSharh from tblSharheFeeDynamic Where fldCodeSharh4=tblSharheFeeDynamic.fldCodeSharh
	update #temp1 set fldSharhFee5=tblSharheFeeDynamic.fldSharh from tblSharheFeeDynamic Where fldCodeSharh5=tblSharheFeeDynamic.fldCodeSharh
	update #temp1 set fldSharhFee6=tblSharheFeeDynamic.fldSharh from tblSharheFeeDynamic Where fldCodeSharh6=tblSharheFeeDynamic.fldCodeSharh

	update #temp1 set #temp1.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#temp1.fldCodeKala and tblkalatranslate.fldLanguage=@Language

	if @sort='codekala'
		select 
		fldNameKala,       
		fldCodeKala,       
		fldCodeVahed1,      
		fldCodeVahed2,      
		fldNameVahed1,       
		FldNameVahed2,      
		fldCodeGroup,      
		fldNameGroup,       
		fldFee1,      
		fldFee2,      
		fldFee3,      
		fldFee4,      
		fldFee5,      
		fldFee6,      
		fldGheimatMasrafkonande,
		fldSharhFee1,
		fldSharhFee2,
		fldSharhFee3,
		fldSharhFee4,
		fldSharhFee5,
		fldSharhFee6
		from #temp1
		ORDER BY fldCodeKala Asc
	else if @sort='namekala'
			select 
		fldNameKala,       
		fldCodeKala,       
		fldCodeVahed1,      
		fldCodeVahed2,      
		fldNameVahed1,       
		FldNameVahed2,      
		fldCodeGroup,      
		fldNameGroup,       
		fldFee1,      
		fldFee2,      
		fldFee3,      
		fldFee4,      
		fldFee5,      
		fldFee6,      
		fldGheimatMasrafkonande,
		fldSharhFee1,
		fldSharhFee2,
		fldSharhFee3,
		fldSharhFee4,
		fldSharhFee5,
		fldSharhFee6
		from #temp1
		ORDER BY fldNameKala Asc


END

GO
/****** Object:  StoredProcedure [dbo].[ListeTafziliWithNoe]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListeTafziliWithNoe] 
	@CodeNoe nvarchar(10)
AS
BEGIN
	Select fldCodeTafzili,fldSharheTafzili,fldCodeNoe from tblTafzili
		where fldCodeNoe=@CodeNoe
		order by Convert(dec,fldCodeTafzili)
END

GO
/****** Object:  StoredProcedure [dbo].[ListeTafziliWithNoe_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[ListeTafziliWithNoe_WithUserCode] 
	@CodeNoe nvarchar(10),
	@UserCode bigint
AS
BEGIN

declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 

	Select fldCodeTafzili,fldSharheTafzili,fldCodeNoe from tblTafzili
		where 
		fldCodeTafzili in (select code from @accessCodes) and 
		fldCodeNoe=@CodeNoe
		order by Convert(dec,fldCodeTafzili)
END

GO
/****** Object:  StoredProcedure [dbo].[ListKalaInFactorForooshByFldCodeGroupMulti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListKalaInFactorForooshByFldCodeGroupMulti]
	-- Add the parameters for the stored procedure here
	@language nvarchar(10)
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	
	SET NOCOUNT ON;

	create table #tblTemp(
		fldCodeKala bigint,
		fldNameKala nvarchar(100),
		fldNameVahed nvarchar(100),
		fldNameVahed2 nvarchar(100),
		fldFeeForoosh decimal(18,4),
		fldCodeGroup bigint
	)


    insert into #tblTemp
		(fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldFeeForoosh,fldCodeGroup)
	SELECT        dbo.tblKala.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2, dbo.tblKala.fldFeeForoosh, dbo.tblKala.fldCodeGroup
FROM            dbo.tblKala INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed1 = dbo.tblVahed.ID INNER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID


update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language

select fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldFeeForoosh,fldCodeGroup from #tblTemp
END

GO
/****** Object:  StoredProcedure [dbo].[ListKalaInFactorForooshMulti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListKalaInFactorForooshMulti]
	@language nvarchar(10)
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    create table #tblTemp
	(
	fldCodeKala bigint,
	fldNameKala nvarchar(100),
	fldNameVahed nvarchar(100),
	fldNameVahed2 nvarchar(100),
	fldFeeForoosh decimal(18,4)
	)

	insert into #tblTemp
		(fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldFeeForoosh)
		SELECT        dbo.tblKala.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2, dbo.tblKala.fldFeeForoosh
			FROM            dbo.tblKala INNER JOIN
								 dbo.tblVahed ON dbo.tblKala.fldCodeVahed1 = dbo.tblVahed.ID INNER JOIN
								 dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID

	 update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language

	 select fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldFeeForoosh from #tbltemp
END

GO
/****** Object:  StoredProcedure [dbo].[ListSmallKalaMulti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ListSmallKalaMulti]
	@language nvarchar(10)
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    create table #tblTemp
	(
	fldCodeKala bigint,
	fldNameKala nvarchar(100)
	)

	insert into #tblTemp
		(fldCodeKala,fldNameKala)
		SELECT         fldCodeKala,  fldNameKala
			FROM            dbo.tblKala

	 update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language

	 select fldCodeKala,fldNameKala from #tbltemp
END

GO
/****** Object:  StoredProcedure [dbo].[LoadPrinterOfKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[LoadPrinterOfKala] 
	-- Add the parameters for the stored procedure here
	@CodeKala bigint,
	@IdPrinter Bigint output
AS
BEGIN
	set @IdPrinter =(Select fldIDChapgarKala from tblIDChapgarKala Where fldCodeKala=@CodeKala)
	if @IdPrinter is null
		set @IdPrinter = -1
		
END

GO
/****** Object:  StoredProcedure [dbo].[LoadShomareFishBeTafkikeChapger]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[LoadShomareFishBeTafkikeChapger]
	@fldShomareFactor bigint,
	@IDChapgarKala bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	create table #tmpFactor2(
		fldRadif bigint,
		fldCodeKala bigint,
		fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
		fldSharhKala nvarchar(100) COLLATE Persian_100_CI_AS,
		fldTedadCarton nvarchar(10) COLLATE Persian_100_CI_AS,
		fldTedadJoz bigint,
		fldFeeJoz bigint,		
		fldFeeYekCarton bigint,
		fldFeeKol bigint ,
        fldDarsadTakhfif bigint,
		fldMablaghTakhfif bigint,
		fldFeeVahed1BadAzTakhfif bigint, 
        fldFeeVahed2BadAzTakhfif bigint,
		fldArzeshAfzode bigint,
		fldFeeKolBadAzTakhfif bigint, 
        fldMablaghKhales bigint,
		fldCodeAnbar bigint,
		fldNameVahed nvarchar(100) COLLATE Persian_100_CI_AS,	
		fldNameVahed2	nvarchar(100) COLLATE Persian_100_CI_AS,	
		fldNameAnbar nvarchar(50) COLLATE Persian_100_CI_AS,
		fldVahed1Code bigint,
		fldVahed2Code bigint,
		fldGhymatMasrafkonande bigint,
		fldCodePrinter bigint

	)

		insert into #tmpFactor2(fldRadif,fldCodeKala ,fldNameKala ,fldSharhKala,fldTedadCarton,fldTedadJoz,
				fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif, 
				fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,
				fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande)
		
		SELECT        dbo.tblRizeForoosh.fldRadif, dbo.tblRizeForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblRizeForoosh.fldSharhKala, dbo.tblRizeForoosh.fldTedadCarton, 
                         dbo.tblRizeForoosh.fldTedadJoz, dbo.tblRizeForoosh.fldFeeJoz, dbo.tblRizeForoosh.fldFeeYekCarton, dbo.tblRizeForoosh.fldFeeKol, 
                         dbo.tblRizeForoosh.fldDarsadTakhfif, dbo.tblRizeForoosh.fldMablaghTakhfif, dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, 
                         dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif, 
                         dbo.tblRizeForoosh.fldMablaghKhales, dbo.tblRizeForoosh.fldCodeAnbar, tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2, 
                         dbo.tblAnbar.fldNameAnbar, tblVahed_2.ID AS fldVahed1Code, dbo.tblVahed.ID AS fldVahed2Code, isnull(dbo.tblKala.fldGhymatMasrafkonande,0) as fldGhymatMasrafkonande
		FROM            dbo.tblAnbar INNER JOIN
                         dbo.tblRizeForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeForoosh.fldCodeAnbar INNER JOIN
                         dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
WHERE        (dbo.tblRizeForoosh.fldShomareFactor = @fldShomareFactor)

	update #tmpFactor2 set fldCodePrinter = tblIDChapgarKala.fldIDChapgarKala  from tblIDChapgarKala Where #tmpFactor2.fldCodeKala=tblIDChapgarKala.fldCodeKala
	update #tmpFactor2 set fldCodePrinter =0 where fldCodePrinter is null
	select fldNameKala,fldTedadJoz,fldCodePrinter from #tmpFactor2 Where fldCodePrinter=@IDChapgarKala order by Convert(dec,fldRadif)
END

GO
/****** Object:  StoredProcedure [dbo].[LoadSuperVisor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[LoadSuperVisor]
	-- Add the parameters for the stored procedure here
	
	
AS
BEGIN
	update tblTafzili set fldNoeMohasebePoorsant=0  where fldNoeMohasebePoorsant is null
	select fldCodeTafzili , fldSharheTafzili,fldPoorsant,ID,fldNoeMohasebePoorsant,(case when fldNoeMohasebePoorsant=1 then 'فروش' else 'واریز' end)  as fldSharheMohasebe from tblTafzili Where fldCodeNoe='0' And fldNoeVizitor=1
END

GO
/****** Object:  StoredProcedure [dbo].[LoadVaseteArshiveAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[LoadVaseteArshiveAmani] 
	-- Add the parameters for the stored procedure here
	@ShomareSanad nvarchar(50),
	@CodeTafziliVizitor nvarchar(50) output,
	--@CodeTafziliSuperVizor nvarchar(50) output,
	@bigMablaghPoorsantVizitor bigint output,
	@bigMablaghPoorsantSuperVizor bigint output

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @CodeMoinHoghoogh nvarchar(10)

	execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output									


	set @CodeTafziliVizitor =(SELECT        fldCodeVasete
									FROM            dbo.tblRizeForoosh
										WHERE        (fldShomareSanad = @ShomareSanad) And fldRadif=1 )
	if @CodeTafziliVizitor is null
		set @CodeTafziliVizitor=-1

	--set @bigMablaghPoorsantVizitor =(SELECT        fldBedehkar
	--								FROM            dbo.tblSodooreSanad
	--									WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
	--										(SELECT        fldCodeTafzili
	--											FROM            dbo.tblTafzili
	--											WHERE        (fldNoeVizitor = 2))))

	--if @bigMablaghPoorsantVizitor is null
	set @bigMablaghPoorsantVizitor=0


	
	--set @CodeTafziliSuperVizor =(SELECT        fldCodeTafzili
	--								FROM            dbo.tblSodooreSanad
	--									WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
	--										(SELECT        fldCodeTafzili
	--											FROM            dbo.tblTafzili
	--											WHERE        (fldNoeVizitor = 1))))
	--if @CodeTafziliSuperVizor is null
	--	set @CodeTafziliSuperVizor=0


	--set @bigMablaghPoorsantSuperVizor =(SELECT        fldBedehkar
	--								FROM            dbo.tblSodooreSanad
	--									WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
	--										(SELECT        fldCodeTafzili
	--											FROM            dbo.tblTafzili
	--											WHERE        (fldNoeVizitor = 1))))
	--if @bigMablaghPoorsantSuperVizor is null
	set @bigMablaghPoorsantSuperVizor=0
END

GO
/****** Object:  StoredProcedure [dbo].[LoadVaseteArshiveBargashtAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[LoadVaseteArshiveBargashtAmani] 
	-- Add the parameters for the stored procedure here
	@ShomareSanad nvarchar(50),
	@CodeTafziliVizitor nvarchar(50) output,
	--@CodeTafziliSuperVizor nvarchar(50) output,
	@bigMablaghPoorsantVizitor bigint output,
	@bigMablaghPoorsantSuperVizor bigint output

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @CodeMoinHoghoogh nvarchar(10)

	execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output									


	set @CodeTafziliVizitor =(SELECT        fldCodeVasete
									FROM            dbo.tblRizeBargashtAzForoosh
										WHERE        (fldShomareSanad = @ShomareSanad) And fldRadif=1 )
	if @CodeTafziliVizitor is null
		set @CodeTafziliVizitor=-1

	--set @bigMablaghPoorsantVizitor =(SELECT        fldBedehkar
	--								FROM            dbo.tblSodooreSanad
	--									WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
	--										(SELECT        fldCodeTafzili
	--											FROM            dbo.tblTafzili
	--											WHERE        (fldNoeVizitor = 2))))

	--if @bigMablaghPoorsantVizitor is null
	set @bigMablaghPoorsantVizitor=0


	
	--set @CodeTafziliSuperVizor =(SELECT        fldCodeTafzili
	--								FROM            dbo.tblSodooreSanad
	--									WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
	--										(SELECT        fldCodeTafzili
	--											FROM            dbo.tblTafzili
	--											WHERE        (fldNoeVizitor = 1))))
	--if @CodeTafziliSuperVizor is null
	--	set @CodeTafziliSuperVizor=0


	--set @bigMablaghPoorsantSuperVizor =(SELECT        fldBedehkar
	--								FROM            dbo.tblSodooreSanad
	--									WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
	--										(SELECT        fldCodeTafzili
	--											FROM            dbo.tblTafzili
	--											WHERE        (fldNoeVizitor = 1))))
	--if @bigMablaghPoorsantSuperVizor is null
	set @bigMablaghPoorsantSuperVizor=0
END

GO
/****** Object:  StoredProcedure [dbo].[LoadVaseteArshiveBargashtAzForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[LoadVaseteArshiveBargashtAzForoosh] 
--چندزبانه
	-- Add the parameters for the stored procedure here
	@ShomareSanad nvarchar(50),
	@CodeTafziliVizitor nvarchar(50) output,
	--@CodeTafziliSuperVizor nvarchar(50) output,
	@bigMablaghPoorsantVizitor decimal(18,4) output,
	@bigMablaghPoorsantSuperVizor decimal(18,4) output

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @CodeMoinHoghoogh nvarchar(10)

	execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output									


	set @CodeTafziliVizitor =(SELECT        fldCodeVasete
									FROM            dbo.tblRizeBargashtAzForoosh
										WHERE        (fldShomareSanad = @ShomareSanad) And fldRadif=1 )
	if @CodeTafziliVizitor is null
		set @CodeTafziliVizitor=-1

	set @bigMablaghPoorsantVizitor =(SELECT        fldBestankar
									FROM            dbo.tblSodooreSanad
										WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
											(SELECT        fldCodeTafzili
												FROM            dbo.tblTafzili
												WHERE        (fldNoeVizitor = 2))))

	if @bigMablaghPoorsantVizitor is null
		set @bigMablaghPoorsantVizitor=0


	
	--set @CodeTafziliSuperVizor =(SELECT        fldCodeTafzili
	--								FROM            dbo.tblSodooreSanad
	--									WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
	--										(SELECT        fldCodeTafzili
	--											FROM            dbo.tblTafzili
	--											WHERE        (fldNoeVizitor = 1))))
	--if @CodeTafziliSuperVizor is null
	--	set @CodeTafziliSuperVizor=0


	set @bigMablaghPoorsantSuperVizor =(SELECT        fldBestankar
									FROM            dbo.tblSodooreSanad
										WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
											(SELECT        fldCodeTafzili
												FROM            dbo.tblTafzili
												WHERE        (fldNoeVizitor = 1))))
	if @bigMablaghPoorsantSuperVizor is null
		set @bigMablaghPoorsantSuperVizor=0
END

GO
/****** Object:  StoredProcedure [dbo].[LoadVaseteArshiveForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[LoadVaseteArshiveForoosh] 
--چندزبانه
	-- Add the parameters for the stored procedure here
	@ShomareSanad nvarchar(50),
	@CodeTafziliVizitor nvarchar(50) output,
	--@CodeTafziliSuperVizor nvarchar(50) output,
	@bigMablaghPoorsantVizitor [decimal](18, 4) output,
	@bigMablaghPoorsantSuperVizor [decimal](18, 4) output

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @CodeMoinHoghoogh nvarchar(10)

	execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output									


	set @CodeTafziliVizitor =(SELECT        fldCodeVasete
									FROM            dbo.tblRizeForoosh
										WHERE        (fldShomareSanad = @ShomareSanad) And fldRadif=1 )
	if @CodeTafziliVizitor is null
		set @CodeTafziliVizitor=-1

	set @bigMablaghPoorsantVizitor =(SELECT        fldBedehkar
									FROM            dbo.tblSodooreSanad
										WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
											(SELECT        fldCodeTafzili
												FROM            dbo.tblTafzili
												WHERE        (fldNoeVizitor = 2))))

	if @bigMablaghPoorsantVizitor is null
		set @bigMablaghPoorsantVizitor=0


	
	--set @CodeTafziliSuperVizor =(SELECT        fldCodeTafzili
	--								FROM            dbo.tblSodooreSanad
	--									WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
	--										(SELECT        fldCodeTafzili
	--											FROM            dbo.tblTafzili
	--											WHERE        (fldNoeVizitor = 1))))
	--if @CodeTafziliSuperVizor is null
	--	set @CodeTafziliSuperVizor=0


	set @bigMablaghPoorsantSuperVizor =(SELECT        fldBedehkar
									FROM            dbo.tblSodooreSanad
										WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
											(SELECT        fldCodeTafzili
												FROM            dbo.tblTafzili
												WHERE        (fldNoeVizitor = 1))))
	if @bigMablaghPoorsantSuperVizor is null
		set @bigMablaghPoorsantSuperVizor=0
END

GO
/****** Object:  StoredProcedure [dbo].[LoadVaseteArshiveSanad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[LoadVaseteArshiveSanad] 
--چندزبانه
	-- Add the parameters for the stored procedure here
	@ShomareSanad nvarchar(50),
	@CodeTafziliVizitor nvarchar(50) output,
	--@CodeTafziliSuperVizor nvarchar(50) output,
	@bigMablaghPoorsantVizitor decimal(18,4) output,
	@bigMablaghPoorsantSuperVizor decimal(18,4) output

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @CodeMoinHoghoogh nvarchar(10)

	execute codemoinhesab 'hoghoogh' ,@CodeMoinHoghoogh output									


	set @CodeTafziliVizitor =(SELECT        fldCodeTafzili
									FROM            dbo.tblSodooreSanad
										WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
											(SELECT        fldCodeTafzili
												FROM            dbo.tblTafzili
												WHERE        (fldNoeVizitor = 2))))
	if @CodeTafziliVizitor is null  -- برای حالتی که واریزی واسطه داره ولی پورسانت نداره
		begin
			set @CodeTafziliVizitor = (Select Top(1) fldVasete from tblSodooreSanad Where fldSanad=@ShomareSanad)
			if @CodeTafziliVizitor is null
				set @CodeTafziliVizitor=-1
		end

	set @bigMablaghPoorsantVizitor =(SELECT        fldBedehkar
									FROM            dbo.tblSodooreSanad
										WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
											(SELECT        fldCodeTafzili
												FROM            dbo.tblTafzili
												WHERE        (fldNoeVizitor = 2))))

	if @bigMablaghPoorsantVizitor is null
		set @bigMablaghPoorsantVizitor=0


	
	--set @CodeTafziliSuperVizor =(SELECT        fldCodeTafzili
	--								FROM            dbo.tblSodooreSanad
	--									WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
	--										(SELECT        fldCodeTafzili
	--											FROM            dbo.tblTafzili
	--											WHERE        (fldNoeVizitor = 1))))
	--if @CodeTafziliSuperVizor is null
	--	set @CodeTafziliSuperVizor=0


	set @bigMablaghPoorsantSuperVizor =(SELECT        fldBedehkar
									FROM            dbo.tblSodooreSanad
										WHERE        (fldSanad = @ShomareSanad) AND (fldMoinKamel = @CodeMoinHoghoogh) AND (fldCodeTafzili IN
											(SELECT        fldCodeTafzili
												FROM            dbo.tblTafzili
												WHERE        (fldNoeVizitor = 1))))
	if @bigMablaghPoorsantSuperVizor is null
		set @bigMablaghPoorsantSuperVizor=0
END

GO
/****** Object:  StoredProcedure [dbo].[LoadVizitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[LoadVizitor]
	-- Add the parameters for the stored procedure here
	
	
AS
BEGIN
	update tblTafzili set fldNoeMohasebePoorsant=0  where fldNoeMohasebePoorsant is null
	select fldCodeTafzili , fldSharheTafzili,fldPoorsant,ID,fldNoeMohasebePoorsant,(case when fldNoeMohasebePoorsant=1 then 'فروش' else 'واریز' end ) as fldSharheMohasebe from tblTafzili Where fldCodeNoe='0' And fldNoeVizitor=2
END

GO
/****** Object:  StoredProcedure [dbo].[MandeAnbarReport]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[MandeAnbarReport]
	@CodeGroup bigint
as

begin
	Select * from tblKala Where fldCodeGroup=@CodeGroup
end

GO
/****** Object:  StoredProcedure [dbo].[MandeKolTafzili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[MandeKolTafzili]
@CodeTafzili nvarchar(10),
@Mande bigint output
as

begin
	--declare @CodeKolMoin nvarchar(10),@MandeTalab bigint,@MandeBedehi bigint
	--execute CodeMoinHesab 'hesabhayedaryaftani',@CodeKolMoin output

	--set @MandeTalab=(SELECT       ISNULL( SUM(ISNULL(fldBedehkar, 0) - ISNULL(fldBestankar, 0)),0) AS SumM
	--	FROM            tblSodooreSanad
	--	WHERE      (fldMoinKamel=@CodeKolMoin) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldSanad)<=@Sanad)

	--execute CodeMoinHesab 'hesabhayepardakhtani',@CodeKolMoin output

	--set @MandeBedehi=(SELECT       ISNULL( SUM(ISNULL(fldBestankar, 0) - ISNULL(fldBedehkar, 0)),0) AS SumM
	--	FROM            tblSodooreSanad
	--	WHERE      (fldMoinKamel=@CodeKolMoin) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldSanad)<=@Sanad)
	--set @Mande=@MandeTalab-@MandeBedehi

	declare @CodeKol nvarchar(100),@MandeTalab bigint,@MandeBedehi bigint,@Sanad bigint

	set @Sanad =(Select Max(Convert(dec,fldSanad)) from tblSodooreSanad )
	if @Sanad is null
		set @sanad=0

	execute CodeKolHesab 'kolhesabhayedaryaftani',@CodeKol output

	--INSERT  INTO dbo.tblTest5(name1, name2, name3)
	--	VALUES        (@CodeKol,'','')
	--execute NameKolMoin @strcodemoinkamel  ,@strNameHesab output
	set @MandeTalab=(SELECT       ISNULL( SUM(ISNULL(fldBedehkar, 0) - ISNULL(fldBestankar, 0)),0) AS SumM
		FROM            tblSodooreSanad
		WHERE      (fldCodeKol=@CodeKol) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldSanad)<=@Sanad)

	execute CodeKolHesab 'kolhesabhayepardakhtani',@CodeKol output

	set @MandeBedehi=(SELECT       ISNULL( SUM(ISNULL(fldBestankar, 0) - ISNULL(fldBedehkar, 0)),0) AS SumM
		FROM            tblSodooreSanad
		WHERE      (fldCodeKol=@CodeKol) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldSanad)<=@Sanad)
	set @Mande=@MandeTalab-@MandeBedehi

end

GO
/****** Object:  StoredProcedure [dbo].[MandeTafzili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE proc [dbo].[MandeTafzili]
--چندزبانه
@CodeTafzili nvarchar(10),
@Sanad bigint,
@Mande decimal(18,4) output
as

begin
	--declare @CodeKolMoin nvarchar(10),@MandeTalab bigint,@MandeBedehi bigint
	--execute CodeMoinHesab 'hesabhayedaryaftani',@CodeKolMoin output

	--set @MandeTalab=(SELECT       ISNULL( SUM(ISNULL(fldBedehkar, 0) - ISNULL(fldBestankar, 0)),0) AS SumM
	--	FROM            tblSodooreSanad
	--	WHERE      (fldMoinKamel=@CodeKolMoin) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldSanad)<=@Sanad)

	--execute CodeMoinHesab 'hesabhayepardakhtani',@CodeKolMoin output

	--set @MandeBedehi=(SELECT       ISNULL( SUM(ISNULL(fldBestankar, 0) - ISNULL(fldBedehkar, 0)),0) AS SumM
	--	FROM            tblSodooreSanad
	--	WHERE      (fldMoinKamel=@CodeKolMoin) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldSanad)<=@Sanad)
	--set @Mande=@MandeTalab-@MandeBedehi

	declare @CodeKol nvarchar(100),@MandeTalab decimal(18,4),@MandeBedehi decimal(18,4)

	execute CodeKolHesab 'kolhesabhayedaryaftani',@CodeKol output

	--INSERT  INTO dbo.tblTest5(name1, name2, name3)
	--	VALUES        (@CodeKol,'','')
	--execute NameKolMoin @strcodemoinkamel  ,@strNameHesab output
	set @MandeTalab=(SELECT       ISNULL( SUM(ISNULL(fldBedehkar, 0) - ISNULL(fldBestankar, 0)),0) AS SumM
		FROM            tblSodooreSanad
		WHERE      (fldCodeKol=@CodeKol) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldSanad)<=@Sanad)

	execute CodeKolHesab 'kolhesabhayepardakhtani',@CodeKol output

	set @MandeBedehi=(SELECT       ISNULL( SUM(ISNULL(fldBestankar, 0) - ISNULL(fldBedehkar, 0)),0) AS SumM
		FROM            tblSodooreSanad
		WHERE      (fldCodeKol=@CodeKol) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldSanad)<=@Sanad)
	set @Mande=@MandeTalab-@MandeBedehi

end

GO
/****** Object:  StoredProcedure [dbo].[MandeTafziliBedehi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[MandeTafziliBedehi]
@CodeTafzili nvarchar(10),
@Sanad bigint,
@Mande bigint output
as

begin
	declare @CodeKolMoin nvarchar(10)
	execute CodeMoinHesab 'hesabhayepardakhtani',@CodeKolMoin output

	set @Mande=(SELECT       ISNULL( SUM(ISNULL(fldBedehkar, 0) - ISNULL(fldBestankar, 0)),0) AS SumM
		FROM            tblSodooreSanad
		WHERE      (fldMoinKamel=@CodeKolMoin) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldSanad)<=@Sanad)

	
end

GO
/****** Object:  StoredProcedure [dbo].[MaxAndMinShomareSanad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[MaxAndMinShomareSanad]
	@Max bigint output,
	@min bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @Max=(select max(convert(bigint,fldsanad)) from tblSodooreSanad ) 
set @min=(select min(convert(bigint,fldsanad)) from tblSodooreSanad ) 



if @Max is null
	set @Max=0

if @min is null
	set @min=0

Return @Max
Return @min
END




GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDate]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[MaxDateAndMinDate]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(flddate) from tblRizeForoosh) 
if @MaxDate is null
	set @MaxDate=0
set @minDate=(select min(flddate) from tblRizeForoosh) 
if @minDate is null	
	set @minDate=0

Return @MaxDate
Return @minDate
END

GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDate_RizeBargashtAzForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[MaxDateAndMinDate_RizeBargashtAzForoosh]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(flddate) from tblRizeBargashtAzForoosh) 
set @minDate=(select min(flddate) from tblRizeBargashtAzForoosh) 

if @MaxDate is null
	set @MaxDate=0

if @minDate is null
	set @minDate=0

Return @MaxDate
Return @minDate
END

GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDate_RizeBargashtAzKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[MaxDateAndMinDate_RizeBargashtAzKharid]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(flddate) from tblRizeBargashtAzKharid) 
set @minDate=(select min(flddate) from tblRizeBargashtAzKharid) 

if @MaxDate is null
	set @MaxDate=0

if @minDate is null
	set @minDate=0

Return @MaxDate
Return @minDate
END

GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDate_RizeKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[MaxDateAndMinDate_RizeKharid]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(flddate) from tblRizeKharid) 
set @minDate=(select min(flddate) from tblRizeKharid) 

if @MaxDate is null
	set @MaxDate=0

if @minDate is null
	set @minDate=0

Return @MaxDate
Return @minDate
END

GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDate_RizePishPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[MaxDateAndMinDate_RizePishPishForoosh]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(flddate) from tblRizePishPishForoosh) 
set @minDate=(select min(flddate) from tblRizePishPishForoosh) 

if @MaxDate is null
	set @MaxDate=0

if @minDate is null
	set @minDate=0

Return @MaxDate
Return @minDate
END

GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDateAmalkardKarbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[MaxDateAndMinDateAmalkardKarbar]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(Convert(dec,flddate)) from tblAmalkardekarbar where NoeAmaliat=N'جدید') 
set @minDate=(select min(Convert(dec,flddate)) from tblAmalkardekarbar where NoeAmaliat=N'جدید') 

if @MaxDate is null
	set @MaxDate=0

if @minDate is null
	set @minDate=0

Return @MaxDate
Return @minDate
END

GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDateDaramad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[MaxDateAndMinDateDaramad]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(Convert(dec,flddate)) from tblSodooreSanad where fldType='daramad') 
set @minDate=(select min(Convert(dec,flddate)) from tblSodooreSanad where fldType='daramad') 

if @MaxDate is null
	set @MaxDate=0

if @minDate is null
	set @minDate=0

Return @MaxDate
Return @minDate
END




GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDateDaryaft]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
Create PROCEDURE [dbo].[MaxDateAndMinDateDaryaft]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(flddate) from tblSodooreSanad inner join dbo.tblDaryaft on dbo.tblDaryaft.fldShomareSanad=dbo.tblSodooreSanad.fldSanad
where dbo.tblDaryaft.fldType='Daryaft') 
if @MaxDate is null
	set @MaxDate=0
set @minDate=(select min(flddate) from tblSodooreSanad inner join dbo.tblDaryaft on dbo.tblDaryaft.fldShomareSanad=dbo.tblSodooreSanad.fldSanad
where dbo.tblDaryaft.fldType='Daryaft') 
if @minDate is null	
	set @minDate=0

Return @MaxDate
Return @minDate
END

GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDateForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[MaxDateAndMinDateForoosh]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(flddate) from tblRizeForoosh)
if @MaxDate is null
	set @MaxDate=0
set @minDate=(select min(flddate) from tblRizeForoosh)
if @minDate is null	
	set @minDate=0

Return @MaxDate
Return @minDate
END



GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDateHazine]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[MaxDateAndMinDateHazine]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(Convert(dec,flddate)) from tblSodooreSanad where fldType='hazine') 
set @minDate=(select min(Convert(dec,flddate)) from tblSodooreSanad where fldType='hazine') 

if @MaxDate is null
	set @MaxDate=0

if @minDate is null
	set @minDate=0

Return @MaxDate
Return @minDate
END




GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDatePardakht]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[MaxDateAndMinDatePardakht]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(flddate) from tblSodooreSanad inner join dbo.tblPardakht on dbo.tblPardakht.fldShomareSanad=dbo.tblSodooreSanad.fldSanad
where dbo.tblPardakht.fldType='pardakht') 
if @MaxDate is null
	set @MaxDate=0
set @minDate=(select min(flddate) from tblSodooreSanad inner join dbo.tblPardakht on dbo.tblPardakht.fldShomareSanad=dbo.tblSodooreSanad.fldSanad
where dbo.tblPardakht.fldType='pardakht') 
if @minDate is null	
	set @minDate=0

Return @MaxDate
Return @minDate
END

GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDatePishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[MaxDateAndMinDatePishForoosh]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(flddate) from tblRizePishForoosh)
if @MaxDate is null
	set @MaxDate=0
set @minDate=(select min(flddate) from tblRizePishForoosh)
if @minDate is null	
	set @minDate=0

Return @MaxDate
Return @minDate
END



GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDateSanad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[MaxDateAndMinDateSanad]
--چندزبانه
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(Convert(dec,flddate)) from tblSodooreSanad where LEN(fldDate)=8) 
set @minDate=(select min(Convert(dec,flddate)) from tblSodooreSanad where LEN(fldDate)=8) 

if @MaxDate is null
	set @MaxDate=0

if @minDate is null
	set @minDate=0

Return @MaxDate
Return @minDate
END

GO
/****** Object:  StoredProcedure [dbo].[MaxDateAndMinDateSanadEnteghalVajh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[MaxDateAndMinDateSanadEnteghalVajh]
	@MaxDate bigint output,
	@minDate bigint output
AS
BEGIN
	
	SET NOCOUNT ON;

set @MaxDate=(select max(Convert(dec,flddate)) from tblSodooreSanad where  fldType='enteghalevajh') 
set @minDate=(select min(Convert(dec,flddate)) from tblSodooreSanad where  fldType='enteghalevajh') 

if @MaxDate is null
	set @MaxDate=0

if @minDate is null
	set @minDate=0

Return @MaxDate
Return @minDate
END




GO
/****** Object:  StoredProcedure [dbo].[MaxShomareAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[MaxShomareAmani]
@lngMaxShomareAmani bigint output
as

begin

            			SET NOCOUNT ON;
	declare @lngCount bigint
	set @lngCount=(Select Count(fldShomareAmani) from tblRizeForoosh)
	if @lngCount>0 
		begin
 			set @lngMaxShomareAmani =(Select Max(fldShomareAmani) from tblRizeForoosh)
		end
	else
		set @lngMaxShomareAmani =0
end

GO
/****** Object:  StoredProcedure [dbo].[MaxShomareBargashtAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[MaxShomareBargashtAmani]
@lngMaxShomareAmani bigint output
as

begin

            			SET NOCOUNT ON;
	declare @lngCount bigint
	set @lngCount=(Select Count(fldShomareBargashtAmani) from tblRizeBargashtAzForoosh)
	if @lngCount>0 
		begin
 			set @lngMaxShomareAmani =(Select Max(fldShomareBargashtAmani) from tblRizeBargashtAzForoosh)
		end
	else
		set @lngMaxShomareAmani =0
end

GO
/****** Object:  StoredProcedure [dbo].[MaxShomareDaryaft]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[MaxShomareDaryaft]
@lngMaxShomareDaryaft bigint output
as

begin
	declare @lngCount bigint
	set @lngCount=(Select Count(fldShomareDaryaft) from tblDaryaft)
	if @lngCount>0 
		begin
 			set @lngMaxShomareDaryaft =(Select Max(fldShomareDaryaft) from tblDaryaft)
		end
	else
		set @lngMaxShomareDaryaft =0
end

GO
/****** Object:  StoredProcedure [dbo].[MaxShomareEnteghal]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[MaxShomareEnteghal]
@lngMaxShomareEnteghal bigint output
as

begin

            			SET NOCOUNT ON;
	declare @lngCount bigint
	set @lngCount=(Select Count(fldShomareEnteghal) from tblRizeForoosh)
	if @lngCount>0 
		begin
 			set @lngMaxShomareEnteghal =(Select Max(fldShomareEnteghal) from tblRizeForoosh)
		end
	else
		set @lngMaxShomareEnteghal =0
end

GO
/****** Object:  StoredProcedure [dbo].[MaxShomareFish]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[MaxShomareFish]
	-- Add the parameters for the stored procedure here
	@MaxShomare bigint output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @bigMax bigint
    -- Insert statements for procedure here
	set @bigMax=(Select Max(fldShomareFish) from tblFish)
	if @bigMax is  null
		set @MaxShomare=1
	else
		set @MaxShomare=@bigMax+1
END

GO
/****** Object:  StoredProcedure [dbo].[MaxShomarePardakht]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[MaxShomarePardakht]
@lngMaxShomarePardakht bigint output
as

begin
	declare @lngCount bigint
	set @lngCount=(Select Count(fldShomarePardakht) from tblPardakht)
	if @lngCount>0 
		begin
 			set @lngMaxShomarePardakht =(Select Max(fldShomarePardakht) from tblPardakht)
		end
	else
		set @lngMaxShomarePardakht =0
end

GO
/****** Object:  StoredProcedure [dbo].[MaxShomarePas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[MaxShomarePas]
@lngMaxShomarePas bigint output
as

begin

     SET NOCOUNT ON;
	declare @lngCount bigint
	set @lngCount=(Select Count(fldShomarePas) from tblPas)
	if @lngCount > 0 
		begin
 			set @lngMaxShomarePas =(Select Max(fldShomarePas) from tblPas)
		end
	else
		set @lngMaxShomarePas =0
end

GO
/****** Object:  StoredProcedure [dbo].[MaxShomareSanad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[MaxShomareSanad]
@lngMaxShomareSanad bigint output
as

begin
	declare @lngCount bigint
	set @lngCount=(Select Count(fldSanad) from tblSodooreSanad)
	if @lngCount>0 
		begin
 			set @lngMaxShomareSanad =(Select Max(Convert(bigint,fldSanad)) from tblSodooreSanad)
		end
	else
		set @lngMaxShomareSanad =0
end

GO
/****** Object:  StoredProcedure [dbo].[MaxShomareSanad_Mandeh_Mojodi_Ashkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[MaxShomareSanad_Mandeh_Mojodi_Ashkhas] --ali 96-01-29
@lngMaxShomareSanad bigint output
as

begin
     SET NOCOUNT ON;
	declare @lngCount bigint
	set @lngCount=(Select Count(fldSanad) from tblSodooreSanad where fldSanad='1')
	if @lngCount > 0 
		begin
 			set @lngMaxShomareSanad =(Select Max(Convert(bigint,fldSanad)) from tblSodooreSanad)
			set @lngMaxShomareSanad=@lngMaxShomareSanad + 1
		end
	else
		set @lngMaxShomareSanad =1
end

GO
/****** Object:  StoredProcedure [dbo].[MaxShomareSanad_Mandeh_Mojodi_Ebteda]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[MaxShomareSanad_Mandeh_Mojodi_Ebteda] --ali 95-12-24
@lngMaxShomareSanad bigint output
as

begin
     SET NOCOUNT ON;
	declare @lngCount bigint
	set @lngCount=(Select Count(fldSanad) from tblSodooreSanad where fldSanad='1')
	if @lngCount > 0 
		begin
 			set @lngMaxShomareSanad =(Select Max(Convert(bigint,fldSanad)) from tblSodooreSanad)
			set @lngMaxShomareSanad=@lngMaxShomareSanad + 1
		end
	else
		set @lngMaxShomareSanad =1
end

GO
/****** Object:  StoredProcedure [dbo].[MaxShomareVosool]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[MaxShomareVosool]
@lngMaxShomareVosool bigint output
as

begin

     SET NOCOUNT ON;
	declare @lngCount bigint
	set @lngCount=(Select Count(fldShomareVosool) from tblVosool)
	if @lngCount > 0 
		begin
 			set @lngMaxShomareVosool =(Select Max(fldShomareVosool) from tblVosool)
		end
	else
		set @lngMaxShomareVosool =0
end

GO
/****** Object:  StoredProcedure [dbo].[MinMaxDateAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[MinMaxDateAnbar]
	@MinDate bigint output,
	@MaxDate bigint output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @tmp bigint
    -- Insert statements for procedure here
	set @MinDate = 0
	set @MaxDate = 0

	set @tmp = (Select Min(fldDate) from tblRizeKharid)
	if @tmp is not null
		set @MinDate =@tmp

	set @tmp = (Select Min(fldDate) from tblRizeForoosh)
	if @tmp is not null
		begin
			if @MinDate=0
				set @MinDate=@tmp
			else if @tmp<@MinDate
				begin
					set @MinDate =@tmp
				end

		end

	set @tmp = (Select Min(fldDate) from tblRizeBargashtAzKharid)
	if @tmp is not null
		begin
			if @MinDate=0
				set @MinDate=@tmp
			else if @tmp<@MinDate
				begin
					set @MinDate =@tmp
				end

		end

	set @tmp = (Select Min(fldDate) from tblRizeBargashtAzForoosh)
	if @tmp is not null
		begin
			if @MinDate=0
				set @MinDate=@tmp
			else if @tmp<@MinDate
				begin
					set @MinDate =@tmp
				end

		end
		

		--######MAX

	set @tmp = (Select Max(fldDate) from tblRizeKharid)
	if @tmp is not null
		set @MaxDate =@tmp

	set @tmp = (Select Max(fldDate) from tblRizeForoosh)
	if @tmp is not null
		begin
			if @MaxDate=0
				set @MaxDate=@tmp
			else if @tmp>@MaxDate
				begin
					set @MaxDate =@tmp
				end

		end

	set @tmp = (Select Max(fldDate) from tblRizeBargashtAzKharid)
	if @tmp is not null
		begin
			if @MaxDate=0
				set @MaxDate=@tmp
			else if @tmp>@MaxDate
				begin
					set @MaxDate =@tmp
				end

		end

	set @tmp = (Select Max(fldDate) from tblRizeBargashtAzForoosh)
	if @tmp is not null
		begin
			if @MaxDate=0
				set @MaxDate=@tmp
			else if @tmp>@MaxDate
				begin
					set @MaxDate =@tmp
				end

		end

	set @tmp = (Select Max(fldDate) from tblRizeTolid)
	if @tmp is not null
		begin
			if @MaxDate=0
				set @MaxDate=@tmp
			else if @tmp>@MaxDate
				begin
					set @MaxDate =@tmp
				end

		end

END

GO
/****** Object:  StoredProcedure [dbo].[MohasebeTarikh1]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[MohasebeTarikh1] 
	-- Add the parameters for the stored procedure here
	(@fldDatejari bigint,
	@typemodedate1 varchar(20),
	@mobile int

	)
AS
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END

BEGIN
	
SET NOCOUNT ON;
create table #TempTb1 
(
id int,
fldSharheTafzili  Nvarchar(200) collate Persian_100_CI_AS,
fldOstan  Nvarchar(50) collate Persian_100_CI_AS,
fldCity  Nvarchar(50) collate Persian_100_CI_AS,
TDate1 bigint,
fldshomare  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeMeli  Nvarchar(20) collate Persian_100_CI_AS,
fldCodePosti  Nvarchar(20) collate Persian_100_CI_AS,
fldAddress  Nvarchar(200) collate Persian_100_CI_AS,
TypeMode varchar(20)

)


if @typemodedate1='sal'
begin
delete from #TempTb1 where TypeMode<>@typemodedate1

insert into #TempTb1(id,fldSharheTafzili,fldOstan,fldCity,TDate1,fldCodeMeli,fldCodePosti,fldAddress,TypeMode)
 select distinct dbo.tblTafzili.fldCodeTafzili,fldSharheTafzili,fldOstan,fldCity, fldDate1,fldCodeMeli
 ,fldCodePosti,fldAddress,'sal' 
 from tblTafzili INNER JOIN
	tblDaftarcheTell on dbo.tblDaftarcheTell.fldCodeTafzili=dbo.tblTafzili.fldCodeTafzili 	
 where SUBSTRING(CONVERT(nvarchar, fldDate1), 5, 4)=
 SUBSTRING(CONVERT(nvarchar, @fldDatejari), 5, 4)


end
if @typemodedate1='mah'
begin
  delete from #TempTb1 where TypeMode<>@typemodedate1

insert into #TempTb1(id,fldSharheTafzili,fldOstan,fldCity,TDate1,fldCodeMeli,fldCodePosti,fldAddress,TypeMode)
 select distinct dbo.tblTafzili.fldCodeTafzili,fldSharheTafzili,fldOstan,fldCity, fldDate1,fldCodeMeli
 ,fldCodePosti,fldAddress,@typemodedate1 
 from tblTafzili INNER JOIN
	tblDaftarcheTell on dbo.tblDaftarcheTell.fldCodeTafzili=dbo.tblTafzili.fldCodeTafzili 	
 where SUBSTRING(CONVERT(nvarchar, fldDate1), 7, 2)=
 SUBSTRING(CONVERT(nvarchar, @fldDatejari), 7, 2)


end

if @mobile=1 
		begin
declare @EmpCodeTafzili nvarchar(10)
			declare EmpCursor Cursor For
				Select id from #TempTb1  With (NoLock) 
					open EmpCursor
					fetch Next From EmpCursor into @EmpCodeTafzili
					While @@Fetch_Status = 0
						Begin	
															
							update #TempTb1  set  fldshomare=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
									where id=@EmpCodeTafzili
							Fetch Next from EmpCursor
						into @EmpCodeTafzili
					end

			Close EmpCurSor
			Deallocate EmpCursor
			end
 select id,fldSharheTafzili,fldOstan,fldCity,
 SUBSTRING(CONVERT(nvarchar, TDate1), 1, 4)+'/'+ SUBSTRING(CONVERT(nvarchar, TDate1), 5, 2)+'/'+ SUBSTRING(CONVERT(nvarchar, TDate1), 7, 2)as Tdate1
 ,fldshomare,fldCodeMeli,fldCodePosti,fldAddress from #TempTb1



END

GO
/****** Object:  StoredProcedure [dbo].[MohasebeTarikh2]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[MohasebeTarikh2] 
	-- Add the parameters for the stored procedure here
	(@fldDatejari bigint,
	@typemodedate2 varchar(20),
	@mobile int
	)
AS
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END

BEGIN
	
SET NOCOUNT ON;

create table #TempTb2 
(
id int,
fldSharheTafzili  Nvarchar(200) collate Persian_100_CI_AS,
fldOstan  Nvarchar(50) collate Persian_100_CI_AS,
fldCity  Nvarchar(50) collate Persian_100_CI_AS,
TDate2 bigint,
fldshomare  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeMeli  Nvarchar(20) collate Persian_100_CI_AS,
fldCodePosti  Nvarchar(20) collate Persian_100_CI_AS,
fldAddress  Nvarchar(200) collate Persian_100_CI_AS,
TypeMode varchar(20)

)


if @typemodedate2='sal'
begin
delete from #TempTb2 where TypeMode<>@typemodedate2

insert into #TempTb2(id,fldSharheTafzili,fldOstan,fldCity,TDate2,fldCodeMeli,fldCodePosti,fldAddress,TypeMode)
 select distinct dbo.tblTafzili.fldCodeTafzili,fldSharheTafzili,fldOstan,fldCity, fldDate2,fldCodeMeli
 ,fldCodePosti,fldAddress,@typemodedate2 
 from tblTafzili
 where SUBSTRING(CONVERT(nvarchar, fldDate2), 5, 4)=
 SUBSTRING(CONVERT(nvarchar, @fldDatejari), 5, 4)

 
end
if @typemodedate2='mah'
begin
  delete from #TempTb2 where TypeMode<>@typemodedate2

insert into #TempTb2(id,fldSharheTafzili,fldOstan,fldCity,TDate2,fldCodeMeli,fldCodePosti,fldAddress,TypeMode)
 select distinct dbo.tblTafzili.fldCodeTafzili,fldSharheTafzili,fldOstan,fldCity,fldDate2,fldCodeMeli
 ,fldCodePosti,fldAddress,@typemodedate2 
 from tblTafzili
 where SUBSTRING(CONVERT(nvarchar, fldDate2), 7, 2)=
 SUBSTRING(CONVERT(nvarchar, @fldDatejari), 7, 2)



end

if @mobile=1 
		begin
declare @EmpCodeTafzili nvarchar(10)
			declare EmpCursor Cursor For
				Select id from #TempTb2  With (NoLock) 
					open EmpCursor
					fetch Next From EmpCursor into @EmpCodeTafzili
					While @@Fetch_Status = 0
						Begin	
					

									
							update #TempTb2  set  fldshomare=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
									where id=@EmpCodeTafzili
							Fetch Next from EmpCursor
						into @EmpCodeTafzili
					end

			Close EmpCurSor
			Deallocate EmpCursor
			end

 select distinct id,fldSharheTafzili,fldOstan,fldCity,
 SUBSTRING(CONVERT(nvarchar, TDate2), 1, 4)+'/'+ SUBSTRING(CONVERT(nvarchar, TDate2), 5, 2)+'/'+ SUBSTRING(CONVERT(nvarchar, TDate2), 7, 2)as Tdate2,
 fldshomare,fldCodeMeli,fldCodePosti,fldAddress from #TempTb2



END

GO
/****** Object:  StoredProcedure [dbo].[NameHesab]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[NameHesab]
@CodeHesab nvarchar(10),
@SharheHesab nvarchar(50) Output

as
begin
	set @SharheHesab=(Select fldSharhehesab from tblKolMoin Where fldCodeHesab=@CodeHesab)
end

GO
/****** Object:  StoredProcedure [dbo].[NameKolMoin]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[NameKolMoin]
	@MoinKamel nvarchar(10),
	@strName nvarchar(50) output
AS
BEGIN

	set @strName =( Select fldSharheHesab from tblKolMoin Where fldCodeHesab=@MoinKamel) 

END

GO
/****** Object:  StoredProcedure [dbo].[NamePrinter]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[NamePrinter]
	-- Add the parameters for the stored procedure here
	@IdPrinter bigint,
	@EsmePrinter nvarchar(Max) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @strName nvarchar(max)

	set @strName = (Select fldNamePrinter from tblPrinters Where id=@IdPrinter)
	if @strName is null
		set @EsmePrinter=''
	else
		set @EsmePrinter = @strName
    
END

GO
/****** Object:  StoredProcedure [dbo].[NextOrBeforFactorShakhs]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[NextOrBeforFactorShakhs] 
	-- Add the parameters for the stored procedure here
@Type nvarchar(50),
@ShomareFactorForooshJari bigint,
@CodeTafzili nvarchar(10),
@NewShomareFactorForoosh bigint output

as

begin
   
       SET NOCOUNT ON;
  
	if @Type='before'
		begin
		 set @NewShomareFactorForoosh=(	SELECT        ISNULL( max(fldShomareFactor),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizeForoosh
				WHERE        (fldShomareFactor < @ShomareFactorForooshJari and  fldCodeTafsili=@CodeTafzili ))
		
		end

	if @Type='next'
		begin
		set @NewShomareFactorForoosh=(	SELECT        isnull( min(fldShomareFactor),@ShomareFactorForooshJari) AS Expr1
				FROM            tblRizeForoosh
				WHERE        (fldShomareFactor > @ShomareFactorForooshJari  and  fldCodeTafsili=@CodeTafzili))
		end
end

GO
/****** Object:  StoredProcedure [dbo].[procGridBargashtAzForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[procGridBargashtAzForoosh]
	--چند زبانه
	@language nvarchar(10),
	@fldShomareFactor bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN


	SET NOCOUNT ON;


	create table #tblTemp
	(
	fldRadif bigint, 
	fldCodeKala bigint, 
	fldNameKala nvarchar(100), 
	fldSharhKala nvarchar(100),
	fldTedadCarton nvarchar(10), 
    fldTedadJoz decimal(18,4),
	fldFeeJoz decimal(18,4),
	fldFeeYekCarton decimal(18,4), 
	fldFeeKol decimal(18,4), 
    fldDarsadTakhfif decimal(18,4), 
	fldMablaghTakhfif decimal(18,4), 
	fldFeeVahed1BadAzTakhfif decimal(18,4), 
    fldFeeVahed2BadAzTakhfif decimal(18,4), 
	fldArzeshAfzode decimal(18,4),
	fldFeeKolBadAzTakhfif decimal(18,4), 
    fldMablaghKhales decimal(18,4),
	fldCodeAnbar bigint,
	fldNameVahed  nvarchar(100), 
	fldNameVahed2  nvarchar(100), 
    fldNameAnbar  nvarchar(100), 
	fldVahed1Code bigint,
	fldVahed2Code bigint, 
	fldGhymatMasrafkonande decimal(18,4),
	fldDateEngheza bigint
	)

	insert into #tblTemp
		(fldRadif,fldCodeKala,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,
		fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldDateEngheza)
   SELECT        dbo.tblRizeBargashtAzForoosh.fldRadif, dbo.tblRizeBargashtAzForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblRizeBargashtAzForoosh.fldSharhKala, dbo.tblRizeBargashtAzForoosh.fldTedadCarton, 
                         Convert(float,dbo.tblRizeBargashtAzForoosh.fldTedadJoz) as fldTedadJoz, dbo.tblRizeBargashtAzForoosh.fldFeeJoz, dbo.tblRizeBargashtAzForoosh.fldFeeYekCarton, dbo.tblRizeBargashtAzForoosh.fldFeeKol, 
                         dbo.tblRizeBargashtAzForoosh.fldDarsadTakhfif, dbo.tblRizeBargashtAzForoosh.fldMablaghTakhfif, dbo.tblRizeBargashtAzForoosh.fldFeeVahed1BadAzTakhfif, 
                         dbo.tblRizeBargashtAzForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldArzeshAfzode, dbo.tblRizeBargashtAzForoosh.fldFeeKolBadAzTakhfif, 
                         dbo.tblRizeBargashtAzForoosh.fldMablaghKhales, dbo.tblRizeBargashtAzForoosh.fldCodeAnbar, tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2, 
                         dbo.tblAnbar.fldNameAnbar, tblVahed_2.ID AS fldVahed1Code, dbo.tblVahed.ID AS fldVahed2Code, isnull(dbo.tblKala.fldGhymatMasrafkonande,0) as fldGhymatMasrafkonande,fldDateEngheza
FROM            dbo.tblAnbar INNER JOIN
                         dbo.tblRizeBargashtAzForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeBargashtAzForoosh.fldCodeAnbar INNER JOIN
                         dbo.tblKala ON dbo.tblRizeBargashtAzForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
WHERE        (dbo.tblRizeBargashtAzForoosh.fldShomareFactor = @fldShomareFactor)


update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language

select fldRadif,fldCodeKala,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,
fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldDateEngheza from #tblTemp
END

GO
/****** Object:  StoredProcedure [dbo].[procGridBargashtAzKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[procGridBargashtAzKharid]
--چندزبانه
	@language nvarchar(10),
	@fldShomareFactor bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	create table #tblTemp
	(
	fldRadif bigint, 
	fldCodeKala bigint, 
	fldNameKala nvarchar(100), 
	fldSharhKala nvarchar(100),
	fldTedadCarton nvarchar(10), 
    fldTedadJoz decimal(18,4),
	fldFeeJoz decimal(18,4),
	fldFeeYekCarton decimal(18,4), 
	fldFeeKol decimal(18,4), 
    fldDarsadTakhfif decimal(18,4), 
	fldMablaghTakhfif decimal(18,4), 
	fldFeeVahed1BadAzTakhfif decimal(18,4), 
    fldFeeVahed2BadAzTakhfif decimal(18,4), 
	fldArzeshAfzode decimal(18,4),
	fldFeeKolBadAzTakhfif decimal(18,4), 
    fldMablaghKhales decimal(18,4),
	fldCodeAnbar bigint,
	fldHamloNaghl decimal(18,4),
	fldNameVahed  nvarchar(100), 
	fldNameVahed2  nvarchar(100), 
    fldNameAnbar  nvarchar(100), 
	fldVahed1Code bigint,
	fldVahed2Code bigint, 
	fldGhymatMasrafkonande decimal(18,4),
	fldDateEngheza bigint
	)

	insert into #tblTemp
		(fldRadif,fldCodeKala,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,
		fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,fldHamloNaghl,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldDateEngheza)
   SELECT        dbo.tblRizeBargashtAzKharid.fldRadif, dbo.tblRizeBargashtAzKharid.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblRizeBargashtAzKharid.fldSharhKala, dbo.tblRizeBargashtAzKharid.fldTedadCarton, 
                         convert(float, dbo.tblRizeBargashtAzKharid.fldTedadJoz)as fldTedadJoz, dbo.tblRizeBargashtAzKharid.fldFeeJoz, dbo.tblRizeBargashtAzKharid.fldFeeYekCarton, dbo.tblRizeBargashtAzKharid.fldFeeKol, 
                         dbo.tblRizeBargashtAzKharid.fldDarsadTakhfif, dbo.tblRizeBargashtAzKharid.fldMablaghTakhfif, dbo.tblRizeBargashtAzKharid.fldFeeVahed1BadAzTakhfif, 
                         dbo.tblRizeBargashtAzKharid.fldFeeVahed2BadAzTakhfif, dbo.tblRizeBargashtAzKharid.fldArzeshAfzode, dbo.tblRizeBargashtAzKharid.fldFeeKolBadAzTakhfif, 
                         dbo.tblRizeBargashtAzKharid.fldMablaghKhales, dbo.tblRizeBargashtAzKharid.fldCodeAnbar,dbo.tblRizeBargashtAzKharid.fldHamloNaghl, tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2, 
                         dbo.tblAnbar.fldNameAnbar, tblVahed_2.ID AS fldVahed1Code, dbo.tblVahed.ID AS fldVahed2Code, isnull(dbo.tblKala.fldGhymatMasrafkonande,0) as fldGhymatMasrafkonande,fldDateEngheza
			FROM            dbo.tblAnbar INNER JOIN
									 dbo.tblRizeBargashtAzKharid ON dbo.tblAnbar.fldCode = dbo.tblRizeBargashtAzKharid.fldCodeAnbar INNER JOIN
									 dbo.tblKala ON dbo.tblRizeBargashtAzKharid.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
									 dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
									 dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
			WHERE        (dbo.tblRizeBargashtAzKharid.fldShomareFactor = @fldShomareFactor)


update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language

select fldRadif,fldCodeKala,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,
fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,fldHamloNaghl,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldDateEngheza from #tblTemp


END



GO
/****** Object:  StoredProcedure [dbo].[procGridForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[procGridForoosh]
--چندزبانه
	@language nvarchar(10),
	@fldShomareFactor bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	create table #tblTemp
	(
	fldRadif bigint, 
	fldCodeKala bigint, 
	fldNameKala nvarchar(100), 
	fldSharhKala nvarchar(100),
	fldTedadCarton nvarchar(10), 
    fldTedadJoz decimal(18,4),
	fldFeeJoz decimal(18,4),
	fldFeeYekCarton decimal(18,4), 
	fldFeeKol decimal(18,4), 
    fldDarsadTakhfif decimal(18,4), 
	fldMablaghTakhfif decimal(18,4), 
	fldFeeVahed1BadAzTakhfif decimal(18,4), 
    fldFeeVahed2BadAzTakhfif decimal(18,4), 
	fldArzeshAfzode decimal(18,4),
	fldFeeKolBadAzTakhfif decimal(18,4), 
    fldMablaghKhales decimal(18,4),
	fldCodeAnbar bigint,
	fldNameVahed  nvarchar(100), 
	fldNameVahed2  nvarchar(100), 
    fldNameAnbar  nvarchar(100), 
	fldVahed1Code bigint,
	fldVahed2Code bigint, 
	fldGhymatMasrafkonande decimal(18,4),
	fldType nvarchar(13),
	fldDateEngheza bigint
	)

	insert into #tblTemp
		(fldRadif,fldCodeKala,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,
		fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldType,fldDateEngheza)
		   SELECT        dbo.tblRizeForoosh.fldRadif, dbo.tblRizeForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblRizeForoosh.fldSharhKala, dbo.tblRizeForoosh.fldTedadCarton, 
								 Convert(float,dbo.tblRizeForoosh.fldTedadJoz) as fldTedadJoz, dbo.tblRizeForoosh.fldFeeJoz, dbo.tblRizeForoosh.fldFeeYekCarton, dbo.tblRizeForoosh.fldFeeKol, 
								 dbo.tblRizeForoosh.fldDarsadTakhfif, dbo.tblRizeForoosh.fldMablaghTakhfif, dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, 
								 dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif, 
								 dbo.tblRizeForoosh.fldMablaghKhales, dbo.tblRizeForoosh.fldCodeAnbar, tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2, 
								 dbo.tblAnbar.fldNameAnbar, tblVahed_2.ID AS fldVahed1Code, dbo.tblVahed.ID AS fldVahed2Code, isnull(dbo.tblKala.fldGhymatMasrafkonande,0) as fldGhymatMasrafkonande,fldEshantion as fldType,fldDateEngheza
		FROM            dbo.tblAnbar INNER JOIN
								 dbo.tblRizeForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeForoosh.fldCodeAnbar INNER JOIN
								 dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
								 dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
								 dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
		WHERE        (dbo.tblRizeForoosh.fldShomareFactor = @fldShomareFactor)


		update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language

		select fldRadif,fldCodeKala,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,
		fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldType,fldDateEngheza from #tblTemp
END

GO
/****** Object:  StoredProcedure [dbo].[procGridForooshAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[procGridForooshAmani]
	@fldShomareFactor bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

   SELECT        dbo.tblRizeForoosh.fldRadif, dbo.tblRizeForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblRizeForoosh.fldSharhKala, dbo.tblRizeForoosh.fldTedadCarton, 
                         Convert(float,dbo.tblRizeForoosh.fldTedadJoz) as fldTedadJoz, dbo.tblRizeForoosh.fldFeeJoz, dbo.tblRizeForoosh.fldFeeYekCarton, dbo.tblRizeForoosh.fldFeeKol, 
                         dbo.tblRizeForoosh.fldDarsadTakhfif, dbo.tblRizeForoosh.fldMablaghTakhfif, dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, 
                         dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif, 
                         dbo.tblRizeForoosh.fldMablaghKhales, dbo.tblRizeForoosh.fldCodeAnbar, tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2, 
                         dbo.tblAnbar.fldNameAnbar, tblVahed_2.ID AS fldVahed1Code, dbo.tblVahed.ID AS fldVahed2Code, isnull(dbo.tblKala.fldGhymatMasrafkonande,0) as fldGhymatMasrafkonande,fldDateEngheza
FROM            dbo.tblAnbar INNER JOIN
                         dbo.tblRizeForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeForoosh.fldCodeAnbar INNER JOIN
                         dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
WHERE        (dbo.tblRizeForoosh.fldShomareAmani = @fldShomareFactor)
END

GO
/****** Object:  StoredProcedure [dbo].[procGridForooshBargashtAmani]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[procGridForooshBargashtAmani]
	@fldShomareFactor bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	create TABLE #TBL1(
		fldRadif BIGINT,
		fldCodeKala bigint,
		fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
		fldSharhKala nvarchar(100) COLLATE Persian_100_CI_AS,
		fldTedadCarton nvarchar(10) COLLATE Persian_100_CI_AS,
		fldTedadJoz decimal(18,2),
		fldFeeJoz decimal(18,2),
		fldFeeYekCarton decimal(18,0),
		fldFeeKol decimal(18,0),
		fldDarsadTakhfif decimal(18,2),		
		fldMablaghTakhfif decimal(18,0),
		fldFeeVahed1BadAzTakhfif decimal(18,2),
		fldFeeVahed2BadAzTakhfif decimal(18,2),
		fldArzeshAfzode decimal(18,2),
		fldFeeKolBadAzTakhfif  decimal(18,0), 
        fldMablaghKhales  decimal(18,0), 
		fldCodeAnbar bigint, 
		fldNameVahed nvarchar(100) COLLATE Persian_100_CI_AS,
		fldNameVahed2 nvarchar(100) COLLATE Persian_100_CI_AS,
		fldNameAnbar nvarchar(50) COLLATE Persian_100_CI_AS,
		fldVahed1Code int,
		fldVahed2Code int, 
		fldGhymatMasrafkonande decimal(18,0), 
		fldDateEngheza bigint
	)


   insert into #TBL1
		(fldRadif ,fldCodeKala,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif, 
			fldMablaghKhales,fldCodeAnbar,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldDateEngheza)
   SELECT        dbo.tblRizeBargashtAzForoosh.fldRadif, dbo.tblRizeBargashtAzForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblRizeBargashtAzForoosh.fldSharhKala, dbo.tblRizeBargashtAzForoosh.fldTedadCarton, 
                         Convert(float,dbo.tblRizeBargashtAzForoosh.fldTedadJoz) as fldTedadJoz, dbo.tblRizeBargashtAzForoosh.fldFeeJoz, dbo.tblRizeBargashtAzForoosh.fldFeeYekCarton, dbo.tblRizeBargashtAzForoosh.fldFeeKol, 
                         dbo.tblRizeBargashtAzForoosh.fldDarsadTakhfif, dbo.tblRizeBargashtAzForoosh.fldMablaghTakhfif, dbo.tblRizeBargashtAzForoosh.fldFeeVahed1BadAzTakhfif, 
                         dbo.tblRizeBargashtAzForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldArzeshAfzode, dbo.tblRizeBargashtAzForoosh.fldFeeKolBadAzTakhfif, 
                         dbo.tblRizeBargashtAzForoosh.fldMablaghKhales, dbo.tblRizeBargashtAzForoosh.fldCodeAnbar, tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2, 
                         dbo.tblAnbar.fldNameAnbar, tblVahed_2.ID AS fldVahed1Code, dbo.tblVahed.ID AS fldVahed2Code, isnull(dbo.tblKala.fldGhymatMasrafkonande,0) as fldGhymatMasrafkonande,fldDateEngheza
FROM            dbo.tblAnbar INNER JOIN
                         dbo.tblRizeBargashtAzForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeBargashtAzForoosh.fldCodeAnbar INNER JOIN
                         dbo.tblKala ON dbo.tblRizeBargashtAzForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
WHERE        (dbo.tblRizeBargashtAzForoosh.fldShomareBargashtAmani = @fldShomareFactor)

update #tbl1 set fldCodeAnbar=tblRizeKharid.fldCodeAnbar from tblRizeKharid Where tblRizeKharid.fldShomareBargashtAmani=@fldShomareFactor
update #tbl1 set fldNameAnbar=tblAnbar.fldNameAnbar from tblAnbar Where fldCodeAnbar=tblAnbar.fldCode

Select fldRadif,fldCodeKala ,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,
		fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldDateEngheza  from #TBL1 
END

GO
/****** Object:  StoredProcedure [dbo].[procGridKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[procGridKharid]
--چندزبانه
	@language nvarchar(10),
	@fldShomareFactor bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


	create table #tblTemp
	(
	fldRadif bigint, 
	fldCodeKala bigint, 
	fldNameKala nvarchar(100), 
	fldSharhKala nvarchar(100),
	fldTedadCarton nvarchar(10), 
    fldTedadJoz decimal(18,4),
	fldFeeJoz decimal(18,4),
	fldFeeYekCarton decimal(18,4), 
	fldFeeKol decimal(18,4), 
    fldDarsadTakhfif decimal(18,4), 
	fldMablaghTakhfif decimal(18,4), 
	fldFeeVahed1BadAzTakhfif decimal(18,4), 
    fldFeeVahed2BadAzTakhfif decimal(18,4), 
	fldArzeshAfzode decimal(18,4),
	fldFeeKolBadAzTakhfif decimal(18,4), 
    fldMablaghKhales decimal(18,4),
	fldCodeAnbar bigint,
	fldHamloNaghl decimal(18,4),
	fldNameVahed  nvarchar(100), 
	fldNameVahed2  nvarchar(100), 
    fldNameAnbar  nvarchar(100), 
	fldVahed1Code bigint,
	fldVahed2Code bigint, 
	fldGhymatMasrafkonande decimal(18,4),
	fldDateEngheza bigint
	)

	insert into #tblTemp
		(fldRadif,fldCodeKala,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,
		fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,fldHamloNaghl,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldDateEngheza)
   SELECT        dbo.tblRizeKharid.fldRadif, dbo.tblRizeKharid.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblRizeKharid.fldSharhKala, dbo.tblRizeKharid.fldTedadCarton, 
                         convert(float, dbo.tblRizeKharid.fldTedadJoz) as fldTedadJoz, dbo.tblRizeKharid.fldFeeJoz, dbo.tblRizeKharid.fldFeeYekCarton, dbo.tblRizeKharid.fldFeeKol, 
                         dbo.tblRizeKharid.fldDarsadTakhfif, dbo.tblRizeKharid.fldMablaghTakhfif, dbo.tblRizeKharid.fldFeeVahed1BadAzTakhfif, 
                         dbo.tblRizeKharid.fldFeeVahed2BadAzTakhfif, dbo.tblRizeKharid.fldArzeshAfzode, dbo.tblRizeKharid.fldFeeKolBadAzTakhfif, 
                         dbo.tblRizeKharid.fldMablaghKhales, dbo.tblRizeKharid.fldCodeAnbar,dbo.tblRizeKharid.fldHamloNaghl, tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2, 
                         dbo.tblAnbar.fldNameAnbar, tblVahed_2.ID AS fldVahed1Code, dbo.tblVahed.ID AS fldVahed2Code, isnull(dbo.tblKala.fldGhymatMasrafkonande,0) as fldGhymatMasrafkonande,fldDateEngheza
			FROM            dbo.tblAnbar INNER JOIN
									 dbo.tblRizeKharid ON dbo.tblAnbar.fldCode = dbo.tblRizeKharid.fldCodeAnbar INNER JOIN
									 dbo.tblKala ON dbo.tblRizeKharid.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
									 dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
									 dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
			WHERE        (dbo.tblRizeKharid.fldShomareFactor = @fldShomareFactor)

update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language

select fldRadif,fldCodeKala,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,
fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,fldHamloNaghl,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldDateEngheza from #tblTemp
END

GO
/****** Object:  StoredProcedure [dbo].[procGridPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[procGridPishForoosh]
--چندزبانه
	@language nvarchar(10),
	@fldShomareFactor bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN


	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


create table #tblTemp
	(
	fldRadif bigint, 
	fldCodeKala bigint, 
	fldNameKala nvarchar(100), 
	fldSharhKala nvarchar(100),
	fldTedadCarton nvarchar(10), 
    fldTedadJoz decimal(18,4),
	fldFeeJoz decimal(18,4),
	fldFeeYekCarton decimal(18,4), 
	fldFeeKol decimal(18,4), 
    fldDarsadTakhfif decimal(18,4), 
	fldMablaghTakhfif decimal(18,4), 
	fldFeeVahed1BadAzTakhfif decimal(18,4), 
    fldFeeVahed2BadAzTakhfif decimal(18,4), 
	fldArzeshAfzode decimal(18,4),
	fldFeeKolBadAzTakhfif decimal(18,4), 
    fldMablaghKhales decimal(18,4),
	fldCodeAnbar bigint,
	fldCodeVasete bigint,
	fldNameVahed  nvarchar(100), 
	fldNameVahed2  nvarchar(100), 
    fldNameAnbar  nvarchar(100), 
	fldVahed1Code bigint,
	fldVahed2Code bigint, 
	fldGhymatMasrafkonande decimal(18,4),
	fldType nvarchar(13)
	)

	insert into #tblTemp
		(fldRadif,fldCodeKala,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,
		fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,fldCodeVasete,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldType)
   SELECT        dbo.tblRizePishForoosh.fldRadif, dbo.tblRizePishForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblRizePishForoosh.fldSharhKala, dbo.tblRizePishForoosh.fldTedadCarton, 
                         Convert(float,dbo.tblRizePishForoosh.fldTedadJoz) as fldTedadJoz, dbo.tblRizePishForoosh.fldFeeJoz, dbo.tblRizePishForoosh.fldFeeYekCarton, dbo.tblRizePishForoosh.fldFeeKol, 
                         dbo.tblRizePishForoosh.fldDarsadTakhfif, dbo.tblRizePishForoosh.fldMablaghTakhfif, dbo.tblRizePishForoosh.fldFeeVahed1BadAzTakhfif, 
                         dbo.tblRizePishForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizePishForoosh.fldArzeshAfzode, dbo.tblRizePishForoosh.fldFeeKolBadAzTakhfif, 
                         dbo.tblRizePishForoosh.fldMablaghKhales, dbo.tblRizePishForoosh.fldCodeAnbar,dbo.tblRizePishForoosh.fldCodeVasete, tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2, 
                         dbo.tblAnbar.fldNameAnbar, tblVahed_2.ID AS fldVahed1Code, dbo.tblVahed.ID AS fldVahed2Code, isnull(dbo.tblKala.fldGhymatMasrafkonande,0) as fldGhymatMasrafkonande,fldEshantion as fldType
FROM            dbo.tblAnbar INNER JOIN
                         dbo.tblRizePishForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizePishForoosh.fldCodeAnbar INNER JOIN
                         dbo.tblKala ON dbo.tblRizePishForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
WHERE        (dbo.tblRizePishForoosh.fldShomareFactor = @fldShomareFactor)


		update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language

		select fldRadif,fldCodeKala,fldNameKala,fldSharhKala,fldTedadCarton,fldTedadJoz,fldFeeJoz,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,
		fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,fldCodeAnbar,fldCodeVasete,fldNameVahed,fldNameVahed2,fldNameAnbar,fldVahed1Code,fldVahed2Code,fldGhymatMasrafkonande,fldType from #tblTemp

END

GO
/****** Object:  StoredProcedure [dbo].[procGridPishPishForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[procGridPishPishForoosh]
--چندزبانه
	@fldShomareFactor bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

   SELECT        dbo.tblRizePishPishForoosh.fldRadif, dbo.tblRizePishPishForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblRizePishPishForoosh.fldSharhKala, dbo.tblRizePishPishForoosh.fldTedadCarton, 
                         convert(float,dbo.tblRizePishPishForoosh.fldTedadJoz) as fldTedadJoz, dbo.tblRizePishPishForoosh.fldFeeJoz, dbo.tblRizePishPishForoosh.fldFeeYekCarton, dbo.tblRizePishPishForoosh.fldFeeKol, 
                         dbo.tblRizePishPishForoosh.fldDarsadTakhfif, dbo.tblRizePishPishForoosh.fldMablaghTakhfif, dbo.tblRizePishPishForoosh.fldFeeVahed1BadAzTakhfif, 
                         dbo.tblRizePishPishForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizePishPishForoosh.fldArzeshAfzode, dbo.tblRizePishPishForoosh.fldFeeKolBadAzTakhfif, 
                         dbo.tblRizePishPishForoosh.fldMablaghKhales, dbo.tblRizePishPishForoosh.fldCodeAnbar,dbo.tblRizePishPishForoosh.fldCodeVasete, tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2, 
                         dbo.tblAnbar.fldNameAnbar, tblVahed_2.ID AS fldVahed1Code, dbo.tblVahed.ID AS fldVahed2Code, isnull(dbo.tblKala.fldGhymatMasrafkonande,0) as fldGhymatMasrafkonande
FROM            dbo.tblAnbar INNER JOIN
                         dbo.tblRizePishPishForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizePishPishForoosh.fldCodeAnbar INNER JOIN
                         dbo.tblKala ON dbo.tblRizePishPishForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
WHERE        (dbo.tblRizePishPishForoosh.fldShomareFactor = @fldShomareFactor)
END

GO
/****** Object:  StoredProcedure [dbo].[QueryDynamic]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[QueryDynamic]
	@strQuery nvarchar(3000)
as
begin
	--declare	@strQuery nvarchar(3000)
	--set @strQuery='select * from tblForoosh'
	execute (@strQuery)
end

GO
/****** Object:  StoredProcedure [dbo].[Report_ListAshkhas_groupby_CodeGroup_order_SharhTafsili_final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Report_ListAshkhas_groupby_CodeGroup_order_SharhTafsili_final]
@CodeGroupAshkhas varchar(5000)=null
as
--declare @myReport varchar(5000)
begin
     SET NOCOUNT ON;
	 -- -----------------------------------------TempTable In Below Code
IF OBJECT_ID('tempdb..#TempListAshkhas1') IS NOT NULL
 begin
	delete from  #TempListAshkhas1
 end
else 
begin
create table #TempListAshkhas1(
fldSharheTafzili Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldGroupName Nvarchar(200) COLLATE Persian_100_CI_AS,
fldShomare Nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeTafzili bigint, 
fldCodeMeli Nvarchar(50) COLLATE Persian_100_CI_AS,
fldAddress Nvarchar(400) COLLATE Persian_100_CI_AS

)
end
insert into #TempListAshkhas1(fldSharheTafzili,fldCodeGroup,fldGroupName,fldCodeTafzili,fldCodeMeli,fldAddress)
select dbo.tblTafzili.fldSharheTafzili,
dbo.tblTafzili.fldCodeGroup,
dbo.tblGroupAshkhas.fldGroupName,
dbo.tblTafzili.fldCodeTafzili, 
isnull(dbo.tblTafzili.fldCodeMeli,0) as fldCodeMeli,
isnull (dbo.tblTafzili.fldAddress,'') as fldAddress
                         
FROM            dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup 


update #TempListAshkhas1 set fldShomare=tbl5.fldShomare from
(select fldShomare,fldcodetafzili from
	(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
	FROM            [tblDaftarcheTell]) DB_Address
	where RowNo=1) as tbl5 
	where #TempListAshkhas1.fldCodetafzili=tbl5.fldCodeTafzili


if @CodeGroupAshkhas<>''
begin
declare @StrDeleteGroupAshkhas varchar(5000)
set  @StrDeleteGroupAshkhas='delete from #TempListAshkhas1
	where  '+ @CodeGroupAshkhas + ''
	execute(@StrDeleteGroupAshkhas)
end


SELECT  fldSharheTafzili,fldCodeGroup,fldGroupName,fldShomare,fldCodeTafzili,fldCodeMeli,fldAddress
                   
FROM      #TempListAshkhas1
ORDER BY  fldSharheTafzili asc

----set @myReport= 'SELECT DISTINCT 
----                         dbo.tblTafzili.fldSharheTafzili,dbo.tblTafzili.fldCodeGroup,dbo.tblGroupAshkhas.fldGroupName,dbo.tblDaftarcheTell.fldShomare,dbo.tblTafzili.fldCodeTafzili, 
----                         dbo.tblTafzili.fldCodeMeli,dbo.tblTafzili.fldAddress
----FROM            dbo.tblGroupAshkhas INNER JOIN
----                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup LEFT OUTER JOIN
----                         dbo.tblDaftarcheTell ON CONVERT(int, dbo.tblTafzili.fldCodeTafzili) = CONVERT(int, dbo.tblDaftarcheTell.fldCodeTafzili)
----GROUP BY  dbo.tblGroupAshkhas.fldGroupName,dbo.tblTafzili.fldCodeGroup,dbo.tblGroupAshkhas.fldGroupId,dbo.tblDaftarcheTell.fldShomare,dbo.tblTafzili.fldCodeTafzili, 
----                         dbo.tblTafzili.fldSharheTafzili,dbo.tblTafzili.fldCodeMeli,dbo.tblTafzili.fldAddress
----HAVING        (dbo.tblTafzili.fldCodeGroup IN (' + @CodeGroupAshkhas + '))
----ORDER BY  dbo.tblTafzili.fldSharheTafzili'

----exec( @myReport)
end

GO
/****** Object:  StoredProcedure [dbo].[Report_ListAshkhas_groupby_NameGroup_order_SharhTafsili_final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Report_ListAshkhas_groupby_NameGroup_order_SharhTafsili_final]
@CodeGroupAshkhas varchar(5000)=null
as
--declare @myReport varchar(5000)
begin
     SET NOCOUNT ON;
	 -- -----------------------------------------TempTable In Below Code

	 IF OBJECT_ID('tempdb..#TempListAshkhas') IS NOT NULL
 begin
	delete from  #TempListAshkhas
 end
else 
begin
create table #TempListAshkhas(
fldSharheTafzili Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldGroupName Nvarchar(200) COLLATE Persian_100_CI_AS,
fldShomare Nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeTafzili bigint, 
fldCodeMeli Nvarchar(50) COLLATE Persian_100_CI_AS,
fldAddress Nvarchar(400) COLLATE Persian_100_CI_AS

)
end
insert into #TempListAshkhas(fldSharheTafzili,fldCodeGroup,fldGroupName,fldCodeTafzili,fldCodeMeli,fldAddress)
select dbo.tblTafzili.fldSharheTafzili,
dbo.tblTafzili.fldCodeGroup,
dbo.tblGroupAshkhas.fldGroupName,
dbo.tblTafzili.fldCodeTafzili, 
isnull(dbo.tblTafzili.fldCodeMeli,0) as fldCodeMeli,
isnull (dbo.tblTafzili.fldAddress,'') as fldAddress
                         
FROM            dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup




update #TempListAshkhas set fldShomare=tbl5.fldShomare from
(select fldShomare,fldcodetafzili from
	(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
	FROM            [tblDaftarcheTell]) DB_Address
	where RowNo=1) as tbl5 
	where #TempListAshkhas.fldCodetafzili=tbl5.fldCodeTafzili


if @CodeGroupAshkhas<>''
begin
declare @StrDeleteGroupAshkhas varchar(5000)
set  @StrDeleteGroupAshkhas='delete from #TempListAshkhas
	where  '+ @CodeGroupAshkhas + ''
	execute(@StrDeleteGroupAshkhas)
end


SELECT  fldSharheTafzili,fldCodeGroup,fldGroupName,fldShomare,fldCodeTafzili,fldCodeMeli,fldAddress
                   
FROM      #TempListAshkhas
ORDER BY  fldSharheTafzili asc


--set @myReport= 'SELECT DISTINCT 
--                         dbo.tblTafzili.fldSharheTafzili,dbo.tblTafzili.fldCodeGroup,dbo.tblGroupAshkhas.fldGroupName,isnull(dbo.tblDaftarcheTell.fldShomare,0) as fldShomare,dbo.tblTafzili.fldCodeTafzili, 
--                         isnull(dbo.tblTafzili.fldCodeMeli,0) as fldCodeMeli,dbo.tblTafzili.fldAddress
--FROM            dbo.tblGroupAshkhas INNER JOIN
--                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup LEFT OUTER JOIN
--                         dbo.tblDaftarcheTell ON CONVERT(bigint, dbo.tblTafzili.fldCodeTafzili) = CONVERT(bigint, dbo.tblDaftarcheTell.fldCodeTafzili)
--GROUP BY  dbo.tblGroupAshkhas.fldGroupName,dbo.tblTafzili.fldCodeGroup,dbo.tblGroupAshkhas.fldGroupId,dbo.tblDaftarcheTell.fldShomare,dbo.tblTafzili.fldCodeTafzili, 
--                         dbo.tblTafzili.fldSharheTafzili,dbo.tblTafzili.fldCodeMeli,dbo.tblTafzili.fldAddress
--HAVING          dbo.tblTafzili.fldCodeGroup IN (' + @CodeGroupAshkhas + ')
--ORDER BY  dbo.tblTafzili.fldSharheTafzili '

--exec( @myReport)


        

 END

GO
/****** Object:  StoredProcedure [dbo].[Report_ListKala_groupby_NameGroup_order_CodeKala_final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Report_ListKala_groupby_NameGroup_order_CodeKala_final]
@language nvarchar(10),
@CodeGroupKala varchar(5000)
as
--declare @myReport varchar(5000)
begin



SET NOCOUNT ON;

	-- -----------------------------------------TempTable In Below Code

IF OBJECT_ID('tempdb..#TempListKala1') IS NOT NULL
 begin
	delete from  #TempListKala1
 end
else 
begin
create table #TempListKala1(
fldNameKala         nvarchar(300) COLLATE Persian_100_CI_AS, 
fldCodeKala         bigint,
fldCodeVahed1       varchar(100) COLLATE Persian_100_CI_AS,
fldCodeVahed2       varchar(100) COLLATE Persian_100_CI_AS, 
fldNameVahed        nvarchar(100) COLLATE Persian_100_CI_AS, 
FldNameVahed2       nvarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroup        bigint,
fldGroupName        nvarchar(300) COLLATE Persian_100_CI_AS,
fldFeeForoosh       varchar(100) COLLATE Persian_100_CI_AS , 
fldFeeBadAzTakhfif  varchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh    varchar(50) COLLATE Persian_100_CI_AS,
fldNerkhePorsantKol varchar(50) COLLATE Persian_100_CI_AS,
fldNerkhePorsantJoz varchar(50) COLLATE Persian_100_CI_AS,
fldGhymatMasrafkonande varchar(50) COLLATE Persian_100_CI_AS,
fldSerial varchar(50) COLLATE Persian_100_CI_AS)
end

insert into #TempListKala1(fldNameKala,fldCodeKala,fldCodeVahed1,fldCodeVahed2,fldNameVahed,FldNameVahed2,
                          fldCodeGroup,fldGroupName,fldFeeForoosh,fldFeeBadAzTakhfif,fldNogheSefaresh,
						  fldNerkhePorsantKol,fldNerkhePorsantJoz,fldGhymatMasrafkonande,fldSerial)
select   dbo.tblKala.fldNameKala, dbo.tblKala.fldCodeKala,  dbo.tblKala.fldCodeVahed1, dbo.tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed, 
                         dbo.tblVahed.fldNameVahed AS FldNameVahed2, dbo.tblKala.fldCodeGroup, dbo.tblGroupKala.fldGroupName, dbo.tblKala.fldFeeForoosh, 
                         dbo.tblKala.fldFeeBadAzTakhfif, dbo.tblKala.fldNogheSefaresh, dbo.tblKala.fldNerkhePorsantKol, dbo.tblKala.fldNerkhePorsantJoz,dbo.tblKala.fldGhymatMasrafkonande, dbo.tblKala.fldSerial
FROM     dbo.tblKala LEFT OUTER JOIN
                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId LEFT OUTER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed1 = tblVahed_1.ID LEFT OUTER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID


if @CodeGroupKala<>''
begin
	declare @StrDeleteGroupKala varchar(5000)
	set  @StrDeleteGroupKala='delete from #TempListKala1
		where '+ @CodeGroupKala + ''
		execute(@StrDeleteGroupKala)
end


update #TempListKala1 set #TempListKala1.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#TempListKala1.fldCodeKala and tblkalatranslate.fldLanguage=@Language

select 
fldNameKala,       
fldCodeKala,       
fldCodeVahed1,      
fldCodeVahed2,      
fldNameVahed,       
FldNameVahed2,      
fldCodeGroup,      
fldGroupName,       
fldFeeForoosh,      
fldFeeBadAzTakhfif, 
fldNogheSefaresh,   
fldNerkhePorsantKol,
fldNerkhePorsantJoz,
fldGhymatMasrafkonande,
fldSerial
from #TempListKala1
ORDER BY fldCodeKala Asc


--set @myReport=' select   DISTINCT dbo.tblKala.fldNameKala,dbo.tblKala.fldCodeKala,dbo.tblKala.fldCodeVahed1, dbo.tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed, 
--                         dbo.tblVahed.fldNameVahed AS FldNameVahed2, dbo.tblKala.fldCodeGroup, dbo.tblGroupKala.fldGroupName, dbo.tblKala.fldFeeForoosh, 
--                         dbo.tblKala.fldFeeBadAzTakhfif, dbo.tblKala.fldNogheSefaresh, dbo.tblKala.fldNerkhePorsantKol, dbo.tblKala.fldNerkhePorsantJoz
--FROM            dbo.tblKala LEFT OUTER JOIN
--                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId LEFT OUTER JOIN
--                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed1 = tblVahed_1.ID LEFT OUTER JOIN
--                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
--GROUP BY        dbo.tblGroupKala.fldGroupName,dbo.tblKala.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblKala.fldCodeVahed1, dbo.tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed, 
--                         dbo.tblKala.fldFeeForoosh, dbo.tblKala.fldFeeBadAzTakhfif, dbo.tblKala.fldNogheSefaresh, dbo.tblKala.fldNerkhePorsantKol, 
--                         dbo.tblKala.fldNerkhePorsantJoz, dbo.tblKala.fldCodeGroup, dbo.tblVahed.fldNameVahed
--						 HAVING        (dbo.tblKala.fldCodeGroup IN ('+  @CodeGroupKala +'))
--ORDER BY dbo.tblKala.fldCodeKala'
-- exec( @myReport)
end

GO
/****** Object:  StoredProcedure [dbo].[Report_ListKala_groupby_NameGroup_order_NameKala_final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Report_ListKala_groupby_NameGroup_order_NameKala_final]
@language nvarchar(10),
@CodeGroupKala varchar(5000)
as
--declare @myReport varchar(5000)
begin



SET NOCOUNT ON;

	-- -----------------------------------------TempTable In Below Code

IF OBJECT_ID('tempdb..#TempListKala1') IS NOT NULL
 begin
	delete from  #TempListKala1
 end
else 
begin
create table #TempListKala1(
fldNameKala         nvarchar(300) COLLATE Persian_100_CI_AS, 
fldCodeKala         bigint,
fldCodeVahed1       varchar(100) COLLATE Persian_100_CI_AS,
fldCodeVahed2       varchar(100) COLLATE Persian_100_CI_AS, 
fldNameVahed        nvarchar(100) COLLATE Persian_100_CI_AS, 
FldNameVahed2       nvarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroup        bigint,
fldGroupName        nvarchar(300) COLLATE Persian_100_CI_AS,
fldFeeForoosh       varchar(100) COLLATE Persian_100_CI_AS , 
fldFeeBadAzTakhfif  varchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh    varchar(50) COLLATE Persian_100_CI_AS,
fldNerkhePorsantKol varchar(50) COLLATE Persian_100_CI_AS,
fldNerkhePorsantJoz varchar(50) COLLATE Persian_100_CI_AS,
fldGhymatMasrafkonande varchar(50) COLLATE Persian_100_CI_AS,
fldSerial  varchar(50) COLLATE Persian_100_CI_AS)
end

insert into #TempListKala1(fldNameKala,fldCodeKala,fldCodeVahed1,fldCodeVahed2,fldNameVahed,FldNameVahed2,
                          fldCodeGroup,fldGroupName,fldFeeForoosh,fldFeeBadAzTakhfif,fldNogheSefaresh,
						  fldNerkhePorsantKol,fldNerkhePorsantJoz,fldGhymatMasrafkonande,fldSerial)
select   dbo.tblKala.fldNameKala, dbo.tblKala.fldCodeKala,  dbo.tblKala.fldCodeVahed1, dbo.tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed, 
                         dbo.tblVahed.fldNameVahed AS FldNameVahed2, dbo.tblKala.fldCodeGroup, dbo.tblGroupKala.fldGroupName, dbo.tblKala.fldFeeForoosh, 
                         dbo.tblKala.fldFeeBadAzTakhfif, dbo.tblKala.fldNogheSefaresh, dbo.tblKala.fldNerkhePorsantKol, dbo.tblKala.fldNerkhePorsantJoz,dbo.tblKala.fldGhymatMasrafkonande, dbo.tblKala.fldSerial
FROM     dbo.tblKala LEFT OUTER JOIN
                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId LEFT OUTER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed1 = tblVahed_1.ID LEFT OUTER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID


if @CodeGroupKala<>''
begin
	declare @StrDeleteGroupKala varchar(5000)
	set  @StrDeleteGroupKala='delete from #TempListKala1
		where '+ @CodeGroupKala + ''
		execute(@StrDeleteGroupKala)
end



update #TempListKala1 set #TempListKala1.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#TempListKala1.fldCodeKala and tblkalatranslate.fldLanguage=@Language


select 
fldNameKala,       
fldCodeKala,       
fldCodeVahed1,      
fldCodeVahed2,      
fldNameVahed,       
FldNameVahed2,      
fldCodeGroup,      
fldGroupName,       
fldFeeForoosh,      
fldFeeBadAzTakhfif, 
fldNogheSefaresh,   
fldNerkhePorsantKol,
fldNerkhePorsantJoz,
fldGhymatMasrafkonande,
fldSerial
from #TempListKala1
ORDER BY fldNameKala Asc


--set @myReport=' select   DISTINCT dbo.tblKala.fldNameKala,dbo.tblKala.fldCodeKala,dbo.tblKala.fldCodeVahed1, dbo.tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed, 
--                         dbo.tblVahed.fldNameVahed AS FldNameVahed2, dbo.tblKala.fldCodeGroup, dbo.tblGroupKala.fldGroupName, dbo.tblKala.fldFeeForoosh, 
--                         dbo.tblKala.fldFeeBadAzTakhfif, dbo.tblKala.fldNogheSefaresh, dbo.tblKala.fldNerkhePorsantKol, dbo.tblKala.fldNerkhePorsantJoz
--FROM            dbo.tblKala LEFT OUTER JOIN
--                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId LEFT OUTER JOIN
--                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed1 = tblVahed_1.ID LEFT OUTER JOIN
--                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
--GROUP BY        dbo.tblGroupKala.fldGroupName,dbo.tblKala.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblKala.fldCodeVahed1, dbo.tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed, 
--                         dbo.tblKala.fldFeeForoosh, dbo.tblKala.fldFeeBadAzTakhfif, dbo.tblKala.fldNogheSefaresh, dbo.tblKala.fldNerkhePorsantKol, 
--                         dbo.tblKala.fldNerkhePorsantJoz, dbo.tblKala.fldCodeGroup, dbo.tblVahed.fldNameVahed
--						 HAVING        (dbo.tblKala.fldCodeGroup IN ('+  @CodeGroupKala +'))
--ORDER BY dbo.tblKala.fldCodeKala'
-- exec( @myReport)
end
GO
/****** Object:  StoredProcedure [dbo].[ReportAll_ListAshkhas_groupby_CodeGroup_order_SharhTafsili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ReportAll_ListAshkhas_groupby_CodeGroup_order_SharhTafsili]
as


create table #TempListAshkhas(
fldSharheTafzili Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldGroupName Nvarchar(200) COLLATE Persian_100_CI_AS,
fldShomare Nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeTafzili bigint, 
fldCodeMeli Nvarchar(50) COLLATE Persian_100_CI_AS,
fldAddress Nvarchar(400) COLLATE Persian_100_CI_AS

)

insert into #TempListAshkhas (fldSharheTafzili ,fldCodeGroup ,fldGroupName ,fldCodeTafzili , fldCodeMeli ,fldAddress )
SELECT DISTINCT 
                         dbo.tblTafzili.fldSharheTafzili,dbo.tblTafzili.fldCodeGroup,dbo.tblGroupAshkhas.fldGroupName,dbo.tblTafzili.fldCodeTafzili, 
                         dbo.tblTafzili.fldCodeMeli,dbo.tblTafzili.fldAddress
FROM            dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup LEFT OUTER JOIN
                         dbo.tblDaftarcheTell ON CONVERT(int, dbo.tblTafzili.fldCodeTafzili) = CONVERT(int, dbo.tblDaftarcheTell.fldCodeTafzili)
GROUP BY  dbo.tblGroupAshkhas.fldGroupId,dbo.tblTafzili.fldCodeGroup,dbo.tblGroupAshkhas.fldGroupName,dbo.tblTafzili.fldCodeTafzili, 
                         dbo.tblTafzili.fldSharheTafzili,dbo.tblTafzili.fldCodeMeli,dbo.tblTafzili.fldAddress

--ORDER BY  dbo.tblTafzili.fldSharheTafzili



update #TempListAshkhas set fldShomare=tbl5.fldShomare from
(select fldShomare,fldcodetafzili from
	(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
	FROM            [tblDaftarcheTell]) DB_Address
	where RowNo=1) as tbl5 
	where #TempListAshkhas.fldCodetafzili=tbl5.fldCodeTafzili

Select  fldSharheTafzili ,fldCodeGroup ,fldGroupName ,fldCodeTafzili ,fldShomare, fldCodeMeli ,fldAddress  from #TempListAshkhas ORDER BY  fldSharheTafzili

GO
/****** Object:  StoredProcedure [dbo].[ReportAll_ListAshkhas_groupby_NameGroup_order_SharhTafsili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ReportAll_ListAshkhas_groupby_NameGroup_order_SharhTafsili]
as


create table #TempListAshkhas(
fldSharheTafzili Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldGroupName Nvarchar(200) COLLATE Persian_100_CI_AS,
fldShomare Nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeTafzili bigint, 
fldCodeMeli Nvarchar(50) COLLATE Persian_100_CI_AS,
fldAddress Nvarchar(400) COLLATE Persian_100_CI_AS

)
insert into #TempListAshkhas (fldSharheTafzili ,fldCodeGroup ,fldGroupName ,fldCodeTafzili , fldCodeMeli ,fldAddress )
SELECT DISTINCT 
                         dbo.tblTafzili.fldSharheTafzili,dbo.tblTafzili.fldCodeGroup,dbo.tblGroupAshkhas.fldGroupName,dbo.tblTafzili.fldCodeTafzili, 
                         dbo.tblTafzili.fldCodeMeli,dbo.tblTafzili.fldAddress
FROM            dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup LEFT OUTER JOIN
                         dbo.tblDaftarcheTell ON CONVERT(int, dbo.tblTafzili.fldCodeTafzili) = CONVERT(int, dbo.tblDaftarcheTell.fldCodeTafzili)
GROUP BY  dbo.tblGroupAshkhas.fldGroupId,dbo.tblTafzili.fldCodeGroup,dbo.tblGroupAshkhas.fldGroupName,dbo.tblDaftarcheTell.fldShomare,dbo.tblTafzili.fldCodeTafzili, 
                         dbo.tblTafzili.fldSharheTafzili,dbo.tblTafzili.fldCodeMeli,dbo.tblTafzili.fldAddress


update #TempListAshkhas set fldShomare=tbl5.fldShomare from
(select fldShomare,fldcodetafzili from
	(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
	FROM            [tblDaftarcheTell]) DB_Address
	where RowNo=1) as tbl5 
	where #TempListAshkhas.fldCodetafzili=tbl5.fldCodeTafzili

Select  fldSharheTafzili ,fldCodeGroup ,fldGroupName ,fldCodeTafzili ,fldShomare, fldCodeMeli ,fldAddress  from #TempListAshkhas ORDER BY  fldSharheTafzili

GO
/****** Object:  StoredProcedure [dbo].[ReportAll_ListKala_groupby_NameGroup_order_CodeKala_final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ReportAll_ListKala_groupby_NameGroup_order_CodeKala_final]

as

begin
 select   DISTINCT dbo.tblKala.fldNameKala, dbo.tblKala.fldCodeKala,  dbo.tblKala.fldCodeVahed1, dbo.tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed, 
                         dbo.tblVahed.fldNameVahed AS FldNameVahed2, dbo.tblKala.fldCodeGroup, dbo.tblGroupKala.fldGroupName, dbo.tblKala.fldFeeForoosh, 
                         dbo.tblKala.fldFeeBadAzTakhfif, dbo.tblKala.fldNogheSefaresh, dbo.tblKala.fldNerkhePorsantKol, dbo.tblKala.fldNerkhePorsantJoz,dbo.tblKala.fldGhymatMasrafkonande,fldSerial
FROM            dbo.tblKala LEFT OUTER JOIN
                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId LEFT OUTER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed1 = tblVahed_1.ID LEFT OUTER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
GROUP BY        dbo.tblGroupKala.fldGroupName,dbo.tblKala.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblKala.fldCodeVahed1, dbo.tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed, 
                         dbo.tblKala.fldFeeForoosh, dbo.tblKala.fldFeeBadAzTakhfif, dbo.tblKala.fldNogheSefaresh, dbo.tblKala.fldNerkhePorsantKol, 
                         dbo.tblKala.fldNerkhePorsantJoz, dbo.tblKala.fldCodeGroup, dbo.tblVahed.fldNameVahed,dbo.tblKala.fldGhymatMasrafkonande,fldSerial
						 
ORDER BY dbo.tblKala.fldCodeKala

end

GO
/****** Object:  StoredProcedure [dbo].[ReportAll_ListKala_groupby_NameGroup_order_NameKala_final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ReportAll_ListKala_groupby_NameGroup_order_NameKala_final]

as

begin
 select   DISTINCT dbo.tblKala.fldNameKala, dbo.tblKala.fldCodeKala,  dbo.tblKala.fldCodeVahed1, dbo.tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed, 
                         dbo.tblVahed.fldNameVahed AS FldNameVahed2, dbo.tblKala.fldCodeGroup, dbo.tblGroupKala.fldGroupName, dbo.tblKala.fldFeeForoosh, 
                         dbo.tblKala.fldFeeBadAzTakhfif, dbo.tblKala.fldNogheSefaresh, dbo.tblKala.fldNerkhePorsantKol, dbo.tblKala.fldNerkhePorsantJoz,dbo.tblKala.fldGhymatMasrafkonande,fldSerial
FROM            dbo.tblKala LEFT OUTER JOIN
                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId LEFT OUTER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed1 = tblVahed_1.ID LEFT OUTER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
GROUP BY        dbo.tblGroupKala.fldGroupName,dbo.tblKala.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblKala.fldCodeVahed1, dbo.tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed, 
                         dbo.tblKala.fldFeeForoosh, dbo.tblKala.fldFeeBadAzTakhfif, dbo.tblKala.fldNogheSefaresh, dbo.tblKala.fldNerkhePorsantKol, 
                         dbo.tblKala.fldNerkhePorsantJoz, dbo.tblKala.fldCodeGroup, dbo.tblVahed.fldNameVahed,dbo.tblKala.fldGhymatMasrafkonande,fldSerial
						 
ORDER BY dbo.tblKala.fldNameKala

end

GO
/****** Object:  StoredProcedure [dbo].[ResetShomarefish]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ResetShomarefish] 
	-- Add the parameters for the stored procedure here
	@ShomareFish bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    Delete from tblFish
	insert into tblFish	
		(fldShomareFish,fldShomareFactor)
		values
		(@ShomareFish,0)

END

GO
/****** Object:  StoredProcedure [dbo].[RizeFactorTasfieNashode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[RizeFactorTasfieNashode]
--چندزبانه
	@ShomareFactor bigint

AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	
	CREATE TABLE #tmpRizeTasfie(	
	[Sharh][nvarchar](100) COLLATE Persian_100_CI_AS,
	[ShomareDaryaft][bigint],	
	[fldMablagheMandeFactor]	decimal (18, 4),
	[fldMablagheDaryaft]decimal (18, 4),
	[fldMande]decimal (18, 4)	,
	[fldDate] [bigint],
	[fldShomareBargashtAzForoosh] [bigint],
	[id] bigint
	)

	declare @Mablagh decimal (18, 4)
	
	set @Mablagh=(Select fldMablagheMandeFactor  from tblTasfie Where fldShomareFactor=@ShomareFactor)
	if @Mablagh is not null
		begin
			declare @DateFactor bigint
			set @DateFactor=(Select top(1) flddate from tblRizeForoosh Where fldShomareFactor=@ShomareFactor)
			
			insert into #tmpRizeTasfie
				(Sharh,ShomareDaryaft, fldMablagheMandeFactor,fldMablagheDaryaft,fldMande,fldDate,id)
			values('' , 0,@Mablagh ,0,@Mablagh,right(@DateFactor,6),1)

			insert into #tmpRizeTasfie
				(Sharh,ShomareDaryaft, fldMablagheMandeFactor,fldMablagheDaryaft,fldMande,fldDate,fldShomareBargashtAzForoosh,id)
				(Select '',fldShomareDaryaft,0,fldMablagh,0,0,fldShomareBargashtAzForoosh,id from tblRizeTasfie Where fldShomareFactor=@ShomareFactor)
				
				
			---نمایش تاریخ دریافت
			update #tmpRizeTasfie set fldDate= tbl2.flddate from (SELECT DISTINCT tbl1.fldShomareSanad, tbl1.fldShomareFactor, tbl1.fldShomareDaryaft, tbl1.fldMablagh, tbl1.fldShomareBargashtAzForoosh, tblSodooreSanad.fldDate
				FROM         tblSodooreSanad INNER JOIN
                          (SELECT     tblRizeTasfie.ID, tblRizeTasfie.fldShomareFactor, tblRizeTasfie.fldShomareDaryaft, tblRizeTasfie.fldMablagh, 
                                                   tblRizeTasfie.fldShomareBargashtAzForoosh, tblDaryaft.fldShomareSanad
                             FROM         tblRizeTasfie INNER JOIN
                                                   tblDaryaft ON tblRizeTasfie.fldShomareDaryaft = tblDaryaft.fldShomareDaryaft) AS tbl1 ON tblSodooreSanad.fldSanad = tbl1.fldShomareSanad) as tbl2
				where #tmpRizeTasfie.ShomareDaryaft=tbl2.fldShomareDaryaft                                        
			 
		
		
		--update #tmpRizeTasfie set fldDate= tbl2.flddate from (SELECT DISTINCT tbl1.fldShomareSanad, tbl1.fldShomareFactor, tbl1.fldShomareDaryaft, tbl1.fldMablagh, tbl1.fldShomareBargashtAzForoosh, tblSodooreSanad.fldDate
		--		FROM         tblSodooreSanad INNER JOIN
  --                        (SELECT     tblRizeTasfie.ID, tblRizeTasfie.fldShomareFactor, tblRizeTasfie.fldShomareDaryaft, tblRizeTasfie.fldMablagh, 
  --                                                 tblRizeTasfie.fldShomareBargashtAzForoosh, tblDaryaft.fldShomareSanad
  --                           FROM         tblRizeTasfie INNER JOIN
  --                                                 tblDaryaft ON tblRizeTasfie.fldShomareDaryaft = tblDaryaft.fldShomareDaryaft) AS tbl1 ON tblSodooreSanad.fldSanad = tbl1.fldShomareSanad) as tbl2
		--		where #tmpRizeTasfie.ShomareDaryaft=tbl2.fldShomareDaryaft       
		--نمایش تاریخ برای برگشت از فروش	 
		update #tmpRizeTasfie set fldDate= tbl3.flddate from (SELECT DISTINCT tbl1.fldShomareSanad, tbl1.fldShomareFactor, tbl1.fldShomareDaryaft, tbl1.fldMablagh, tbl1.fldShomareBargashtAzForoosh, tblSodooreSanad.fldDate
			FROM         tblSodooreSanad INNER JOIN
                          (SELECT     tblRizeTasfie.ID, tblRizeTasfie.fldShomareFactor, tblRizeTasfie.fldShomareDaryaft, tblRizeTasfie.fldMablagh, 
                                                   tblRizeTasfie.fldShomareBargashtAzForoosh, tblBargashtAzForoosh.fldShomareSanad
                             FROM         tblRizeTasfie INNER JOIN
                                                   tblBargashtAzForoosh ON tblRizeTasfie.fldShomareBargashtAzForoosh = tblBargashtAzForoosh.fldShomareFactorForoosh) AS tbl1 ON 
                      tblSodooreSanad.fldSanad = tbl1.fldShomareSanad) as tbl3
				where #tmpRizeTasfie.fldShomareBargashtAzForoosh=tbl3.fldShomareBargashtAzForoosh      
		
		end
	Select Sharh,ShomareDaryaft,fldMablagheMandeFactor,fldMablagheDaryaft,fldMande,fldDate,fldShomareBargashtAzForoosh from #tmpRizeTasfie order by fldDate,id

END

GO
/****** Object:  StoredProcedure [dbo].[Serach_Filter_Tbl_Tafsili_in_FormAshkhas_ByCodeTafzili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Serach_Filter_Tbl_Tafsili_in_FormAshkhas_ByCodeTafzili]
@codeTafzili nvarchar(10)
as
begin
SELECT DISTINCT fldCodeTafzili, fldCodeGroup, fldSharheTafzili, fldAddress,fldBlackList,
                fldCity,fldCodeEghtesadi,fldCodeMeli,fldCodePosti,fldDate1,fldDate2,
                fldPic,fldOstan,fldShomareHesab,fldShomareSabt,
				fldVisitor,fldSharhBlackList,fldSaghfeEtebar 
				
FROM            dbo.tblTafzili

where fldCodeTafzili=@codeTafzili 
end

GO
/****** Object:  StoredProcedure [dbo].[Serach_Filter_Tbl_Tafsili_in_FormAshkhas_ByCodeTafzili_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Serach_Filter_Tbl_Tafsili_in_FormAshkhas_ByCodeTafzili_WithUserCode]
@codeTafzili nvarchar(10),
@userCode bigint
as
begin

declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 

SELECT DISTINCT fldCodeTafzili, fldCodeGroup, fldSharheTafzili, fldAddress,fldBlackList,
                fldCity,fldCodeEghtesadi,fldCodeMeli,fldCodePosti,fldDate1,fldDate2,
                fldPic,fldOstan,fldShomareHesab,fldShomareSabt,
				fldVisitor,fldSharhBlackList,fldSaghfeEtebar 
				
FROM            dbo.tblTafzili

where 
fldCodeTafzili in (select code from @accessCodes) and
fldCodeTafzili=@codeTafzili 
end

GO
/****** Object:  StoredProcedure [dbo].[SerachIntbl_GroupAshkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[SerachIntbl_GroupAshkhas]
@StrSearchTxt nvarchar(50)
as
select fldGroupId,fldGroupName from tblGroupAshkhas
where fldGroupName like '%' + @StrSearchTxt  + '%'

GO
/****** Object:  StoredProcedure [dbo].[SerachIntbl_GroupAshkhas_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[SerachIntbl_GroupAshkhas_WithUserCode]
--چندزبانه
@StrSearchTxt nvarchar(50),
@userCode bigint
as
begin

declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableGroupAshkhasCode(@userCode) 

select fldGroupId,fldGroupName from tblGroupAshkhas
where 
fldGroupId in (select code from @accessCodes) and
fldGroupName like '%' + @StrSearchTxt  + '%'


end

GO
/****** Object:  StoredProcedure [dbo].[SerachIntbl_GroupKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[SerachIntbl_GroupKala]
@StrSearchTxt nvarchar(50)
as
select fldGroupId,fldGroupName  from tblGroupKala
where fldGroupName like '%' + @StrSearchTxt  + '%'

GO
/****** Object:  StoredProcedure [dbo].[ShomareSanadDaryaftArchive]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShomareSanadDaryaftArchive]
@ShomareDaryaft bigint,
@ShomareSanad nvarchar(50) output

as

begin

	Set @ShomareSanad=(Select fldShomareSanad from tblDaryaft Where fldShomareDaryaft=@ShomareDaryaft)
end

GO
/****** Object:  StoredProcedure [dbo].[ShomareSanadPardakhtArchive]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[ShomareSanadPardakhtArchive]
@ShomarePardakht bigint,
@ShomareSanad nvarchar(50) output

as

begin

	Set @ShomareSanad=(Select fldShomareSanad from tblPardakht Where fldShomarePardakht=@ShomarePardakht)
end

GO
/****** Object:  StoredProcedure [dbo].[ShomareSanadVamArchive]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[ShomareSanadVamArchive]
@ShomareVam bigint,
@ShomareSanad nvarchar(50) output

as

begin

	Set @ShomareSanad=(Select fldShomareSanad from tblVamInfo Where fldShomareVam=@ShomareVam)
end

GO
/****** Object:  StoredProcedure [dbo].[ShomareSanadVosoolArchive]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShomareSanadVosoolArchive]
@ShomareVosool bigint,
@ShomareSanadVosool nvarchar(50) output

as

begin
               SET NOCOUNT ON;
	Set @ShomareSanadVosool=(Select fldShomareSanad from tblVosool Where fldShomareVosool=@ShomareVosool)
end

GO
/****** Object:  StoredProcedure [dbo].[Show_Mojoodie_Anbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Show_Mojoodie_Anbar]

@mm nvarchar(10),
@nn nvarchar(10)

as

begin
 
 IF OBJECT_ID('tempdb..##TempMojoodieAnbar') IS NOT NULL
 begin
	delete from  #TempMojoodieAnbar
 end
else 
begin
create table #TempMojoodieAnbar(

fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 0),
fldTedadeKhorooj decimal(18, 0),
fldTedadeJoz decimal(18, 0), 
fldTedadeKol decimal(18, 0), 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 0),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS
)
end


insert into #TempMojoodieAnbar (
fldCodeGroup,fldCodeKala,fldNameKala,fldTedadeVorood,fldTedadeKhorooj,
fldTedadeJoz,fldTedadeKol,fldNameAnbar,fldNogheSefaresh,fldFeeForoosh,
fldLastFee,fldMianginFee,fldNameVahed,fldNameVahed2 )
values (1,2,'3',4,5,6,7,'8',9,10,11,12,'13','14')


Select * from #TempMojoodieAnbar
end

GO
/****** Object:  StoredProcedure [dbo].[ShowAllAshkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowAllAshkhas]

as

	SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											  where fldCodeNoe='1' 
GO
/****** Object:  StoredProcedure [dbo].[ShowAllAshkhas_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowAllAshkhas_WithUserCode]
--چندزبانه
@userCode bigint

as
begin

declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 


	SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
    where fldCodeTafzili in (select code from @accessCodes) and  fldCodeNoe='1' 

end

GO
/****** Object:  StoredProcedure [dbo].[ShowAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[ShowAnbar]

as
begin
	select [fldCode],[fldNameAnbar],[ID] from tblAnbar
		where fldCode<2000
		order by id
end

GO
/****** Object:  StoredProcedure [dbo].[ShowArchiveCheckVagozari]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowArchiveCheckVagozari]
--چندزبانه
@ShomarePardakht bigint
as
begin
		--SELECT        tblCheckeDaryafti.ID, tblCheckeDaryafti.fldShomareCheck, tblCheckeDaryafti.fldShomareHesab,SUBSTRING(CONVERT(nvarchar, 
  --                       tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, 
  --                       tblCheckeDaryafti.fldDateSarResid), 7, 2) as fldDateSarResid, tblCheckeDaryafti.fldCodeBank, 
		--							tblCheckeDaryafti.fldShobe, tblCheckeDaryafti.fldMablagh,tblBankeMoshtari.fldNameBank
		--FROM            tblCheckeDaryafti INNER JOIN
		--							tblBankeMoshtari ON tblCheckeDaryafti.fldCodeBank = tblBankeMoshtari.ID
		--WHERE        (isnumeric(fldShomarePardakht) = 0) AND (isnumeric(fldShomareVosool) = 0)


		SELECT        tbl1.ID, tbl1.fldCodeSahebeCheck, tblTafzili.fldSharheTafzili, tbl1.fldShomareCheck, tbl1.fldShomareHesab, tbl1.fldDateSarResid, tbl1.fldCodeBank, tbl1.fldShobe, 
                         tbl1.fldMablagh, tbl1.fldNameBank
			FROM            (SELECT        tblCheckeDaryafti.ID, tblCheckeDaryafti.fldCodeSahebeCheck, tblCheckeDaryafti.fldShomareCheck, tblCheckeDaryafti.fldShomareHesab, 
																SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 
																5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid, tblCheckeDaryafti.fldCodeBank, 
																tblCheckeDaryafti.fldShobe, tblCheckeDaryafti.fldMablagh, tblBankeMoshtari.fldNameBank
										FROM            tblCheckeDaryafti INNER JOIN
																tblBankeMoshtari ON tblCheckeDaryafti.fldCodeBank = tblBankeMoshtari.ID
										WHERE    fldShomarePardakht=@ShomarePardakht)     AS tbl1 INNER JOIN
										tblTafzili ON tbl1.fldCodeSahebeCheck = tblTafzili.fldCodeTafzili
	return
end

GO
/****** Object:  StoredProcedure [dbo].[ShowAshkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowAshkhas]
@NameShakhs nvarchar(200),
@CodeGroup  bigint
as

	if @NameShakhs<>'' 
		if @codegroup=0 
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where fldSharheTafzili like '%'+@NameShakhs+'%' and  fldCodeNoe='1' 
			end
		else
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where fldSharheTafzili like '%'+@NameShakhs+'%' and fldCodeGroup=@CodeGroup and  fldCodeNoe='1' 
			end
	else
		if @codegroup=0 
			begin
					SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId	 and  fldCodeNoe='1' 										 
			end
		else
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where fldCodeGroup=@CodeGroup and  fldCodeNoe='1' 
			end
GO
/****** Object:  StoredProcedure [dbo].[ShowAshkhas_Filter_CodeKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[ShowAshkhas_Filter_CodeKala]
@CodeShakhs nvarchar(200),
@CodeGroup  bigint
as

	if @CodeShakhs<>'' 
		if @codegroup=0 
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where fldCodeTafzili like '%'+@CodeShakhs+'%'
			end
		else
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where fldCodeTafzili like '%'+@CodeShakhs+'%' and fldCodeGroup=@CodeGroup
			end
	else
		if @codegroup=0 
			begin
					SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId											 
			end
		else
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where fldCodeGroup=@CodeGroup
			end

GO
/****** Object:  StoredProcedure [dbo].[ShowAshkhas_Filter_CodeKala_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[ShowAshkhas_Filter_CodeKala_WithUserCode]
@CodeShakhs nvarchar(200),
@CodeGroup  bigint,
@userCode bigint
as
begin

declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 

	if @CodeShakhs<>'' 
		if @codegroup=0 
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where 
											 fldCodeTafzili in (select code from @accessCodes) and
											 fldCodeTafzili like '%'+@CodeShakhs+'%'
			end
		else
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where 
											 fldCodeTafzili in (select code from @accessCodes) and
											 fldCodeTafzili like '%'+@CodeShakhs+'%' and fldCodeGroup=@CodeGroup
			end
	else
		if @codegroup=0 
			begin
					SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId											 
											 where  fldCodeTafzili in (select code from @accessCodes)
			end
		else
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where 
											 fldCodeTafzili in (select code from @accessCodes) and
											 fldCodeGroup=@CodeGroup
			end

end

GO
/****** Object:  StoredProcedure [dbo].[ShowAshkhas-WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowAshkhas-WithUserCode]
@NameShakhs nvarchar(200),
@CodeGroup  bigint,
@userCode bigint
as
begin

declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 

	if @NameShakhs<>'' 
		if @codegroup=0 
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where 
											 fldCodeTafzili in (select code from @accessCodes) and
											 fldSharheTafzili like '%'+@NameShakhs+'%' and  fldCodeNoe='1' 
			end
		else
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where 
											 fldCodeTafzili in (select code from @accessCodes) and
											 fldSharheTafzili like '%'+@NameShakhs+'%' and fldCodeGroup=@CodeGroup and  fldCodeNoe='1' 
			end
	else
		if @codegroup=0 
			begin
					SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId											 
											 where fldCodeTafzili in (select code from @accessCodes)  and  fldCodeNoe='1' 
			end
		else
			begin
				SELECT        tblTafzili.fldCodeTafzili, tblGroupAshkhas.fldGroupName, tblTafzili.fldCodeGroup, tblTafzili.fldSharheTafzili, tblTafzili.fldAddress
					FROM            tblTafzili INNER JOIN
											 tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
											 where 
											 fldCodeTafzili in (select code from @accessCodes) and
											 fldCodeGroup=@CodeGroup and  fldCodeNoe='1' 
			end


end

GO
/****** Object:  StoredProcedure [dbo].[ShowBankeMa]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[ShowBankeMa]

as
begin
		declare @strCodeKoleHesab nvarchar(10)
		execute CodeKolHesab 'bank',@strcodekolehesab output

	select [fldSharheHesab] as fldNameBank,[fldCodeHesab] from tblKolMoin
		where  fldCodeHesab like @strCodeKoleHesab + '%' and len(fldCodeHesab)>len(@strCodeKoleHesab)
		order by fldCodeHesab
end

GO
/****** Object:  StoredProcedure [dbo].[ShowBankeMoshtari]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
Create proc [dbo].[ShowBankeMoshtari]

as
begin
	select [fldNameBank],[ID] from tblBankeMoshtari
		order by id
end

GO
/****** Object:  StoredProcedure [dbo].[ShowBankInFormKhabandanCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowBankInFormKhabandanCheck]
--چندزبانه
AS
BEGIN
	
	SET NOCOUNT ON;

declare @codebank nvarchar(100) 
declare @codesandoog nvarchar(50)

execute CodeKolHesab 'bank', @codebank output


SELECT         fldSharheHesab,fldCodeHesab
FROM            tblKolMoin
WHERE        (fldCodeHesab like @codebank+'%' ) And (LEN(fldCodeHesab) > LEN(@codebank))
order by fldSharheHesab

  
END
GO
/****** Object:  StoredProcedure [dbo].[ShowCheckDaryafti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[ShowCheckDaryafti]
--چندزبانه
@ShomareSanad nvarchar(50)
as
begin
	--SELECT        ID.tblCheckeDaryafti,ID.tblBankeMoshtari, fldShomareCheck, fldShomareHesab, fldDateSarResid, fldCodeBank,fldNameBank, fldShobe, fldMablagh   
                           
	--					FROM            tblCheckeDaryafti,tblBankeMoshtari
	--					WHERE        (fldSanad = @ShomareSanad) and ID.tblBankeMoshtari=fldCodeBank



		SELECT        tblCheckeDaryafti.ID, tblCheckeDaryafti.fldShomareCheck, tblCheckeDaryafti.fldShomareHesab,SUBSTRING(CONVERT(nvarchar, 
                         tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, 
                         tblCheckeDaryafti.fldDateSarResid), 7, 2) as fldDateSarResid, tblCheckeDaryafti.fldCodeBank, 
									tblCheckeDaryafti.fldShobe, tblCheckeDaryafti.fldMablagh,tblBankeMoshtari.fldNameBank,fldSharheDaryaft
		FROM            tblCheckeDaryafti INNER JOIN
									tblBankeMoshtari ON tblCheckeDaryafti.fldCodeBank = tblBankeMoshtari.ID
		WHERE        (tblCheckeDaryafti.fldSanad = @ShomareSanad)
	return
end

GO
/****** Object:  StoredProcedure [dbo].[ShowCheckDaryaftiAndVagozariInFormVosoolCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowCheckDaryaftiAndVagozariInFormVosoolCheck]
--چندزبانه
AS
BEGIN
	
SET NOCOUNT ON;


SELECT        dbo.tblCheckeDaryafti.ID,dbo.tblCheckeDaryafti.fldShomareCheck,dbo.tblCheckeDaryafti.fldShomareHesab,dbo.tblCheckeDaryafti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,
                        SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid,
						 dbo.tblCheckeDaryafti.fldCodeBank,dbo.tblBankeMoshtari.fldNameBank, dbo.tblCheckeDaryafti.fldShobe, 
                         dbo.tblCheckeDaryafti.fldMablagh, SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),1,4)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),5,2)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),7,2) As fldDateDaryaft ,dbo.tblCheckeDaryafti.fldShomareDaryaft,dbo.tblCheckeDaryafti.fldVaziat, dbo.tblCheckeDaryafti.fldShomarePardakht
                         
FROM            dbo.tblCheckeDaryafti INNER JOIN
                         dbo.tblTafzili ON dbo.tblCheckeDaryafti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
                         dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID
WHERE        (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat IS NULL) OR
                         (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat = 'vagozari' or dbo.tblCheckeDaryafti.fldVaziat ='Bargasht') 
			order by tblCheckeDaryafti.fldDateSarResid,tblCheckeDaryafti.fldDateDaryaft
END

GO
/****** Object:  StoredProcedure [dbo].[ShowCheckDaryaftiAndVagozarNashodeAndVosoolNashodeINfrmKhabandan]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowCheckDaryaftiAndVagozarNashodeAndVosoolNashodeINfrmKhabandan]
--چندزبانه
AS
BEGIN
	
SET NOCOUNT ON;


SELECT        dbo.tblCheckeDaryafti.ID,dbo.tblCheckeDaryafti.fldShomareCheck,dbo.tblCheckeDaryafti.fldShomareHesab,dbo.tblCheckeDaryafti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,
                        SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid,
						 dbo.tblCheckeDaryafti.fldCodeBank,dbo.tblBankeMoshtari.fldNameBank, dbo.tblCheckeDaryafti.fldShobe, 
                         dbo.tblCheckeDaryafti.fldMablagh, SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),1,4)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),5,2)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),7,2) As fldDateDaryaft ,dbo.tblCheckeDaryafti.fldShomareDaryaft,dbo.tblCheckeDaryafti.fldVaziat, dbo.tblCheckeDaryafti.fldShomarePardakht
                         
FROM            dbo.tblCheckeDaryafti INNER JOIN
                         dbo.tblTafzili ON dbo.tblCheckeDaryafti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
                         dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID
WHERE        (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat IS NULL) OR
                         (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat is NULL)
						 order by tblCheckeDaryafti.fldDateSarResid
END
GO
/****** Object:  StoredProcedure [dbo].[ShowCheckDaryaftiVosoolVaVagozarNashode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[ShowCheckDaryaftiVosoolVaVagozarNashode]
--چندزبانه
@ShomareCheck nvarchar(50)
as
begin
		--SELECT        tblCheckeDaryafti.ID, tblCheckeDaryafti.fldShomareCheck, tblCheckeDaryafti.fldShomareHesab,SUBSTRING(CONVERT(nvarchar, 
  --                       tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, 
  --                       tblCheckeDaryafti.fldDateSarResid), 7, 2) as fldDateSarResid, tblCheckeDaryafti.fldCodeBank, 
		--							tblCheckeDaryafti.fldShobe, tblCheckeDaryafti.fldMablagh,tblBankeMoshtari.fldNameBank
		--FROM            tblCheckeDaryafti INNER JOIN
		--							tblBankeMoshtari ON tblCheckeDaryafti.fldCodeBank = tblBankeMoshtari.ID
		--WHERE        (isnumeric(fldShomarePardakht) = 0) AND (isnumeric(fldShomareVosool) = 0)


		SELECT        tbl1.ID, tbl1.fldCodeSahebeCheck, tblTafzili.fldSharheTafzili, tbl1.fldShomareCheck, tbl1.fldShomareHesab, tbl1.fldDateSarResid, tbl1.fldCodeBank, tbl1.fldShobe, 
                         tbl1.fldMablagh, tbl1.fldNameBank
			FROM            (SELECT        tblCheckeDaryafti.ID, tblCheckeDaryafti.fldCodeSahebeCheck, tblCheckeDaryafti.fldShomareCheck, tblCheckeDaryafti.fldShomareHesab, 
																SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 
																5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid, tblCheckeDaryafti.fldCodeBank, 
																tblCheckeDaryafti.fldShobe, tblCheckeDaryafti.fldMablagh, tblBankeMoshtari.fldNameBank
										FROM            tblCheckeDaryafti INNER JOIN
																tblBankeMoshtari ON tblCheckeDaryafti.fldCodeBank = tblBankeMoshtari.ID
										WHERE   fldvaziat is null  and     (isnumeric(tblCheckeDaryafti.fldShomarePardakht) = 0) AND (isnumeric(tblCheckeDaryafti.fldShomareVosool) = 0 and Convert(nvarchar,fldShomareCheck) like '%' + @ShomareCheck + '%')) AS tbl1 INNER JOIN
										tblTafzili ON tbl1.fldCodeSahebeCheck = tblTafzili.fldCodeTafzili
	return
end

GO
/****** Object:  StoredProcedure [dbo].[ShowCheckPardakhti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[ShowCheckPardakhti]
--چندزبانه
@ShomareSanad nvarchar(50)
as
begin
	--SELECT        ID.tblCheckePardakht,ID.tblBankeMoshtari, fldShomareCheck, fldShomareHesab, fldDateSarResid, fldCodeBank,fldNameBank, fldShobe, fldMablagh   
                           
	--					FROM            tblCheckePardakht,tblBankeMoshtari
	--					WHERE        (fldSanad = @ShomareSanad) and ID.tblBankeMoshtari=fldCodeBank


		declare @strCodeMoinHesab nvarchar(10)
		execute CodeMoinHesab 'bank',@strCodeMoinHesab  output
		--SELECT        tblCheckePardakht.ID, tblCheckePardakht.fldShomareCheck, tblCheckePardakht.fldShomareHesab,SUBSTRING(CONVERT(nvarchar, 
  --                       tblCheckePardakht.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckePardakht.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, 
  --                       tblCheckePardakht.fldDateSarResid), 7, 2) as fldDateSarResid, tblCheckePardakht.fldCodeBank, 
		--							tblCheckePardakht.fldShobe, tblCheckePardakht.fldMablagh,tblBankeMoshtari.fldNameBank
		--FROM            tblCheckePardakht INNER JOIN
		--							tblBankeMoshtari ON tblCheckePardakht.fldCodeBank = tblBankeMoshtari.ID
		--WHERE        (tblCheckePardakht.fldSanad = @ShomareSanad)

		--SELECT        tblCheckePardakhti.ID, tblCheckePardakhti.fldShomareCheck, tblCheckePardakhti.fldShomareHesab, SUBSTRING(CONVERT(nvarchar, 
  --                       tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar,
  --                        tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResid, tblCheckePardakhti.fldCodeBank, tblCheckePardakhti.fldShobe, tblCheckePardakhti.fldMablagh, 
  --                       tblKolMoin.fldSharheHesab as fldNameBank
		--			FROM            tblCheckePardakhti INNER JOIN
  --                       tblKolMoin ON CONVERT(nvarchar, tblCheckePardakhti.fldCodeBank) = @strCodeKoleHesab
		--			WHERE        (tblCheckePardakhti.fldSanad = @ShomareSanad)


		

--Select * from tblCheckePardakhti,tblKolMoin
--    Where 
--    CONVERT(nvarchar, tblCheckePardakhti.fldCodeBank) = '11701'
--and  (tblCheckePardakhti.fldSanad = '1')
--and tblCheckePardakhti.fldCodeBank=tblKolMoin.fldCodeHesab

SELECT        tblCheckePardakhti.ID, tblCheckePardakhti.fldShomareCheck, tblCheckePardakhti.fldShomareHesab,SUBSTRING(CONVERT(nvarchar, 
                         tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, 
                         tblCheckePardakhti.fldDateSarResid), 7, 2) as fldDateSarResid,  
                         tblCheckePardakhti.fldCodeBank, tblCheckePardakhti.fldShobe, tblCheckePardakhti.fldMablagh, tblCheckePardakhti.fldDatePardakht, 
                         tblCheckePardakhti.fldCodeSahebeCheck, tblCheckePardakhti.fldShomarePardakht, tblCheckePardakhti.fldCodeSahebeHesab, tblCheckePardakhti.fldVasete, 
                         tblCheckePardakhti.fldSanad, tblKolMoin.fldSharheHesab as fldNameBank
FROM            tblCheckePardakhti INNER JOIN
                         tblKolMoin ON tblCheckePardakhti.fldCodeBank = tblKolMoin.fldCodeHesab
WHERE        (tblCheckePardakhti.fldSanad = @ShomareSanad)





	return
end

GO
/****** Object:  StoredProcedure [dbo].[ShowCode_Daryaft_Daramad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowCode_Daryaft_Daramad]
@Sanad nvarchar(50),
@CodeHesab nvarchar(50) output
as

begin
	set @CodeHesab=( SELECT        fldMoinKamel
		FROM            tblSodooreSanad
		WHERE        (fldSanad = @Sanad) AND (isnumeric(fldBestankar) = 1) AND (fldBestankar > 0))
end

GO
/****** Object:  StoredProcedure [dbo].[ShowDaryaftInfo]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowDaryaftInfo]
--چندزبانه
@ShomareDaryaft bigint,

@ShomareSanad nvarchar(50) output,
@DateDaryaft int output,
@CodeTafzili nvarchar(10) output,
@SharheKol nvarchar(200) output,
@TypeDaryaft nvarchar(20),

----صندوق
@Naghd decimal(18,4) output,
@CodeMoinSandoogh nvarchar(10) output,
@SNaghd nvarchar(200) output,


----بانک
@Bank decimal(18,4) output,
@CodeMoinBank nvarchar(10) output,

@SBank nvarchar(200) output,

----چک
@MablaghCheck decimal(18,4) output,
@SCheck nvarchar(200) output,

----تخفیفات
@Takhfif decimal(18,4) output,
@STakhfif nvarchar(200) output,

----واسطه
@CodeVasete nvarchar(10) output,

@NoeKhata nvarchar(30) output 

as

begin
	begin transaction transact
		begin try
		--اطلاعات کلی
		declare @KolSandoogh nvarchar(100),@strMablagh nvarchar(20),@KolBank nvarchar(100),@KolMoinCheck nvarchar(100),@KolMoinTakhfif nvarchar(100),@strType nvarchar(20)
		set @ShomareSanad=(Select fldShomareSanad from tblDaryaft Where fldShomareDaryaft=@ShomareDaryaft)
		set @strtype=(Select fldType from tblDaryaft Where fldShomareDaryaft=@ShomareDaryaft)
		set @DateDaryaft=(Select fldDate from tblSodooreSanad Where fldSanad=@ShomareSanad and fldRadif='1')
		if @strtype='daryaft' or @strtype='bargashtazkharid' or @strtype='foroosh'
			begin
				set @CodeTafzili=(Select fldCodeTafzili from tblDaryaft Where fldShomareDaryaft=@ShomareDaryaft)
			end
		else if @strtype='daramad' 
			begin
				--set @CodeTafzili=(Select TOP (1) fldCodeSahebeCheck from tblCheckedaryafti Where fldShomareDaryaft=@ShomareDaryaft)
				set @CodeTafzili=(Select TOP (1) fldCodeTafzili from tblSodooreSanad Where fldSanad=@shomareSanad and isnumeric(fldCodeTafzili)=1 )
				if @CodeTafzili is null
					set @CodeTafzili=''
			end
		--رکوردهای این بخش بیشتر از کی هست باید کد معین حسابهای دریافتنی اضافه بشه
		declare @fldHesabDaryaftani bigint,@fldHesabPardakhtani bigint
		 
		
		if @strtype='bargashtazkharid'
			begin
				set @fldHesabPardakhtani=(select  fldCodeMoinHesabhayePardakhtani from tblTanzimat)
				set @SharheKol=(Select fldSharh from tblSodooreSanad Where fldSanad=@ShomareSanad And fldMoinKamel=@fldHesabPardakhtani and fldBestankar>0)
				--set @SharheKol=(Select fldSharh from tblSodooreSanad Where fldSanad=302 And fldMoinKamel=31301 and fldBestankar>0)
				--set @SharheKol='55'

			end
		else
			begin
				set @fldHesabDaryaftani=(select  fldCodeMoinHesabhayeDaryaftani from tblTanzimat)
				set @SharheKol=(Select fldSharh from tblSodooreSanad Where fldSanad=@ShomareSanad And fldMoinKamel=@fldHesabDaryaftani and fldBestankar>0)
				--set @SharheKol='66'
			end
		set @CodeVasete=(Select Top(1)isnull(fldVasete,'') from tblSodooreSanad Where fldSanad=@ShomareSanad And fldBestankar>0)
		--set @SharheKol=@TypeDaryaft
		

		--صندوق
		execute CodeKolHesab  'sandoogh', @KolSandoogh output
		set @strMablagh=(Select isnull(fldBedehkar,0) from tblSodooreSanad Where fldCodeKol=@kolSandoogh and fldSanad=@ShomareSanad)	
		set @CodeMoinSandoogh =(Select fldMoinKamel from tblSodooreSanad Where fldCodeKol=@kolSandoogh and fldSanad=@ShomareSanad)	
		set @SNaghd=(Select isnull(fldSharh,'') from tblSodooreSanad Where fldCodeKol=@kolSandoogh and fldSanad=@ShomareSanad)	

		if @strMablagh is null
			begin			
				set @naghd=0
				set @CodeMoinSandoogh =''
				set @SNaghd=''
			end
		else
			begin			
				set @naghd=Convert(decimal(18,4),@strMablagh)
			end

		--بانک
		execute CodeKolHesab  'bank', @KolBank output
		set @strMablagh=(Select isnull(fldBedehkar,0) from tblSodooreSanad Where fldCodeKol=@KolBank and fldSanad=@ShomareSanad)
		set @CodeMoinBank=(Select fldMoinKamel from tblSodooreSanad Where fldCodeKol=@KolBank and fldSanad=@ShomareSanad)		
		set @SBank=(Select isnull(fldSharh,'') from tblSodooreSanad Where fldCodeKol=@KolBank and fldSanad=@ShomareSanad)		
		if @strMablagh is null
			begin			
				set @bank=0
				set @CodeMoinBank=''
				set @SBank=''
			end
		else
			begin			
				set @bank=Convert(decimal(18,4),@strMablagh)
			end
		--چک
		execute CodeMoinHesab  'check', @KolMoincheck output
		set @strMablagh=(Select isnull(fldBedehkar,0) from tblSodooreSanad Where fldMoinKamel=@KolMoincheck and fldSanad=@ShomareSanad)
		--set @CodeMoin=(Select fldMoinKamel from tblSodooreSanad Where fldMoinKamel=@KolMoincheck and fldSanad=@ShomareSanad)		
		set @SCheck=(Select isnull(fldSharh,'') from tblSodooreSanad Where fldMoinKamel=@KolMoincheck and fldSanad=@ShomareSanad)		
		if @strMablagh is null
			begin			
				set @MablaghCheck=0
				--set @CodeMoincheck=''
				set @Scheck=''
			end
		else
			begin			
				set @MablaghCheck=Convert(decimal(18,4),@strMablagh)
			end


		--تخفیفات
		execute CodeMoinHesab  'takhfifatedaryaft', @KolMoinTakhfif output
		set @strMablagh=(Select isnull(fldBedehkar,0) from tblSodooreSanad Where fldMoinKamel=@KolMoinTakhfif and fldSanad=@ShomareSanad)
		--set @CodeMoin=(Select fldMoinKamel from tblSodooreSanad Where fldMoinKamel=@KolMoincheck and fldSanad=@ShomareSanad)		
		set @STakhfif=(Select isnull(fldSharh,'') from tblSodooreSanad Where fldMoinKamel=@KolMoinTakhfif and fldSanad=@ShomareSanad)		
		if @strMablagh is null
			begin			
				set @Takhfif=0
				--set @CodeMoincheck=''
				set @STakhfif=''
			end
		else
			begin			
				set @Takhfif=Convert(decimal(18,4),@strMablagh)
			end

		commit transaction
			end try

		begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='rolback'

		end catch
	--insert into tblTest7 (fld1,fld2)
	--values(@CodeMoinBank,@bank)

	--declare @nn decimal
	--set @nn=(Select fldBedehkar from tblSodooreSanad Where fldMoinkamel='11001' and fldSanad='47')
	--insert into tblTest7 (fld2)
	--	values(@nn)
	--Select fldBedehkar from tblSodooreSanad Where fldCodeKol='110' and fldSanad='48'
end

GO
/****** Object:  StoredProcedure [dbo].[ShowDastrasiSandoogh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ShowDastrasiSandoogh] 
	@CodeUser bigint,
	@CodeSanadoogh bigint output,
	@CodeBank bigint  output,
	@ElzamiSandoogh nvarchar(10) output,
	@Elzamibank nvarchar(10) output
AS
BEGIN
	set @CodeSanadoogh=(Select fldSandoogh from tblDastrasiSandoogh
							where fldCodeUser=@CodeUser and fldType ='sandoogh')
	if @CodeSanadoogh is null
		set @CodeSanadoogh=1

	set @ElzamiSandoogh=(Select fldElzamiSandoogh from tblDastrasiSandoogh
							where fldCodeUser=@CodeUser and fldType ='sandoogh')
	if @ElzamiSandoogh is null
		set @ElzamiSandoogh='false'

	set @CodeBank=(Select fldSandoogh from tblDastrasiSandoogh
							where fldCodeUser=@CodeUser and fldType ='bank')
	if @CodeBank  is null
		set @CodeBank=1

	set @Elzamibank=(Select fldElzamiBank from tblDastrasiSandoogh
							where fldCodeUser=@CodeUser and fldType ='bank')
	if @Elzamibank is null
		set @Elzamibank='false'

END

GO
/****** Object:  StoredProcedure [dbo].[ShowHesabKolMoin]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
Create proc [dbo].[ShowHesabKolMoin]
@CodeKolHesab nvarChar(8)
as
begin
	select [fldCodeHesab],[fldSharheHesab],[ID] from tblKolMoin
		Where fldCodeHesab<>@CodeKolHesab and left(fldCodeHesab,len(@CodeKolHesab))=@CodeKolHesab
		order by fldCodeHesab
end

GO
/****** Object:  StoredProcedure [dbo].[ShowHesabKolMoin_Filter]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[ShowHesabKolMoin_Filter]
@CodeKolHesab nvarChar(8),
@Sharh nvarchar(50)
as
begin
	if isnumeric(@Sharh)=0 
		begin
			select [fldCodeHesab],[fldSharheHesab],[ID] from tblKolMoin
				Where fldCodeHesab<>@CodeKolHesab and left(fldCodeHesab,len(@CodeKolHesab))=@CodeKolHesab and fldSharheHesab like '%' + @Sharh + '%'
				order by fldCodeHesab
		end
	else if isnumeric(@Sharh)=1 
		begin
			select [fldCodeHesab],[fldSharheHesab],[ID] from tblKolMoin
				Where fldCodeHesab<>@CodeKolHesab and left(fldCodeHesab,len(@CodeKolHesab))=@CodeKolHesab and fldCodeHesab like  @Sharh + '%'
				order by fldCodeHesab
		end
end

GO
/****** Object:  StoredProcedure [dbo].[ShowHesabKolMoinNoeTafzili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[ShowHesabKolMoinNoeTafzili]
@CodeKolHesab nvarChar(8)
as
begin
	select [fldCodeHesab],[fldSharheHesab],[ID],[fldCodeNoeHesab] from tblKolMoin
		Where fldCodeHesab<>@CodeKolHesab and left(fldCodeHesab,len(@CodeKolHesab))=@CodeKolHesab --and fldCodeNoeHesab<>'2'
		order by fldCodeHesab
end

GO
/****** Object:  StoredProcedure [dbo].[ShowHesabKolMoinSandooghBank]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
Create proc [dbo].[ShowHesabKolMoinSandooghBank]
--@CodeKolBank nvarChar(8)
as
begin
	declare @strCodeBank nvarchar(10),@strCodeSandoogh nvarchar(10)
    execute  CodeKolHesab 'sandoogh', @strCodeSandoogh output
	execute  CodeKolHesab 'bank', @strCodeBank output
	select [fldCodeHesab],[fldSharheHesab],[ID],[fldCodeNoeHesab] from tblKolMoin
		Where fldCodeHesab<>@strCodeSandoogh and fldCodeHesab<>@strCodeBank  and (left(fldCodeHesab,len(@strCodeSandoogh))=@strCodeSandoogh or left(fldCodeHesab,len(@strCodeBank))=@strCodeBank)
		order by fldCodeHesab
end

GO
/****** Object:  StoredProcedure [dbo].[ShowHesabKolMoinWithType]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
Create proc [dbo].[ShowHesabKolMoinWithType]
@CodeKolHesab nvarChar(8)
as
begin
	select [fldCodeHesab],[fldSharheHesab],[ID],fldCodeNoeHesab from tblKolMoin
		Where fldCodeHesab<>@CodeKolHesab and left(fldCodeHesab,len(@CodeKolHesab))=@CodeKolHesab
		order by fldCodeHesab
end

GO
/****** Object:  StoredProcedure [dbo].[ShowInfo]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowInfo]
--چندزبانه
@TblField nvarchar(50),
@Vorood nvarchar(100),
@Khorooj nvarchar(100) output
as
begin
	if @TblField='namevizitor'
		begin
			set @khorooj=(Select fldGroupName from tblGroupAshkhas Where fldGroupId=Convert(bigint,@Vorood))
		end

	if @TblField='codevizitoretafzili'
		begin
			set @Khorooj=(Select Convert(bigint,fldVisitor) from tblTafzili Where fldCodeTafzili=@Vorood) 
		end

end

GO
/****** Object:  StoredProcedure [dbo].[ShowInfoChecksKhabandehShodeInFormVagozri]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ShowInfoChecksKhabandehShodeInFormVagozri]
--چندزبانه
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

  SELECT        dbo.tblCheckeDaryafti.fldShomareCheck, dbo.tblCheckeDaryafti.fldShomareHesab, dbo.tblCheckeDaryafti.fldCodeSahebeCheck, 
                         dbo.tblTafzili.fldSharheTafzili, SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, 
                         dbo.tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid, 
                         dbo.tblCheckeDaryafti.fldCodeBank, dbo.tblBankeMoshtari.fldNameBank, dbo.tblCheckeDaryafti.fldShobe, dbo.tblCheckeDaryafti.fldMablagh, 
                         SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft), 5, 2) 
                         + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft), 7, 2) AS fldDateDaryaft, dbo.tblCheckeDaryafti.fldShomareDaryaft, 
                          SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateVagozariBeBank), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, 
                         dbo.tblCheckeDaryafti.fldDateVagozariBeBank), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateVagozariBeBank), 7, 2) 
                         AS fldDateVagozariBeBank, dbo.tblCheckeDaryafti.fldCodeBankVagozari, 
                         dbo.tblKolMoin.fldSharheHesab,dbo.tblCheckeDaryafti.fldTozihateVagozariBeBank,dbo.tblCheckeDaryafti.fldVaziat, dbo.tblCheckeDaryafti.ID,dbo.tblCheckeDaryafti.fldShomarePardakht
FROM            dbo.tblCheckeDaryafti INNER JOIN
                         dbo.tblTafzili ON dbo.tblCheckeDaryafti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
                         dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID INNER JOIN
                         dbo.tblKolMoin ON dbo.tblCheckeDaryafti.fldCodeBankVagozari = dbo.tblKolMoin.fldCodeHesab
WHERE  (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat = 'vagozari')
END
GO
/****** Object:  StoredProcedure [dbo].[ShowInfoEnteghal]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowInfoEnteghal]
	@ShomareEnteghal bigint,
	@fldType nvarchar(50)

AS
BEGIN
	
	SET NOCOUNT ON;

   select  fldCodeAnbar,fldCodeAnbar,fldCodeTafsili,fldSharhKala,fldTedadJoz  from tblRizeForoosh 
   where fldShomareEnteghal=@ShomareEnteghal and fldType=@fldType
END

GO
/****** Object:  StoredProcedure [dbo].[ShowList_ReportJaameBargashtAzFoRoosh_RizeForoosh_final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowList_ReportJaameBargashtAzFoRoosh_RizeForoosh_final]
--چندزبانه
	@fldCodeArz int,
	@language nvarchar(10),
	@userCode bigint,
	@ListCodeKala varchar(5000)=null,
	@ListGroupKala varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListCodeAshkhas varchar(5000)=null,
	@ListCodeVisitor varchar(5000)=null,
	@ListCodeAnbar varchar(5000)=null,
	@DataType varchar(40),
	@DateFrom varchar(8),
	@DateTo varchar(8),
	@FlagSort Varchar(5)
as
   
	
BEGIN
	
	SET NOCOUNT ON;


-- ----------------------------------------
-- -----------------------------------------TempTable In Below Code

IF OBJECT_ID('tempdb..#TempRizeBargashtAzForoosh') IS NOT NULL
 begin
	delete from  #TempRizeBargashtAzForoosh
 end
else 
begin
create table #TempRizeBargashtAzForoosh(

fldShomareSanad Nvarchar(30) COLLATE Persian_100_CI_AS,
fldShomareFactor Nvarchar(30) COLLATE Persian_100_CI_AS,
fldCodeTafsili Nvarchar(30) COLLATE Persian_100_CI_AS,
fldSharheTafzili Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldCodeAnbar Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldCodeKala bigint,
fldNameKala Nvarchar(200) COLLATE Persian_100_CI_AS,
fldGhymatMasrafkonande  decimal(18,4),
fldSharhKala Nvarchar(250) COLLATE Persian_100_CI_AS,
fldTedadCarton Nvarchar(100) COLLATE Persian_100_CI_AS, 
fldTedadJoz  decimal(18,4),
fldFeeJoz  decimal(18,4),
fldFeeYekCarton  decimal(18,4),
fldFeeKol  decimal(18,4),
fldDarsadTakhfif  decimal(18,4),
fldMablaghTakhfif  decimal(18,4),
fldFeeVahed1BadAzTakhfif  decimal(18,4),
fldFeeVahed2BadAzTakhfif  decimal(18,4),
fldArzeshAfzode  decimal(18,4),
fldFeeKolBadAzTakhfif  decimal(18,4),
fldMablaghKhales  decimal(18,4),
fldType Nvarchar(50) COLLATE Persian_100_CI_AS, 
fldDate int, 
fldGroupId Nvarchar(30) COLLATE Persian_100_CI_AS,
fldGroupName Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeVasete Nvarchar(30) COLLATE Persian_100_CI_AS,
fldNameVasete Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldGroupNameAshkhas Nvarchar(200) COLLATE Persian_100_CI_AS,
fldTedadJozKarton  decimal(18,4),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS
)
end

-- Get All AccessKala
declare @accessKala table(code bigint)

insert into @accessKala(code)
--exec ZSP_GetKalaAccesses @userCode
exec [ZSP_GetAccessableGroupKalaCode] @userCode

INSERT INTO #TempRizeBargashtAzForoosh(fldShomareSanad ,
fldShomareFactor ,
fldCodeTafsili ,
fldSharheTafzili , 
fldCodeAnbar , 
fldNameAnbar , 
fldCodeKala ,
fldNameKala ,
fldGhymatMasrafkonande,
fldSharhKala ,
fldTedadCarton , 
fldTedadJoz ,
fldFeeJoz , 
fldFeeYekCarton ,
fldFeeKol , 
fldDarsadTakhfif , 
fldMablaghTakhfif , 
fldFeeVahed1BadAzTakhfif ,
fldFeeVahed2BadAzTakhfif , 
fldArzeshAfzode , 
fldFeeKolBadAzTakhfif , 
fldMablaghKhales , 
fldType, 
fldDate, 
fldGroupId,
fldGroupName, 
fldCodeVasete,
fldNameVasete,
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldTedadJozKarton,
fldNameVahed,
fldNameVahed2)




SELECT        dbo.tblRizeBargashtAzForoosh.fldShomareSanad, dbo.tblRizeBargashtAzForoosh.fldShomareFactor, dbo.tblRizeBargashtAzForoosh.fldCodeTafsili, tblTafzili_1.fldSharheTafzili, 
                         dbo.tblRizeBargashtAzForoosh.fldCodeAnbar, dbo.tblAnbar.fldNameAnbar, dbo.tblRizeBargashtAzForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblKala.fldGhymatMasrafkonande, 
                         dbo.tblRizeBargashtAzForoosh.fldSharhKala, dbo.tblRizeBargashtAzForoosh.fldTedadCarton,ABS(dbo.tblRizeBargashtAzForoosh.fldTedadJoz), dbo.tblRizeBargashtAzForoosh.fldFeeJoz, 
                         dbo.tblRizeBargashtAzForoosh.fldFeeYekCarton, dbo.tblRizeBargashtAzForoosh.fldFeeKol, dbo.tblRizeBargashtAzForoosh.fldDarsadTakhfif, dbo.tblRizeBargashtAzForoosh.fldMablaghTakhfif, 
                         dbo.tblRizeBargashtAzForoosh.fldFeeVahed1BadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldArzeshAfzode, 
                         dbo.tblRizeBargashtAzForoosh.fldFeeKolBadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldMablaghKhales, dbo.tblRizeBargashtAzForoosh.fldType, dbo.tblRizeBargashtAzForoosh.fldDate, 
                         dbo.tblGroupKala.fldGroupId, dbo.tblGroupKala.fldGroupName, dbo.tblRizeBargashtAzForoosh.fldCodeVasete, dbo.tblTafzili.fldSharheTafzili AS fldNameVasete, 
                         dbo.tblGroupAshkhas.fldGroupId AS fldCodeGroupAshkhas, dbo.tblGroupAshkhas.fldGroupName AS fldGroupNameAshkhas, 
                         dbo.tblKala.fldTedadJoz AS fldTedadJozKarton, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2
FROM            dbo.tblVahed INNER JOIN
                         dbo.tblRizeBargashtAzForoosh INNER JOIN
                         dbo.tblAnbar ON dbo.tblRizeBargashtAzForoosh.fldCodeAnbar = dbo.tblAnbar.fldCode INNER JOIN
                         dbo.tblKala ON dbo.tblRizeBargashtAzForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId ON dbo.tblVahed.ID = dbo.tblKala.fldCodeVahed1 INNER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID LEFT OUTER JOIN
                         dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili AS tblTafzili_1 ON dbo.tblGroupAshkhas.fldGroupId = tblTafzili_1.fldCodeGroup ON 
                         dbo.tblRizeBargashtAzForoosh.fldCodeTafsili = tblTafzili_1.fldCodeTafzili LEFT OUTER JOIN
                         dbo.tblTafzili ON dbo.tblRizeBargashtAzForoosh.fldCodeVasete = dbo.tblTafzili.fldCodeTafzili LEFT OUTER JOIN
                         dbo.tblGroupAshkhas AS tblGroupAshkhas_1 ON dbo.tblRizeBargashtAzForoosh.fldCodeVasete = tblGroupAshkhas_1.fldGroupId

						 where 
						  -- access kala limitation
						  tblKala.fldCodeGroup in (select code from @accesskala) and
						 dbo.tblRizeBargashtAzForoosh.fldType=@DataType  ANd dbo.tblRizeBargashtAzForoosh.fldDate  BETWEEN  @DateFrom  AND  @DateTo 

         if @ListCodeKala<>''
		  begin
		   declare @StrDeleteKala varchar(5000)
		    set  @StrDeleteKala='delete from #TempRizeBargashtAzForoosh 
		            where fldCodeKala NOT IN ('+ @ListCodeKala +')'
					execute(@StrDeleteKala)
		  end

		  if @ListGroupKala<>''
		   begin
		     declare @StrDeleteGroupKala varchar(5000)
		       set  @StrDeleteGroupKala='delete from #TempRizeBargashtAzForoosh 
		            where '+ @ListGroupKala + ''
					execute(@StrDeleteGroupKala)
		   end


		   if @ListCodeAshkhas <>''
		    begin
		     declare @StrDeleteAshkhas varchar(5000)
		      set  @StrDeleteAshkhas='delete from #TempRizeBargashtAzForoosh 
		            where fldCodeTafsili NOT IN ('+ @ListCodeAshkhas +')'
					execute(@StrDeleteAshkhas)
		    end


			if @ListGroupAshkhas<>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempRizeBargashtAzForoosh 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end

			if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempRizeBargashtAzForoosh 
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


			if @ListCodeVisitor<>''
		     begin
		      declare @StrDeleteVisitor varchar(5000)
		       set  @StrDeleteVisitor='delete from #TempRizeBargashtAzForoosh 
		            where '+ @ListCodeVisitor + ''
					execute(@StrDeleteVisitor)
		     end
-- ###############################################################
-- ######################### Access ashkhas limitation
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 

delete #TempRizeBargashtAzForoosh where fldCodeTafsili not in(select code from @accessCodes)
-- ###############################################################

update #TempRizeBargashtAzForoosh set #TempRizeBargashtAzForoosh.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#TempRizeBargashtAzForoosh.fldCodeKala and tblkalatranslate.fldLanguage=@Language








if @fldCodeArz=1	 --واحد ارزی پایه
	begin
		update tblSodooreSanad set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

		update #TempRizeBargashtAzForoosh set fldMablaghKhales=fldMablaghKhales/ fldNerkheArz,fldFeeJoz=fldFeeJoz/ fldNerkheArz,fldFeeYekCarton=fldFeeYekCarton/ fldNerkheArz,fldFeeKol=fldFeeKol/ fldNerkheArz,
		fldMablaghTakhfif=fldMablaghTakhfif/ fldNerkheArz,
		fldFeeVahed1BadAzTakhfif=fldFeeVahed1BadAzTakhfif/ fldNerkheArz,
		fldFeeVahed2BadAzTakhfif=fldFeeVahed2BadAzTakhfif/ fldNerkheArz, 
		fldFeeKolBadAzTakhfif=fldFeeKolBadAzTakhfif/ fldNerkheArz,
		fldArzeshAfzode=fldArzeshAfzode/ fldNerkheArz ,
		fldGhymatMasrafkonande=fldGhymatMasrafkonande / fldNerkheArz from tblSodooreSanad Where #TempRizeBargashtAzForoosh.fldShomareSanad=tblSodooreSanad.fldSanad
	end

if @fldCodeArz>1
	begin
			update tblSodooreSanad set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

			declare @Nerkh decimal(18,4)

			set @Nerkh =(Select fldNerkheArz from tblVahedeArzi Where fldCodeArz=(@fldCodeArz))


			--تبدیل به نرخ پایه
			update #TempRizeBargashtAzForoosh set fldMablaghKhales=fldMablaghKhales/ fldNerkheArz,fldFeeJoz=fldFeeJoz/ fldNerkheArz,fldFeeYekCarton=fldFeeYekCarton/ fldNerkheArz,fldFeeKol=fldFeeKol/ fldNerkheArz,
				fldMablaghTakhfif=fldMablaghTakhfif/ fldNerkheArz,
				fldFeeVahed1BadAzTakhfif=fldFeeVahed1BadAzTakhfif/ fldNerkheArz,
				fldFeeVahed2BadAzTakhfif=fldFeeVahed2BadAzTakhfif/ fldNerkheArz, 
				fldFeeKolBadAzTakhfif=fldFeeKolBadAzTakhfif/ fldNerkheArz,
				fldArzeshAfzode=fldArzeshAfzode/ fldNerkheArz ,
				fldGhymatMasrafkonande=fldGhymatMasrafkonande / fldNerkheArz from tblSodooreSanad Where #TempRizeBargashtAzForoosh.fldShomareSanad=tblSodooreSanad.fldSanad --and fldCodeArz<>@fldCodeArz

				--تبدیل به نرخ روز ارز انتخابی
			update #TempRizeBargashtAzForoosh set fldMablaghKhales=fldMablaghKhales* @Nerkh,fldFeeJoz=fldFeeJoz* @Nerkh,fldFeeYekCarton=fldFeeYekCarton* @Nerkh,fldFeeKol=fldFeeKol* @Nerkh,
			fldMablaghTakhfif=fldMablaghTakhfif* @Nerkh,
			fldFeeVahed1BadAzTakhfif=fldFeeVahed1BadAzTakhfif* @Nerkh,
			fldFeeVahed2BadAzTakhfif=fldFeeVahed2BadAzTakhfif* @Nerkh, 
			fldFeeKolBadAzTakhfif=fldFeeKolBadAzTakhfif* @Nerkh,
			fldArzeshAfzode=fldArzeshAfzode* @Nerkh ,
			fldGhymatMasrafkonande=fldGhymatMasrafkonande * @Nerkh from tblSodooreSanad Where #TempRizeBargashtAzForoosh.fldShomareSanad=tblSodooreSanad.fldSanad --and fldCodeArz<>@fldCodeArz
	end








if @FlagSort='0'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,

case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end ,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end ,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType
From #TempRizeBargashtAzForoosh	ORDER BY fldDate ASC
 end
 
 	
if @FlagSort='1'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end ,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end ,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType
From #TempRizeBargashtAzForoosh	ORDER BY fldShomareSanad ASC
 end
 

if @FlagSort='2'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end ,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end ,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType
From #TempRizeBargashtAzForoosh	ORDER BY fldCodeKala ASC
 end
 

if @FlagSort='3'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end ,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end ,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType
From #TempRizeBargashtAzForoosh	ORDER BY fldNameKala ASC
 end
 
 if @FlagSort='4'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end ,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end ,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType
From #TempRizeBargashtAzForoosh	ORDER BY fldCodeTafsili ASC
 end
 

if @FlagSort='5'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end ,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end ,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType
From #TempRizeBargashtAzForoosh	ORDER BY fldSharheTafzili  ASC

end
 
END

GO
/****** Object:  StoredProcedure [dbo].[ShowList_ReportJaameFoRoosh_RizeForoosh_final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowList_ReportJaameFoRoosh_RizeForoosh_final]
--چندزبانه	
	@fldCodeArz int,
	@userCode bigint,
	@ListCodeKala varchar(5000)=null,
	@ListGroupKala varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListCodeAshkhas varchar(5000)=null,
	@ListCodeVisitor varchar(5000)=null,
	@ListCodeAnbar varchar(5000)=null,
	@DataType varchar(40),
	@DateFrom varchar(8),
	@DateTo varchar(8),
	@FlagSort Varchar(5)
as
   
	
BEGIN
	
	SET NOCOUNT ON;


-- ----------------------------------------
-- -----------------------------------------TempTable In Below Code

IF OBJECT_ID('tempdb..#TempRizeForoosh') IS NOT NULL
 begin
	delete from  #TempRizeForoosh
 end
else 
begin
create table #TempRizeForoosh(

fldShomareSanad Nvarchar(30) COLLATE Persian_100_CI_AS,
fldShomareFactor Nvarchar(30) COLLATE Persian_100_CI_AS,
fldCodeTafsili Nvarchar(30) COLLATE Persian_100_CI_AS,
fldSharheTafzili Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldCodeAnbar Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldCodeKala bigint,
fldNameKala Nvarchar(200) COLLATE Persian_100_CI_AS,
fldGhymatMasrafkonande decimal(18,4),
fldSharhKala Nvarchar(250) COLLATE Persian_100_CI_AS,
fldTedadCarton Nvarchar(100) COLLATE Persian_100_CI_AS, 
fldTedadJoz decimal(18,4),
fldFeeJoz  decimal(18,4), 
fldFeeYekCarton decimal(18,4),
fldFeeKol decimal(18,4), 
fldDarsadTakhfif decimal(18,4), 
fldMablaghTakhfif decimal(18,4), 
fldFeeVahed1BadAzTakhfif decimal(18,4),
fldFeeVahed2BadAzTakhfif decimal(18,4), 
fldArzeshAfzode decimal(18,4), 
fldFeeKolBadAzTakhfif decimal(18,4), 
fldMablaghKhales decimal(18,4), 
fldType Nvarchar(50) COLLATE Persian_100_CI_AS, 
fldDate int, 
fldGroupId Nvarchar(30) COLLATE Persian_100_CI_AS,
fldGroupName Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeVasete Nvarchar(30) COLLATE Persian_100_CI_AS,
fldNameVasete Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldGroupNameAshkhas Nvarchar(200) COLLATE Persian_100_CI_AS,
fldTedadJozKarton decimal(18,4),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS
)
end

------------------

-- Start getting KalaAccess

declare @accessKala table(code bigint)

insert into @accessKala(code)
--exec ZSP_GetKalaAccesses @userCode
exec [ZSP_GetAccessableGroupKalaCode] @userCode

INSERT INTO #TempRizeForoosh(fldShomareSanad ,
fldShomareFactor ,
fldCodeTafsili ,
fldSharheTafzili , 
fldCodeAnbar , 
fldNameAnbar , 
fldCodeKala ,
fldNameKala ,
fldGhymatMasrafkonande,
fldSharhKala ,
fldTedadCarton , 
fldTedadJoz ,
fldFeeJoz , 
fldFeeYekCarton ,
fldFeeKol , 
fldDarsadTakhfif , 
fldMablaghTakhfif , 
fldFeeVahed1BadAzTakhfif ,
fldFeeVahed2BadAzTakhfif , 
fldArzeshAfzode , 
fldFeeKolBadAzTakhfif , 
fldMablaghKhales , 
fldType, 
fldDate, 
fldGroupId,
fldGroupName, 
fldCodeVasete,
fldNameVasete,
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldTedadJozKarton,
fldNameVahed,
fldNameVahed2)

SELECT        dbo.tblRizeForoosh.fldShomareSanad, dbo.tblRizeForoosh.fldShomareFactor, dbo.tblRizeForoosh.fldCodeTafsili, tblTafzili_1.fldSharheTafzili, 
                         dbo.tblRizeForoosh.fldCodeAnbar, dbo.tblAnbar.fldNameAnbar, dbo.tblRizeForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblKala.fldGhymatMasrafkonande, 
                         dbo.tblRizeForoosh.fldSharhKala, dbo.tblRizeForoosh.fldTedadCarton,ABS(dbo.tblRizeForoosh.fldTedadJoz), dbo.tblRizeForoosh.fldFeeJoz, 
                         dbo.tblRizeForoosh.fldFeeYekCarton, dbo.tblRizeForoosh.fldFeeKol, dbo.tblRizeForoosh.fldDarsadTakhfif, dbo.tblRizeForoosh.fldMablaghTakhfif, 
                         dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, 
                         dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif, dbo.tblRizeForoosh.fldMablaghKhales, dbo.tblRizeForoosh.fldType, dbo.tblRizeForoosh.fldDate, 
                         dbo.tblGroupKala.fldGroupId, dbo.tblGroupKala.fldGroupName, dbo.tblRizeForoosh.fldCodeVasete, dbo.tblTafzili.fldSharheTafzili AS fldNameVasete, 
                         dbo.tblGroupAshkhas.fldGroupId AS fldCodeGroupAshkhas, dbo.tblGroupAshkhas.fldGroupName AS fldGroupNameAshkhas, 
                         dbo.tblKala.fldTedadJoz AS fldTedadJozKarton, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2
FROM            dbo.tblVahed INNER JOIN
                         dbo.tblRizeForoosh INNER JOIN
                         dbo.tblAnbar ON dbo.tblRizeForoosh.fldCodeAnbar = dbo.tblAnbar.fldCode INNER JOIN
                         dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId ON dbo.tblVahed.ID = dbo.tblKala.fldCodeVahed1 INNER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID LEFT OUTER JOIN
                         dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili AS tblTafzili_1 ON dbo.tblGroupAshkhas.fldGroupId = tblTafzili_1.fldCodeGroup ON 
                         dbo.tblRizeForoosh.fldCodeTafsili = tblTafzili_1.fldCodeTafzili LEFT OUTER JOIN
                         dbo.tblTafzili ON dbo.tblRizeForoosh.fldCodeVasete = dbo.tblTafzili.fldCodeTafzili LEFT OUTER JOIN
                         dbo.tblGroupAshkhas AS tblGroupAshkhas_1 ON dbo.tblRizeForoosh.fldCodeVasete = tblGroupAshkhas_1.fldGroupId

						 where 
						 -- here we adding access kala limitation
						 (tblKala.fldCodeGroup in (select code from @accessKala)) and 
						 dbo.tblRizeForoosh.fldType=@DataType  ANd dbo.tblRizeForoosh.fldDate  BETWEEN  @DateFrom  AND  @DateTo 
         
		 

		 if @ListCodeKala<>''
		  begin
		   declare @StrDeleteKala varchar(5000)
		    set  @StrDeleteKala='delete from #TempRizeForoosh 
		            where fldCodeKala NOT IN ('+ @ListCodeKala +')'
					execute(@StrDeleteKala)
		  end

		  if @ListGroupKala<>''
		   begin
		     declare @StrDeleteGroupKala varchar(5000)
		       set  @StrDeleteGroupKala='delete from #TempRizeForoosh 
		            where '+ @ListGroupKala + ''
					execute(@StrDeleteGroupKala)
		   end


		   if @ListCodeAshkhas <>''
		    begin
		     declare @StrDeleteAshkhas varchar(5000)
		      set  @StrDeleteAshkhas='delete from #TempRizeForoosh 
		            where fldCodeTafsili NOT IN ('+ @ListCodeAshkhas +')'
					execute(@StrDeleteAshkhas)
		    end


			if @ListGroupAshkhas<>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempRizeForoosh 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end

			if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempRizeForoosh 
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


			if @ListCodeVisitor<>''
		     begin
		      declare @StrDeleteVisitor varchar(5000)
		       set  @StrDeleteVisitor='delete from #TempRizeForoosh 
		            where '+ @ListCodeVisitor + ''
					execute(@StrDeleteVisitor)
		     end

	
-- ##################################################
-- ####Group ASHKHAS Limitation######################
-- ##################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 

-- ##################################################

delete from #TempRizeForoosh where fldCodeTafsili not in (select code from @accessCodes)

-- ##################################################

--Multi arz
--*********************************************************************
if @fldCodeArz=1	 --واحد ارزی پایه
	begin
		update tblSodooreSanad set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

		update #TempRizeForoosh set fldMablaghKhales=fldMablaghKhales/ fldNerkheArz,fldFeeJoz=fldFeeJoz/ fldNerkheArz,fldFeeYekCarton=fldFeeYekCarton/ fldNerkheArz,fldFeeKol=fldFeeKol/ fldNerkheArz,
		fldMablaghTakhfif=fldMablaghTakhfif/ fldNerkheArz,
		fldFeeVahed1BadAzTakhfif=fldFeeVahed1BadAzTakhfif/ fldNerkheArz,
		fldFeeVahed2BadAzTakhfif=fldFeeVahed2BadAzTakhfif/ fldNerkheArz, 
		fldFeeKolBadAzTakhfif=fldFeeKolBadAzTakhfif/ fldNerkheArz,
		fldArzeshAfzode=fldArzeshAfzode/ fldNerkheArz ,
		fldGhymatMasrafkonande=fldGhymatMasrafkonande / fldNerkheArz from tblSodooreSanad Where #TempRizeForoosh.fldShomareSanad=tblSodooreSanad.fldSanad
	end

if @fldCodeArz>1
	begin
			update tblSodooreSanad set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

			declare @Nerkh decimal(18,4)

			set @Nerkh =(Select fldNerkheArz from tblVahedeArzi Where fldCodeArz=(@fldCodeArz))


			--تبدیل به نرخ پایه
			update #TempRizeForoosh set fldMablaghKhales=fldMablaghKhales/ fldNerkheArz,fldFeeJoz=fldFeeJoz/ fldNerkheArz,fldFeeYekCarton=fldFeeYekCarton/ fldNerkheArz,fldFeeKol=fldFeeKol/ fldNerkheArz,
				fldMablaghTakhfif=fldMablaghTakhfif/ fldNerkheArz,
				fldFeeVahed1BadAzTakhfif=fldFeeVahed1BadAzTakhfif/ fldNerkheArz,
				fldFeeVahed2BadAzTakhfif=fldFeeVahed2BadAzTakhfif/ fldNerkheArz, 
				fldFeeKolBadAzTakhfif=fldFeeKolBadAzTakhfif/ fldNerkheArz,
				fldArzeshAfzode=fldArzeshAfzode/ fldNerkheArz ,
				fldGhymatMasrafkonande=fldGhymatMasrafkonande / fldNerkheArz from tblSodooreSanad Where #TempRizeForoosh.fldShomareSanad=tblSodooreSanad.fldSanad --and fldCodeArz<>@fldCodeArz

				--تبدیل به نرخ روز ارز انتخابی
			update #TempRizeForoosh set fldMablaghKhales=fldMablaghKhales* @Nerkh,fldFeeJoz=fldFeeJoz* @Nerkh,fldFeeYekCarton=fldFeeYekCarton* @Nerkh,fldFeeKol=fldFeeKol* @Nerkh,
			fldMablaghTakhfif=fldMablaghTakhfif* @Nerkh,
			fldFeeVahed1BadAzTakhfif=fldFeeVahed1BadAzTakhfif* @Nerkh,
			fldFeeVahed2BadAzTakhfif=fldFeeVahed2BadAzTakhfif* @Nerkh, 
			fldFeeKolBadAzTakhfif=fldFeeKolBadAzTakhfif* @Nerkh,
			fldArzeshAfzode=fldArzeshAfzode* @Nerkh ,
			fldGhymatMasrafkonande=fldGhymatMasrafkonande * @Nerkh from tblSodooreSanad Where #TempRizeForoosh.fldShomareSanad=tblSodooreSanad.fldSanad --and fldCodeArz<>@fldCodeArz
	end


--if @fldCodeArz<>0	
--	if @beroozArz=1  --یعنی نرخ روز ارز رو محاسبه کنه
--		begin
--			update tblSodooreSanad set fldNerkh=1,fldarz=1 Where fldNerkh is null

--			declare @Nerkh decimal(18,4)

--			set @Nerkh =(Select fldnerkh from tblVahedeArzi Where fldCode=@fldCodeArz)

--			update #TempRizeForoosh set fldMablaghKhales=fldMablaghKhales* @Nerkh,fldFeeJoz=fldFeeJoz* @Nerkh,fldFeeYekCarton=fldFeeYekCarton* @Nerkh,fldFeeKol=fldFeeKol* @Nerkh,
--			fldMablaghTakhfif=fldMablaghTakhfif* @Nerkh,
--			fldFeeVahed1BadAzTakhfif=fldFeeVahed1BadAzTakhfif* @Nerkh,
--			fldFeeVahed2BadAzTakhfif=fldFeeVahed2BadAzTakhfif* @Nerkh, 
--			fldFeeKolBadAzTakhfif=fldFeeKolBadAzTakhfif* @Nerkh,
--			fldArzeshAfzode=fldArzeshAfzode* @Nerkh ,
--			fldGhymatMasrafkonande=fldGhymatMasrafkonande * @Nerkh from tblSodooreSanad Where #TempRizeForoosh.fldShomareSanad=tblSodooreSanad.fldSanad
--		end
--	else
--		begin
--			update tblSodooreSanad set fldNerkh=1,fldarz=1 Where fldNerkh is null

--			update #TempRizeForoosh set fldMablaghKhales=fldMablaghKhales* fldNerkh,fldFeeJoz=fldFeeJoz* fldNerkh,fldFeeYekCarton=fldFeeYekCarton* fldNerkh,fldFeeKol=fldFeeKol* fldNerkh,
--			fldMablaghTakhfif=fldMablaghTakhfif* fldNerkh,
--			fldFeeVahed1BadAzTakhfif=fldFeeVahed1BadAzTakhfif* fldNerkh,
--			fldFeeVahed2BadAzTakhfif=fldFeeVahed2BadAzTakhfif* fldNerkh, 
--			fldFeeKolBadAzTakhfif=fldFeeKolBadAzTakhfif* fldNerkh,
--			fldArzeshAfzode=fldArzeshAfzode* fldNerkh ,
--			fldGhymatMasrafkonande=fldGhymatMasrafkonande * fldNerkh from tblSodooreSanad Where #TempRizeForoosh.fldShomareSanad=tblSodooreSanad.fldSanad
--		end
--********************************************************************
if @FlagSort='0'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType
From #TempRizeForoosh	ORDER BY fldDate,Convert(dec,fldShomareFactor) ASC
 end
 
 	


if @FlagSort='1'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate,
fldSharhKala,
fldType
From #TempRizeForoosh	ORDER BY Convert(dec,fldShomareSanad),Convert(dec,fldShomareFactor) ASC
 end
 

if @FlagSort='2'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType
From #TempRizeForoosh	ORDER BY fldCodeKala,Convert(dec,fldShomareFactor) ASC
 end
 

if @FlagSort='3'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType
From #TempRizeForoosh	ORDER BY fldNameKala ASC
 end
 
 if @FlagSort='4'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType
From #TempRizeForoosh	ORDER BY Convert(dec,fldCodeTafsili),Convert(dec,fldShomareSanad) ASC
 end
 

if @FlagSort='5'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
fldCodeVasete,
fldNameVasete,
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType
From #TempRizeForoosh	ORDER BY fldSharheTafzili,Convert(dec,fldShomareFactor)  ASC
end
 
END

GO
/****** Object:  StoredProcedure [dbo].[ShowList_ReportJaameKharid_BargashtAzRizeBargashtAzKharid_final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowList_ReportJaameKharid_BargashtAzRizeBargashtAzKharid_final]
--چندزبانه
	@fldCodeArz int,
    @language nvarchar(10),
	@userCode bigint,
	@ListCodeKala varchar(5000)=null,
	@ListGroupKala varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListCodeAshkhas varchar(5000)=null,
	--@ListCodeVisitor varchar(5000)=null,
	@ListCodeAnbar varchar(5000)=null,
	@DataType varchar(40),
	@DateFrom varchar(8),
	@DateTo varchar(8),
	@FlagSort Varchar(5)
as
   
	
BEGIN
	
	SET NOCOUNT ON;


-- ----------------------------------------
-- -----------------------------------------TempTable In Below Code

IF OBJECT_ID('tempdb..#TempRizeBargashtAzKharid') IS NOT NULL
 begin
	delete from  #TempRizeBargashtAzKharid
 end
else 
begin
create table #TempRizeBargashtAzKharid(

fldShomareSanad Nvarchar(30) COLLATE Persian_100_CI_AS,
fldShomareFactor Nvarchar(30) COLLATE Persian_100_CI_AS,
fldCodeTafsili Nvarchar(30) COLLATE Persian_100_CI_AS,
fldSharheTafzili Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldCodeAnbar Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldCodeKala bigint,
fldNameKala Nvarchar(200) COLLATE Persian_100_CI_AS,
fldGhymatMasrafkonande decimal(18,4),
fldSharhKala Nvarchar(250) COLLATE Persian_100_CI_AS,
fldTedadCarton Nvarchar(100) COLLATE Persian_100_CI_AS, 
fldTedadJoz decimal(18,4),
fldFeeJoz decimal(18,4),
fldFeeYekCarton decimal(18,4),
fldFeeKol decimal(18,4),
fldDarsadTakhfif decimal(18,4),
fldMablaghTakhfif decimal(18,4),
fldFeeVahed1BadAzTakhfif decimal(18,4),
fldFeeVahed2BadAzTakhfif decimal(18,4),
fldArzeshAfzode decimal(18,4),
fldFeeKolBadAzTakhfif decimal(18,4),
fldMablaghKhales decimal(18,4),
fldType Nvarchar(50) COLLATE Persian_100_CI_AS, 
fldDate int, 
fldGroupId Nvarchar(30) COLLATE Persian_100_CI_AS,
fldGroupName Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldGroupNameAshkhas Nvarchar(200) COLLATE Persian_100_CI_AS,
fldTedadJozKarton decimal(18,4),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldHamloNaghl  decimal(18,4),
)
end

-- Get All AccessKala
declare @accessKala table(code bigint)

insert into @accessKala(code)
--exec ZSP_GetKalaAccesses @userCode
exec [ZSP_GetAccessableGroupKalaCode] @userCode


INSERT INTO #TempRizeBargashtAzKharid(fldShomareSanad ,
fldShomareFactor ,
fldCodeTafsili ,
fldSharheTafzili , 
fldCodeAnbar , 
fldNameAnbar , 
fldCodeKala ,
fldNameKala ,
fldGhymatMasrafkonande,
fldSharhKala ,
fldTedadCarton , 
fldTedadJoz ,
fldFeeJoz , 
fldFeeYekCarton ,
fldFeeKol , 
fldDarsadTakhfif , 
fldMablaghTakhfif , 
fldFeeVahed1BadAzTakhfif ,
fldFeeVahed2BadAzTakhfif , 
fldArzeshAfzode , 
fldFeeKolBadAzTakhfif , 
fldMablaghKhales , 
fldType, 
fldDate, 
fldGroupId,
fldGroupName, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldTedadJozKarton,
fldNameVahed,
fldNameVahed2,
fldHamloNaghl)




SELECT        dbo.tblRizeBargashtAzKharid.fldShomareSanad, dbo.tblRizeBargashtAzKharid.fldShomareFactor, dbo.tblRizeBargashtAzKharid.fldCodeTafsili, dbo.tblTafzili.fldSharheTafzili, 
                         dbo.tblRizeBargashtAzKharid.fldCodeAnbar, dbo.tblAnbar.fldNameAnbar, dbo.tblRizeBargashtAzKharid.fldCodeKala,
						 dbo.tblKala.fldNameKala,dbo.tblRizeBargashtAzKharid.fldGhymatMasrafkonande, dbo.tblRizeBargashtAzKharid.fldSharhKala, dbo.tblRizeBargashtAzKharid.fldTedadCarton, 
                         ABS(dbo.tblRizeBargashtAzKharid.fldTedadJoz), dbo.tblRizeBargashtAzKharid.fldFeeJoz, dbo.tblRizeBargashtAzKharid.fldFeeYekCarton, dbo.tblRizeBargashtAzKharid.fldFeeKol, dbo.tblRizeBargashtAzKharid.fldDarsadTakhfif, 
                         dbo.tblRizeBargashtAzKharid.fldMablaghTakhfif, dbo.tblRizeBargashtAzKharid.fldFeeVahed1BadAzTakhfif, dbo.tblRizeBargashtAzKharid.fldFeeVahed2BadAzTakhfif, 
                         dbo.tblRizeBargashtAzKharid.fldArzeshAfzode, dbo.tblRizeBargashtAzKharid.fldFeeKolBadAzTakhfif, dbo.tblRizeBargashtAzKharid.fldMablaghKhales, dbo.tblRizeBargashtAzKharid.fldType, 
                         dbo.tblRizeBargashtAzKharid.fldDate, dbo.tblGroupKala.fldGroupId, dbo.tblGroupKala.fldGroupName, dbo.tblGroupAshkhas.fldGroupId AS fldGroupAshkhas, 
                         dbo.tblGroupAshkhas.fldGroupName AS fldGroupNameAshkhas, dbo.tblKala.fldTedadJoz AS fldTedadJozKarton, dbo.tblVahed.fldNameVahed, 
                         tblVahed_1.fldNameVahed AS fldNameVahed2, dbo.tblRizeBargashtAzKharid.fldHamloNaghl

FROM            dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup LEFT OUTER JOIN
                         dbo.tblKala INNER JOIN
                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId INNER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed1 = dbo.tblVahed.ID INNER JOIN
                         dbo.tblRizeBargashtAzKharid INNER JOIN
                         dbo.tblAnbar ON dbo.tblRizeBargashtAzKharid.fldCodeAnbar = dbo.tblAnbar.fldCode ON dbo.tblKala.fldCodeKala = dbo.tblRizeBargashtAzKharid.fldCodeKala ON 
                         dbo.tblTafzili.fldCodeTafzili = dbo.tblRizeBargashtAzKharid.fldCodeTafsili

						 where 
						 -- add kala access limitation
						 tblKala.fldCodeGroup in (select code from @accesskala) and
						 dbo.tblRizeBargashtAzKharid.fldType=@DataType  ANd dbo.tblRizeBargashtAzKharid.fldDate  BETWEEN  @DateFrom  AND  @DateTo 

         if @ListCodeKala<>''
		  begin
		   declare @StrDeleteKala varchar(5000)
		    set  @StrDeleteKala='delete from #TempRizeBargashtAzKharid
		            where fldCodeKala NOT IN ('+ @ListCodeKala +')'
					execute(@StrDeleteKala)
		  end

		  if @ListGroupKala<>''
		   begin
		     declare @StrDeleteGroupKala varchar(5000)
		       set  @StrDeleteGroupKala='delete from #TempRizeBargashtAzKharid 
		            where '+ @ListGroupKala + ''
					execute(@StrDeleteGroupKala)
		   end


		   if @ListCodeAshkhas <>''
		    begin
		     declare @StrDeleteAshkhas varchar(5000)
		      set  @StrDeleteAshkhas='delete from #TempRizeBargashtAzKharid 
		            where fldCodeTafsili NOT IN ('+ @ListCodeAshkhas +')'
					execute(@StrDeleteAshkhas)
		    end


			if @ListGroupAshkhas<>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempRizeBargashtAzKharid 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end

			if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempRizeBargashtAzKharid 
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


-- #######################################################################
-- Group Ashkhas limitation
-- #######################################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 

delete from #TempRizeBargashtAzKharid where fldCodeTafsili not in (select code from @accessCodes)
-- #######################################################################

update #TempRizeBargashtAzKharid set #TempRizeBargashtAzKharid.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#TempRizeBargashtAzKharid.fldCodeKala and tblkalatranslate.fldLanguage=@Language


--Multi arz
--*********************************************************************
if @fldCodeArz=1	 --واحد ارزی پایه
	begin
		update tblSodooreSanad set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

		update #TempRizeBargashtAzKharid set fldMablaghKhales=fldMablaghKhales/ fldNerkheArz,fldFeeJoz=fldFeeJoz/ fldNerkheArz,fldFeeYekCarton=fldFeeYekCarton/ fldNerkheArz,fldFeeKol=fldFeeKol/ fldNerkheArz,
		fldMablaghTakhfif=fldMablaghTakhfif/ fldNerkheArz,
		fldFeeVahed1BadAzTakhfif=fldFeeVahed1BadAzTakhfif/ fldNerkheArz,
		fldFeeVahed2BadAzTakhfif=fldFeeVahed2BadAzTakhfif/ fldNerkheArz, 
		fldFeeKolBadAzTakhfif=fldFeeKolBadAzTakhfif/ fldNerkheArz,
		fldArzeshAfzode=fldArzeshAfzode/ fldNerkheArz ,
		fldGhymatMasrafkonande=fldGhymatMasrafkonande / fldNerkheArz from tblSodooreSanad Where #TempRizeBargashtAzKharid.fldShomareSanad=tblSodooreSanad.fldSanad
	end

if @fldCodeArz>1
	begin
			update tblSodooreSanad set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

			declare @Nerkh decimal(18,4)

			set @Nerkh =(Select fldNerkheArz from tblVahedeArzi Where fldCodeArz=(@fldCodeArz))


			--تبدیل به نرخ پایه
			update #TempRizeBargashtAzKharid set fldMablaghKhales=fldMablaghKhales/ fldNerkheArz,fldFeeJoz=fldFeeJoz/ fldNerkheArz,fldFeeYekCarton=fldFeeYekCarton/ fldNerkheArz,fldFeeKol=fldFeeKol/ fldNerkheArz,
				fldMablaghTakhfif=fldMablaghTakhfif/ fldNerkheArz,
				fldFeeVahed1BadAzTakhfif=fldFeeVahed1BadAzTakhfif/ fldNerkheArz,
				fldFeeVahed2BadAzTakhfif=fldFeeVahed2BadAzTakhfif/ fldNerkheArz, 
				fldFeeKolBadAzTakhfif=fldFeeKolBadAzTakhfif/ fldNerkheArz,
				fldArzeshAfzode=fldArzeshAfzode/ fldNerkheArz ,
				fldGhymatMasrafkonande=fldGhymatMasrafkonande / fldNerkheArz from tblSodooreSanad Where #TempRizeBargashtAzKharid.fldShomareSanad=tblSodooreSanad.fldSanad --and fldCodeArz<>@fldCodeArz

				--تبدیل به نرخ روز ارز انتخابی
			update #TempRizeBargashtAzKharid set fldMablaghKhales=fldMablaghKhales* @Nerkh,fldFeeJoz=fldFeeJoz* @Nerkh,fldFeeYekCarton=fldFeeYekCarton* @Nerkh,fldFeeKol=fldFeeKol* @Nerkh,
			fldMablaghTakhfif=fldMablaghTakhfif* @Nerkh,
			fldFeeVahed1BadAzTakhfif=fldFeeVahed1BadAzTakhfif* @Nerkh,
			fldFeeVahed2BadAzTakhfif=fldFeeVahed2BadAzTakhfif* @Nerkh, 
			fldFeeKolBadAzTakhfif=fldFeeKolBadAzTakhfif* @Nerkh,
			fldArzeshAfzode=fldArzeshAfzode* @Nerkh ,
			fldGhymatMasrafkonande=fldGhymatMasrafkonande * @Nerkh from tblSodooreSanad Where #TempRizeBargashtAzKharid.fldShomareSanad=tblSodooreSanad.fldSanad --and fldCodeArz<>@fldCodeArz
	end


if @FlagSort='0'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeBargashtAzKharid	ORDER BY fldDate ASC
 end
 
 	
if @FlagSort='1'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeBargashtAzKharid	ORDER BY fldShomareSanad ASC
 end
 

if @FlagSort='2'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeBargashtAzKharid	ORDER BY fldCodeKala ASC
 end
 

if @FlagSort='3'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeBargashtAzKharid	ORDER BY fldNameKala ASC
 end
 
 if @FlagSort='4'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeBargashtAzKharid	ORDER BY fldCodeTafsili ASC
 end
 
 
if @FlagSort='5'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
1,
2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeBargashtAzKharid	ORDER BY fldSharheTafzili  ASC
end
 
END

GO
/****** Object:  StoredProcedure [dbo].[ShowList_ReportJaameKharid_RizeKharid_final]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowList_ReportJaameKharid_RizeKharid_final]
	@language nvarchar(10),
	@userCode bigint,
	@ListCodeKala varchar(5000)=null,
	@ListGroupKala varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListCodeAshkhas varchar(5000)=null,
	--@ListCodeVisitor varchar(5000)=null,
	@ListCodeAnbar varchar(5000)=null,
	@DataType varchar(40),
	@DateFrom varchar(8),
	@DateTo varchar(8),
	@FlagSort Varchar(5)
as
   
	
BEGIN
	
	SET NOCOUNT ON;


-- ----------------------------------------
-- -----------------------------------------TempTable In Below Code

IF OBJECT_ID('tempdb..#TempRizeKharid') IS NOT NULL
 begin
	delete from  #TempRizeKharid
 end
else 
begin
create table #TempRizeKharid(

fldShomareSanad Nvarchar(30) COLLATE Persian_100_CI_AS,
fldShomareFactor Nvarchar(30) COLLATE Persian_100_CI_AS,
fldCodeTafsili Nvarchar(30) COLLATE Persian_100_CI_AS,
fldSharheTafzili Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldCodeAnbar Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldCodeKala bigint,
fldNameKala Nvarchar(200) COLLATE Persian_100_CI_AS,
fldGhymatMasrafkonande Nvarchar(100) COLLATE Persian_100_CI_AS,
fldSharhKala Nvarchar(250) COLLATE Persian_100_CI_AS,
fldTedadCarton Nvarchar(100) COLLATE Persian_100_CI_AS, 
fldTedadJoz decimal(18,4),
fldFeeJoz decimal(18,4),
fldFeeYekCarton decimal(18,4),
fldFeeKol decimal(18,4),
fldDarsadTakhfif decimal(18,4),
fldMablaghTakhfif decimal(18,4), 
fldFeeVahed1BadAzTakhfif decimal(18,4),
fldFeeVahed2BadAzTakhfif decimal(18,4),
fldArzeshAfzode decimal(18,4),
fldFeeKolBadAzTakhfif decimal(18,4),
fldMablaghKhales decimal(18,4),
fldType Nvarchar(50) COLLATE Persian_100_CI_AS, 
fldDate int, 
fldGroupId Nvarchar(30) COLLATE Persian_100_CI_AS,
fldGroupName Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldGroupNameAshkhas Nvarchar(200) COLLATE Persian_100_CI_AS,
fldTedadJozKarton decimal(18,4),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldHamloNaghl  decimal(18,4)
)
end

-- Get All AccessKala
declare @accessKala table(code bigint)
insert into @accessKala(code)
--exec ZSP_GetKalaAccesses @userCode
exec [ZSP_GetAccessableGroupKalaCode] @userCode

-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodesAshkhas table(code bigint)
insert into @accessCodesAshkhas select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################


INSERT INTO #TempRizeKharid(fldShomareSanad ,
fldShomareFactor ,
fldCodeTafsili ,
fldSharheTafzili , 
fldCodeAnbar , 
fldNameAnbar , 
fldCodeKala ,
fldNameKala ,
fldGhymatMasrafkonande,
fldSharhKala ,
fldTedadCarton , 
fldTedadJoz ,
fldFeeJoz , 
fldFeeYekCarton ,
fldFeeKol , 
fldDarsadTakhfif , 
fldMablaghTakhfif , 
fldFeeVahed1BadAzTakhfif ,
fldFeeVahed2BadAzTakhfif , 
fldArzeshAfzode , 
fldFeeKolBadAzTakhfif , 
fldMablaghKhales , 
fldType, 
fldDate, 
fldGroupId,
fldGroupName, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldTedadJozKarton,
fldNameVahed,
fldNameVahed2,
fldHamloNaghl)




SELECT        dbo.tblRizeKharid.fldShomareSanad, dbo.tblRizeKharid.fldShomareFactor, dbo.tblRizeKharid.fldCodeTafsili, dbo.tblTafzili.fldSharheTafzili, 
                         dbo.tblRizeKharid.fldCodeAnbar, dbo.tblAnbar.fldNameAnbar, dbo.tblRizeKharid.fldCodeKala,
						 dbo.tblKala.fldNameKala,dbo.tblRizeKharid.fldGhymatMasrafkonande, dbo.tblRizeKharid.fldSharhKala, dbo.tblRizeKharid.fldTedadCarton, 
                         ABS(dbo.tblRizeKharid.fldTedadJoz), dbo.tblRizeKharid.fldFeeJoz, dbo.tblRizeKharid.fldFeeYekCarton, dbo.tblRizeKharid.fldFeeKol, dbo.tblRizeKharid.fldDarsadTakhfif, 
                         dbo.tblRizeKharid.fldMablaghTakhfif, dbo.tblRizeKharid.fldFeeVahed1BadAzTakhfif, dbo.tblRizeKharid.fldFeeVahed2BadAzTakhfif, 
                         dbo.tblRizeKharid.fldArzeshAfzode, dbo.tblRizeKharid.fldFeeKolBadAzTakhfif, dbo.tblRizeKharid.fldMablaghKhales, dbo.tblRizeKharid.fldType, 
                         dbo.tblRizeKharid.fldDate, dbo.tblGroupKala.fldGroupId, dbo.tblGroupKala.fldGroupName, dbo.tblGroupAshkhas.fldGroupId AS fldGroupAshkhas, 
                         dbo.tblGroupAshkhas.fldGroupName AS fldGroupNameAshkhas, dbo.tblKala.fldTedadJoz AS fldTedadJozKarton, dbo.tblVahed.fldNameVahed, 
                         tblVahed_1.fldNameVahed AS fldNameVahed2, dbo.tblRizeKharid.fldHamloNaghl

FROM            dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup LEFT OUTER JOIN
                         dbo.tblKala INNER JOIN
                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId INNER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed1 = dbo.tblVahed.ID INNER JOIN
                         dbo.tblRizeKharid INNER JOIN
                         dbo.tblAnbar ON dbo.tblRizeKharid.fldCodeAnbar = dbo.tblAnbar.fldCode ON dbo.tblKala.fldCodeKala = dbo.tblRizeKharid.fldCodeKala ON 
                         dbo.tblTafzili.fldCodeTafzili = dbo.tblRizeKharid.fldCodeTafsili

						 where 
						 -- add kala access limitation
						 tblKala.fldCodeGroup in (select code from @accesskala) and
						 -- ##################### Group Ashkhas Limitation
						 fldCodeTafsili in (select code from @accessCodesAshkhas) and
						 dbo.tblRizeKharid.fldType=@DataType  ANd dbo.tblRizeKharid.fldDate  BETWEEN  @DateFrom  AND  @DateTo 

         if @ListCodeKala<>''
		  begin
		   declare @StrDeleteKala varchar(5000)
		    set  @StrDeleteKala='delete from #TempRizeKharid
		            where fldCodeKala NOT IN ('+ @ListCodeKala +')'
					execute(@StrDeleteKala)
		  end

		  if @ListGroupKala<>''
		   begin
		     declare @StrDeleteGroupKala varchar(5000)
		       set  @StrDeleteGroupKala='delete from #TempRizeKharid 
		            where '+ @ListGroupKala + ''
					execute(@StrDeleteGroupKala)
		   end


		   if @ListCodeAshkhas <>''
		    begin
		     declare @StrDeleteAshkhas varchar(5000)
		      set  @StrDeleteAshkhas='delete from #TempRizeKharid 
		            where fldCodeTafsili NOT IN ('+ @ListCodeAshkhas +')'
					execute(@StrDeleteAshkhas)
		    end


			if @ListGroupAshkhas<>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempRizeKharid 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end

			if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempRizeKharid 
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


update #TempRizeKharid set #TempRizeKharid.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#TempRizeKharid.fldCodeKala and tblkalatranslate.fldLanguage=@Language

if @FlagSort='0'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
--iIF(fldNameVahed=N'انتخاب کنید' ,'',fldNameVahed) as fldNameVahed,
--select cast when fldNameVahed=N'انتخاب کنید' then '' else '' end,
CASE  WHEN fldNameVahed=N'انتخاب کنید' THEN ''  END  as fldNameVahed,
CASE  WHEN fldNameVahed2=N'انتخاب کنید' THEN ''  END  as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeKharid	ORDER BY fldDate ASC
 end
 
 	
if @FlagSort='1'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
--IIF(fldNameVahed=N'انتخاب کنید' ,'',fldNameVahed) as fldNameVahed,
CASE  WHEN fldNameVahed=N'انتخاب کنید' THEN ''  END  as fldNameVahed,
--IIF(fldNameVahed2=N'انتخاب کنید' ,'',fldNameVahed2) as fldNameVahed2,
CASE  WHEN fldNameVahed2=N'انتخاب کنید' THEN ''  END  as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeKharid	ORDER BY fldShomareSanad ASC
 end
 

if @FlagSort='2'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
--IIF(fldNameVahed=N'انتخاب کنید' ,'',fldNameVahed) as fldNameVahed,
CASE  WHEN fldNameVahed=N'انتخاب کنید' THEN ''  END  as fldNameVahed,
--IIF(fldNameVahed2=N'انتخاب کنید' ,'',fldNameVahed2) as fldNameVahed2,
CASE  WHEN fldNameVahed2=N'انتخاب کنید' THEN ''  END  as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeKharid	ORDER BY fldCodeKala ASC
 end
 

if @FlagSort='3'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
--IIF(fldNameVahed=N'انتخاب کنید' ,'',fldNameVahed) as fldNameVahed,
CASE  WHEN fldNameVahed=N'انتخاب کنید' THEN ''  END  as fldNameVahed,
--IIF(fldNameVahed2=N'انتخاب کنید' ,'',fldNameVahed2) as fldNameVahed2,
CASE  WHEN fldNameVahed2=N'انتخاب کنید' THEN ''  END  as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeKharid	ORDER BY fldNameKala ASC
 end
 
 if @FlagSort='4'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
--IIF(fldNameVahed=N'انتخاب کنید' ,'',fldNameVahed) as fldNameVahed,
CASE  WHEN fldNameVahed=N'انتخاب کنید' THEN ''  END  as fldNameVahed,
--IIF(fldNameVahed2=N'انتخاب کنید' ,'',fldNameVahed2) as fldNameVahed2,
CASE  WHEN fldNameVahed2=N'انتخاب کنید' THEN ''  END  as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeKharid	ORDER BY fldCodeTafsili ASC
 end
 

if @FlagSort='5'
 begin
select fldShomareSanad,
fldShomareFactor,
fldCodeTafsili,
fldSharheTafzili,
fldCodeKala,
fldNameKala,
--IIF(fldNameVahed=N'انتخاب کنید' ,'',fldNameVahed) as fldNameVahed,
CASE  WHEN fldNameVahed=N'انتخاب کنید' THEN ''  END  as fldNameVahed,
--IIF(fldNameVahed2=N'انتخاب کنید' ,'',fldNameVahed2) as fldNameVahed2,
CASE  WHEN fldNameVahed2=N'انتخاب کنید' THEN ''  END  as fldNameVahed2,
fldTedadCarton,
fldTedadJozKarton,
fldTedadJoz,
fldFeeJoz, 
fldFeeYekCarton,
fldFeeKol,
fldDarsadTakhfif, 
fldMablaghTakhfif,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldFeeKolBadAzTakhfif,
fldArzeshAfzode, 
fldGhymatMasrafkonande,
fldMablaghKhales, 
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar, 
substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate, 
fldSharhKala,
fldType,
fldHamloNaghl
From #TempRizeKharid	ORDER BY fldSharheTafzili  ASC
end
 
END

GO
/****** Object:  StoredProcedure [dbo].[ShowList_TblTell_By_CodeTafsili_From_Form_Ashkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowList_TblTell_By_CodeTafsili_From_Form_Ashkhas]
--چندزبانه
@fldCodeTafsili nvarchar(10)
as
begin
SELECT        fldShomare
FROM          dbo.tblDaftarcheTell
where fldCodeTafzili=@fldCodeTafsili 
end

GO
/****** Object:  StoredProcedure [dbo].[ShowList_TblTell_By_CodeTafsili_From_Form_Ashkhas_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowList_TblTell_By_CodeTafsili_From_Form_Ashkhas_WithUserCode]
--چندزبانه
@fldCodeTafsili nvarchar(10),
@userCode bigint
as
begin

-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################
-- ##########################################################



SELECT        fldShomare
FROM          dbo.tblDaftarcheTell
where fldCodeTafzili=@fldCodeTafsili 
and fldCodeTafzili in (select code from @accessCodes)
end

GO
/****** Object:  StoredProcedure [dbo].[ShowList_Vasteha]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[ShowList_Vasteha]
as
begin
select fldGroupId,fldGroupName   from tblGroupAshkhas
where convert(nvarchar,[fldGroupId]) like convert(nvarchar,10100)+ '%' 
and len(convert(nvarchar,[fldGroupId]))=9
end

GO
/****** Object:  StoredProcedure [dbo].[ShowListEbteda_Doreh_Ashkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowListEbteda_Doreh_Ashkhas]
	
	--@fldtype nvarchar(50),
	--@Result nvarchar(20) output
AS
BEGIN
	
	SET NOCOUNT ON;
SELECT        dbo.tblSodooreSanad.fldSanad,dbo.tblSodooreSanad.fldCodeTafzili, dbo.tblTafzili.fldSharheTafzili, dbo.tblSodooreSanad.fldBedehkar,
              dbo.tblSodooreSanad.fldBestankar, dbo.tblSodooreSanad.fldRadif, 
              dbo.tblSodooreSanad.fldDate, dbo.tblSodooreSanad.fldSharh
FROM            dbo.tblTafzili INNER JOIN
                         dbo.tblSodooreSanad ON dbo.tblTafzili.fldCodeTafzili = dbo.tblSodooreSanad.fldCodeTafzili
WHERE        (dbo.tblSodooreSanad.fldType = 'EbtedaAshkhas')
END

GO
/****** Object:  StoredProcedure [dbo].[ShowListeDasteCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[ShowListeDasteCheck]
@CodeBank bigint

as
begin
	SELECT        ID, fldCodeBank, fldShobe, fldShomareCheck, fldShomareHesab
		FROM            tblDasteCheck
		WHERE        (fldCodeBank = @CodeBank)
		ORDER BY fldShomareCheck
end

GO
/****** Object:  StoredProcedure [dbo].[ShowListEnteghal_Kala_Anbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowListEnteghal_Kala_Anbar]
--چندزبانه
	@fldShomareEnteghal bigint,
	@fldtype nvarchar(50),
	@Result nvarchar(20) output

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

SELECT        dbo.tblKala.fldNameKala, dbo.tblRizeForoosh.fldTedadJoz, dbo.tblRizeForoosh.fldTedadCarton, dbo.tblRizeForoosh.fldSharhKala,  
                         dbo.tblKala.fldCodeKala AS fldCodeKala,tblRizeForoosh.fldDateEngheza as fldDateEngheza
FROM            dbo.tblRizeForoosh INNER JOIN
                         dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala
WHERE        (dbo.tblRizeForoosh.fldShomareEnteghal = @fldShomareEnteghal and dbo.tblRizeForoosh.fldType=@fldtype)
END

GO
/****** Object:  StoredProcedure [dbo].[ShowListePersonel]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ShowListePersonel] 
AS

IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	CREATE TABLE #tmpSooratHesab2(
	[ID] [bigint]  NOT NULL,
	[fldSharheTafzili] [nvarchar](200)  COLLATE Persian_100_CI_AS,
	[fldCodeTafzili][nvarchar](200)  COLLATE Persian_100_CI_AS,
	[fldOstan] [nvarchar](50)  COLLATE Persian_100_CI_AS,
	[fldCity] [nvarchar](50) COLLATE Persian_100_CI_AS,
	[fldAddress] [nvarchar](200) COLLATE Persian_100_CI_AS,
	[fldTell] [nvarchar](50) COLLATE Persian_100_CI_AS
	)

	Insert into	#tmpSooratHesab2(ID,fldSharheTafzili,fldCodeTafzili,fldOstan,fldCity,fldAddress)
			SELECT        ID, fldSharheTafzili, fldCodeTafzili, fldOstan, fldCity, fldAddress
				FROM            tblTafzili
					Where fldCodeNoe='0' and fldNoeVizitor=2

	update #tmpSooratHesab2
		set fldTell=tblDaftarcheTell.fldShomare
		from tblDaftarcheTell Where #tmpSooratHesab2.fldCodeTafzili=tblDaftarcheTell.fldCodeTafzili

		Select fldCodeTafzili, fldSharheTafzili, fldOstan, fldCity, fldTell,fldAddress,ID from #tmpSooratHesab2
END

GO
/****** Object:  StoredProcedure [dbo].[ShowListePersonelType]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ShowListePersonelType] 
@NoePersonel nvarchar(20)
AS

IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	CREATE TABLE #tmpSooratHesab2(
	[ID] [bigint]  NOT NULL,
	[fldSharheTafzili] [nvarchar](200) COLLATE Persian_100_CI_AS ,
	[fldCodeTafzili][nvarchar](200) COLLATE Persian_100_CI_AS ,
	[fldOstan] [nvarchar](50)  COLLATE Persian_100_CI_AS,
	[fldCity] [nvarchar](50) COLLATE Persian_100_CI_AS,
	[fldAddress] [nvarchar](200) COLLATE Persian_100_CI_AS,
	[fldTell] [nvarchar](50) COLLATE Persian_100_CI_AS
	)

	Insert into	#tmpSooratHesab2(ID,fldSharheTafzili,fldCodeTafzili,fldOstan,fldCity,fldAddress)
			SELECT        ID, fldSharheTafzili, fldCodeTafzili, fldOstan, fldCity, fldAddress
				FROM            tblTafzili
					Where fldCodeNoe='0' and fldNoePersonel=@NoePersonel

	update #tmpSooratHesab2
		set fldTell=tblDaftarcheTell.fldShomare
		from tblDaftarcheTell Where #tmpSooratHesab2.fldCodeTafzili=tblDaftarcheTell.fldCodeTafzili

		Select fldCodeTafzili, fldSharheTafzili, fldOstan, fldCity, fldTell,fldAddress,ID from #tmpSooratHesab2
END

GO
/****** Object:  StoredProcedure [dbo].[ShowListeUser]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ShowListeUser] 
	@CodeUser bigint
AS
BEGIN
	declare @intType integer

	set @intType = (Select fldType from tblSecurity Where fldCode=@CodeUser)
	if @intType=0 --سازنده
		Select ID, fldCode, fldUser, fldPassword, fldType from tblSecurity
	else if @intType=1 -- مدیر
		Select ID, fldCode, fldUser, fldPassword, fldType from tblSecurity where fldType<>0
	else if @intType=2 --کاربر
		Select ID, fldCode, fldUser, fldPassword, fldType from tblSecurity where fldCode=@CodeUser

END

GO
/****** Object:  StoredProcedure [dbo].[ShowListGroupAshkhas_AfterAdd]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListGroupAshkhas_AfterAdd](@ID  nvarchar(50))
as
begin
SELECT [fldGroupId]
      ,[fldGroupName]
  FROM [dbo].[tblGroupAshkhas]
  --where fldGroupId=convert(bigint,len(convert(nvarchar,[fldGroupId])=3))
  where convert(nvarchar,[fldGroupId]) like @ID + '%' and  len(convert(nvarchar,[fldGroupId]))=(len(@ID)+3)
order by convert(nvarchar,[fldGroupId])
end

GO
/****** Object:  StoredProcedure [dbo].[ShowListGroupAshkhas_AfterAdd_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[ShowListGroupAshkhas_AfterAdd_WithUserCode]
(
@ID  nvarchar(50),
@userCode bigint
)
as
begin

declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableGroupAshkhasCode(@userCode) 

SELECT [fldGroupId]
      ,[fldGroupName]
  FROM [dbo].[tblGroupAshkhas]
  --where fldGroupId=convert(bigint,len(convert(nvarchar,[fldGroupId])=3))
  where 
  fldGroupId in (select code from @accessCodes ) and
  convert(nvarchar,[fldGroupId]) like @ID + '%' and  len(convert(nvarchar,[fldGroupId]))=(len(@ID)+3)
order by convert(nvarchar,[fldGroupId])
end

GO
/****** Object:  StoredProcedure [dbo].[ShowListGroupAshkhas_ViSitors_ShowAll]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListGroupAshkhas_ViSitors_ShowAll]
as
begin
select fldGroupId,fldGroupName   from tblGroupAshkhas
where convert(nvarchar,[fldGroupId]) like convert(nvarchar,10100)+ '%' 
and len(convert(nvarchar,[fldGroupId]))=6
end

GO
/****** Object:  StoredProcedure [dbo].[ShowListGroupAshkhas_ViSitors_ShowAll_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[ShowListGroupAshkhas_ViSitors_ShowAll_WithUserCode]
@userCode bigint
as
begin

declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableGroupAshkhasCode(@userCode) 

select fldGroupId,fldGroupName   from tblGroupAshkhas
where 
fldGroupId in (select code from @accessCodes) and 
convert(nvarchar,[fldGroupId]) like convert(nvarchar,10100)+ '%' 
and len(convert(nvarchar,[fldGroupId]))=6
end

GO
/****** Object:  StoredProcedure [dbo].[ShowListGroupKala_AfterAdd]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListGroupKala_AfterAdd](@ID  nvarchar(50))
as
begin
SELECT [fldGroupId]
      ,[fldGroupName]
  FROM [dbo].[tblGroupKala]
  --where fldGroupId=convert(bigint,len(convert(nvarchar,[fldGroupId])=3))
  where convert(nvarchar,[fldGroupId]) like @ID + '%' and  len(convert(nvarchar,[fldGroupId]))=(len(@ID)+3)
order by convert(nvarchar,[fldGroupId])
end

GO
/****** Object:  StoredProcedure [dbo].[ShowListGruopAshkhas_All]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListGruopAshkhas_All]
as
begin
SELECT [fldGroupId]
      ,[fldGroupName]
  FROM [dbo].[tblGroupAshkhas]
  --where fldGroupId=convert(bigint,len(convert(nvarchar,[fldGroupId])=3))
  where len(convert(nvarchar,[fldGroupId]))=3
order by convert(nvarchar,[fldGroupId])
end

GO
/****** Object:  StoredProcedure [dbo].[ShowListGruopAshkhas_All_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListGruopAshkhas_All_WithUserCode]
--چندزبانه
@userCode bigint
as
begin

declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableGroupAshkhasCode(@userCode) 

SELECT [fldGroupId]
      ,[fldGroupName]
  FROM [dbo].[tblGroupAshkhas]
  --where fldGroupId=convert(bigint,len(convert(nvarchar,[fldGroupId])=3))
  where 
  fldGroupId in (select code from @accessCodes) and
  len(convert(nvarchar,[fldGroupId]))=3
order by convert(nvarchar,[fldGroupId])
end


GO
/****** Object:  StoredProcedure [dbo].[showListGruopAshkhasBy_Enter]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[showListGruopAshkhasBy_Enter](@id  bigint,@len  int)
--چندزبانه
as

if(exists(select [fldGroupId],fldGroupName from tblGroupAshkhas where convert(nvarchar,[fldGroupId]) like convert(nvarchar,@id)+ '%' and len(convert(nvarchar,[fldGroupId]))=@len+3))
begin
SELECT [fldGroupId]
      ,[fldGroupName]
  FROM [dbo].[tblGroupAshkhas]
  where convert(nvarchar,[fldGroupId]) like convert(nvarchar,@id)+ '%' and len(convert(nvarchar,[fldGroupId]))=@len+3
order by convert(nvarchar,[fldGroupId])
end

GO
/****** Object:  StoredProcedure [dbo].[showListGruopAshkhasBy_Enter_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[showListGruopAshkhasBy_Enter_WithUserCode]
(@id  bigint,
@len  int,
@userCode bigint
)
as

if(exists(select [fldGroupId],fldGroupName from tblGroupAshkhas where convert(nvarchar,[fldGroupId]) like convert(nvarchar,@id)+ '%' and len(convert(nvarchar,[fldGroupId]))=@len+3))
begin

declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableGroupAshkhasCode(@userCode) 

SELECT [fldGroupId]
      ,[fldGroupName]
  FROM [dbo].[tblGroupAshkhas]
  where 
  fldGroupId in (select code from @accessCodes) and
  convert(nvarchar,[fldGroupId]) like convert(nvarchar,@id)+ '%' and len(convert(nvarchar,[fldGroupId]))=@len+3
order by convert(nvarchar,[fldGroupId])
end

GO
/****** Object:  StoredProcedure [dbo].[showListGruopAshkhasBy_Escap]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[showListGruopAshkhasBy_Escap](@id  nvarchar(30),@lenid  nvarchar(30))
as


if (@lenid >= 3)
begin
SELECT [fldGroupId]
      ,[fldGroupName]
FROM [dbo].[tblGroupAshkhas]
where convert(nvarchar,[fldGroupId]) like convert(nvarchar,@id)+ '%' and len(convert(nvarchar,[fldGroupId]))=@lenid+3
order by convert(nvarchar,[fldGroupId])

end

GO
/****** Object:  StoredProcedure [dbo].[showListGruopAshkhasBy_Escap_WithUserName]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  proc [dbo].[showListGruopAshkhasBy_Escap_WithUserName]
--چندزبانه
(@id  nvarchar(30),
@lenid  nvarchar(30),
@userCode bigint
)
as
begin


declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableGroupAshkhasCode(@userCode) 

if (@lenid >= 3)
begin
SELECT [fldGroupId]
      ,[fldGroupName]
FROM [dbo].[tblGroupAshkhas]
where 
fldGroupId in (select code from @accessCodes) and
convert(nvarchar,[fldGroupId]) like convert(nvarchar,@id)+ '%' and len(convert(nvarchar,[fldGroupId]))=@lenid+3
order by convert(nvarchar,[fldGroupId])

end


end

GO
/****** Object:  StoredProcedure [dbo].[ShowListGruopKala_All]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListGruopKala_All]
--چندزبانه
as
begin
SELECT [fldGroupId]
      ,[fldGroupName]
  FROM [dbo].[tblGroupKala]
  --where fldGroupId=convert(bigint,len(convert(nvarchar,[fldGroupId])=3))
  where len(convert(nvarchar,[fldGroupId]))=3
order by convert(nvarchar,[fldGroupId])
end

GO
/****** Object:  StoredProcedure [dbo].[showListGruopKalaBy_Enter]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[showListGruopKalaBy_Enter](@id  bigint,@len  int)
--چندزبانه
as

if(exists(select [fldGroupId],fldGroupName from tblGroupKala where convert(nvarchar,[fldGroupId]) like convert(nvarchar,@id)+ '%' and len(convert(nvarchar,[fldGroupId]))=@len+3))
begin
SELECT [fldGroupId]
      ,[fldGroupName]
  FROM [dbo].[tblGroupKala]
  where convert(nvarchar,[fldGroupId]) like convert(nvarchar,@id)+ '%' and len(convert(nvarchar,[fldGroupId]))=@len+3
order by convert(nvarchar,[fldGroupId])
end

GO
/****** Object:  StoredProcedure [dbo].[showListGruopKalaBy_Escap]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[showListGruopKalaBy_Escap](@id  nvarchar(30),@lenid  nvarchar(30))
--چندزبانه
as


if (@lenid >= 3)
begin
SELECT [fldGroupId]
      ,[fldGroupName]
  FROM [dbo].[tblGroupKala]
where convert(nvarchar,[fldGroupId]) like convert(nvarchar,@id)+ '%' and len(convert(nvarchar,[fldGroupId]))=@lenid+3
order by convert(nvarchar,[fldGroupId])

end

GO
/****** Object:  StoredProcedure [dbo].[ShowListKalaMulti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ShowListKalaMulti]
	@language nvarchar(10)
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    create table #tblTemp
	(
	fldCodeKala bigint,
	fldNameKala nvarchar(100),
	fldNameVahed nvarchar(100),
	fldNameVahed2 nvarchar(100),
	fldFeeForoosh decimal(18,4),
	fldCodeGroup bigint
	)

	insert into #tblTemp
		(fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldFeeForoosh,fldCodeGroup)
		SELECT        dbo.tblKala.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2, dbo.tblKala.fldFeeForoosh, dbo.tblKala.fldCodeGroup
			FROM            dbo.tblKala INNER JOIN
									 dbo.tblVahed ON dbo.tblKala.fldCodeVahed1 = dbo.tblVahed.ID INNER JOIN
									 dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID

	 update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language

	 select fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldFeeForoosh,fldCodeGroup from #tbltemp
END

GO
/****** Object:  StoredProcedure [dbo].[ShowListMojodi_Ebteda_Doreh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowListMojodi_Ebteda_Doreh]
	--@fldShomareSanad bigint,
	@language nvarchar(10),
	@fldtype nvarchar(50),
	@Result nvarchar(20) output
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	
	SET NOCOUNT ON;

create table #tblTemp(
 fldNameAnbar nvarchar(100),
 fldShomareSanad bigint,
 fldNameKala nvarchar(100),
 fldRadif bigint,
 fldCodeAnbar bigint, 
 fldCodeKala bigint,
 fldSharhKala nvarchar(100), 
 fldTedadJoz decimal(18,4),
 fldFeeJoz decimal(18,4), 
 fldFeeKol decimal(18,4), 
 fldTedadCarton nvarchar(100), 
 fldType nvarchar(100),
 fldDate bigint,
 fldTime nvarchar(30),
 fldDateEngheza bigint
)

insert into #tblTemp
 (fldNameAnbar,fldShomareSanad,fldNameKala,fldRadif,fldCodeAnbar,fldCodeKala,fldSharhKala,fldTedadJoz,fldFeeJoz,fldFeeKol,fldTedadCarton,fldType,fldDate,fldTime,fldDateEngheza)
SELECT                   dbo.tblAnbar.fldNameAnbar, dbo.tblRizeKharid.fldShomareSanad, dbo.tblKala.fldNameKala,
                         dbo.tblRizeKharid.fldRadif, dbo.tblRizeKharid.fldCodeAnbar, 
                         dbo.tblRizeKharid.fldCodeKala, dbo.tblRizeKharid.fldSharhKala, 
						 dbo.tblRizeKharid.fldTedadJoz, dbo.tblRizeKharid.fldFeeJoz, dbo.tblRizeKharid.fldFeeKol, 
                         dbo.tblRizeKharid.fldTedadCarton, dbo.tblRizeKharid.fldType, dbo.tblRizeKharid.fldDate, dbo.tblRizeKharid.fldTime,tblRizeKharid.fldDateEngheza
FROM          dbo.tblAnbar INNER JOIN
                         dbo.tblRizeKharid ON dbo.tblAnbar.fldCode = dbo.tblRizeKharid.fldCodeAnbar INNER JOIN
                         dbo.tblKala ON dbo.tblRizeKharid.fldCodeKala = dbo.tblKala.fldCodeKala
where          tblRizeKharid.fldType='mojodi_ebteda_doreh' 
--where          tblRizeKharid.fldType='mojodi_ebteda_doreh'  And fldShomareSanad=@fldShomareSanad

update #tblTemp set #tblTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language


select fldNameAnbar,fldShomareSanad,fldNameKala,fldRadif,fldCodeAnbar,fldCodeKala,fldSharhKala,fldTedadJoz,fldFeeJoz,fldFeeKol,fldTedadCarton,fldType,fldDate,fldTime,fldDateEngheza from #tblTemp
END

GO
/****** Object:  StoredProcedure [dbo].[ShowListMojoodieAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListMojoodieAnbar]
--چندزبانه
  @language nvarchar(10),
  @userCode bigint,
  @MiangineKharid bigint output,
  @JameTedad decimal(18, 4) output,
  @lngSumTedadeKolSahih decimal(18, 4) output,
  @Order int,
  @Filter int,
  @ListCodeKala varchar(5000)=null,
  @ListCodeAnbar varchar(5000)=null,
  @ListCodeAnbarKhales ZT_CodeNumbers readonly,
  @ListGroupKalaKhales ZT_CodeNumbers readonly,
  @ListGroupKala varchar(5000)=null,
  @DateTa bigint,
  @NoeFeeKharid int,
  @NoeAnbar nvarchar(20)
--@nn nvarchar(10)

as

IF 1=0 BEGIN
 SET FMTONLY OFF
END

begin
 

create table #TempMojoodieAnbar(

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 4),
fldTedadeKhorooj decimal(18, 4),
fldTedadeJoz decimal(18, 4), 
fldTedadeKol decimal(18, 4), 
fldTedadDarKarton decimal(18, 4),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 4),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20) COLLATE Persian_100_CI_AS
)


create table #TempShowMojoodi(

fldCodeKala bigint,
SumVorood decimal(18, 4),
SumVoroodT nvarchar(200),
SumKhorooj decimal(18, 4),
SumKhoroojT nvarchar(200),
SumMande decimal(18, 4),
Mande nvarchar(200),
fldTedadDarKarton decimal(18, 4),
fldTedadKartonadadSahih decimal(18, 4),
fldTedadDarKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadVoroodKarton decimal(18, 4),
fldTedadVoroodKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadKhoroojKarton decimal(18, 4),
fldTedadKhoroojKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh decimal(18, 4),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldKharid decimal(18, 4),
fldArzesheMojoodi decimal(18, 4),
fldFeeForoosh decimal(18, 4)
)



declare @countGroup bigint

update tblRizeKharid set fldFeeKolBadAzTakhfif=fldFeeVahed1BadAzTakhfif*fldTedadJoz where  fldType='mojodi_ebteda_doreh'

set @countGroup=0--( select count(CodeNumber) from @ListGroupKalaKhales)
if @countGroup >0
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
							 fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeKharid Where flddate<=@DateTa  and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales) and fldCodeKala in (select fldcodekala from tblKala where fldCodeGroup in (select CodeNumber from @ListGroupKalaKhales) )
else
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
							 fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeKharid Where flddate<=@DateTa   and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales)

--else
--		INSERT INTO #TempMojoodieAnbar
--							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
--							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
--	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
--							 fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
--	FROM            tblRizeKharid Where flddate<=@DateTa  and fldCodeKala in (select fldcodekala from tblKala where fldCodeGroup in (@ListGroupKalaKhales) )

if @countGroup >0
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJozNahaei, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
							 fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeTolid where fldType='mahsool' and flddate<=@DateTa  and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales) and fldCodeKala in (select fldcodekala from tblKala where fldCodeGroup in (select CodeNumber from @ListGroupKalaKhales) )
else
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJozNahaei, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
							 fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeTolid where fldType='mahsool' and flddate<=@DateTa  and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales)


if @countGroup >0
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
							 fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeBargashtAzForoosh Where flddate<=@DateTa  and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales) and fldCodeKala in (select fldcodekala from tblKala where fldCodeGroup in (select CodeNumber from @ListGroupKalaKhales) )
else
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
							 fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeBargashtAzForoosh Where flddate<=@DateTa  and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales)


if @countGroup >0
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
							 0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeForoosh Where flddate<=@DateTa  and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales) and fldCodeKala in (select fldcodekala from tblKala where fldCodeGroup in (select CodeNumber from @ListGroupKalaKhales) )
else
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
							 0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeForoosh Where flddate<=@DateTa   and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales)

if @countGroup >0
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJozAvalie , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
							 0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeTolid where fldType='avalie' and flddate<=@DateTa  and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales)   and fldCodeKala in (select fldcodekala from tblKala where fldCodeGroup in (select CodeNumber from @ListGroupKalaKhales) )
else
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJozAvalie , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
							 0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeTolid where fldType='avalie' and flddate<=@DateTa  and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales)


if @countGroup >0
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
							 0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeBargashtAzKharid Where flddate<=@DateTa  and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales)  and fldCodeKala in (select fldcodekala from tblKala where fldCodeGroup in (select CodeNumber from @ListGroupKalaKhales) )
else
	INSERT INTO #TempMojoodieAnbar
							 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
							 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
	SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
							 0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
	FROM            tblRizeBargashtAzKharid Where flddate<=@DateTa  and fldCodeAnbar in (Select CodeNumber  from @ListCodeAnbarKhales)


update #TempMojoodieAnbar set fldFeeForoosh=tblKala.fldFeeForoosh from tblKala Where #TempMojoodieAnbar.fldCodeKala=tblKala.fldCodeKala


update #TempMojoodieAnbar set fldFeeForoosh=tblKala.fldFeeForoosh from tblKala Where #TempMojoodieAnbar.fldCodeKala=tblKala.fldCodeKala

Insert into #TempMojoodieAnbar
	(fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
		fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',fldCodeGroup, fldCodeKala, fldNameKala ,0,0 , - 1 , - 1 , 0, '' , fldNogheSefaresh, fldFeeForoosh, 
                         0, 0, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,0,''
FROM            tblKala

	--where
	--fldCodeKala  not In (Select fldCodeKala from #TempMojoodieAnbar)



if @NoeAnbar='amani'
	delete #TempMojoodieAnbar where fldCodeAnbar<2000
else
	delete #TempMojoodieAnbar where fldCodeAnbar>=2000


UPDATE       #TempMojoodieAnbar
SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
FROM            tblKala INNER JOIN
                         #TempMojoodieAnbar ON tblKala.fldCodeKala = #TempMojoodieAnbar.fldCodeKala


UPDATE       #TempMojoodieAnbar
SET                #TempMojoodieAnbar.fldGroupName = tblGroupKala.fldGroupName
FROM            tblGroupKala INNER JOIN
                         #TempMojoodieAnbar ON #TempMojoodieAnbar.fldCodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempMojoodieAnbar
SET                fldNameAnbar = tblAnbar.fldNameAnbar
FROM            tblAnbar INNER JOIN
                         #TempMojoodieAnbar ON tblAnbar.fldCode = #TempMojoodieAnbar.fldCodeAnbar


update #tempMojoodieAnbar set #tempMojoodieAnbar.fldNameVahed=tbl2.fldNameVahed,#tempMojoodieAnbar.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

where tbl2.fldCodeKala=#tempMojoodieAnbar.fldCodeKala


if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempMojoodieAnbar 
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


--فیلتر تا قبل از تاریخ 



Insert into #TempShowMojoodi
	(SumVorood,SumKhorooj,SumMande,fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,fldKharid,fldFeeForoosh)
SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, SUM(fldTedadeVorood - fldTedadeKhorooj) AS sumMande, 
                         fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,0,fldFeeForoosh
FROM            #TempMojoodieAnbar
GROUP BY fldCodeKala, fldGroupName, fldCodeGroup, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2, fldTedadDarKarton, fldTedadeKol,fldFeeForoosh



update #TempShowMojoodi set Mande=SumMande,SumVoroodT=SumVorood,SumKhoroojT=SumKhorooj
update #TempShowMojoodi set Mande=LEFT(Convert(varchar,SumMande),len(Convert(varchar,SumMande))-3) where right(convert(varchar,SumMande),2)='00'
update #TempShowMojoodi set SumVoroodT=LEFT(Convert(varchar,SumVorood),len(Convert(varchar,SumVorood))-3) where right(convert(varchar,SumVorood),2)='00'
update #TempShowMojoodi set SumKhoroojT=LEFT(Convert(varchar,SumKhorooj),len(Convert(varchar,SumKhorooj))-3) where right(convert(varchar,SumKhorooj),2)='00'

--update #TempShowMojoodi set fldTedadDarKarton=1
--	where fldTedadDarKarton=0

--تعداد کارتن و تعداد جز در آن
update #TempShowMojoodi set fldNameVahed2='' where fldTedadDarKarton=0

update #TempShowMojoodi Set fldTedadDarKartonShow=Convert(varchar,floor(SumMande / fldTedadDarKarton)) + ' ' + fldNameVahed2 + REPLACE(CONVERT(VARCHAR, CONVERT(decimal(18,4), Convert(varchar, { fn MOD(Convert(decimal(18,4), SumMande), fldTedadDarKarton) } )), 2), '.0000', '') + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadVoroodKartonShow=Convert(varchar,  floor(SumVorood / fldTedadDarKarton)) + ' ' + fldNameVahed2 + REPLACE(CONVERT(VARCHAR, CONVERT(decimal(18,4),  Convert(varchar, { fn MOD(Convert(decimal(18,4),SumVorood), fldTedadDarKarton) } )), 2), '.0000', '') + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadKhoroojKartonShow=Convert(varchar,floor(SumKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 + REPLACE(CONVERT(VARCHAR, CONVERT(decimal(18,4),  Convert(varchar, { fn MOD(Convert(decimal(18,4),Sumkhorooj), fldTedadDarKarton) } )), 2), '.0000', '')  + ' ' + fldNameVahed
	where fldTedadDarKarton>0


set @MiangineKharid=0


--SELECT REPLACE(CONVERT(VARCHAR, CONVERT(decimal(18,4),  Convert(varchar, { fn MOD(Convert(decimal(18,4),Sumkhorooj), fldTedadDarKarton) } )), 2), '.0000', '') ,fldcodekala
--FROM tblkala

 

--SELECT  ,fldcodekala
--FROM tblkala

--SELECT REPLACE(CONVERT(VARCHAR, CONVERT(decimal(18,4), fldFeeForoosh), 2), '.0000', '') ,fldcodekala
--FROM tblkala


--SELECT fldFeeForoosh,fldcodekala
--FROM tblkala
--update #TempShowMojoodi set fldKharid=tbl1.Miangin from
--(SELECT        ROUND(SUM(fldFeeKol) / SUM(fldTedadJoz), 0) AS Miangin, fldCodeKala
--FROM            tblRizeKharid
----WHERE        (fldShomareFactor > 0)
--GROUP BY fldCodeKala) as tbl1
--where tbl1.fldCodeKala=#TempShowMojoodi.fldCodeKala

if @NoeFeeKharid=1 --میانگین خرید
	update #TempShowMojoodi set fldKharid=tbl1.Miangin from
	(SELECT        ROUND(SUM(fldFeeKolBadAzTakhfif) / SUM(fldTedadJoz), 4) AS Miangin, fldCodeKala
	FROM            tblRizeKharid
	where fldType<>'enteghal'
	GROUP BY fldCodeKala
	having         SUM(fldTedadJoz)>0 
	) as tbl1
	where tbl1.fldCodeKala=#TempShowMojoodi.fldCodeKala  
else if @NoeFeeKharid=2 --آخرین فی خرید
	begin
	--update #TempShowMojoodi set fldKharid=ROUND((tblRizeKharid.fldFeeKol) / (tblRizeKharid.fldTedadJoz), 0) from
	--	tblRizeKharid where  tblRizeKharid.fldCodeKala=#TempShowMojoodi.fldCodeKala 
	
		DECLARE @t TABLE(
		UniqueID INT,
		fldCodeKala bigint,
		[Fee] decimal(18,4),
		fldDate BIGINT
		);

		insert into @t
			(fldCodeKala,[Fee],fldDate)
			Select fldCodeKala, ROUND((tblRizeKharid.fldFeeKol) / (tblRizeKharid.fldTedadJoz), 4),fldDATE from tblRizeKharid where fldType<>'enteghal' and tblRizeKharid.fldTedadJoz>0


		update #TempShowMojoodi set fldKharid=tbl1.[Fee] from 
			(SELECT t.fldCodeKala,MAX(t.[Fee]) AS [Fee],t.fldDate
			FROM @t t
			INNER JOIN(
				SELECT MAX(fldDate) AS fldDate,fldCodeKala
				FROM @t t
				GROUP BY fldCodeKala
			) AS t2
			ON t.[fldDate] = t2.[fldDate] 
				AND t.[fldCodeKala] = t2.fldCodeKala
			GROUP BY 
				t.fldCodeKala,
				t.[fldDate])as tbl1
			Where #TempShowMojoodi.fldCodeKala=tbl1.fldCodeKala

	end
else if @NoeFeeKharid=3 --في فروش
	begin
		update #TempShowMojoodi set fldKharid=fldFeeForoosh
	end	



--پاک کردن گروه کالاهایی که در گزارش نیستن
if @ListGroupKala<>''
		   begin
		     declare @StrDeleteGroupKala varchar(5000)
		       set  @StrDeleteGroupKala='delete from #TempShowMojoodi 
		            where '+ @ListGroupKala + ''
					execute(@StrDeleteGroupKala)
					end
--پاک کردن کالاهایی که در گزارش نیستن
if @ListCodeKala<>''
		  begin
		   declare @StrDeleteKala varchar(5000)
		    set  @StrDeleteKala='delete from #TempShowMojoodi 
		            where fldCodeKala NOT IN ('+ @ListCodeKala +')'
					execute(@StrDeleteKala)
		  end
--نقطه سفارش
if @Filter=1 	
	begin
		delete from #TempShowMojoodi Where fldNogheSefaresh < SumMande 
		delete from #TempShowMojoodi Where fldNogheSefaresh = 0 
	end
--موجودی مثبت
if @Filter=2
		delete from #TempShowMojoodi Where  SumMande <=0
		

--موجودی منفی
if @Filter=3
		delete from #TempShowMojoodi Where  SumMande >=0

--موجودی صفر
if @Filter=4
		delete from #TempShowMojoodi Where  SumMande <>0





			--if @ListGroupAshkhas<>''
		 --     begin
		 --      declare @StrDeleteGroupAshkhas varchar(5000)
		 --       set  @StrDeleteGroupAshkhas='delete from #TempRizeForoosh 
		 --           where '+ @ListGroupAshkhas + ''
			--		execute(@StrDeleteGroupAshkhas)
		 --     end


 declare @StrOrder nvarchar(50)
 declare @Select nvarchar(500)


  delete #TempShowMojoodi where fldNameKala='NameKala'
  
  -- Group Kala access
 Declare @value nvarchar(100)
  set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPKALA')
  if @value = 'True'
  Begin -- Limitation Active  
	-- Get All AccessKala
	declare @accesskala table(code bigint)
	insert into @accesskala 
	exec [ZSP_GetKalaAccesses] @userCode

	delete #TempShowMojoodi where fldCodeGroup not in (select code from @accesskala)
end

update #TempShowMojoodi set #TempShowMojoodi.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#TempShowMojoodi.fldCodeKala and tblkalatranslate.fldLanguage=@Language

 Set @Select='Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj, SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
	fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid,fldArzesheMojoodi,fldFeeForoosh from #TempShowMojoodi'

--Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
	--fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid ,@strorder
	--from #TempShowMojoodi

--محاسبه ارزش موجودی هر کالا
update #TempShowMojoodi set fldArzesheMojoodi=0
update #TempShowMojoodi set fldArzesheMojoodi=SumMande*fldKharid where SumMande>0

--محاسبه میانگین خرید هرکالا
set @MiangineKharid= (Select Sum(fldKharid*SumMande) as SumMiangin from #TempShowMojoodi Where SumMande>0)


if   @MiangineKharid is null
	set @MiangineKharid=0

	

	


update #TempShowMojoodi set fldTedadKartonadadSahih=(SumMande/fldTedadDarKarton)   Where fldTedadDarKarton>0
update #TempShowMojoodi set fldTedadKartonadadSahih=0 Where isnumeric(fldTedadKartonadadSahih)=0
set @lngSumTedadeKolSahih=(Select Sum(fldTedadKartonadadSahih) from #TempShowMojoodi)

Set @JameTedad=(Select Sum(SumMande)  from #TempShowMojoodi Where SumMande>0)
if   @JameTedad is null
	set @JameTedad=0

if @Order=0 
	set @StrOrder=' order by fldCodeKala'
else if @order=1 	
	set @StrOrder=' order by fldNameKala'
else if @order=2 
	set @StrOrder=' order by Convert(decimal(18,4),SumMande) desc'
else if @order=3 
	set @StrOrder=' order by Convert(decimal(18,4),SumMande) asc'
else if @order=4 
	set @StrOrder=' order by fldKharid desc'	
else if @order=5 
	set @StrOrder=' order by fldKharid asc'
else if @Order=6
	set @StrOrder=' order by fldGroupName desc'
else if @Order=7
	set @StrOrder=' order by fldCodeGroup asc'



execute(@select + @StrOrder)

	 

end

GO
/****** Object:  StoredProcedure [dbo].[ShowListMojoodieAnbarAll]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[ShowListMojoodieAnbarAll]

  @MiangineKharid  nvarchar(10) output,
  @JameTedad bigint output,
  @Order int,
  @Filter int
--@nn nvarchar(10)

as

begin
 

create table #TempMojoodieAnbar(

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 0),
fldTedadeKhorooj decimal(18, 0),
fldTedadeJoz decimal(18, 0), 
fldTedadeKol decimal(18, 0), 
fldTedadDarKarton decimal(18, 0),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 0),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20) COLLATE Persian_100_CI_AS
)


create table #TempShowMojoodi(

fldCodeKala bigint,
SumVorood decimal(18, 0),
SumKhorooj decimal(18, 0),
SumMande decimal(18, 0),
fldTedadDarKarton decimal(18, 0),
fldTedadDarKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadVoroodKarton decimal(18, 0),
fldTedadVoroodKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadKhoroojKarton decimal(18, 0),
fldTedadKhoroojKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh decimal(18, 0),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldKharid bigint,
fldArzesheMojoodi bigint
)



INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeKharid

INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzForoosh


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeForoosh

INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzKharid


UPDATE       #TempMojoodieAnbar
SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
FROM            tblKala INNER JOIN
                         #TempMojoodieAnbar ON tblKala.fldCodeKala = #TempMojoodieAnbar.fldCodeKala


UPDATE       #TempMojoodieAnbar
SET                #TempMojoodieAnbar.fldGroupName = tblGroupKala.fldGroupName
FROM            tblGroupKala INNER JOIN
                         #TempMojoodieAnbar ON #TempMojoodieAnbar.fldCodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempMojoodieAnbar
SET                fldNameAnbar = tblAnbar.fldNameAnbar
FROM            tblAnbar INNER JOIN
                         #TempMojoodieAnbar ON tblAnbar.fldCode = #TempMojoodieAnbar.fldCodeAnbar


update #tempMojoodieAnbar set #tempMojoodieAnbar.fldNameVahed=tbl2.fldNameVahed,#tempMojoodieAnbar.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

where tbl2.fldCodeKala=#tempMojoodieAnbar.fldCodeKala




--فیلتر تا قبل از تاریخ 



Insert into #TempShowMojoodi
	(SumVorood,SumKhorooj,SumMande,fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,fldKharid)
SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, SUM(fldTedadeVorood - fldTedadeKhorooj) AS sumMande, 
                         fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,0
FROM            #TempMojoodieAnbar
GROUP BY fldCodeKala, fldGroupName, fldCodeGroup, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2, fldTedadDarKarton, fldTedadeKol


--update #TempShowMojoodi set fldTedadDarKarton=1
--	where fldTedadDarKarton=0

--تعداد کارتن و تعداد جز در آن
update #TempShowMojoodi set fldNameVahed2='' where fldTedadDarKarton=0

update #TempShowMojoodi Set fldTedadDarKartonShow=Convert(varchar,floor(SumMande / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(SumMande, fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadVoroodKartonShow=Convert(varchar,floor(SumVorood / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(SumVorood, fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadKhoroojKartonShow=Convert(varchar,floor(SumKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Sumkhorooj, fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0


set @MiangineKharid=0


update #TempShowMojoodi set fldKharid=tbl1.Miangin from
(SELECT        ROUND(SUM(fldFeeKol) / SUM(fldTedadJoz), 0) AS Miangin, fldCodeKala
FROM            tblRizeKharid
WHERE        (fldShomareFactor > 0)
GROUP BY fldCodeKala) as tbl1
where tbl1.fldCodeKala=#TempShowMojoodi.fldCodeKala


--نقطه سفارش
if @Filter=1 	
	begin
		delete from #TempShowMojoodi Where fldNogheSefaresh < SumMande 
		delete from #TempShowMojoodi Where fldNogheSefaresh = 0 
	end
--موجودی مثبت
if @Filter=2
		delete from #TempShowMojoodi Where  SumMande <=0
		

--موجودی منفی
if @Filter=3
		delete from #TempShowMojoodi Where  SumMande >=0

--موجودی صفر
if @Filter=4
		delete from #TempShowMojoodi Where  SumMande <>0


--محاسبه ارزش موجودی هر کالا
update #TempShowMojoodi set fldArzesheMojoodi=0
update #TempShowMojoodi set fldArzesheMojoodi=SumMande*fldKharid where SumMande>0

--محاسبه میانگین خرید هرکالا
set @MiangineKharid= (Select Sum(fldKharid*SumMande) as SumMiangin from #TempShowMojoodi Where SumMande>0)


if   @MiangineKharid is null
	set @MiangineKharid=0

	

	

Set @JameTedad=(Select Sum(SumMande)  from #TempShowMojoodi Where SumMande>0)

if   @JameTedad is null
	set @JameTedad=0

			--if @ListGroupAshkhas<>''
		 --     begin
		 --      declare @StrDeleteGroupAshkhas varchar(5000)
		 --       set  @StrDeleteGroupAshkhas='delete from #TempRizeForoosh 
		 --           where '+ @ListGroupAshkhas + ''
			--		execute(@StrDeleteGroupAshkhas)
		 --     end


 declare @StrOrder nvarchar(50)
 declare @Select nvarchar(500)



 Set @Select='Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
	fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid,fldArzesheMojoodi from #TempShowMojoodi '

--Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
	--fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid ,@strorder
	--from #TempShowMojoodi

if @Order=0 
	set @StrOrder=' order by fldCodeKala'
else if @order=1 	
	set @StrOrder=' order by fldNameKala'
else if @order=2 
	set @StrOrder=' order by SumMande desc'
else if @order=3 
	set @StrOrder=' order by SumMande asc'
else if @order=4 
	set @StrOrder=' order by fldKharid desc'	
else if @order=5 
	set @StrOrder=' order by fldKharid asc'
else if @Order=6
	set @StrOrder=' order by fldGroupName desc'
else if @Order=7
	set @StrOrder=' order by fldCodeGroup asc'

execute(@select + @StrOrder)

	 

end

GO
/****** Object:  StoredProcedure [dbo].[ShowListMojoodieAnbarDateEngheza]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListMojoodieAnbarDateEngheza]
--چندزبانه
  @language nvarchar(10),
  @userCode bigint,
  @MiangineKharid decimal(18,4) output,
  @JameTedad decimal(18,4) output,
  @lngSumTedadeKolSahih decimal(18,4) output,
  @Order int,
  @Filter int,
  @ListCodeKala varchar(5000)=null,
  @ListCodeAnbar varchar(5000)=null,
  @ListGroupKala varchar(5000)=null,
  @DateTa bigint,
  @NoeFeeKharid int
--@nn nvarchar(10)

as

IF 1=0 BEGIN
 SET FMTONLY OFF
END

begin
 

create table #TempMojoodieAnbar(

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 4),
fldTedadeKhorooj decimal(18, 4),
fldTedadeJoz decimal(18, 4), 
fldTedadeKol decimal(18, 4), 
fldTedadDarKarton decimal(18, 4),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 4),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20) COLLATE Persian_100_CI_AS,
fldDateEngheza bigint
)


create table #TempShowMojoodi(

fldCodeKala bigint,
SumVorood decimal(18,4),
SumKhorooj decimal(18,4),
SumMande decimal(18,4),
fldTedadDarKarton decimal(18,4),
fldTedadKartonadadSahih decimal(18,4),
fldTedadDarKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadVoroodKarton decimal(18,4),
fldTedadVoroodKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadKhoroojKarton decimal(18,4),
fldTedadKhoroojKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh decimal(18,4),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldKharid decimal(18,4),
fldArzesheMojoodi decimal(18,4),
fldFeeForoosh decimal(18,4),
fldDateEngheza bigint
)



INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType,fldDateEngheza)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType,fldDateEngheza
FROM            tblRizeKharid Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJozNahaei, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeTolid where fldType='mahsool' and flddate<=@DateTa



INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType,fldDateEngheza)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType,fldDateEngheza
FROM            tblRizeBargashtAzForoosh Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType,fldDateEngheza)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType,fldDateEngheza
FROM            tblRizeForoosh Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJozAvalie , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeTolid where fldType='avalie' and flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType,fldDateEngheza)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType,fldDateEngheza
FROM            tblRizeBargashtAzKharid Where flddate<=@DateTa


update #TempMojoodieAnbar set fldFeeForoosh=tblKala.fldFeeForoosh from tblKala Where #TempMojoodieAnbar.fldCodeKala=tblKala.fldCodeKala


Insert into #TempMojoodieAnbar
	(fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
		fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',fldCodeGroup, fldCodeKala, fldNameKala ,0,0 , - 1 , - 1 , 0, '' , fldNogheSefaresh, fldFeeForoosh, 
                         0, 0, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,0,''
FROM            tblKala
	where fldCodeKala Not In (Select fldCodeKala from #TempMojoodieAnbar)


update #TempMojoodieAnbar set fldDateEngheza=0 Where fldDateEngheza is Null

UPDATE       #TempMojoodieAnbar
SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
FROM            tblKala INNER JOIN
                         #TempMojoodieAnbar ON tblKala.fldCodeKala = #TempMojoodieAnbar.fldCodeKala


UPDATE       #TempMojoodieAnbar
SET                #TempMojoodieAnbar.fldGroupName = tblGroupKala.fldGroupName
FROM            tblGroupKala INNER JOIN
                         #TempMojoodieAnbar ON #TempMojoodieAnbar.fldCodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempMojoodieAnbar
SET                fldNameAnbar = tblAnbar.fldNameAnbar
FROM            tblAnbar INNER JOIN
                         #TempMojoodieAnbar ON tblAnbar.fldCode = #TempMojoodieAnbar.fldCodeAnbar


update #tempMojoodieAnbar set #tempMojoodieAnbar.fldNameVahed=tbl2.fldNameVahed,#tempMojoodieAnbar.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

where tbl2.fldCodeKala=#tempMojoodieAnbar.fldCodeKala


if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempMojoodieAnbar 
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


--فیلتر تا قبل از تاریخ 



Insert into #TempShowMojoodi
	(SumVorood,SumKhorooj,SumMande,fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,fldKharid,fldFeeForoosh,fldDateEngheza)
SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, SUM(fldTedadeVorood - fldTedadeKhorooj) AS sumMande, 
                         fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,0,fldFeeForoosh,fldDateEngheza
FROM            #TempMojoodieAnbar
GROUP BY fldCodeKala, fldGroupName, fldCodeGroup, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2, fldTedadDarKarton, fldTedadeKol,fldFeeForoosh,fldDateEngheza





--update #TempShowMojoodi set fldTedadDarKarton=1
--	where fldTedadDarKarton=0

--تعداد کارتن و تعداد جز در آن
update #TempShowMojoodi set fldNameVahed2='' where fldTedadDarKarton=0

update #TempShowMojoodi Set fldTedadDarKartonShow=Convert(varchar,floor(SumMande / fldTedadDarKarton)) + ' ' + fldNameVahed2 + REPLACE(CONVERT(VARCHAR, CONVERT(decimal(18,4), Convert(varchar, { fn MOD(Convert(decimal(18,4), SumMande), fldTedadDarKarton) } )), 2), '.0000', '') + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadVoroodKartonShow=Convert(varchar,  floor(SumVorood / fldTedadDarKarton)) + ' ' + fldNameVahed2 + REPLACE(CONVERT(VARCHAR, CONVERT(decimal(18,4),  Convert(varchar, { fn MOD(Convert(decimal(18,4),SumVorood), fldTedadDarKarton) } )), 2), '.0000', '') + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadKhoroojKartonShow=Convert(varchar,floor(SumKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 + REPLACE(CONVERT(VARCHAR, CONVERT(decimal(18,4),  Convert(varchar, { fn MOD(Convert(decimal(18,4),Sumkhorooj), fldTedadDarKarton) } )), 2), '.0000', '')  + ' ' + fldNameVahed
	where fldTedadDarKarton>0


set @MiangineKharid=0


--update #TempShowMojoodi set fldKharid=tbl1.Miangin from
--(SELECT        ROUND(SUM(fldFeeKol) / SUM(fldTedadJoz), 0) AS Miangin, fldCodeKala
--FROM            tblRizeKharid
----WHERE        (fldShomareFactor > 0)
--GROUP BY fldCodeKala) as tbl1
--where tbl1.fldCodeKala=#TempShowMojoodi.fldCodeKala

if @NoeFeeKharid=1 --میانگین خرید
	update #TempShowMojoodi set fldKharid=tbl1.Miangin from
	(SELECT        ROUND(SUM(fldFeeKol) / SUM(fldTedadJoz), 4) AS Miangin, fldCodeKala
	FROM            tblRizeKharid
	GROUP BY fldCodeKala
	having         SUM(fldTedadJoz)>0
	) as tbl1
	where tbl1.fldCodeKala=#TempShowMojoodi.fldCodeKala 
else --آخرین فی خرید
	begin
	--update #TempShowMojoodi set fldKharid=ROUND((tblRizeKharid.fldFeeKol) / (tblRizeKharid.fldTedadJoz), 0) from
	--	tblRizeKharid where  tblRizeKharid.fldCodeKala=#TempShowMojoodi.fldCodeKala 
	
		DECLARE @t TABLE(
		UniqueID INT,
		fldCodeKala bigint,
		[Fee] decimal(18,4),
		fldDate BIGINT
		);

		insert into @t
			(fldCodeKala,[Fee],fldDate)
			Select fldCodeKala, ROUND((tblRizeKharid.fldFeeKol) / (tblRizeKharid.fldTedadJoz), 4),fldDATE from tblRizeKharid


		update #TempShowMojoodi set fldKharid=tbl1.[Fee] from 
			(SELECT t.fldCodeKala,MAX(t.[Fee]) AS [Fee],t.fldDate
			FROM @t t
			INNER JOIN(
				SELECT MAX(fldDate) AS fldDate,fldCodeKala
				FROM @t t
				GROUP BY fldCodeKala
			) AS t2
			ON t.[fldDate] = t2.[fldDate] 
				AND t.[fldCodeKala] = t2.fldCodeKala
			GROUP BY 
				t.fldCodeKala,
				t.[fldDate])as tbl1
			Where #TempShowMojoodi.fldCodeKala=tbl1.fldCodeKala

	end
	



--پاک کردن گروه کالاهایی که در گزارش نیستن
if @ListGroupKala<>''
		   begin
		     declare @StrDeleteGroupKala varchar(5000)
		       set  @StrDeleteGroupKala='delete from #TempShowMojoodi 
		            where '+ @ListGroupKala + ''
					execute(@StrDeleteGroupKala)
					end
--پاک کردن کالاهایی که در گزارش نیستن
if @ListCodeKala<>''
		  begin
		   declare @StrDeleteKala varchar(5000)
		    set  @StrDeleteKala='delete from #TempShowMojoodi 
		            where fldCodeKala NOT IN ('+ @ListCodeKala +')'
					execute(@StrDeleteKala)
		  end
--نقطه سفارش
if @Filter=1 	
	begin
		delete from #TempShowMojoodi Where fldNogheSefaresh < SumMande 
		delete from #TempShowMojoodi Where fldNogheSefaresh = 0 
	end
--موجودی مثبت
if @Filter=2
		delete from #TempShowMojoodi Where  SumMande <=0
		

--موجودی منفی
if @Filter=3
		delete from #TempShowMojoodi Where  SumMande >=0

--موجودی صفر
if @Filter=4
		delete from #TempShowMojoodi Where  SumMande <>0


--محاسبه ارزش موجودی هر کالا
update #TempShowMojoodi set fldArzesheMojoodi=0
update #TempShowMojoodi set fldArzesheMojoodi=SumMande*fldKharid where SumMande>0

--محاسبه میانگین خرید هرکالا
set @MiangineKharid= (Select Sum(fldKharid*SumMande) as SumMiangin from #TempShowMojoodi Where SumMande>0)


if   @MiangineKharid is null
	set @MiangineKharid=0

	

	

Set @JameTedad=(Select Sum(SumMande)  from #TempShowMojoodi Where SumMande>0)
update #TempShowMojoodi set fldTedadKartonadadSahih=(SumMande/fldTedadDarKarton)   Where fldTedadDarKarton>0
update #TempShowMojoodi set fldTedadKartonadadSahih=0 Where isnumeric(fldTedadKartonadadSahih)=0
set @lngSumTedadeKolSahih=(Select Sum(fldTedadKartonadadSahih) from #TempShowMojoodi)


if   @JameTedad is null
	set @JameTedad=0

			--if @ListGroupAshkhas<>''
		 --     begin
		 --      declare @StrDeleteGroupAshkhas varchar(5000)
		 --       set  @StrDeleteGroupAshkhas='delete from #TempRizeForoosh 
		 --           where '+ @ListGroupAshkhas + ''
			--		execute(@StrDeleteGroupAshkhas)
		 --     end


 declare @StrOrder nvarchar(50)
 declare @Select nvarchar(500)


 -- Delete Not accessable Kala
 
 -- Group Kala access
 Declare @value nvarchar(100)
  set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPKALA')
  if @value = 'True'
  Begin -- Limitation Active  
	-- Get All AccessKala
	declare @accesskala table(code bigint)
	insert into @accesskala 
	exec [ZSP_GetKalaAccesses] @userCode

	delete #TempShowMojoodi where fldCodeGroup not in (select code from @accesskala)
end

update #TempShowMojoodi set #TempShowMojoodi.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#TempShowMojoodi.fldCodeKala and tblkalatranslate.fldLanguage=@Language

 Set @Select='Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
	fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid,fldArzesheMojoodi,fldFeeForoosh,fldDateEngheza from #TempShowMojoodi '

--Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
	--fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid ,@strorder
	--from #TempShowMojoodi

if @Order=0 
	set @StrOrder=' order by fldCodeKala,fldDateEngheza'
else if @order=1 	
	set @StrOrder=' order by fldNameKala,fldDateEngheza'
else if @order=2 
	set @StrOrder=' order by SumMande desc,fldDateEngheza,fldCodeKala'
else if @order=3 
	set @StrOrder=' order by SumMande asc,fldDateEngheza'
else if @order=4 
	set @StrOrder=' order by fldKharid desc,fldDateEngheza,fldCodeKala'	
else if @order=5 
	set @StrOrder=' order by fldKharid asc,fldDateEngheza,fldCodeKala'
else if @Order=6
	set @StrOrder=' order by fldGroupName desc,fldDateEngheza,fldCodeKala'
else if @Order=7
	set @StrOrder=' order by fldCodeGroup asc,fldDateEngheza,fldCodeKala'
else if @Order=8
	set @StrOrder=' order by fldDateEngheza asc,fldCodeKala'
else if @Order=9
	set @StrOrder=' order by fldDateEngheza desc,fldCodeKala'

execute(@select + @StrOrder)

	 

end

GO
/****** Object:  StoredProcedure [dbo].[ShowListMojoodieAnbarForForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListMojoodieAnbarForForoosh]    
--چندزبانه    
--@nn nvarchar(10)
	@language nvarchar(10),
	@ShowMojoodi int

as

IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
 

create table #TempMojoodieAnbar(

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 4),
fldTedadeKhorooj decimal(18, 4),
fldTedadeJoz decimal(18, 4), 
fldTedadeKol decimal(18, 4), 
fldTedadDarKarton decimal(18, 4),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 4),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20) COLLATE Persian_100_CI_AS
)


create table #TempShowMojoodi2(

fldCodeKala bigint,
SumVorood decimal(18, 4),
SumKhorooj decimal(18, 4),
SumMande decimal(18, 4),
Mande nvarchar(18),
mandeeee nvarchar(10),
fldTedadDarKarton decimal(18, 4),
fldTedadDarKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadVoroodKarton decimal(18, 4),
fldTedadVoroodKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadKhoroojKarton decimal(18, 4),
fldTedadKhoroojKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh decimal(18, 4),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldKharid decimal(18, 4),
fldFeeForoosh money,
fldArzesheMojoodi decimal(18, 4)
)

if @ShowMojoodi=1
	begin
		INSERT INTO #TempMojoodieAnbar
								 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
								 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
		SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
								 fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
		FROM            tblRizeKharid
			where fldType<>'amani'


		INSERT INTO #TempMojoodieAnbar
								 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
								 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
		SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJozNahaei, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
								 fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
		FROM            tblRizeTolid where fldType='mahsool'


		INSERT INTO #TempMojoodieAnbar
								 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
								 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
		SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
								 fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
		FROM            tblRizeBargashtAzForoosh


		INSERT INTO #TempMojoodieAnbar
								 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
								 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
		SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
								 0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
		FROM            tblRizeForoosh



		INSERT INTO #TempMojoodieAnbar
								 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
								 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
		SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJozAvalie , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
								 0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
		FROM            tblRizeTolid where fldType='avalie'



		INSERT INTO #TempMojoodieAnbar
								 (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
								 fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
		SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
								 0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
		FROM            tblRizeBargashtAzKharid


		Insert into #TempMojoodieAnbar
			(fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
				fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
		SELECT        'NameGroup',fldCodeGroup, fldCodeKala, fldNameKala ,0,0 , - 1 , - 1 , 0, '' , fldNogheSefaresh, fldFeeForoosh, 
								 0, 0, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,0,''
		FROM            tblKala
			where fldCodeKala Not In (Select fldCodeKala from #TempMojoodieAnbar)



		UPDATE       #TempMojoodieAnbar
		SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
		FROM            tblKala INNER JOIN
								 #TempMojoodieAnbar ON tblKala.fldCodeKala = #TempMojoodieAnbar.fldCodeKala


		UPDATE       #TempMojoodieAnbar
		SET                #TempMojoodieAnbar.fldGroupName = tblGroupKala.fldGroupName
		FROM            tblGroupKala INNER JOIN
								 #TempMojoodieAnbar ON #TempMojoodieAnbar.fldCodeGroup = tblGroupKala.fldGroupId


		UPDATE       #TempMojoodieAnbar
		SET                fldNameAnbar = tblAnbar.fldNameAnbar
		FROM            tblAnbar INNER JOIN
								 #TempMojoodieAnbar ON tblAnbar.fldCode = #TempMojoodieAnbar.fldCodeAnbar


		update #tempMojoodieAnbar set #tempMojoodieAnbar.fldNameVahed=tbl2.fldNameVahed,#tempMojoodieAnbar.fldNameVahed2=tbl2.fldNameVahed2
		from
		(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
		FROM            tblVahed INNER JOIN
									 (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
										FROM            tblKala INNER JOIN
																 tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

		where tbl2.fldCodeKala=#tempMojoodieAnbar.fldCodeKala




		--فیلتر تا قبل از تاریخ 



		Insert into #TempShowMojoodi2
			(SumVorood,SumKhorooj,SumMande,fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,fldKharid)
		SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, SUM(fldTedadeVorood - fldTedadeKhorooj) AS sumMande, 
								 fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,0
		FROM            #TempMojoodieAnbar
		GROUP BY fldCodeKala, fldGroupName, fldCodeGroup, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2, fldTedadDarKarton, fldTedadeKol





		--update #TempShowMojoodi set fldTedadDarKarton=1
		--	where fldTedadDarKarton=0

		--تعداد کارتن و تعداد جز در آن
		update #TempShowMojoodi2 set fldNameVahed2='' where fldTedadDarKarton=0

		update #TempShowMojoodi2 Set fldTedadDarKartonShow=Convert(varchar,floor(SumMande / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, Convert(float,{ fn MOD(SumMande, fldTedadDarKarton) }) ) + ' ' + fldNameVahed
			where fldTedadDarKarton>0

		update #TempShowMojoodi2 Set fldTedadVoroodKartonShow=Convert(varchar,floor(SumVorood / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, Convert(float,{ fn MOD(SumVorood, fldTedadDarKarton) }) ) + ' ' + fldNameVahed
			where fldTedadDarKarton>0

		update #TempShowMojoodi2 Set fldTedadKhoroojKartonShow=Convert(varchar,floor(SumKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar,Convert(float, { fn MOD(Sumkhorooj, fldTedadDarKarton) } )) + ' ' + fldNameVahed
			where fldTedadDarKarton>0





		update #TempShowMojoodi2 set fldKharid=tbl1.Miangin from
		(SELECT        ROUND(SUM(fldFeeKol) / SUM(fldTedadJoz), 0) AS Miangin, fldCodeKala
		FROM            tblRizeKharid
		WHERE        (fldShomareFactor > 0)
		GROUP BY fldCodeKala) as tbl1
		where tbl1.fldCodeKala=#TempShowMojoodi2.fldCodeKala





		--محاسبه ارزش موجودی هر کالا
		update #TempShowMojoodi2 set fldArzesheMojoodi=0
		update #TempShowMojoodi2 set fldArzesheMojoodi=SumMande*fldKharid where SumMande>0



	

	

		update #TempShowMojoodi2 set fldFeeForoosh=tblKala.fldFeeForoosh from tblkala Where #TempShowMojoodi2.fldCodeKala=tblKala.fldCodeKala



		 declare @StrOrder nvarchar(50)
		 declare @Select nvarchar(500)



		 Set @Select='Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
			fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid,fldArzesheMojoodi from #TempShowMojoodi2 '

		--Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
			--fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid ,@strorder
			--from #TempShowMojoodi

		--execute(@select )

		--select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
		--	fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid,fldArzesheMojoodi,fldFeeForoosh from #TempShowMojoodi	 
		--update #TempShowMojoodi Set fldTedadDarKartonShow='666',SumMande=6547

		update #TempShowMojoodi2 set Mande=SumMande
		update #TempShowMojoodi2 set Mande=LEFT(Convert(varchar,SumMande),len(Convert(varchar,SumMande))-3) where right(convert(varchar,SumMande),2)='00'
	end
else
		Insert into #TempShowMojoodi2
			(fldCodeKala,fldNameKala,fldFeeForoosh,fldNameVahed,fldNameVahed2,SumMande,fldTedadDarKarton, fldCodeGroup, fldTedadDarKartonShow)
			SELECT        dbo.tblKala.fldCodeKala , dbo.tblKala.fldNameKala,fldFeeForoosh, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2,0 as SumMande,0,tblKala.fldCodeGroup,''                         
			FROM            dbo.tblKala INNER JOIN
									 dbo.tblVahed ON dbo.tblKala.fldCodeVahed1 = dbo.tblVahed.ID INNER JOIN
									 dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID

update #TempShowMojoodi2 set #TempShowMojoodi2.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#TempShowMojoodi2.fldCodeKala and tblkalatranslate.fldLanguage=@Language

select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,Mande as SumMande,fldFeeForoosh,fldCodeGroup,fldTedadDarKartonShow from  #TempShowMojoodi2	  Where fldNameKala<>'NameKala'

end

GO
/****** Object:  StoredProcedure [dbo].[ShowListTbl_Tafsili_in_Form_Enteghal_Kala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[ShowListTbl_Tafsili_in_Form_Enteghal_Kala]
as
begin
SELECT DISTINCT fldCodeTafzili, fldSharheTafzili
                
				
FROM            dbo.tblTafzili

end

GO
/****** Object:  StoredProcedure [dbo].[ShowListTbl_Tafsili_in_FormAshkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListTbl_Tafsili_in_FormAshkhas]
as
begin
SELECT DISTINCT fldCodeTafzili, fldCodeGroup, fldSharheTafzili, fldAddress,fldBlackList,
                fldCity,fldCodeEghtesadi,fldCodeMeli,fldCodePosti,fldDate1,fldDate2,
                fldPic,fldOstan,fldShomareHesab,fldShomareSabt,
				fldVisitor,fldSharhBlackList,fldSaghfeEtebar 
				
FROM            dbo.tblTafzili
where fldcodeNoe='1'

end

GO
/****** Object:  StoredProcedure [dbo].[ShowListTbl_Tafsili_in_FormAshkhas_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListTbl_Tafsili_in_FormAshkhas_WithUserCode]
--چندزبانه
@userCode bigint
as
begin

-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################

SELECT DISTINCT fldCodeTafzili, fldCodeGroup, fldSharheTafzili, fldAddress,fldBlackList,
                fldCity,fldCodeEghtesadi,fldCodeMeli,fldCodePosti,fldDate1,fldDate2,
                fldPic,fldOstan,fldShomareHesab,fldShomareSabt,
				fldVisitor,fldSharhBlackList,fldSaghfeEtebar 
				
FROM            dbo.tblTafzili
where 
-- ##################### Group Ashkhas Limitation
fldCodeTafzili in (select code from @accessCodes) and
fldcodeNoe='1'
order by fldCodeTafzili

end
GO
/****** Object:  StoredProcedure [dbo].[ShowListtblDaftarcheTell_Has_CodeTafisili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListtblDaftarcheTell_Has_CodeTafisili]
@codeTafsili nvarchar(10)
as
begin

select fldIdDaftarche,fldCodeTafzili,fldName,fldShomare,fldSms,fldSharh   from tblDaftarcheTell 
where fldCodeTafzili=@codeTafsili
end

GO
/****** Object:  StoredProcedure [dbo].[ShowListtblDaftarcheTell_NotHas_CodeTafisili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowListtblDaftarcheTell_NotHas_CodeTafisili]

as
begin

select fldIdDaftarche,fldCodeTafzili,fldName,fldShomare,fldSms,fldSharh   from tblDaftarcheTell 

end

GO
/****** Object:  StoredProcedure [dbo].[ShowListVahedeArzi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE  PROCEDURE [dbo].[ShowListVahedeArzi]
@language nvarchar(10)
	-- Add the parameters for the stored procedure here	
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	create table #tblTemp(
		fldCodeArz bigint,
		fldName nvarchar(50)
	)

	delete #tblTemp

	insert into #tblTemp
		(fldCodeArz,fldName)
		values (0,(select * from dbo.translateFunction(@language,18503)))

	insert into #tblTemp
		(fldCodeArz,fldName)
		values (1,(select * from dbo.translateFunction(@language,18504)))

	insert into #tblTemp
		(fldCodeArz,fldName)
		Select fldCodeArz,fldname from tblVahedeArzi

	Select fldCodeArz,fldName from #tblTemp order by fldCodeArz
END

GO
/****** Object:  StoredProcedure [dbo].[ShowMojodiAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
CREATE PROCEDURE [dbo].[ShowMojodiAnbar] 
--چندزبانه
@CodeKala bigint,
@CodeAnbar bigint,
@TypeShow nvarchar(40),
@ReturnAllAnbar  decimal(18,4) output,
@ReturnAllKhoroji decimal(18,4) output,
@ReturnAllVorodi decimal(18,4) output

AS
BEGIN
	
	SET NOCOUNT ON;

declare @SumAllForoosh decimal(18,4)
declare @SumAllTolidAvalie decimal(18,4)
declare @SumAllKharid decimal(18,4)
declare @SumAllTolidNahaei decimal(18,4)
declare @SumAllBargashtAzForoosh decimal(18,4)
declare @SumAllBargashtAzKharid decimal(18,4)
declare @MandehMojodi decimal(18,4)
declare @SumAllForoosh2 decimal(18,4)
declare @SumAllTolidAvalie2 decimal(18,4)
declare @SumAllBargashtAzForoosh2 decimal(18,4)
declare @SumAllKharid2 decimal(18,4)
declare @SumAllTolidNahaei2 decimal(18,4)
declare @SumAllBargashtAzKharid2 decimal(18,4)
declare @MandehMojodi2 decimal(18,4)
   
   if(@TypeShow='AllAnbar')
   begin
   set @SumAllForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeForoosh where fldCodeKala=@CodeKala)
   set @SumAllTolidAvalie=(select Isnull(sum(fldTedadJozAvalie),0) from tblRizeTolid where fldCodeKala=@CodeKala)
   set @SumAllBargashtAzForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzForoosh where fldCodeKala=@CodeKala)
   set @SumAllKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeKharid  where fldCodeKala=@CodeKala)
   set @SumAllTolidNahaei=(select Isnull(sum(fldTedadJozNahaei),0) from tblRizeTolid  where fldCodeKala=@CodeKala)
   set @SumAllBargashtAzKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzKharid  where fldCodeKala=@CodeKala)   
   set @MandehMojodi=@SumAllKharid+@SumAllTolidNahaei+@SumAllBargashtAzForoosh - @SumAllForoosh-@SumAllBargashtAzKharid-@SumAllTolidAvalie
   set @ReturnAllAnbar=@MandehMojodi
   Return @ReturnAllAnbar
   end

     
	 if(@TypeShow='AllKhoroji')
	 begin
	 set @SumAllForoosh2=(select Isnull(sum(fldTedadJoz),0) from tblRizeForoosh where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	 set @SumAllTolidAvalie=(select Isnull(sum(fldTedadJozAvalie),0) from tblRizeTolid where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	 set @SumAllBargashtAzForoosh2=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzForoosh where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	 set @SumAllKharid2=(select Isnull(sum(fldTedadJoz),0) from tblRizeKharid where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)	
	 set @SumAllTolidNahaei=(select Isnull(sum(fldTedadJozNahaei),0) from tblRizeTolid where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)	
	 set @SumAllBargashtAzKharid2=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzKharid where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	 set @SumAllForoosh2=@SumAllKharid2+@SumAllBargashtAzForoosh2+@SumAllTolidNahaei - @SumAllForoosh2-@SumAllBargashtAzKharid2-@SumAllTolidAvalie
	 set @ReturnAllKhoroji= @SumAllForoosh2
	 Return @ReturnAllKhoroji
	 end


	 if(@TypeShow='AllVorodi')
	 begin
	set @SumAllKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeKharid  where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	set @SumAllTolidNahaei=(select Isnull(sum(fldTedadJozNahaei),0) from tblRizeTolid  where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	set @SumAllBargashtAzKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzKharid  where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	set @SumAllForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeForoosh where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	set @SumAllTolidAvalie=(select Isnull(sum(fldTedadJozAvalie),0) from tblRizeTolid where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	set @SumAllBargashtAzForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzForoosh where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)

	set @SumAllKharid=@SumAllKharid+@SumAllBargashtAzForoosh+@SumAllTolidNahaei - @SumAllForoosh-@SumAllBargashtAzKharid-@SumAllTolidAvalie
    set	@ReturnAllVorodi=@SumAllKharid
	Return @ReturnAllVorodi
	 end

	  if(@TypeShow='AnbarMakhsos')
	 begin
	   set @SumAllForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeForoosh where fldCodeKala=@CodeKala and fldCodeAnbar=@CodeAnbar)
	   set @SumAllTolidAvalie=(select Isnull(sum(fldTedadJozAvalie),0) from tblRizeTolid where fldCodeKala=@CodeKala and fldCodeAnbar=@CodeAnbar)
	   set @SumAllBargashtAzForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzForoosh where fldCodeKala=@CodeKala and fldCodeAnbar=@CodeAnbar)
	   set @SumAllKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeKharid  where fldCodeKala=@CodeKala and fldCodeAnbar=@CodeAnbar)
	   set @SumAllTolidNahaei=(select Isnull(sum(fldTedadJozNahaei),0) from tblRizeTolid  where fldCodeKala=@CodeKala and fldCodeAnbar=@CodeAnbar)
	   set @SumAllBargashtAzKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzKharid  where fldCodeKala=@CodeKala and fldCodeAnbar=@CodeAnbar)	   
	   set @MandehMojodi=@SumAllKharid+@SumAllBargashtAzForoosh +@SumAllTolidNahaei - @SumAllForoosh-@SumAllBargashtAzKharid-@SumAllTolidAvalie
	   set @ReturnAllAnbar=@MandehMojodi
	   Return @ReturnAllAnbar
	 end
	 
END

GO
/****** Object:  StoredProcedure [dbo].[ShowMojodiAnbarTaTarikh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
CREATE PROCEDURE [dbo].[ShowMojodiAnbarTaTarikh] 
@CodeKala bigint,
@CodeAnbar bigint,
@TypeShow nvarchar(40),
@DateTa bigint,
@ReturnAllAnbar  decimal(16,2) output,
@ReturnAllKhoroji decimal(16,2) output,
@ReturnAllVorodi decimal(16,2) output

AS
BEGIN
	
	SET NOCOUNT ON;

declare @SumAllForoosh decimal(16,2)
declare @SumAllKharid decimal(16,2)
declare @SumAllBargashtAzForoosh decimal(16,2)
declare @SumAllBargashtAzKharid decimal(16,2)
declare @SumAllTolidAvalie decimal(16,2)
declare @SumAllTolidNahaei decimal(16,2)
declare @MandehMojodi decimal(16,2)
declare @SumAllForoosh2 decimal(16,2)
declare @SumAllBargashtAzForoosh2 decimal(16,2)
declare @SumAllKharid2 decimal(16,2)
declare @SumAllBargashtAzKharid2 decimal(16,2)
declare @MandehMojodi2 decimal(16,2)

   
   if(@TypeShow='AllAnbar')
   begin
   set @SumAllForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeForoosh where fldCodeKala=@CodeKala And fldDate<=@DateTa)
   set @SumAllBargashtAzForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzForoosh where fldCodeKala=@CodeKala And fldDate<=@DateTa)
   set @SumAllKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeKharid  where fldCodeKala=@CodeKala And fldDate<=@DateTa)
   set @SumAllBargashtAzKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzKharid  where fldCodeKala=@CodeKala And fldDate<=@DateTa)   

   set @SumAllTolidAvalie=(select Isnull(sum(fldTedadJozAvalie),0) from tblRizeTolid  where fldCodeKala=@CodeKala And fldDate<=@DateTa)  
   set @SumAllTolidNahaei=(select Isnull(sum(fldTedadJozNahaei),0) from tblRizeTolid  where fldCodeKala=@CodeKala And fldDate<=@DateTa)  
    
   set @MandehMojodi=@SumAllKharid+@SumAllBargashtAzForoosh+@SumAllTolidNahaei - @SumAllForoosh-@SumAllBargashtAzKharid-@SumAllTolidAvalie
   set @ReturnAllAnbar=@MandehMojodi
   Return @ReturnAllAnbar
   end

     
	 if(@TypeShow='AllKhoroji')
	 begin
	 set @SumAllForoosh2=(select Isnull(sum(fldTedadJoz),0) from tblRizeForoosh where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	 set @SumAllBargashtAzForoosh2=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzForoosh where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	 set @SumAllKharid2=(select Isnull(sum(fldTedadJoz),0) from tblRizeKharid where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	 set @SumAllBargashtAzKharid2=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzKharid where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	 set @SumAllForoosh2=@SumAllKharid2+@SumAllBargashtAzForoosh2 - @SumAllForoosh2-@SumAllBargashtAzKharid2
	 set @ReturnAllKhoroji= @SumAllForoosh2
	 Return @ReturnAllKhoroji
	 end


	 if(@TypeShow='AllVorodi')
	 begin
	set @SumAllKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeKharid  where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	set @SumAllBargashtAzKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzKharid  where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	set @SumAllForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeForoosh where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)
	set @SumAllBargashtAzForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzForoosh where fldCodeKala=@CodeKala And fldCodeAnbar=@CodeAnbar)

	set @SumAllKharid=@SumAllKharid+@SumAllBargashtAzForoosh - @SumAllForoosh-@SumAllBargashtAzKharid
    set	@ReturnAllVorodi=@SumAllKharid
	Return @ReturnAllVorodi
	 end

	  if(@TypeShow='AnbarMakhsos')
	 begin
	   set @SumAllForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeForoosh where fldCodeKala=@CodeKala and fldCodeAnbar=@CodeAnbar)
	   set @SumAllBargashtAzForoosh=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzForoosh where fldCodeKala=@CodeKala and fldCodeAnbar=@CodeAnbar)
	   set @SumAllKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeKharid  where fldCodeKala=@CodeKala and fldCodeAnbar=@CodeAnbar)
	   set @SumAllBargashtAzKharid=(select Isnull(sum(fldTedadJoz),0) from tblRizeBargashtAzKharid  where fldCodeKala=@CodeKala and fldCodeAnbar=@CodeAnbar)	   
	   set @MandehMojodi=@SumAllKharid+@SumAllBargashtAzForoosh - @SumAllForoosh-@SumAllBargashtAzKharid
	   set @ReturnAllAnbar=@MandehMojodi
	   Return @ReturnAllAnbar
	 end
	 
END

GO
/****** Object:  StoredProcedure [dbo].[ShowPardakhtInfo]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowPardakhtInfo]
--چندزبانه
@ShomarePardakht bigint,

@ShomareSanad nvarchar(50) output,
@DatePardakht int output,
@CodeTafzili nvarchar(10) output,
@SharheKol nvarchar(200) output,

----صندوق
@Naghd decimal(18,4) output,
@CodeMoinSandoogh nvarchar(10) output,
@SNaghd nvarchar(200) output,


----بانک
@Bank decimal(18,4) output,
@CodeMoinBank nvarchar(10) output,

@SBank nvarchar(200) output,

----چک
@MablaghCheck decimal(18,4) output,
@SCheck nvarchar(200) output,

--چک واگذاری مشتریان
@MablagheCheckeVagozari decimal(18,4) output,
@SCheckVagozari nvarchar(200) output,

----تخفیفات
@Takhfif decimal(18,4) output,
@STakhfif nvarchar(200) output,

----واسطه
@CodeVasete nvarchar(10) output,

@NoeKhata nvarchar(30) output 

as

begin
	begin transaction transact
		begin try
		--اطلاعات کلی
		declare @KolSandoogh nvarchar(100),@strMablagh nvarchar(20),@KolBank nvarchar(100),@KolMoinCheck nvarchar(100),@KolMoinTakhfif nvarchar(100)
		set @ShomareSanad=(Select fldShomareSanad from tblPardakht Where fldShomarePardakht=@ShomarePardakht)
		set @DatePardakht=(Select fldDate from tblSodooreSanad Where fldSanad=@ShomareSanad and fldRadif='1')
		set @CodeTafzili=(Select fldCodeTafzili from tblPardakht Where fldShomarePardakht=@ShomarePardakht)
		declare @CodeMoinHesabhayePardakhtani nvarchar(100),@CodeMoinHesab nvarchar(10),@TooleKol bigint,@Toolemoin bigint,@strtype nvarchar(20),@fldHesabDaryaftani bigint
			
		execute  ToolePaye 'kol',@TooleKol output
		execute  ToolePaye 'moin',@Toolemoin output
		execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinHesabhayePardakhtani output


		set @strtype=(Select fldType from tblPardakht Where fldShomarePardakht=@ShomarePardakht)
		if @strtype='bargashtazforoosh'
			begin
				set @fldHesabDaryaftani=(select  fldCodeMoinHesabhayeDaryaftani from tblTanzimat)
				set @SharheKol=(Select fldSharh from tblSodooreSanad Where fldSanad=@ShomareSanad And fldBestankar>0 and fldMoinKamel=@fldHesabDaryaftani)				
			end
		else if @strtype='hazine'
			set @SharheKol=(Select top(1)fldSharh from tblSodooreSanad Where fldSanad=@ShomareSanad And fldBedehkar>0)
		else if @strtype='enteghalevajh'
			set @SharheKol=(Select top(1)fldSharh from tblSodooreSanad Where fldSanad=@ShomareSanad And fldBedehkar>0)
		else 
			set @SharheKol=(Select fldSharh from tblSodooreSanad Where fldSanad=@ShomareSanad And fldBedehkar>0 and fldMoinKamel=@CodeMoinHesabhayePardakhtani)



		set @CodeVasete='0'--(Select isnull(fldVasete,'') from tblSodooreSanad Where fldSanad=@ShomareSanad And fldBedehkar>0)

		--صندوق
		execute CodeKolHesab  'sandoogh', @KolSandoogh output
		set @strMablagh=(Select isnull(fldBestankar,0) from tblSodooreSanad Where fldCodeKol=@kolSandoogh and fldSanad=@ShomareSanad and fldBestankar>0)	
		set @CodeMoinSandoogh =(Select fldMoinKamel from tblSodooreSanad Where fldCodeKol=@kolSandoogh and fldSanad=@ShomareSanad and fldBestankar>0)	
		set @SNaghd=(Select isnull(fldSharh,'') from tblSodooreSanad Where fldCodeKol=@kolSandoogh and fldSanad=@ShomareSanad and fldBestankar>0)	

		if @strMablagh is null
			begin			
				set @naghd=0
				set @CodeMoinSandoogh =''
				set @SNaghd=''
			end
		else
			begin			
				set @naghd=Convert(decimal(18,4),@strMablagh)
			end

		--بانک
		execute CodeKolHesab  'bank', @KolBank output
		set @strMablagh=(Select isnull(fldBestankar,0) from tblSodooreSanad Where fldCodeKol=@KolBank and fldSanad=@ShomareSanad and fldBestankar>0)
		set @CodeMoinBank=(Select fldMoinKamel from tblSodooreSanad Where fldCodeKol=@KolBank and fldSanad=@ShomareSanad and fldBestankar>0)		
		set @SBank=(Select isnull(fldSharh,'') from tblSodooreSanad Where fldCodeKol=@KolBank and fldSanad=@ShomareSanad and fldBestankar>0)		
		if @strMablagh is null
			begin			
				set @bank=0
				set @CodeMoinBank=''
				set @SBank=''
			end
		else
			begin			
				set @bank=Convert(decimal(18,4),@strMablagh)
			end
		--چک پرداختی از خودمان
		execute CodeMoinHesab  'checkpardakhti', @KolMoincheck output
		set @strMablagh=(Select isnull(fldBestankar,0) from tblSodooreSanad Where fldMoinKamel=@KolMoincheck and fldSanad=@ShomareSanad)
		
		--set @CodeMoin=(Select fldMoinKamel from tblSodooreSanad Where fldMoinKamel=@KolMoincheck and fldSanad=@ShomareSanad)		
		set @SCheck=(Select isnull(fldSharh,'') from tblSodooreSanad Where fldMoinKamel=@KolMoincheck and fldSanad=@ShomareSanad)		
		if @strMablagh is null
			begin			
				set @MablaghCheck=0
				--set @CodeMoincheck=''
				set @Scheck=''
			end
		else
			begin			
				set @MablaghCheck=Convert(decimal(18,4),@strMablagh)
			end


		--چک واگذاری مشتریان
		if @strtype='hazine'
			execute CodeMoinHesab  'checkvagozaripersonel', @KolMoincheck output
		else
			execute CodeMoinHesab  'check', @KolMoincheck output


		set @strMablagh=(Select isnull(fldBestankar,0) from tblSodooreSanad Where fldMoinKamel=@KolMoincheck and fldSanad=@ShomareSanad)
		--set @CodeMoin=(Select fldMoinKamel from tblSodooreSanad Where fldMoinKamel=@KolMoincheck and fldSanad=@ShomareSanad)		
		set @SCheckVagozari=(Select isnull(fldSharh,'') from tblSodooreSanad Where fldMoinKamel=@KolMoincheck and fldSanad=@ShomareSanad)		
		if @strMablagh is null
			begin			
				set @MablagheCheckeVagozari=0
				--set @CodeMoincheck=''
				set @Scheck=''
			end
		else
			begin			
				set @MablagheCheckeVagozari=Convert(decimal(18,4),@strMablagh)
			end


		--تخفیفات
		execute CodeMoinHesab  'takhfifatePardakht', @KolMoinTakhfif output
		set @strMablagh=(Select isnull(fldBestankar,0) from tblSodooreSanad Where fldMoinKamel=@KolMoinTakhfif and fldSanad=@ShomareSanad)
		--set @CodeMoin=(Select fldMoinKamel from tblSodooreSanad Where fldMoinKamel=@KolMoincheck and fldSanad=@ShomareSanad)		
		set @STakhfif=(Select isnull(fldSharh,'') from tblSodooreSanad Where fldMoinKamel=@KolMoinTakhfif and fldSanad=@ShomareSanad)		
		if @strMablagh is null
			begin			
				set @Takhfif=0
				--set @CodeMoincheck=''
				set @STakhfif=''
			end
		else
			begin			
				set @Takhfif=Convert(decimal(18,4),@strMablagh)
			end

		commit transaction
			end try

		begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='rolback'

		end catch
	--insert into tblTest7 (fld1,fld2)
	--values(@CodeMoinBank,@bank)

	--declare @nn decimal
	--set @nn=(Select fldBestankar from tblSodooreSanad Where fldMoinkamel='11001' and fldSanad='47')
	--insert into tblTest7 (fld2)
	--	values(@nn)
	--Select fldBestankar from tblSodooreSanad Where fldCodeKol='110' and fldSanad='48'
end

GO
/****** Object:  StoredProcedure [dbo].[ShowSandooghAndBankInFormVosoolCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ShowSandooghAndBankInFormVosoolCheck]
--چندزبانه
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

declare @codebank nvarchar(100) 
declare @codesandoog nvarchar(50)

execute CodeKolHesab 'bank', @codebank output
execute CodeKolHesab 'sandoogh',@codesandoog output

SELECT         fldSharheHesab,fldCodeHesab
FROM            tblKolMoin
WHERE        (fldCodeHesab like @codebank+'%' Or fldCodeHesab like @codesandoog+'%') And (LEN(fldCodeHesab) > LEN(@codebank))
order by fldSharheHesab
END

GO
/****** Object:  StoredProcedure [dbo].[ShowShomareFish]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ShowShomareFish] 
	-- Add the parameters for the stored procedure here
	@ShomareFactor bigint,
	@ShomareFish bigint output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	declare @SFish bigint

	set @SFish = (Select fldShomareFish from tblFish Where fldShomareFactor = @ShomareFactor)
	if @SFish is null
		set @shomarefish=0
	else
		set @shomarefish=@SFish
END

GO
/****** Object:  StoredProcedure [dbo].[ShowTanzimatNew]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ShowTanzimatNew]

	@Name1 nvarChar(30),
	@Meghdar nvarChar(30) OutPut	
as 

begin
	
	Set @Meghdar=(Select fldMeghdar from tblTanzimatNew Where fldName=@Name1)
	if @meghdar is  null
		begin
			set @meghdar='NOTHING'	
		end

end

GO
/****** Object:  StoredProcedure [dbo].[ShowtblTarheTakhfif]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[ShowtblTarheTakhfif]
--@CodeKolHesab nvarChar(8)
as
begin
	select Id,fldMablagh,fldDarsad,countRow from tblTarheTakhfif
	ORDER BY fldMablagh 
		--Where fldCodeHesab<>@CodeKolHesab and left(fldCodeHesab,len(@CodeKolHesab))=@CodeKolHesab
		
end

GO
/****** Object:  StoredProcedure [dbo].[ShowUsers]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ShowUsers] 
	-- Add the parameters for the stored procedure here
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	Select Convert(varchar,fldCode) + '-'  + fldUser as fldUserCode ,fldpassword from tblsecurity
END

GO
/****** Object:  StoredProcedure [dbo].[ShowVahed]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[ShowVahed]

as
begin
	select [fldNameVahed],[ID] from tblVahed
		where id<>1
		order by id
end

GO
/****** Object:  StoredProcedure [dbo].[SoodeFactor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE proc [dbo].[SoodeFactor]

@dtForoosh tpRizeForoosh  readonly,
@Sood bigint output
as 
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END

begin

	--create table #tmpForoosh(
	--	CodeKala bigint,
	--	fldTedadJoz bigint,
	--	fldFeeTakhfifVahed1 bigint,
	--	fldLastFeeKharid
	--)
	
	declare @EmpRadif int,@EmpR bigint,@bigLastFeeKharid bigint,@bigLastFeeForoosh bigint,@CodeKala bigint,@bigTedad bigint, @bigSumSood bigint
	set @bigSumSood=0
	
    declare EmpCursor Cursor For
            Select fldRadif from @dtForoosh 
        		open EmpCursor
        		fetch Next From EmpCursor into @EmpR
        		While @@Fetch_Status = 0
        			Begin	
						declare @Tedad as bigint
        				set @CodeKala=(Select fldCodeKala from @dtForoosh Where fldRadif=@EmpR)       
						update tblrizeKharid set fldMablaghKhales=fldfeeKol Where fldType='mojodi_ebteda_doreh'	 				        				        				
        				set @bigLastFeeKharid=(Select top(1) fldMablaghKhales/fldTedadJoz  from tblRizeKharid Where fldtype<>'enteghal' and fldCodeKala=@CodeKala order by fldDate desc,ID desc)
        				if ISNUMERIC(@bigLastFeeKharid)=0
        					set @bigLastFeeKharid=0


						set @Tedad=(Select fldTedadJoz from @dtForoosh Where fldRadif=@EmpR)
						if @tedad>0 
							begin
        						set @bigLastFeeForoosh=(Select fldMablaghKhales/fldTedadJoz from @dtForoosh Where fldRadif=@EmpR)
        						set @bigTedad=(Select fldTedadJoz from @dtForoosh Where fldRadif=@EmpR)
        					end
        				else
							begin
								set @bigLastFeeForoosh=0
        						set @bigTedad=0
							end
        				
							
        				set @bigSumSood =@bigSumSood + (@bigLastFeeForoosh-@bigLastFeeKharid)*@bigTedad
       -- 				INSERT INTO tblTest7
							--(fld1, fld2, fld3)
							--VALUES     (@bigLastFeeKharid,@bigLastFeeForoosh,@bigSumSood)
        				
        				--update #TempListeAshkhas3  set  fldTell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
        				--		where fldCodetafzili =@EmpCodeKala
        				Fetch Next from EmpCursor
        			into @EmpR
        		end
        Close EmpCurSor
        Deallocate EmpCursor
	
	if isnumeric(@bigSumSood)=1
		set @Sood=@bigSumSood
	else
		set @Sood=0
	--set @Sood=10
end

GO
/****** Object:  StoredProcedure [dbo].[SoodeGroupFifo]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[SoodeGroupFifo](@DateFROM BIGINT,@DateTo BIGINT, @ListCodeAnbar VARCHAR(5000),@fldCodekala bigint, @fldCodeGroup bigint)
AS
BEGIN
--Declare	@DateFROM BIGINT=13980101,@DateTo BIGINT = 19990101;
--DECLARE	@ListCodeAnbar VARCHAR(5000)=NULL;
--DECLARE @fldCodekala bigint =  NULL;--10080 --10004; -- NULL

SET NOCOUNT ON

CREATE Table	#KardeksTable ([ID] BIGINT, [fldSharh] NVARCHAR(200) COLLATE Persian_100_CI_AS, 
		[fldtypeShow] NVARCHAR(40) COLLATE Persian_100_CI_AS, [fldShomareFactor] BIGINT, [fldDateShow] NVARCHAR(25),
		[fldTedadeJozVorood] decimal(18,2), [fldTedadeJozKhorooj] decimal(18,2), [SumMandeJoz] decimal(18,2),
		[fldNameVahed] NVARCHAR(100) COLLATE Persian_100_CI_AS, [fldFeeJoz] BIGINT, [fldFeeVahed1BadAzTakhfif] BIGINT,
		[fldFeeYekKarton] BIGINT, [fldMablaghKol] BIGINT, [fldTedadVoroodKartonShow] NVARCHAR(100) COLLATE Persian_100_CI_AS,
		[fldTedadKhoroojKartonShow] NVARCHAR(100) COLLATE Persian_100_CI_AS, [fldTedadDarKarton] BIGINT,
		[fldNameVahed2] NVARCHAR(100) COLLATE Persian_100_CI_AS, [fldKolMande] NVARCHAR(100) COLLATE Persian_100_CI_AS,
		[fldShomareSanad] BIGINT, [fldCodeAnbar] BIGINT, [fldNameAnbar] NVARCHAR(50) COLLATE Persian_100_CI_AS,  [fldDate] BIGINT,
		[fldtype] NVARCHAR(20) COLLATE Persian_100_CI_AS, [fldRadif] BIGINT, [IdOrder] BIGINT, [fldCodeTafsili] BIGINT,
		[CodeKala] BIGINT NULL, [NameKala] NVARCHAR(100) COLLATE Persian_100_CI_AS, [CodeGroup] BIGINT, [NameGroup] NVARCHAR(80) COLLATE Persian_100_CI_AS,
		[fldShomareEnteghal] BIGINT, [fldTedadeJozVoroodT] NVARCHAR(10), [fldTedadeJozKhoroojT] NVARCHAR(10), [fldTedadDarKartonShow] NVARCHAR(100) COLLATE Persian_100_CI_AS, 
		[SumMandeJozT] NVARCHAR(100),  [fldJozMande] BIGINT, [ConditionSum] BIGINT);

with cte as
(SELECT	Buy.ID,fldCodeTafsili,Buy.fldCodeKala, fldShomareFactor,fldShomareEnteghal,fldShomareSanad,fldDate, '' [fldSharh],fldCodeAnbar, fldFeeJoz,
		fldFeeVahed1BadAzTakhfif, fldFeeYekCarton,fldMablaghKhales [fldMablaghKol],Buy.fldTedadJoz [fldTedadeJozVorood],0 [fldTedadeJozKhorooj],
		fldtype, NULL [fldRadif]
FROM	tblRizeKharid Buy
UNION ALL
SELECT	NULL ID, 0 [fldCodeTafsili],[Produce].fldCodeKala, fldShomareTolid fldShomareFactor, 0 [fldShomareEnteghal], fldShomareSanad,fldDate,
		'' [fldSharh], fldCodeAnbar, fldFeeJoz,fldFeeVahed1BadAzTakhfif,fldFeeYekCarton,fldMablaghKhales [fldMablaghKol], fldTedadJozNahaei [fldTedadeJozVorood],
		0 [fldTedadeJozKhorooj], fldtype, NULL [fldRadif]
FROM	tblRizeTolid Produce
WHERE	fldType='mahsool'
UNION ALL
SELECT	NULL ID, fldCodeTafsili, [ReSell].fldCodeKala,fldShomareFactor, 0 [fldShomareEnteghal], fldShomareSanad, fldDate, '' [fldSharh], fldCodeAnbar,
		fldFeeJoz, fldFeeVahed1BadAzTakhfif, fldFeeYekCarton,fldMablaghKhales [fldMablaghKol], [ReSell].fldTedadJoz [fldTedadeJozVorood], 0 [fldTedadeJozKhorooj],
		'bargashtazforoosh' [fldtype], NULL [fldRadif]
FROM	tblRizeBargashtAzForoosh [ReSell]
UNION ALL
SELECT	[Sell].id [ID], fldCodeTafsili,[Sell].fldCodeKala, [fldShomareFactor], fldShomareEnteghal, fldShomareSanad, fldDate,'' [fldSharh], fldCodeAnbar,
		fldFeeJoz, fldFeeVahed1BadAzTakhfif,fldFeeYekCarton, fldMablaghKhales [fldMablaghKol], 0 [fldTedadeJozVorood], [Sell].fldTedadJoz [fldTedadeJozKhorooj],
		fldtype, fldRadif
FROM	tblRizeForoosh [Sell]
UNION ALL
SELECT	NULL [ID], 0 [fldCodeTafsili],[Produce].fldCodeKala, fldShomareTolid [fldShomareFactor], 0 [fldShomareEnteghal], fldShomareSanad,
		fldDate,'' [fldSharh], fldCodeAnbar, fldFeeJoz, fldFeeVahed1BadAzTakhfif, fldFeeYekCarton, fldMablaghKhales [fldMablaghKol],
		0 [fldTedadeJozVorood], fldTedadJozAvalie [fldTedadeJozKhorooj], fldtype,fldRadif
FROM	tblRizeTolid [Produce]
WHERE	fldType='avalie'
UNION ALL
SELECT	NULL [ID], fldCodeTafsili, [ReBuy].fldCodeKala, fldShomareFactor, NULL [fldShomareEnteghal], fldShomareSanad,fldDate,'' [fldSharh], fldCodeAnbar,
		fldFeeJoz, fldFeeVahed1BadAzTakhfif, fldFeeYekCarton, fldMablaghKhales [fldMablaghKol], 0 [fldTedadeJozVorood], [ReBuy].fldTedadJoz [fldTedadeJozKhorooj],
		'bargashtazkharid' [fldtype], NULL fldRadif
FROM	tblRizeBargashtAzKharid [ReBuy])
INSERT	INTO #KardeksTable
SELECT	cte.[ID],
		[Tafzili].fldSharheTafzili + ' '+ CASE WHEN [fldType]='Moghayrat' THEN 'مغایرت' ELSE cte.[fldSharh] END [fldSharh],
		CASE [fldType]	WHEN 'kharid' THEN 'خرید'
				WHEN 'mojodi_ebteda_doreh' THEN'ابتدای دوره'
				WHEN 'enteghal' THEN 'انتقال بین انبار'
				WHEN 'foroosh' THEN 'فروش'
				WHEN 'bargashtazforoosh' THEN 'برگشت از فروش'
				WHEN 'bargashtazkharid' THEN 'برگشت از خرید'
				WHEN 'avalie' THEN 'تبدیل به محصول نهایی'
				WHEN 'mahsool' THEN 'محصول تولید شده در فرایند تولید'
				WHEN 'Moghayrat' THEN 'مغایرت' END [fldTypeShow],
		CASE WHEN fldType='enteghal' THEN [fldShomareEnteghal] ELSE [fldShomareFactor] END [fldShomareFactor],
		SUBSTRING(CONVERT(NVARCHAR(10),fldDate),0,5) +'/' +SUBSTRING(CONVERT(NVARCHAR(10),fldDate),5,2) + '/' + SUBSTRING(CONVERT(NVARCHAR(10),fldDate),7,2) [flddateshow],
		[fldTedadeJozVorood], [fldTedadeJozKhorooj],
		NULL SumMandeJoz,
		[Vahed1].fldNameVahed [fldNameVahed], [fldFeeJoz], [fldFeeVahed1BadAzTakhfif], [fldFeeYekCarton], [fldMablaghKol],
		CASE WHEN fldTedadeJozVorood > 0 THEN CASE WHEN [Stuff].fldTedadJoz > 0 THEN CONVERT(VARCHAR,floor(fldTedadeJozVorood / [Stuff].fldTedadJoz)) + ' ' + [Vahed2].fldNameVahed +  CONVERT(VARCHAR, { fn MOD(CONVERT(DEC,fldTedadeJozVorood), [Stuff].fldTedadJoz) } ) + ' ' + [Vahed1].fldNameVahed ELSE '' END ELSE '' END [fldTedadVoroodKartonShow],
		CASE WHEN fldTedadeJozKhorooj > 0 THEN CASE WHEN [Stuff].fldTedadJoz > 0 THEN CONVERT(VARCHAR,floor(fldTedadeJozKhorooj / [Stuff].fldTedadJoz)) + ' ' + [Vahed2].fldNameVahed +  CONVERT(VARCHAR, { fn MOD(CONVERT(DEC,fldTedadeJozKhorooj), [Stuff].fldTedadJoz) } ) + ' ' + [Vahed1].fldNameVahed ELSE '' END ELSE '' END [fldTedadKhoroojKartonShow],
		[Stuff].fldTedadJoz [fldTedadDarKarton], CASE WHEN [Stuff].fldTedadJoz=0 THEN '' ELSE [Vahed2].fldNameVahed END [fldNameVahed2],
		NULL [fldKolMande],
		[fldShomareSanad], [fldCodeAnbar],[Anbar].[fldNameAnbar], [fldDate], [fldtype], fldRadif,
		CONVERT(VARCHAR,CONVERT(BIGINT, CONVERT(VARCHAR,fldDate) + CONVERT(VARCHAR, fldShomareSanad))) +
			CASE WHEN fldtype='enteghal' THEN fldShomareEnteghal ELSE '' END IdOrder,
		CASE WHEN [fldType]='Moghayrat' THEN 0 ELSE fldCodeTafsili END fldCodeTafsili, cte.fldCodeKala [CodeKala],[Stuff].fldNameKala [NameKala],
		[Stuff].fldCodeGroup [CodeGroup], [StuffGroup].fldGroupName [NameGroup], [fldShomareEnteghal],
		NULL [fldTedadeJozVoroodT], NULL [fldTedadeJozKhoroojT], NULL [fldTedadDarKartonShow], 
		NULL [SumMandeJozT], -1 [fldJozMande], 1 [ConditionSum]
FROM	cte
		LEFT JOIN tblKala [Stuff] ON [cte].fldCodeKala=[Stuff].fldCodeKala
		LEFT JOIN tblGroupKala [StuffGroup] ON [Stuff].fldCodeGroup=[StuffGroup].fldGroupId
		LEFT JOIN tblAnbar [Anbar] ON [Anbar].fldCode=[cte].fldCodeAnbar
		LEFT JOIN tblVahed [Vahed1] ON [Stuff].fldCodeVahed1=[Vahed1].ID
		LEFT JOIN tblVahed [Vahed2] ON [Stuff].fldCodeVahed2=[Vahed2].ID
		LEFT JOIN tblTafzili [Tafzili] ON cte.fldCodeTafsili=[Tafzili].fldCodeTafzili
WHERE	fldDate BETWEEN  @DateFROM  AND  @DateTo
		AND (@ListCodeAnbar IS NULL OR fldCodeAnbar IN (SELECT [Value] FROM dbo.SplitString(@ListCodeAnbar,',')))
		AND (@fldCodeKala IS NULL OR [Stuff].fldCodeKala = @fldCodekala)
		AND (@fldCodeGroup IS NULL OR [Stuff].fldCodeGroup LIKE CONVERT(nvarchar,@fldCodeGroup) + '%')

UPDATE #KardeksTable SET IdOrder=CONVERT(VARCHAR,IdOrder) + '1' WHERE LEN(IdOrder)=9 and fldTedadeJozVorood>0
UPDATE #KardeksTable SET IdOrder=CONVERT(VARCHAR,IdOrder) + '2' WHERE LEN(IdOrder)=9 and fldTedadeJozKhorooj>0

UPDATE	Result
SET		SumMandeJoz = Db.SumMandeJoz
FROM	#KardeksTable Result
		INNER JOIN
		(SELECT	a.CodeKala,a.IdOrder, SUM(b.fldTedadeJozVorood - b.fldTedadeJozKhorooj) SumMandeJoz
		FROM	#KardeksTable a
				CROSS Apply #KardeksTable b
		WHERE	a.CodeKala=b.CodeKala AND CONVERT(VARCHAR, b.IdOrder) <= CONVERT(VARCHAR, a.IdOrder)
		Group BY a.CodeKala,a.IdOrder) DB ON Result.IdOrder=DB.IdOrder AND Result.CodeKala=Db.CodeKala

UPDATE #KardeksTable SET SumMandeJozT=SumMandeJoz,fldTedadeJozVoroodT=fldTedadeJozVorood,fldTedadeJozKhoroojT=fldTedadeJozKhorooj
UPDATE #KardeksTable SET SumMandeJozT=LEFT(CONVERT(VARCHAR,SumMandeJoz),LEN(CONVERT(VARCHAR,SumMandeJoz))-3) WHERE RIGHT(CONVERT(VARCHAR,SumMandeJoz),2)='00'
UPDATE #KardeksTable SET fldTedadeJozVoroodT=LEFT(CONVERT(VARCHAR,fldTedadeJozVorood),LEN(CONVERT(VARCHAR,fldTedadeJozVorood))-3) WHERE right(CONVERT(VARCHAR,fldTedadeJozVorood),2)='00'
UPDATE #KardeksTable SET fldTedadeJozKhoroojT=LEFT(CONVERT(VARCHAR,fldTedadeJozKhorooj),LEN(CONVERT(VARCHAR,fldTedadeJozKhorooj))-3) WHERE right(CONVERT(VARCHAR,fldTedadeJozKhorooj),2)='00'
UPDATE #KardeksTable SET fldKolMande=CONVERT(VARCHAR,floor(SumMandeJoz / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  CONVERT(VARCHAR, { fn MOD(CONVERT(DEC,SumMandeJoz), fldTedadDarKarton) } ) + ' ' + fldNameVahed WHERE fldTedadDarKarton>0;

DECLARE	@ResultTable Table([CodeKala] bigint,[CodeGroup] bigint,SumBuyAmount decimal,
		SumBuyCount bigint,SumSellAmount decimal,SumSellCount bigint,SumReSellAmount decimal,SumReSellCount bigint,
		--ISNULL(SumBuyCount,0)+ISNULL(SumReSellCount,0)-ISNULL(SumSellCount,0) Temp,
		RemaindCount bigint,RemaindAtEnd bigint, StuffRemaindAtEnd bigint, SellAmountAtEnd decimal);

WITH CteOrder AS (SELECT ROW_NUMBER() OVER(Partition BY CodeKala ORDER BY flddate DESC,fldshomaresanad DESC) AS [Row],* FROM	#KardeksTable)
INSERT INTO @ResultTable
SELECT	[Stuff].fldCodeKala [CodeKala],fldCodeGroup [CodeGroup], SumBuyAmount,
		SumBuyCount,SumSellAmount,SumSellCount,SumReSellAmount,SumReSellCount,
		--ISNULL(SumBuyCount,0)+ISNULL(SumReSellCount,0)-ISNULL(SumSellCount,0) Temp,
		DbRemain.RemaindCount,RemaindAtEnd, NULL AS StuffRemaindAtEnd, NULL AS SellAmountAtEnd
FROM	[tblKala] [Stuff]
		LEFT JOIN	(SELECT	Res.[CodeKala],DB.SumMandeJoz RemaindCount
					FROM	CteOrder Res
							INNER JOIN CteOrder DB ON Res.CodeKala=DB.CodeKala AND Res.[Row]=Db.[Row]
					WHERE	DB.[Row]=1) DbRemain ON [Stuff].fldCodeKala=DbRemain.CodeKala
		Left JOIN	(SELECT	CodeKala, Sum(fldMablaghKol) SumBuyAmount
					FROM	#KardeksTable
					WHERE	fldtype IN ('Kharid', 'mojodi_ebteda_doreh')
					Group BY [CodeKala]) DbBuyAmount ON [Stuff].fldCodeKala=DbBuyAmount.CodeKala
		Left JOIN	(SELECT	CodeKala, Sum(fldTedadeJozVorood) SumBuyCount
					FROM	#KardeksTable
					WHERE	fldtype IN ('Kharid')
					Group BY [CodeKala]) DbBuyCount ON [Stuff].fldCodeKala=DbBuyCount.CodeKala
		Left JOIN	(SELECT	CodeKala, Sum(fldMablaghKol) SumSellAmount,Sum(fldTedadeJozKhorooj) SumSellCount
					FROM	#KardeksTable
					WHERE	fldtype ='Foroosh'
					Group BY [CodeKala]) DbSell ON [Stuff].fldCodeKala=DbSell.CodeKala
		Left JOIN	(SELECT	CodeKala, Sum(fldMablaghKol) SumReSellAmount,Sum(fldTedadeJozVorood) SumReSellCount
					FROM	#KardeksTable
					WHERE	fldtype ='bargashtazforoosh'
					Group BY [CodeKala]) DbReSell ON [Stuff].fldCodeKala=DbReSell.CodeKala
		Left JOIN	(SELECT	CodeKala,Sum(fldTedadeJozVorood) RemaindAtEnd
					FROM	#KardeksTable
					WHERE	fldtype IN ('Kharid', 'mojodi_ebteda_doreh')--,'bargashtazforoosh')
					Group BY [CodeKala]) DbRemaindCount ON [Stuff].fldCodeKala=DbRemaindCount.CodeKala
WHERE	DbRemain.RemaindCount>0;


DECLARE StuffCursor Cursor For
SELECT	DISTINCT [CodeKala],RemaindCount From @ResultTable

DECLARE @CodeKala BIGINT,@StuffRemaindCount int

Open StuffCursor
FETCH NEXT FROM StuffCursor INTO @CodeKala,@StuffRemaindCount
WHILE @@FETCH_STATUS=0
BEGIN
	
	DECLARE @SellAmountAtEnd Decimal = 0, @fldTedadeJozVorood int,@fldFeeVahed1BadAzTakhfif bigint, @Temp int = 0
	DECLARE @StuffRemaindAtEnd int = 0, @fldtype nvarchar
		
	DECLARE	KardeksCursor Cursor FOR
	SELECT	[CodeKala],[fldTedadeJozVorood],fldFeeVahed1BadAzTakhfif,fldType From	#KardeksTable WHERE [CodeKala]=@CodeKala AND fldtype IN ('Kharid','mojodi_ebteda_doreh') ORDER BY IdOrder DESC

	Open KardeksCursor
	FETCH NEXT FROM KardeksCursor INTO @CodeKala,@fldTedadeJozVorood,@fldFeeVahed1BadAzTakhfif,@fldtype
	WHILE @@FETCH_STATUS=0
	BEGIN
		IF @StuffRemaindCount > 0
		BEGIN
			IF @StuffRemaindCount > @fldTedadeJozVorood
			BEGIN
				SET @SellAmountAtEnd = @SellAmountAtEnd + (@fldTedadeJozVorood * @fldFeeVahed1BadAzTakhfif)
			END
			ELSE
			BEGIN
				SET @SellAmountAtEnd = @SellAmountAtEnd + (@StuffRemaindCount * @fldFeeVahed1BadAzTakhfif)
			END

			SET @StuffRemaindCount = @StuffRemaindCount - @fldTedadeJozVorood
		END

		FETCH NEXT FROM KardeksCursor INTO @CodeKala,@fldTedadeJozVorood,@fldFeeVahed1BadAzTakhfif,@fldtype
	END
	CLOSE KardeksCursor
	DEALLOCATE KardeksCursor

	update @ResultTable set StuffRemaindAtEnd = @StuffRemaindAtEnd, SellAmountAtEnd= @SellAmountAtEnd WHERE [CodeKala]=@CodeKala

	FETCH NEXT FROM StuffCursor INTO @CodeKala,@StuffRemaindCount
END

Close StuffCursor
DEALLOCATE StuffCursor

SELECT	[CodeKala],[CodeGroup], ISNULL(SumBuyAmount,0) SumBuyAmount,ISNULL(SumBuyCount,0) SumBuyCount,
		ISNULL(SumSellAmount,0) SumSellAmount, ISNULL(SumSellCount,0) SumSellCount,
		ISNULL(SumReSellAmount,0) SumReSellAmount, ISNULL(SumReSellCount,0) SumReSellCount,
		--ISNULL(SumBuyCount,0)+ISNULL(SumReSellCount,0)-ISNULL(SumSellCount,0) Temp,
		RemaindCount,RemaindAtEnd, StuffRemaindAtEnd,
		BahayeTamamShodeFifo = ISNULL(SumBuyAmount,0) - ISNULL(SellAmountAtEnd,0),
		Benefit = ISNULL(SumSellAmount,0) - ISNULL(SumReSellAmount,0) - (ISNULL(SumBuyAmount,0) - ISNULL(SellAmountAtEnd,0))
FROM	@ResultTable

DROP Table #KardeksTable

	SET NOCOUNT OFF
END

GO
/****** Object:  StoredProcedure [dbo].[SooratHesab]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[SooratHesab]
--چندزبانه
@fast nvarchar(5),
@CodeTafzili nvarChar(10),
@bigDateMin bigint ,
@bigDateMax bigint ,
@flagMande int ,
@intOrder int,
@Language nvarchar(20),
@SumBedGhabl decimal (18, 4) output,
@SumBesGhabl decimal (18, 4) output
as


begin
	declare @EmpId bigint,@Select nvarchar(500),@Type nvarchar(50),@ShomareFactor bigint,@CodeKolMoin nvarchar(50),@CodeKolMoinTanzimat nvarchar(50),@CodeKolTanzimat nvarchar(50),@CodeKolSanad nvarchar(50)
	declare @ShomareSanad bigint,@CodekolMoinDaryaftTanzimat nvarchar(10),@ShomareSanadEbteda bigint,@SharheTranslate1 nvarchar(max),@SharheTranslate2 nvarchar(max),@SharheTranslate3 nvarchar(max),@SharheTranslate4 nvarchar(max),@SharheTranslate5 nvarchar(max),@SharheTranslate6 nvarchar(max),@SharheTranslate7 nvarchar(max),@SharheTranslate nvarchar(max)
	declare @ShomareDaryaft bigint,@ShomarePardakht bigint, @strAlaki nvarchar(50),@lngAlaki bigint,@SharheKol nvarchar(200) ,@lngNaghd decimal (18, 4),@strMoinKamelSandoogh nvarchar(10),@strSharhenaghd nvarchar(200) ,@lngBank decimal (18, 4),@strMoinKamelBank nvarchar(10),@strShareBank nvarchar(200),@lngCheck decimal (18, 4),@strSharheCheck nvarchar(200),@strMoinKamelCheckDaryafti nvarchar(10),@strMoinKamelCheckPardakhti nvarchar(10),@lngMablagheCheckeVagozari decimal (18, 4),@strSCheckVaghozari nvarchar(200),@strMoinKamelCheckVagozari nvarchar(10),@lngTakhfif decimal (18, 4),@strSharheTakhfifat nvarchar(200)
	declare @str1 nvarchar(200),@str2 nvarchar(200),@str3 nvarchar(200),@str4 nvarchar(200),@str5 nvarchar(200),@str6 nvarchar(200),@str7 nvarchar(200),@lngAlaki2 bigint
	declare @CodekolMoinHesabhayeDaryaftaniTanzimat nvarchar(10),@CodekolMoinHesabhayePardakhtaniTanzimat nvarchar(10),@strSharheHesab nvarchar(50),@bigBed decimal (18, 4),@bigBes decimal (18, 4), @CodeMoinKamelHesabhayePardakhtaniTanzimat  nvarchar(10)	

	CREATE TABLE #tmpSooratHesab2(
	[ID] [nvarchar](20) COLLATE Persian_100_CI_AS  NULL  ,
	[radif] [bigint]  null,
	[shomare] [nvarchar](100) COLLATE Persian_100_CI_AS null  ,
	[fldSanad] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldSharh] [nvarchar](150) COLLATE Persian_100_CI_AS NULL  ,
	[fldRiz] [nvarchar](150) COLLATE Persian_100_CI_AS NULL  ,
	[fldDate] [bigint] NULL,
	[fldTedad][float] null,
	[fldNameVahed] [nvarchar](100)  COLLATE Persian_100_CI_AS,
	[fldNameVahed2] [nvarchar](100)  COLLATE Persian_100_CI_AS,
	[fldFee]decimal (18, 4) null,
	[fldFeeYekCarton]decimal (18, 4) null,
	[fldFeeKol]decimal (18, 4) null,
	[fldDarsadTakhfif]decimal (18, 4) null,
	[fldMablaghTakhfif]decimal (18, 4) null,
	[fldFeeVahed1BadAzTakhfif]decimal (18, 4) null,
	[fldFeeVahed2BadAzTakhfif]decimal (18, 4) null,
	[fldArzeshAfzode]decimal (18, 4) null,
	[fldFeeKolBadAzTakhfif]decimal (18, 4) null,
	[fldMablaghKhales]decimal (18, 4) null,
	[fldCodeAnbar] [bigint] null,	
	[fldNameAnbar][nvarchar](50) COLLATE Persian_100_CI_AS null  ,
	[fldCodeKarbar] bigint,
	[fldNameKarbar] [nvarchar](25) COLLATE Persian_100_CI_AS NULL ,


	[fldMablagh]decimal (18, 4) null,
	[fldBedehkar] [decimal](18, 4) NULL,
	[fldBestankar] [decimal](18, 4) NULL,
	[fldMande]decimal (18, 4) null,
	[fldCodeKol] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldCodeMoin] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldCodeTafzili] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldCodeMarkaz1] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldCodeMarkaz2] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldRadif] [varchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldCodeSharh] [varchar](50) COLLATE Persian_100_CI_AS NULL  ,		
	[fldMoinKamel] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldCodeNoeTafzili] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldGhateiMovaghat] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldGhateiDaem] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldSanadGhadim] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldShomarePeigiri] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldMoghayerat] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldType] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldVaziat][nvarchar](20) COLLATE Persian_100_CI_AS NULL  ,
	[fldVasete] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  

) ON [PRIMARY]
---اسناد مربوط به کدتفصیلی یک به یک بررسی میشه و متناسب با نوع سند تحلیل و گسترش میباشد
		declare @bigDateMinForSanad bigint ,@bigDateMaxForSanad bigint ,@bigCodeKolHazine nvarchar(50),@bigCodeKolDaramad nvarchar(50) ,@bigCodeMoinKharid nvarchar(50),@strStartHazine nvarchar(1),@strStartDaramad nvarchar(1),@sanadCheckDaryaftEbteda bigint,@sanadCheckEbtedaPardakht bigint
		declare @tooleKol as int,@strKolForoosh nvarchar(50),@Asnadepardakhtani nvarchar(50),@Asnadepardaryaftani nvarchar(50),@CodemoinForoosh nvarchar(20)

		set @tooleKol=(Select top(1) fldKol from tblPaye)
		execute codemoinhesab 'foroosh' ,@strKolForoosh output

	set @bigDateMinForSanad=@bigDateMin
	set @bigDateMaxForSanad=@bigDateMax

	execute CodeKolHesab 'hazine',@bigCodeKolHazine output
	execute CodeKolHesab 'daramad',@bigCodeKolDaramad output
	execute CodeMoinHesab 'kharid',@bigCodeMoinKharid output
	execute CodeMoinHesab 'checkpardakhti',@Asnadepardakhtani output
	execute CodeMoinHesab 'foroosh',@CodemoinForoosh output

	set @strStartHazine=substring(Convert(varchar,@bigCodeKolHazine),1,1)
	set @strStartDaramad=substring(Convert(varchar,@bigCodeKolDaramad),1,1)
	execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinKamelHesabhayePardakhtaniTanzimat output
	execute codemoinhesab 'checkpardakhti' ,@strMoinKamelCheckPardakhti output	
	execute codemoinhesab 'check' ,@Asnadepardaryaftani output
	execute codemoinhesab 'check' ,@strMoinKamelCheckDaryafti output
	update tblSodooreSanad set fldType='' where fldType is null

	set @ShomareSanadEbteda=(select fldShomareEftetahie from tblControlPanel)


	set @sanadCheckDaryaftEbteda=(select top(1)fldSanad from tblCheckeDaryafti Where fldShomareDaryaft=0)
	if @sanadCheckDaryaftEbteda is null
		set @sanadCheckDaryaftEbteda=0

	set @sanadCheckEbtedaPardakht=(select top(1)fldSanad from tblCheckePardakhti Where fldShomarePardakht=0)
	if @sanadCheckEbtedaPardakht is null
		set @sanadCheckEbtedaPardakht=0

		--(fldSanad<>@ShomareSanadEbteda and fldMoinKamel<>@strMoinKamelCheckDaryafti) and

		delete #tmpSooratHesab2

		update tblSodooreSanad set fldType='' Where fldtype is null

if @fast='ok'
	begin
		insert into #tmpSooratHesab2 (fldCodeTafzili,fldSanad,fldRiz,fldDate,fldBedehkar,fldBestankar,fldCodeKol,fldCodeMoin,fldMoinkamel,fldType)			
		Select  fldCodeTafzili,fldSanad,fldSharh,fldDate,fldBedehkar,fldBestankar,fldCodeKol,fldCodeMoin,fldMoinkamel,fldType from tblSodooreSanad With (NoLock) where (fldType<>'checkebteda') and  fldsanad<>@sanadCheckDaryaftEbteda and fldSanad<>@sanadCheckEbtedaPardakht and fldCodeTafzili=@CodeTafzili and (Convert(dec,fldDate) between @bigDateMinForSanad and @bigDateMaxForSanad)  and  ((fldtype<>'Vosool' and fldtype<>'pasecheck')) and fldType<>'ebteda' and not(fldtype='ekhtetamie') and Not( fldType='eftetahie' and fldMoinKamel=@strMoinKamelCheckPardakhti)  and Not( fldType='eftetahie' and fldMoinKamel=@strMoinKamelCheckDaryafti)  and fldCodeKol<>@bigCodeKolHazine  and fldMoinKamel<>@bigCodeMoinKharid and substring(fldMoinKamel,1,1)<>@strStartHazine and (substring(fldMoinKamel,1,@tooleKol)<>@strStartDaramad or  fldMoinKamel=@strKolForoosh) and (substring(fldMoinKamel,1,1)<>@CodeMoinKamelHesabhayePardakhtaniTanzimat and fldType<>'hazine') and  fldcodekol <>@Asnadepardakhtani and fldMoinKamel<>@CodemoinForoosh  ORDER BY CONVERT(dec, fldSanad), CONVERT(dec, fldRadif)
                                                                                                            



		--update #tmpSooratHesab2 set fldRiz =fldRiz + ' ' +  tblTafzili.fldSharheTafzili from tblTafzili Where #tmpSooratHesab2.fldCodeTafzili=tblTafzili.fldCodeTafzili

		update #tmpSooratHesab2 set fldSharh='افتتاحیه' Where fldType='eftetahie'
		update #tmpSooratHesab2 set fldSharh='خرید' Where fldType='kharid'
		update #tmpSooratHesab2 set fldSharh='پرداخت' Where fldType='pardakht'
		update #tmpSooratHesab2 set fldSharh='دریافت' Where fldType='daryaft'
		update #tmpSooratHesab2 set fldSharh='برگشت از خرید' Where fldType='bargashtazkharid'
		update #tmpSooratHesab2 set fldSharh='فروش' Where fldType='foroosh'
		update #tmpSooratHesab2 set fldSharh='برگشت از فروش' Where fldType='bargashtazforoosh'

		update #tmpSooratHesab2 set fldriz='فاکتور خرید' + ' ' + Convert(varchar,fldShomareFactorKharid),shomare=fldShomareFactorKharid from tblKharid Where #tmpSooratHesab2.fldSanad=tblKharid.fldShomareSanad
		update #tmpSooratHesab2 set fldriz='برگشت از خرید ' + ' ' + Convert(varchar,fldShomareFactorKharid),shomare=fldShomareFactorKharid from tblBargashtAzKharid Where #tmpSooratHesab2.fldSanad=tblBargashtAzKharid.fldShomareSanad

		update #tmpSooratHesab2 set fldriz='فاکتور فروش' + ' ' + Convert(varchar,fldShomareFactorForoosh), shomare=fldShomareFactorforoosh from tblForoosh Where #tmpSooratHesab2.fldSanad=tblForoosh.fldShomareSanad
		update #tmpSooratHesab2 set fldriz='برگشت از فروش' + ' ' + Convert(varchar,fldShomareFactorForoosh), shomare=fldShomareFactorforoosh from tblBargashtAzForoosh Where #tmpSooratHesab2.fldSanad=tblBargashtAzForoosh.fldShomareSanad

		update #tmpSooratHesab2 set fldriz='دریافت شماره' + ' ' + Convert(varchar,fldShomareDaryaft), shomare=fldShomareDaryaft from tblDaryaft Where #tmpSooratHesab2.fldSanad=tblDaryaft.fldShomareSanad
		update #tmpSooratHesab2 set fldriz='پرداخت شماره' + ' ' + Convert(varchar,fldShomarePardakht), shomare=fldShomarePardakht from tblPardakht Where #tmpSooratHesab2.fldSanad=tblPardakht.fldShomareSanad

		if @flagMande =1 --با نقل مانده		
			begin
				declare @decMandeGhablBed decimal(18,0) ,@decMandeGhablBes decimal(18,0) ,@decMandeGhabl decimal(18,0) 

					declare @CodeKol nvarchar(100),@MandeTalab bigint,@MandeBedehi bigint

					execute CodeKolHesab 'kolhesabhayedaryaftani',@CodeKol output

					set @MandeTalab=(SELECT       ISNULL( SUM(ISNULL(fldBedehkar, 0)-ISNULL(fldBestankar, 0)),0) AS SumM
						FROM            tblSodooreSanad
						WHERE      (fldCodeKol=@CodeKol) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldDate)<@bigDateMin)

					

					execute CodeKolHesab 'kolhesabhayepardakhtani',@CodeKol output

					set @MandeBedehi=(SELECT       ISNULL( SUM(ISNULL(fldBestankar, 0)-ISNULL(fldBedehkar, 0)),0) AS SumM
						FROM            tblSodooreSanad
						WHERE      (fldCodeKol=@CodeKol) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldDate)<@bigDateMin)
					set @decMandeGhabl=@MandeTalab-@MandeBedehi								
				if @decMandeGhabl<>0
					insert into #tmpSooratHesab2
						(id,shomare,fldSanad,fldSharh,fldDate,fldBedehkar,fldBestankar,fldMande,fldtype)
							values
						('0','','0','مانده قبل',@bigDateMin,@MandeTalab,@MandeBedehi,@decMandeGhabl,'ghabl')
			end 

		set @SumBedGhabl=(Select Sum(fldBedehkar) as SumBed from  #tmpSooratHesab2  Where fldBestankar is not null and fldBedehkar is not null and Convert(dec,fldDate)<@bigDateMin)
		if @SumBedGhabl is null
			set @SumBedGhabl =0

		set @SumBesGhabl=(Select Sum(fldBestankar) as SumBes from  #tmpSooratHesab2  Where fldBestankar is not null and fldBedehkar is not null  and Convert(dec,fldDate)<@bigDateMin)
		if @SumBesGhabl is null
			set @SumBesGhabl=0

			update #tmpSooratHesab2 set id=0

			if @intOrder=0  --مرتب سازی براساس تاریخ
				Select  ID,radif,shomare,fldSanad,fldSharh,fldRiz,substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate,fldTedad,fldNameVahed,
					fldNameVahed2,fldFee,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,
					fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,
					fldMablaghKhales,fldCodeAnbar,fldNameAnbar,fldMablagh,fldBedehkar,fldBestankar,fldMande,
					fldType,fldVaziat,fldVasete,fldCodeKarbar,fldNameKarbar  from #tmpSooratHesab2 order by Convert(dec,fldDate),Convert(dec,fldSanad)	


				--select ID,radif,shomare,fldSanad,fldSharh,fldRiz,substring(convert(nvarchar(10),fldDate),0,3) +'/' +substring(convert(nvarchar(10),fldDate),3,2) + '/' + substring(convert(nvarchar(10),fldDate),5,2) as fldDate,fldTedad,fldNameVahed,
				--	fldNameVahed2,fldFee,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,
				--	fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,
				--	fldMablaghKhales,fldCodeAnbar,fldNameAnbar,fldMablagh,fldBedehkar,fldBestankar,fldMande,
				--	fldType,fldVaziat,fldVasete,fldCodeKarbar,fldNameKarbar



			else
				if @intOrder=1  --مرتب سازی براساس سند
					Select  ID,radif,shomare,fldSanad,fldSharh,fldRiz,substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate,fldTedad,fldNameVahed,
					fldNameVahed2,fldFee,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,
					fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,
					fldMablaghKhales,fldCodeAnbar,fldNameAnbar,fldMablagh,fldBedehkar,fldBestankar,fldMande,
					fldType,fldVaziat,fldVasete,fldCodeKarbar,fldNameKarbar  from #tmpSooratHesab2 order by Convert(dec,fldSanad)	

	end
else
	begin
				declare @kolhesabhayepardakhtani nvarchar(50),@kolhesabhayedaryaftani nvarchar(50),@CodeKoleHesabhayeDaryaftani2 nvarchar(20),@CodeKoleHesabhayePardakhtani2 nvarchar(20),@MoinKamel nvarchar(50),@fldCodeKol nvarchar(50)
				execute CodeKolHesab 'kolhesabhayepardakhtani' ,@kolhesabhayepardakhtani output
				execute CodeKolHesab 'kolhesabhayedaryaftani' ,@kolhesabhayedaryaftani output
				declare EmpCursor Cursor For
					Select Id from tblSodooreSanad With (NoLock) where (fldType='' and ((fldCodeKol=@kolhesabhayedaryaftani or fldCodeKol=@kolhesabhayepardakhtani )  and (fldType<>'checkebteda')  and  fldCodeTafzili=@CodeTafzili)  ) or  ((fldCodeKol=@kolhesabhayedaryaftani or fldCodeKol=@kolhesabhayepardakhtani ) and (fldType<>'checkebteda') and  fldsanad<>@sanadCheckDaryaftEbteda and fldSanad<>@sanadCheckEbtedaPardakht and fldCodeTafzili=@CodeTafzili and (Convert(dec,fldDate) between @bigDateMinForSanad and @bigDateMaxForSanad)  and  ((fldtype<>'Vosool' and fldtype<>'pasecheck')) and fldType<>'ebteda' and not(fldtype='ekhtetamie') and Not( fldType='eftetahie' and fldMoinKamel=@strMoinKamelCheckPardakhti)  and Not( fldType='eftetahie' and fldMoinKamel=@strMoinKamelCheckDaryafti)  and fldCodeKol<>@bigCodeKolHazine  and fldMoinKamel<>@bigCodeMoinKharid and substring(fldMoinKamel,1,1)<>@strStartHazine and (substring(fldMoinKamel,1,@tooleKol)<>@strStartDaramad or  fldMoinKamel=@strKolForoosh) and (substring(fldMoinKamel,1,1)<>@CodeMoinKamelHesabhayePardakhtaniTanzimat and fldType<>'hazine')) ORDER BY CONVERT(dec, fldSanad), CONVERT(dec, fldRadif)
						open EmpCursor
						fetch Next From EmpCursor into @EmpId
						While @@Fetch_Status = 0
							Begin					
								--Update tblSodooreSanad With (RowLock) Set [fldGhateiMovaghat]='False'					
								--Where ID=@EmpId
					
					execute CodeKolHesab 'kolhesabhayedaryaftani',@CodeKoleHesabhayeDaryaftani2 output
							execute CodeKolHesab 'kolhesabhayepardakhtani',@CodeKoleHesabhayePardakhtani2 output

							set @type =(Select fldType from tblSodooresanad Where ID=@EmpId)

							--برای سند دستی که اگر چیزی غیر از حسابهای دریافتنی و پرداختنی بود نمایش نده
							--################################################################################
								if @type=''
									begin
										set @fldCodeKol=(Select fldCodeKol
											from tblSodooreSanad 
												Where ID=Convert(varchar,@EmpId))
										if @fldCodeKol<>@CodeKoleHesabhayeDaryaftani2 and @fldCodeKol<>@CodeKoleHesabhayePardakhtani2
												begin
													set @type=''
												end
											else
												begin
													INSERT INTO #tmpSooratHesab2
													 (ID, fldSanad, fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
													 fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
													 fldVasete)
												Select ID, fldSanad,fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
													 fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
													 fldVasete
												from tblSodooreSanad 
													Where ID=Convert(varchar,@EmpId)
												end
									end
									--################################################################################
								else
									begin
										-- ریختن کلیه اسناد مربوط به تفصیلی در جدول موقت  #########
										INSERT INTO #tmpSooratHesab2
											 (ID, fldSanad, fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
											 fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
											 fldVasete)
										Select ID, fldSanad,fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
											 fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
											 fldVasete
										from tblSodooreSanad 
											Where ID=Convert(varchar,@EmpId)
									end
													
								set @CodeKolMoin =(Select fldMoinKamel from tblSodooresanad Where ID=@EmpId)
								execute codemoinhesab 'foroosh' ,@CodekolmoinTanzimat output
								execute codemoinhesab 'hesabhayedaryaftani' ,@CodekolMoinDaryaftTanzimat output	
								execute codemoinhesab 'check' ,@strMoinKamelCheckDaryafti output	
								execute codemoinhesab 'checkpardakhti' ,@strMoinKamelCheckPardakhti output	
								set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
								execute codemoinhesab 'hesabhayepardakhtani' ,@CodekolMoinHesabhayePardakhtaniTanzimat output	
									
								set @CodekolMoinHesabhayeDaryaftaniTanzimat = @CodekolMoinDaryaftTanzimat
					

								--###### فروش ##################
								if @type='foroosh'   ---برای نمایش ریز فاکتور فروش
									begin
										if @CodekolmoinTanzimat = @CodeKolMoin --اگر سند فروش بود
											begin
												declare @bigDate bigint,@codeKarbar bigint,@NameKarbar nvarchar(20)							
												set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
												set @ShomareFactor =(Select  fldShomareFactorForoosh from tblForoosh Where fldShomareSanad = @ShomareSanad)
									

												update #tmpSooratHesab2
													set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayeDaryaftaniTanzimat 

												execute z187TranslateKalame @language,16157,@SharheTranslate1 output
												update #tmpSooratHesab2									
													set fldSharh=' ' + @SharheTranslate1 +case when (fldSharh='' or fldSharh is null) then '' else '-' + fldSharh end   where fldSanad=@ShomareSanad And fldMoinKamel= @CodekolMoinHesabhayeDaryaftaniTanzimat 

													--,fldSharh=' فروش' + iif( fldSharh='','', '-' + fldSharh )

												execute z187TranslateKalame @language,16162,@SharheTranslate1 output--ف
												update #tmpSooratHesab2 set shomare = @SharheTranslate1+Convert(varchar, @ShomareFactor) where fldSanad= @ShomareSanad
												insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
														fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
														fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
														fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
														fldCodeAnbar,fldNameAnbar,fldType)
												Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),@SharheTranslate1+Convert(varchar, @ShomareFactor),@ShomareSanad, dbo.tblKala.fldNameKala + ' ' + dbo.tblRizeForoosh.fldSharhKala,
														@bigDate,dbo.tblRizeForoosh.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeForoosh.fldFeeJoz,dbo.tblRizeForoosh.fldFeeYekCarton,
														dbo.tblRizeForoosh.fldFeeKol,dbo.tblRizeForoosh.fldDarsadTakhfif,dbo.tblRizeForoosh.fldMablaghTakhfif,dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, 
														dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif,dbo.tblRizeForoosh.fldMablaghKhales,
														dbo.tblRizeForoosh.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'foroosh'
												FROM            dbo.tblAnbar INNER JOIN
																		 dbo.tblRizeForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeForoosh.fldCodeAnbar INNER JOIN
																		 dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
																		 dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
																		 dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
												WHERE        (dbo.tblRizeForoosh.fldShomareFactor = @ShomareFactor)

											--اضافه کردن نام کاربر
											 set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'فروش' and NoeAmaliat=N'جدید' order by id desc)
											 if @codekarbar is not null
												set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'فروش' and NoeAmaliat=N'جدید' order by id desc)
											 else
												begin
													set @codekarbar=0
													set @namekarbar =''
												end

											update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad
												--### حذف ردیف سندی که کد کل و معینش فروشه ، در حقیقت فقط باید حسابهای دریافتنی و پرداختنی و سندهای دستی نمایش داده بشه									
											end


										else if @CodekolMoinHesabhayeDaryaftaniTanzimat = @CodeKolMoin  -- اگر سند مربوط به دریافت در فروش بود
											begin
												set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)									
												set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
												set @ShomareFactor =(Select  fldShomareFactorForoosh from tblForoosh Where fldShomareSanad = @ShomareSanad)
												set @ShomareDaryaft =(Select  fldShomareDaryaft from tblDaryaft Where fldShomareSanad = @ShomareSanad)
												set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
												set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)

												execute	ShowDaryaftInfo @ShomareDaryaft, @str1, @lngAlaki, @lngAlaki2, @SharheKol output, 'daryaft', @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3

												UPDATE       #tmpSooratHesab2
													SET                fldSharh = @SharheKol
														where #tmpSooratHesab2.ID = @EmpId


												execute z187TranslateKalame @language,16159,@SharheTranslate1 output--دریافت
												execute z187TranslateKalame @language,16163,@SharheTranslate2 output--ش د
												execute z187TranslateKalame @language,16164,@SharheTranslate3 output--ش ف
												execute z187TranslateKalame @language,16165,@SharheTranslate4 output--چک شماره
												execute z187TranslateKalame @language,16166,@SharheTranslate5 output--تاريخ
												update #tmpSooratHesab2
													set fldSharh =@SharheTranslate1 +case when @SharheKol='' then '' else ' - ' +@SharheKol end , shomare =@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor)  where fldSanad=@ShomareSanad And fldMoinKamel =  @CodekolMoinHesabhayeDaryaftaniTanzimat  and fldBestankar>0

												update #tmpSooratHesab2
													set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayeDaryaftaniTanzimat 


												if @bigbes>0
													begin
														if @lngNaghd>0
															begin
																set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																	values (Convert(varchar,@EmpId) + '101' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab +case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end   ,@bigDate,@lngNaghd,'foroosh')
															end

														if @lngBank>0
															begin
																set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																	values (Convert(varchar,@EmpId) + '102' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strShareBank='' then '' else '-' +@strShareBank end   ,@bigDate,@lngBank,'foroosh')
															end
									
														if @lngCheck>0 
															begin
																--set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelCheckDaryafti)
																--set @strSharheHesab ='چک شماره ' 
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

																		select Convert(varchar,@EmpId) +'103' + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) +  ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate4 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + @SharheTranslate5 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSharheCheck='' then '' else '-' +@strSharheCheck end   ,@bigDate,fldMablagh,'foroosh'
																			from tblCheckeDaryafti Where fldSanad = @ShomareSanad

																update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad and fldMoinKamel = @strMoinKamelCheckDaryafti
															end

														if @lngTakhfif>0
															begin
																set @strSharheHesab = (Select * from dbo.translatefunction(@LANGUAGE,18285)) + ' '
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																	values (Convert(varchar,@EmpId) + '104' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab +case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end  ,@bigDate,@lngTakhfif,'foroosh')
															end
													end
											end

								
									end

					
								set @CodeKolMoin =(Select fldMoinKamel from tblSodooresanad Where ID=@EmpId)--11301
								set @CodeKolSanad =(Select fldCodeKol from tblSodooresanad Where ID=@EmpId)--113
								set @CodeKolTanzimat =(Select fldCodeKol from tblSodooresanad Where ID=@EmpId)--110
								execute CodeKolHesab 'hesabhayedaryaftani' ,@CodeKolTanzimat output--11301

					
				


				
							if @type='kharid'   ---برای نمایش ریز فاکتور خرید
									begin
										execute codemoinhesab 'kharid' ,@CodekolmoinTanzimat output
										execute codemoinhesab 'hesabhayepardakhtani' ,@CodekolMoinHesabhayePardakhtaniTanzimat output	

										set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
										update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckPardakhti
										update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckdaryafti

										if @CodekolmoinTanzimat = @CodeKolMoin --اگر سند خرید بود
											begin
									
												set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
						
											end

										else if @CodekolMoinHesabhayePardakhtaniTanzimat = @CodeKolMoin  -- اگر سند مربوط به پرداخت در خرید بود
											begin
												set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)									
												set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
												set @ShomareFactor =(Select  fldShomareFactorKharid from tblKharid Where fldShomareSanad = @ShomareSanad)
												set @ShomarePardakht =(Select  fldShomarePardakht from tblPardakht Where fldShomareSanad = @ShomareSanad)
												set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
												set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)

												execute	ShowPardakhtInfo @ShomarePardakht, @str1, @lngAlaki, @str1, @SharheKol output, @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output,@lngMablagheCheckeVagozari output, @strSCheckVaghozari output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3



												set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
												set @ShomareFactor =(Select  fldShomareFactorkharid from tblKharid Where fldShomareSanad = @ShomareSanad)
									
												if @bigbes>0  or (@bigbes=0 and @bigbed=0)
													begin
														execute z187TranslateKalame @language,16167,@SharheTranslate1 output--خ
														execute z187TranslateKalame @language,16155,@SharheTranslate2 output--خرید

														update #tmpSooratHesab2 set shomare = @SharheTranslate1+Convert(varchar, @ShomareFactor) ,fldSharh=' ' + @SharheTranslate2 +case when fldSharh='' then '' else '-' +fldSharh end   where ID=@EmpId
														update #tmpSooratHesab2 set shomare = @SharheTranslate1+Convert(varchar, @ShomareFactor) where fldSanad= @ShomareSanad
														insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
																fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
																fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
																fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
																fldCodeAnbar,fldNameAnbar,fldType)
														Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),@SharheTranslate1+Convert(varchar, @ShomareFactor),@ShomareSanad, dbo.tblKala.fldNameKala + ' ' + dbo.tblRizekharid.fldSharhKala,
																@bigDate,dbo.tblRizekharid.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizekharid.fldFeeJoz,dbo.tblRizekharid.fldFeeYekCarton,
																dbo.tblRizekharid.fldFeeKol,dbo.tblRizekharid.fldDarsadTakhfif,dbo.tblRizekharid.fldMablaghTakhfif,dbo.tblRizekharid.fldFeeVahed1BadAzTakhfif, 
																dbo.tblRizekharid.fldFeeVahed2BadAzTakhfif, dbo.tblRizekharid.fldArzeshAfzode, dbo.tblRizekharid.fldFeeKolBadAzTakhfif,dbo.tblRizekharid.fldMablaghKhales,
																dbo.tblRizekharid.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'kharid'
														FROM            dbo.tblAnbar INNER JOIN
																					dbo.tblRizekharid ON dbo.tblAnbar.fldCode = dbo.tblRizekharid.fldCodeAnbar INNER JOIN
																					dbo.tblKala ON dbo.tblRizekharid.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
																					dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
																					dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
														WHERE        (dbo.tblRizekharid.fldShomareFactor = @ShomareFactor)
													end



											--اضافه کردن نام کاربر
											 set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'خرید' and NoeAmaliat=N'جدید' order by id desc)
											 if @codekarbar is not null
												set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'خرید' and NoeAmaliat=N'جدید' order by id desc)
											 else
												begin
													set @codekarbar=0
													set @namekarbar =''
												end
												update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad

												update #tmpSooratHesab2
													set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayePardakhtaniTanzimat 

									

												if @bigbed>0
													begin
														execute z187TranslateKalame @language,16160,@SharheTranslate1 output--پرداخت
														execute z187TranslateKalame @language,16168,@SharheTranslate2 output--ش پ
														execute z187TranslateKalame @language,16169,@SharheTranslate3 output--ش خ
														execute z187TranslateKalame @language,16170,@SharheTranslate4 output--شماره چک
														execute z187TranslateKalame @language,16166,@SharheTranslate5 output--تاريخ
														execute z187TranslateKalame @language,16172,@SharheTranslate6 output--واگذاري چک شماره
														execute z187TranslateKalame @language,16183,@SharheTranslate7 output--تخفیفات خرید

														update #tmpSooratHesab2 set fldSharh=@SharheTranslate1 + ' ' +  fldSharh ,shomare =@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor) where id=@EmpId
														if @lngNaghd>0
															begin
																set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																	values (Convert(varchar,@EmpId) + '100' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end ,@bigDate,@lngNaghd,'kharid')
															end

														if @lngBank>0
															begin
																set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																	values (Convert(varchar,@EmpId) + '200' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strShareBank='' then '' else '-' +    @strShareBank end,@bigDate,@lngBank,'kharid')
															end
									
														if @lngCheck>0 
															begin													
													
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

																		select Convert(varchar,@EmpId ) + '300' + + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate4 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + ' ' + @SharheTranslate5 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSharheCheck='' then '' else '-' +@strSharheCheck end ,@bigDate,fldMablagh,'kharid'
																			from tblCheckePardakhti Where fldSanad = @ShomareSanad													
															end

							
														if @lngMablagheCheckeVagozari>0 
															begin													
													
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

																		select Convert(varchar,@EmpId ) + '400' + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate6 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + @SharheTranslate5 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSCheckVaghozari='' then '' else '-' +@strSCheckVaghozari end ,@bigDate,fldMablagh,'kharid'
																			from tblCheckeDaryafti Where fldShomarePardakht = @ShomarePardakht													
															end

														if @lngTakhfif>0
															begin
																set @strSharheHesab = @SharheTranslate7 + ' '
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																	values (Convert(varchar,@EmpId ) + '500' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharheTakhfifat='' then '' else '-' +  @strSharheTakhfifat end  ,@bigDate,@lngTakhfif,'kharid')
															end
													end
											end
								
									end



								execute codemoinhesab 'foroosh' ,@CodekolmoinTanzimat output

					

								--###### برگشت از فروش ##################
								if @type='bargashtazforoosh'   ---برای نمایش ریز برگشت از فروش
									begin
										execute z187TranslateKalame @language,16171,@SharheTranslate1 output--'ب ف'
										execute z187TranslateKalame @language,16160,@SharheTranslate2 output--پرداخت
										execute z187TranslateKalame @language,16168,@SharheTranslate3 output--ش پ
										execute z187TranslateKalame @language,16169,@SharheTranslate4 output--ش خ
										execute z187TranslateKalame @language,16156,@SharheTranslate5 output--برگشت از فروش
										execute z187TranslateKalame @language,16165,@SharheTranslate6 output--شماره چک
							

										set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
										update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckPardakhti
										update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckdaryafti

						

										update #tmpSooratHesab2
														set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayeDaryaftaniTanzimat 

										begin
											if @CodekolmoinTanzimat = @CodeKolMoin --اگر سند برگشت از فروش بود
												begin
									
													set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
													set @ShomareFactor =(Select  fldShomareFactorForoosh from tblBargashtAzForoosh Where fldShomareSanad = @ShomareSanad)
									
												end

											else if @CodekolMoinHesabhayeDaryaftaniTanzimat = @CodeKolMoin  -- اگر سند مربوط به پرداخت در برگشت از فروش بود
												begin
													set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)									
													set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
													set @ShomareFactor =(Select  fldShomareFactorForoosh from tblBargashtAzForoosh Where fldShomareSanad = @ShomareSanad)
													set @ShomarePardakht =(Select  fldShomarePardakht from tblPardakht Where fldShomareSanad = @ShomareSanad)
													set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
													set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)
									
													execute	ShowPardakhtInfo @ShomarePardakht, @str1, @lngAlaki, @str1, @SharheKol output, @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output,@lngMablagheCheckeVagozari output, @strSCheckVaghozari output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3


													set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
													set @ShomareFactor =(Select  fldShomareFactorForoosh from tblBargashtAzForoosh Where fldShomareSanad = @ShomareSanad)

												--اضافه کردن نام کاربر
												 set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'برگشت از فروش' and NoeAmaliat=N'جدید' order by id desc)
												 if @codekarbar is not null
													set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'برگشت از فروش' and NoeAmaliat=N'جدید' order by id desc)
												 else
													begin
														set @codekarbar=0
														set @namekarbar =''
													end
												update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad
												
													if @bigbes>0  or (@bigbes=0 and @bigbed=0)
														begin
															update #tmpSooratHesab2 set shomare =@SharheTranslate1+Convert(varchar, @ShomareFactor) ,fldSharh=' ' + @SharheTranslate5 + case when fldSharh ='' then '' else '-' +  fldSharh end where ID=@EmpId
															update #tmpSooratHesab2 set shomare = @SharheTranslate1+Convert(varchar, @ShomareFactor) where fldSanad= @ShomareSanad
															insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
																	fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
																	fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
																	fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
																	fldCodeAnbar,fldNameAnbar,fldType)
															Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),@SharheTranslate1+Convert(varchar, @ShomareFactor),@ShomareSanad, dbo.tblKala.fldNameKala + ' ' + dbo.tblRizeBargashtAzForoosh.fldSharhKala,
																	@bigDate,dbo.tblRizeBargashtAzForoosh.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeBargashtAzForoosh.fldFeeJoz,dbo.tblRizeBargashtAzForoosh.fldFeeYekCarton,
																	dbo.tblRizeBargashtAzForoosh.fldFeeKol,dbo.tblRizeBargashtAzForoosh.fldDarsadTakhfif,dbo.tblRizeBargashtAzForoosh.fldMablaghTakhfif,dbo.tblRizeBargashtAzForoosh.fldFeeVahed1BadAzTakhfif, 
																	dbo.tblRizeBargashtAzForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldArzeshAfzode, dbo.tblRizeBargashtAzForoosh.fldFeeKolBadAzTakhfif,dbo.tblRizeBargashtAzForoosh.fldMablaghKhales,
																	dbo.tblRizeBargashtAzForoosh.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'bargashtazforoosh'
															FROM            dbo.tblAnbar INNER JOIN
																						dbo.tblRizeBargashtAzForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeBargashtAzForoosh.fldCodeAnbar INNER JOIN
																						dbo.tblKala ON dbo.tblRizeBargashtAzForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
																						dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
																						dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
															WHERE        (dbo.tblRizeBargashtAzForoosh.fldShomareFactor = @ShomareFactor)
														end


											
												if @bigbed>0
													begin
														update #tmpSooratHesab2 set fldSharh=@SharheTranslate2 + ' ' +  fldSharh ,shomare = @SharheTranslate3 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate1 + Convert(varchar,@ShomareFactor) where id=@EmpId
														if @lngNaghd>0
															begin
																set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																	values (Convert(varchar,@EmpId) + '100' ,@SharheTranslate3 + Convert(varchar,@ShomarePardakht)  + ' ' + @SharheTranslate1  + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end,@bigDate,@lngNaghd,'bargashtazforoosh')
															end

														if @lngBank>0
															begin
																set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																	values (Convert(varchar,@EmpId) + '200' ,@SharheTranslate3 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate4 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strShareBank='' then '' else '-' + @strShareBank end  ,@bigDate,@lngBank,'bargashtazforoosh')
															end
									
														if @lngCheck>0 
															begin													
													
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

																		select Convert(varchar,@EmpId ) + '300' + + Convert(varchar,id) ,@SharheTranslate3 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate4 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate6 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + ' تاریخ ' + Convert(varchar,fldDateSarResid) + case when @strSharheCheck='' then '' else '-' +@strSharheCheck end  ,@bigDate,fldMablagh,'bargashtazforoosh'
																			from tblCheckePardakhti Where fldSanad = @ShomareSanad													
															end

								

													if @lngMablagheCheckeVagozari>0 
														begin													
															execute z187TranslateKalame @language,16172,@SharheTranslate1 output--واگذاری چک شماره
															execute z187TranslateKalame @language,16166,@SharheTranslate2 output--تاريخ
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

																	select Convert(varchar,@EmpId ) + '400' + Convert(varchar,id) ,@SharheTranslate3 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate4 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate1 +' ' + Convert(varchar,fldShomareCheck) + ' ' + ' ' + @SharheTranslate2 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSCheckVaghozari='' then '' else '-' +@strSCheckVaghozari end ,@bigDate,fldMablagh,'bargashtazforoosh'
																		from tblCheckeDaryafti Where fldShomarePardakht = @ShomarePardakht													
														end

														if @lngTakhfif>0
															begin
																execute z187TranslateKalame @language,16172,@SharheTranslate1 output--واگذاری چک شماره
																execute z187TranslateKalame @language,16172,@SharheTranslate2 output--تخفیفات فروش
																set @strSharheHesab =@SharheTranslate2 + ' '
																insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																	values (Convert(varchar,@EmpId ) + '500' ,@SharheTranslate3 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate4 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharheTakhfifat='' then '' else ' - ' +@strSharheTakhfifat end,@bigDate,@lngTakhfif,'bargashtazforoosh')
															end
													end



												end		    
									   end 						   
								
									end

					

								if @type='bargashtazkharid'   ---برای نمایش ریز برگشت از خرید
									begin
									execute z187TranslateKalame @language,16159,@SharheTranslate1 output--دریافت
									execute z187TranslateKalame @language,16163,@SharheTranslate2 output--ش د
									execute z187TranslateKalame @language,16173,@SharheTranslate3 output--ب خ
									execute z187TranslateKalame @language,16158,@SharheTranslate4 output--برگشت از خرید
										execute codemoinhesab 'kharid' ,@CodekolmoinTanzimat output
										execute codemoinhesab 'hesabhayepardakhtani' ,@CodekolMoinHesabhayePardakhtaniTanzimat output	

										set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
										update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckPardakhti
										update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckdaryafti

							
										set @ShomareFactor =(Select  fldShomareFactorKharid from tblBargashtAzKharid Where fldShomareSanad = @ShomareSanad)
												
										update #tmpSooratHesab2
											set fldSharh =@SharheTranslate1 + case when  @SharheKol='' then '' else ' - ' +@SharheKol end ,Shomare=@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' + @SharheTranslate3 + Convert(varchar, @ShomareFactor )  where fldSanad=@ShomareSanad And fldMoinKamel =  @CodekolMoinHesabhayePardakhtaniTanzimat  and fldBestankar>0

										if @CodekolmoinTanzimat = @CodeKolMoin --اگر سند خرید بود
											begin
									
												set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)

											end

										else if @CodekolMoinHesabhayePardakhtaniTanzimat = @CodeKolMoin  -- اگر سند مربوط به دریافت در برگشت از خرید بود
											begin
												set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)									
												set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
												set @ShomareFactor =(Select  fldShomareFactorKharid from tblBargashtAzKharid Where fldShomareSanad = @ShomareSanad)
												set @ShomareDaryaft =(Select  fldShomareDaryaft from tblDaryaft Where fldShomareSanad = @ShomareSanad)
												set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
												set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)
									
									
												execute	ShowDaryaftInfo @ShomareDaryaft, @str1, @lngAlaki, @lngAlaki2, @SharheKol output, 'bargashtazkharid', @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3


												set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
												set @ShomareFactor =(Select  fldShomareFactorkharid from tblBargashtAzKharid Where fldShomareSanad = @ShomareSanad)

												--اضافه کردن نام کاربر
												 set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'برگشت از خرید' and NoeAmaliat=N'جدید' order by id desc)
												 if @codekarbar is not null
													set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'برگشت از خرید' and NoeAmaliat=N'جدید' order by id desc)
												 else
													begin
														set @codekarbar=0
														set @namekarbar =''
													end
												update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad


									
												if @bigbed>0  or (@bigbes=0 and @bigbed=0)
													begin
														update #tmpSooratHesab2 set shomare = @SharheTranslate3+Convert(varchar, @ShomareFactor) ,fldSharh=' ' + @SharheTranslate4 + case when fldSharh='' then '' else '-' +fldSharh end   where ID=@EmpId
														update #tmpSooratHesab2 set shomare = @SharheTranslate3+Convert(varchar, @ShomareFactor) where fldSanad= @ShomareSanad

																						

														insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
																fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
																fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
																fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
																fldCodeAnbar,fldNameAnbar,fldType)
														Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),@SharheTranslate3+Convert(varchar, @ShomareFactor),@ShomareSanad, dbo.tblKala.fldNameKala + ' ' + dbo.tblRizeBargashtAzkharid.fldSharhKala,
																@bigDate,dbo.tblRizeBargashtAzkharid.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeBargashtAzkharid.fldFeeJoz,dbo.tblRizeBargashtAzkharid.fldFeeYekCarton,
																dbo.tblRizeBargashtAzkharid.fldFeeKol,dbo.tblRizeBargashtAzkharid.fldDarsadTakhfif,dbo.tblRizeBargashtAzkharid.fldMablaghTakhfif,dbo.tblRizeBargashtAzkharid.fldFeeVahed1BadAzTakhfif, 
																dbo.tblRizeBargashtAzkharid.fldFeeVahed2BadAzTakhfif, dbo.tblRizeBargashtAzkharid.fldArzeshAfzode, dbo.tblRizeBargashtAzkharid.fldFeeKolBadAzTakhfif,dbo.tblRizeBargashtAzkharid.fldMablaghKhales,
																dbo.tblRizeBargashtAzkharid.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'bargashtazkharid'
														FROM            dbo.tblAnbar INNER JOIN
																					dbo.tblRizeBargashtAzkharid ON dbo.tblAnbar.fldCode = dbo.tblRizeBargashtAzkharid.fldCodeAnbar INNER JOIN
																					dbo.tblKala ON dbo.tblRizeBargashtAzkharid.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
																					dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
																					dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
														WHERE        (dbo.tblRizeBargashtAzkharid.fldShomareFactor = @ShomareFactor)
										




														update #tmpSooratHesab2
															set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayePardakhtaniTanzimat 

											

									
														begin
															--set @ShomareSanad=@ShomareSanad
																if @lngNaghd>0
																	begin

																		set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
																		insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																			values (Convert(varchar,@EmpId+1) + '101' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' +@SharheTranslate3  + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end ,@bigDate,@lngNaghd,'bargashtazkharid')
																	end

																if @lngBank>0
																	begin
																		set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
																		insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																			values (Convert(varchar,@EmpId+1) + '102' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' +@SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strShareBank='' then '' else '-' +@strShareBank end  ,@bigDate,@lngBank,'bargashtazkharid')
																	end
									
																if @lngCheck>0 
																	begin
																		execute z187TranslateKalame @language,16165,@SharheTranslate1 output--چک شماره
																		execute z187TranslateKalame @language,16166,@SharheTranslate output--تاریخ
																		insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																				select Convert(varchar,@EmpId+1) +'103'   + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' +@SharheTranslate3 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate1 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + @SharheTranslate + ' ' + Convert(varchar,fldDateSarResid) +  case when @strSharheCheck='' then '' else '-' +@strSharheCheck end  ,@bigDate,fldMablagh,'bargashtazkharid'
																					from tblCheckeDaryafti Where fldSanad = @ShomareSanad

																		update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad and fldMoinKamel = @strMoinKamelCheckDaryafti
																	end

																if @lngTakhfif>0
																	begin
																		execute z187TranslateKalame @language,16185,@SharheTranslate output--تخفیفات خرید
																		set @strSharheHesab =@SharheTranslate + ' '
																		insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																			values (Convert(varchar,@EmpId+1) + '104' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' +@SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end  ,@bigDate,@lngTakhfif,'bargashtazkharid')
																	end

												
															end
													end
									
											end
								
									end

								if @type='daryaft'   ---برای نمایش ریز دریافت مستقیم
									begin
										set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
										set @ShomareDaryaft =(Select  fldShomareDaryaft from tblDaryaft Where fldShomareSanad = @ShomareSanad)

										--if @ShomareSanad=408
										--	begin
										--		INSERT INTO dbo.tblTest5
										--				 (name1, name2, name3)
										--					VALUES        (@bigDate,@ShomareDaryaft,'aa')
											--end

										execute z187TranslateKalame @language,16159,@SharheTranslate1 output--دریافت
										execute z187TranslateKalame @language,16163,@SharheTranslate2 output--ش د
										execute z187TranslateKalame @language,16165,@SharheTranslate3 output--چک شماره
										execute z187TranslateKalame @language,16166,@SharheTranslate4 output--تاریخ
										execute z187TranslateKalame @language,16186,@SharheTranslate5 output--تخفیفات دریافت


										set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
										set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)
										update #tmpSooratHesab2 set fldSharh=@SharheTranslate1 ,Shomare=@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) Where ID=@EmpId

										execute	ShowDaryaftInfo @ShomareDaryaft, @str1, @lngAlaki, @lngAlaki2, @SharheKol output, 'bargashtazkharid', @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3


										--اضافه کردن نام کاربر
										set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomareDaryaft and fldNameForm='دريافت' and NoeAmaliat='جدید' order by id desc)
										if @codekarbar is not null
										set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomareDaryaft and fldNameForm=N'دريافت' and NoeAmaliat='جدید' order by id desc)
										else
										begin
											set @codekarbar=0
											set @namekarbar =''
										end
										update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad

										if @bigbes>0
											begin
												if @lngNaghd>0
												begin
													set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
													insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
														values (Convert(varchar,@EmpId) + '101' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) ,@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end ,@bigDate,@lngNaghd,'daryaft')
												end

												if @lngBank>0
												begin
													set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
													insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
														values (Convert(varchar,@EmpId)+ '102' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) ,@ShomareSanad,@strSharheHesab + case when @strShareBank='' then '' else '-' +@strShareBank end ,@bigDate,@lngBank,'daryaft')
												end
									
												if @lngCheck>0 
												begin
													insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

													select Convert(varchar,@EmpId)+'103'  + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft)  ,@ShomareSanad,@SharheTranslate3 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + @SharheTranslate4 +' ' + Convert(varchar,fldDateSarResid) +  case when @strSharheCheck='' then '' else '-' +@strSharheCheck end  ,@bigDate,fldMablagh,'daryaft'
													from tblCheckeDaryafti Where fldSanad = @ShomareSanad

													update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad and fldMoinKamel = @strMoinKamelCheckDaryafti
												end

											if @lngTakhfif>0
												begin
													set @strSharheHesab = @SharheTranslate5 + ' '
													insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
														values (Convert(varchar,@EmpId) + '104' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) ,@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end  ,@bigDate,@lngTakhfif,'daryaft')
												end
								
											end
									end




								if @type = 'pardakht'  -- اگر سند مربوط به پرداخت اصلی بود
									begin
											execute z187TranslateKalame @language,16160,@SharheTranslate1 output--پرداخت
											execute z187TranslateKalame @language,16168,@SharheTranslate2 output--ش پ
											execute z187TranslateKalame @language,16170,@SharheTranslate3 output--شماره چک
											execute z187TranslateKalame @language,16166,@SharheTranslate4 output--تاریخ
											execute z187TranslateKalame @language,16172,@SharheTranslate5 output--واگذاری چک شماره

											set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)									
											set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)								
											set @ShomarePardakht =(Select  fldShomarePardakht from tblPardakht Where fldShomareSanad = @ShomareSanad)
											set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
											set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)
									
											execute	ShowPardakhtInfo @ShomarePardakht, @str1, @lngAlaki, @str1, @SharheKol output, @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output,@lngMablagheCheckeVagozari output, @strSCheckVaghozari output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3

											--اضافه کردن نام کاربر
											set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomarePardakht and fldNameForm='پرداخت' and NoeAmaliat='جدید' order by id desc)
											if @codekarbar is not null
											set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomarePardakht and fldNameForm=N'پرداخت' and NoeAmaliat='جدید' order by id desc)
											else
											begin
												set @codekarbar=0
												set @namekarbar =''
											end
											update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad


											set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)																	
												
											--if @bigbes>0 
											--	begin
													--update #tmpSooratHesab2 set shomare = 'ب ف'+Convert(varchar, @ShomareFactor) ,fldSharh=' برگشت از فروش' + iif(  fldSharh ='','','-' +  fldSharh ) where ID=@EmpId
													--update #tmpSooratHesab2 set shomare = 'ب ف'+Convert(varchar, @ShomareFactor) where fldSanad= @ShomareSanad
													--insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
													--		fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
													--		fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
													--		fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
													--		fldCodeAnbar,fldNameAnbar)
													--Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),'ب ف'+Convert(varchar, @ShomareFactor),@ShomareSanad, dbo.tblKala.fldNameKala + ' ' + dbo.tblRizeBargashtAzForoosh.fldSharhKala,
													--		@bigDate,dbo.tblRizeBargashtAzForoosh.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeBargashtAzForoosh.fldFeeJoz,dbo.tblRizeBargashtAzForoosh.fldFeeYekCarton,
													--		dbo.tblRizeBargashtAzForoosh.fldFeeKol,dbo.tblRizeBargashtAzForoosh.fldDarsadTakhfif,dbo.tblRizeBargashtAzForoosh.fldMablaghTakhfif,dbo.tblRizeBargashtAzForoosh.fldFeeVahed1BadAzTakhfif, 
													--		dbo.tblRizeBargashtAzForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldArzeshAfzode, dbo.tblRizeBargashtAzForoosh.fldFeeKolBadAzTakhfif,dbo.tblRizeBargashtAzForoosh.fldMablaghKhales,
													--		dbo.tblRizeBargashtAzForoosh.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar
													--FROM            dbo.tblAnbar INNER JOIN
													--							dbo.tblRizeBargashtAzForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeBargashtAzForoosh.fldCodeAnbar INNER JOIN
													--							dbo.tblKala ON dbo.tblRizeBargashtAzForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
													--							dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
													--							dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
													--WHERE        (dbo.tblRizeBargashtAzForoosh.fldShomareFactor = @ShomareFactor)
												--end


											
										if @bigbed>0
											begin

											--INSERT INTO dbo.tblTest5
											--		 (name1, name2, name3)
											--			VALUES        (@lngNaghd,@ShomarePardakht,@ShomareFactor)

								
												update #tmpSooratHesab2
														set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayePardakhtaniTanzimat 

												update #tmpSooratHesab2 set fldSharh=@SharheTranslate1 + ' ' +  fldSharh ,Shomare=@SharheTranslate2 + Convert(varchar,@ShomarePardakht) where id=@EmpId
												if @lngNaghd>0
													begin
														set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
														insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
															values (Convert(varchar,@EmpId) + '100' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) ,@ShomareSanad,@strSharheHesab + case when  @strSharhenaghd='' then '' else '-' +@strSharhenaghd end,@bigDate,@lngNaghd,'pardakht')
													end

												if @lngBank>0
													begin
														set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
														insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
															values (Convert(varchar,@EmpId) + '200' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) ,@ShomareSanad,@strSharheHesab + case when  @strShareBank='' then '' else '-' + @strShareBank end  ,@bigDate,@lngBank,'pardakht')
													end
									
												if @lngCheck>0 
													begin													
													
														insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

																select Convert(varchar,@EmpId ) + '300' + + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) ,@ShomareSanad,@SharheTranslate3 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + ' ' + @SharheTranslate4 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSharheCheck='' then '' else '-' +@strSharheCheck end  ,@bigDate,fldMablagh,'pardakht'
																	from tblCheckePardakhti Where fldSanad = @ShomareSanad													
													end

											----INSERT INTO dbo.tblTest5
											----	 (name1, name2, name3)
											----		VALUES        (@lngMablagheCheckeVagozari,'','hh')

											if @lngMablagheCheckeVagozari>0 
												begin													
													
													insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

															select Convert(varchar,@EmpId ) + '400' + Convert(varchar,id) ,@SharheTranslate2 ,@ShomareSanad,@SharheTranslate5 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + ' ' + @SharheTranslate4 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSCheckVaghozari='' then '' else '-' +@strSCheckVaghozari end ,@bigDate,fldMablagh,'pardakht'
																from tblCheckeDaryafti Where fldShomarePardakht = @ShomarePardakht													
												end

												if @lngTakhfif>0
													begin
														execute z187TranslateKalame @language,16187,@SharheTranslate output--تخفیفات پرداخت
														set @strSharheHesab = @SharheTranslate + ' '
														insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
															values (Convert(varchar,@EmpId ) + '500' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) ,@ShomareSanad,@strSharheHesab + case when @strSharheTakhfifat='' then '' else ' - ' +@strSharheTakhfifat end ,@bigDate,@lngTakhfif,'pardakht')
													end
								



										end		    
								
									end

									if @type='EbtedaAshkhas'
										begin
											execute z187TranslateKalame @language,16174,@SharheTranslate1 output--طلب ابتدای دوره
											execute z187TranslateKalame @language,16175,@SharheTranslate2 output--بدهی ابتدای دوره

											update #tmpSooratHesab2 set fldSharh=@SharheTranslate1 + case when fldSharh<>'' and fldSharh is not null then '-' +fldSharh else '' end,fldType='ebtedadore' Where fldBestankar>0 and ID=@EmpId 
											update #tmpSooratHesab2 set fldSharh=@SharheTranslate2 + case when fldSharh<>'' and fldSharh is not null then '-' +fldSharh else '' end,fldType='ebtedadore' Where fldBedehkar>0 and ID=@EmpId 
										end

									if @type is null
										begin
											declare @strNameHesab nvarchar(50),@strCodeMoinKamel nvarchar(50)

											set @strcodemoinkamel = (Select fldMoinKamel from #tmpSooratHesab2 Where ID=@EmpId)
											execute NameKolMoin @strcodemoinkamel  ,@strNameHesab output
											update #tmpSooratHesab2 set fldSharh=@strNameHesab + case when fldSharh<>'' and fldSharh is not null then '-' +fldSharh else '' end,fldType='other' Where   ID=@EmpId 
										end
									if @type='amani'
										begin
											declare @ShomareAmani bigint
											declare @bigDate2 bigint									
											set @bigDate2=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)

											set @ShomareAmani =(Select  Top(1)fldShomareAmani from tblRizeForoosh  Where fldShomareSanad = @ShomareSanad)
											update #tmpSooratHesab2 set shomare = 'امانی'+Convert(varchar, @ShomareAmani), fldBedehkar=0, fldSharh='فروش امانی' + case when fldSharh<>'' and fldSharh is not null then '-' +fldSharh else '' end,fldType='amani' Where fldBedehkar>0 and ID=@EmpId 

											insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
														fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
														fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
														fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
														fldCodeAnbar,fldNameAnbar,fldType)
												Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),'امانی'+Convert(varchar, @ShomareAmani),@ShomareSanad, dbo.tblKala.fldNameKala + ' ' + dbo.tblRizeForoosh.fldSharhKala,
														@bigDate2,dbo.tblRizeForoosh.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeForoosh.fldFeeJoz,dbo.tblRizeForoosh.fldFeeYekCarton,
														dbo.tblRizeForoosh.fldFeeKol,dbo.tblRizeForoosh.fldDarsadTakhfif,dbo.tblRizeForoosh.fldMablaghTakhfif,dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, 
														dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif,dbo.tblRizeForoosh.fldMablaghKhales,
														dbo.tblRizeForoosh.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'amani'
												FROM            dbo.tblAnbar INNER JOIN
																		 dbo.tblRizeForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeForoosh.fldCodeAnbar INNER JOIN
																		 dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
																		 dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
																		 dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
												WHERE        (dbo.tblRizeForoosh.fldShomareAmani = @ShomareAmani)
										end

										if @type='checkebteda'
											begin									
												execute z187TranslateKalame @language,16178,@SharheTranslate1 output--چک دریافتی ابتدای دوره		
												execute z187TranslateKalame @language,16179,@SharheTranslate2 output--چک ابتدای دوره
												execute z187TranslateKalame @language,16165,@SharheTranslate3 output--چک شماره
												execute z187TranslateKalame @language,16166,@SharheTranslate4 output--تاریخ

									
												set @bigDate2=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)

												--set @ShomareAmani =(Select  Top(1)fldShomareAmani from tblRizeForoosh  Where fldShomareSanad = @ShomareSanad)
												update #tmpSooratHesab2 set fldBedehkar=0,fldBestankar=0, shomare = @SharheTranslate1,  fldSharh=@SharheTranslate2 + case when fldSharh<>'' and fldSharh is not null then '-' +fldSharh else '' end,fldType='checkebteda' Where fldBedehkar>0 and ID=@EmpId 

												insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
													select Convert(varchar,@EmpId)+'103'  + Convert(varchar,id) ,@SharheTranslate2  ,@ShomareSanad,@SharheTranslate3 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + @SharheTranslate4 + ' ' + Convert(varchar,fldDateSarResid) +  case when @strSharheCheck='' then '' else '-' +@strSharheCheck end  ,@bigDate2,fldMablagh,'checkebteda'
													from tblCheckeDaryafti Where fldSanad = @ShomareSanad and fldCodeSahebeCheck=12326

												--insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
												--			fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
												--			fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
												--			fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
												--			fldCodeAnbar,fldNameAnbar,fldType)
												--	Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),'امانی'+Convert(varchar, @ShomareAmani),@ShomareSanad, dbo.tblKala.fldNameKala + ' ' + dbo.tblRizeForoosh.fldSharhKala,
												--			@bigDate2,dbo.tblRizeForoosh.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeForoosh.fldFeeJoz,dbo.tblRizeForoosh.fldFeeYekCarton,
												--			dbo.tblRizeForoosh.fldFeeKol,dbo.tblRizeForoosh.fldDarsadTakhfif,dbo.tblRizeForoosh.fldMablaghTakhfif,dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, 
												--			dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif,dbo.tblRizeForoosh.fldMablaghKhales,
												--			dbo.tblRizeForoosh.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'amani'
												--	FROM            dbo.tblAnbar INNER JOIN
												--							 dbo.tblRizeForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeForoosh.fldCodeAnbar INNER JOIN
												--							 dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
												--							 dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
												--							 dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
												--	WHERE        (dbo.tblRizeForoosh.fldShomareAmani = @ShomareAmani)
											end

							
								if @type='BargashteCheckeVagoz'   ---برگشت چک واگذارشده
									begin									
												declare @ShomarePardakht2 bigint,@CodeTafzili2 nvarchar(10),@SumBed2 decimal (18, 4),@Bed2 decimal (18, 4),@ShomareSanad2  bigint,@cnt2 bigint,@ShomareEnteghal bigint,@CodeMoin2 nvarchar(50)
												set @bigDate2=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
												set @ShomareSanad2=(Select fldSanad from tblSodooresanad Where ID=@EmpId)
												set @CodeTafzili2=(Select fldCodeTafzili from tblSodooresanad Where ID=@EmpId)
												set @SumBed2=(Select Sum(fldBedehkar) as SumBed from tblSodooreSanad Where fldCodeTafzili=@CodeTafzili2 And fldBedehkar>0 and fldSanad=@ShomareSanad2)
												set @Bed2=(Select top(1)fldBedehkar from tblSodooreSanad Where ID=@EmpId)
												set @ShomareEnteghal=(Select top(1)fldShomareEnteghal from tblCheckeDaryafti Where fldShomareSanadEnteghal=@ShomareSanad2)
												execute CodeMoinHesab 'check',@codemoin2 output
												--insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
												--			values ( 999 ,'621'  ,132,'345',990205,111,'dfg')

												--set  @cnt2=(Select count(fldCodeTafzili) from tblTafzili Where fldCodeTafzili=@CodeTafzili2 and ID<@EmpId )
												--if @cnt2=0
												--	begin
														if @Bed2>0 
															begin																
																set @ShomarePardakht2=(Select top(1)fldShomarePardakht from tblCheckeDaryafti Where fldShomareSanadEnteghal=@ShomareSanad2)
									

																--update #tmpSooratHesab2 set  fldBestankar=6666, shomare =@ShomareEnteghal,  fldSharh='' + case when fldSharh<>'' and fldSharh is not null then ' ' +fldSharh else '' end,fldType='BargashteCheckeVagozarShode' Where fldBedehkar>0 and ID=@EmpId 

																--insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																--	select Convert(varchar,@EmpId)+'103' ,'bbb'  ,@ShomareSanad2,'چک شماره ' + Convert(varchar,fldShomareCheck) + ' تاریخ ' + Convert(varchar,fldDateSarResid)   ,@bigDate2,fldMablagh,'BargashteCheckeVagozarShode'
																--	from tblCheckeDaryafti Where fldShomareSanadEnteghal = @ShomareSanad2 and  fldShomarePardakht=21
																--update #tmpSooratHesab2 set shomare =34 Where id=@EmpId --and fldMoinKamel <> @codemoin2 and fldCodeTafzili=@CodeTafzili2

																update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad2 and fldMoinKamel = @codemoin2 and fldCodeTafzili=@CodeTafzili2
															end
														else
															begin
																update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad2 and fldMoinKamel = @codemoin2 and fldCodeTafzili=@CodeTafzili2
																update #tmpSooratHesab2 set shomare =@ShomareEnteghal Where id=@EmpId --and fldMoinKamel <> @codemoin2 and fldCodeTafzili=@CodeTafzili2
															end 

												--	end
												--else
												--	update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad2 and fldMoinKamel = '31301' and fldCodeTafzili=@CodeTafzili2
													--set @ShomarePardakht2=(Select top(1)fldShomarePardakht from tblCheckeDaryafti Where fldShomareCheck)
													--set @ShomarePardakht2=(select fldCodeTafzili from tblPardakht where fldShomarePardakht=@ShomarePardakht2)

										
													--select * from tblCheckeDaryafti Where fldShomareSanadEnteghal = 132

									end

						

									if @type='OdateCheck'   ---عودت چک فروخته شده
										begin									
													declare @ShomarePardakht3 bigint,@CodeTafzili3 nvarchar(10),@SumBed3 decimal (18, 4),@BeS3 decimal (18, 4),@ShomareSanad3  bigint,@cnt3 bigint,@ShomareEnteghal3 bigint,@CodeMoin3 nvarchar(50)
													set @bigDate2=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
													set @ShomareSanad3=(Select fldSanad from tblSodooresanad Where ID=@EmpId)
													set @CodeTafzili3=(Select fldCodeTafzili from tblSodooresanad Where ID=@EmpId)
													set @SumBed3=(Select Sum(fldBedehkar) as SumBed from tblSodooreSanad Where fldCodeTafzili=@CodeTafzili3 And fldBedehkar>0 and fldSanad=@ShomareSanad3)
													set @BeS3=(Select top(1)fldBestankar from tblSodooreSanad Where ID=@EmpId)
													set @ShomareEnteghal=(Select top(1)fldShomareOdat from tblCheckeDaryafti Where fldShomareSanadOdat=@ShomareSanad3)
													execute CodeMoinHesab 'check',@codemoin3 output
													--insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
													--			values ( 999 ,'621'  ,132,'345',990205,111,'dfg')

													--set  @cnt2=(Select count(fldCodeTafzili) from tblTafzili Where fldCodeTafzili=@CodeTafzili2 and ID<@EmpId )
													--if @cnt2=0
													--	begin
															if @BeS3>0 
																begin																
																	set @ShomarePardakht3=(Select top(1)fldShomarePardakht from tblCheckeDaryafti Where fldShomareSanadEnteghal=@ShomareSanad3)
									

																	update #tmpSooratHesab2 set   shomare =@ShomareEnteghal,  fldSharh='' + case when fldSharh<>'' and fldSharh is not null then ' ' +fldSharh else '' end,fldType='OdateCheck' Where fldBedehkar>0 and ID=@EmpId 

																	--insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																	--	select Convert(varchar,@EmpId)+'103' ,'bbb'  ,@ShomareSanad2,'چک شماره ' + Convert(varchar,fldShomareCheck) + ' تاریخ ' + Convert(varchar,fldDateSarResid)   ,@bigDate2,fldMablagh,'BargashteCheckeVagozarShode'
																	--	from tblCheckeDaryafti Where fldShomareSanadEnteghal = @ShomareSanad2 and  fldShomarePardakht=21

																	update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad3 and fldMoinKamel = @codemoin3 and fldCodeTafzili=@CodeTafzili3
																end
															else
																begin
																	update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad3 and fldMoinKamel = @codemoin3 and fldCodeTafzili=@CodeTafzili3
																	update #tmpSooratHesab2 set shomare =@ShomareEnteghal Where id=@EmpId 
																end

													--	end
													--else
													--	update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad2 and fldMoinKamel = '31301' and fldCodeTafzili=@CodeTafzili2
														--set @ShomarePardakht2=(Select top(1)fldShomarePardakht from tblCheckeDaryafti Where fldShomareCheck)
														--set @ShomarePardakht2=(select fldCodeTafzili from tblPardakht where fldShomarePardakht=@ShomarePardakht2)

										
														--select * from tblCheckeDaryafti Where fldShomareSanadEnteghal = 132

										end

									if @Type='bazpascheckekhodeman'  --بازپسگیری چک خود
										begin									
													declare @ShomarePardakht4 bigint,@CodeTafzili4 nvarchar(10),@SumBed4 decimal (18, 4),@Bed4 decimal (18, 4),@ShomareSanad4  bigint,@CodeMoin4 nvarchar(50)
													set @bigDate2=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
													set @ShomareSanad4=(Select fldSanad from tblSodooresanad Where ID=@EmpId)
													set @CodeTafzili4=(Select fldCodeTafzili from tblSodooresanad Where ID=@EmpId)
													set @SumBed4=(Select Sum(fldBedehkar) as SumBed from tblSodooreSanad Where fldCodeTafzili=@CodeTafzili4 And fldBedehkar>0 and fldSanad=@ShomareSanad4)
													set @Bed4=(Select top(1)fldBedehkar from tblSodooreSanad Where ID=@EmpId)
													set @ShomareEnteghal=(Select top(1)fldShomareOdat from tblCheckeDaryafti Where fldShomareSanadOdat=@ShomareSanad4)
													execute CodeMoinHesab 'checkpardakhti',@codemoin4 output

													if @Bed4>0 
														begin																
															set @ShomarePardakht4=(Select top(1)fldShomarePardakht from tblCheckeDaryafti Where fldShomareSanadEnteghal=@ShomareSanad4)
									

															update #tmpSooratHesab2 set   shomare =@ShomareEnteghal,  fldSharh='' + case when fldSharh<>'' and fldSharh is not null then ' ' +fldSharh else '' end,fldType='OdateCheck' Where fldBedehkar>0 and ID=@EmpId 

															update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad3 and fldMoinKamel = @codemoin4 and fldCodeTafzili=@CodeTafzili4
														end
													else
														begin
															update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad4 and fldMoinKamel = @codemoin4 and fldCodeTafzili=@CodeTafzili4
															update #tmpSooratHesab2 set shomare =@ShomareEnteghal Where id=@EmpId 
														end

										end


										--if @type='ekhtetamie'
										--	begin
										--		update #tmpSooratHesab2
										--				set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel= @strMoinKamelCheckDaryafti 
										--	end
										--if @type='eftetahie'
										--	begin
										--		update #tmpSooratHesab2
										--				set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel= @strMoinKamelCheckDaryafti 
										--	end
							Fetch Next from EmpCursor
							into @EmpId
						end
				Close EmpCurSor
				Deallocate EmpCursor

				--set @select ='Select ID, fldSanad, fldDay, fldMonth, fldYear, fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
			 --                        fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
			 --                        fldVasete from #tmpSooratHesab2'

				--execute (@Select)
				--Select ID, fldSanad,  fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
			 --                       fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
			 --                       fldVasete from #tmpSooratHesab2



			  execute codemoinhesab 'hesabhayedaryaftani' ,@CodekolMoinDaryaftTanzimat output	

				INSERT INTO #tmpSooratHesab2
									 (ID, fldSanad, fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
									 fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
									 fldVasete)
								Select ID, fldSanad,fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
									 fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
									 fldVasete
								from tblSodooreSanad 
									Where fldCodeTafzili=@CodeTafzili and fldMoinKamel=@CodekolMoinDaryaftTanzimat and fldType='ebteda'

				--رکوردهایی که در پایان باید حذف بشن
				delete #tmpSooratHesab2 where fldVaziat='delete'

				--#### حذف محدوده ها ######


				set @SumBedGhabl=(Select Sum(fldBedehkar) as SumBed from  #tmpSooratHesab2  Where fldBestankar is not null and fldBedehkar is not null and Convert(dec,fldDate)<@bigDateMin)
				if @SumBedGhabl is null
					set @SumBedGhabl =0

				set @SumBesGhabl=(Select Sum(fldBestankar) as SumBes from  #tmpSooratHesab2  Where fldBestankar is not null and fldBedehkar is not null  and Convert(dec,fldDate)<@bigDateMin)
				if @SumBesGhabl is null
					set @SumBesGhabl=0

				delete #tmpSooratHesab2  Where Convert(dec,fldDate)<@bigDateMin
				delete #tmpSooratHesab2  Where Convert(dec,fldDate)>@bigDateMax
	
				update #tmpSooratHesab2 set fldNameVahed2='' Where fldNameVahed2=N'انتخاب کنید'

								

				if @flagMande =1 --با نقل مانده		
					--if (@SumBedGhabl-@SumBesGhabl)<>0
					--	insert into #tmpSooratHesab2
					--		(id,shomare,fldSanad,fldSharh,fldDate,fldBedehkar,fldBestankar,fldMande,fldtype)
					--			values
					--		('0','','0','مانده قبل','0',@SumBedGhabl,@SumBesGhabl,@SumBedGhabl-@SumBesGhabl,'ghabl')
					begin					
							declare @CodeKol2 nvarchar(100),@MandeTalab2 decimal (18, 4),@MandeBedehi2 decimal (18, 4)

							execute CodeKolHesab 'kolhesabhayedaryaftani',@CodeKol2 output

							set @MandeTalab2=(SELECT       ISNULL( SUM(ISNULL(fldBedehkar, 0)-ISNULL(fldBestankar, 0)),0) AS SumM
								FROM            tblSodooreSanad
								WHERE      (fldCodeKol=@CodeKol2) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldDate)<@bigDateMin)

					

							execute CodeKolHesab 'kolhesabhayepardakhtani',@CodeKol2 output

							set @MandeBedehi2=(SELECT       ISNULL( SUM(ISNULL(fldBestankar, 0)-ISNULL(fldBedehkar, 0)),0) AS SumM
								FROM            tblSodooreSanad
								WHERE      (fldCodeKol=@CodeKol2) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldDate)<@bigDateMin)
							set @decMandeGhabl=@MandeTalab2-@MandeBedehi2								
						if @decMandeGhabl<>0
							execute z187TranslateKalame @language,16180,@SharheTranslate output--مانده قبل
							insert into #tmpSooratHesab2
								(id,shomare,fldSanad,fldSharh,fldDate,fldBedehkar,fldBestankar,fldMande,fldtype)
									values
								('0','','0',@SharheTranslate,@bigDateMin,@MandeTalab2,@MandeBedehi2,@decMandeGhabl,'ghabl')
					end 


				--select * from #tmpSooratHesab2 order by Convert(dec,fldSanad),Convert(varchar,id)
				declare @strOrder nvarchar(30)

				--if @intOrder=0  --مرتب سازی براساس تاریخ
				--	select * from #tmpSooratHesab2 order by Convert(dec,fldDate),Convert(varchar,id)
				--else --مرتب سازی براساس سند
				--	select * from #tmpSooratHesab2 order by Convert(dec,fldSanad),Convert(varchar,id)

				--update  #tmpSooratHesab2 set flddate=substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2)
				delete from #tmpSooratHesab2 where fldSanad in (Select fldSanadPas from tblCheckePardakhti Where fldCodeSahebeCheck=@CodeTafzili )
	
				declare @MaxId bigint
				set @Maxid =(Select Max(Convert(dec,Id)) as MaxId from #tmpSooratHesab2)
				if isnumeric(@maxId)=0
					set @maxId=1
				else
					set @maxId=@maxId+1


				execute z187TranslateKalame @language,16181,@SharheTranslate1 output--برگشت چک
				execute z187TranslateKalame @language,16182,@SharheTranslate2 output--برگشت چک به شماره
				--set @MaxId=1000
				insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
						Select Convert(varchar,@MaxId),@SharheTranslate1 ,'0',@SharheTranslate2 + ' '  + Convert(varchar,fldShomareCheck),substring(Convert(varchar,fldDateBargasht),3,6),fldMablagh,'bargasht' 
							FROM tblCheckeDaryafti
							Where fldVaziat='Bargasht' and fldCodeSahebeCheck=@CodeTafzili
			
						--values ('2000' ,'ش برگشت' ,'',''  ,0,0,'bargasht')
							--values (Convert(varchar,@EmpId) + '200' ,'ش برگشت' + Convert(varchar,@ShomarePardakht) ,@ShomareSanad,@strSharheHesab + case when  @strShareBank='' then '' else '-' + @strShareBank end  ,@bigDate,@lngBank,'pardakht')



				if @intOrder=0  --مرتب سازی براساس تاریخ
					select  ID,radif,shomare,fldSanad,fldSharh,fldRiz,substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate,fldTedad,fldNameVahed,
						fldNameVahed2,fldFee,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,
						fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,
						fldMablaghKhales,fldCodeAnbar,fldNameAnbar,fldMablagh,fldBedehkar,fldBestankar,fldMande,
						fldType,fldVaziat,fldVasete,fldCodeKarbar,fldNameKarbar
					from #tmpSooratHesab2  order by Convert(dec,fldDate),Convert(varchar,id)
					--select  shomare,fldSanad,fldSharh,fldRiz,fldDate,fldTedad,fldNameVahed,
					--	fldFee,fldMablaghKhales,fldBedehkar,fldBestankar,fldMande			
					--	from #tmpSooratHesab2  order by Convert(dec,fldDate),Convert(varchar,id)
				else
					if @intOrder=1  --مرتب سازی براساس سند
						select ID,radif,shomare,fldSanad,fldSharh,fldRiz,substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate,fldTedad,fldNameVahed,
							fldNameVahed2,fldFee,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,
							fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,
							fldMablaghKhales,fldCodeAnbar,fldNameAnbar,fldMablagh,fldBedehkar,fldBestankar,fldMande,
							fldType,fldVaziat,fldVasete,fldCodeKarbar,fldNameKarbar
						from #tmpSooratHesab2  order by Convert(dec,fldSanad),Convert(varchar,id)
		end
end





GO
/****** Object:  StoredProcedure [dbo].[SooratHesab22]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[SooratHesab22]
@mm bigint output
as
begin
	Select *
		Into   #Temp
		From   tblSodooreSanad

		Declare @Id int,@Jam bigint
		set @Jam=0

		While (Select Count(*) From #Temp) > 0
		Begin

			Select Top 1 @Id = Id From #Temp

			--Do some processing here

			--Delete #Temp Where Id = @Id
			set @jam=@Jam+( Select fldBedehkar from #Temp Where Id = @Id)
		End
		set @mm=@jam
end

GO
/****** Object:  StoredProcedure [dbo].[SooratHesabMulti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[SooratHesabMulti]
--چندزبانه
@fast nvarchar(5),
@CodeTafzili nvarChar(10),
@bigDateMin bigint ,
@bigDateMax bigint ,
@flagMande int ,
@intOrder int,
@Language nvarchar(20),
@SumBedGhabl decimal (18, 4) output,
@SumBesGhabl decimal (18, 4) output
as


begin
	declare @EmpId bigint,@Select nvarchar(500),@Type nvarchar(50),@ShomareFactor bigint,@CodeKolMoin nvarchar(50),@CodeKolMoinTanzimat nvarchar(50),@CodeKolTanzimat nvarchar(50),@CodeKolSanad nvarchar(50)
	declare @ShomareSanad bigint,@CodekolMoinDaryaftTanzimat nvarchar(10),@ShomareSanadEbteda bigint,@SharheTranslate1 nvarchar(max),@SharheTranslate2 nvarchar(max),@SharheTranslate3 nvarchar(max),@SharheTranslate4 nvarchar(max),@SharheTranslate5 nvarchar(max),@SharheTranslate6 nvarchar(max),@SharheTranslate7 nvarchar(max),@SharheTranslate nvarchar(max)
	declare @ShomareDaryaft bigint,@ShomarePardakht bigint, @strAlaki nvarchar(50),@lngAlaki bigint,@SharheKol nvarchar(200) ,@lngNaghd decimal (18, 4),@strMoinKamelSandoogh nvarchar(10),@strSharhenaghd nvarchar(200) ,@lngBank decimal (18, 4),@strMoinKamelBank nvarchar(10),@strShareBank nvarchar(200),@lngCheck decimal (18, 4),@strSharheCheck nvarchar(200),@strMoinKamelCheckDaryafti nvarchar(10),@strMoinKamelCheckPardakhti nvarchar(10),@lngMablagheCheckeVagozari decimal (18, 4),@strSCheckVaghozari nvarchar(200),@strMoinKamelCheckVagozari nvarchar(10),@lngTakhfif decimal (18, 4),@strSharheTakhfifat nvarchar(200)
	declare @str1 nvarchar(200),@str2 nvarchar(200),@str3 nvarchar(200),@str4 nvarchar(200),@str5 nvarchar(200),@str6 nvarchar(200),@str7 nvarchar(200),@lngAlaki2 bigint
	declare @CodekolMoinHesabhayeDaryaftaniTanzimat nvarchar(10),@CodekolMoinHesabhayePardakhtaniTanzimat nvarchar(10),@strSharheHesab nvarchar(50),@bigBed decimal (18, 4),@bigBes decimal (18, 4), @CodeMoinKamelHesabhayePardakhtaniTanzimat  nvarchar(10)	

create table #tblKalaTemp(
fldCodeKala bigint,
fldNamekala nvarchar(100),
fldCodeVahed1 bigint,
fldCodeVahed2 bigint
)


	CREATE TABLE #tmpSooratHesab2(
	[ID] [nvarchar](20) COLLATE Persian_100_CI_AS  NULL  ,
	[radif] [bigint]  null,
	[shomare] [nvarchar](100) COLLATE Persian_100_CI_AS null  ,
	[fldSanad] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldSharh] [nvarchar](150) COLLATE Persian_100_CI_AS NULL  ,
	[fldRiz] [nvarchar](150) COLLATE Persian_100_CI_AS NULL  ,
	[fldDate] [bigint] NULL,
	[fldTedad][float] null,
	[fldNameVahed] [nvarchar](100)  COLLATE Persian_100_CI_AS,
	[fldNameVahed2] [nvarchar](100)  COLLATE Persian_100_CI_AS,
	[fldFee]decimal (18, 4) null,
	[fldFeeYekCarton]decimal (18, 4) null,
	[fldFeeKol]decimal (18, 4) null,
	[fldDarsadTakhfif]decimal (18, 4) null,
	[fldMablaghTakhfif]decimal (18, 4) null,
	[fldFeeVahed1BadAzTakhfif]decimal (18, 4) null,
	[fldFeeVahed2BadAzTakhfif]decimal (18, 4) null,
	[fldArzeshAfzode]decimal (18, 4) null,
	[fldFeeKolBadAzTakhfif]decimal (18, 4) null,
	[fldMablaghKhales]decimal (18, 4) null,
	[fldCodeAnbar] [bigint] null,	
	[fldNameAnbar][nvarchar](50) COLLATE Persian_100_CI_AS null  ,
	[fldCodeKarbar] bigint,
	[fldNameKarbar] [nvarchar](25) COLLATE Persian_100_CI_AS NULL ,


	[fldMablagh]decimal (18, 4) null,
	[fldBedehkar] [decimal](18, 4) NULL,
	[fldBestankar] [decimal](18, 4) NULL,
	[fldMande]decimal (18, 4) null,
	[fldCodeKol] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldCodeMoin] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldCodeTafzili] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldCodeMarkaz1] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldCodeMarkaz2] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldRadif] [varchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldCodeSharh] [varchar](50) COLLATE Persian_100_CI_AS NULL  ,		
	[fldMoinKamel] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldCodeNoeTafzili] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldGhateiMovaghat] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldGhateiDaem] [nvarchar](50) COLLATE Persian_100_CI_AS NULL ,
	[fldSanadGhadim] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldShomarePeigiri] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldMoghayerat] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldType] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  ,
	[fldVaziat][nvarchar](20) COLLATE Persian_100_CI_AS NULL  ,
	[fldVasete] [nvarchar](50) COLLATE Persian_100_CI_AS NULL  

) ON [PRIMARY]


---اسناد مربوط به کدتفصیلی یک به یک بررسی میشه و متناسب با نوع سند تحلیل و گسترش میباشد
		declare @bigDateMinForSanad bigint ,@bigDateMaxForSanad bigint ,@bigCodeKolHazine nvarchar(50),@bigCodeKolDaramad nvarchar(50) ,@bigCodeMoinKharid nvarchar(50),@strStartHazine nvarchar(1),@strStartDaramad nvarchar(1),@sanadCheckDaryaftEbteda bigint,@sanadCheckEbtedaPardakht bigint
		declare @tooleKol as int,@strKolForoosh nvarchar(50),@Asnadepardakhtani nvarchar(50),@Asnadepardaryaftani nvarchar(50),@CodemoinForoosh nvarchar(20)

		set @tooleKol=(Select top(1) fldKol from tblPaye)
		execute codemoinhesab 'foroosh' ,@strKolForoosh output

	set @bigDateMinForSanad=@bigDateMin
	set @bigDateMaxForSanad=@bigDateMax

	execute CodeKolHesab 'hazine',@bigCodeKolHazine output
	execute CodeKolHesab 'daramad',@bigCodeKolDaramad output
	execute CodeMoinHesab 'kharid',@bigCodeMoinKharid output
	execute CodeMoinHesab 'checkpardakhti',@Asnadepardakhtani output
	execute CodeMoinHesab 'foroosh',@CodemoinForoosh output

	set @strStartHazine=substring(Convert(varchar,@bigCodeKolHazine),1,1)
	set @strStartDaramad=substring(Convert(varchar,@bigCodeKolDaramad),1,1)
	execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoinKamelHesabhayePardakhtaniTanzimat output
	execute codemoinhesab 'checkpardakhti' ,@strMoinKamelCheckPardakhti output	
		execute codemoinhesab 'check' ,@Asnadepardaryaftani output
	execute codemoinhesab 'check' ,@strMoinKamelCheckDaryafti output
	update tblSodooreSanad set fldType='' where fldType is null

	set @ShomareSanadEbteda=(select fldShomareEftetahie from tblControlPanel)


	set @sanadCheckDaryaftEbteda=(select top(1)fldSanad from tblCheckeDaryafti Where fldShomareDaryaft=0)
	if @sanadCheckDaryaftEbteda is null
		set @sanadCheckDaryaftEbteda=0

	set @sanadCheckEbtedaPardakht=(select top(1)fldSanad from tblCheckePardakhti Where fldShomarePardakht=0)
	if @sanadCheckEbtedaPardakht is null
		set @sanadCheckEbtedaPardakht=0

		insert into #tblKalaTemp
			(fldCodeKala,fldNamekala,fldCodeVahed1,fldCodeVahed2)
			Select fldCodeKala,fldnamekala,fldCodeVahed1,fldCodeVahed2 from tblKala

			update #tblKalaTemp set #tblKalaTemp.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tblKalaTemp.fldCodeKala and tblkalatranslate.fldLanguage=@Language

		--(fldSanad<>@ShomareSanadEbteda and fldMoinKamel<>@strMoinKamelCheckDaryafti) and
if @fast='ok'
	begin
		declare @CodeKoleHesabhayeDaryaftani nvarchar(20),@CodeKoleHesabhayePardakhtani nvarchar(20)
		
		execute CodeKolHesab 'kolhesabhayedaryaftani',@CodeKoleHesabhayeDaryaftani output
		execute CodeKolHesab 'kolhesabhayepardakhtani',@CodeKoleHesabhayePardakhtani output
		

		insert into #tmpSooratHesab2 (fldCodeTafzili,fldSanad,fldRiz,fldDate,fldBedehkar,fldBestankar,fldCodeKol,fldCodeMoin,fldMoinkamel,fldType)			
		Select  fldCodeTafzili,fldSanad,fldSharh,fldDate,fldBedehkar,fldBestankar,fldCodeKol,fldCodeMoin,fldMoinkamel,fldType from tblSodooreSanad With (NoLock) where fldCodeTafzili=@CodeTafzili and (Convert(dec,fldDate) between @bigDateMinForSanad and @bigDateMaxForSanad) and (fldCodeKol=@CodeKoleHesabhayeDaryaftani or fldCodeKol=@CodeKoleHesabhayePardakhtani)   ORDER BY CONVERT(dec, fldSanad), CONVERT(dec, fldRadif)
                                                                                                            



		--update #tmpSooratHesab2 set fldRiz =fldRiz + ' ' +  tblTafzili.fldSharheTafzili from tblTafzili Where #tmpSooratHesab2.fldCodeTafzili=tblTafzili.fldCodeTafzili

		update #tmpSooratHesab2 set fldSharh='افتتاحیه' Where fldType='eftetahie'
		update #tmpSooratHesab2 set fldSharh='خرید' Where fldType='kharid'
		update #tmpSooratHesab2 set fldSharh='پرداخت' Where fldType='pardakht'
		update #tmpSooratHesab2 set fldSharh='دریافت' Where fldType='daryaft'
		update #tmpSooratHesab2 set fldSharh='برگشت از خرید' Where fldType='bargashtazkharid'
		update #tmpSooratHesab2 set fldSharh='فروش' Where fldType='foroosh'
		update #tmpSooratHesab2 set fldSharh='برگشت از فروش' Where fldType='bargashtazforoosh'

		update #tmpSooratHesab2 set fldriz='فاکتور خرید' + ' ' + Convert(varchar,fldShomareFactorKharid),shomare=fldShomareFactorKharid from tblKharid Where #tmpSooratHesab2.fldSanad=tblKharid.fldShomareSanad
		update #tmpSooratHesab2 set fldriz='برگشت از خرید ' + ' ' + Convert(varchar,fldShomareFactorKharid),shomare=fldShomareFactorKharid from tblBargashtAzKharid Where #tmpSooratHesab2.fldSanad=tblBargashtAzKharid.fldShomareSanad

		update #tmpSooratHesab2 set fldriz='فاکتور فروش' + ' ' + Convert(varchar,fldShomareFactorForoosh), shomare=fldShomareFactorforoosh from tblForoosh Where #tmpSooratHesab2.fldSanad=tblForoosh.fldShomareSanad
		update #tmpSooratHesab2 set fldriz='برگشت از فروش' + ' ' + Convert(varchar,fldShomareFactorForoosh), shomare=fldShomareFactorforoosh from tblBargashtAzForoosh Where #tmpSooratHesab2.fldSanad=tblBargashtAzForoosh.fldShomareSanad

		update #tmpSooratHesab2 set fldriz='دریافت شماره' + ' ' + Convert(varchar,fldShomareDaryaft), shomare=fldShomareDaryaft from tblDaryaft Where #tmpSooratHesab2.fldSanad=tblDaryaft.fldShomareSanad
		update #tmpSooratHesab2 set fldriz='پرداخت شماره' + ' ' + Convert(varchar,fldShomarePardakht), shomare=fldShomarePardakht from tblPardakht Where #tmpSooratHesab2.fldSanad=tblPardakht.fldShomareSanad

		if @flagMande =1 --با نقل مانده		
			begin
				declare @decMandeGhablBed decimal(18,0) ,@decMandeGhablBes decimal(18,0) ,@decMandeGhabl decimal(18,0) 

					declare @CodeKol nvarchar(100),@MandeTalab bigint,@MandeBedehi bigint

					execute CodeKolHesab 'kolhesabhayedaryaftani',@CodeKol output

					set @MandeTalab=(SELECT       ISNULL( SUM(ISNULL(fldBedehkar, 0)-ISNULL(fldBestankar, 0)),0) AS SumM
						FROM            tblSodooreSanad
						WHERE      (fldCodeKol=@CodeKol) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldDate)<@bigDateMin)

					

					execute CodeKolHesab 'kolhesabhayepardakhtani',@CodeKol output

					set @MandeBedehi=(SELECT       ISNULL( SUM(ISNULL(fldBestankar, 0)-ISNULL(fldBedehkar, 0)),0) AS SumM
						FROM            tblSodooreSanad
						WHERE      (fldCodeKol=@CodeKol) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldDate)<@bigDateMin)
					set @decMandeGhabl=@MandeTalab-@MandeBedehi								
				if @decMandeGhabl<>0
					insert into #tmpSooratHesab2
						(id,shomare,fldSanad,fldSharh,fldDate,fldBedehkar,fldBestankar,fldMande,fldtype)
							values
						('0','','0','مانده قبل',@bigDateMin,@MandeTalab,@MandeBedehi,@decMandeGhabl,'ghabl')
			end 

		set @SumBedGhabl=(Select Sum(fldBedehkar) as SumBed from  #tmpSooratHesab2  Where fldBestankar is not null and fldBedehkar is not null and Convert(dec,fldDate)<@bigDateMin)
		if @SumBedGhabl is null
			set @SumBedGhabl =0

		set @SumBesGhabl=(Select Sum(fldBestankar) as SumBes from  #tmpSooratHesab2  Where fldBestankar is not null and fldBedehkar is not null  and Convert(dec,fldDate)<@bigDateMin)
		if @SumBesGhabl is null
			set @SumBesGhabl=0

			update #tmpSooratHesab2 set id=0

			if @intOrder=0  --مرتب سازی براساس تاریخ
				Select  ID,radif,shomare,fldSanad,fldSharh,fldRiz,substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate,fldTedad,fldNameVahed,
					fldNameVahed2,fldFee,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,
					fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,
					fldMablaghKhales,fldCodeAnbar,fldNameAnbar,fldMablagh,fldBedehkar,fldBestankar,fldMande,
					fldType,fldVaziat,fldVasete,fldCodeKarbar,fldNameKarbar  from #tmpSooratHesab2 order by Convert(dec,fldDate),Convert(dec,fldSanad)	


				--select ID,radif,shomare,fldSanad,fldSharh,fldRiz,substring(convert(nvarchar(10),fldDate),0,3) +'/' +substring(convert(nvarchar(10),fldDate),3,2) + '/' + substring(convert(nvarchar(10),fldDate),5,2) as fldDate,fldTedad,fldNameVahed,
				--	fldNameVahed2,fldFee,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,
				--	fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,
				--	fldMablaghKhales,fldCodeAnbar,fldNameAnbar,fldMablagh,fldBedehkar,fldBestankar,fldMande,
				--	fldType,fldVaziat,fldVasete,fldCodeKarbar,fldNameKarbar



			else
				if @intOrder=1  --مرتب سازی براساس سند
					Select  ID,radif,shomare,fldSanad,fldSharh,fldRiz,substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate,fldTedad,fldNameVahed,
					fldNameVahed2,fldFee,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,
					fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,
					fldMablaghKhales,fldCodeAnbar,fldNameAnbar,fldMablagh,fldBedehkar,fldBestankar,fldMande,
					fldType,fldVaziat,fldVasete,fldCodeKarbar,fldNameKarbar  from #tmpSooratHesab2 order by Convert(dec,fldSanad)	

	end
else
	begin
			declare EmpCursor Cursor For
				Select Id from tblSodooreSanad With (NoLock) where (fldType<>'checkebteda') and  fldsanad<>@sanadCheckDaryaftEbteda and fldSanad<>@sanadCheckEbtedaPardakht and fldCodeTafzili=@CodeTafzili and (Convert(dec,fldDate) between @bigDateMinForSanad and @bigDateMaxForSanad)  and  ((fldtype<>'Vosool' and fldtype<>'pasecheck')) and fldType<>'ebteda' and not(fldtype='ekhtetamie') and Not( fldType='eftetahie' and fldMoinKamel=@strMoinKamelCheckPardakhti)  and Not( fldType='eftetahie' and fldMoinKamel=@strMoinKamelCheckDaryafti)  and fldCodeKol<>@bigCodeKolHazine  and fldMoinKamel<>@bigCodeMoinKharid and substring(fldMoinKamel,1,1)<>@strStartHazine and (substring(fldMoinKamel,1,@tooleKol)<>@strStartDaramad or  fldMoinKamel=@strKolForoosh) and (substring(fldMoinKamel,1,1)<>@CodeMoinKamelHesabhayePardakhtaniTanzimat and fldType<>'hazine') ORDER BY CONVERT(dec, fldSanad), CONVERT(dec, fldRadif)
					open EmpCursor
					fetch Next From EmpCursor into @EmpId
					While @@Fetch_Status = 0
						Begin					
							--Update tblSodooreSanad With (RowLock) Set [fldGhateiMovaghat]='False'					
							--Where ID=@EmpId
					
							-- ریختن کلیه اسناد مربوط به تفصیلی در جدول موقت  #########
							INSERT INTO #tmpSooratHesab2
								 (ID, fldSanad, fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
								 fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
								 fldVasete)
							Select ID, fldSanad,fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
								 fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
								 fldVasete
							from tblSodooreSanad 
								Where ID=Convert(varchar,@EmpId)

					
							set @type =(Select fldType from tblSodooresanad Where ID=@EmpId)
							set @CodeKolMoin =(Select fldMoinKamel from tblSodooresanad Where ID=@EmpId)
							execute codemoinhesab 'foroosh' ,@CodekolmoinTanzimat output
							execute codemoinhesab 'hesabhayedaryaftani' ,@CodekolMoinDaryaftTanzimat output	
							execute codemoinhesab 'check' ,@strMoinKamelCheckDaryafti output	
							execute codemoinhesab 'checkpardakhti' ,@strMoinKamelCheckPardakhti output	
							set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
							execute codemoinhesab 'hesabhayepardakhtani' ,@CodekolMoinHesabhayePardakhtaniTanzimat output	
									
							set @CodekolMoinHesabhayeDaryaftaniTanzimat = @CodekolMoinDaryaftTanzimat
					

							--###### فروش ##################
							if @type='foroosh'   ---برای نمایش ریز فاکتور فروش
								begin
									if @CodekolmoinTanzimat = @CodeKolMoin --اگر سند فروش بود
										begin
											declare @bigDate bigint,@codeKarbar bigint,@NameKarbar nvarchar(20)							
											set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
											set @ShomareFactor =(Select  fldShomareFactorForoosh from tblForoosh Where fldShomareSanad = @ShomareSanad)
									

											update #tmpSooratHesab2
												set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayeDaryaftaniTanzimat 

											execute z187TranslateKalame @language,16157,@SharheTranslate1 output
											update #tmpSooratHesab2									
												set fldSharh=' ' + @SharheTranslate1 +case when (fldSharh='' or fldSharh is null) then '' else '-' + fldSharh end   where fldSanad=@ShomareSanad And fldMoinKamel= @CodekolMoinHesabhayeDaryaftaniTanzimat 

												--,fldSharh=' فروش' + iif( fldSharh='','', '-' + fldSharh )

											execute z187TranslateKalame @language,16162,@SharheTranslate1 output--ف
											update #tmpSooratHesab2 set shomare = @SharheTranslate1+Convert(varchar, @ShomareFactor) where fldSanad= @ShomareSanad
											insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
													fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
													fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
													fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
													fldCodeAnbar,fldNameAnbar,fldType)
											Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),@SharheTranslate1+Convert(varchar, @ShomareFactor),@ShomareSanad, #tblKalaTemp.fldNameKala + ' ' + dbo.tblRizeForoosh.fldSharhKala,
													@bigDate,dbo.tblRizeForoosh.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeForoosh.fldFeeJoz,dbo.tblRizeForoosh.fldFeeYekCarton,
													dbo.tblRizeForoosh.fldFeeKol,dbo.tblRizeForoosh.fldDarsadTakhfif,dbo.tblRizeForoosh.fldMablaghTakhfif,dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, 
													dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif,dbo.tblRizeForoosh.fldMablaghKhales,
													dbo.tblRizeForoosh.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'foroosh'
											FROM            dbo.tblAnbar INNER JOIN
																	 dbo.tblRizeForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeForoosh.fldCodeAnbar INNER JOIN
																	 #tblKalaTemp ON dbo.tblRizeForoosh.fldCodeKala = #tblKalaTemp.fldCodeKala INNER JOIN
																	 dbo.tblVahed AS tblVahed_2 ON #tblKalaTemp.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
																	 dbo.tblVahed ON #tblKalaTemp.fldCodeVahed2 = dbo.tblVahed.ID
											WHERE        (dbo.tblRizeForoosh.fldShomareFactor = @ShomareFactor)

										--اضافه کردن نام کاربر
										 set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'فروش' and NoeAmaliat=N'جدید' order by id desc)
										 if @codekarbar is not null
											set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'فروش' and NoeAmaliat=N'جدید' order by id desc)
										 else
											begin
												set @codekarbar=0
												set @namekarbar =''
											end

										update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad
											--### حذف ردیف سندی که کد کل و معینش فروشه ، در حقیقت فقط باید حسابهای دریافتنی و پرداختنی و سندهای دستی نمایش داده بشه									
										end


									else if @CodekolMoinHesabhayeDaryaftaniTanzimat = @CodeKolMoin  -- اگر سند مربوط به دریافت در فروش بود
										begin
											set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)									
											set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
											set @ShomareFactor =(Select  fldShomareFactorForoosh from tblForoosh Where fldShomareSanad = @ShomareSanad)
											set @ShomareDaryaft =(Select  fldShomareDaryaft from tblDaryaft Where fldShomareSanad = @ShomareSanad)
											set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
											set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)

											execute	ShowDaryaftInfo @ShomareDaryaft, @str1, @lngAlaki, @lngAlaki2, @SharheKol output, 'daryaft', @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3

											UPDATE       #tmpSooratHesab2
												SET                fldSharh = @SharheKol
													where #tmpSooratHesab2.ID = @EmpId


											execute z187TranslateKalame @language,16159,@SharheTranslate1 output--دریافت
											execute z187TranslateKalame @language,16163,@SharheTranslate2 output--ش د
											execute z187TranslateKalame @language,16164,@SharheTranslate3 output--ش ف
											execute z187TranslateKalame @language,16165,@SharheTranslate4 output--چک شماره
											execute z187TranslateKalame @language,16166,@SharheTranslate5 output--تاريخ
											update #tmpSooratHesab2
												set fldSharh =@SharheTranslate1 +case when @SharheKol='' then '' else ' - ' +@SharheKol end , shomare =@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor)  where fldSanad=@ShomareSanad And fldMoinKamel =  @CodekolMoinHesabhayeDaryaftaniTanzimat  and fldBestankar>0

											update #tmpSooratHesab2
												set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayeDaryaftaniTanzimat 


											if @bigbes>0
												begin
													if @lngNaghd>0
														begin
															set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																values (Convert(varchar,@EmpId) + '101' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab +case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end   ,@bigDate,@lngNaghd,'foroosh')
														end

													if @lngBank>0
														begin
															set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																values (Convert(varchar,@EmpId) + '102' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strShareBank='' then '' else '-' +@strShareBank end   ,@bigDate,@lngBank,'foroosh')
														end
									
													if @lngCheck>0 
														begin
															--set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelCheckDaryafti)
															--set @strSharheHesab ='چک شماره ' 
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

																	select Convert(varchar,@EmpId) +'103' + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) +  ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate4 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + @SharheTranslate5 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSharheCheck='' then '' else '-' +@strSharheCheck end   ,@bigDate,fldMablagh,'foroosh'
																		from tblCheckeDaryafti Where fldSanad = @ShomareSanad

															update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad and fldMoinKamel = @strMoinKamelCheckDaryafti
														end

													if @lngTakhfif>0
														begin
															set @strSharheHesab = (Select * from dbo.translatefunction(@LANGUAGE,18285)) + ' '
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																values (Convert(varchar,@EmpId) + '104' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab +case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end  ,@bigDate,@lngTakhfif,'foroosh')
														end
												end
										end

								
								end

					
							set @CodeKolMoin =(Select fldMoinKamel from tblSodooresanad Where ID=@EmpId)--11301
							set @CodeKolSanad =(Select fldCodeKol from tblSodooresanad Where ID=@EmpId)--113
							set @CodeKolTanzimat =(Select fldCodeKol from tblSodooresanad Where ID=@EmpId)--110
							execute CodeKolHesab 'hesabhayedaryaftani' ,@CodeKolTanzimat output--11301

					
				


				
						if @type='kharid'   ---برای نمایش ریز فاکتور خرید
								begin
									execute codemoinhesab 'kharid' ,@CodekolmoinTanzimat output
									execute codemoinhesab 'hesabhayepardakhtani' ,@CodekolMoinHesabhayePardakhtaniTanzimat output	

									set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
									update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckPardakhti
									update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckdaryafti

									if @CodekolmoinTanzimat = @CodeKolMoin --اگر سند خرید بود
										begin
									
											set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
						
										end

									else if @CodekolMoinHesabhayePardakhtaniTanzimat = @CodeKolMoin  -- اگر سند مربوط به پرداخت در خرید بود
										begin
											set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)									
											set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
											set @ShomareFactor =(Select  fldShomareFactorKharid from tblKharid Where fldShomareSanad = @ShomareSanad)
											set @ShomarePardakht =(Select  fldShomarePardakht from tblPardakht Where fldShomareSanad = @ShomareSanad)
											set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
											set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)

											execute	ShowPardakhtInfo @ShomarePardakht, @str1, @lngAlaki, @str1, @SharheKol output, @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output,@lngMablagheCheckeVagozari output, @strSCheckVaghozari output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3



											set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
											set @ShomareFactor =(Select  fldShomareFactorkharid from tblKharid Where fldShomareSanad = @ShomareSanad)
									
											if @bigbes>0  or (@bigbes=0 and @bigbed=0)
												begin
													execute z187TranslateKalame @language,16167,@SharheTranslate1 output--خ
													execute z187TranslateKalame @language,16155,@SharheTranslate2 output--خرید

													update #tmpSooratHesab2 set shomare = @SharheTranslate1+Convert(varchar, @ShomareFactor) ,fldSharh=' ' + @SharheTranslate2 +case when fldSharh='' then '' else '-' +fldSharh end   where ID=@EmpId
													update #tmpSooratHesab2 set shomare = @SharheTranslate1+Convert(varchar, @ShomareFactor) where fldSanad= @ShomareSanad
													insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
															fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
															fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
															fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
															fldCodeAnbar,fldNameAnbar,fldType)
													Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),@SharheTranslate1+Convert(varchar, @ShomareFactor),@ShomareSanad, #tblKalaTemp.fldNameKala + ' ' + dbo.tblRizekharid.fldSharhKala,
															@bigDate,dbo.tblRizekharid.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizekharid.fldFeeJoz,dbo.tblRizekharid.fldFeeYekCarton,
															dbo.tblRizekharid.fldFeeKol,dbo.tblRizekharid.fldDarsadTakhfif,dbo.tblRizekharid.fldMablaghTakhfif,dbo.tblRizekharid.fldFeeVahed1BadAzTakhfif, 
															dbo.tblRizekharid.fldFeeVahed2BadAzTakhfif, dbo.tblRizekharid.fldArzeshAfzode, dbo.tblRizekharid.fldFeeKolBadAzTakhfif,dbo.tblRizekharid.fldMablaghKhales,
															dbo.tblRizekharid.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'kharid'
													FROM            dbo.tblAnbar INNER JOIN
																				dbo.tblRizekharid ON dbo.tblAnbar.fldCode = dbo.tblRizekharid.fldCodeAnbar INNER JOIN
																				#tblKalaTemp ON dbo.tblRizekharid.fldCodeKala = #tblKalaTemp.fldCodeKala INNER JOIN
																				dbo.tblVahed AS tblVahed_2 ON #tblKalaTemp.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
																				dbo.tblVahed ON #tblKalaTemp.fldCodeVahed2 = dbo.tblVahed.ID
													WHERE        (dbo.tblRizekharid.fldShomareFactor = @ShomareFactor)
												end



										--اضافه کردن نام کاربر
										 set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'خرید' and NoeAmaliat=N'جدید' order by id desc)
										 if @codekarbar is not null
											set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'خرید' and NoeAmaliat=N'جدید' order by id desc)
										 else
											begin
												set @codekarbar=0
												set @namekarbar =''
											end
											update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad

											update #tmpSooratHesab2
												set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayePardakhtaniTanzimat 

									

											if @bigbed>0
												begin
													execute z187TranslateKalame @language,16160,@SharheTranslate1 output--پرداخت
													execute z187TranslateKalame @language,16168,@SharheTranslate2 output--ش پ
													execute z187TranslateKalame @language,16169,@SharheTranslate3 output--ش خ
													execute z187TranslateKalame @language,16170,@SharheTranslate4 output--شماره چک
													execute z187TranslateKalame @language,16166,@SharheTranslate5 output--تاريخ
													execute z187TranslateKalame @language,16172,@SharheTranslate6 output--واگذاري چک شماره
													execute z187TranslateKalame @language,16183,@SharheTranslate7 output--تخفیفات خرید

													update #tmpSooratHesab2 set fldSharh=@SharheTranslate1 + ' ' +  fldSharh ,shomare =@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor) where id=@EmpId
													if @lngNaghd>0
														begin
															set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																values (Convert(varchar,@EmpId) + '100' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end ,@bigDate,@lngNaghd,'kharid')
														end

													if @lngBank>0
														begin
															set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																values (Convert(varchar,@EmpId) + '200' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strShareBank='' then '' else '-' +    @strShareBank end,@bigDate,@lngBank,'kharid')
														end
									
													if @lngCheck>0 
														begin													
													
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

																	select Convert(varchar,@EmpId ) + '300' + + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate4 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + ' ' + @SharheTranslate5 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSharheCheck='' then '' else '-' +@strSharheCheck end ,@bigDate,fldMablagh,'kharid'
																		from tblCheckePardakhti Where fldSanad = @ShomareSanad													
														end

							
													if @lngMablagheCheckeVagozari>0 
														begin													
													
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

																	select Convert(varchar,@EmpId ) + '400' + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate6 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + @SharheTranslate5 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSCheckVaghozari='' then '' else '-' +@strSCheckVaghozari end ,@bigDate,fldMablagh,'kharid'
																		from tblCheckeDaryafti Where fldShomarePardakht = @ShomarePardakht													
														end

													if @lngTakhfif>0
														begin
															set @strSharheHesab = @SharheTranslate7 + ' '
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																values (Convert(varchar,@EmpId ) + '500' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharheTakhfifat='' then '' else '-' +  @strSharheTakhfifat end  ,@bigDate,@lngTakhfif,'kharid')
														end
												end
										end
								
								end



							execute codemoinhesab 'foroosh' ,@CodekolmoinTanzimat output

					

							--###### برگشت از فروش ##################
							if @type='bargashtazforoosh'   ---برای نمایش ریز برگشت از فروش
								begin
									execute z187TranslateKalame @language,16171,@SharheTranslate1 output--'ب ف'
									execute z187TranslateKalame @language,16160,@SharheTranslate2 output--پرداخت
									execute z187TranslateKalame @language,16168,@SharheTranslate3 output--ش پ
									execute z187TranslateKalame @language,16169,@SharheTranslate4 output--ش خ
									execute z187TranslateKalame @language,16156,@SharheTranslate5 output--برگشت از فروش
									execute z187TranslateKalame @language,16165,@SharheTranslate6 output--شماره چک
							

									set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
									update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckPardakhti
									update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckdaryafti

						

									update #tmpSooratHesab2
													set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayeDaryaftaniTanzimat 

									begin
										if @CodekolmoinTanzimat = @CodeKolMoin --اگر سند برگشت از فروش بود
											begin
									
												set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
												set @ShomareFactor =(Select  fldShomareFactorForoosh from tblBargashtAzForoosh Where fldShomareSanad = @ShomareSanad)
									
											end

										else if @CodekolMoinHesabhayeDaryaftaniTanzimat = @CodeKolMoin  -- اگر سند مربوط به پرداخت در برگشت از فروش بود
											begin
												set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)									
												set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
												set @ShomareFactor =(Select  fldShomareFactorForoosh from tblBargashtAzForoosh Where fldShomareSanad = @ShomareSanad)
												set @ShomarePardakht =(Select  fldShomarePardakht from tblPardakht Where fldShomareSanad = @ShomareSanad)
												set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
												set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)
									
												execute	ShowPardakhtInfo @ShomarePardakht, @str1, @lngAlaki, @str1, @SharheKol output, @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output,@lngMablagheCheckeVagozari output, @strSCheckVaghozari output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3


												set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
												set @ShomareFactor =(Select  fldShomareFactorForoosh from tblBargashtAzForoosh Where fldShomareSanad = @ShomareSanad)

											--اضافه کردن نام کاربر
											 set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'برگشت از فروش' and NoeAmaliat=N'جدید' order by id desc)
											 if @codekarbar is not null
												set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'برگشت از فروش' and NoeAmaliat=N'جدید' order by id desc)
											 else
												begin
													set @codekarbar=0
													set @namekarbar =''
												end
											update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad
												
												if @bigbes>0  or (@bigbes=0 and @bigbed=0)
													begin
														update #tmpSooratHesab2 set shomare =@SharheTranslate1+Convert(varchar, @ShomareFactor) ,fldSharh=' ' + @SharheTranslate5 + case when fldSharh ='' then '' else '-' +  fldSharh end where ID=@EmpId
														update #tmpSooratHesab2 set shomare = @SharheTranslate1+Convert(varchar, @ShomareFactor) where fldSanad= @ShomareSanad
														insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
																fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
																fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
																fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
																fldCodeAnbar,fldNameAnbar,fldType)
														Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),@SharheTranslate1+Convert(varchar, @ShomareFactor),@ShomareSanad, #tblKalaTemp.fldNameKala + ' ' + dbo.tblRizeBargashtAzForoosh.fldSharhKala,
																@bigDate,dbo.tblRizeBargashtAzForoosh.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeBargashtAzForoosh.fldFeeJoz,dbo.tblRizeBargashtAzForoosh.fldFeeYekCarton,
																dbo.tblRizeBargashtAzForoosh.fldFeeKol,dbo.tblRizeBargashtAzForoosh.fldDarsadTakhfif,dbo.tblRizeBargashtAzForoosh.fldMablaghTakhfif,dbo.tblRizeBargashtAzForoosh.fldFeeVahed1BadAzTakhfif, 
																dbo.tblRizeBargashtAzForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldArzeshAfzode, dbo.tblRizeBargashtAzForoosh.fldFeeKolBadAzTakhfif,dbo.tblRizeBargashtAzForoosh.fldMablaghKhales,
																dbo.tblRizeBargashtAzForoosh.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'bargashtazforoosh'
														FROM            dbo.tblAnbar INNER JOIN
																					dbo.tblRizeBargashtAzForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeBargashtAzForoosh.fldCodeAnbar INNER JOIN
																					#tblKalaTemp ON dbo.tblRizeBargashtAzForoosh.fldCodeKala = #tblKalaTemp.fldCodeKala INNER JOIN
																					dbo.tblVahed AS tblVahed_2 ON #tblKalaTemp.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
																					dbo.tblVahed ON #tblKalaTemp.fldCodeVahed2 = dbo.tblVahed.ID
														WHERE        (dbo.tblRizeBargashtAzForoosh.fldShomareFactor = @ShomareFactor)
													end


											
											if @bigbed>0
												begin
													update #tmpSooratHesab2 set fldSharh=@SharheTranslate2 + ' ' +  fldSharh ,shomare = @SharheTranslate3 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate1 + Convert(varchar,@ShomareFactor) where id=@EmpId
													if @lngNaghd>0
														begin
															set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																values (Convert(varchar,@EmpId) + '100' ,@SharheTranslate3 + Convert(varchar,@ShomarePardakht)  + ' ' + @SharheTranslate1  + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end,@bigDate,@lngNaghd,'bargashtazforoosh')
														end

													if @lngBank>0
														begin
															set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																values (Convert(varchar,@EmpId) + '200' ,@SharheTranslate3 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate4 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strShareBank='' then '' else '-' + @strShareBank end  ,@bigDate,@lngBank,'bargashtazforoosh')
														end
									
													if @lngCheck>0 
														begin													
													
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

																	select Convert(varchar,@EmpId ) + '300' + + Convert(varchar,id) ,@SharheTranslate3 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate4 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate6 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + ' تاریخ ' + Convert(varchar,fldDateSarResid) + case when @strSharheCheck='' then '' else '-' +@strSharheCheck end  ,@bigDate,fldMablagh,'bargashtazforoosh'
																		from tblCheckePardakhti Where fldSanad = @ShomareSanad													
														end

								

												if @lngMablagheCheckeVagozari>0 
													begin													
														execute z187TranslateKalame @language,16172,@SharheTranslate1 output--واگذاری چک شماره
														execute z187TranslateKalame @language,16166,@SharheTranslate2 output--تاريخ
														insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

																select Convert(varchar,@EmpId ) + '400' + Convert(varchar,id) ,@SharheTranslate3 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate4 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate1 +' ' + Convert(varchar,fldShomareCheck) + ' ' + ' ' + @SharheTranslate2 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSCheckVaghozari='' then '' else '-' +@strSCheckVaghozari end ,@bigDate,fldMablagh,'bargashtazforoosh'
																	from tblCheckeDaryafti Where fldShomarePardakht = @ShomarePardakht													
													end

													if @lngTakhfif>0
														begin
															execute z187TranslateKalame @language,16172,@SharheTranslate1 output--واگذاری چک شماره
															execute z187TranslateKalame @language,16172,@SharheTranslate2 output--تخفیفات فروش
															set @strSharheHesab =@SharheTranslate2 + ' '
															insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																values (Convert(varchar,@EmpId ) + '500' ,@SharheTranslate3 + Convert(varchar,@ShomarePardakht) + ' ' + @SharheTranslate4 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharheTakhfifat='' then '' else ' - ' +@strSharheTakhfifat end,@bigDate,@lngTakhfif,'bargashtazforoosh')
														end
												end



											end		    
								   end 						   
								
								end

					

							if @type='bargashtazkharid'   ---برای نمایش ریز برگشت از خرید
								begin
								execute z187TranslateKalame @language,16159,@SharheTranslate1 output--دریافت
								execute z187TranslateKalame @language,16163,@SharheTranslate2 output--ش د
								execute z187TranslateKalame @language,16173,@SharheTranslate3 output--ب خ
								execute z187TranslateKalame @language,16158,@SharheTranslate4 output--برگشت از خرید
									execute codemoinhesab 'kharid' ,@CodekolmoinTanzimat output
									execute codemoinhesab 'hesabhayepardakhtani' ,@CodekolMoinHesabhayePardakhtaniTanzimat output	

									set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
									update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckPardakhti
									update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad=@ShomareSanad and fldMoinKamel = @strMoinKamelCheckdaryafti

							
									set @ShomareFactor =(Select  fldShomareFactorKharid from tblBargashtAzKharid Where fldShomareSanad = @ShomareSanad)
												
									update #tmpSooratHesab2
										set fldSharh =@SharheTranslate1 + case when  @SharheKol='' then '' else ' - ' +@SharheKol end ,Shomare=@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' + @SharheTranslate3 + Convert(varchar, @ShomareFactor )  where fldSanad=@ShomareSanad And fldMoinKamel =  @CodekolMoinHesabhayePardakhtaniTanzimat  and fldBestankar>0

									if @CodekolmoinTanzimat = @CodeKolMoin --اگر سند خرید بود
										begin
									
											set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)

										end

									else if @CodekolMoinHesabhayePardakhtaniTanzimat = @CodeKolMoin  -- اگر سند مربوط به دریافت در برگشت از خرید بود
										begin
											set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)									
											set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)
											set @ShomareFactor =(Select  fldShomareFactorKharid from tblBargashtAzKharid Where fldShomareSanad = @ShomareSanad)
											set @ShomareDaryaft =(Select  fldShomareDaryaft from tblDaryaft Where fldShomareSanad = @ShomareSanad)
											set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
											set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)
									
									
											execute	ShowDaryaftInfo @ShomareDaryaft, @str1, @lngAlaki, @lngAlaki2, @SharheKol output, 'bargashtazkharid', @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3


											set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
											set @ShomareFactor =(Select  fldShomareFactorkharid from tblBargashtAzKharid Where fldShomareSanad = @ShomareSanad)

											--اضافه کردن نام کاربر
											 set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'برگشت از خرید' and NoeAmaliat=N'جدید' order by id desc)
											 if @codekarbar is not null
												set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomareFactor and fldNameForm=N'برگشت از خرید' and NoeAmaliat=N'جدید' order by id desc)
											 else
												begin
													set @codekarbar=0
													set @namekarbar =''
												end
											update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad


									
											if @bigbed>0  or (@bigbes=0 and @bigbed=0)
												begin
													update #tmpSooratHesab2 set shomare = @SharheTranslate3+Convert(varchar, @ShomareFactor) ,fldSharh=' ' + @SharheTranslate4 + case when fldSharh='' then '' else '-' +fldSharh end   where ID=@EmpId
													update #tmpSooratHesab2 set shomare = @SharheTranslate3+Convert(varchar, @ShomareFactor) where fldSanad= @ShomareSanad

																						

													insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
															fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
															fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
															fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
															fldCodeAnbar,fldNameAnbar,fldType)
													Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),@SharheTranslate3+Convert(varchar, @ShomareFactor),@ShomareSanad, #tblKalaTemp.fldNameKala + ' ' + dbo.tblRizeBargashtAzkharid.fldSharhKala,
															@bigDate,dbo.tblRizeBargashtAzkharid.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeBargashtAzkharid.fldFeeJoz,dbo.tblRizeBargashtAzkharid.fldFeeYekCarton,
															dbo.tblRizeBargashtAzkharid.fldFeeKol,dbo.tblRizeBargashtAzkharid.fldDarsadTakhfif,dbo.tblRizeBargashtAzkharid.fldMablaghTakhfif,dbo.tblRizeBargashtAzkharid.fldFeeVahed1BadAzTakhfif, 
															dbo.tblRizeBargashtAzkharid.fldFeeVahed2BadAzTakhfif, dbo.tblRizeBargashtAzkharid.fldArzeshAfzode, dbo.tblRizeBargashtAzkharid.fldFeeKolBadAzTakhfif,dbo.tblRizeBargashtAzkharid.fldMablaghKhales,
															dbo.tblRizeBargashtAzkharid.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'bargashtazkharid'
													FROM            dbo.tblAnbar INNER JOIN
																				dbo.tblRizeBargashtAzkharid ON dbo.tblAnbar.fldCode = dbo.tblRizeBargashtAzkharid.fldCodeAnbar INNER JOIN
																				#tblKalaTemp ON dbo.tblRizeBargashtAzkharid.fldCodeKala = #tblKalaTemp.fldCodeKala INNER JOIN
																				dbo.tblVahed AS tblVahed_2 ON #tblKalaTemp.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
																				dbo.tblVahed ON #tblKalaTemp.fldCodeVahed2 = dbo.tblVahed.ID
													WHERE        (dbo.tblRizeBargashtAzkharid.fldShomareFactor = @ShomareFactor)
										




													update #tmpSooratHesab2
														set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayePardakhtaniTanzimat 

											

									
													begin
														--set @ShomareSanad=@ShomareSanad
															if @lngNaghd>0
																begin

																	set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
																	insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																		values (Convert(varchar,@EmpId+1) + '101' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' +@SharheTranslate3  + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end ,@bigDate,@lngNaghd,'bargashtazkharid')
																end

															if @lngBank>0
																begin
																	set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
																	insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																		values (Convert(varchar,@EmpId+1) + '102' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' +@SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strShareBank='' then '' else '-' +@strShareBank end  ,@bigDate,@lngBank,'bargashtazkharid')
																end
									
															if @lngCheck>0 
																begin
																	execute z187TranslateKalame @language,16165,@SharheTranslate1 output--چک شماره
																	execute z187TranslateKalame @language,16166,@SharheTranslate output--تاریخ
																	insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																			select Convert(varchar,@EmpId+1) +'103'   + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' +@SharheTranslate3 + Convert(varchar,@ShomareFactor) ,@ShomareSanad,@SharheTranslate1 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + @SharheTranslate + ' ' + Convert(varchar,fldDateSarResid) +  case when @strSharheCheck='' then '' else '-' +@strSharheCheck end  ,@bigDate,fldMablagh,'bargashtazkharid'
																				from tblCheckeDaryafti Where fldSanad = @ShomareSanad

																	update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad and fldMoinKamel = @strMoinKamelCheckDaryafti
																end

															if @lngTakhfif>0
																begin
																	execute z187TranslateKalame @language,16185,@SharheTranslate output--تخفیفات خرید
																	set @strSharheHesab =@SharheTranslate + ' '
																	insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																		values (Convert(varchar,@EmpId+1) + '104' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) + ' ' +@SharheTranslate3 + Convert(varchar,@ShomareFactor),@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end  ,@bigDate,@lngTakhfif,'bargashtazkharid')
																end

												
														end
												end
									
										end
								
								end

							if @type='daryaft'   ---برای نمایش ریز دریافت مستقیم
								begin
									set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
									set @ShomareDaryaft =(Select  fldShomareDaryaft from tblDaryaft Where fldShomareSanad = @ShomareSanad)

									--if @ShomareSanad=408
									--	begin
									--		INSERT INTO dbo.tblTest5
									--				 (name1, name2, name3)
									--					VALUES        (@bigDate,@ShomareDaryaft,'aa')
										--end

									execute z187TranslateKalame @language,16159,@SharheTranslate1 output--دریافت
									execute z187TranslateKalame @language,16163,@SharheTranslate2 output--ش د
									execute z187TranslateKalame @language,16165,@SharheTranslate3 output--چک شماره
									execute z187TranslateKalame @language,16166,@SharheTranslate4 output--تاریخ
									execute z187TranslateKalame @language,16186,@SharheTranslate5 output--تخفیفات دریافت


									set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
									set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)
									update #tmpSooratHesab2 set fldSharh=@SharheTranslate1 ,Shomare=@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) Where ID=@EmpId

									execute	ShowDaryaftInfo @ShomareDaryaft, @str1, @lngAlaki, @lngAlaki2, @SharheKol output, 'bargashtazkharid', @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3


									--اضافه کردن نام کاربر
									set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomareDaryaft and fldNameForm='دريافت' and NoeAmaliat='جدید' order by id desc)
									if @codekarbar is not null
									set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomareDaryaft and fldNameForm=N'دريافت' and NoeAmaliat='جدید' order by id desc)
									else
									begin
										set @codekarbar=0
										set @namekarbar =''
									end
									update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad

									if @bigbes>0
										begin
											if @lngNaghd>0
											begin
												set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
												insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
													values (Convert(varchar,@EmpId) + '101' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) ,@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end ,@bigDate,@lngNaghd,'daryaft')
											end

											if @lngBank>0
											begin
												set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
												insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
													values (Convert(varchar,@EmpId)+ '102' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) ,@ShomareSanad,@strSharheHesab + case when @strShareBank='' then '' else '-' +@strShareBank end ,@bigDate,@lngBank,'daryaft')
											end
									
											if @lngCheck>0 
											begin
												insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

												select Convert(varchar,@EmpId)+'103'  + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft)  ,@ShomareSanad,@SharheTranslate3 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + @SharheTranslate4 +' ' + Convert(varchar,fldDateSarResid) +  case when @strSharheCheck='' then '' else '-' +@strSharheCheck end  ,@bigDate,fldMablagh,'daryaft'
												from tblCheckeDaryafti Where fldSanad = @ShomareSanad

												update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad and fldMoinKamel = @strMoinKamelCheckDaryafti
											end

										if @lngTakhfif>0
											begin
												set @strSharheHesab = @SharheTranslate5 + ' '
												insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
													values (Convert(varchar,@EmpId) + '104' ,@SharheTranslate2 + Convert(varchar,@ShomareDaryaft) ,@ShomareSanad,@strSharheHesab + case when @strSharhenaghd='' then '' else '-' +@strSharhenaghd end  ,@bigDate,@lngTakhfif,'daryaft')
											end
								
										end
								end




							if @type = 'pardakht'  -- اگر سند مربوط به پرداخت اصلی بود
								begin
										execute z187TranslateKalame @language,16160,@SharheTranslate1 output--پرداخت
										execute z187TranslateKalame @language,16168,@SharheTranslate2 output--ش پ
										execute z187TranslateKalame @language,16170,@SharheTranslate3 output--شماره چک
										execute z187TranslateKalame @language,16166,@SharheTranslate4 output--تاریخ
										execute z187TranslateKalame @language,16172,@SharheTranslate5 output--واگذاری چک شماره

										set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)									
										set @ShomareSanad=(Select Convert(bigint,fldSanad) from tblSodooresanad Where ID=@EmpId)								
										set @ShomarePardakht =(Select  fldShomarePardakht from tblPardakht Where fldShomareSanad = @ShomareSanad)
										set @bigbed = (Select fldBedehkar from tblSodooresanad Where ID=@EmpId)
										set @bigbes = (Select fldBestankar from tblSodooresanad Where ID=@EmpId)
									
										execute	ShowPardakhtInfo @ShomarePardakht, @str1, @lngAlaki, @str1, @SharheKol output, @lngNaghd  output, @strMoinKamelSandoogh  output, @strSharhenaghd  output, @lngBank  output, @strMoinKamelBank  output, @strShareBank  output, @lngCheck  output, @strSharheCheck  output,@lngMablagheCheckeVagozari output, @strSCheckVaghozari output, @lngTakhfif  output, @strSharheTakhfifat  output, @str2, @str3

										--اضافه کردن نام کاربر
										set @codekarbar=	(select top(1) fldCodeKarbar from tblAmalkardekarbar where fldShomare=@ShomarePardakht and fldNameForm='پرداخت' and NoeAmaliat='جدید' order by id desc)
										if @codekarbar is not null
										set @namekarbar =(select top(1) fldNameKarbar from tblAmalkardekarbar where fldShomare=@ShomarePardakht and fldNameForm=N'پرداخت' and NoeAmaliat='جدید' order by id desc)
										else
										begin
											set @codekarbar=0
											set @namekarbar =''
										end
										update #tmpSooratHesab2 set fldCodeKarbar = @codeKarbar,fldNameKarbar=@NameKarbar  where fldSanad= @ShomareSanad


										set @bigDate=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)																	
												
										--if @bigbes>0 
										--	begin
												--update #tmpSooratHesab2 set shomare = 'ب ف'+Convert(varchar, @ShomareFactor) ,fldSharh=' برگشت از فروش' + iif(  fldSharh ='','','-' +  fldSharh ) where ID=@EmpId
												--update #tmpSooratHesab2 set shomare = 'ب ف'+Convert(varchar, @ShomareFactor) where fldSanad= @ShomareSanad
												--insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
												--		fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
												--		fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
												--		fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
												--		fldCodeAnbar,fldNameAnbar)
												--Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),'ب ف'+Convert(varchar, @ShomareFactor),@ShomareSanad, dbo.tblKala.fldNameKala + ' ' + dbo.tblRizeBargashtAzForoosh.fldSharhKala,
												--		@bigDate,dbo.tblRizeBargashtAzForoosh.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeBargashtAzForoosh.fldFeeJoz,dbo.tblRizeBargashtAzForoosh.fldFeeYekCarton,
												--		dbo.tblRizeBargashtAzForoosh.fldFeeKol,dbo.tblRizeBargashtAzForoosh.fldDarsadTakhfif,dbo.tblRizeBargashtAzForoosh.fldMablaghTakhfif,dbo.tblRizeBargashtAzForoosh.fldFeeVahed1BadAzTakhfif, 
												--		dbo.tblRizeBargashtAzForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldArzeshAfzode, dbo.tblRizeBargashtAzForoosh.fldFeeKolBadAzTakhfif,dbo.tblRizeBargashtAzForoosh.fldMablaghKhales,
												--		dbo.tblRizeBargashtAzForoosh.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar
												--FROM            dbo.tblAnbar INNER JOIN
												--							dbo.tblRizeBargashtAzForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeBargashtAzForoosh.fldCodeAnbar INNER JOIN
												--							dbo.tblKala ON dbo.tblRizeBargashtAzForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
												--							dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
												--							dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
												--WHERE        (dbo.tblRizeBargashtAzForoosh.fldShomareFactor = @ShomareFactor)
											--end


											
									if @bigbed>0
										begin

										--INSERT INTO dbo.tblTest5
										--		 (name1, name2, name3)
										--			VALUES        (@lngNaghd,@ShomarePardakht,@ShomareFactor)

								
											update #tmpSooratHesab2
													set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel<> @CodekolMoinHesabhayePardakhtaniTanzimat 

											update #tmpSooratHesab2 set fldSharh=@SharheTranslate1 + ' ' +  fldSharh ,Shomare=@SharheTranslate2 + Convert(varchar,@ShomarePardakht) where id=@EmpId
											if @lngNaghd>0
												begin
													set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelSandoogh)											
													insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
														values (Convert(varchar,@EmpId) + '100' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) ,@ShomareSanad,@strSharheHesab + case when  @strSharhenaghd='' then '' else '-' +@strSharhenaghd end,@bigDate,@lngNaghd,'pardakht')
												end

											if @lngBank>0
												begin
													set @strSharheHesab = (Select fldSharhehesab from tblKolMoin Where fldCodeHesab =@strMoinKamelBank)
													insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
														values (Convert(varchar,@EmpId) + '200' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) ,@ShomareSanad,@strSharheHesab + case when  @strShareBank='' then '' else '-' + @strShareBank end  ,@bigDate,@lngBank,'pardakht')
												end
									
											if @lngCheck>0 
												begin													
													
													insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

															select Convert(varchar,@EmpId ) + '300' + + Convert(varchar,id) ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) ,@ShomareSanad,@SharheTranslate3 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + ' ' + @SharheTranslate4 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSharheCheck='' then '' else '-' +@strSharheCheck end  ,@bigDate,fldMablagh,'pardakht'
																from tblCheckePardakhti Where fldSanad = @ShomareSanad													
												end

										----INSERT INTO dbo.tblTest5
										----	 (name1, name2, name3)
										----		VALUES        (@lngMablagheCheckeVagozari,'','hh')

										if @lngMablagheCheckeVagozari>0 
											begin													
													
												insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)

														select Convert(varchar,@EmpId ) + '400' + Convert(varchar,id) ,@SharheTranslate2 ,@ShomareSanad,@SharheTranslate5 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + ' ' + @SharheTranslate4 + ' ' + Convert(varchar,fldDateSarResid) + case when @strSCheckVaghozari='' then '' else '-' +@strSCheckVaghozari end ,@bigDate,fldMablagh,'pardakht'
															from tblCheckeDaryafti Where fldShomarePardakht = @ShomarePardakht													
											end

											if @lngTakhfif>0
												begin
													execute z187TranslateKalame @language,16187,@SharheTranslate output--تخفیفات پرداخت
													set @strSharheHesab = @SharheTranslate + ' '
													insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
														values (Convert(varchar,@EmpId ) + '500' ,@SharheTranslate2 + Convert(varchar,@ShomarePardakht) ,@ShomareSanad,@strSharheHesab + case when @strSharheTakhfifat='' then '' else ' - ' +@strSharheTakhfifat end ,@bigDate,@lngTakhfif,'pardakht')
												end
								



									end		    
								
								end

								if @type='EbtedaAshkhas'
									begin
										execute z187TranslateKalame @language,16174,@SharheTranslate1 output--طلب ابتدای دوره
										execute z187TranslateKalame @language,16175,@SharheTranslate2 output--بدهی ابتدای دوره

										update #tmpSooratHesab2 set fldSharh=@SharheTranslate1 + case when fldSharh<>'' and fldSharh is not null then '-' +fldSharh else '' end,fldType='ebtedadore' Where fldBestankar>0 and ID=@EmpId 
										update #tmpSooratHesab2 set fldSharh=@SharheTranslate2 + case when fldSharh<>'' and fldSharh is not null then '-' +fldSharh else '' end,fldType='ebtedadore' Where fldBedehkar>0 and ID=@EmpId 
									end

								if @type is null
									begin
										declare @strNameHesab nvarchar(50),@strCodeMoinKamel nvarchar(50)

										set @strcodemoinkamel = (Select fldMoinKamel from #tmpSooratHesab2 Where ID=@EmpId)
										execute NameKolMoin @strcodemoinkamel  ,@strNameHesab output
										update #tmpSooratHesab2 set fldSharh=@strNameHesab + case when fldSharh<>'' and fldSharh is not null then '-' +fldSharh else '' end,fldType='other' Where   ID=@EmpId 
									end
								if @type='amani'
									begin
										declare @ShomareAmani bigint
										declare @bigDate2 bigint									
										set @bigDate2=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)

										set @ShomareAmani =(Select  Top(1)fldShomareAmani from tblRizeForoosh  Where fldShomareSanad = @ShomareSanad)
										update #tmpSooratHesab2 set shomare = 'امانی'+Convert(varchar, @ShomareAmani), fldBedehkar=0, fldSharh='فروش امانی' + case when fldSharh<>'' and fldSharh is not null then '-' +fldSharh else '' end,fldType='amani' Where fldBedehkar>0 and ID=@EmpId 

										insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
													fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
													fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
													fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
													fldCodeAnbar,fldNameAnbar,fldType)
											Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),'امانی'+Convert(varchar, @ShomareAmani),@ShomareSanad, #tblKalaTemp.fldNameKala + ' ' + dbo.tblRizeForoosh.fldSharhKala,
													@bigDate2,dbo.tblRizeForoosh.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeForoosh.fldFeeJoz,dbo.tblRizeForoosh.fldFeeYekCarton,
													dbo.tblRizeForoosh.fldFeeKol,dbo.tblRizeForoosh.fldDarsadTakhfif,dbo.tblRizeForoosh.fldMablaghTakhfif,dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, 
													dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif,dbo.tblRizeForoosh.fldMablaghKhales,
													dbo.tblRizeForoosh.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'amani'
											FROM            dbo.tblAnbar INNER JOIN
																	 dbo.tblRizeForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeForoosh.fldCodeAnbar INNER JOIN
																	 #tblKalaTemp ON dbo.tblRizeForoosh.fldCodeKala = #tblKalaTemp.fldCodeKala INNER JOIN
																	 dbo.tblVahed AS tblVahed_2 ON #tblKalaTemp.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
																	 dbo.tblVahed ON #tblKalaTemp.fldCodeVahed2 = dbo.tblVahed.ID
											WHERE        (dbo.tblRizeForoosh.fldShomareAmani = @ShomareAmani)
									end

									if @type='checkebteda'
										begin									
											execute z187TranslateKalame @language,16178,@SharheTranslate1 output--چک دریافتی ابتدای دوره		
											execute z187TranslateKalame @language,16179,@SharheTranslate2 output--چک ابتدای دوره
											execute z187TranslateKalame @language,16165,@SharheTranslate3 output--چک شماره
											execute z187TranslateKalame @language,16166,@SharheTranslate4 output--تاریخ

									
											set @bigDate2=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)

											--set @ShomareAmani =(Select  Top(1)fldShomareAmani from tblRizeForoosh  Where fldShomareSanad = @ShomareSanad)
											update #tmpSooratHesab2 set fldBedehkar=0,fldBestankar=0, shomare = @SharheTranslate1,  fldSharh=@SharheTranslate2 + case when fldSharh<>'' and fldSharh is not null then '-' +fldSharh else '' end,fldType='checkebteda' Where fldBedehkar>0 and ID=@EmpId 

											insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
												select Convert(varchar,@EmpId)+'103'  + Convert(varchar,id) ,@SharheTranslate2  ,@ShomareSanad,@SharheTranslate3 + ' ' + Convert(varchar,fldShomareCheck) + ' ' + @SharheTranslate4 + ' ' + Convert(varchar,fldDateSarResid) +  case when @strSharheCheck='' then '' else '-' +@strSharheCheck end  ,@bigDate2,fldMablagh,'checkebteda'
												from tblCheckeDaryafti Where fldSanad = @ShomareSanad and fldCodeSahebeCheck=12326

											--insert into #tmpSooratHesab2 (ID,shomare,fldSanad,fldRiz,
											--			fldDate,fldTedad,fldNameVahed,fldNameVahed2,fldFee,fldFeeYekCarton,
											--			fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,
											--			fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,fldMablaghKhales,
											--			fldCodeAnbar,fldNameAnbar,fldType)
											--	Select Convert(varchar,@EmpId) + Convert(varchar,fldRadif+100),'امانی'+Convert(varchar, @ShomareAmani),@ShomareSanad, dbo.tblKala.fldNameKala + ' ' + dbo.tblRizeForoosh.fldSharhKala,
											--			@bigDate2,dbo.tblRizeForoosh.fldTedadJoz,tblVahed_2.fldNameVahed, dbo.tblVahed.fldNameVahed AS fldNameVahed2,dbo.tblRizeForoosh.fldFeeJoz,dbo.tblRizeForoosh.fldFeeYekCarton,
											--			dbo.tblRizeForoosh.fldFeeKol,dbo.tblRizeForoosh.fldDarsadTakhfif,dbo.tblRizeForoosh.fldMablaghTakhfif,dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, 
											--			dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif,dbo.tblRizeForoosh.fldMablaghKhales,
											--			dbo.tblRizeForoosh.fldCodeAnbar,dbo.tblAnbar.fldNameAnbar,'amani'
											--	FROM            dbo.tblAnbar INNER JOIN
											--							 dbo.tblRizeForoosh ON dbo.tblAnbar.fldCode = dbo.tblRizeForoosh.fldCodeAnbar INNER JOIN
											--							 dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
											--							 dbo.tblVahed AS tblVahed_2 ON dbo.tblKala.fldCodeVahed1 = tblVahed_2.ID INNER JOIN
											--							 dbo.tblVahed ON dbo.tblKala.fldCodeVahed2 = dbo.tblVahed.ID
											--	WHERE        (dbo.tblRizeForoosh.fldShomareAmani = @ShomareAmani)
										end

							
							if @type='BargashteCheckeVagoz'   ---برگشت چک واگذارشده
								begin									
											declare @ShomarePardakht2 bigint,@CodeTafzili2 nvarchar(10),@SumBed2 decimal (18, 4),@Bed2 decimal (18, 4),@ShomareSanad2  bigint,@cnt2 bigint,@ShomareEnteghal bigint,@CodeMoin2 nvarchar(50)
											set @bigDate2=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
											set @ShomareSanad2=(Select fldSanad from tblSodooresanad Where ID=@EmpId)
											set @CodeTafzili2=(Select fldCodeTafzili from tblSodooresanad Where ID=@EmpId)
											set @SumBed2=(Select Sum(fldBedehkar) as SumBed from tblSodooreSanad Where fldCodeTafzili=@CodeTafzili2 And fldBedehkar>0 and fldSanad=@ShomareSanad2)
											set @Bed2=(Select top(1)fldBedehkar from tblSodooreSanad Where ID=@EmpId)
											set @ShomareEnteghal=(Select top(1)fldShomareEnteghal from tblCheckeDaryafti Where fldShomareSanadEnteghal=@ShomareSanad2)
											execute CodeMoinHesab 'check',@codemoin2 output
											--insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
											--			values ( 999 ,'621'  ,132,'345',990205,111,'dfg')

											--set  @cnt2=(Select count(fldCodeTafzili) from tblTafzili Where fldCodeTafzili=@CodeTafzili2 and ID<@EmpId )
											--if @cnt2=0
											--	begin
													if @Bed2>0 
														begin																
															set @ShomarePardakht2=(Select top(1)fldShomarePardakht from tblCheckeDaryafti Where fldShomareSanadEnteghal=@ShomareSanad2)
									

															--update #tmpSooratHesab2 set  fldBestankar=6666, shomare =@ShomareEnteghal,  fldSharh='' + case when fldSharh<>'' and fldSharh is not null then ' ' +fldSharh else '' end,fldType='BargashteCheckeVagozarShode' Where fldBedehkar>0 and ID=@EmpId 

															--insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
															--	select Convert(varchar,@EmpId)+'103' ,'bbb'  ,@ShomareSanad2,'چک شماره ' + Convert(varchar,fldShomareCheck) + ' تاریخ ' + Convert(varchar,fldDateSarResid)   ,@bigDate2,fldMablagh,'BargashteCheckeVagozarShode'
															--	from tblCheckeDaryafti Where fldShomareSanadEnteghal = @ShomareSanad2 and  fldShomarePardakht=21
															--update #tmpSooratHesab2 set shomare =34 Where id=@EmpId --and fldMoinKamel <> @codemoin2 and fldCodeTafzili=@CodeTafzili2

															update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad2 and fldMoinKamel = @codemoin2 and fldCodeTafzili=@CodeTafzili2
														end
													else
														begin
															update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad2 and fldMoinKamel = @codemoin2 and fldCodeTafzili=@CodeTafzili2
															update #tmpSooratHesab2 set shomare =@ShomareEnteghal Where id=@EmpId --and fldMoinKamel <> @codemoin2 and fldCodeTafzili=@CodeTafzili2
														end 

											--	end
											--else
											--	update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad2 and fldMoinKamel = '31301' and fldCodeTafzili=@CodeTafzili2
												--set @ShomarePardakht2=(Select top(1)fldShomarePardakht from tblCheckeDaryafti Where fldShomareCheck)
												--set @ShomarePardakht2=(select fldCodeTafzili from tblPardakht where fldShomarePardakht=@ShomarePardakht2)

										
												--select * from tblCheckeDaryafti Where fldShomareSanadEnteghal = 132

								end

						

								if @type='OdateCheck'   ---عودت چک فروخته شده
									begin									
												declare @ShomarePardakht3 bigint,@CodeTafzili3 nvarchar(10),@SumBed3 decimal (18, 4),@BeS3 decimal (18, 4),@ShomareSanad3  bigint,@cnt3 bigint,@ShomareEnteghal3 bigint,@CodeMoin3 nvarchar(50)
												set @bigDate2=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
												set @ShomareSanad3=(Select fldSanad from tblSodooresanad Where ID=@EmpId)
												set @CodeTafzili3=(Select fldCodeTafzili from tblSodooresanad Where ID=@EmpId)
												set @SumBed3=(Select Sum(fldBedehkar) as SumBed from tblSodooreSanad Where fldCodeTafzili=@CodeTafzili3 And fldBedehkar>0 and fldSanad=@ShomareSanad3)
												set @BeS3=(Select top(1)fldBestankar from tblSodooreSanad Where ID=@EmpId)
												set @ShomareEnteghal=(Select top(1)fldShomareOdat from tblCheckeDaryafti Where fldShomareSanadOdat=@ShomareSanad3)
												execute CodeMoinHesab 'check',@codemoin3 output
												--insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
												--			values ( 999 ,'621'  ,132,'345',990205,111,'dfg')

												--set  @cnt2=(Select count(fldCodeTafzili) from tblTafzili Where fldCodeTafzili=@CodeTafzili2 and ID<@EmpId )
												--if @cnt2=0
												--	begin
														if @BeS3>0 
															begin																
																set @ShomarePardakht3=(Select top(1)fldShomarePardakht from tblCheckeDaryafti Where fldShomareSanadEnteghal=@ShomareSanad3)
									

																update #tmpSooratHesab2 set   shomare =@ShomareEnteghal,  fldSharh='' + case when fldSharh<>'' and fldSharh is not null then ' ' +fldSharh else '' end,fldType='OdateCheck' Where fldBedehkar>0 and ID=@EmpId 

																--insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
																--	select Convert(varchar,@EmpId)+'103' ,'bbb'  ,@ShomareSanad2,'چک شماره ' + Convert(varchar,fldShomareCheck) + ' تاریخ ' + Convert(varchar,fldDateSarResid)   ,@bigDate2,fldMablagh,'BargashteCheckeVagozarShode'
																--	from tblCheckeDaryafti Where fldShomareSanadEnteghal = @ShomareSanad2 and  fldShomarePardakht=21

																update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad3 and fldMoinKamel = @codemoin3 and fldCodeTafzili=@CodeTafzili3
															end
														else
															begin
																update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad3 and fldMoinKamel = @codemoin3 and fldCodeTafzili=@CodeTafzili3
																update #tmpSooratHesab2 set shomare =@ShomareEnteghal Where id=@EmpId 
															end

												--	end
												--else
												--	update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad2 and fldMoinKamel = '31301' and fldCodeTafzili=@CodeTafzili2
													--set @ShomarePardakht2=(Select top(1)fldShomarePardakht from tblCheckeDaryafti Where fldShomareCheck)
													--set @ShomarePardakht2=(select fldCodeTafzili from tblPardakht where fldShomarePardakht=@ShomarePardakht2)

										
													--select * from tblCheckeDaryafti Where fldShomareSanadEnteghal = 132

									end

								if @Type='bazpascheckekhodeman'  --بازپسگیری چک خود
									begin									
												declare @ShomarePardakht4 bigint,@CodeTafzili4 nvarchar(10),@SumBed4 decimal (18, 4),@Bed4 decimal (18, 4),@ShomareSanad4  bigint,@CodeMoin4 nvarchar(50)
												set @bigDate2=(Select Convert(bigint,flddate) from tblSodooresanad Where ID=@EmpId)
												set @ShomareSanad4=(Select fldSanad from tblSodooresanad Where ID=@EmpId)
												set @CodeTafzili4=(Select fldCodeTafzili from tblSodooresanad Where ID=@EmpId)
												set @SumBed4=(Select Sum(fldBedehkar) as SumBed from tblSodooreSanad Where fldCodeTafzili=@CodeTafzili4 And fldBedehkar>0 and fldSanad=@ShomareSanad4)
												set @Bed4=(Select top(1)fldBedehkar from tblSodooreSanad Where ID=@EmpId)
												set @ShomareEnteghal=(Select top(1)fldShomareOdat from tblCheckeDaryafti Where fldShomareSanadOdat=@ShomareSanad4)
												execute CodeMoinHesab 'checkpardakhti',@codemoin4 output

												if @Bed4>0 
													begin																
														set @ShomarePardakht4=(Select top(1)fldShomarePardakht from tblCheckeDaryafti Where fldShomareSanadEnteghal=@ShomareSanad4)
									

														update #tmpSooratHesab2 set   shomare =@ShomareEnteghal,  fldSharh='' + case when fldSharh<>'' and fldSharh is not null then ' ' +fldSharh else '' end,fldType='OdateCheck' Where fldBedehkar>0 and ID=@EmpId 

														update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad3 and fldMoinKamel = @codemoin4 and fldCodeTafzili=@CodeTafzili4
													end
												else
													begin
														update #tmpSooratHesab2 set fldVaziat='delete' Where fldSanad= @ShomareSanad4 and fldMoinKamel = @codemoin4 and fldCodeTafzili=@CodeTafzili4
														update #tmpSooratHesab2 set shomare =@ShomareEnteghal Where id=@EmpId 
													end

									end


									--if @type='ekhtetamie'
									--	begin
									--		update #tmpSooratHesab2
									--				set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel= @strMoinKamelCheckDaryafti 
									--	end
									--if @type='eftetahie'
									--	begin
									--		update #tmpSooratHesab2
									--				set fldVaziat='delete' where fldSanad=@ShomareSanad And fldMoinKamel= @strMoinKamelCheckDaryafti 
									--	end
						Fetch Next from EmpCursor
						into @EmpId
					end
			Close EmpCurSor
			Deallocate EmpCursor

			--set @select ='Select ID, fldSanad, fldDay, fldMonth, fldYear, fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
		 --                        fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
		 --                        fldVasete from #tmpSooratHesab2'

			--execute (@Select)
			--Select ID, fldSanad,  fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
		 --                       fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
		 --                       fldVasete from #tmpSooratHesab2



		  execute codemoinhesab 'hesabhayedaryaftani' ,@CodekolMoinDaryaftTanzimat output	

			INSERT INTO #tmpSooratHesab2
								 (ID, fldSanad, fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
								 fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
								 fldVasete)
							Select ID, fldSanad,fldCodeKol, fldCodeMoin, fldCodeTafzili, fldCodeMarkaz1, fldCodeMarkaz2, fldBedehkar, fldBestankar, fldRadif, 
								 fldCodeSharh, fldDate, fldSharh, fldMoinKamel, fldCodeNoeTafzili, fldGhateiMovaghat, fldGhateiDaem, fldSanadGhadim, fldShomarePeigiri, fldMoghayerat, fldType, 
								 fldVasete
							from tblSodooreSanad 
								Where fldCodeTafzili=@CodeTafzili and fldMoinKamel=@CodekolMoinDaryaftTanzimat and fldType='ebteda'

			--رکوردهایی که در پایان باید حذف بشن
			delete #tmpSooratHesab2 where fldVaziat='delete'

			--#### حذف محدوده ها ######


			set @SumBedGhabl=(Select Sum(fldBedehkar) as SumBed from  #tmpSooratHesab2  Where fldBestankar is not null and fldBedehkar is not null and Convert(dec,fldDate)<@bigDateMin)
			if @SumBedGhabl is null
				set @SumBedGhabl =0

			set @SumBesGhabl=(Select Sum(fldBestankar) as SumBes from  #tmpSooratHesab2  Where fldBestankar is not null and fldBedehkar is not null  and Convert(dec,fldDate)<@bigDateMin)
			if @SumBesGhabl is null
				set @SumBesGhabl=0

			delete #tmpSooratHesab2  Where Convert(dec,fldDate)<@bigDateMin
			delete #tmpSooratHesab2  Where Convert(dec,fldDate)>@bigDateMax
	
			update #tmpSooratHesab2 set fldNameVahed2='' Where fldNameVahed2=N'انتخاب کنید'

								

			if @flagMande =1 --با نقل مانده		
				--if (@SumBedGhabl-@SumBesGhabl)<>0
				--	insert into #tmpSooratHesab2
				--		(id,shomare,fldSanad,fldSharh,fldDate,fldBedehkar,fldBestankar,fldMande,fldtype)
				--			values
				--		('0','','0','مانده قبل','0',@SumBedGhabl,@SumBesGhabl,@SumBedGhabl-@SumBesGhabl,'ghabl')
				begin					
						declare @CodeKol2 nvarchar(100),@MandeTalab2 decimal (18, 4),@MandeBedehi2 decimal (18, 4)

						execute CodeKolHesab 'kolhesabhayedaryaftani',@CodeKol2 output

						set @MandeTalab2=(SELECT       ISNULL( SUM(ISNULL(fldBedehkar, 0)-ISNULL(fldBestankar, 0)),0) AS SumM
							FROM            tblSodooreSanad
							WHERE      (fldCodeKol=@CodeKol2) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldDate)<@bigDateMin)

					

						execute CodeKolHesab 'kolhesabhayepardakhtani',@CodeKol2 output

						set @MandeBedehi2=(SELECT       ISNULL( SUM(ISNULL(fldBestankar, 0)-ISNULL(fldBedehkar, 0)),0) AS SumM
							FROM            tblSodooreSanad
							WHERE      (fldCodeKol=@CodeKol2) and  (fldCodeTafzili = @CodeTafzili ) and Convert(bigint,fldDate)<@bigDateMin)
						set @decMandeGhabl=@MandeTalab2-@MandeBedehi2								
					if @decMandeGhabl<>0
						execute z187TranslateKalame @language,16180,@SharheTranslate output--مانده قبل
						insert into #tmpSooratHesab2
							(id,shomare,fldSanad,fldSharh,fldDate,fldBedehkar,fldBestankar,fldMande,fldtype)
								values
							('0','','0',@SharheTranslate,@bigDateMin,@MandeTalab2,@MandeBedehi2,@decMandeGhabl,'ghabl')
				end 


			--select * from #tmpSooratHesab2 order by Convert(dec,fldSanad),Convert(varchar,id)
			declare @strOrder nvarchar(30)

			--if @intOrder=0  --مرتب سازی براساس تاریخ
			--	select * from #tmpSooratHesab2 order by Convert(dec,fldDate),Convert(varchar,id)
			--else --مرتب سازی براساس سند
			--	select * from #tmpSooratHesab2 order by Convert(dec,fldSanad),Convert(varchar,id)

			--update  #tmpSooratHesab2 set flddate=substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2)
			delete from #tmpSooratHesab2 where fldSanad in (Select fldSanadPas from tblCheckePardakhti Where fldCodeSahebeCheck=@CodeTafzili )
	
			declare @MaxId bigint
			set @Maxid =(Select Max(Convert(dec,Id)) as MaxId from #tmpSooratHesab2)
			if isnumeric(@maxId)=0
				set @maxId=1
			else
				set @maxId=@maxId+1


			execute z187TranslateKalame @language,16181,@SharheTranslate1 output--برگشت چک
			execute z187TranslateKalame @language,16182,@SharheTranslate2 output--برگشت چک به شماره
			--set @MaxId=1000
			insert into #tmpSooratHesab2 (id,shomare,fldSanad,fldRiz,fldDate,fldMablaghKhales,fldType)
					Select Convert(varchar,@MaxId),@SharheTranslate1 ,'0',@SharheTranslate2 + ' '  + Convert(varchar,fldShomareCheck),substring(Convert(varchar,fldDateBargasht),3,6),fldMablagh,'bargasht' 
						FROM tblCheckeDaryafti
						Where fldVaziat='Bargasht' and fldCodeSahebeCheck=@CodeTafzili
			
					--values ('2000' ,'ش برگشت' ,'',''  ,0,0,'bargasht')
						--values (Convert(varchar,@EmpId) + '200' ,'ش برگشت' + Convert(varchar,@ShomarePardakht) ,@ShomareSanad,@strSharheHesab + case when  @strShareBank='' then '' else '-' + @strShareBank end  ,@bigDate,@lngBank,'pardakht')



			if @intOrder=0  --مرتب سازی براساس تاریخ
				select  ID,radif,shomare,fldSanad,fldSharh,fldRiz,substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate,fldTedad,fldNameVahed,
					fldNameVahed2,fldFee,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,
					fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,
					fldMablaghKhales,fldCodeAnbar,fldNameAnbar,fldMablagh,fldBedehkar,fldBestankar,fldMande,
					fldType,fldVaziat,fldVasete,fldCodeKarbar,fldNameKarbar
				from #tmpSooratHesab2  order by Convert(dec,fldDate),Convert(varchar,id)
				--select  shomare,fldSanad,fldSharh,fldRiz,fldDate,fldTedad,fldNameVahed,
				--	fldFee,fldMablaghKhales,fldBedehkar,fldBestankar,fldMande			
				--	from #tmpSooratHesab2  order by Convert(dec,fldDate),Convert(varchar,id)
			else
				if @intOrder=1  --مرتب سازی براساس سند
					select ID,radif,shomare,fldSanad,fldSharh,fldRiz,substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate,fldTedad,fldNameVahed,
						fldNameVahed2,fldFee,fldFeeYekCarton,fldFeeKol,fldDarsadTakhfif,fldMablaghTakhfif,
						fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldFeeKolBadAzTakhfif,
						fldMablaghKhales,fldCodeAnbar,fldNameAnbar,fldMablagh,fldBedehkar,fldBestankar,fldMande,
						fldType,fldVaziat,fldVasete,fldCodeKarbar,fldNameKarbar
					from #tmpSooratHesab2  order by Convert(dec,fldSanad),Convert(varchar,id)
	end
end





GO
/****** Object:  StoredProcedure [dbo].[spEmpDetails]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spEmpDetails]
@Type VARCHAR(15),

@Details3 TestEmpType2 Readonly
AS
BEGIN
            if @type='1'
				begin
            --For inserting the new records in the table
					INSERT INTO tblTest3(fld1, fld2) 
						SELECT ID, Name FROM @Details3 
				end
			else
				begin
					INSERT INTO tblTest3(fld1, fld2) 
						SELECT ID, Name FROM @Details3 
				end
				EXEC usp_InsertProductionLocation @Details3;
end

GO
/****** Object:  StoredProcedure [dbo].[spTable]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[spTable]
@tbl tbl1 Readonly

as

begin

	insert into tbltest6
		select * from @tbl
end

GO
/****** Object:  StoredProcedure [dbo].[TafzilieDaramad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[TafzilieDaramad]
@CodeKolMoin nvarchar(10),
@Sanad bigint

as

begin
	declare @CodeKolMoin2 nvarchar(10),@strTypeOfTafzili nvarchar(50)
	execute CodeMoinHesab 'daramad',@CodeKolMoin output

	set @strTypeOfTafzili=(	Select isnull(fldCodeNoeHesab,'-1') from tblKolMoin where fldCodeHesab=  @CodeKolMoin  and fldNoeHesab<>'تفصيلي ندارد')
	if @strTypeOfTafzili<>'-1' 
		begin	
			--set @Mande=(SELECT       isnull( SUM(ISNULL(fldBedehkar, -1) - ISNULL(fldBestankar, 0)),0) AS SumM
			--	FROM            tblSodooreSanad
			--	WHERE      (fldMoinKamel=@CodeKolMoin)  and Convert(bigint,fldSanad)<=@Sanad)
			Select fldCodeTafzili,fldSharheTafzili from tblTafzili where fldCodeNoe=@strTypeOfTafzili order by fldCodeTafzili

		end
end

GO
/****** Object:  StoredProcedure [dbo].[TekrariAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[TekrariAnbar]

@NameAnbar nvarchar(70),
@ID int,
@IDType int,
@flagOUt int output
as
begin
	declare @RowCountAnbar int
	if @IDType=0  --برای مقایسه برای حالت اضافه کردن رکورد
		begin
		 set @RowCountAnbar=(Select Count(fldNameAnbar) from tblAnbar  Where fldNameAnbar=@NameAnbar)
		 set @flagOUt=@rowcountanbar
		end
	else
		begin --برای مقایسه با غیرخودش در حالت ویرایش
		   set	@RowCountAnbar=(Select  Count(fldNameAnbar) from tblAnbar  Where fldNameAnbar=@NameAnbar and ID<>@ID)
		   set @flagOUt=@rowcountanbar
		end
end

GO
/****** Object:  StoredProcedure [dbo].[TekrariBankeMoshtari]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[TekrariBankeMoshtari]

@NameBank nvarchar(70),
@ID int,
@IDType int,
@flagOUt int output
as
begin
	declare @RowCountBank int
	if @IDType=0  --برای مقایسه برای حالت اضافه کردن رکورد
		begin
		 set @RowCountBank=(Select Count(fldNameBank) from tblBankeMoshtari  Where fldNameBank=@NameBank)
		 set @flagOUt=@rowcountBank
		end
	else
		begin --برای مقایسه با غیرخودش در حالت ویرایش
		   set	@RowCountBank=(Select  Count(fldNameBank) from tblBankeMoshtari  Where fldNameBank=@NameBank and ID<>@ID)
		   set @flagOUt=@rowcountBank
		end
end

GO
/****** Object:  StoredProcedure [dbo].[TekrariKolMoin]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[TekrariKolMoin]

@NameKolMoin nvarchar(50),
@ID int,
@IDType int,
@flagOUt int output
as
begin
	declare @RowCountKolMoin int
	if @IDType=0  --برای مقایسه برای حالت اضافه کردن رکورد
		begin
		 set @RowCountKolMoin=(Select Count(fldSharheHesab) from tblKolMoin  Where fldSharheHesab=@NameKolMoin)
		 set @flagOUt=@RowCountKolMoin
		end
	else
		begin --برای مقایسه با غیرخودش در حالت ویرایش
		   set	@RowCountKolMoin=(Select  Count(fldSharheHesab) from tblKolMoin  Where fldSharheHesab=@NameKolMoin and ID<>@ID)
		   set @flagOUt=@RowCountKolMoin
		end
end

GO
/****** Object:  StoredProcedure [dbo].[TekrariMablaghPorsantVisitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[TekrariMablaghPorsantVisitor]
--چندزبانه
@MablaghPorsant decimal (18, 4),
@fldCodeVisitor int,
@fldRadif int,
@IDType int,
@flagOUt int output
as
begin
	declare @RowCountPorsant int
	if @IDType=0  --براي مقايسه براي حالت اضافه کردن رکورد
		begin
		 set @RowCountPorsant=(Select Count(fldMablagh) from tblPorsantVisitor  Where fldMablagh=@MablaghPorsant and fldCodeVisitor=@fldCodeVisitor and fldRadif<>@fldRadif)
		 set @flagOUt=@RowCountPorsant
		end
	else
		begin --براي مقايسه با غيرخودش در حالت ويرايش
		   set	@RowCountPorsant=(Select  Count(fldMablagh) from tblPorsantVisitor  Where fldMablagh=@MablaghPorsant and fldCodeVisitor=@fldCodeVisitor and fldRadif<>@fldRadif)
		   set @flagOUt=@RowCountPorsant
		end
end

GO
/****** Object:  StoredProcedure [dbo].[TekrariMablaghTakhfif]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[TekrariMablaghTakhfif]
--چندزبانه
@MablaghTakhfif bigint,
@ID int,
@IDType int,
@flagOUt int output
as
begin
	declare @RowCountTakhfif int
	if @IDType=0  --براي مقايسه براي حالت اضافه کردن رکورد
		begin
		 set @RowCountTakhfif=(Select Count(fldMablagh) from tblTarheTakhfif  Where fldMablagh=@MablaghTakhfif)
		 set @flagOUt=@RowCountTakhfif
		end
	else
		begin --براي مقايسه با غيرخودش در حالت ويرايش
		   set	@RowCountTakhfif=(Select  Count(fldMablagh) from tblTarheTakhfif  Where fldMablagh=@MablaghTakhfif and countRow<>@ID)
		   set @flagOUt=@RowCountTakhfif
		end
end

GO
/****** Object:  StoredProcedure [dbo].[TekrariVahed]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[TekrariVahed]

@NameVahed nvarchar(70),
@ID int,
@IDType int,
@flagOUt int output
as
begin
	declare @RowCountAnbar int
	if @IDType=0  --برای مقایسه برای حالت اضافه کردن رکورد
		begin
		 set @RowCountAnbar=(Select Count(fldNameVahed) from tblVahed  Where fldNameVahed=@NameVahed)
		 set @flagOUt=@rowcountanbar
		end
	else
		begin --برای مقایسه با غیرخودش در حالت ویرایش
		   set	@RowCountAnbar=(Select  Count(fldNameVahed) from tblVahed  Where fldNameVahed=@NameVahed and ID<>@ID)
		   set @flagOUt=@rowcountanbar
		end
end

GO
/****** Object:  StoredProcedure [dbo].[TestCursor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[TestCursor]
--@NameHesab nvarChar(50),
--@Meghdar bigint OutPut


as
--این کرسر کارش اینه که یک یکه رکوردهای جدول سند رو فراخوانی میکنه و طبق حلقه بروزسانی انجام میده
begin
	declare @EmpId bigint
	declare EmpCursor Cursor For
		Select Id from tblSodooreSanad With (NoLock)
			open EmpCursor
			fetch Next From EmpCursor into @EmpId
			While @@Fetch_Status = 0
				Begin
					Update tblSodooreSanad With (RowLock) Set [fldGhateiMovaghat]='false'
					Where ID=@EmpId

				Fetch Next from EmpCursor
				into @EmpId
			end
	Close EmpCurSor
	Deallocate EmpCursor

end

GO
/****** Object:  StoredProcedure [dbo].[testehsan]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[testehsan]
	@dtCheckDaryafti tblCheckDaryafti readonly,
	@s bigint output
	as

BEGIN
    
	set @s=(SELECT count(*) from @dtCheckDaryafti);
END

GO
/****** Object:  StoredProcedure [dbo].[TestInsert]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[TestInsert]
@adad bigint
--@inID bigint output
as

begin
	--declare @lngShomareSanad nvarchar(50)
	declare @lngShomareSanad int
	--execute insertSanadFirst '111','01','11101','1111','1',@adad,0,'1',950101,'sha','daryaft',@lngShomareSanad output	;
	execute  ToolePaye 'kol',@lngShomareSanad output;
	INSERT INTO tblTest7(fld1)
		VALUES        (@lngShomareSanad)
end

GO
/****** Object:  StoredProcedure [dbo].[TESTReport_ListAshkhas_groupby_NameGroup_order_SharhTafsili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[TESTReport_ListAshkhas_groupby_NameGroup_order_SharhTafsili]
@CodeGroupAshkhas varchar(1000)
as
declare @myReport varchar(1000)
begin
 set @myReport='SELECT DISTINCT 
                         dbo.tblTafzili.fldCodeGroup, dbo.tblGroupAshkhas.fldGroupId, dbo.tblGroupAshkhas.fldGroupName, dbo.tblDaftarcheTell.fldShomare, 
                         dbo.tblTafzili.fldCodeTafzili
FROM            dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili ON dbo.tblGroupAshkhas.fldGroupId = dbo.tblTafzili.fldCodeGroup INNER JOIN
                         dbo.tblDaftarcheTell ON CONVERT(int, dbo.tblTafzili.fldCodeTafzili) = CONVERT(int, dbo.tblDaftarcheTell.fldCodeTafzili)
where fldCodeGroup in('+ @CodeGroupAshkhas +')
GROUP BY dbo.tblGroupAshkhas.fldGroupName, dbo.tblTafzili.fldCodeTafzili, dbo.tblTafzili.fldSharheTafzili, dbo.tblDaftarcheTell.fldShomare, dbo.tblTafzili.fldCodeMeli, 
                         dbo.tblTafzili.fldAddress,dbo.tblGroupAshkhas.fldGroupId
ORDER BY dbo.tblTafzili.fldSharheTafzili'
exec( @myReport)
end

GO
/****** Object:  StoredProcedure [dbo].[TestTransaction]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[TestTransaction]
@intadad as bigint,
@intKhata int output
as
begin
	begin transaction transact
		begin try
			insert into tblTest2
				values('mohammad')
				
			if @intadad>0 
				begin
					insert into tblTest
						values(@intadad)
				end
			else
				begin
					insert into tblTest
						values('majid')
				end
			
	commit transaction
		end try

	begin catch
		if @@trancount>0 
			rollback transaction ;
			set @intkhata=1

	end catch
end

GO
/****** Object:  StoredProcedure [dbo].[ToolePaye]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[ToolePaye]
@type nvarchar(10),
@Tool int output
as
begin
	if @type='kol'
		begin
			set @tool=(select fldKol from   tblPaye )
		end
	else if @type='moin'
		begin
			set @tool=(select fldMoin from tblPaye)
		end
	else if @type='tafzili'
		begin
			set @tool=(select fldTafzili from tblPaye)
		end
	else if @type='markaz1'
		begin
			set @tool=(select fldMarkaz1 from tblPaye)
		end
	else if @type='markaz2'
		begin
			set @tool=(select fldMarkaz2 from tblPaye)
		end

end

GO
/****** Object:  StoredProcedure [dbo].[typeSanad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[typeSanad] 
	-- Add the parameters for the stored procedure here
	@Sanad bigint,
	@strType nvarchar(20) output,
	@Shomare bigint output
AS
BEGIN
	declare @bigCount bigint

	set @strType=''
	--فروش
   	set @bigCount = (select  count(fldShomareSanad) as cnt from tblForoosh where fldShomareSanad=@Sanad )
	if @bigCount>0
		begin
			set @strType='foroosh'
			set @shomare = (select  fldShomareFactorForoosh  from tblForoosh where fldShomareSanad=@Sanad )
		end

	--خرید
   	set @bigCount = (select  count(fldShomareSanad) as cnt from tblKharid where fldShomareSanad=@Sanad )
	if @bigCount>0
		begin
			set @strType='kharid'
			set @shomare = (select  fldShomareFactorKharid  from tblKharid where fldShomareSanad=@Sanad )
		end

	--برگشت از فروش
   	set @bigCount = (select  count(fldShomareSanad) as cnt from tblBargashtAzForoosh where fldShomareSanad=@Sanad )
	if @bigCount>0
		begin
			set @strType='BargashtAzForoosh'
			set @shomare = (select  fldShomareFactorForoosh  from tblBargashtAzForoosh where fldShomareSanad=@Sanad )
		end

	--برگشت از خرید
   	set @bigCount = (select  count(fldShomareSanad) as cnt from tblBargashtAzKharid where fldShomareSanad=@Sanad )
	if @bigCount>0
		begin
			set @strType='BargashtAzKharid'
			set @shomare = (select  fldShomareFactorKharid  from tblBargashtAzKharid where fldShomareSanad=@Sanad )
		end

	--دریافت
   	set @bigCount = (select  count(fldShomareSanad) as cnt from tblDaryaft where fldShomareSanad=@Sanad And fldType='daryaft')
	if @bigCount>0
		begin
			set @strType='daryaft'
			set @shomare = (select  fldShomareDaryaft  from tblDaryaft where fldShomareSanad=@Sanad And fldType='daryaft')
		end

	--درآمد
   	set @bigCount = (select  count(fldShomareSanad) as cnt from tblDaryaft where fldShomareSanad=@Sanad And fldType='daramad')
	if @bigCount>0
		begin
			set @strType='daramad'
			set @shomare = (select  fldShomareDaryaft  from tblDaryaft where fldShomareSanad=@Sanad And fldType='daramad')
		end
	--پرداخت
   	set @bigCount = (select  count(fldShomareSanad) as cnt from tblPardakht where fldShomareSanad=@Sanad And fldType='pardakht')
	if @bigCount>0
		begin
			set @strType='pardakht'
			set @shomare = (select  fldShomarePardakht  from tblPardakht where fldShomareSanad=@Sanad And fldType='pardakht')
		end

	
	--انتقال وجه
   	set @bigCount = (select  count(fldShomareSanad) as cnt from tblPardakht where fldShomareSanad=@Sanad And fldType='enteghalevajh')
	if @bigCount>0
		begin
			set @strType='enteghalevajh'
			set @shomare = (select  fldShomarePardakht  from tblPardakht where fldShomareSanad=@Sanad And fldType='enteghalevajh')
		end

	--پرداخت هزینه
   	set @bigCount = (select  count(fldShomareSanad) as cnt from tblPardakht where fldShomareSanad=@Sanad And fldType='hazine')
	if @bigCount>0
		begin
			set @strType='hazine'
			set @shomare = (select  fldShomarePardakht  from tblPardakht where fldShomareSanad=@Sanad And fldType='hazine')
		end


	--وصول چک
   	set @bigCount = (select  count(fldShomareSanad) as cnt from tblVosool where fldShomareSanad=@Sanad )
	if @bigCount>0
		begin
			set @strType='vosool'
			set @shomare = (select  fldShomareVosool  from tblVosool where fldShomareSanad=@Sanad )
		end

		--پاس چک
   	set @bigCount = (select  count(fldShomareSanad) as cnt from tblPas where fldShomareSanad=@Sanad )
	if @bigCount>0
		begin
			set @strType='pasecheck'
			set @shomare = (select  fldShomarePas  from tblPas where fldShomareSanad=@Sanad )
		end
	--
   	set @bigCount = (select  count(fldSanad) as cnt from tblSodooreSanad where fldSanad=@Sanad And fldType='EbtedaAshkhas')
	if @bigCount>0
		begin
			set @strType='EbtedaAshkhas'
		end

	if @strType=''
		set @strType='sanaddasti'
END

GO
/****** Object:  StoredProcedure [dbo].[UpadteChap]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[UpadteChap]
	@fldNameMoassese nvarchar(150),
	@fldTozihateBala nvarchar(150),
	@fldAdress nvarchar(MAX),
	@fldPasafhe nvarchar(MAX),
	@fldCodeEghtesadi nvarchar(20),
	@fldShomareSabt nvarchar(20),
	@fldCodePosti nvarchar(20),
	@fldTell nvarchar(50)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	update tblChap 
		set fldNameMoassese= @fldNameMoassese ,
			fldTozihateBala=@fldTozihateBala ,
			fldAdress=@fldAdress ,
			fldPasafhe=@fldPasafhe ,
			fldCodeEghtesadi=@fldCodeEghtesadi ,
			fldShomareSabt=@fldShomareSabt ,
			fldCodePosti=@fldCodePosti ,
			fldTell=@fldTell 
	
	update tblControlPanel
		set fldNameSherkat=@fldNameMoassese
END

GO
/****** Object:  StoredProcedure [dbo].[Update_SuperVizor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Update_SuperVizor] 
--چندزبانه
	@ID bigint,
	@fldPoorsant decimal (18, 4),
	@fldSharheTafzili nvarchar(200),
	@fldNoeMohasebePoorsant int
AS
BEGIN
	update tblTafzili set fldSharheTafzili=@fldSharheTafzili,fldPoorsant=@fldPoorsant,
			fldNoeMohasebePoorsant=@fldNoeMohasebePoorsant
		where ID=@ID
		
	
END

GO
/****** Object:  StoredProcedure [dbo].[Update_Tbl_DaftarcheTell_Has_CodeTafisili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Update_Tbl_DaftarcheTell_Has_CodeTafisili]
@fldIdTell bigint,
@fldcodeTafzili nvarchar(10),
@fldShomare	nvarchar(50),
@fldSms	nvarchar(20),
@fldSharh	nvarchar(100),
@flagUpdate int output	
as
begin

update tblDaftarcheTell
set
fldCodeTafzili=@fldcodeTafzili,
fldShomare= @fldShomare,
fldSms= @fldSms,
fldSharh= @fldSharh 
 where fldIdDaftarche=@fldIdTell
set @flagUpdate=1
end

GO
/****** Object:  StoredProcedure [dbo].[Update_Tbl_DaftarcheTell_NotHas_CodeTafisili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Update_Tbl_DaftarcheTell_NotHas_CodeTafisili]
@fldIdTell bigint,
@fldName	nvarchar(50),
@fldShomare	nvarchar(50),
@fldSms	nvarchar(20),
@fldSharh	nvarchar(100),
@flagUpdate int output	
as
begin


update tblDaftarcheTell
set
fldCodeTafzili='0',
fldName= @fldName,
fldShomare= @fldShomare,
fldSms= @fldSms,
fldSharh= @fldSharh 
 where fldIdDaftarche=@fldIdTell
set @flagUpdate=1
end

GO
/****** Object:  StoredProcedure [dbo].[Update_Tbl_Tafsili]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[Update_Tbl_Tafsili]
--چندزبانه
@fldNoeHesab	nvarchar(15),	
@fldSharheTafzili	nvarchar(200),
@fldCodeTafzili	nvarchar(10),	
@fldCodeNoe	nvarchar(5),	--set "1"
@fldNoe	nvarchar(10),	
@fldCodeGroup	bigint,	        
@fldOstan	nvarchar(50),	
@fldCity	nvarchar(50),	
@fldAddress	nvarchar(200),	
@fldShomareHesab	nvarchar(20),	
@fldCodeMeli	nvarchar(20),	
@fldCodeEghtesadi	nvarchar(20),	
@fldCodePosti	nvarchar(20),	
@fldShomareSabt	nvarchar(20),	
@fldSaghfeEtebar	decimal(18,4),
@fldBlackList	nvarchar(5),	
@fldSharhBlackList	nvarchar(100),	
@fldDate1	bigint,	
@fldDate2	bigint,	
@fldVisitor	bigint,	
@fldPic	nvarchar(300),
@flagUpdateOK int output

as
begin
set @fldVisitor=isnull(@fldVisitor,0)
update  tblTafzili
set fldNoeHesab='',
    fldSharheTafzili=@fldSharheTafzili,
	fldCodeNoe=@fldCodeNoe,
	fldNoe=@fldNoe,
	fldCodeGroup=@fldCodeGroup,
	fldOstan=@fldOstan,
	fldCity=@fldCity,
	fldAddress=@fldAddress,
	fldShomareHesab=@fldShomareHesab,
	fldCodeMeli=@fldCodeMeli,
	fldCodeEghtesadi=@fldCodeEghtesadi,
	fldCodePosti=@fldCodePosti,
	fldShomareSabt=@fldShomareSabt,
	fldSaghfeEtebar=@fldSaghfeEtebar,
	fldBlackList=@fldBlackList,
	fldSharhBlackList=@fldSharhBlackList,
	fldDate1=@fldDate1,
	fldDate2=@fldDate2,
	fldVisitor=@fldVisitor,
	fldPic=@fldPic
where fldCodeTafzili=@fldCodeTafzili

update tblDaftarcheTell set fldName=@fldSharheTafzili where fldCodeTafzili=@fldCodeTafzili

set @flagUpdateOK=1
END

GO
/****** Object:  StoredProcedure [dbo].[Update_Vizitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Update_Vizitor] 
	@ID bigint,
	@fldPoorsant decimal(18,2),
	@fldSharheTafzili nvarchar(200),
	@fldNoeMohasebePoorsant int,
	@CodeSuperVisor nvarchar(10)
AS
BEGIN
	update tblTafzili set fldSharheTafzili=@fldSharheTafzili,fldPoorsant=@fldPoorsant,
			fldNoeMohasebePoorsant=@fldNoeMohasebePoorsant,fldCodeSuperVisor=@CodeSuperVisor
		where ID=@ID
		
	
END

GO
/****** Object:  StoredProcedure [dbo].[UpdateDaftarCheTelPersonel]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[UpdateDaftarCheTelPersonel] 
	@CodeTafzili nvarchar(10),
	@Name nvarchar(50),
	@ShomareOld nvarchar(50),
	@ShomareNew nvarchar(50)

AS
BEGIN
	--if ISNUMERIC(@shomarenew)=1
	delete tblDaftarcheTell Where fldShomare=@ShomareOld and fldCodeTafzili=@CodeTafzili

	INSERT        INTO              tblDaftarcheTell(fldCodeTafzili, fldName, fldShomare, fldSms, fldSharh)
			VALUES        (@CodeTafzili,@Name,@ShomareNew,'غیرفعال','')
		
	--update tblDaftarcheTell
	--	set fldShomare=@ShomareNew,fldName=@Name
	--	Where fldShomare=@ShomareOld and fldCodeTafzili=@CodeTafzili
END

GO
/****** Object:  StoredProcedure [dbo].[UpdateGroupAshkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[UpdateGroupAshkhas]
@fldGroupAshkhasId bigint,
@fldGroupAshkhasName nvarchar(50),
@flagduplicate  int output
AS

Begin
if not Exists(select fldGroupName  from tblGroupAshkhas where fldGroupName=@fldGroupAshkhasName and  fldGroupId<> @fldGroupAshkhasId)
	 begin
		update tblGroupAshkhas
		set fldGroupName=@fldGroupAshkhasName
		where fldGroupId=@fldGroupAshkhasId

		set @flagduplicate=0 
	 end
 else
	 begin
		set @flagduplicate=1
	 end
end

GO
/****** Object:  StoredProcedure [dbo].[UpdateGroupKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[UpdateGroupKala]
@fldGroupKalaId bigint,
@fldGroupKalaName nvarchar(50),
@flagduplicate  int output
AS
--BEGIN
--set @hasrow=(select count(fldGroupId) from tblGroupKala where CONVERT(nvarchar(50),fldGroupId) like CONVERT(nvarchar(50), @fldGroupKalaId) + '%')
--if @hasrow=1
--begin

--set @f=0

Begin
if not Exists(select fldGroupName  from tblGroupKala where fldGroupName=@fldGroupKalaName)
 begin
update tblGroupKala
set fldGroupName=@fldGroupKalaName
where fldGroupId=@fldGroupKalaId

set @flagduplicate=0 

 end
 else
 begin
 set @flagduplicate=1
 end
end
--end
--else
--begin
--set @f=1
--  end
--END

GO
/****** Object:  StoredProcedure [dbo].[UpdateTblAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[UpdateTblAnbar]

@ID int,
@NameAnbar nvarchar(70),
@flagOut int output
as

begin
	Update tblAnbar Set fldNameAnbar=@NameAnbar Where ID=@id
	set @flagOut=1
end

GO
/****** Object:  StoredProcedure [dbo].[UpdateTblBankeMoshtari]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[UpdateTblBankeMoshtari]

@ID int,
@NameBank nvarchar(70),
@flagOut int output
as

begin
	Update tblBankeMoshtari Set fldNameBank=@NameBank Where ID=@id
	set @flagOut=1
end

GO
/****** Object:  StoredProcedure [dbo].[UpdateTblKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[UpdateTblKala]
	@dtTblKala dtKala  readonly
AS
BEGIN
	delete from tblKala

	INSERT      into          dbo.tblKala( fldNameKala, fldCodeKala, fldSerial, fldCodeGroup, fldCodeVahed1, fldCodeVahed2, fldNogheSefaresh, fldSharh, fldTedadJoz, fldFeeDarsadKharid, 
                         fldFeeBadAzTakhfif, fldNerkhePorsantKol, fldNerkhePorsantJoz, fldFeeForoosh, fldPathPic, fldGhymatMasrafkonande, fldIDPrinter)
			select         fldNameKala, fldCodeKala, fldSerial, fldCodeGroup, fldCodeVahed1, fldCodeVahed2, fldNogheSefaresh, fldSharh, fldTedadJoz, fldFeeDarsadKharid, 
									 fldFeeBadAzTakhfif, fldNerkhePorsantKol, fldNerkhePorsantJoz, fldFeeForoosh, fldPathPic, fldGhymatMasrafkonande, fldIDPrinter output from @dtTblKala
END

GO
/****** Object:  StoredProcedure [dbo].[UpdateTblKolMoin]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[UpdateTblKolMoin]

@ID int,
@NameHesab nvarchar(70),
@CodeType nvarchar(5),
@SharheNoeTafzili nvarchar(20),
@flagOut int output
as

begin
	Update tblKolMoin Set fldSharheHesab =@NameHesab,fldCodeNoeHesab=@CodeType,fldNoeHesab=@SharheNoeTafzili Where ID=@id
	set @flagOut=1
end

GO
/****** Object:  StoredProcedure [dbo].[UpdateTblPorsantVisitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[UpdateTblPorsantVisitor]
--چندزبانه
@fldRadif int,
@fldMablagh decimal (18, 4),
@fldDarsad decimal (18, 4),
@flagOut int output
as

begin
	Update tblPorsantVisitor Set fldMablagh =@fldMablagh,fldDarsad=@fldDarsad Where fldRadif=@fldRadif
	set @flagOut=1
end

GO
/****** Object:  StoredProcedure [dbo].[UpdateTblTafziliIdCaller]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[UpdateTblTafziliIdCaller] 
	@CodeTafzili nvarchar(10),
	@Address nvarchar(200),
	@SharheTafzili nvarchar(200)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	update tblTafzili Set fldAddress=@Address , fldSharheTafzili=@SharheTafzili  Where fldCodeTafzili=@CodeTafzili

END

GO
/****** Object:  StoredProcedure [dbo].[UpdateTblTarheTakhfif]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[UpdateTblTarheTakhfif]
--چندزبانه
@ID int,
@fldMablagh decimal(18,4),
@fldDarsad decimal(18,4),
@flagOut int output
as

begin
	Update tblTarheTakhfif Set fldMablagh =@fldMablagh,fldDarsad=@fldDarsad Where countRow=@id
	set @flagOut=1
end

GO
/****** Object:  StoredProcedure [dbo].[UpdateTblVahed]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[UpdateTblVahed]

@ID int,
@NameVahed nvarchar(70),
@flagOut int output
as

begin
	Update tblVahed Set fldNameVahed=@NameVahed Where ID=@id
	set @flagOut=1
end

GO
/****** Object:  StoredProcedure [dbo].[usp_InsertProductionLocation]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_InsertProductionLocation]  
    @TVP LocationTableType READONLY  
    AS   
    SET NOCOUNT ON  
    INSERT INTO tblTest5 
           (name1,  
            id) 
        SELECT *
        FROM  @TVP;

GO
/****** Object:  StoredProcedure [dbo].[z100HavaleGoroohi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z100HavaleGoroohi] 
	-- Add the parameters for the stored procedure here
	@strWhere nvarchar(4000),
	@intorder int,
	@lngSumTedadejoz float output,
	@lngSumTedadeKol bigint output
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here

	create table #tmpHavale
		(
			fldCodeKala bigint,
			fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
			fldSumJoz float,
			fldTedadDarKarton decimal(18,0),
			fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
			fldTedadDarKartonShow nvarchar(70) COLLATE Persian_100_CI_AS ,
			fldTedadKartonadadSahih bigint,
			fldNameVahed2 NVarchar(100) COLLATE Persian_100_CI_AS,
			fldCodeVahed1 bigint,
			fldCodeVahed2 bigint
		)

  execute('insert into #tmpHavale 
		(fldCodeKala,fldNameKala,fldTedadDarKarton,fldSumJoz, fldCodeVahed1,fldCodeVahed2)
		SELECT        tblRizeForoosh.fldCodeKala, tblKala.fldNameKala,tblKala.fldTedadJoz,Convert(float, SUM(tblRizeForoosh.fldTedadJoz)) AS SumM,tblKala.fldCodeVahed1,tblKala.fldCodeVahed2
		FROM            tblRizeForoosh INNER JOIN
								 tblKala ON tblRizeForoosh.fldCodeKala = tblKala.fldCodeKala '
						 + @strWhere + 
		
		 ' GROUP BY tblRizeForoosh.fldCodeKala,  tblKala.fldNameKala,tblKala.fldTedadJoz ,tblKala.fldCodeVahed1,tblKala.fldCodeVahed2 ')





--تعداد کارتن و تعداد جز در آن
update #tmpHavale set fldNameVahed2='' where fldTedadDarKarton=0


update #tmpHavale set #tmpHavale.fldNameVahed=tbl2.fldNameVahed,#tmpHavale.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2
where #tmpHavale.fldCodeKala=tbl2.fldCodeKala


update #tmpHavale Set fldTedadDarKartonShow=Convert(varchar,floor(Convert(dec,fldSumJoz) /Convert(dec, fldTedadDarKarton))) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Convert(dec,fldSumJoz), Convert(dec, fldTedadDarKarton)) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #tmpHavale set fldNameVahed='' where fldCodeVahed1=1
update #tmpHavale set fldNameVahed2='' where fldCodeVahed2=1
--update #tmpHavale Set fldTedadVoroodKartonShow=Convert(varchar,floor(SumVorood / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(SumVorood, fldTedadDarKarton) } ) + ' ' + fldNameVahed
--	where fldTedadDarKarton>0

--update #tmpHavale Set fldTedadKhoroojKartonShow=Convert(varchar,floor(SumKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Sumkhorooj, fldTedadDarKarton) } ) + ' ' + fldNameVahed
--	where fldTedadDarKarton>0
update #tmpHavale set fldTedadKartonadadSahih=(fldSumJoz/fldTedadDarKarton)   Where fldTedadDarKarton>0
update #tmpHavale set fldTedadKartonadadSahih=0 Where isnumeric(fldTedadKartonadadSahih)=0

set @lngSumTedadejoz=(Select Sum(fldSumJoz) from #tmpHavale)
set @lngSumTedadeKol=(Select Sum(fldTedadKartonadadSahih) from #tmpHavale)

	if @intorder=0  --مرتب سازی براساس کد کالا
		Select fldCodeKala,fldNameKala,fldSumJoz, fldNameVahed,fldTedadDarKarton,fldTedadDarKartonShow,fldNameVahed2 from #tmpHavale order by fldCodeKala


	if @intorder=1  --مرتب سازی براساس نام کالا
		Select fldCodeKala,fldNameKala,fldSumJoz, fldNameVahed,fldTedadDarKarton,fldTedadDarKartonShow,fldNameVahed2 from #tmpHavale order by fldNameKala


	if @intorder=2  --مرتب سازی براساس تعداد جز
		Select fldCodeKala,fldNameKala,fldSumJoz, fldNameVahed,fldTedadDarKarton,fldTedadDarKartonShow,fldNameVahed2 from #tmpHavale order by fldSumJoz


	--execute('SELECT        tblRizeForoosh.fldCodeKala, tblKala.fldNameKala, SUM(tblRizeForoosh.fldTedadJoz) AS SumM
	--	FROM            tblRizeForoosh INNER JOIN
	--							 tblKala ON tblRizeForoosh.fldCodeKala = tblKala.fldCodeKala '
	--					 + @strWhere + 
		
	--	' GROUP BY tblRizeForoosh.fldCodeKala,  tblKala.fldNameKala ')

END

GO
/****** Object:  StoredProcedure [dbo].[z101ExecueQuery]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[z101ExecueQuery] 
	@strQuery varchar(5000)=null
	--@ListCodeAshkhas varchar(5000)=null,
	--@ListCodeVisitor varchar(5000)=null,
	--@DataType varchar(40),
	--@DateFrom varchar(8),
	--@DateTo varchar(8),
	--@FlagSort Varchar(5)
	--@strQuery varhar(5000)=null
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	execute (@strQuery)

END

GO
/****** Object:  StoredProcedure [dbo].[z102InsertHavaleGoroohi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[z102InsertHavaleGoroohi]
	-- Add the parameters for the stored procedure here
	
	@tblShomareFactor dtlisteforoosh readonly,	
	@fldDate bigint,
	@fldCodeRanande bigint,
	@fldShomareHavale bigint,
	@NoeKhata nvarchar(10) output
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


--اگر شماره حواله صفر اومد یعنی شماره جدید ثبت بشه
--در غیر اینصورت اصلاحه
	declare @maxShomareHavale bigint
		
begin transaction transact
	begin try
		set @maxShomareHavale=(Select Max(fldShomareHavale) as MaxShomare from tblHavaleGoroohi)
		if @maxShomareHavale is null	
			set @maxShomareHavale=0

		set @maxShomareHavale=@maxShomareHavale+1

		if @fldShomareHavale>0 
			begin
				delete from tblHavaleGoroohi where fldShomareHavale=@fldShomareHavale
				set @maxShomareHavale=@fldShomareHavale
			end

		

		INSERT INTO tblHavaleGoroohi
			(fldShomareFactor, fldShomareHavale, fldDate, fldCodeRanande)
			select fldShomareFactor,@maxShomareHavale,@fldDate,@fldCodeRanande  from @tblShomarefactor
	
			commit transaction
			end try
		begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='rolback'

		end catch
		

END


GO
/****** Object:  StoredProcedure [dbo].[z103ReportCheckDaryafti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z103ReportCheckDaryafti]
--چندزبانه
@fldCodeArz int,
@language nvarchar(10),
@Type nvarchar(20),
@ListGroupAshkhas varchar(5000)=null,
@ListCodeAshkhas varchar(5000)=null,
@ListCodeVisitor varchar(5000)=null,
@DateSarResidAz bigint,
@DateSarResidTa bigint,
@DateDaryaftAz bigint,
@DateDaryaftTa bigint,
@JameMablagh decimal(18,4) output

	
AS
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END
BEGIN
	
SET NOCOUNT ON;


create table #TempReportCheck(
fldCodeArz int,
fldNerkheArz decimal(18,4),
ID bigint,
fldShomareCheck bigint,
fldShomareHesab bigint,
fldCodeSahebeCheck Nvarchar(10) collate Persian_100_CI_AS,
fldSharheTafzili  Nvarchar(200) collate Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldDateSarResid bigint,
fldDateSarResidShow  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeBank int, 
fldNameBank Nvarchar(100) collate Persian_100_CI_AS, 
fldShobe Nvarchar(40) collate Persian_100_CI_AS,
fldMablagh decimal(18,4),
fldDateDaryaft bigint, 
fldDateDaryaftShow Nvarchar(20) collate Persian_100_CI_AS,
fldShomareDaryaft bigint,
fldVaziat  Nvarchar(15) collate Persian_100_CI_AS,
fldShomarePardakht bigint,
fldShomareVosool bigint,
fldDateVosool bigint,
fldDateVosoolShow nvarchar(15) collate Persian_100_CI_AS,
fldVaziatShow nvarchar(200)  collate Persian_100_CI_AS,
fldSanadVosool bigint,
fldCodeHesabeVosool bigint,
fldNameHesabeVosool nvarchar(200)  collate Persian_100_CI_AS,
fldCodeBankVagozari bigint,
fldNameBankVagozari nvarchar(200)  collate Persian_100_CI_AS,
fldDateVagozariBeBank bigint,
fldDateVagozariBeBankShow nvarchar(20)  collate Persian_100_CI_AS,
fldCodeVasete nvarchar(10)  collate Persian_100_CI_AS,
fldNameVasete nvarchar(200) collate Persian_100_CI_AS,
fldNameGroupAshkhas nvarchar(80) collate Persian_100_CI_AS

)

--if @type='all'
	begin

	--براي انتقال چکهای ابتدای دوره
	--##################################################################################################################
	update tblCheckeDaryafti set fldVaziat='vagozari' where ISNUMERIC( fldDateVagozariBeBank )=1 and fldVaziat is null
	update tblCheckeDaryafti set fldVaziat='vosool' where ISNUMERIC( fldDateVosool )=1 and fldVaziat is null
	update tblCheckeDaryafti set fldVaziat='forooshecheck' where ISNUMERIC( fldShomarePardakht )=1 and fldVaziat is null
	update tblCheckeDaryafti set fldVaziat='Bargasht' where ISNUMERIC( fldDateBargasht )=1 and fldVaziat is null
	--##################################################################################################################

	Insert into  #TempReportCheck(fldCodeArz,fldNerkheArz, ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDateDaryaft,fldDateDaryaftShow,fldShomareDaryaft,fldVaziat,fldShomarePardakht,
			fldShomareVosool,fldDateVosool,fldDateVosoolShow,fldSanadVosool,fldCodeHesabeVosool,fldCodeBankVagozari,fldDateVagozariBeBank,fldDateVagozariBeBankShow,fldCodeVasete
			 )
		SELECT   fldCodeArz,fldNerkheArz,dbo.tblCheckeDaryafti.ID,dbo.tblCheckeDaryafti.fldShomareCheck,dbo.tblCheckeDaryafti.fldShomareHesab,dbo.tblCheckeDaryafti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldCodeGroup,fldDateSarResid,
								SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid,
								 dbo.tblCheckeDaryafti.fldCodeBank,dbo.tblBankeMoshtari.fldNameBank, dbo.tblCheckeDaryafti.fldShobe, 
								 dbo.tblCheckeDaryafti.fldMablagh,fldDateDaryaft, SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),1,4)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),5,2)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),7,2) As fldDateDaryaft ,dbo.tblCheckeDaryafti.fldShomareDaryaft,dbo.tblCheckeDaryafti.fldVaziat, dbo.tblCheckeDaryafti.fldShomarePardakht,
								 fldShomareVosool,fldDateVosool,
								 SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVosool), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVosool), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVosool), 7, 2) AS fldDateVosoolShow,
								 dbo.tblCheckeDaryafti.fldSanadVosool,dbo.tblCheckeDaryafti.fldCodeHesabeVosool,dbo.tblCheckeDaryafti.fldCodeBankVagozari,fldDateVagozariBeBank,
								 SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVagozariBeBank), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVagozariBeBank), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVagozariBeBank), 7, 2) AS fldDateVagozariBeBankShow,tblCheckeDaryafti.fldVasete
                         
		FROM            dbo.tblCheckeDaryafti INNER JOIN
								 dbo.tblTafzili ON dbo.tblCheckeDaryafti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
								 dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID									 
					order by tblCheckeDaryafti.fldDateSarResid,tblCheckeDaryafti.fldDateDaryaft
	end
--else if @type='vosoolnashode'
--	SELECT        dbo.tblCheckeDaryafti.ID,dbo.tblCheckeDaryafti.fldShomareCheck,dbo.tblCheckeDaryafti.fldShomareHesab,dbo.tblCheckeDaryafti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldDateSarResid,
--								SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid,
--								 dbo.tblCheckeDaryafti.fldCodeBank,dbo.tblBankeMoshtari.fldNameBank, dbo.tblCheckeDaryafti.fldShobe, 
--								 dbo.tblCheckeDaryafti.fldMablagh, SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),1,4)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),5,2)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),7,2) As fldDateDaryaft ,dbo.tblCheckeDaryafti.fldShomareDaryaft,dbo.tblCheckeDaryafti.fldVaziat, dbo.tblCheckeDaryafti.fldShomarePardakht
                         
--		FROM            dbo.tblCheckeDaryafti INNER JOIN
--								 dbo.tblTafzili ON dbo.tblCheckeDaryafti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
--								 dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID
--		WHERE        (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat IS NULL) OR
--								 (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat = 'vagozari')
--					order by tblCheckeDaryafti.fldDateSarResid,tblCheckeDaryafti.fldDateDaryaft

--if @type<>'search' 
--	if @ListGroupAshkhas<>''
--		begin
--		declare @StrDeleteGroupAshkhas varchar(5000)
--		set  @StrDeleteGroupAshkhas='delete from #TempReportCheck 
--			where '+ @ListGroupAshkhas + ''
--			execute(@StrDeleteGroupAshkhas)
--		end


if @ListCodeAshkhas <>''
begin
	declare @StrDeleteAshkhas varchar(5000)
	set  @StrDeleteAshkhas='delete from #TempReportCheck 
		where fldCodeSahebeCheck NOT IN ('+ @ListCodeAshkhas +')'
		execute(@StrDeleteAshkhas)
end


if @ListCodeVisitor<>''
	begin
	if @ListCodeVisitor='-1' --لیست فاکتورهای بدون ویزیتور
		begin
			delete from #TempReportCheck where fldCodeVasete<>''
		end
	else
		begin
			declare @StrDeleteVisitor varchar(5000)
			set  @StrDeleteVisitor='delete from #TempReportCheck 
				where '+ @ListCodeVisitor + ''
				execute(@StrDeleteVisitor)
		end
	end

if @Type<>'search'
	begin
		delete #TempReportCheck Where fldDateSarResid< @DateSarResidAz
		delete #TempReportCheck Where fldDateSarResid> @DateSarResidTa
		delete #TempReportCheck Where fldDateDaryaft< @DateDaryaftAz
		delete #TempReportCheck Where fldDateDaryaft> @DateDaryaftTa
	end
else if @Type='search'
	begin
		execute('delete #TempReportCheck Where' + @ListGroupAshkhas )
	end

update #TempReportCheck set fldNameVasete=tblTafzili.fldSharheTafzili from tblTafzili Where #TempReportCheck.fldCodeVasete=tblTafzili.fldCodeTafzili
update #TempReportCheck set fldNameBankVagozari =tblKolMoin.fldSharheHesab from tblKolMoin Where tblKolMoin.fldCodeHesab =fldCodeBankVagozari
update #TempReportCheck set fldVaziatShow=(Select * from dbo.translatefunction(@LANGUAGE,18286)) + ' ' + fldNameBankVagozari where fldVaziat='vagozari'
update #TempReportCheck set fldVaziatShow=(Select * from dbo.translatefunction(@LANGUAGE,18287)) + ' ' + Convert(varchar,fldShomareVosool) where fldVaziat='vosool'
update #TempReportCheck set fldVaziat='foroosh', fldVaziatShow=(Select * from dbo.translatefunction(@LANGUAGE,18288)) + ' ' + Convert(varchar,fldShomarePardakht) + ' ' + (Select * from dbo.translatefunction(@LANGUAGE,18289)) + ' ' + tbl1.NameShakhs from 
		(SELECT        tblTafzili.fldSharheTafzili As NameShakhs, tblPardakht.fldShomarePardakht As ShomarePardakht FROM tblTafzili INNER JOIN tblPardakht ON tblTafzili.fldCodeTafzili = tblPardakht.fldCodeTafzili) As Tbl1
		Where tbl1.ShomarePardakht=fldShomarePardakht

update #TempReportCheck set fldVaziat ='' where fldVaziat is null
if @type='vosoolnashode'
	delete #TempReportCheck where fldVaziat<>''
if @type='vosool'
	begin
		delete #TempReportCheck where fldVaziat<>'vosool'
		update #TempReportCheck set fldNameHesabeVosool=tblKolMoin.fldSharheHesab from tblKolMoin 
			Where #TempReportCheck.fldCodeHesabeVosool=tblKolMoin.fldCodeHesab
	end
if @type='vagozari'
	delete #TempReportCheck where fldVaziat<>'vagozari'
if @type='Bargasht'
	delete #TempReportCheck where fldVaziat<>'Bargasht'
if @type='foroosh'
	delete #TempReportCheck where fldVaziat<>'foroosh'




update #TempReportCheck set fldNameGroupAshkhas=tblGroupAshkhas.fldGroupName from tblGroupAshkhas Where tblGroupAshkhas.fldGroupId=#TempReportCheck.fldCodeGroupAshkhas

Set @JameMablagh=(Select Sum(fldMablagh) from #TempReportCheck)
if @JameMablagh is null
	set @JameMablagh=0



if @fldCodeArz=1	 --واحد ارزی پایه
	begin
		update #TempReportCheck set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

		update #TempReportCheck set fldMablagh=fldMablagh/ fldNerkheArz --from tblCheckeDaryafti Where #TempReportCheck.fldShomareSanad=tblCheckeDaryafti.fldSanad
	end

if @fldCodeArz>1
	begin
			update tblSodooreSanad set fldNerkheArz=1,fldCodeArz=1 Where fldNerkheArz is null

			declare @Nerkh decimal(18,4)

			set @Nerkh =(Select fldNerkheArz from tblVahedeArzi Where fldCodeArz=(@fldCodeArz))


			--تبدیل به نرخ پایه
			update #TempReportCheck set fldMablagh=fldMablagh/ fldNerkheArz --from tblSodooreSanad Where #TempReportCheck.fldShomareSanad=tblSodooreSanad.fldSanad --and fldCodeArz<>@fldCodeArz

				--تبدیل به نرخ روز ارز انتخابی
			update #TempReportCheck set fldMablagh=fldMablagh* @Nerkh
	end



Select ROW_NUMBER() OVER(ORDER BY fldDateDaryaft,ID ) AS Row,ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,
		fldCodeBank,fldNameBank,fldShobe,fldMablagh,fldDateDaryaft,fldDateDaryaftShow,
		fldShomareDaryaft,fldVaziat ,fldVaziatShow,fldShomarePardakht,
		fldShomareVosool,fldDateVosool,fldDateVosoolShow,fldSanadVosool,fldCodeHesabeVosool,fldNameHesabeVosool,fldCodeBankVagozari,fldNameBankVagozari,fldDateVagozariBeBank,fldDateVagozariBeBankShow,fldCodeVasete,fldNameVasete,fldNameGroupAshkhas
		 from  #TempReportCheck

END
GO
/****** Object:  StoredProcedure [dbo].[z103ReportCheckDaryafti_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z103ReportCheckDaryafti_WithUserCode]
--چندزبانه
@language nvarchar(10),
@Type nvarchar(20),
@ListGroupAshkhas varchar(5000)=null,
@ListCodeAshkhas varchar(5000)=null,
@ListCodeVisitor varchar(5000)=null,
@DateSarResidAz bigint,
@DateSarResidTa bigint,
@DateDaryaftAz bigint,
@DateDaryaftTa bigint,
@userCode bigint,
@JameMablagh decimal(18,4) output
	
AS
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END
BEGIN
	
SET NOCOUNT ON;


create table #TempReportCheck(

ID bigint,
fldShomareCheck bigint,
fldShomareHesab bigint,
fldCodeSahebeCheck Nvarchar(10) collate Persian_100_CI_AS,
fldSharheTafzili  Nvarchar(200) collate Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldDateSarResid bigint,
fldDateSarResidShow  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeBank int, 
fldNameBank Nvarchar(100) collate Persian_100_CI_AS, 
fldShobe Nvarchar(40) collate Persian_100_CI_AS,
fldMablagh decimal(18,4),
fldDateDaryaft bigint, 
fldDateDaryaftShow Nvarchar(20) collate Persian_100_CI_AS,
fldShomareDaryaft bigint,
fldVaziat  Nvarchar(15) collate Persian_100_CI_AS,
fldShomarePardakht bigint,
fldShomareVosool bigint,
fldDateVosool bigint,
fldDateVosoolShow nvarchar(15) collate Persian_100_CI_AS,
fldVaziatShow nvarchar(200)  collate Persian_100_CI_AS,
fldSanadVosool bigint,
fldCodeHesabeVosool bigint,
fldNameHesabeVosool nvarchar(200)  collate Persian_100_CI_AS,
fldCodeBankVagozari bigint,
fldNameBankVagozari nvarchar(200)  collate Persian_100_CI_AS,
fldDateVagozariBeBank bigint,
fldDateVagozariBeBankShow nvarchar(20)  collate Persian_100_CI_AS,
fldCodeVasete nvarchar(10)  collate Persian_100_CI_AS,
fldNameVasete nvarchar(200) collate Persian_100_CI_AS,
fldNameGroupAshkhas nvarchar(80) collate Persian_100_CI_AS

)

--if @type='all'
	begin

	--براي انتقال چکهای ابتدای دوره
	--##################################################################################################################
	update tblCheckeDaryafti set fldVaziat='vagozari' where ISNUMERIC( fldDateVagozariBeBank )=1 and fldVaziat is null
	update tblCheckeDaryafti set fldVaziat='vosool' where ISNUMERIC( fldDateVosool )=1 and fldVaziat is null
	update tblCheckeDaryafti set fldVaziat='forooshecheck' where ISNUMERIC( fldShomarePardakht )=1 and fldVaziat is null
	update tblCheckeDaryafti set fldVaziat='Bargasht' where ISNUMERIC( fldDateBargasht )=1 and fldVaziat is null
	--##################################################################################################################

	Insert into  #TempReportCheck(ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDateDaryaft,fldDateDaryaftShow,fldShomareDaryaft,fldVaziat,fldShomarePardakht,
			fldShomareVosool,fldDateVosool,fldDateVosoolShow,fldSanadVosool,fldCodeHesabeVosool,fldCodeBankVagozari,fldDateVagozariBeBank,fldDateVagozariBeBankShow,fldCodeVasete
			 )
		SELECT        dbo.tblCheckeDaryafti.ID,dbo.tblCheckeDaryafti.fldShomareCheck,dbo.tblCheckeDaryafti.fldShomareHesab,dbo.tblCheckeDaryafti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldCodeGroup,fldDateSarResid,
								SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid,
								 dbo.tblCheckeDaryafti.fldCodeBank,dbo.tblBankeMoshtari.fldNameBank, dbo.tblCheckeDaryafti.fldShobe, 
								 dbo.tblCheckeDaryafti.fldMablagh,fldDateDaryaft, SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),1,4)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),5,2)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),7,2) As fldDateDaryaft ,dbo.tblCheckeDaryafti.fldShomareDaryaft,dbo.tblCheckeDaryafti.fldVaziat, dbo.tblCheckeDaryafti.fldShomarePardakht,
								 fldShomareVosool,fldDateVosool,
								 SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVosool), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVosool), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVosool), 7, 2) AS fldDateVosoolShow,
								 dbo.tblCheckeDaryafti.fldSanadVosool,dbo.tblCheckeDaryafti.fldCodeHesabeVosool,dbo.tblCheckeDaryafti.fldCodeBankVagozari,fldDateVagozariBeBank,
								 SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVagozariBeBank), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVagozariBeBank), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateVagozariBeBank), 7, 2) AS fldDateVagozariBeBankShow,tblCheckeDaryafti.fldVasete
                         
		FROM            dbo.tblCheckeDaryafti INNER JOIN
								 dbo.tblTafzili ON dbo.tblCheckeDaryafti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
								 dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID									 
					order by tblCheckeDaryafti.fldDateSarResid,tblCheckeDaryafti.fldDateDaryaft
	end
--else if @type='vosoolnashode'
--	SELECT        dbo.tblCheckeDaryafti.ID,dbo.tblCheckeDaryafti.fldShomareCheck,dbo.tblCheckeDaryafti.fldShomareHesab,dbo.tblCheckeDaryafti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldDateSarResid,
--								SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid,
--								 dbo.tblCheckeDaryafti.fldCodeBank,dbo.tblBankeMoshtari.fldNameBank, dbo.tblCheckeDaryafti.fldShobe, 
--								 dbo.tblCheckeDaryafti.fldMablagh, SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),1,4)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),5,2)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),7,2) As fldDateDaryaft ,dbo.tblCheckeDaryafti.fldShomareDaryaft,dbo.tblCheckeDaryafti.fldVaziat, dbo.tblCheckeDaryafti.fldShomarePardakht
                         
--		FROM            dbo.tblCheckeDaryafti INNER JOIN
--								 dbo.tblTafzili ON dbo.tblCheckeDaryafti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
--								 dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID
--		WHERE        (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat IS NULL) OR
--								 (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat = 'vagozari')
--					order by tblCheckeDaryafti.fldDateSarResid,tblCheckeDaryafti.fldDateDaryaft

--if @type<>'search' 
--	if @ListGroupAshkhas<>''
--		begin
--		declare @StrDeleteGroupAshkhas varchar(5000)
--		set  @StrDeleteGroupAshkhas='delete from #TempReportCheck 
--			where '+ @ListGroupAshkhas + ''
--			execute(@StrDeleteGroupAshkhas)
--		end


if @ListCodeAshkhas <>''
begin
	declare @StrDeleteAshkhas varchar(5000)
	set  @StrDeleteAshkhas='delete from #TempReportCheck 
		where fldCodeSahebeCheck NOT IN ('+ @ListCodeAshkhas +')'
		execute(@StrDeleteAshkhas)
end


if @ListCodeVisitor<>''
	begin
	if @ListCodeVisitor='-1' --لیست فاکتورهای بدون ویزیتور
		begin
			delete from #TempReportCheck where fldCodeVasete<>''
		end
	else
		begin
			declare @StrDeleteVisitor varchar(5000)
			set  @StrDeleteVisitor='delete from #TempReportCheck 
				where '+ @ListCodeVisitor + ''
				execute(@StrDeleteVisitor)
		end
	end

if @Type<>'search'
	begin
		delete #TempReportCheck Where fldDateSarResid< @DateSarResidAz
		delete #TempReportCheck Where fldDateSarResid> @DateSarResidTa
		delete #TempReportCheck Where fldDateDaryaft< @DateDaryaftAz
		delete #TempReportCheck Where fldDateDaryaft> @DateDaryaftTa
	end
else if @Type='search'
	begin
		execute('delete #TempReportCheck Where' + @ListGroupAshkhas )
	end

update #TempReportCheck set fldNameVasete=tblTafzili.fldSharheTafzili from tblTafzili Where #TempReportCheck.fldCodeVasete=tblTafzili.fldCodeTafzili
update #TempReportCheck set fldNameBankVagozari =tblKolMoin.fldSharheHesab from tblKolMoin Where tblKolMoin.fldCodeHesab =fldCodeBankVagozari
update #TempReportCheck set fldVaziatShow=(Select * from dbo.translatefunction(@LANGUAGE,18286)) + ' ' + fldNameBankVagozari where fldVaziat='vagozari'
update #TempReportCheck set fldVaziatShow=(Select * from dbo.translatefunction(@LANGUAGE,18287)) + ' ' + Convert(varchar,fldShomareVosool) where fldVaziat='vosool'
update #TempReportCheck set fldVaziat='forooshecheck', fldVaziatShow=(Select * from dbo.translatefunction(@LANGUAGE,18288)) + ' ' + Convert(varchar,fldShomarePardakht) + ' ' + (Select * from dbo.translatefunction(@LANGUAGE,18289)) + ' ' + tbl1.NameShakhs from 
		(SELECT        tblTafzili.fldSharheTafzili As NameShakhs, tblPardakht.fldShomarePardakht As ShomarePardakht FROM tblTafzili INNER JOIN tblPardakht ON tblTafzili.fldCodeTafzili = tblPardakht.fldCodeTafzili) As Tbl1
		Where tbl1.ShomarePardakht=fldShomarePardakht

update #TempReportCheck set fldVaziat ='' where fldVaziat is null
if @type='vosoolnashode'
	delete #TempReportCheck where fldVaziat<>''
if @type='vosool'
	begin
		delete #TempReportCheck where fldVaziat<>'vosool'
		update #TempReportCheck set fldNameHesabeVosool=tblKolMoin.fldSharheHesab from tblKolMoin 
			Where #TempReportCheck.fldCodeHesabeVosool=tblKolMoin.fldCodeHesab
	end
if @type='vagozari'
	delete #TempReportCheck where fldVaziat<>'vagozari'
if @type='Bargasht'
	delete #TempReportCheck where fldVaziat<>'Bargasht'
if @type='foroosh'
	delete #TempReportCheck where fldVaziat<>'forooshecheck'




update #TempReportCheck set fldNameGroupAshkhas=tblGroupAshkhas.fldGroupName from tblGroupAshkhas Where tblGroupAshkhas.fldGroupId=#TempReportCheck.fldCodeGroupAshkhas

Set @JameMablagh=(Select Sum(fldMablagh) from #TempReportCheck)
if @JameMablagh is null
	set @JameMablagh=0

-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################

Select ROW_NUMBER() OVER(ORDER BY fldDateDaryaft,ID ) AS Row,ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,
		fldCodeBank,fldNameBank,fldShobe,fldMablagh,fldDateDaryaft,fldDateDaryaftShow,
		fldShomareDaryaft,fldVaziat ,fldVaziatShow,fldShomarePardakht,
		fldShomareVosool,fldDateVosool,fldDateVosoolShow,fldSanadVosool,fldCodeHesabeVosool,fldNameHesabeVosool,fldCodeBankVagozari,fldNameBankVagozari,fldDateVagozariBeBank,fldDateVagozariBeBankShow,fldCodeVasete,fldNameVasete,fldNameGroupAshkhas
		 from  #TempReportCheck
		 where fldCodeSahebeCheck in (select code from @accessCodes)

END
GO
/****** Object:  StoredProcedure [dbo].[z104ChartForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z104ChartForoosh]
	@language nvarchar(10),
	@year bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @strCodeMoinForoosh nvarchar(20),@JameForoosh bigint
    create table #tmpChartForoosh
		(
			fldNameMah nvarchar(20) COLLATE Persian_100_CI_AS,
			fldShomareMah bigint,
			fldJameForoosh bigint
		)
	
	
	execute CodeMoinHesab 'foroosh',@strCodeMoinForoosh output --'71301'
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0101' ) and Convert(bigint,  Convert(varchar, @year) + '0131' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
		
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13313)),1,@JameForoosh)
		
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0201' ) and Convert(bigint,  Convert(varchar, @year) + '0231' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
		
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13314)),2,@JameForoosh)
		
		
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0301' ) and Convert(bigint,  Convert(varchar, @year) + '0331' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13315)),3,@JameForoosh)
	
	
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0401' ) and Convert(bigint,  Convert(varchar, @year) + '0431' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13316)),4,@JameForoosh)
	
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0501' ) and Convert(bigint,  Convert(varchar, @year) + '0531' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13317)),5,@JameForoosh)
		
	
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0601' ) and Convert(bigint,  Convert(varchar, @year) + '0631' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13318)),6,@JameForoosh)
		
		
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0701' ) and Convert(bigint,  Convert(varchar, @year) + '0731' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13319)),7,@JameForoosh)
		
		
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0801' ) and Convert(bigint,  Convert(varchar, @year) + '0831' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13320)),8,@JameForoosh)
		
		
		
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0901' ) and Convert(bigint,  Convert(varchar, @year) + '0931' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13321)),9,@JameForoosh)
		
		
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '1001' ) and Convert(bigint,  Convert(varchar, @year) + '1031' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13322)),10,@JameForoosh)
		
		
		
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '1101' ) and Convert(bigint,  Convert(varchar, @year) + '1131' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13323)),11,@JameForoosh)	
		
		
	set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '1201' ) and Convert(bigint,  Convert(varchar, @year) + '1231' ))
	if ISNUMERIC(@JameForoosh)=0 
		set @JameForoosh=0
	insert into #tmpChartForoosh
		(fldNameMah,fldShomareMah,fldJameForoosh)
		Values
		((Select * from dbo.translatefunction(@LANGUAGE,13324)),12,@JameForoosh)
		
	select fldNameMah,fldShomareMah,fldJameForoosh from #tmpChartForoosh		
END
GO
/****** Object:  StoredProcedure [dbo].[z105ChartForooshVizitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z105ChartForooshVizitor]
	@year bigint,
	@KolMahiane int,
	@DateAz bigint,
	@DateTa bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @strCodeMoinForoosh nvarchar(20),@JameForoosh bigint
    create table #tmpChartForoosh
		(
			fldCodeVasete bigint,
			fldNameVizitor nvarchar(200) COLLATE Persian_100_CI_AS,			
			fldJameForoosh bigint
		)
	
	if @KolMahiane=0 
		insert into #tmpChartForoosh
			(fldJameForoosh,fldCodeVasete)
		SELECT     SUM(fldMablaghKhales) AS Expr1, fldCodeVasete
			FROM         tblRizeForoosh
			WHERE     (fldCodeVasete <> 0)
			GROUP BY fldCodeVasete
	else
		insert into #tmpChartForoosh
			(fldJameForoosh,fldCodeVasete)
		SELECT     SUM(fldMablaghKhales) AS Expr1, fldCodeVasete
			FROM         tblRizeForoosh
			WHERE     (fldCodeVasete <> 0)  and fldDate between   @DateAz and @DateTa --CONVERT(bigint, Convert(varchar,@year ) + Convert(varchar,@KolMahiane) + '01') And CONVERT(bigint, Convert(varchar,@year ) + Convert(varchar,@KolMahiane) + '31')
			GROUP BY fldCodeVasete
		
	update #tmpChartForoosh set fldNameVizitor=tblTafzili.fldSharheTafzili from tblTafzili where #tmpChartForoosh.fldCodeVasete=tblTafzili.fldCodeTafzili
	
	select fldCodeVasete,fldNameVizitor,fldJameForoosh from #tmpChartForoosh
	--execute CodeMoinHesab 'foroosh',@strCodeMoinForoosh output --'71301'
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0101' ) and Convert(bigint,  Convert(varchar, @year) + '0131' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
		
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('فروردین',1,@JameForoosh)
		
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0201' ) and Convert(bigint,  Convert(varchar, @year) + '0231' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
		
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('اردیبهشت',2,@JameForoosh)
		
		
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0301' ) and Convert(bigint,  Convert(varchar, @year) + '0331' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('خرداد',3,@JameForoosh)
	
	
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0401' ) and Convert(bigint,  Convert(varchar, @year) + '0431' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('تیر',4,@JameForoosh)
	
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0501' ) and Convert(bigint,  Convert(varchar, @year) + '0531' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('مرداد',5,@JameForoosh)
		
	
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0601' ) and Convert(bigint,  Convert(varchar, @year) + '0631' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('شهریور',6,@JameForoosh)
		
		
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0701' ) and Convert(bigint,  Convert(varchar, @year) + '0731' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('مهر',7,@JameForoosh)
		
		
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0801' ) and Convert(bigint,  Convert(varchar, @year) + '0831' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('آبان',8,@JameForoosh)
		
		
		
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '0901' ) and Convert(bigint,  Convert(varchar, @year) + '0931' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('آذر',9,@JameForoosh)
		
		
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '1001' ) and Convert(bigint,  Convert(varchar, @year) + '1031' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('دی',10,@JameForoosh)
		
		
		
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '1101' ) and Convert(bigint,  Convert(varchar, @year) + '1131' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('بهمن',11,@JameForoosh)	
		
		
	--set @JameForoosh=(Select  SUM(fldBestankar-fldBedehkar) As SumForoosh from tblSodooreSanad	Where fldMoinKamel=@strCodeMoinForoosh And flddate between Convert(bigint, Convert(varchar, @year) + '1201' ) and Convert(bigint,  Convert(varchar, @year) + '1231' ))
	--if ISNUMERIC(@JameForoosh)=0 
	--	set @JameForoosh=0
	--insert into #tmpChartForoosh
	--	(fldNameMah,fldShomareMah,fldJameForoosh)
	--	Values
	--	('اسفند',12,@JameForoosh)
		
	--select fldNameMah,fldShomareMah,fldJameForoosh from #tmpChartForoosh		
END

GO
/****** Object:  StoredProcedure [dbo].[z106ChartVarizVizitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z106ChartVarizVizitor]
--چندزبانه
	@year bigint,
	@KolMahiane int,
	@DateAz bigint,
	@DateTa bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @strCodeMoinVariz nvarchar(20),@JameVariz bigint
    create table #tmpChartVariz
		(
			fldCodeVasete bigint,
			fldNameVizitor nvarchar(200) COLLATE Persian_100_CI_AS,			
			fldJameVariz decimal (18, 4)
		)


	declare @KolBank nvarchar(50),@KolSandoogh nvarchar(50)
		
	execute CodeKolHesab 'bank',@KolBank output
	execute CodeKolHesab 'sandoogh',@KolSandoogh output
	
	if @KolMahiane=0 
	begin
		insert into #tmpChartVariz
			(fldJameVariz,fldCodeVasete)
		SELECT     SUM(fldBedehkar-fldBestankar) AS Expr1,fldVasete
			FROM         tblSodooreSanad
			WHERE     (fldVasete <> 0)   and (fldCodekol=@KolBank or fldCodekol=@KolSandoogh)
			GROUP BY fldVasete
			

				insert into #tmpChartVariz
			(fldJameVariz,fldCodeVasete)
		SELECT     SUM(fldMablagh) AS Expr1,  CONVERT(bigint,fldVasete)AS fldCodeVasete
			FROM         tblCheckeDaryafti
			WHERE     (CONVERT(bigint,fldVasete) <> 0 and ISNUMERIC(fldShomareVosool)=1)
			GROUP BY CONVERT(bigint,fldVasete)
end
	else
	begin
		insert into #tmpChartVariz
			(fldJameVariz,fldCodeVasete)
		SELECT     SUM(fldBedehkar-fldBestankar) AS Expr1, fldVasete
			FROM         tblSodooreSanad
			WHERE     (fldVasete <> 0) and (fldCodekol=@KolBank or fldCodekol=@KolSandoogh) and fldDate between   CONVERT(bigint, @DateAz) and CONVERT(bigint,  @DateTa)  --CONVERT(bigint, Convert(varchar,@year ) + Convert(varchar,@KolMahiane) + '01') And CONVERT(bigint, Convert(varchar,@year ) + Convert(varchar,@KolMahiane) + '31')
			GROUP BY fldVasete

			
		insert into #tmpChartVariz
			(fldJameVariz,fldCodeVasete)
		SELECT     SUM(fldMablagh) AS Expr1,  CONVERT(bigint,fldVasete)AS fldCodeVasete
			FROM         tblCheckeDaryafti
			WHERE     (CONVERT(bigint,fldVasete) <> 0 and ISNUMERIC(fldShomareVosool)=1) and fldDateVosool between   @DateAz and @DateTa
			GROUP BY CONVERT(bigint,fldVasete)
	end
	update #tmpChartVariz set fldNameVizitor=tblTafzili.fldSharheTafzili from tblTafzili where #tmpChartVariz.fldCodeVasete=tblTafzili.fldCodeTafzili 
	
	select fldCodeVasete,fldNameVizitor,Sum(fldJameVariz) as fldJameVariz from #tmpChartVariz
		group by fldCodeVasete,fldNameVizitor
	
END

GO
/****** Object:  StoredProcedure [dbo].[z106CountKalaForooshBadAzFee0]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z106CountKalaForooshBadAzFee0]
--چندزبانه
	@CodeKala bigint,
	@CodeTafzili bigint,
	@CountTedad decimal(18,4) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @MaxIDFee0 bigint
	set @MaxIdFee0 = (Select MAX(Id) from tblRizeForoosh Where fldCodeKala=@CodeKala and fldCodeTafsili=@CodeTafzili and fldFeeJoz=0)
	if @MaxIdFee0  is null
		set @MaxIdFee0 =0
    -- Insert statements for procedure here
	set @CountTedad=(Select Sum(fldTedadJoz) as cnt from tblrizeforoosh where fldCodeKala=@codekala and fldCodeTafsili=@CodeTafzili and ID>@MaxIDFee0)
	if @CountTedad is null
		set @CountTedad=0
END

GO
/****** Object:  StoredProcedure [dbo].[z107InsertInfoInTolid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z107InsertInfoInTolid]
--چندزبانه
(
	@Flag nvarchar(15),
	@HazineTabdil decimal(18,4),
	--@dtCheckDaryafti tblCheckDaryafti readonly
	@RizeTolid [tpRizeTolidNewNew-Multi] readonly,
	@NoeKhata nvarchar(20) output
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	--insert into  tblRizeTolid  (fldShomareSanad, fldShomareTolid, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
 --                     fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
 --                     fldDate, fldTime)
	--		Select    fldShomareSanad, fldShomareTolid, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
 --                     fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
 --                     fldDate, fldTime output from @dtCheckDaryafti;
			
		begin transaction transact
		begin try
		
			if @Flag='insert'
				begin
					declare @ShomareTolid2 bigint,@cntTekrari bigint,@ShomareTolidAsli bigint
					set @ShomareTolid2=(Select  top(1) fldShomareTolid from @RizeTolid)
					
					set @cntTekrari=(select COUNT(fldShomaretolid) from tblRizeTolid Where fldShomareTolid=@ShomareTolid2)
					
					--#####################################################
					--اگر شماره تولید تکراری باشد خودش جدید معرفی میکنه
					set @ShomareTolidAsli=(Select Top(1) fldShomareTolid from @RizeTolid )
					if @cntTekrari>0
						set @ShomareTolidAsli=(Select MAX(fldShomareTolid)+1 as Max from tblRizeTolid )
					--#####################################################
					
																			
					Insert into  tblRizeTolid (fldShomareSanad, fldShomareTolid, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
						fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
						fldDate, fldTime,fldDarsdeTakhfif,fldMablagheTakhfif,fldHazineTabdil,fldDateEngheza)
					Select fldShomareSanad, @ShomareTolidAsli, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
							  fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
							  fldDate, fldTime,fldDarsdeTakhfif,fldMablagheTakhfif,@HazineTabdil,fldDateEngheza output from @RizeTolid;	
							  
					set @NoeKhata='insert'
				end
			else
				begin
					declare @ShomareTolid bigint
					set @ShomareTolid=(Select  top(1) fldShomareTolid from @RizeTolid)
						

						
					delete tblRizeTolid Where fldShomareTolid=@ShomareTolid
					
					Insert into  tblRizeTolid (fldShomareSanad, fldShomareTolid, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
						fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
						fldDate, fldTime,fldDarsdeTakhfif,fldMablagheTakhfif,fldHazineTabdil,fldDateEngheza)
					Select fldShomareSanad, @ShomareTolid, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
							  fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
							  fldDate, fldTime,fldDarsdeTakhfif,fldMablagheTakhfif,@HazineTabdil,fldDateEngheza output from @RizeTolid;	
					
					set @NoeKhata='update'
				end
			commit transaction
		end try

		begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='error'

		end catch	

		
    -- Insert statements for procedure here	
END

GO
/****** Object:  StoredProcedure [dbo].[z107MiangineMozoodekala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z107MiangineMozoodekala]
	@fldCodeKala bigint,
	@fldTarikhAz bigint,
	@fldTarikhTa bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


    create table #tmpMiangin
		(
			fldCodeKala bigint,
			fldTedad int,			
			fldMablagh decimal,
			fldTarikh bigint
		)

		insert into #tmpMiangin(fldCodeKala,fldTedad,fldMablagh,fldTarikh)
		select fldCodeKala,fldTedadJoz,fldFeeJoz,fldDate from tblRizeKharid
		where fldCodeKala=@fldCodeKala and fldDate between @fldTarikhAz and @fldTarikhTa

		insert into #tmpMiangin(fldCodeKala,fldTedad,fldMablagh,fldTarikh)
		select fldCodeKala,fldTedadJozNahaei,fldFeeJoz,fldDate from tblRizeTolid
		where fldCodeKala=@fldCodeKala and fldDate between @fldTarikhAz and @fldTarikhTa and fldType='mahsool'

		insert into #tmpMiangin(fldCodeKala,fldTedad,fldMablagh,fldTarikh)
		select fldCodeKala,fldTedadJoz,fldFeeJoz,fldDate from tblRizeBargashtAzForoosh
		where fldCodeKala=@fldCodeKala and fldDate between @fldTarikhAz and @fldTarikhTa

		select ROW_NUMBER()OVER(ORDER BY fldTarikh) as fldRadif,fldCodeKala,fldTedad,fldMablagh,fldTarikh from #tmpMiangin order by fldTarikh
	
	
END

GO
/****** Object:  StoredProcedure [dbo].[z108GardesheKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z108GardesheKala]
--چندزبانه
	@userCode bigint,
	@ListGroupKala varchar(5000)=null
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

        create table #tmpGardesheKala
		(
			CodeKala bigint,
			CodeGroup bigint,
			CodeAnbar bigint,
			NameGroup nvarchar(100) collate Persian_100_CI_AS,
			NameKala nvarchar(100) collate Persian_100_CI_AS,
			NameAnbar nvarchar(100) collate Persian_100_CI_AS,
			TedadDarKarton decimal(18,4),
			MablagheEbteda decimal(18,4),
			TedadeEbteda decimal(18,4),
			TedadKartonEbteda nvarchar(100) collate Persian_100_CI_AS,

			MablagheKharid decimal(18,4),
			TedadeKharid decimal(18,4),
			TedadKartonKharid nvarchar(100) collate Persian_100_CI_AS,
			TedadMoghayerat decimal(18,4),

			MablagheBargashtAzKharid decimal(18,4),
			TedadeBargashtAzKharid decimal(18,4),
			TedadKartonBargashtAzKharid nvarchar(100) collate Persian_100_CI_AS,

			MablagheKhalesKharid decimal(18,4),
			TedadeKhalesKharid decimal(18,4),
			TedadKartonKhalesKharid nvarchar(100) collate Persian_100_CI_AS,

			MablagheForoosh decimal(18,4),
			TedadeForoosh decimal(18,4),
			TedadeKartonForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablagheBargashtAzForoosh decimal(18,4),
			TedadeBargashtAzForoosh decimal(18,4),
			TedadeKartonBargashtAzForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablagheKhalesForoosh decimal(18,4),
			TedadeKhalesForoosh decimal(18,4),
			TedadKartonKhalesForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablaghePayan decimal(18,4),
			TedadePayan decimal(18,4),
			TedadeKartonPayan nvarchar(100) collate Persian_100_CI_AS,
			SoodeKala decimal(18,4)
		)

-- Group Kala access
 Declare @value nvarchar(100)
  set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPKALA')
  if @value = 'True'
  Begin -- Limitation Active  
	-- Get All AccessKala
	declare @accesskala table(code bigint)
	insert into @accesskala 
	exec [ZSP_GetKalaAccesses] @userCode


		insert into #tmpGardesheKala
			(CodeKala,CodeGroup,NameKala,TedadDarKarton)
			Select  fldCodeKala,fldCodeGroup,fldNameKala,fldTedadJoz 
				from  tblKala
				-- add kala access limitation
				where tblKala.fldCodeGroup in (select code from @accesskala)

end
else begin

insert into #tmpGardesheKala
			(CodeKala,CodeGroup,NameKala,TedadDarKarton)
			Select  fldCodeKala,fldCodeGroup,fldNameKala,fldTedadJoz 
				from  tblKala

end


		if @ListGroupKala<>''
		   begin
		     declare @StrDeleteGroupKala varchar(5000)
		       set  @StrDeleteGroupKala='delete from #tmpGardesheKala 
		            where '+ @ListGroupKala + ''
					execute(@StrDeleteGroupKala)
		   end
		--update #tmpGardesheKala set TedadeEbteda=Sum(tblRizeKharid.fldTedadeJoz) from tblRizeKharid where #tmpGardesheKala.CodeKala=tblRizeKharid.fldCodeKala group by tblRizeKharid.fldCodeKala

		update           #tmpGardesheKala
			set NameGroup = tblGroupKala.fldGroupName from tblGroupKala Where CodeGroup=tblGroupKala.fldGroupId

		--####ابتدای دوره####
		update           #tmpGardesheKala
			set tedadeEbteda=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizekharid Where fldType='mojodi_ebteda_doreh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheEbteda=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldFeeKol) as Sum1 from tblRizekharid Where fldType='mojodi_ebteda_doreh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 
			--mojodi_ebteda_doreh



		--کارتن و عدد
		update #tmpGardesheKala Set TedadKartonEbteda=Convert(varchar,floor(tedadeEbteda / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeEbteda, TedadDarKarton) } ) 
			where TedadDarKarton>0
		--######################
		--####تعداد مغایرت####
		update           #tmpGardesheKala
			set TedadMoghayerat=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizekharid Where fldType='Moghayrat' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		update           #tmpGardesheKala
			set TedadMoghayerat-=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeForoosh Where fldType='Moghayrat' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 
		--######خرید#####
		update           #tmpGardesheKala
			set tedadekharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizekharid Where fldType='kharid' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set Mablaghekharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizekharid Where fldType='kharid' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		--کارتن و عدد
		update #tmpGardesheKala Set TedadKartonkharid=Convert(varchar,floor(TedadeKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeKharid, TedadDarKarton) } ) 
			where TedadDarKarton>0
		--#######################

		--######برگشت از خرید#####
		update           #tmpGardesheKala
			set tedadeBargashtAzkharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeBargashtAzkharid Where fldType='bargashtazkharid' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheBargashtAzKharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeBargashtAzkharid Where fldType='bargashtazkharid' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		--کارتن و عدد
		update #tmpGardesheKala Set TedadKartonBargashtAzkharid=Convert(varchar,floor(TedadeBargashtAzKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeBargashtAzKharid, TedadDarKarton) } ) 
			where TedadDarKarton>0
		--#######################


		--######فروش#####
		update           #tmpGardesheKala
			set TedadeForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeForoosh Where fldType='Foroosh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeForoosh Where fldType='Foroosh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		--کارتن و عدد
		update #tmpGardesheKala Set TedadeKartonForoosh=Convert(varchar,floor(TedadeForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeForoosh, TedadDarKarton) } ) 
			where TedadDarKarton>0
		--#######################

		--######برگشت از فروش#####
		update           #tmpGardesheKala
			set TedadeBargashtAzForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeBargashtAzForoosh Where fldType='bargashtazforoosh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheBargashtAzForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeBargashtAzForoosh Where fldType='bargashtazforoosh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		--کارتن و عدد
		update #tmpGardesheKala Set TedadeKartonForoosh=Convert(varchar,floor(TedadeForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeForoosh, TedadDarKarton) } ) 
			where TedadDarKarton>0
		--#######################

		update           #tmpGardesheKala
			set TedadeEbteda=0 Where TedadeEbteda is null  
		update           #tmpGardesheKala
			set TedadeKharid=0 Where TedadeKharid is null
		update           #tmpGardesheKala
			set TedadeBargashtAzKharid=0 Where TedadeBargashtAzKharid is null
		update           #tmpGardesheKala
			set TedadeForoosh=0 Where TedadeForoosh is null
		update           #tmpGardesheKala
			set TedadeBargashtAzForoosh=0 Where TedadeBargashtAzForoosh is null
		update           #tmpGardesheKala
			set MablagheEbteda=0 Where MablagheEbteda is null
		update           #tmpGardesheKala
			set MablagheKharid=0 Where MablagheKharid is null
		update           #tmpGardesheKala
			set MablagheBargashtAzKharid=0 Where MablagheBargashtAzKharid is null
		update           #tmpGardesheKala
			set MablagheForoosh=0 Where MablagheForoosh is null
		update           #tmpGardesheKala
			set MablagheBargashtAzForoosh=0 Where MablagheBargashtAzForoosh is null
		update           #tmpGardesheKala
			set MablaghePayan=0 Where MablaghePayan is null
		update           #tmpGardesheKala
			set TedadMoghayerat=0 Where TedadMoghayerat is null

		--######خالص خرید#######
		update           #tmpGardesheKala
			set TedadeKhalesKharid=TedadeKharid-TedadeBargashtAzKharid
		update #tmpGardesheKala Set TedadKartonKhalesKharid=Convert(varchar,floor(TedadeKhalesKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(TedadeKhalesKharid, TedadDarKarton) } ) 
			where TedadDarKarton>0

		update #tmpGardesheKala Set MablagheKhalesKharid=MablagheKharid-MablagheBargashtAzKharid

		--######خالص فروش#######
		update           #tmpGardesheKala
			set TedadeKhalesForoosh=TedadeForoosh-TedadeBargashtAzForoosh
		update #tmpGardesheKala Set TedadKartonKhalesForoosh=Convert(varchar,floor(TedadeKhalesForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(TedadeKhalesForoosh, TedadDarKarton) } ) 
			where TedadDarKarton>0

		update #tmpGardesheKala Set MablagheKhalesForoosh=MablagheForoosh-MablagheBargashtAzForoosh
		--######پایان دوره#####
		update           #tmpGardesheKala
			set tedadePayan=TedadeEbteda+TedadeKharid+TedadeBargashtAzForoosh +TedadMoghayerat -TedadeForoosh-TedadeBargashtAzKharid
		update #tmpGardesheKala
			set MablaghePayan=(MablagheEbteda+MablagheKharid)/(TedadeEbteda+TedadeKharid)*tedadePayan
			where TedadeEbteda+TedadeKharid>0
		--کارتن و عدد
		update #tmpGardesheKala Set TedadeKartonPayan=Convert(varchar,floor(TedadePayan/ TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadePayan, TedadDarKarton) } ) 
			where TedadDarKarton>0

		--#######################

		--####انتقال بین انبار و تولید کنترل شود#######




		Select ROW_NUMBER() OVER(ORDER BY CodeKala ) AS Row, CodeKala,NameGroup,NameKala,TedadDarKarton,TedadeEbteda,TedadKartonEbteda,MablagheEbteda,
			TedadeKharid,TedadKartonKharid,MablagheKharid,TedadeBargashtAzKharid,TedadKartonBargashtAzKharid,MablagheBargashtAzKharid,TedadeKhalesKharid,TedadKartonKhalesKharid,MablagheKhalesKharid,TedadeForoosh,TedadeKartonForoosh,MablagheForoosh,TedadeBargashtAzForoosh,TedadeKartonBargashtAzForoosh,MablagheBargashtAzForoosh,TedadeKhalesForoosh,TedadKartonKhalesForoosh,MablagheKhalesForoosh,TedadePayan,TedadeKartonPayan,MablaghePayan
		from #tmpGardesheKala
	
END

GO
/****** Object:  StoredProcedure [dbo].[z109InsertInfoInFormooleTolid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z109InsertInfoInFormooleTolid]
(
	@Flag nvarchar(15),
	@NameFormool nvarchar(100),
	@HazineTabdil bigint,
	--@dtCheckDaryafti tblCheckDaryafti readonly
	@RizeFormooleTolid [tpRizeTolid2-Multi] readonly,
	@NoeKhata nvarchar(20) output
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	--insert into  tblRizeTolid  (fldShomareSanad, fldShomareTolid, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
 --                     fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
 --                     fldDate, fldTime)
	--		Select    fldShomareSanad, fldShomareTolid, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
 --                     fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
 --                     fldDate, fldTime output from @dtCheckDaryafti;
			
		begin transaction transact
		begin try
		
			if @Flag='insert'
				begin
					declare @ShomareTolid2 bigint,@cntTekrari bigint,@ShomareTolidAsli bigint
					set @ShomareTolid2=(Select  top(1) fldShomareTolid from @RizeFormooleTolid)
					
					set @cntTekrari=(select COUNT(fldShomaretolid) from tblRizeFormooleTolid Where fldShomareTolid=@ShomareTolid2)
					
					--#####################################################
					--اگر شماره تولید تکراری باشد خودش جدید معرفی میکنه
					set @ShomareTolidAsli=(Select Top(1) fldShomareTolid from @RizeFormooleTolid )
					if @cntTekrari>0
						set @ShomareTolidAsli=(Select MAX(fldShomareTolid)+1 as Max from tblRizeFormooleTolid )
					--#####################################################
					
																			
					Insert into  tblRizeFormooleTolid (fldNameFormool, fldShomareSanad, fldShomareTolid, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
						fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
						fldDate, fldTime,fldDarsdeTakhfif,fldMablagheTakhfif,fldHazineTabdil)
					Select @NameFormool,fldShomareSanad, @ShomareTolidAsli, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
							  fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
							  fldDate, fldTime,fldDarsdeTakhfif,fldMablagheTakhfif,@HazineTabdil output from @RizeFormooleTolid;	
							  
					set @NoeKhata='insert'
				end
			else
				begin
					declare @ShomareTolid bigint
					set @ShomareTolid=(Select  top(1) fldShomareTolid from @RizeFormooleTolid)
						

						
					delete tblRizeFormooleTolid Where fldShomareTolid=@ShomareTolid
					
					Insert into  tblRizeFormooleTolid (fldNameFormool, fldShomareSanad, fldShomareTolid, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
						fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
						fldDate, fldTime,fldDarsdeTakhfif,fldMablagheTakhfif,fldHazineTabdil)
					Select @NameFormool,fldShomareSanad, @ShomareTolid, fldCodeUser, fldRadif, fldCodeAnbar, fldCodeKala, fldSharhKala, fldTedadCartonAvalie, fldTedadJozAvalie, 
							  fldTedadCartonNahaei, fldTedadJozNahaei, fldFeeJoz, fldFeeYekCarton, fldFeeKol, fldFeeVahed1BadAzTakhfif, fldFeeVahed2BadAzTakhfif, fldMablaghKhales, fldType, 
							  fldDate, fldTime,fldDarsdeTakhfif,fldMablagheTakhfif,@HazineTabdil output from @RizeFormooleTolid;	
					
					set @NoeKhata='update'
				end
			commit transaction
		end try

		begin catch
			if @@trancount>0 
				rollback transaction ;
				set @NoeKhata='error'

		end catch	

		
    -- Insert statements for procedure here	
END

GO
/****** Object:  StoredProcedure [dbo].[z109MandeMarakez]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================

CREATE PROCEDURE [dbo].[z109MandeMarakez]
	@CodeMarkaz nvarchar(50)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
		--SELECT        SUM(fldBedehkar - fldBestankar) AS SumMande, fldCodeTafzili
		--		FROM            tblSodooreSanad
		--		WHERE        (ISnumeric(fldCodeMarkaz1) = 1) AND (isnumeric(fldCodeTafzili) = 1) and fldCodeMarkaz1=@CodeMarkaz
		--		GROUP BY fldCodeMarkaz1, fldCodeTafzili
		--		ORDER BY fldCodeTafzili


		SELECT        SUM(tblSodooreSanad.fldBedehkar - tblSodooreSanad.fldBestankar) AS SumMande, tblSodooreSanad.fldCodeTafzili, tblTafzili.fldSharheTafzili
			FROM            tblSodooreSanad INNER JOIN
									 tblTafzili ON tblSodooreSanad.fldCodeTafzili = tblTafzili.fldCodeTafzili
			WHERE        (ISnumeric(tblSodooreSanad.fldCodeMarkaz1) = 1) AND (isnumeric(tblSodooreSanad.fldCodeTafzili) = 1) AND (fldCodeMarkaz1=@CodeMarkaz)
			GROUP BY tblSodooreSanad.fldCodeMarkaz1, tblSodooreSanad.fldCodeTafzili, tblTafzili.fldSharheTafzili



END

GO
/****** Object:  StoredProcedure [dbo].[z109ReportCheckPardakhti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[z109ReportCheckPardakhti]
@Type nvarchar(20),
@fldShomareCheck bigint,
@ListGroupAshkhas varchar(5000)=null,
@ListCodeAshkhas varchar(5000)=null,
@ListCodeBank varchar(5000)=null,
@DateSarResidAz bigint,
@DateSarResidTa bigint,
@DatePardakhtAz bigint,
@DatePardakhtTa bigint,
@DatePassAz bigint,
@DatePassTa bigint,
@JameMablagh bigint output

	
AS
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END
BEGIN
	
SET NOCOUNT ON;


create table #TempReportCheckPardakht(

ID bigint,
fldShomareCheck bigint,
fldShomareHesab bigint,
fldCodeSahebeCheck Nvarchar(10) collate Persian_100_CI_AS,
fldSharheTafzili  Nvarchar(200) collate Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldDateSarResid bigint,
fldDateSarResidShow  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeBank int, 
fldNameBank Nvarchar(100) collate Persian_100_CI_AS, 
fldShobe Nvarchar(40) collate Persian_100_CI_AS,
fldMablagh decimal(18,0),
fldDatePardakht bigint, 
fldDatePardakhtShow Nvarchar(20) collate Persian_100_CI_AS,
fldVaziat  Nvarchar(15) collate Persian_100_CI_AS,
fldShomarePardakht bigint,
fldShomarePass bigint,
fldDatePass bigint,
fldDatePassShow nvarchar(15) collate Persian_100_CI_AS,
fldVaziatShow nvarchar(50)  collate Persian_100_CI_AS,
fldNameGroupAshkhas nvarchar(80) collate Persian_100_CI_AS

)

if @type='all' or  @type='search'
	begin

	Insert into  #TempReportCheckPardakht(ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili
	,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDatePardakht,fldDatePardakhtShow
			,fldShomarePardakht,fldShomarePass,fldDatePass,fldDatePassShow
			 )
		SELECT        dbo.tblCheckePardakhti.ID,dbo.tblCheckePardakhti.fldShomareCheck,dbo.tblCheckePardakhti.fldShomareHesab
		             ,dbo.tblCheckePardakhti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldCodeGroup,tblCheckePardakhti.fldDateSarResid,
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResidShow,
								 dbo.tblCheckePardakhti.fldCodeBank,dbo.tblKolMoin.fldSharheHesab, dbo.tblCheckePardakhti.fldShobe, 
								 dbo.tblCheckePardakhti.fldMablagh,fldDatePardakht, 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),1,4)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),5,2)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),7,2) As fldDatePardakhtShow,
								 dbo.tblCheckePardakhti.fldShomarePardakht,fldShomarePass,fldDatePass,
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 1, 4) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 5, 2) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 7, 2) AS fldDatePassShow

							
							
                         
		FROM            dbo.tblCheckePardakhti INNER JOIN
								 dbo.tblTafzili ON dbo.tblCheckePardakhti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
								 dbo.tblKolMoin ON dbo.tblCheckePardakhti.fldCodeBank =dbo.tblKolMoin.fldCodeHesab									 
					order by tblCheckePardakhti.fldDateSarResid,tblCheckePardakhti.fldDatePardakht
	end
	if @type='Pass'
	begin
		Insert into  #TempReportCheckPardakht(ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili
	,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDatePardakht,fldDatePardakhtShow
			,fldShomarePardakht,fldShomarePass,fldDatePass,fldDatePassShow
			 )
		SELECT        dbo.tblCheckePardakhti.ID,dbo.tblCheckePardakhti.fldShomareCheck,dbo.tblCheckePardakhti.fldShomareHesab
		             ,dbo.tblCheckePardakhti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldCodeGroup,tblCheckePardakhti.fldDateSarResid,
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResidShow,
								 dbo.tblCheckePardakhti.fldCodeBank,dbo.tblKolMoin.fldSharheHesab, dbo.tblCheckePardakhti.fldShobe, 
								 dbo.tblCheckePardakhti.fldMablagh,fldDatePardakht, 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),1,4)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),5,2)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),7,2) As fldDatePardakhtShow,
								 dbo.tblCheckePardakhti.fldShomarePardakht,fldShomarePass,fldDatePass,
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 1, 4) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 5, 2) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 7, 2) AS fldDatePassShow

							
							
                         
		FROM            dbo.tblCheckePardakhti INNER JOIN
								 dbo.tblTafzili ON dbo.tblCheckePardakhti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
								 dbo.tblKolMoin ON dbo.tblCheckePardakhti.fldCodeBank =dbo.tblKolMoin.fldCodeHesab				
								 where ISNUMERIC(fldShomarePass)=1					 
					order by tblCheckePardakhti.fldDateSarResid,tblCheckePardakhti.fldDatePardakht
	end
if @type='PassNashode'
begin
Insert into  #TempReportCheckPardakht(ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili
	,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDatePardakht,fldDatePardakhtShow
			,fldShomarePardakht,fldShomarePass,fldDatePass,fldDatePassShow
			 )
		SELECT        dbo.tblCheckePardakhti.ID,dbo.tblCheckePardakhti.fldShomareCheck,dbo.tblCheckePardakhti.fldShomareHesab
		             ,dbo.tblCheckePardakhti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldCodeGroup,tblCheckePardakhti.fldDateSarResid,
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResidShow,
								 dbo.tblCheckePardakhti.fldCodeBank,dbo.tblKolMoin.fldSharheHesab, dbo.tblCheckePardakhti.fldShobe, 
								 dbo.tblCheckePardakhti.fldMablagh,fldDatePardakht, 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),1,4)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),5,2)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),7,2) As fldDatePardakhtShow,
								 dbo.tblCheckePardakhti.fldShomarePardakht,fldShomarePass,fldDatePass,
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 1, 4) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 5, 2) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 7, 2) AS fldDatePassShow

							
							
                         
		FROM            dbo.tblCheckePardakhti INNER JOIN
								 dbo.tblTafzili ON dbo.tblCheckePardakhti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
								 dbo.tblKolMoin ON dbo.tblCheckePardakhti.fldCodeBank =dbo.tblKolMoin.fldCodeHesab				
								 where  ISNUMERIC(fldShomarePass)<>1					 
					order by tblCheckePardakhti.fldDateSarResid,tblCheckePardakhti.fldDatePardakht
	--delete #TempReportCheckPardakht where fldShomarePass=2
	end


--else if @type='vosoolnashode'
--	SELECT        dbo.tblCheckeDaryafti.ID,dbo.tblCheckeDaryafti.fldShomareCheck,dbo.tblCheckeDaryafti.fldShomareHesab,dbo.tblCheckeDaryafti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldDateSarResid,
--								SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid,
--								 dbo.tblCheckeDaryafti.fldCodeBank,dbo.tblBankeMoshtari.fldNameBank, dbo.tblCheckeDaryafti.fldShobe, 
--								 dbo.tblCheckeDaryafti.fldMablagh, SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),1,4)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),5,2)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),7,2) As fldDateDaryaft ,dbo.tblCheckeDaryafti.fldShomareDaryaft,dbo.tblCheckeDaryafti.fldVaziat, dbo.tblCheckeDaryafti.fldShomarePardakht
                         
--		FROM            dbo.tblCheckeDaryafti INNER JOIN
--								 dbo.tblTafzili ON dbo.tblCheckeDaryafti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
--								 dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID
--		WHERE        (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat IS NULL) OR
--								 (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat = 'vagozari')
--					order by tblCheckeDaryafti.fldDateSarResid,tblCheckeDaryafti.fldDateDaryaft

--if @type<>'search' 
--	if @ListGroupAshkhas<>''
--		begin
--		declare @StrDeleteGroupAshkhas varchar(5000)
--		set  @StrDeleteGroupAshkhas='delete from #TempReportCheck 
--			where '+ @ListGroupAshkhas + ''
--			execute(@StrDeleteGroupAshkhas)
--		end
	

if @ListGroupAshkhas <>''
begin
	declare @StrDeleteGroupAshkhas varchar(5000)
	set  @StrDeleteGroupAshkhas='delete from #TempReportCheckPardakht 
		where fldCodeGroupAshkhas NOT IN ('+ @ListGroupAshkhas +')'
		execute(@StrDeleteGroupAshkhas)
end
if @ListCodeAshkhas <>''
begin
	declare @StrDeleteAshkhas varchar(5000)
	set  @StrDeleteAshkhas='delete from #TempReportCheckPardakht 
		where fldCodeSahebeCheck NOT IN ('+ @ListCodeAshkhas +')'
		execute(@StrDeleteAshkhas)
end

if @ListCodeBank <>''
begin

--delete from #TempReportCheckPardakht where fldCodeBank NOT IN @ListCodeBank

	declare @StrDeleteBank varchar(5000)
	set  @StrDeleteBank='delete from #TempReportCheckPardakht 
		where fldCodeBank NOT IN ('+ @ListCodeBank +')'
		execute(@StrDeleteBank)
end
--if @ListCodeVisitor<>''
--	begin
--	if @ListCodeVisitor='-1' --ليست فاکتورهاي بدون ويزيتور
--		begin
--			delete from #TempReportCheck where fldCodeVasete<>''
--		end
--	else
--		begin
--			declare @StrDeleteVisitor varchar(5000)
--			set  @StrDeleteVisitor='delete from #TempReportCheck 
--				where '+ @ListCodeVisitor + ''
--				execute(@StrDeleteVisitor)
--		end
--	end

if @Type<>'search'
	begin
		delete #TempReportCheckPardakht Where fldDateSarResid< @DateSarResidAz
		delete #TempReportCheckPardakht Where fldDateSarResid> @DateSarResidTa
		delete #TempReportCheckPardakht Where fldDatePardakht< @DatePardakhtAz
		delete #TempReportCheckPardakht Where fldDatePardakht> @DatePardakhtTa
		delete #TempReportCheckPardakht Where fldDatePardakht< @DatePassAz
		delete #TempReportCheckPardakht Where fldDatePardakht> @DatePassTa
	end
else if @Type='search'
--begin
--	declare @StrSearch varchar(5000)
--	set  @StrSearch='delete from #TempReportCheckPardakht 
--		where fldShomareCheck <> '+ @ListCodeAshkhas 
--		execute(@StrSearch)
--end
	begin
		--execute('delete #TempReportCheckPardakht Where fldShomareCheck = ' + @fldShomareCheck )


		delete from #TempReportCheckPardakht Where fldShomareCheck <> @fldShomareCheck



	--	execute('delete #TempReportCheckPardakht Where fldShomareCheck not like %' + @ListGroupAshkhas +'%')
	end

--update #TempReportCheckPardakht set fldNameVasete=tblTafzili.fldSharheTafzili from tblTafzili Where #TempReportCheck.fldCodeVasete=tblTafzili.fldCodeTafzili
--update #TempReportCheckPardakht set fldNameBankVagozari =tblKolMoin.fldSharheHesab from tblKolMoin Where tblKolMoin.fldCodeHesab =fldCodeBankVagozari
--update #TempReportCheckPardakht set fldVaziatShow='واگذاري به ' + fldNameBankVagozari where fldVaziat='vagozari'
--update #TempReportCheckPardakht set fldVaziatShow='وصول چک به شماره وصول ' + Convert(varchar,fldShomareVosool) where fldVaziat='vosool'
--update #TempReportCheckPardakht set fldVaziat='foroosh', fldVaziatShow='واگذاري چک به ش پرداخت' + Convert(varchar,fldShomarePardakht) + ' به ' + tbl1.NameShakhs from 
--		(SELECT        tblTafzili.fldSharheTafzili As NameShakhs, tblPardakht.fldShomarePardakht As ShomarePardakht FROM tblTafzili INNER JOIN tblPardakht ON tblTafzili.fldCodeTafzili = tblPardakht.fldCodeTafzili) As Tbl1
--		Where tbl1.ShomarePardakht=fldShomarePardakht

--update TempReportCheckPardakht set fldVaziat ='' where fldVaziat is null
--if @type='vosoolnashode'
--	delete #TempReportCheckPardakht where fldVaziat<>'vosoolnashode'
--if @type='vosool'
--	begin
--		delete #TempReportCheckPardakht where fldVaziat<>'vosool'
--		--update #TempReportCheckPardakht set fldNameHesabeVosool=tblKolMoin.fldSharheHesab from tblKolMoin 
--			--Where #TempReportCheckPardakht.fldCodeHesabeVosool=tblKolMoin.fldCodeHesab
--	end



update #TempReportCheckPardakht set fldNameGroupAshkhas=tblGroupAshkhas.fldGroupName from tblGroupAshkhas Where tblGroupAshkhas.fldGroupId=#TempReportCheckPardakht.fldCodeGroupAshkhas

Set @JameMablagh=(Select Sum(fldMablagh) from #TempReportCheckPardakht)
if @JameMablagh is null
	set @JameMablagh=0

Select ROW_NUMBER() OVER(ORDER BY fldDatePardakht,ID ) AS [Row],ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili
	,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDatePardakht,fldDatePardakhtShow
			,fldShomarePardakht,fldShomarePass,fldDatePass,fldDatePassShow
		 from  #TempReportCheckPardakht

END

GO
/****** Object:  StoredProcedure [dbo].[z109ReportCheckPardakhti_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z109ReportCheckPardakhti_WithUserCode]
--چندزبانه
@Type nvarchar(20),
@fldShomareCheck bigint,
@ListGroupAshkhas varchar(5000)=null,
@ListCodeAshkhas varchar(5000)=null,
@ListCodeBank varchar(5000)=null,
@DateSarResidAz bigint,
@DateSarResidTa bigint,
@DatePardakhtAz bigint,
@DatePardakhtTa bigint,
@DatePassAz bigint,
@DatePassTa bigint,
@userCode bigint,
@JameMablagh decimal(18,4) output

	
AS
IF 1 = 0 BEGIN
    SET FMTONLY OFF
END
BEGIN
	
SET NOCOUNT ON;


create table #TempReportCheckPardakht(

ID bigint,
fldShomareCheck bigint,
fldShomareHesab bigint,
fldCodeSahebeCheck Nvarchar(10) collate Persian_100_CI_AS,
fldSharheTafzili  Nvarchar(200) collate Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldDateSarResid bigint,
fldDateSarResidShow  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeBank int, 
fldNameBank Nvarchar(100) collate Persian_100_CI_AS, 
fldShobe Nvarchar(40) collate Persian_100_CI_AS,
fldMablagh decimal(18,4),
fldDatePardakht bigint, 
fldDatePardakhtShow Nvarchar(20) collate Persian_100_CI_AS,
fldVaziat  Nvarchar(15) collate Persian_100_CI_AS,
fldShomarePardakht bigint,
fldShomarePass bigint,
fldDatePass bigint,
fldDatePassShow nvarchar(15) collate Persian_100_CI_AS,
fldVaziatShow nvarchar(50)  collate Persian_100_CI_AS,
fldNameGroupAshkhas nvarchar(80) collate Persian_100_CI_AS

)

if @type='all' or  @type='search'
	begin

	Insert into  #TempReportCheckPardakht(ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili
	,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDatePardakht,fldDatePardakhtShow
			,fldShomarePardakht,fldShomarePass,fldDatePass,fldDatePassShow
			 )
		SELECT        dbo.tblCheckePardakhti.ID,dbo.tblCheckePardakhti.fldShomareCheck,dbo.tblCheckePardakhti.fldShomareHesab
		             ,dbo.tblCheckePardakhti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldCodeGroup,tblCheckePardakhti.fldDateSarResid,
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResidShow,
								 dbo.tblCheckePardakhti.fldCodeBank,dbo.tblKolMoin.fldSharheHesab, dbo.tblCheckePardakhti.fldShobe, 
								 dbo.tblCheckePardakhti.fldMablagh,fldDatePardakht, 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),1,4)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),5,2)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),7,2) As fldDatePardakhtShow,
								 dbo.tblCheckePardakhti.fldShomarePardakht,fldShomarePass,fldDatePass,
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 1, 4) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 5, 2) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 7, 2) AS fldDatePassShow

							
							
                         
		FROM            dbo.tblCheckePardakhti INNER JOIN
								 dbo.tblTafzili ON dbo.tblCheckePardakhti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
								 dbo.tblKolMoin ON dbo.tblCheckePardakhti.fldCodeBank =dbo.tblKolMoin.fldCodeHesab									 
					order by tblCheckePardakhti.fldDateSarResid,tblCheckePardakhti.fldDatePardakht
	end
	if @type='Pass'
	begin
		Insert into  #TempReportCheckPardakht(ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili
	,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDatePardakht,fldDatePardakhtShow
			,fldShomarePardakht,fldShomarePass,fldDatePass,fldDatePassShow
			 )
		SELECT        dbo.tblCheckePardakhti.ID,dbo.tblCheckePardakhti.fldShomareCheck,dbo.tblCheckePardakhti.fldShomareHesab
		             ,dbo.tblCheckePardakhti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldCodeGroup,tblCheckePardakhti.fldDateSarResid,
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResidShow,
								 dbo.tblCheckePardakhti.fldCodeBank,dbo.tblKolMoin.fldSharheHesab, dbo.tblCheckePardakhti.fldShobe, 
								 dbo.tblCheckePardakhti.fldMablagh,fldDatePardakht, 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),1,4)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),5,2)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),7,2) As fldDatePardakhtShow,
								 dbo.tblCheckePardakhti.fldShomarePardakht,fldShomarePass,fldDatePass,
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 1, 4) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 5, 2) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 7, 2) AS fldDatePassShow

							
							
                         
		FROM            dbo.tblCheckePardakhti INNER JOIN
								 dbo.tblTafzili ON dbo.tblCheckePardakhti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
								 dbo.tblKolMoin ON dbo.tblCheckePardakhti.fldCodeBank =dbo.tblKolMoin.fldCodeHesab				
								 where ISNUMERIC(fldShomarePass)=1					 
					order by tblCheckePardakhti.fldDateSarResid,tblCheckePardakhti.fldDatePardakht
	end
if @type='PassNashode'
begin

update tblCheckePardakhti set fldBatel=0 Where  fldBatel is null 

Insert into  #TempReportCheckPardakht(ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili
	,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDatePardakht,fldDatePardakhtShow
			,fldShomarePardakht,fldShomarePass,fldDatePass,fldDatePassShow
			 )
		SELECT        dbo.tblCheckePardakhti.ID,dbo.tblCheckePardakhti.fldShomareCheck,dbo.tblCheckePardakhti.fldShomareHesab
		             ,dbo.tblCheckePardakhti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldCodeGroup,tblCheckePardakhti.fldDateSarResid,
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResidShow,
								 dbo.tblCheckePardakhti.fldCodeBank,dbo.tblKolMoin.fldSharheHesab, dbo.tblCheckePardakhti.fldShobe, 
								 dbo.tblCheckePardakhti.fldMablagh,fldDatePardakht, 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),1,4)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),5,2)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),7,2) As fldDatePardakhtShow,
								 dbo.tblCheckePardakhti.fldShomarePardakht,fldShomarePass,fldDatePass,
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 1, 4) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 5, 2) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 7, 2) AS fldDatePassShow           
		FROM            dbo.tblCheckePardakhti INNER JOIN
								 dbo.tblTafzili ON dbo.tblCheckePardakhti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
								 dbo.tblKolMoin ON dbo.tblCheckePardakhti.fldCodeBank =dbo.tblKolMoin.fldCodeHesab				
								 where  ISNUMERIC(fldShomarePass)<>1 and isnumeric(fldShomareBazPas)=0	 and fldBatel=0			 
					order by tblCheckePardakhti.fldDateSarResid,tblCheckePardakhti.fldDatePardakht
	--delete #TempReportCheckPardakht where fldShomarePass=2
	end


if @type='batel'
begin
update tblCheckePardakhti set fldBatel=0 Where  fldBatel is null 

Insert into  #TempReportCheckPardakht(ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili
	,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDatePardakht,fldDatePardakhtShow
			,fldShomarePardakht,fldShomarePass,fldDatePass,fldDatePassShow
			 )
		SELECT        dbo.tblCheckePardakhti.ID,dbo.tblCheckePardakhti.fldShomareCheck,dbo.tblCheckePardakhti.fldShomareHesab
		             ,dbo.tblCheckePardakhti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldCodeGroup,tblCheckePardakhti.fldDateSarResid,
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResidShow,
								 dbo.tblCheckePardakhti.fldCodeBank,dbo.tblKolMoin.fldSharheHesab, dbo.tblCheckePardakhti.fldShobe, 
								 dbo.tblCheckePardakhti.fldMablagh,fldDatePardakht, 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),1,4)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),5,2)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),7,2) As fldDatePardakhtShow,
								 dbo.tblCheckePardakhti.fldShomarePardakht,fldShomarePass,fldDatePass,
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 1, 4) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 5, 2) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 7, 2) AS fldDatePassShow           
		FROM            dbo.tblCheckePardakhti INNER JOIN
								 dbo.tblTafzili ON dbo.tblCheckePardakhti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
								 dbo.tblKolMoin ON dbo.tblCheckePardakhti.fldCodeBank =dbo.tblKolMoin.fldCodeHesab				
								 where   fldBatel=1 
					order by tblCheckePardakhti.fldDateSarResid,tblCheckePardakhti.fldDatePardakht
	--delete #TempReportCheckPardakht where fldShomarePass=2
	end

if @type='BazPasShode'
begin
Insert into  #TempReportCheckPardakht(ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili
	,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDatePardakht,fldDatePardakhtShow
			,fldShomarePardakht,fldShomarePass,fldDatePass,fldDatePassShow
			 )
		SELECT        dbo.tblCheckePardakhti.ID,dbo.tblCheckePardakhti.fldShomareCheck,dbo.tblCheckePardakhti.fldShomareHesab
		             ,dbo.tblCheckePardakhti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldCodeGroup,tblCheckePardakhti.fldDateSarResid,
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResidShow,
								 dbo.tblCheckePardakhti.fldCodeBank,dbo.tblKolMoin.fldSharheHesab, dbo.tblCheckePardakhti.fldShobe, 
								 dbo.tblCheckePardakhti.fldMablagh,fldDatePardakht, 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),1,4)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),5,2)+ '/' + 
								 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckePardakhti.fldDatePardakht),7,2) As fldDatePardakhtShow,
								 dbo.tblCheckePardakhti.fldShomarePardakht,fldShomarePass,fldDatePass,
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 1, 4) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 5, 2) + '/' + 
								 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDatePass), 7, 2) AS fldDatePassShow

							
							
                         
		FROM            dbo.tblCheckePardakhti INNER JOIN
								 dbo.tblTafzili ON dbo.tblCheckePardakhti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
								 dbo.tblKolMoin ON dbo.tblCheckePardakhti.fldCodeBank =dbo.tblKolMoin.fldCodeHesab				
								 where  ISNUMERIC(fldShomareBazPas)=1					 
					order by tblCheckePardakhti.fldDateSarResid,tblCheckePardakhti.fldDatePardakht
	--delete #TempReportCheckPardakht where fldShomarePass=2
	end
--else if @type='vosoolnashode'
--	SELECT        dbo.tblCheckeDaryafti.ID,dbo.tblCheckeDaryafti.fldShomareCheck,dbo.tblCheckeDaryafti.fldShomareHesab,dbo.tblCheckeDaryafti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldDateSarResid,
--								SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid,
--								 dbo.tblCheckeDaryafti.fldCodeBank,dbo.tblBankeMoshtari.fldNameBank, dbo.tblCheckeDaryafti.fldShobe, 
--								 dbo.tblCheckeDaryafti.fldMablagh, SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),1,4)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),5,2)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),7,2) As fldDateDaryaft ,dbo.tblCheckeDaryafti.fldShomareDaryaft,dbo.tblCheckeDaryafti.fldVaziat, dbo.tblCheckeDaryafti.fldShomarePardakht
                         
--		FROM            dbo.tblCheckeDaryafti INNER JOIN
--								 dbo.tblTafzili ON dbo.tblCheckeDaryafti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
--								 dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID
--		WHERE        (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat IS NULL) OR
--								 (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat = 'vagozari')
--					order by tblCheckeDaryafti.fldDateSarResid,tblCheckeDaryafti.fldDateDaryaft

--if @type<>'search' 
--	if @ListGroupAshkhas<>''
--		begin
--		declare @StrDeleteGroupAshkhas varchar(5000)
--		set  @StrDeleteGroupAshkhas='delete from #TempReportCheck 
--			where '+ @ListGroupAshkhas + ''
--			execute(@StrDeleteGroupAshkhas)
--		end
	

if @ListGroupAshkhas <>''
begin
	declare @StrDeleteGroupAshkhas varchar(5000)
	set  @StrDeleteGroupAshkhas='delete from #TempReportCheckPardakht 
		where fldCodeGroupAshkhas NOT IN ('+ @ListGroupAshkhas +')'
		execute(@StrDeleteGroupAshkhas)
end
if @ListCodeAshkhas <>''
begin
	declare @StrDeleteAshkhas varchar(5000)
	set  @StrDeleteAshkhas='delete from #TempReportCheckPardakht 
		where fldCodeSahebeCheck NOT IN ('+ @ListCodeAshkhas +')'
		execute(@StrDeleteAshkhas)
end

if @ListCodeBank <>''
begin

--delete from #TempReportCheckPardakht where fldCodeBank NOT IN @ListCodeBank

	declare @StrDeleteBank varchar(5000)
	set  @StrDeleteBank='delete from #TempReportCheckPardakht 
		where fldCodeBank NOT IN ('+ @ListCodeBank +')'
		execute(@StrDeleteBank)
end
--if @ListCodeVisitor<>''
--	begin
--	if @ListCodeVisitor='-1' --ليست فاکتورهاي بدون ويزيتور
--		begin
--			delete from #TempReportCheck where fldCodeVasete<>''
--		end
--	else
--		begin
--			declare @StrDeleteVisitor varchar(5000)
--			set  @StrDeleteVisitor='delete from #TempReportCheck 
--				where '+ @ListCodeVisitor + ''
--				execute(@StrDeleteVisitor)
--		end
--	end

if @Type<>'search'
	begin
		delete #TempReportCheckPardakht Where fldDateSarResid< @DateSarResidAz
		delete #TempReportCheckPardakht Where fldDateSarResid> @DateSarResidTa
		delete #TempReportCheckPardakht Where fldDatePardakht< @DatePardakhtAz
		delete #TempReportCheckPardakht Where fldDatePardakht> @DatePardakhtTa
		delete #TempReportCheckPardakht Where fldDatePardakht< @DatePassAz
		delete #TempReportCheckPardakht Where fldDatePardakht> @DatePassTa
	end
else if @Type='search'
--begin
--	declare @StrSearch varchar(5000)
--	set  @StrSearch='delete from #TempReportCheckPardakht 
--		where fldShomareCheck <> '+ @ListCodeAshkhas 
--		execute(@StrSearch)
--end
	begin
		--execute('delete #TempReportCheckPardakht Where fldShomareCheck = ' + @fldShomareCheck )


		delete from #TempReportCheckPardakht Where fldShomareCheck <> @fldShomareCheck



	--	execute('delete #TempReportCheckPardakht Where fldShomareCheck not like %' + @ListGroupAshkhas +'%')
	end

--update #TempReportCheckPardakht set fldNameVasete=tblTafzili.fldSharheTafzili from tblTafzili Where #TempReportCheck.fldCodeVasete=tblTafzili.fldCodeTafzili
--update #TempReportCheckPardakht set fldNameBankVagozari =tblKolMoin.fldSharheHesab from tblKolMoin Where tblKolMoin.fldCodeHesab =fldCodeBankVagozari
--update #TempReportCheckPardakht set fldVaziatShow='واگذاري به ' + fldNameBankVagozari where fldVaziat='vagozari'
--update #TempReportCheckPardakht set fldVaziatShow='وصول چک به شماره وصول ' + Convert(varchar,fldShomareVosool) where fldVaziat='vosool'
--update #TempReportCheckPardakht set fldVaziat='foroosh', fldVaziatShow='واگذاري چک به ش پرداخت' + Convert(varchar,fldShomarePardakht) + ' به ' + tbl1.NameShakhs from 
--		(SELECT        tblTafzili.fldSharheTafzili As NameShakhs, tblPardakht.fldShomarePardakht As ShomarePardakht FROM tblTafzili INNER JOIN tblPardakht ON tblTafzili.fldCodeTafzili = tblPardakht.fldCodeTafzili) As Tbl1
--		Where tbl1.ShomarePardakht=fldShomarePardakht

--update TempReportCheckPardakht set fldVaziat ='' where fldVaziat is null
--if @type='vosoolnashode'
--	delete #TempReportCheckPardakht where fldVaziat<>'vosoolnashode'
--if @type='vosool'
--	begin
--		delete #TempReportCheckPardakht where fldVaziat<>'vosool'
--		--update #TempReportCheckPardakht set fldNameHesabeVosool=tblKolMoin.fldSharheHesab from tblKolMoin 
--			--Where #TempReportCheckPardakht.fldCodeHesabeVosool=tblKolMoin.fldCodeHesab
--	end



update #TempReportCheckPardakht set fldNameGroupAshkhas=tblGroupAshkhas.fldGroupName from tblGroupAshkhas Where tblGroupAshkhas.fldGroupId=#TempReportCheckPardakht.fldCodeGroupAshkhas

Set @JameMablagh=(Select Sum(fldMablagh) from #TempReportCheckPardakht)
if @JameMablagh is null
	set @JameMablagh=0

-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################

Select ROW_NUMBER() OVER(ORDER BY fldDatePardakht,ID ) AS [Row],ID,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldSharheTafzili
	,fldCodeGroupAshkhas,fldDateSarResid,fldDateSarResidShow,fldCodeBank,fldNameBank, 
			fldShobe,fldMablagh,fldDatePardakht,fldDatePardakhtShow
			,fldShomarePardakht,fldShomarePass,fldDatePass,fldDatePassShow
		 from  #TempReportCheckPardakht
		 where 
		 -- ##################### Group Ashkhas Limitation
		fldCodeSahebeCheck in (select code from @accessCodes)

END
GO
/****** Object:  StoredProcedure [dbo].[z110ShowInfoChecksBargashtShodeInFormBargasht]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z110ShowInfoChecksBargashtShodeInFormBargasht]
--چندزبانه
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

select dbo.tblCheckeDaryafti.fldShomareCheck,dbo.tblCheckeDaryafti.fldShomareHesab,dbo.tblCheckeDaryafti.fldCodeSahebeCheck,
dbo.tblTafzili.fldSharheTafzili,
 SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' 
 + SUBSTRING(CONVERT(nvarchar, 
 dbo.tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' 
 + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid,dbo.tblCheckeDaryafti.fldCodeBank,
 dbo.tblBankeMoshtari.fldNameBank, dbo.tblCheckeDaryafti.fldShobe, dbo.tblCheckeDaryafti.fldMablagh, 
   SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft), 1, 4) + '/'
 + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft), 5, 2) 
 + '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft), 7, 2) AS fldDateDaryaft,
  dbo.tblCheckeDaryafti.fldShomareDaryaft, 
   SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateBargasht), 1, 4) + '/' 
 + SUBSTRING(CONVERT(nvarchar, 
  dbo.tblCheckeDaryafti.fldDateBargasht), 5, 2) + '/' 
 + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateBargasht), 7, 2) 
  AS fldDateBargasht,dbo.tblCheckeDaryafti.fldSharhBargasht,dbo.tblCheckeDaryafti.fldVaziat,
   dbo.tblCheckeDaryafti.ID,dbo.tblCheckeDaryafti.fldShomarePardakht

   from dbo.tblCheckeDaryafti INNER JOIN
    dbo.tblTafzili ON dbo.tblCheckeDaryafti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
    dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID 
	WHERE  (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat = 'Bargasht')


END

GO
/****** Object:  StoredProcedure [dbo].[z111ShowCheckDaryaftiAndVosoolNashodeINfrmBargasht]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z111ShowCheckDaryaftiAndVosoolNashodeINfrmBargasht]
--چندزبانه
AS
BEGIN
	
SET NOCOUNT ON;


SELECT        dbo.tblCheckeDaryafti.ID,dbo.tblCheckeDaryafti.fldShomareCheck,dbo.tblCheckeDaryafti.fldShomareHesab,dbo.tblCheckeDaryafti.fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,
                        SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid,
						 dbo.tblCheckeDaryafti.fldCodeBank,dbo.tblBankeMoshtari.fldNameBank, dbo.tblCheckeDaryafti.fldShobe, 
                         dbo.tblCheckeDaryafti.fldMablagh, SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),1,4)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),5,2)+ '/' + SUBSTRING(CONVERT(nvarchar, dbo.tblCheckeDaryafti.fldDateDaryaft),7,2) As fldDateDaryaft ,dbo.tblCheckeDaryafti.fldShomareDaryaft,dbo.tblCheckeDaryafti.fldVaziat, dbo.tblCheckeDaryafti.fldShomarePardakht
                         
FROM            dbo.tblCheckeDaryafti INNER JOIN
                         dbo.tblTafzili ON dbo.tblCheckeDaryafti.fldCodeSahebeCheck = dbo.tblTafzili.fldCodeTafzili INNER JOIN
                         dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID
WHERE        (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat IS NULL) OR
                         (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat is NULL)OR
						 (dbo.tblCheckeDaryafti.fldShomarePardakht IS NULL) AND (dbo.tblCheckeDaryafti.fldVaziat='Vagozari')
						 order by tblCheckeDaryafti.fldDateSarResid

						 
END

GO
/****** Object:  StoredProcedure [dbo].[z112InsertInfoBargashtCheck]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z112InsertInfoBargashtCheck] 
--چندزبانه
@fldIdCheck bigint,
@fldDateBargasht  int,
@fldSharhBargasht  nvarchar(50),
@Result nvarchar(50) output
    
AS
BEGIN
	
	SET NOCOUNT ON;

   UPDATE [dbo].[tblCheckeDaryafti]
   SET 
		fldDateBargasht=@fldDateBargasht, 
		fldSharhBargasht=@fldSharhBargasht,
		fldVaziat ='Bargasht'       
	WHERE ID=@fldIdCheck 
    set @Result='UpdateOk'
	
END

GO
/****** Object:  StoredProcedure [dbo].[z113ReportPorsantTabaghati]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z113ReportPorsantTabaghati]
--چندزبانه
	@fldCodeVisitor bigint,
	@fldTarikhAz bigint,
	@fldTarikhTa bigint,
	@Type nvarchar(50)
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


    create table #tmpPorsant
		(
			fldCodeVisitor bigint,						
			fldMablaghSumForoosh decimal(18,4),
			fldMablaghSumBargashtAzForoosh decimal(18,4)
		)
if @Type='Foroosh'
		begin
		insert into #tmpPorsant(fldCodeVisitor,fldMablaghSumForoosh,fldMablaghSumBargashtAzForoosh)
		select fldCodeVasete,(fldMablaghKhales) as fldMablaghSumForoosh,(0) as fldMablaghSumBargashtAzForoosh
		from tblRizeForoosh
		where fldCodeVasete=@fldCodeVisitor and fldDate between @fldTarikhAz and @fldTarikhTa
		end
if @Type='BargashtAzForoosh'
		begin
	insert into #tmpPorsant(fldCodeVisitor,fldMablaghSumForoosh,fldMablaghSumBargashtAzForoosh)
		select fldCodeVasete,(0) as fldMablaghSumForoosh,(fldMablaghKhales) as fldMablaghSumBargashtAzForoosh
		from tblRizeBargashtAzForoosh
		where fldCodeVasete=@fldCodeVisitor and fldDate between @fldTarikhAz and @fldTarikhTa
		end


		select ROW_NUMBER()OVER(ORDER BY fldCodeVisitor) as fldRadif,fldCodeVisitor,fldMablaghSumForoosh,fldMablaghSumBargashtAzForoosh from #tmpPorsant 
	
	
END

GO
/****** Object:  StoredProcedure [dbo].[z114InsertTarheTakhfifHajmi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[z114InsertTarheTakhfifHajmi]

@ID bigint,
@fldCodeKala bigint,
@fldRadif bigint,
@fldTedad int,
@fldDarsad int

as 

begin

	insert into tblTarheTakhfifHajmi 
		values(@ID,@fldCodeKala,@fldRadif,@fldTedad,@fldDarsad)

end

GO
/****** Object:  StoredProcedure [dbo].[z115UpdateTblTarheTakhfifHajmi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z115UpdateTblTarheTakhfifHajmi]

@fldCodeKala bigint,
@fldRadif bigint,
@fldTedad int,
@fldDarsad int,
@flagOut int output
as

begin
	Update tblTarheTakhfifHajmi Set fldTedad =@fldTedad,fldDarsad=@fldDarsad Where fldRadif=@fldRadif and fldCodeKala=@fldCodeKala
	set @flagOut=1
end

GO
/****** Object:  StoredProcedure [dbo].[z116TekrariTedadTakhfifKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[z116TekrariTedadTakhfifKala]

@TedadTakhfif bigint,
@fldCodeKala bigint,
@fldRadif bigint,
@IDType int,
@flagOUt int output
as
begin
	declare @RowCountTakhfif int
	if @IDType=0  --براي مقايسه براي حالت اضافه کردن رکورد
		begin
		 set @RowCountTakhfif=(Select Count(fldTedad) from tblTarheTakhfifHajmi  Where fldTedad=@TedadTakhfif and fldCodeKala=@fldCodeKala and  fldRadif<>@fldRadif)
		 set @flagOUt=@RowCountTakhfif
		end
	else
		begin --براي مقايسه با غيرخودش در حالت ويرايش
		   set	@RowCountTakhfif=(Select  Count(fldTedad) from tblTarheTakhfifHajmi  Where fldTedad=@TedadTakhfif and fldCodeKala=@fldCodeKala and fldTedad=@TedadTakhfif and  fldRadif<>@fldRadif)
		   set @flagOUt=@RowCountTakhfif
		end
end

GO
/****** Object:  StoredProcedure [dbo].[z117ReportFactorHavaleNashode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z117ReportFactorHavaleNashode]
--چندزبانه

AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	
SET NOCOUNT ON;

create table #TempReportFactor(

ID bigint,
fldShomareFactor bigint,
fldSharheTafzili  Nvarchar(200) collate Persian_100_CI_AS,
fldCodeAshkhas bigint,
fldCodeGroupAshkhas bigint,
fldNameGroup nvarchar(200) collate Persian_100_CI_AS
)
create table #Temp(

fldCount bigint,
fldNameGroup nvarchar(200) collate Persian_100_CI_AS
)

insert into #TempReportFactor(ID,fldShomareFactor,fldSharheTafzili,fldCodeAshkhas,fldCodeGroupAshkhas,fldNameGroup)
select ROW_NUMBER()OVER(ORDER BY dbo.tblRizeForoosh.ID) as Radif,dbo.tblRizeForoosh.fldShomareFactor
,dbo.tblTafzili.fldSharheTafzili,dbo.tblRizeForoosh.fldCodeTafsili,dbo.tblTafzili.fldCodeGroup,dbo.tblGroupAshkhas.fldGroupName from tblRizeForoosh inner join
dbo.tblTafzili on (dbo.tblTafzili.fldCodeTafzili=dbo.tblRizeForoosh.fldCodeTafsili) inner join
dbo.tblGroupAshkhas on (dbo.tblTafzili.fldCodeGroup=dbo.tblGroupAshkhas.fldGroupId)
 where fldShomareFactor NOT in
(select fldShomareFactor from tblHavaleGoroohi)

insert into #Temp(fldCount,fldNameGroup) select  count(fldCodeGroupAshkhas)as fldCount,fldNameGroup  
	from #TempReportFactor group by fldNameGroup
	
	select fldCount,fldNameGroup from #Temp					 
END

GO
/****** Object:  StoredProcedure [dbo].[z118VazietTasfieHavale]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z118VazietTasfieHavale] 
--چندزبانه
	@fldShomareHavale bigint
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


CREATE TABLE #tmpDaryaftTasfie4(	
	[fldShomareFactor] [bigint]  null,
	[SumDaryafti] [bigint] null,
	[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldMablagheMande]decimal (18, 4),
	[fldMablagheBargashti]decimal (18, 4),
	[fldmandeTasfienashode] decimal (18, 4),
	[fldSharh][nvarchar](100) COLLATE Persian_100_CI_AS,	
	[fldDateSarResid][bigint],
	[fldDateFactor][bigint],
	[ShomareDaryaft][bigint],
	[SharheTafzili][nvarchar](200) COLLATE Persian_100_CI_AS,
	[codeTafzili][nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldCodeGroupAshkhas][bigint],
	[NameGroup][nvarchar](80) COLLATE Persian_100_CI_AS,
	[fldCodeVasete][bigint],
	[NameVasete][nvarchar](200) COLLATE Persian_100_CI_AS,
	[Tell][nvarchar](50) COLLATE Persian_100_CI_AS,
	[Adress][nvarchar](200) COLLATE Persian_100_CI_AS

	)

	
	
	insert into #tmpDaryaftTasfie4
			(fldShomareFactor,SumDaryafti,fldMablagheBargashti,fldCodeTafzili,fldMablagheMande,fldSharh,fldDateSarResid,fldDateFactor)
			Select distinct dbo.tblTasfie.fldShomareFactor,0,0,fldCodeTafzili, fldMablagheMandeFactor,fldSharh,fldDateSarResid, dbo.tblRizeForoosh.fldDate		
				from  dbo.tblTasfie INNER JOIN
				dbo.tblRizeForoosh  on dbo.tblTasfie.fldShomareFactor=dbo.tblRizeForoosh.fldShomareFactor 
	update #tmpDaryaftTasfie4
		set SumDaryafti=tbl1.sumDaryafti from		
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfie where fldShomareDaryaft<>0 group by fldShomareFactor ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor) as tbl1
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl1.fldShomareFactor
	
	--بروزرساني جمع مبلغ برگشتی bm
	update #tmpDaryaftTasfie4
	set fldMablagheBargashti=tbl11.fldMablagheBargashti from
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as fldMablagheBargashti from tblRizeTasfie where fldShomareBargashtAzForoosh<>0 group by fldShomareFactor ) as tbl11
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl11.fldShomareFactor

		
	
	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
	--	(Select fldShomareFactor, fldMablagh from tblRizeTasfie Where fldShomareDaryaft=@ShomareDaryaft) as tbl2
	--	where tbl2.fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor

	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null
	update   #tmpDaryaftTasfie4 set fldmandeTasfienashode=fldMablagheMande-SumDaryafti-fldMablagheBargashti

	--بروزرساني کد تفصيلي
	update #tmpDaryaftTasfie4 set codeTafzili=tbl3.fldCodeTafsili from
		(SELECT        tblForoosh.fldShomareFactorForoosh,tblRizeForoosh.fldCodeTafsili
			FROM            tblForoosh INNER JOIN
                         tblRizeForoosh ON tblForoosh.fldShomareFactorForoosh = tblRizeForoosh.fldShomareFactor) as tbl3
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl3.fldShomareFactorForoosh

	--بروزرساني نام تفصيلي
	update #tmpDaryaftTasfie4 set SharheTafzili=tbl5.fldSharheTafzili from
		(Select fldSharheTafzili,fldCodeTafzili from tblTafzili) as tbl5
		where #tmpDaryaftTasfie4.codeTafzili=tbl5.fldCodeTafzili

	--بروزرساني کدگروه 
	update #tmpDaryaftTasfie4 set fldCodeGroupAshkhas=tbl6.fldCodeGroup from
		(Select fldCodeGroup,fldCodeTafzili from tblTafzili) as tbl6
		Where #tmpDaryaftTasfie4.fldCodeTafzili=tbl6.fldCodeTafzili
	
	--بروزرساني نام گروه
	update #tmpDaryaftTasfie4  set NameGroup=tbl6.fldGroupName from
		(Select fldGroupId,fldGroupName from tblGroupAshkhas) as tbl6
		where tbl6.fldGroupId=#tmpDaryaftTasfie4.fldCodeGroupAshkhas
	

	--بروزرساني کد ويزيتورها	
	update #tmpDaryaftTasfie4  set fldCodeVasete = tbl7.fldCodeVasete from
		(SELECT        tblForoosh.fldShomareFactorForoosh, tblRizeForoosh.fldCodeVasete
			FROM            tblForoosh INNER JOIN
							 tblRizeForoosh ON tblForoosh.fldShomareFactorForoosh = tblRizeForoosh.fldShomareFactor) as tbl7
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl7.fldShomareFactorForoosh

	--بروزرساني نام ويزيتورها
	update #tmpDaryaftTasfie4  set NameVasete=tbl8.fldSharheTafzili from
		(Select fldSharheTafzili,fldCodeTafzili from tblTafzili) as tbl8
		where #tmpDaryaftTasfie4.fldCodeVasete=tbl8.fldCodeTafzili  and fldCodeVasete<>0
	
	update #tmpDaryaftTasfie4 set NameVasete='' where fldCodeVasete=0

		select NameGroup,fldShomareFactor,fldMablagheMande,SumDaryafti,
		fldMablagheBargashti,fldmandeTasfienashode,fldDateSarResid,Adress,fldSharh
		 from #tmpDaryaftTasfie4 where fldShomareFactor in(select fldShomareFactor from tblHavaleGoroohi where fldShomareHavale=@fldShomareHavale)

	

END

GO
/****** Object:  StoredProcedure [dbo].[z119ReportPorsantVarizVizitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z119ReportPorsantVarizVizitor]
--چندزبانه
	@fldCodeVisitor bigint,
	@DateAz bigint,
	@DateTa bigint
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @strCodeMoinVariz nvarchar(20),@JameVariz decimal(18,4)
    create table #tmpPorsantVariz
		(
			fldCodeVasete bigint,
			fldNameVizitor nvarchar(20),			
			fldJameVariz decimal(18,4)
		)
	
		declare @KolBank nvarchar(50),@KolSandoogh nvarchar(50)
		
		execute CodeKolHesab 'bank',@KolBank output
		execute CodeKolHesab 'sandoogh',@KolSandoogh output

		insert into #tmpPorsantVariz
			(fldJameVariz,fldCodeVasete)
		SELECT     SUM(fldBedehkar-fldBestankar) AS Expr1, fldVasete
			FROM         tblSodooreSanad
			WHERE     (fldVasete <> 0) and (fldCodekol=@KolBank or fldCodeKol=@KolSandoogh) and fldDate between   CONVERT(bigint, SUBSTRING(CONVERT(nvarchar, @DateAz), 3, 8)) and CONVERT(bigint, SUBSTRING(CONVERT(nvarchar, @DateTa), 3, 8))
			GROUP BY fldVasete

			
		insert into #tmpPorsantVariz
			(fldJameVariz,fldCodeVasete)
		SELECT     SUM(fldMablagh) AS Expr1,  CONVERT(bigint,fldVasete)AS fldCodeVasete
			FROM         tblCheckeDaryafti
			WHERE     (CONVERT(bigint,fldVasete) <> 0 and ISNUMERIC(fldShomareVosool)=1) and fldDateVosool between   @DateAz and @DateTa
			GROUP BY CONVERT(bigint,fldVasete)

	update #tmpPorsantVariz set fldNameVizitor=tblTafzili.fldSharheTafzili from tblTafzili where #tmpPorsantVariz.fldCodeVasete=tblTafzili.fldCodeTafzili 
	
	select fldCodeVasete,fldNameVizitor,Sum(fldJameVariz) as fldJameVariz from #tmpPorsantVariz where fldCodeVasete=@fldCodeVisitor
		group by fldCodeVasete,fldNameVizitor
	
END


GO
/****** Object:  StoredProcedure [dbo].[z120MasterMenu]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z120MasterMenu]
@CodeUser bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT        ID, fldNameFarsi, fldNameEnglish
		FROM            tblMenuMaster
		WHERE        (fldNameEnglish NOT IN
									 (SELECT        fldNameEnglish
										FROM            tblMenuSefareshi Where fldCodeUser=@CodeUser))
		order by fldNameFarsi
END

GO
/****** Object:  StoredProcedure [dbo].[z121InsertSharhFeeDynamic]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z121InsertSharhFeeDynamic]

@Code bigint,
@fldSharh nvarchar(150),
@countRow bigint

as 

begin

	insert into tblSharheFeeDynamic (fldCodeSharh,fldSharh,fldCountRow)
		values(@Code,@fldSharh,@countRow)

end

GO
/****** Object:  StoredProcedure [dbo].[z122UpdateSharhFeeDynamic]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

create proc [dbo].[z122UpdateSharhFeeDynamic]

@fldCode int,
@fldSharh nvarchar(150),
@flagOut int output
as

begin
	Update tblSharheFeeDynamic Set fldCodeSharh =@fldCode,fldSharh=@fldSharh Where fldCountRow=@fldCode
	set @flagOut=1
end

GO
/****** Object:  StoredProcedure [dbo].[z123TekrariSharheFeeDynamic]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[z123TekrariSharheFeeDynamic]

@fldSharh nvarchar(150),
@Code int,
@IDType int,
@flagOUt int output
as
begin
	declare @RowCountTakhfif int
	if @IDType=0  --براي مقايسه براي حالت اضافه کردن رکورد
		begin
		 set @RowCountTakhfif=(Select Count(fldSharh) from tblSharheFeeDynamic  Where fldSharh=@fldSharh)
		 set @flagOUt=@RowCountTakhfif
		end
	else
		begin --براي مقايسه با غيرخودش در حالت ويرايش
		   set	@RowCountTakhfif=(Select  Count(fldSharh) from tblSharheFeeDynamic  Where fldSharh=@fldSharh and fldCountRow<>@Code)
		   set @flagOUt=@RowCountTakhfif
		end
end

GO
/****** Object:  StoredProcedure [dbo].[z124InsertMultiFeeDynamic]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z124InsertMultiFeeDynamic]
--چندزبانه
@Id bigint,
@CodeSharh bigint,
@CodeKala bigint,
@fldMablagh decimal(18,4),
@countRow bigint

as 

begin



	insert into tblMultiFeeDynamic(id,fldCodeSharh,fldCodeKala,fldFee,fldcountRow)
	values(@Id,@CodeSharh,@CodeKala,@fldMablagh,@countRow)

end

GO
/****** Object:  StoredProcedure [dbo].[z125UpdateMultiFeeDynamic]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z125UpdateMultiFeeDynamic]
--چندزبانه
@Id bigint,
@CodeSharh bigint,
@CodeKala bigint,
@fldMablagh decimal(18,4),
@flagOut int output
as

begin
	Update tblMultiFeeDynamic Set fldCodeSharh=@CodeSharh,fldCodeKala=@CodeKala,fldFee=@fldMablagh Where Id=@Id
	set @flagOut=1
end

GO
/****** Object:  StoredProcedure [dbo].[z126TekrariMultiFeeDynamic]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z126TekrariMultiFeeDynamic]
--چندزبانه
@fldCodeSharh bigint,
@fldCodeKala bigint,
@fldMablagh decimal (18, 4),
@Code int,
@IDType int,
@flagOUt int output
as
begin
	declare @RowCountTakhfif int
	if @IDType=0  --براي مقايسه براي حالت اضافه کردن رکورد
		begin
		 set @RowCountTakhfif=(Select Count(fldFee) from tblMultiFeeDynamic  Where fldCodeSharh=@fldCodeSharh and fldCodeKala=@fldCodeKala) --and fldFee=@fldMablagh)
		 set @flagOUt=@RowCountTakhfif
		end
	else
		begin --براي مقايسه با غيرخودش در حالت ويرايش
		   set	@RowCountTakhfif=(Select  Count(fldFee) from tblMultiFeeDynamic  Where  fldCodeSharh=@fldCodeSharh and fldCodeKala=@fldCodeKala and fldCountRow<>@Code) --and fldFee=@fldMablagh )
		   set @flagOUt=@RowCountTakhfif
		end
end
--select * from tblMultiFeeDynamic where fldCodeSharh=1001
GO
/****** Object:  StoredProcedure [dbo].[z127InsertCheckEbtedaDoreh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z127InsertCheckEbtedaDoreh]
--
@fldCodeArz int,
@fldNerkheArz decimal(18,4),
@fldCodeSahbeCheck bigint,
@fldShomareCheck bigint,
@fldShomareHesab bigint,
@fldDateSarResid int,
@fldDateDaryaft int,
@fldCodeBank int,
@fldShobe nvarchar(50),
@fldMablagh decimal(18,4),
@strShomareSanad nvarchar output

as 

begin
	begin transaction AddRecord
		begin try
		
	declare 
		@intShomareSanad int,
		@strResult nvarchar(50),
		@strResult2 nvarchar(50),
		@strVaziat nvarchar(50),
		@strType nvarchar(50),
		@strRadif1 nvarchar(50),
		@strRadif2 nvarchar(50),
		@count int,
		@fldDate int,
	    @day nvarchar(50),
		@month nvarchar(50),
		@year nvarchar(50),
		@kolVaMoin nvarchar(100),
		@kol nvarchar(50),
		@moin nvarchar(50),
		@Toolkol int,
		@Toolmoin int,
		@KolCheck nvarchar(50),
		@jameMablaghKol decimal(18,4)
		--execute CodeMoinHesab 'check',@kolVaMoin
		set @kolVaMoin=(Select  fldCodeMoinCheck from tblTanzimat )
		set @Toolkol=(select fldKol from tblPaye)
		set @Toolmoin=(select fldMoin from tblPaye)
		set @kol=(SUBSTRING(@kolVaMoin,1,@Toolkol))
		set @moin=(SUBSTRING(@kolVaMoin,@Toolkol+1,@Toolmoin))
		
		set @KolCheck=(Select  fldKolSarmaye from tblTanzimat )
		
		set @count= (Select COUNT(fldShomareCheck) from tblCheckeDaryafti where fldType='checkebteda')
		set @day=(SUBSTRING(CONVERT(nvarchar, @fldDateDaryaft), 7, 2))
		set @month=(SUBSTRING(CONVERT(nvarchar, @fldDateDaryaft), 5, 2))
		set @year=(SUBSTRING(CONVERT(nvarchar, @fldDateDaryaft), 0, 5))
		--set @fldDate=(CONVERT(int, @year+@month+@day))
		set @fldDate=@fldDateDaryaft
		--set @year=@fldDate
	
 if @count>0
begin

 set @strVaziat='Last'
end
 else
begin

	set @strVaziat='First'
end


if @strVaziat='First'
	begin
	


		set @intShomareSanad=(select MAX(CONVERT(decimal(18,4), fldSanad)+1)as fldSanad from tblSodooreSanad)
		execute InsertSanadFirstMultiArz @fldCodeArz,@fldNerkheArz,  @kol,@moin,@kolVaMoin,@fldCodeSahbeCheck,NULL,@fldMablagh,0,'1',@fldDate,NULL,@day,@month,@year,'checkebteda',0,@intShomareSanad output;
			   
		 execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @KolCheck,NULL,@KolCheck,NULL,'1','0',@fldMablagh,@strRadif1,@fldDate,NULL,@day,@month,@year,'checkebteda',0,@intShomareSanad;
		--execute insertSanadFirst @KolCheck,NULL,@KolCheck,NULL,NULL, @fldMablagh,0,'1',@fldDate,NULL,@day,@month,@year,'ebteda',0,@intShomareSanad;

		insert into tblCheckeDaryafti(fldCodeSahebeCheck,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldDateDaryaft,fldCodeBank,fldShobe,fldMablagh,fldSanad,fldType,fldRadif,fldShomareDaryaft) 	
		values(@fldCodeSahbeCheck,@fldShomareCheck,@fldShomareHesab,@fldDateSarResid,@fldDateDaryaft,@fldCodeBank,@fldShobe,@fldMablagh,@intShomareSanad,'checkebteda','1',0)
	end

if @strVaziat='Last'
	begin
	 	set @intShomareSanad=(select distinct CONVERT(decimal(18,4), fldSanad) from tblSodooreSanad where fldType='checkebteda')
	    set @strRadif1=(CONVERT(nvarchar, @count+1))
		set @jameMablaghKol=(select sum(fldMablagh)+@fldMablagh from tblCheckeDaryafti where fldType='checkebteda')
		
		delete from tblSodooreSanad Where fldSanad=@intShomareSanad and fldBestankar>0
		execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @kol,@moin,@kolVaMoin,@fldCodeSahbeCheck,'1',@fldMablagh,'0',@strRadif1,@fldDate,NULL,@day,@month,@year,'checkebteda',0,@intShomareSanad;
	    set @strRadif2=(CONVERT(nvarchar, @count+2))
		execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @KolCheck,NULL,@KolCheck,NULL,'1','0',@jameMablaghKol,@strRadif2,@fldDate,NULL,@day,@month,@year,'checkebteda',0,@intShomareSanad;
	--execute insertSanadLast '1','1','1',@fldCodeSahbeCheck,'1',@fldMablagh,0,2,971003,NULL,'03','10','97','checkebteda',0,'36';
		
		insert into tblCheckeDaryafti(fldCodeSahebeCheck,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldDateDaryaft,fldCodeBank,fldShobe,fldMablagh,fldType,fldSanad,fldRadif,fldShomareDaryaft) 	
		values(@fldCodeSahbeCheck,@fldShomareCheck,@fldShomareHesab,@fldDateSarResid,@fldDateDaryaft,@fldCodeBank,@fldShobe,@fldMablagh,'checkebteda',@intShomareSanad,@strRadif1,0)
	
	end

	update tblSodooreSanad set fldDate= @fldDate,fldDay=@day,fldMonth=@month,fldYear=@year Where fldSanad=@intShomareSanad 	

	update tblSodooreSanad set fldCodeArz=@fldCodeArz,fldNerkheArz=@fldNerkheArz where fldSanad=@intShomareSanad
		
		commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				set @strResult2='error'
				rollback transaction AddRecord;
			end
		end catch
		end

GO
/****** Object:  StoredProcedure [dbo].[z127SoodeKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z127SoodeKala]
@language nvarchar(10)
--چندزبانه
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

        create table #tmpGardesheKala
		(
			CodeKala bigint,
			CodeGroup bigint,
			CodeAnbar bigint,
			NameGroup nvarchar(100) collate Persian_100_CI_AS,
			NameKala nvarchar(100) collate Persian_100_CI_AS,
			NameAnbar nvarchar(100) collate Persian_100_CI_AS,
			TedadDarKarton decimal(18,4),

			MablagheEbteda decimal(18,4),
			TedadeEbteda decimal(18,4),
			TedadKartonEbteda nvarchar(100) collate Persian_100_CI_AS,

			MablagheKharid decimal(18,4),
			TedadeKharid decimal(18,4),
			TedadKartonKharid nvarchar(100) collate Persian_100_CI_AS,

			TedadMoghayerat decimal(18,4),

			MablagheBargashtAzKharid decimal(18,4),
			TedadeBargashtAzKharid decimal(18,4),
			TedadKartonBargashtAzKharid nvarchar(100) collate Persian_100_CI_AS,

			MablagheKhalesKharid decimal(18,4),
			TedadeKhalesKharid decimal(18,4),
			TedadKartonKhalesKharid nvarchar(100) collate Persian_100_CI_AS,

			MablagheForoosh decimal(18,4),
			TedadeForoosh decimal(18,4),
			TedadeKartonForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablagheBargashtAzForoosh decimal(18,4),
			TedadeBargashtAzForoosh decimal(18,4),
			TedadeKartonBargashtAzForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablagheKhalesForoosh decimal(18,4),
			TedadeKhalesForoosh decimal(18,4),
			TedadKartonKhalesForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablaghePayan decimal(18,4),
			TedadePayan decimal(18,4),
			TedadeKartonPayan nvarchar(100) collate Persian_100_CI_AS,

			BahayeTamamShodeForoosh decimal(18,4),
			SoodeKala decimal(18,4)
		)
		delete from #tmpGardesheKala

		insert into #tmpGardesheKala
			(CodeKala,CodeGroup,NameKala,TedadDarKarton)
			Select  fldCodeKala,fldCodeGroup,fldNameKala,fldTedadJoz 
				from  tblKala

		--update #tmpGardesheKala set TedadeEbteda=Sum(tblRizeKharid.fldTedadeJoz) from tblRizeKharid where #tmpGardesheKala.CodeKala=tblRizeKharid.fldCodeKala group by tblRizeKharid.fldCodeKala

		update           #tmpGardesheKala
			set NameGroup = tblGroupKala.fldGroupName from tblGroupKala Where CodeGroup=tblGroupKala.fldGroupId

		--####تعداد مغایرت####
		update           #tmpGardesheKala
			set TedadMoghayerat=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizekharid Where fldType='Moghayrat' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		update           #tmpGardesheKala
			set TedadMoghayerat-=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeForoosh Where fldType='Moghayrat' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		--####ابتدای دوره####
		update           #tmpGardesheKala
			set tedadeEbteda=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizekharid Where fldType='mojodi_ebteda_doreh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheEbteda=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldFeeKol) as Sum1 from tblRizekharid Where fldType='mojodi_ebteda_doreh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 
			--mojodi_ebteda_doreh



		--کارتن و عدد
		update #tmpGardesheKala Set TedadKartonEbteda=Convert(varchar,floor(tedadeEbteda / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeEbteda, TedadDarKarton) } ) 
			where TedadDarKarton>0
		--######################
		--######خرید#####
		update           #tmpGardesheKala
			set tedadekharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizekharid Where fldType='kharid' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set Mablaghekharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizekharid Where fldType='kharid' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		--کارتن و عدد
		update #tmpGardesheKala Set TedadKartonkharid=Convert(varchar,floor(TedadeKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeKharid, TedadDarKarton) } ) 
			where TedadDarKarton>0
		--#######################
		--######برگشت از خرید#####
		update           #tmpGardesheKala
			set tedadeBargashtAzkharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeBargashtAzkharid Where fldType='bargashtazkharid' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheBargashtAzKharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeBargashtAzkharid Where fldType='bargashtazkharid' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		--کارتن و عدد
		update #tmpGardesheKala Set TedadKartonBargashtAzkharid=Convert(varchar,floor(TedadeBargashtAzKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeBargashtAzKharid, TedadDarKarton) } ) 
			where TedadDarKarton>0
		--#######################

		--######فروش#####
		update           #tmpGardesheKala
			set TedadeForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeForoosh Where fldType='Foroosh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeForoosh Where fldType='Foroosh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		--کارتن و عدد
		update #tmpGardesheKala Set TedadeKartonForoosh=Convert(varchar,floor(TedadeForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeForoosh, TedadDarKarton) } ) 
			where TedadDarKarton>0

	--######برگشت از فروش#####
		update           #tmpGardesheKala
			set TedadeBargashtAzForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeBargashtAzForoosh Where fldType='bargashtazforoosh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheBargashtAzForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeBargashtAzForoosh Where fldType='bargashtazforoosh' group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		--کارتن و عدد
		update #tmpGardesheKala Set TedadeKartonForoosh=Convert(varchar,floor(TedadeForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeForoosh, TedadDarKarton) } ) 
			where TedadDarKarton>0
		--#######################
		--#######################
		update           #tmpGardesheKala
			set TedadeEbteda=0 Where TedadeEbteda is null  
		update           #tmpGardesheKala
			set TedadeKharid=0 Where TedadeKharid is null
		update           #tmpGardesheKala
			set TedadeBargashtAzKharid=0 Where TedadeBargashtAzKharid is null
		update           #tmpGardesheKala
			set TedadeForoosh=0 Where TedadeForoosh is null
		update           #tmpGardesheKala
			set TedadeBargashtAzForoosh=0 Where TedadeBargashtAzForoosh is null
		update           #tmpGardesheKala
			set MablagheEbteda=0 Where MablagheEbteda is null
		update           #tmpGardesheKala
			set MablagheKharid=0 Where MablagheKharid is null
		update           #tmpGardesheKala
			set MablagheForoosh=0 Where MablagheForoosh is null
		update           #tmpGardesheKala
			set MablaghePayan=0 Where MablaghePayan is null
		update           #tmpGardesheKala
			set bahayetamamshodeforoosh=0 Where bahayetamamshodeforoosh is null
		update           #tmpGardesheKala
			set SoodeKala=0 Where SoodeKala is null

		update           #tmpGardesheKala
			set TedadMoghayerat=0 Where TedadMoghayerat is null

		--######خالص خرید#######
		update           #tmpGardesheKala
			set TedadeKhalesKharid=TedadeKharid -TedadeBargashtAzKharid
		update #tmpGardesheKala Set TedadKartonKhalesKharid=Convert(varchar,floor(TedadeKhalesKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(TedadeKhalesKharid, TedadDarKarton) } ) 
			where TedadDarKarton>0

		update #tmpGardesheKala Set MablagheKhalesKharid=MablagheKharid-MablagheBargashtAzKharid

		--######خالص فروش#######
		update           #tmpGardesheKala
			set TedadeKhalesForoosh=TedadeForoosh-TedadeBargashtAzForoosh
		update #tmpGardesheKala Set TedadKartonKhalesForoosh=Convert(varchar,floor(TedadeKhalesForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(TedadeKhalesForoosh, TedadDarKarton) } ) 
			where TedadDarKarton>0

		update #tmpGardesheKala Set MablagheKhalesForoosh=MablagheForoosh-MablagheBargashtAzForoosh
		--######پایان دوره#####
		update           #tmpGardesheKala
			set tedadePayan=TedadeEbteda+TedadeKharid+TedadeBargashtAzForoosh + TedadMoghayerat -TedadeForoosh-TedadeBargashtAzKharid
		update #tmpGardesheKala
			set MablaghePayan=(MablagheEbteda+MablagheKharid)/(TedadeEbteda+TedadeKharid)*tedadePayan
			where TedadeEbteda+TedadeKharid>0
		--کارتن و عدد
		update #tmpGardesheKala Set TedadeKartonPayan=Convert(varchar,floor(TedadePayan/ TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadePayan, TedadDarKarton) } ) 
			where TedadDarKarton>0
		--#######################

		--####انتقال بین انبار و تولید کنترل شود#######

		   
		

		update #tmpGardesheKala set BahayeTamamShodeForoosh=MablagheEbteda+MablagheKharid-MablaghePayan
		update #tmpGardesheKala set SoodeKala=MablagheForoosh-BahayeTamamShodeForoosh


		update #tmpGardesheKala set #tmpGardesheKala.NameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#tmpGardesheKala.CodeKala and tblkalatranslate.fldLanguage=@Language

		Select ROW_NUMBER() OVER(ORDER BY CodeKala ) AS Row, CodeKala,NameGroup,NameKala,TedadDarKarton,TedadeEbteda,TedadKartonEbteda,MablagheEbteda,
			TedadeKharid,TedadKartonKharid,MablagheKharid,TedadeForoosh,TedadeKartonForoosh,MablagheForoosh,TedadePayan,TedadeKartonPayan,MablaghePayan,BahayeTamamShodeForoosh ,SoodeKala
		from #tmpGardesheKala 
	
END




GO
/****** Object:  StoredProcedure [dbo].[z128ShowCheakEbtedaDoreh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[z128ShowCheakEbtedaDoreh]
--چندزبانه
as
begin
	select dbo.tblCheckeDaryafti.ID,fldSanad,fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldShomareCheck,dbo.tblCheckeDaryafti.fldShomareHesab,			 
							 SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblCheckeDaryafti.fldDateSarResid), 7, 2) AS fldDateSarResid
			,dbo.tblBankeMoshtari.fldNameBank,fldCodeBank,fldShobe,fldMablagh from tblCheckeDaryafti inner join 
			 dbo.tblBankeMoshtari ON dbo.tblCheckeDaryafti.fldCodeBank = dbo.tblBankeMoshtari.ID inner join
			 dbo.tblTafzili on dbo.tblCheckeDaryafti.fldCodeSahebeCheck=dbo.tblTafzili.fldCodeTafzili
			 where fldType='checkebteda'
end 

GO
/****** Object:  StoredProcedure [dbo].[z129DeleteCheakEbtedaDoreh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[z129DeleteCheakEbtedaDoreh]
@id bigint

as
begin
	begin transaction AddRecord
		begin try

	declare @strRadif nvarchar(50),
			@jamMablagh decimal,
			@strResult nvarchar(50),
			@strResult2 nvarchar(50),
			@intShomareSanad int,
			@count int

	set @strRadif=(select fldRadif from tblCheckeDaryafti where id=@id)
	set @intShomareSanad=(select fldSanad from tblCheckeDaryafti where id=@id)


	delete from tblSodooreSanad where fldType='checkebteda' and fldRadif=@strRadif

	--delete from tblSodooreSanad Where fldSanad=@intShomareSanad and fldBestankar>0
	delete from tblCheckeDaryafti
			 where id=@id
	set @count=(Select COUNT(fldShomareCheck) from tblCheckeDaryafti where fldType='checkebteda')
	if @count>0
	begin
	set @jamMablagh=(select sum(fldMablagh) from tblCheckeDaryafti where fldType='checkebteda')
	update tblSodooreSanad set fldBestankar=@jamMablagh , fldRadif=@strRadif Where fldSanad=@intShomareSanad and fldBestankar>0
	end
	else
	begin
	delete from tblSodooreSanad Where fldSanad=@intShomareSanad and fldBestankar>0
	end
		commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				set @strResult2='error'
				rollback transaction AddRecord;
			end
		end catch


end 

GO
/****** Object:  StoredProcedure [dbo].[z130UpdateCheakEbtedaDoreh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[z130UpdateCheakEbtedaDoreh]
--چندزبانه
@fldCodeArz int,
@fldNerkheArz decimal(18,4),
@id bigint,
@fldCodeSahbeCheck bigint,
@fldShomareCheck bigint,
@fldShomareHesab bigint,
@fldDateSarResid int,
@fldDateDaryaft int,
@fldCodeBank int,
@fldShobe nvarchar(50),
@fldMablagh decimal(18,4),
@strResult nvarchar(25) output
as
begin

begin transaction AddRecord
begin try
	declare @day nvarchar(10),@month nvarchar(10),@year nvarchar(10),@fldDate nvarchar(10)
	declare @strRadif nvarchar(50),
			@jamMablagh decimal(18,4),
			@strResult2 nvarchar(50),
			@intShomareSanad int,
			@count int

				set @strRadif=(select fldRadif from tblCheckeDaryafti where ID=@id)
	set @intShomareSanad=(select fldSanad from tblCheckeDaryafti where ID=@id)
			
			update tblCheckeDaryafti 
			set	
				fldCodeSahebeCheck=@fldCodeSahbeCheck,
				fldShomareCheck =@fldShomareCheck,
				fldShomareHesab =@fldShomareHesab,
				fldDateSarResid =@fldDateSarResid,
				fldDateDaryaft =@fldDateDaryaft,
				fldCodeBank =@fldCodeBank,
				fldShobe =@fldShobe,
				fldMablagh =@fldMablagh,
				fldShomareDaryaft=0
			where ID =@id 

			set @day=(SUBSTRING(CONVERT(nvarchar, @fldDateDaryaft), 7, 2))
			set @month=(SUBSTRING(CONVERT(nvarchar, @fldDateDaryaft), 5, 2))
			set @year=(SUBSTRING(CONVERT(nvarchar, @fldDateDaryaft), 0, 5))
			set @fldDate= @fldDateDaryaft --(CONVERT(int, @year+@month+@day))

			set @jamMablagh=(select sum(fldMablagh) from tblCheckeDaryafti where fldType='checkebteda')
			update tblSodooreSanad set fldBestankar=@jamMablagh , fldRadif=@strRadif,fldDate= @fldDate,fldDay=@day,fldMonth=@month,fldYear=@year Where fldSanad=@intShomareSanad and fldBestankar>0 

		
				
			update tblSodooreSanad set fldCodeTafzili=@fldCodeSahbeCheck, fldBedehkar=@fldMablagh ,fldDate= @fldDate,fldDay=@day,fldMonth=@month,fldYear=@year Where fldSanad=@intShomareSanad and fldRadif=@strRadif and fldBedehkar>0


			update tblSodooreSanad Set fldDate= @fldDate,fldDay=@day,fldMonth=@month,fldYear=@year Where fldSanad=@intShomareSanad

			update tblSodooreSanad set fldCodeArz=@fldCodeArz,fldNerkheArz=@fldNerkheArz Where fldSanad=@intShomareSanad

			update tblCheckeDaryafti set fldDateDaryaft =@fldDateDaryaft where fldShomareDaryaft=0

commit transaction AddRecord;
    end try
begin catch
if @@TranCount > 0
		begin
			set @strResult='error'
					
			rollback transaction AddRecord;
		end
end catch
end 

GO
/****** Object:  StoredProcedure [dbo].[z140MojoodieKalaDateEngheza]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z140MojoodieKalaDateEngheza]
--چندزبانه
@CodeKala bigint
as 
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	Create Table #tmpMovaghat(
		DateEngheza nvarchar(20) COLLATE Persian_100_CI_AS,
		TedadeJoz decimal(18,4),
		TedadKol nvarchar(100) COLLATE Persian_100_CI_AS,
		fldNameVahed1 Nvarchar(100) COLLATE Persian_100_CI_AS,
		fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
		fldTedadDarKarton decimal(18,4)
	)

	Create Table #tmpShow(
		DateEngheza nvarchar(20) COLLATE Persian_100_CI_AS,
		SumTedad decimal(18,4),
		TedadKol nvarchar(100) COLLATE Persian_100_CI_AS,
		fldNameVahed1 Nvarchar(100) COLLATE Persian_100_CI_AS,
		fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
		fldTedadDarKarton decimal(18,4),
		fldTedadVoroodKartonShow Nvarchar(100) COLLATE Persian_100_CI_AS
	)

	delete #tmpShow
	delete #tmpMovaghat

	insert into #tmpMovaghat
		(DateEngheza,TedadeJoz)
	Select fldDateEngheza,fldTedadJoz  from tblRizeKharid Where fldCodeKala=@CodeKala

		insert into #tmpMovaghat
		(DateEngheza,TedadeJoz)
	Select fldDateEngheza,fldTedadJoz  from tblRizeBargashtAzForoosh Where fldCodeKala=@CodeKala

	insert into #tmpMovaghat
		(DateEngheza,TedadeJoz)
	Select fldDateEngheza,-1*fldTedadJoz  from tblRizeForoosh Where fldCodeKala=@CodeKala

	insert into #tmpMovaghat
		(DateEngheza,TedadeJoz)
	Select fldDateEngheza,-1*fldTedadJozAvalie  from tblRizeTolid Where fldCodeKala=@CodeKala And fldType='avalie'

	insert into #tmpMovaghat
		(DateEngheza,TedadeJoz)
	Select fldDateEngheza,fldTedadJozNahaei  from tblRizetolid Where fldCodeKala=@CodeKala And fldType='mahsool'

	insert into #tmpMovaghat
		(DateEngheza,TedadeJoz)
	Select fldDateEngheza,-1*fldTedadJoz  from tblRizeBargashtAzKharid Where fldCodeKala=@CodeKala

	update #tmpMovaghat set DateEngheza=0 Where DateEngheza Is Null
	
	update #tmpMovaghat set DateEngheza= substring(convert(nvarchar(10),DateEngheza),0,5) +'/' +substring(convert(nvarchar(10),DateEngheza),5,2) + '/' + substring(convert(nvarchar(10),DateEngheza),7,2) where DateEngheza<>'0'
	
	update #tmpMovaghat set fldNameVahed1=tbl1.fldNameVahed from
		(Select fldNameVahed,fldCodeKala from tblVahed,tblKala Where tblVahed.ID=tblKala.fldCodeVahed1 and fldCodeKala=@CodeKala) as tbl1 where 1=1

	
	update #tmpMovaghat set fldNameVahed2=tbl1.fldNameVahed from
		(Select fldNameVahed,fldCodeKala from tblVahed,tblKala Where tblVahed.ID=tblKala.fldCodeVahed2 and fldCodeKala=@CodeKala) as tbl1 where 1=1



	Insert into #tmpShow (DateEngheza,SumTedad,fldNameVahed1,fldNameVahed2,fldTedadDarKarton)
		Select   DateEngheza ,Sum(TedadeJoz) As SumTedad,fldNameVahed1,fldNameVahed2,fldTedadDarKarton  from #tmpMovaghat group by DateEngheza,fldNameVahed1,fldNameVahed2,fldTedadDarKarton

	declare @TedadDarKarton decimal(18,4)
	set @TedadDarKarton=(select fldTedadJoz from tblKala where fldCodeKala=@CodeKala)

	
	if @TedadDarKarton>0 
		update #tmpShow Set TedadKol=Convert(varchar,floor(SumTedad / @TedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(SumTedad, @TedadDarKarton) } ) + ' ' + fldNameVahed1			
	else
		update #tmpShow Set TedadKol=''

	Select DateEngheza ,SumTedad ,TedadKol,fldNameVahed1,fldNameVahed2 from #tmpShow
	
end

GO
/****** Object:  StoredProcedure [dbo].[z141InsertMoghyratAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z141InsertMoghyratAnbar]
@strResult nvarchar(25) output,
@strResult2 nvarchar(25) output,
@fldCodeKala bigint,
@fldCodeAnbar bigint,
@fldShomareJoz bigint,
@fldShomareshJozNahei bigint,
@fldCountShomaresh int,
@fldDate bigint,
@fldTime nvarchar(20),
@tedad bigint


as 
begin
	declare @Radif bigint
	declare @MaboTafavotTedad bigint
begin transaction AddRecord
		begin try
	
			if @fldShomareshJozNahei=0
	begin
	if @fldCountShomaresh=1
	
	insert into tblMoghayratMojoodieAnbar(fldCodeKala,fldCodeAnbar,fldShomarsheJoz1,fldShomarsheJoz2,fldShomarsheJoz3,fldShomarsheJozNehai)				
		values(@fldCodeKala,@fldCodeAnbar,@fldShomareJoz,null,null,null)
	
	else if @fldCountShomaresh=2
		update tblMoghayratMojoodieAnbar set fldShomarsheJoz2=@fldShomareJoz  where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar
	else if @fldCountShomaresh=3
		update tblMoghayratMojoodieAnbar set fldShomarsheJoz3=@fldShomareJoz where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar

		if @fldShomareJoz=0 and @fldShomareshJozNahei=0
		begin
			if @tedad>0
			execute z145InsertMoghyratInfoForoosh @fldCodeAnbar,@fldCodeKala,@tedad,@fldDate,@fldTime
			else if @tedad<0
			execute z146InsertMoghyratInfoKharid @fldCodeAnbar,@fldCodeKala,@tedad,@fldDate,@fldTime
		end
	end
	else
	begin
	if @fldCountShomaresh=1
	begin
	insert into tblMoghayratMojoodieAnbar(fldCodeKala,fldCodeAnbar,fldShomarsheJoz1,fldShomarsheJoz2,fldShomarsheJoz3,fldShomarsheJozNehai)				
		values(@fldCodeKala,@fldCodeAnbar,@fldShomareJoz,null,null,@fldShomareshJozNahei)
	
	if @tedad=0
		begin
			execute z146InsertMoghyratInfoKharid @fldCodeAnbar,@fldCodeKala,@fldShomareJoz,@fldDate,@fldTime
		end
	else if @tedad>@fldShomareshJozNahei
		begin
			set @MaboTafavotTedad=@tedad-@fldShomareshJozNahei

				execute z145InsertMoghyratInfoForoosh @fldCodeAnbar,@fldCodeKala,@MaboTafavotTedad,@fldDate,@fldTime
		end
	else if @tedad<@fldShomareshJozNahei
		begin
			set @MaboTafavotTedad=@fldShomareshJozNahei-@tedad
			execute z146InsertMoghyratInfoKharid @fldCodeAnbar,@fldCodeKala,@MaboTafavotTedad,@fldDate,@fldTime
		end
		end
	else if @fldCountShomaresh=2
	begin
		update tblMoghayratMojoodieAnbar set fldShomarsheJoz2=@fldShomareJoz , fldShomarsheJozNehai=@fldShomareshJozNahei where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar
	if @tedad=0
		begin
			execute z146InsertMoghyratInfoKharid @fldCodeAnbar,@fldCodeKala,@fldShomareJoz,@fldDate,@fldTime
		end
	else if @tedad>@fldShomareshJozNahei
		begin
			set @MaboTafavotTedad=@tedad-@fldShomareshJozNahei

				execute z145InsertMoghyratInfoForoosh @fldCodeAnbar,@fldCodeKala,@MaboTafavotTedad,@fldDate,@fldTime
		end
	else if @tedad<@fldShomareshJozNahei
		begin
			set @MaboTafavotTedad=@fldShomareshJozNahei-@tedad
			execute z146InsertMoghyratInfoKharid @fldCodeAnbar,@fldCodeKala,@MaboTafavotTedad,@fldDate,@fldTime
		end
	end
	else if @fldCountShomaresh=3
	begin
		update tblMoghayratMojoodieAnbar set fldShomarsheJoz3=@fldShomareJoz,fldShomarsheJozNehai=@fldShomareshJozNahei where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar
	if @tedad=0
		begin
			execute z146InsertMoghyratInfoKharid @fldCodeAnbar,@fldCodeKala,@fldShomareJoz,@fldDate,@fldTime
		end
	else if @tedad>@fldShomareshJozNahei
		begin
			set @MaboTafavotTedad=@tedad-@fldShomareshJozNahei

				execute z145InsertMoghyratInfoForoosh @fldCodeAnbar,@fldCodeKala,@MaboTafavotTedad,@fldDate,@fldTime
		end
	else if @tedad<@fldShomareshJozNahei
		begin
			set @MaboTafavotTedad=@fldShomareshJozNahei-@tedad
			execute z146InsertMoghyratInfoKharid @fldCodeAnbar,@fldCodeKala,@MaboTafavotTedad,@fldDate,@fldTime
		end
	end
	end
	


	commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				set @strResult2='error'
				rollback transaction AddRecord;
			end
		end catch
end

GO
/****** Object:  StoredProcedure [dbo].[z142ShowListMoghayratMojoodieAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z142ShowListMoghayratMojoodieAnbar]

  @MiangineKharid bigint output,
  @JameTedad bigint output,
  @lngSumTedadeKolSahih bigint output,
  @Order int,
  @Filter int,
  @ListCodeKala varchar(5000)=null,
  @ListCodeAnbar varchar(5000)=null,
  @ListGroupKala varchar(5000)=null,
  @DateTa bigint,
  @NoeFee int
--@nn nvarchar(10)

as

IF 1=0 BEGIN
 SET FMTONLY OFF
END

begin


create table #TempMojoodieAnbar(

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 0),
fldTedadeKhorooj decimal(18, 0),
fldTedadeJoz decimal(18, 0), 
fldTedadeKol decimal(18, 0), 
fldTedadDarKarton decimal(18, 0),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 0),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20) COLLATE Persian_100_CI_AS,
fldShomarshTedadJoz1 bigint,
fldShomarshTedadJoz2 bigint,
fldShomarshTedadJoz3 bigint,
fldMoghyrat1 bigint,
fldMoghyrat2 bigint,
fldMoghyrat3 bigint,
fldShomareshJozNahei bigint

)


create table #TempShowMojoodi(

fldCodeKala bigint,
SumVorood decimal(18, 0),
SumKhorooj decimal(18, 0),
SumMande decimal(18, 0),
fldTedadDarKarton decimal(18, 0),
fldTedadKartonadadSahih decimal(18, 0),
fldTedadDarKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,
fldCodeAnbar int,
fldTedadVoroodKarton decimal(18, 0),
fldTedadVoroodKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadKhoroojKarton decimal(18, 0),
fldTedadKhoroojKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh decimal(18, 0),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldKharid bigint,
fldforoosh bigint,
fldArzesheMojoodi bigint,
fldFeeForoosh bigint,
fldShomarshTedadJoz1 bigint,
fldShomareTedadKol1 nvarchar(50),
fldShomarshTedadJoz2 bigint,
fldShomareTedadKol2 nvarchar(50),
fldShomarshTedadJoz3 bigint,
fldShomareTedadKol3 nvarchar(50),
fldMoghyrat1 bigint,
fldMoghyrat2 bigint,
fldMoghyrat3 bigint,
fldCodeMoghyrat bigint,
fldMoghyratKol1 nvarchar(50),
fldMoghyratKol2 nvarchar(50),
fldMoghyratKol3 nvarchar(50),
fldShomareshJozNahei bigint,
fldShomareshKolNahei nvarchar(50),
fldMandeKolKharid decimal(18, 0),
fldMandeKolForoosh decimal(18, 0)
)
--delete from #TempMojoodieAnbar
 create table #TempMoghayratMojoodieAnbar(
	id			bigint,
	fldCodeKala bigint,
	fldCodeAnbar bigint,
	fldShomarshTedadJoz1 bigint,
	fldShomarshTedadJoz2 bigint,
	fldShomarshTedadJoz3 bigint,
	fldMoghyrat1 bigint,
	fldMoghyrat2 bigint,
	fldMoghyrat3 bigint,
	fldShomareshJozNahei bigint
 )

INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeKharid Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJozNahaei, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeTolid where fldType='mahsool' and flddate<=@DateTa



INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzForoosh Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeForoosh Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJozAvalie , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeTolid where fldType='avalie' and flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzKharid Where flddate<=@DateTa


update #TempMojoodieAnbar set fldFeeForoosh=tblKala.fldFeeForoosh from tblKala Where #TempMojoodieAnbar.fldCodeKala=tblKala.fldCodeKala

Insert into #TempMojoodieAnbar
	(fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
		fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',fldCodeGroup, fldCodeKala, fldNameKala ,0,0 , - 1 , - 1 , 0, '' , fldNogheSefaresh, fldFeeForoosh, 
                         0, 0, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,0,''
FROM            tblKala
	where fldCodeKala Not In (Select fldCodeKala from #TempMojoodieAnbar)



UPDATE       #TempMojoodieAnbar
SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
FROM            tblKala INNER JOIN
                         #TempMojoodieAnbar ON tblKala.fldCodeKala = #TempMojoodieAnbar.fldCodeKala


UPDATE       #TempMojoodieAnbar
SET                #TempMojoodieAnbar.fldGroupName = tblGroupKala.fldGroupName
FROM            tblGroupKala INNER JOIN
                         #TempMojoodieAnbar ON #TempMojoodieAnbar.fldCodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempMojoodieAnbar
SET                fldNameAnbar = tblAnbar.fldNameAnbar
FROM            tblAnbar INNER JOIN
                         #TempMojoodieAnbar ON tblAnbar.fldCode = #TempMojoodieAnbar.fldCodeAnbar


update #tempMojoodieAnbar set #tempMojoodieAnbar.fldNameVahed=tbl2.fldNameVahed,#tempMojoodieAnbar.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

where tbl2.fldCodeKala=#tempMojoodieAnbar.fldCodeKala


if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempMojoodieAnbar 
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


			 --update #TempMojoodieAnbar set  fldShomarshTedadJoz=tblMoghayratMojoodieAnbar.fldShomarsheJoz,fldMoghyrat=SUM(fldTedadeVorood - fldTedadeKhorooj)-tblMoghayratMojoodieAnbar.fldShomarsheJoz
				--	from tblMoghayratMojoodieAnbar 
				--	where #TempMojoodieAnbar.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala  and (#TempMojoodieAnbar.fldCodeAnbar IN (@ListCodeAnbar))

--فيلتر تا قبل از تاريخ 

-----------------------beit-mashal-----------------------------------



--update #TempMojoodieAnbar set  fldShomarshTedadJoz=tblMoghayratMojoodieAnbar.fldShomarsheJoz,fldMoghyrat=@SumTedadJoz-tblMoghayratMojoodieAnbar.fldShomarsheJoz
--from tblMoghayratMojoodieAnbar 
--where #TempMojoodieAnbar.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and #TempMojoodieAnbar.fldCodeAnbar=tblMoghayratMojoodieAnbar.fldCodeAnbar


-----------------------beit-mashal-----------------------------------




-----------------------beit-mashal-----------------------------------

 

--update #TempShowMojoodi set  fldShomarshTedadJoz=tblMoghayratMojoodieAnbar.fldShomarsheJoz,fldMoghyrat=SumMande-tblMoghayratMojoodieAnbar.fldShomarsheJoz,fldCountShomaresh=tblMoghayratMojoodieAnbar.fldCountShomaresh
--from tblMoghayratMojoodieAnbar 
--where #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 


-----------------------beit-mashal-----------------------------------

--Insert into #TempShowMojoodi
--	(SumVorood,SumKhorooj,SumMande,fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,fldKharid,fldFeeForoosh,fldShomarshTedadJoz,fldMoghyrat)
--SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, SUM(fldTedadeVorood - fldTedadeKhorooj) AS sumMande, 
--                         fldTedadDarKarton, fldGroupName, fldCodeGroup, #TempMojoodieAnbar.fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,0 ,fldFeeForoosh,fldShomarshTedadJoz,fldMoghyrat
--FROM            #TempMojoodieAnbar inner join
--				#TempMoghayratMojoodieAnbar on #TempMoghayratMojoodieAnbar.fldCodeKala=#TempMojoodieAnbar.fldCodeKala
--GROUP BY #TempMojoodieAnbar.fldCodeKala, fldGroupName, fldCodeGroup, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2, fldTedadDarKarton, fldTedadeKol,fldFeeForoosh

Insert into #TempShowMojoodi
	(SumVorood,SumKhorooj,SumMande,fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,fldKharid,fldFeeForoosh)
SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, SUM(fldTedadeVorood - fldTedadeKhorooj) AS sumMande, 
                         fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,0,fldFeeForoosh
FROM            #TempMojoodieAnbar
GROUP BY fldCodeKala, fldGroupName, fldCodeGroup, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2, fldTedadDarKarton, fldTedadeKol,fldFeeForoosh


-----------------------beit-mashal-----------------------------------

if @ListCodeAnbar <>''
begin
	update #TempShowMojoodi set  fldCodeMoghyrat=id,
								 fldShomarshTedadJoz1=tblMoghayratMojoodieAnbar.fldShomarsheJoz1,
								 fldShomarshTedadJoz2=tblMoghayratMojoodieAnbar.fldShomarsheJoz2,
								 fldShomarshTedadJoz3=tblMoghayratMojoodieAnbar.fldShomarsheJoz3,
								 fldShomareshJozNahei=tblMoghayratMojoodieAnbar.fldShomarsheJozNehai, 
								 fldMoghyrat1=SumMande-tblMoghayratMojoodieAnbar.fldShomarsheJoz1,
								 fldMoghyrat2=SumMande-tblMoghayratMojoodieAnbar.fldShomarsheJoz2,
								 fldMoghyrat3=SumMande-tblMoghayratMojoodieAnbar.fldShomarsheJoz3
		from tblMoghayratMojoodieAnbar 
		where #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and (Convert(varchar,tblMoghayratMojoodieAnbar.fldCodeAnbar) IN (@ListCodeAnbar)) 
		

		update #TempShowMojoodi Set fldShomareTedadKol1=Convert(varchar,floor(fldShomarshTedadJoz1 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz1, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 
		
		where fldTedadDarKarton>0 and fldShomarshTedadJoz1>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and (Convert(varchar,tblMoghayratMojoodieAnbar.fldCodeAnbar) IN (@ListCodeAnbar)) 

		update #TempShowMojoodi Set fldShomareTedadKol2=Convert(varchar,floor(fldShomarshTedadJoz2 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz2, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 
		where fldTedadDarKarton>0 and fldShomarshTedadJoz2>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and (Convert(varchar,tblMoghayratMojoodieAnbar.fldCodeAnbar) IN (@ListCodeAnbar)) 

		update #TempShowMojoodi Set fldShomareTedadKol3=Convert(varchar,floor(fldShomarshTedadJoz3 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz3, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 
		where fldTedadDarKarton>0 and fldShomarshTedadJoz3>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and (Convert(varchar,tblMoghayratMojoodieAnbar.fldCodeAnbar) IN (@ListCodeAnbar)) 
	--fldMoghyratKol1
	update #TempShowMojoodi Set fldMoghyratKol1=Convert(varchar,floor(fldMoghyrat1 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldMoghyrat1, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 	
		where fldTedadDarKarton>0 and fldMoghyrat1>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and (Convert(varchar,tblMoghayratMojoodieAnbar.fldCodeAnbar) IN (@ListCodeAnbar)) 
	--fldMoghyratKol2
		update #TempShowMojoodi Set fldMoghyratKol2=Convert(varchar,floor(fldMoghyrat2 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldMoghyrat2, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 	
		where fldTedadDarKarton>0 and fldMoghyrat2>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and (Convert(varchar,tblMoghayratMojoodieAnbar.fldCodeAnbar) IN (@ListCodeAnbar)) 
	--fldMoghyratKol3
		update #TempShowMojoodi Set fldMoghyratKol3=Convert(varchar,floor(fldMoghyrat3 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldMoghyrat3, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 	
		where fldTedadDarKarton>0 and fldMoghyrat3>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and (Convert(varchar,tblMoghayratMojoodieAnbar.fldCodeAnbar) IN (@ListCodeAnbar)) 
	--fldShomareshKolNahei
		update #TempShowMojoodi Set fldShomareshKolNahei=Convert(varchar,floor(fldShomareshJozNahei / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomareshJozNahei, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 	
		where fldTedadDarKarton>0 and fldShomareshJozNahei>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and (Convert(varchar,tblMoghayratMojoodieAnbar.fldCodeAnbar) IN (@ListCodeAnbar)) 

		end
else
begin
	update #TempShowMojoodi set  fldCodeMoghyrat=id,
								 fldShomarshTedadJoz1=tblMoghayratMojoodieAnbar.fldShomarsheJoz1,
								 fldShomarshTedadJoz2=tblMoghayratMojoodieAnbar.fldShomarsheJoz2,
								 fldShomarshTedadJoz3=tblMoghayratMojoodieAnbar.fldShomarsheJoz3,
								 fldShomareshJozNahei=tblMoghayratMojoodieAnbar.fldShomarsheJozNehai, 
								 fldMoghyrat1=SumMande-tblMoghayratMojoodieAnbar.fldShomarsheJoz1,
								 fldMoghyrat2=SumMande-tblMoghayratMojoodieAnbar.fldShomarsheJoz2,
								 fldMoghyrat3=SumMande-tblMoghayratMojoodieAnbar.fldShomarsheJoz3
		from tblMoghayratMojoodieAnbar 
		where #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 


		update #TempShowMojoodi Set fldShomareTedadKol1=Convert(varchar,floor(fldShomarshTedadJoz1 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz1, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 
		
		where fldTedadDarKarton>0 and fldShomarshTedadJoz1>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 

		update #TempShowMojoodi Set fldShomareTedadKol2=Convert(varchar,floor(fldShomarshTedadJoz2 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz2, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 
		where fldTedadDarKarton>0 and fldShomarshTedadJoz2>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 

		update #TempShowMojoodi Set fldShomareTedadKol3=Convert(varchar,floor(fldShomarshTedadJoz3 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz3, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 
		where fldTedadDarKarton>0 and fldShomarshTedadJoz3>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 
	--fldMoghyratKol1
	update #TempShowMojoodi Set fldMoghyratKol1=Convert(varchar,floor(fldMoghyrat1 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldMoghyrat1, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 	
		where fldTedadDarKarton>0 and fldMoghyrat1>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 
	--fldMoghyratKol2
		update #TempShowMojoodi Set fldMoghyratKol2=Convert(varchar,floor(fldMoghyrat2 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldMoghyrat2, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 	
		where fldTedadDarKarton>0 and fldMoghyrat2>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 
	--fldMoghyratKol3
		update #TempShowMojoodi Set fldMoghyratKol3=Convert(varchar,floor(fldMoghyrat3 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldMoghyrat3, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 	
		where fldTedadDarKarton>0 and fldMoghyrat3>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 
		--fldShomareshKolNahei
		update #TempShowMojoodi Set fldShomareshKolNahei=Convert(varchar,floor(fldShomareshJozNahei / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomareshJozNahei, fldTedadDarKarton) } ) + ' ' + fldNameVahed
		from tblMoghayratMojoodieAnbar 	
		where fldTedadDarKarton>0 and fldShomareshJozNahei>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 

		
end
--	-- پاک کردن دوره انبارگرداني که در گزارش نيستن
--if @fldCountShomaresh<>0
--		begin
--			delete from #TempShowMojoodi 
--		            where fldCountShomaresh=  @fldCountShomaresh
					
--		end

-----------------------beit-mashal-----------------------------------


--update #TempShowMojoodi set fldTedadDarKarton=1
--	where fldTedadDarKarton=0

--تعداد کارتن و تعداد جز در آن
update #TempShowMojoodi set fldNameVahed2='' where fldTedadDarKarton=0

update #TempShowMojoodi Set fldTedadDarKartonShow=Convert(varchar,floor(SumMande / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(SumMande, fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadVoroodKartonShow=Convert(varchar,floor(SumVorood / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(SumVorood, fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadKhoroojKartonShow=Convert(varchar,floor(SumKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Sumkhorooj, fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

-- if @ListCodeAnbar <>''
--begin

--		update #TempShowMojoodi Set fldShomareTedadKol1=Convert(varchar,floor(fldShomarshTedadJoz1 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz1, fldTedadDarKarton) } ) + ' ' + fldNameVahed
--		from tblMoghayratMojoodieAnbar 
		
--		where fldTedadDarKarton>0 and fldShomarshTedadJoz1>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and (Convert(varchar,tblMoghayratMojoodieAnbar.fldCodeAnbar) IN (@ListCodeAnbar)) 

--		update #TempShowMojoodi Set fldShomareTedadKol2=Convert(varchar,floor(fldShomarshTedadJoz2 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz2, fldTedadDarKarton) } ) + ' ' + fldNameVahed
--		from tblMoghayratMojoodieAnbar 
--		where fldTedadDarKarton>0 and fldShomarshTedadJoz2>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and (Convert(varchar,tblMoghayratMojoodieAnbar.fldCodeAnbar) IN (@ListCodeAnbar)) 

--		update #TempShowMojoodi Set fldShomareTedadKol3=Convert(varchar,floor(fldShomarshTedadJoz3 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz3, fldTedadDarKarton) } ) + ' ' + fldNameVahed
--		from tblMoghayratMojoodieAnbar 
--		where fldTedadDarKarton>0 and fldShomarshTedadJoz3>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala and (Convert(varchar,tblMoghayratMojoodieAnbar.fldCodeAnbar) IN (@ListCodeAnbar)) 
	

--		end
--else
--begin
	

--		update #TempShowMojoodi Set fldShomareTedadKol1=Convert(varchar,floor(fldShomarshTedadJoz1 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz1, fldTedadDarKarton) } ) + ' ' + fldNameVahed
--		from tblMoghayratMojoodieAnbar 
		
--		where fldTedadDarKarton>0 and fldShomarshTedadJoz1>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 

--		update #TempShowMojoodi Set fldShomareTedadKol2=Convert(varchar,floor(fldShomarshTedadJoz2 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz2, fldTedadDarKarton) } ) + ' ' + fldNameVahed
--		from tblMoghayratMojoodieAnbar 
--		where fldTedadDarKarton>0 and fldShomarshTedadJoz2>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 

--		update #TempShowMojoodi Set fldShomareTedadKol3=Convert(varchar,floor(fldShomarshTedadJoz3 / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(fldShomarshTedadJoz3, fldTedadDarKarton) } ) + ' ' + fldNameVahed
--		from tblMoghayratMojoodieAnbar 
--		where fldTedadDarKarton>0 and fldShomarshTedadJoz3>0 and #TempShowMojoodi.fldCodeKala=tblMoghayratMojoodieAnbar.fldCodeKala 
	
--end		

set @MiangineKharid=0


--update #TempShowMojoodi set fldKharid=tbl1.Miangin from
--(SELECT        ROUND(SUM(fldFeeKol) / SUM(fldTedadJoz), 0) AS Miangin, fldCodeKala
--FROM            tblRizeKharid
----WHERE        (fldShomareFactor > 0)
--GROUP BY fldCodeKala) as tbl1
--where tbl1.fldCodeKala=#TempShowMojoodi.fldCodeKala

--if @NoeFeeKharid=1 --ميانگين خريد
--	update #TempShowMojoodi set fldKharid=tbl1.Miangin from
--	(SELECT        ROUND(SUM(fldFeeKol) / SUM(fldTedadJoz), 0) AS Miangin, fldCodeKala
--	FROM            tblRizeKharid
--	GROUP BY fldCodeKala
--	having         SUM(fldTedadJoz)>0
--	) as tbl1
--	where tbl1.fldCodeKala=#TempShowMojoodi.fldCodeKala 
--else --آخرين في خريد
--	update #TempShowMojoodi set fldKharid=ROUND((tblRizeKharid.fldFeeKol) / (tblRizeKharid.fldTedadJoz), 0) from
--		tblRizeKharid where  tblRizeKharid.fldCodeKala=#TempShowMojoodi.fldCodeKala 
if @NoeFee=1  --آخرين في خريد
	begin

	update #TempShowMojoodi set fldKharid=ROUND((tblRizeKharid.fldFeeKol) / (tblRizeKharid.fldTedadJoz), 0) from
		tblRizeKharid where tblRizeKharid.fldTedadJoz > 0 AND  tblRizeKharid.fldCodeKala=#TempShowMojoodi.fldCodeKala 

	update #TempShowMojoodi set fldMandeKolKharid= fldKharid *fldShomareshJozNahei from
		tblRizeKharid  where ISNUMERIC(convert(varchar,fldShomareshJozNahei))=1 and  tblRizeKharid.fldCodeKala=#TempShowMojoodi.fldCodeKala 

	

	end
else --آخرين في فروش
	begin
	update #TempShowMojoodi set fldforoosh=ROUND((tblRizeForoosh.fldFeeKol) / (tblRizeForoosh.fldTedadJoz), 0) from
		tblRizeForoosh where  tblRizeForoosh.fldCodeKala=#TempShowMojoodi.fldCodeKala 
	
	update #TempShowMojoodi set fldMandeKolForoosh=fldforoosh * fldShomareshJozNahei from
		tblRizeForoosh where ISNUMERIC(convert(varchar,fldShomareshJozNahei))=1 and  tblRizeForoosh.fldCodeKala=#TempShowMojoodi.fldCodeKala
	end

	

--پاک کردن گروه کالاهايي که در گزارش نيستن
if @ListGroupKala<>''
		   begin
		     declare @StrDeleteGroupKala varchar(5000)
		       set  @StrDeleteGroupKala='delete from #TempShowMojoodi 
		            where '+ @ListGroupKala + ''
					execute(@StrDeleteGroupKala)
					end
--پاک کردن کالاهايي که در گزارش نيستن
if @ListCodeKala<>''
		  begin
		   declare @StrDeleteKala varchar(5000)
		    set  @StrDeleteKala='delete from #TempShowMojoodi 
		            where fldCodeKala NOT IN ('+ @ListCodeKala +')'
					execute(@StrDeleteKala)
		  end
--نقطه سفارش
if @Filter=1 	
	begin
		delete from #TempShowMojoodi Where fldNogheSefaresh < SumMande 
		delete from #TempShowMojoodi Where fldNogheSefaresh = 0 
	end
--موجودي مثبت
if @Filter=2
		delete from #TempShowMojoodi Where  SumMande <=0
		

--موجودي منفي
if @Filter=3
		delete from #TempShowMojoodi Where  SumMande >=0

--موجودي صفر
if @Filter=4
		delete from #TempShowMojoodi Where  SumMande <>0


--محاسبه ارزش موجودي هر کالا
update #TempShowMojoodi set fldArzesheMojoodi=0
update #TempShowMojoodi set fldArzesheMojoodi=SumMande*fldKharid where SumMande>0

--محاسبه ميانگين خريد هرکالا
set @MiangineKharid= (Select Sum(fldKharid*SumMande) as SumMiangin from #TempShowMojoodi Where SumMande>0)


if   @MiangineKharid is null
	set @MiangineKharid=0

	

	

Set @JameTedad=(Select Sum(SumMande)  from #TempShowMojoodi Where SumMande>0)
update #TempShowMojoodi set fldTedadKartonadadSahih=(SumMande/fldTedadDarKarton)   Where fldTedadDarKarton>0
update #TempShowMojoodi set fldTedadKartonadadSahih=0 Where isnumeric(fldTedadKartonadadSahih)=0
set @lngSumTedadeKolSahih=(Select Sum(fldTedadKartonadadSahih) from #TempShowMojoodi)


if   @JameTedad is null
	set @JameTedad=0

			--if @ListGroupAshkhas<>''
		 --     begin
		 --      declare @StrDeleteGroupAshkhas varchar(5000)
		 --       set  @StrDeleteGroupAshkhas='delete from #TempRizeForoosh 
		 --           where '+ @ListGroupAshkhas + ''
			--		execute(@StrDeleteGroupAshkhas)
		 --     end

 declare @StrNoeMoghayerat nvarchar(50)
 declare @StrOrder nvarchar(50)
 declare @StrDoreh nvarchar(50)
 declare @Select nvarchar(500)

 --fldMoghyratKol1

 Set @Select='Select fldCodeKala ,fldNameKala ,SumMande ,fldTedadDarKarton,fldTedadDarKartonShow ,
	fldShomarshTedadJoz1,fldShomareTedadKol1,fldMoghyrat1,fldMoghyratKol1,
	fldShomarshTedadJoz2,fldShomareTedadKol2,fldMoghyrat2,fldMoghyratKol2,
	fldShomarshTedadJoz3,fldShomareTedadKol3,fldMoghyrat3,fldMoghyratKol3,
	fldCodeMoghyrat,fldShomareshJozNahei,fldShomareshKolNahei,fldKharid,fldforoosh,fldMandeKolKharid,fldMandeKolForoosh from #TempShowMojoodi   '


--Select fldCodeKala ,fldNameKala ,SumMande ,fldTedadDarKarton,fldTedadDarKartonShow ,
--	fldShomarshTedadJoz1,fldShomareTedadKol1,fldMoghyrat1,fldMoghyratKol1,
--	fldShomarshTedadJoz2,fldShomareTedadKol2,fldMoghyrat2,fldMoghyratKol2,
--	fldShomarshTedadJoz3,fldShomareTedadKol3,fldMoghyrat3,fldMoghyratKol3,
--	fldCodeMoghyrat,fldShomareshJozNahei,fldShomareshKolNahei,fldKharid,fldforoosh,fldMandeKolKharid,fldMandeKolForoosh from #TempShowMojoodi where fldMoghyrat1=0 and fldMoghyrat2=0 and fldMoghyrat3=0 order by fldCodeKala

--Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
	--fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid ,@strorder
	--from #TempShowMojoodi
--if @NoeMoghayerat=1--صفر
--	set @StrNoeMoghayerat=' where fldMoghyrat1=0 and fldMoghyrat2=0 and fldMoghyrat3=0 '
--else if @NoeMoghayerat=2---مثبت
--	set @StrNoeMoghayerat=' where fldMoghyrat1>0 and fldMoghyrat2>0 and fldMoghyrat3>0 '
--else if  @NoeMoghayerat=3--منفی
--	set @StrNoeMoghayerat=' where fldMoghyrat1<0 and fldMoghyrat2<0 and fldMoghyrat3<0 '



if @Order=0 
	set @StrOrder=' order by fldCodeKala'
else if @order=1 	
	set @StrOrder=' order by fldNameKala'
else if @order=2 
	set @StrOrder=' order by SumMande desc'
else if @order=3 
	set @StrOrder=' order by SumMande asc'
else if @order=4 
	set @StrOrder=' order by fldKharid desc'	
else if @order=5 
	set @StrOrder=' order by fldKharid asc'
else if @Order=6
	set @StrOrder=' order by fldGroupName desc'
else if @Order=7
	set @StrOrder=' order by fldCodeGroup asc'


--else if @fldCountShomaresh=1 	
--	set @StrDoreh=' Where fldCountShomaresh='+ @fldCountShomaresh
--else if @fldCountShomaresh=2 
--	set @StrDoreh=' Where fldCountShomaresh='+ @fldCountShomaresh
--else if @fldCountShomaresh=3 
--	set @StrDoreh=' Where fldCountShomaresh='+ @fldCountShomaresh
	

execute(@select  + @StrOrder  )

--if @Order=0 
--	set @StrOrder='  fldCodeKala'
--else if @order=1 	
--	set @StrOrder='  fldNameKala'
--else if @order=2 
--	set @StrOrder='  SumMande desc'
--else if @order=3 
--	set @StrOrder='  SumMande asc'
--else if @order=4 
--	set @StrOrder='  fldKharid desc'	
--else if @order=5 
--	set @StrOrder='  fldKharid asc'
--else if @Order=6
--	set @StrOrder=' fldGroupName desc'
--else if @Order=7
--	set @StrOrder='  fldCodeGroup asc'

--Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
--	fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid,fldArzesheMojoodi,fldFeeForoosh,fldShomarshTedadJoz,fldMoghyrat from #TempShowMojoodi order by @StrOrder


	 

end
GO
/****** Object:  StoredProcedure [dbo].[z143UpdateMoghyratAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z143UpdateMoghyratAnbar]
@fldCodeMoghyrat bigint,
@fldShomareJoz bigint,
@fldCountShomaresh int,
@fldShomareshJozNahei bigint


as 

begin
	if @fldShomareshJozNahei<>0
	begin
	if @fldCountShomaresh=1
		update tblMoghayratMojoodieAnbar 
		set fldShomarsheJoz1=@fldShomareJoz ,fldShomarsheJozNehai=@fldShomareshJozNahei
		where id=@fldCodeMoghyrat
	--update tblMoghayratMojoodieAnbar set fldShomarsheJoz3=305,fldShomarsheJozNehai=305 where id=23
	else if @fldCountShomaresh=2
		update tblMoghayratMojoodieAnbar 
		set fldShomarsheJoz2=@fldShomareJoz ,fldShomarsheJozNehai=@fldShomareshJozNahei
		where id=@fldCodeMoghyrat
	else if @fldCountShomaresh=3
		update tblMoghayratMojoodieAnbar 
		set fldShomarsheJoz3=@fldShomareJoz ,fldShomarsheJozNehai=@fldShomareshJozNahei
		where id=@fldCodeMoghyrat
end
else
	begin
	if @fldCountShomaresh=1
		update tblMoghayratMojoodieAnbar 
		set fldShomarsheJoz1=@fldShomareJoz ,fldShomarsheJozNehai=0
		where id=@fldCodeMoghyrat
	--update tblMoghayratMojoodieAnbar set fldShomarsheJoz3=305,fldShomarsheJozNehai=305 where id=23
	else if @fldCountShomaresh=2
		update tblMoghayratMojoodieAnbar 
		set fldShomarsheJoz2=@fldShomareJoz ,fldShomarsheJozNehai=0
		where id=@fldCodeMoghyrat
	else if @fldCountShomaresh=3
		update tblMoghayratMojoodieAnbar 
		set fldShomarsheJoz3=@fldShomareJoz ,fldShomarsheJozNehai=0
		where id=@fldCodeMoghyrat
	end

end

GO
/****** Object:  StoredProcedure [dbo].[z144DeleteMoghyratAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z144DeleteMoghyratAnbar]
@strResult int output,
@fldCodeMoghyrat bigint,
@fldCountShomaresh int,
@fldCodeAnbar bigint,
@fldCodeKala bigint,
@Type nvarchar(50)

as 
begin
	declare @MaxRadif int,
			@count1 int,
			@count2 int,
			@ShomareshNahei bigint
	begin transaction AddRecord
		begin try
	if @fldCountShomaresh=1
		begin
		delete from tblMoghayratMojoodieAnbar  where id=@fldCodeMoghyrat

	    set @count1 =(select count(fldRadif) from tblRizeKharid where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat')
		if @count1 > 0
		begin
		delete from tblRizeKharid where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat'
		end
		  set @count2 =(select count(fldRadif) from tblRizeForoosh where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat')
		if @count2 > 0
		begin
		delete from tblRizeForoosh where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat'
		end

		end
	else if @fldCountShomaresh=2
		begin
		

		if @Type='Kharid'
		begin

			set @MaxRadif=(select Max(fldRadif) from tblRizeKharid where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat')
			delete from tblRizeKharid where fldRadif=@MaxRadif and fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat'
		end
		if @Type='Foroosh'
		begin
			set @MaxRadif=(select Max(fldRadif) from tblRizeForoosh where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat')
			delete from tblRizeForoosh where fldRadif=@MaxRadif and fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat'
		end
		set @ShomareshNahei=(select fldShomarsheJoz1 from tblMoghayratMojoodieAnbar where id=@fldCodeMoghyrat)
		update tblMoghayratMojoodieAnbar set fldShomarsheJoz2=null, fldShomarsheJozNehai=@ShomareshNahei where id=@fldCodeMoghyrat
		end
	else if @fldCountShomaresh=3
		begin
		
		
		if @Type='Kharid'
		begin
			set @MaxRadif=(select Max(fldRadif) from tblRizeKharid where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat')
			delete from tblRizeKharid where fldRadif=@MaxRadif and fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat'
		end
		if @Type='Foroosh'
		begin
			set @MaxRadif=(select Max(fldRadif) from tblRizeForoosh where fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat')
			delete from tblRizeForoosh where fldRadif=@MaxRadif and fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar and fldType='Moghayrat'
		end
		set @ShomareshNahei=(select fldShomarsheJoz2 from tblMoghayratMojoodieAnbar where id=@fldCodeMoghyrat)
		update tblMoghayratMojoodieAnbar set fldShomarsheJoz3=null, fldShomarsheJozNehai=@ShomareshNahei where id=@fldCodeMoghyrat
		end

		commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult=1
			
				rollback transaction AddRecord;
			end
		end catch
end

GO
/****** Object:  StoredProcedure [dbo].[z145InsertMoghyratInfoForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z145InsertMoghyratInfoForoosh]





@fldCodeAnbar bigint,
@fldCodeKala bigint,
@fldTedadJoz bigint,
@fldDate bigint,
@fldTime nvarchar(20)

as 

begin
	--begin transaction AddRecord
	--	begin try
		
	declare 
	
		@maxRadif bigint,
		@count int

begin
 set	@count=(select count(fldRadif) from tblRizeForoosh where fldType='Moghayrat'  and fldCodeAnbar=@fldCodeAnbar )

	if @count=0
	begin



insert into tblRizeForoosh(fldShomareSanad,
								   fldShomareFactor,
								   fldCodeVasete,
								   fldCodeTafsili,
								   fldCodeUser,
								   fldRadif,
								   fldCodeAnbar,
								   fldCodeKala,
								   fldSharhKala,
								   fldTedadCarton,
								   fldTedadJoz,
								   fldFeeJoz,
								   fldFeeYekCarton,
								   fldFeeKol,
								   fldDarsadTakhfif,
								   fldMablaghTakhfif,
								   fldFeeVahed1BadAzTakhfif,
								   fldFeeVahed2BadAzTakhfif,
								   fldArzeshAfzode,
								   fldFeeKolBadAzTakhfif,
								   fldMablaghKhales,
								   fldType,
								   fldDate,
								   fldTime,
								   fldShomareEnteghal,
								   fldShomareBargasht,
								   fldDateEngheza)
	values(0,
		   0,
		   0,
		   10000,
		   0,
		   1,
		   @fldCodeAnbar,
		   @fldCodeKala,
		   '',
		   0,
		   @fldTedadJoz,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   'Moghayrat',
		   @fldDate,
		   @fldTime,
		   0,
		   0,
		   0
		   )
 end
 else if @count<>0
 begin 
 set	@maxRadif=(select max(fldRadif) from tblRizeForoosh where fldType='Moghayrat' and fldCodeAnbar=@fldCodeAnbar)
 
insert into tblRizeForoosh(fldShomareSanad,
								   fldShomareFactor,
								   fldCodeVasete,
								   fldCodeTafsili,
								   fldCodeUser,
								   fldRadif,
								   fldCodeAnbar,
								   fldCodeKala,
								   fldSharhKala,
								   fldTedadCarton,
								   fldTedadJoz,
								   fldFeeJoz,
								   fldFeeYekCarton,
								   fldFeeKol,
								   fldDarsadTakhfif,
								   fldMablaghTakhfif,
								   fldFeeVahed1BadAzTakhfif,
								   fldFeeVahed2BadAzTakhfif,
								   fldArzeshAfzode,
								   fldFeeKolBadAzTakhfif,
								   fldMablaghKhales,
								   fldType,
								   fldDate,
								   fldTime,
								   fldShomareEnteghal,
								   fldShomareBargasht,
								   fldDateEngheza)
	values(0,
		   0,
		   0,
		   10000,
		   0,
		   @maxRadif+1,
		   @fldCodeAnbar,
		   @fldCodeKala,
		   '',
		   0,
		   @fldTedadJoz,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   'Moghayrat',
		   @fldDate,
		   @fldTime,
		   0,
		   0,
		   0
		   )
 end






		--commit transaction AddRecord;
		
		--end try
		--begin catch
		--	if @@TranCount>0
		--	begin
		--		set @strResult='error'
		--		set @strResult2='error'
		--		rollback transaction AddRecord;
		--	end
		--end catch
		end

		end

GO
/****** Object:  StoredProcedure [dbo].[z146InsertMoghyratInfoKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z146InsertMoghyratInfoKharid]
@fldCodeAnbar bigint,
@fldCodeKala bigint,
@fldTedadJoz bigint,
@fldDate bigint,
@fldTime nvarchar(20)


as 

begin
	--begin transaction AddRecord
	--	begin try
		
	declare 
		@Count bigint,	
		@maxRadif bigint

	begin
	--select * from tblRizeKharid where fldType='Moghayrat'
	set	@count=(select count(fldRadif) from tblRizeKharid where fldType='Moghayrat' and fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar)
	
if @Count=0
	begin


 insert into tblRizeKharid(fldShomareSanad,
								   fldShomareFactor,
								   fldCodeVasete,
								   fldCodeTafsili,
								   fldCodeUser,
								   fldRadif,
								   fldCodeAnbar,
								   fldCodeKala,
								   fldSharhKala,
								   fldTedadCarton,
								   fldTedadJoz,
								   fldFeeJoz,
								   fldFeeYekCarton,
								   fldFeeKol,
								   fldDarsadTakhfif,
								   fldMablaghTakhfif,
								   fldFeeVahed1BadAzTakhfif,
								   fldFeeVahed2BadAzTakhfif,
								   fldArzeshAfzode,
								   fldFeeKolBadAzTakhfif,
								   fldMablaghKhales,
								   fldType,
								   fldDate,
								   fldTime,
								   fldShomareEnteghal,
								   fldDateEngheza)
	values(0,
		   0,
		   0,
		   10000,
		   0,
		   1,
		   @fldCodeAnbar,
		   @fldCodeKala,
		   '',
		   0,
		   @fldTedadJoz,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   'Moghayrat',
		   @fldDate,
		   @fldTime,
		   0,
		   0
		   )
 end

else if @Count<>0

 begin 
 set	@maxRadif=(select Max(fldRadif) from tblRizeKharid where fldType='Moghayrat' and fldCodeKala=@fldCodeKala and fldCodeAnbar=@fldCodeAnbar)
 
 insert into tblRizeKharid(fldShomareSanad,
								   fldShomareFactor,
								   fldCodeVasete,
								   fldCodeTafsili,
								   fldCodeUser,
								   fldRadif,
								   fldCodeAnbar,
								   fldCodeKala,
								   fldSharhKala,
								   fldTedadCarton,
								   fldTedadJoz,
								   fldFeeJoz,
								   fldFeeYekCarton,
								   fldFeeKol,
								   fldDarsadTakhfif,
								   fldMablaghTakhfif,
								   fldFeeVahed1BadAzTakhfif,
								   fldFeeVahed2BadAzTakhfif,
								   fldArzeshAfzode,
								   fldFeeKolBadAzTakhfif,
								   fldMablaghKhales,
								   fldType,
								   fldDate,
								   fldTime,
								   fldShomareEnteghal,
								   fldDateEngheza)
	values(0,
		   0,
		   0,
		   10000,
		   0,
		   @maxRadif+1,
		   @fldCodeAnbar,
		   @fldCodeKala,
		   '',
		   0,
		   @fldTedadJoz,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   0,
		   'Moghayrat',
		   @fldDate,
		   @fldTime,
		   0,
		   0
		   )
 end




	
end





		
		--end try
		--begin catch
		--	if @@TranCount>0
		--	begin
		--		set @strResult='error'
		--		set @strResult2='error'
		--		rollback transaction AddRecord;
		--	end
		--end catch
		end

GO
/****** Object:  StoredProcedure [dbo].[z147ShowListShomarshAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[z147ShowListShomarshAnbar]

  -- @MiangineKharid bigint output,
  --@JameTedad bigint output,
  --@lngSumTedadeKolSahih bigint output,

  @ListCodeKala varchar(5000)=null,
  @ListCodeAnbar varchar(5000)=null,
  @ListGroupKala varchar(5000)=null,
  @DateTa bigint



as

IF 1=0 BEGIN
 SET FMTONLY OFF
END

begin
 

create table #TempMojoodieAnbar(

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18, 0),
fldTedadeKhorooj decimal(18, 0),
fldTedadeJoz decimal(18, 0), 
fldTedadeKol decimal(18, 0), 
fldTedadDarKarton decimal(18, 0),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18, 0),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20) COLLATE Persian_100_CI_AS
)


create table #TempShowMojoodi(

fldCodeKala bigint,
SumVorood decimal(18, 0),
SumKhorooj decimal(18, 0),
SumMande decimal(18, 0),
fldTedadDarKarton decimal(18, 0),
fldTedadKartonadadSahih decimal(18, 0),
fldTedadDarKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,
fldCodeAnbar int,
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldTedadVoroodKarton decimal(18, 0),
fldTedadVoroodKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,
fldTedadKhoroojKarton decimal(18, 0),
fldTedadKhoroojKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,
fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh decimal(18, 0),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldKharid bigint,
fldArzesheMojoodi bigint,
fldFeeForoosh bigint
)



INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeKharid Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJozNahaei, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeTolid where fldType='mahsool' and flddate<=@DateTa



INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzForoosh Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeForoosh Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJozAvalie , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeTolid where fldType='avalie' and flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzKharid Where flddate<=@DateTa



update #TempMojoodieAnbar set fldFeeForoosh=tblKala.fldFeeForoosh from tblKala Where #TempMojoodieAnbar.fldCodeKala=tblKala.fldCodeKala

Insert into #TempMojoodieAnbar
	(fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
		fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',fldCodeGroup, fldCodeKala, fldNameKala ,0,0 , - 1 , - 1 , 0, '' , fldNogheSefaresh, fldFeeForoosh, 
                         0, 0, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,0,''
FROM            tblKala
	where fldCodeKala Not In (Select fldCodeKala from #TempMojoodieAnbar)



UPDATE       #TempMojoodieAnbar
SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
FROM            tblKala INNER JOIN
                         #TempMojoodieAnbar ON tblKala.fldCodeKala = #TempMojoodieAnbar.fldCodeKala


UPDATE       #TempMojoodieAnbar
SET                #TempMojoodieAnbar.fldGroupName = tblGroupKala.fldGroupName
FROM            tblGroupKala INNER JOIN
                         #TempMojoodieAnbar ON #TempMojoodieAnbar.fldCodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempMojoodieAnbar
SET                fldNameAnbar = tblAnbar.fldNameAnbar
FROM            tblAnbar INNER JOIN
                         #TempMojoodieAnbar ON tblAnbar.fldCode = #TempMojoodieAnbar.fldCodeAnbar


update #tempMojoodieAnbar set #tempMojoodieAnbar.fldNameVahed=tbl2.fldNameVahed,#tempMojoodieAnbar.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

where tbl2.fldCodeKala=#tempMojoodieAnbar.fldCodeKala






Insert into #TempShowMojoodi
	(SumVorood,SumKhorooj,SumMande,fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,fldKharid,fldFeeForoosh,fldCodeAnbar,fldNameAnbar)
SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, SUM(fldTedadeVorood - fldTedadeKhorooj) AS sumMande, 
                         fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2,0,fldFeeForoosh,fldCodeAnbar,fldNameAnbar
FROM            #TempMojoodieAnbar
GROUP BY fldCodeKala, fldGroupName, fldCodeGroup, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2, fldTedadDarKarton, fldTedadeKol,fldFeeForoosh,fldCodeAnbar,fldNameAnbar





--update #TempShowMojoodi set fldTedadDarKarton=1
--	where fldTedadDarKarton=0

--تعداد کارتن و تعداد جز در آن
update #TempShowMojoodi set fldNameVahed2='' where fldTedadDarKarton=0

update #TempShowMojoodi Set fldTedadDarKartonShow=Convert(varchar,floor(SumMande / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(SumMande, fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadVoroodKartonShow=Convert(varchar,floor(SumVorood / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(SumVorood, fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadKhoroojKartonShow=Convert(varchar,floor(SumKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 +  Convert(varchar, { fn MOD(Sumkhorooj, fldTedadDarKarton) } ) + ' ' + fldNameVahed
	where fldTedadDarKarton>0





--update #TempShowMojoodi set fldKharid=tbl1.Miangin from
--(SELECT        ROUND(SUM(fldFeeKol) / SUM(fldTedadJoz), 0) AS Miangin, fldCodeKala
--FROM            tblRizeKharid
----WHERE        (fldShomareFactor > 0)
--GROUP BY fldCodeKala) as tbl1
--where tbl1.fldCodeKala=#TempShowMojoodi.fldCodeKala



if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempShowMojoodi 
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


--فیلتر تا قبل از تاریخ 

--پاک کردن گروه کالاهایی که در گزارش نیستن
if @ListGroupKala<>''
		   begin
		     declare @StrDeleteGroupKala varchar(5000)
		       set  @StrDeleteGroupKala='delete from #TempShowMojoodi 
		            where '+ @ListGroupKala + ''
					execute(@StrDeleteGroupKala)
					end
--پاک کردن کالاهایی که در گزارش نیستن
if @ListCodeKala<>''
		  begin
		   declare @StrDeleteKala varchar(5000)
		    set  @StrDeleteKala='delete from #TempShowMojoodi 
		            where fldCodeKala NOT IN ('+ @ListCodeKala +')'
					execute(@StrDeleteKala)
		  end
	


 



Select fldCodeKala ,fldNameKala,fldCodeAnbar,fldNameAnbar,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
	  fldCodeGroup,fldGroupName from #TempShowMojoodi





	 

end


 

GO
/****** Object:  StoredProcedure [dbo].[z148ShowList_ReportSarJameFoRoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[z148ShowList_ReportSarJameFoRoosh]
--چندزبانه
	@language nvarchar(10),
	@userCode bigint,
	@ListCodeKala varchar(5000)=null,
	@ListGroupKala varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListCodeAshkhas varchar(5000)=null,
	@ListCodeVisitor varchar(5000)=null,
	@ListCodeAnbar varchar(5000)=null,
	@DataType varchar(40),
	@DateFrom varchar(8),
	@DateTo varchar(8),
	@FlagSort Varchar(5)
as
   
	
BEGIN
	
	SET NOCOUNT ON;


-- ----------------------------------------
-- -----------------------------------------TempTable In Below Code

IF OBJECT_ID('tempdb..#TempRizeForoosh') IS NOT NULL
 begin
	delete from  #TempRizeForoosh
	delete from  #TempShow
 end
else 
begin
create table #TempRizeForoosh(

fldShomareSanad Nvarchar(30) COLLATE Persian_100_CI_AS,
fldShomareFactor Nvarchar(30) COLLATE Persian_100_CI_AS,
fldCodeTafsili Nvarchar(30) COLLATE Persian_100_CI_AS,
fldSharheTafzili Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldCodeAnbar Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldCodeKala bigint,
fldNameKala Nvarchar(200) COLLATE Persian_100_CI_AS,
fldGhymatMasrafkonande decimal(18,4),
fldSharhKala Nvarchar(250) COLLATE Persian_100_CI_AS,
fldTedadCarton Nvarchar(100) COLLATE Persian_100_CI_AS, 
fldTedadJoz decimal(18,4),
fldFeeJoz decimal(18,4), 
fldFeeYekCarton decimal(18,4),
fldFeeKol decimal(18,4), 
fldFeeVahed1BadAzTakhfif decimal(18,4),
fldFeeVahed2BadAzTakhfif decimal(18,4), 
fldArzeshAfzode decimal(18,4), 
fldFeeKolBadAzTakhfif decimal(18,4), 
fldMablaghKhales decimal(18,4), 
fldType Nvarchar(50) COLLATE Persian_100_CI_AS, 
fldDate int, 
fldGroupId Nvarchar(30) COLLATE Persian_100_CI_AS,
fldGroupName Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeVasete Nvarchar(30) COLLATE Persian_100_CI_AS,
fldNameVasete Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldGroupNameAshkhas Nvarchar(200) COLLATE Persian_100_CI_AS,
fldTedadJozKarton decimal(18,4),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS
)



--fldCodeKala ,
--fldNameKala ,
--fldNameVahed,
--fldNameVahed2,
--fldTedadJoz,
--fldTedadJozKarton,
--fldTedadCarton,
--fldFeeJoz,
--fldFeeKol,
--fldFeeYekCarton,
--fldDarsadTakhfif,
--fldMablaghTakhfif,
--fldFeeVahed1BadAzTakhfif,
--fldFeeVahed2BadAzTakhfif,
--fldFeeKolBadAzTakhfif,
--fldArzeshAfzode,
--fldGhymatMasrafkonande, 
--fldGroupId,
--fldGroupName, 
--fldCodeAnbar, 
--fldNameAnbar,
--fldCodeTafsili,
--fldSharheTafzili

create table #TempShow(

fldCodeKala bigint,
fldNameKala Nvarchar(200) COLLATE Persian_100_CI_AS,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadJoz  decimal(18,4),
fldTedadJozKarton decimal(18,4),
fldTedadCarton Nvarchar(100) COLLATE Persian_100_CI_AS,
fldFeeJoz decimal(18,4),
fldFeeYekCarton decimal(18,4), 
fldFeeVahed1BadAzTakhfif decimal(18,4),
fldFeeVahed2BadAzTakhfif decimal(18,4), 
fldFeeKolBadAzTakhfif decimal(18,4), 
fldArzeshAfzode decimal(18,4), 
fldGhymatMasrafkonande decimal(18,4),
fldGroupId Nvarchar(30) COLLATE Persian_100_CI_AS,
fldGroupName Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeAnbar Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS,
fldSharhKala Nvarchar(250) COLLATE Persian_100_CI_AS
)

end


	--declare @ListCodeKala varchar(5000)='',@ListGroupKala varchar(5000)='',@ListGroupAshkhas varchar(5000)='',@ListCodeAshkhas varchar(5000)='',@ListCodeVisitor varchar(5000)='',@ListCodeAnbar varchar(5000)='', @DateFrom varchar(8)='0',@DateTo varchar(8)='99999999', @FlagSort Varchar(5)='0'

update         tblRizeForoosh
set fldFeeVahed1BadAzTakhfif=fldMablaghKhales/fldTedadJoz 
WHERE        ((CONVERT(decimal(18,4), fldMablaghKhales)- CONVERT(decimal(18,4), fldArzeshAfzode))<> fldTedadJoz * fldFeeVahed1BadAzTakhfif)


UPDATE       tblRizeBargashtAzForoosh
SET                fldFeeVahed1BadAzTakhfif = fldMablaghKhales / fldTedadJoz
WHERE         ((CONVERT(decimal(18,4), fldMablaghKhales)- CONVERT(decimal(18,4), fldArzeshAfzode))<> fldTedadJoz * fldFeeVahed1BadAzTakhfif)


-- *************************************************************
-- *******Access Kala Limitation********************************
-- *************************************************************
declare @accessKala table(code bigint)
insert into @accessKala(code)
--exec ZSP_GetKalaAccesses @userCode
exec [ZSP_GetAccessableGroupKalaCode] @userCode
-- *************************************************************

-- *************************************************************
-- ******Group Ashkhas Limitation*******************************
-- *************************************************************
declare @accessAshkhasCodes table(code bigint)
insert into @accessAshkhasCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- *************************************************************


INSERT INTO #TempRizeForoosh(fldShomareSanad ,
fldShomareFactor ,
fldCodeTafsili ,
fldSharheTafzili , 
fldCodeAnbar , 
fldNameAnbar , 
fldCodeKala ,
fldNameKala ,
fldGhymatMasrafkonande,
fldSharhKala ,
fldTedadCarton , 
fldTedadJoz ,
fldFeeJoz , 
fldFeeYekCarton ,
fldFeeKol , 
fldFeeVahed1BadAzTakhfif ,
fldFeeVahed2BadAzTakhfif , 
fldArzeshAfzode , 
fldFeeKolBadAzTakhfif , 
fldMablaghKhales , 
fldType, 
fldDate, 
fldGroupId,
fldGroupName, 
fldCodeVasete,
fldNameVasete,
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldTedadJozKarton,
fldNameVahed,
fldNameVahed2)




SELECT        dbo.tblRizeForoosh.fldShomareSanad, dbo.tblRizeForoosh.fldShomareFactor, dbo.tblRizeForoosh.fldCodeTafsili, tblTafzili_1.fldSharheTafzili, 
                         dbo.tblRizeForoosh.fldCodeAnbar, dbo.tblAnbar.fldNameAnbar, dbo.tblRizeForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblKala.fldGhymatMasrafkonande, 
                         dbo.tblRizeForoosh.fldSharhKala, dbo.tblRizeForoosh.fldTedadCarton,ABS(dbo.tblRizeForoosh.fldTedadJoz), dbo.tblRizeForoosh.fldFeeJoz, 
                         dbo.tblRizeForoosh.fldFeeYekCarton, dbo.tblRizeForoosh.fldFeeKol, 
                         dbo.tblRizeForoosh.fldFeeVahed1BadAzTakhfif, dbo.tblRizeForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeForoosh.fldArzeshAfzode, 
                         dbo.tblRizeForoosh.fldFeeKolBadAzTakhfif, dbo.tblRizeForoosh.fldMablaghKhales, dbo.tblRizeForoosh.fldType, dbo.tblRizeForoosh.fldDate, 
                         dbo.tblGroupKala.fldGroupId, dbo.tblGroupKala.fldGroupName, dbo.tblRizeForoosh.fldCodeVasete, dbo.tblTafzili.fldSharheTafzili AS fldNameVasete, 
                         dbo.tblGroupAshkhas.fldGroupId AS fldCodeGroupAshkhas, dbo.tblGroupAshkhas.fldGroupName AS fldGroupNameAshkhas, 
                         dbo.tblKala.fldTedadJoz AS fldTedadJozKarton, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2
FROM            dbo.tblVahed INNER JOIN
                         dbo.tblRizeForoosh INNER JOIN
                         dbo.tblAnbar ON dbo.tblRizeForoosh.fldCodeAnbar = dbo.tblAnbar.fldCode INNER JOIN
                         dbo.tblKala ON dbo.tblRizeForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId ON dbo.tblVahed.ID = dbo.tblKala.fldCodeVahed1 INNER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID LEFT OUTER JOIN
                         dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili AS tblTafzili_1 ON dbo.tblGroupAshkhas.fldGroupId = tblTafzili_1.fldCodeGroup ON 
                         dbo.tblRizeForoosh.fldCodeTafsili = tblTafzili_1.fldCodeTafzili LEFT OUTER JOIN
                         dbo.tblTafzili ON dbo.tblRizeForoosh.fldCodeVasete = dbo.tblTafzili.fldCodeTafzili LEFT OUTER JOIN
                         dbo.tblGroupAshkhas AS tblGroupAshkhas_1 ON dbo.tblRizeForoosh.fldCodeVasete = tblGroupAshkhas_1.fldGroupId

						 where 
-- add kala access limitation
tblKala.fldCodeGroup in (select code from @accesskala) and
-- Group Ashkhas Limitation
fldCodeTafsili in (select code from @accessAshkhasCodes) and
						 dbo.tblRizeForoosh.fldType='Foroosh'  and dbo.tblRizeForoosh.fldDate  BETWEEN  @DateFrom  AND  @DateTo

INSERT INTO #TempRizeForoosh(fldShomareSanad ,
fldShomareFactor ,
fldCodeTafsili ,
fldSharheTafzili , 
fldCodeAnbar , 
fldNameAnbar , 
fldCodeKala ,
fldNameKala ,
fldGhymatMasrafkonande,
fldSharhKala ,
fldTedadCarton , 
fldTedadJoz ,
fldFeeJoz , 
fldFeeYekCarton ,
fldFeeKol , 
fldFeeVahed1BadAzTakhfif ,
fldFeeVahed2BadAzTakhfif , 
fldArzeshAfzode , 
fldFeeKolBadAzTakhfif , 
fldMablaghKhales , 
fldType, 
fldDate, 
fldGroupId,
fldGroupName, 
fldCodeVasete,
fldNameVasete,
fldCodeGroupAshkhas,
fldGroupNameAshkhas,
fldTedadJozKarton,
fldNameVahed,
fldNameVahed2)



SELECT        dbo.tblRizeBargashtAzForoosh.fldShomareSanad, dbo.tblRizeBargashtAzForoosh.fldShomareFactor, dbo.tblRizeBargashtAzForoosh.fldCodeTafsili, tblTafzili_1.fldSharheTafzili, 
                         dbo.tblRizeBargashtAzForoosh.fldCodeAnbar, dbo.tblAnbar.fldNameAnbar, dbo.tblRizeBargashtAzForoosh.fldCodeKala, dbo.tblKala.fldNameKala, dbo.tblKala.fldGhymatMasrafkonande, 
                         dbo.tblRizeBargashtAzForoosh.fldSharhKala, dbo.tblRizeBargashtAzForoosh.fldTedadCarton,-1*ABS(dbo.tblRizeBargashtAzForoosh.fldTedadJoz), dbo.tblRizeBargashtAzForoosh.fldFeeJoz, 
                         dbo.tblRizeBargashtAzForoosh.fldFeeYekCarton, dbo.tblRizeBargashtAzForoosh.fldFeeKol, 
                         dbo.tblRizeBargashtAzForoosh.fldFeeVahed1BadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldFeeVahed2BadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldArzeshAfzode, 
                         dbo.tblRizeBargashtAzForoosh.fldFeeKolBadAzTakhfif, dbo.tblRizeBargashtAzForoosh.fldMablaghKhales, dbo.tblRizeBargashtAzForoosh.fldType, dbo.tblRizeBargashtAzForoosh.fldDate, 
                         dbo.tblGroupKala.fldGroupId, dbo.tblGroupKala.fldGroupName, dbo.tblRizeBargashtAzForoosh.fldCodeVasete, dbo.tblTafzili.fldSharheTafzili AS fldNameVasete, 
                         dbo.tblGroupAshkhas.fldGroupId AS fldCodeGroupAshkhas, dbo.tblGroupAshkhas.fldGroupName AS fldGroupNameAshkhas, 
                         dbo.tblKala.fldTedadJoz AS fldTedadJozKarton, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2
FROM            dbo.tblVahed INNER JOIN
                         dbo.tblRizeBargashtAzForoosh INNER JOIN
                         dbo.tblAnbar ON dbo.tblRizeBargashtAzForoosh.fldCodeAnbar = dbo.tblAnbar.fldCode INNER JOIN
                         dbo.tblKala ON dbo.tblRizeBargashtAzForoosh.fldCodeKala = dbo.tblKala.fldCodeKala INNER JOIN
                         dbo.tblGroupKala ON dbo.tblKala.fldCodeGroup = dbo.tblGroupKala.fldGroupId ON dbo.tblVahed.ID = dbo.tblKala.fldCodeVahed1 INNER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID LEFT OUTER JOIN
                         dbo.tblGroupAshkhas INNER JOIN
                         dbo.tblTafzili AS tblTafzili_1 ON dbo.tblGroupAshkhas.fldGroupId = tblTafzili_1.fldCodeGroup ON 
                         dbo.tblRizeBargashtAzForoosh.fldCodeTafsili = tblTafzili_1.fldCodeTafzili LEFT OUTER JOIN
                         dbo.tblTafzili ON dbo.tblRizeBargashtAzForoosh.fldCodeVasete = dbo.tblTafzili.fldCodeTafzili LEFT OUTER JOIN
                         dbo.tblGroupAshkhas AS tblGroupAshkhas_1 ON dbo.tblRizeBargashtAzForoosh.fldCodeVasete = tblGroupAshkhas_1.fldGroupId

						 where  
						 
						 -- add kala access limitation
tblKala.fldCodeGroup in (select code from @accesskala) and
-- Group Ashkhas Limitation
fldCodeTafsili in (select code from @accessAshkhasCodes) and
						 
						 dbo.tblRizeBargashtAzForoosh.fldType='BargashtAzForoosh'   ANd dbo.tblRizeBargashtAzForoosh.fldDate   BETWEEN  @DateFrom  AND  @DateTo

--SELECT sUM(fldFeeVahed1BadAzTakhfif*fldTedadJoz) from #TempRizeForoosh

         if @ListCodeKala<>''
		  begin
		   declare @StrDeleteKala varchar(5000)
		    set  @StrDeleteKala='delete from #TempRizeForoosh 
		            where fldCodeKala NOT IN ('+ @ListCodeKala +')'
					execute(@StrDeleteKala)
		  end

		  if @ListGroupKala<>''
		   begin
		     declare @StrDeleteGroupKala varchar(5000)
		       set  @StrDeleteGroupKala='delete from #TempRizeForoosh 
		            where '+ @ListGroupKala + ''
					execute(@StrDeleteGroupKala)
		   end


		   if @ListCodeAshkhas <>''
		    begin
		     declare @StrDeleteAshkhas varchar(5000)
		      set  @StrDeleteAshkhas='delete from #TempRizeForoosh 
		            where fldCodeTafsili NOT IN ('+ @ListCodeAshkhas +')'
					execute(@StrDeleteAshkhas)
		    end


			if @ListGroupAshkhas<>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempRizeForoosh 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end

			if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempRizeForoosh 
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


			if @ListCodeVisitor<>''
		     begin
		      declare @StrDeleteVisitor varchar(5000)
		       set  @StrDeleteVisitor='delete from #TempRizeForoosh 
		            where '+ @ListCodeVisitor + ''
					execute(@StrDeleteVisitor)
		     end


--update #TempRizeForoosh Set fldTedadJozKarton=Convert(varchar,floor( sum(convert(dec,fldFeeJoz)) / fldTedadJozKarton))  +  Convert(varchar, { fn MOD(Convert(dec,  sum(convert(dec,fldFeeJoz))), fldTedadJozKarton) } ) 
--	where fldTedadJozKarton>0

	
--if @FlagSort='0'
-- begin
-- select  

-- sum(convert(dec,fldTedadJoz)) as SumTedad, 
-- fldCodeKala,
--fldNameKala,
--case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
--case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
--fldTedadJozKarton,
--fldFeeJoz,
--fldFeeYekCarton,
--fldDarsadTakhfif, 
--fldMablaghTakhfif,
--fldFeeVahed1BadAzTakhfif,
--fldFeeVahed2BadAzTakhfif, 
--fldArzeshAfzode, 
--fldGhymatMasrafkonande, 
--fldGroupId,
--fldGroupName, 
--fldCodeAnbar, 
--fldNameAnbar

----sum(convert(dec,fldFeeKol)),

--From #TempRizeForoosh 
--group by fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldTedadJozKarton,fldFeeJoz,fldFeeYekCarton,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldGhymatMasrafkonande,fldGroupId,fldGroupName,fldCodeAnbar,fldNameAnbar


--	ORDER BY fldDate,Convert(dec,fldShomareFactor) ASC
-- end
 

-- --fldShomareFactor,fldCodeTafsili,fldSharheTafzili,fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldTedadCarton,fldTedadJozKarton,fldFeeYekCarton,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldFeeKolBadAzTakhfif,fldArzeshAfzode,fldGhymatMasrafkonande,fldMablaghKhales,fldCodeGroupAshkhas,fldGroupNameAshkhas,fldGroupId,fldGroupName,fldCodeAnbar,fldNameAnbar,fldCodeVasete,fldNameVasete,fldDate,fldSharhKala,fldType
-- -- group by fldShomareSanad,fldShomareFactor,fldCodeTafsili,fldSharheTafzili,fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldTedadCarton,fldTedadJozKarton,fldFeeYekCarton,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldFeeKolBadAzTakhfif,fldArzeshAfzode,fldGhymatMasrafkonande,fldMablaghKhales,fldCodeGroupAshkhas,fldGroupNameAshkhas,fldGroupId,fldGroupName,fldCodeAnbar,fldNameAnbar,fldCodeVasete,fldNameVasete,fldDate,fldSharhKala,fldType

 	
--if @FlagSort='1'
-- begin
-- select  
-- sum(convert(dec,fldTedadJoz)) as SumTedad, 
-- fldCodeKala,
--fldNameKala,
--case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
--case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
--fldTedadJozKarton,
--fldFeeJoz,
--fldFeeYekCarton,
--fldDarsadTakhfif, 
--fldMablaghTakhfif,
--fldFeeVahed1BadAzTakhfif,
--fldFeeVahed2BadAzTakhfif, 
--fldArzeshAfzode, 
--fldGhymatMasrafkonande, 
--fldGroupId,
--fldGroupName, 
--fldCodeAnbar, 
--fldNameAnbar
----sum(convert(dec,fldFeeKol)),

--From #TempRizeForoosh 
--group by fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldTedadJozKarton,fldFeeJoz,fldFeeYekCarton,fldDarsadTakhfif,fldMablaghTakhfif,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldGhymatMasrafkonande,fldGroupId,fldGroupName,fldCodeAnbar,fldNameAnbar

--	ORDER BY Convert(dec,fldShomareSanad),Convert(dec,fldShomareFactor) ASC
-- end
 --declare @TedadCarton varchar
 --set @TedadCarton=( select Convert(varchar, convert(int,sum(convert(decimal,fldTedadJoz)) / convert(decimal,fldTedadJozKarton)))  +'-' + Convert(varchar, { fn MOD(Convert(dec, sum(convert(dec,fldTedadJoz))), convert(dec,fldTedadJozKarton)) } ) as fldTedadCarton
	--               from #TempRizeForoosh where fldTedadJozKarton>0 group by fldFeeJoz,fldTedadJozKarton)
	-- Convert(varchar, convert(int,sum(convert(decimal,fldTedadJoz)) / convert(decimal,fldTedadJozKarton)))  +'-' + Convert(varchar, { fn MOD(Convert(dec, sum(convert(dec,fldTedadJoz))), convert(dec,fldTedadJozKarton)) } ) as fldTedadCarton,
			   
--marashi
if @FlagSort='0'
 begin

-- select Sum(Convert(float,fldTedadJoz)*Convert(float,fldFeeVahed1BadAzTakhfif)) as Sss from #TempShow

  INSERT INTO #TempShow(
fldTedadJoz,
fldCodeKala ,
fldNameKala ,
fldNameVahed,
fldNameVahed2,
fldTedadJozKarton,
fldTedadCarton,
fldFeeJoz,
fldFeeYekCarton,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif,
fldArzeshAfzode,
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar,
fldSharhKala
)


  select  
  sum(convert(decimal(18,4),fldTedadJoz)) as fldTedadJoz, 
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadJozKarton,
'0' as fldTedadCarton,
 Convert(decimal(18,4),fldFeeJoz) as fldFeeJoz,  
fldFeeYekCarton,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldArzeshAfzode, 
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar,
fldSharhKala
--sum(convert(dec,fldFeeKol)),

From #TempRizeForoosh 
group by fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldTedadJozKarton, Convert(decimal(18,4),fldFeeJoz),fldFeeYekCarton,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldGroupId,fldGroupName,fldCodeAnbar,fldNameAnbar
,fldSharhKala
	ORDER BY fldCodeKala ASC


--marashi
 end
 



if @FlagSort='1'
 begin

 INSERT INTO #TempShow(
fldTedadJoz,
fldCodeKala ,
fldNameKala ,
fldNameVahed,
fldNameVahed2,
fldTedadJozKarton,
fldTedadCarton,
fldFeeJoz,
fldFeeYekCarton,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif,
fldArzeshAfzode,
fldGhymatMasrafkonande, 
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar,
fldSharhKala
)


 select  
  sum(convert(decimal(18,4),fldTedadJoz)) as fldTedadJoz, 
fldCodeKala,
fldNameKala,
case when fldNameVahed=N'انتخاب کنید' then '' else fldNameVahed end as fldNameVahed,
case when fldNameVahed2=N'انتخاب کنید' then '' else fldNameVahed2 end as fldNameVahed2,
fldTedadJozKarton,
'0' as fldTedadCarton,
 Convert(decimal(18,4),fldFeeJoz) as fldFeeJoz,  
fldFeeYekCarton,
fldFeeVahed1BadAzTakhfif,
fldFeeVahed2BadAzTakhfif, 
fldArzeshAfzode, 
fldGhymatMasrafkonande, 
fldGroupId,
fldGroupName, 
fldCodeAnbar, 
fldNameAnbar,
fldSharhKala
--sum(convert(dec,fldFeeKol)),

From #TempRizeForoosh 
group by fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldTedadJozKarton, Convert(decimal(18,4),fldFeeJoz),fldFeeYekCarton,fldFeeVahed1BadAzTakhfif,fldFeeVahed2BadAzTakhfif,fldArzeshAfzode,fldGhymatMasrafkonande,fldGroupId,fldGroupName,fldCodeAnbar,fldNameAnbar
,fldSharhKala
	ORDER BY fldNameKala ASC
 end
 

 


 --select fldFeeJoz from #TempShow order by fldFeeJoz
--marashi
update #TempShow set fldFeeKolBadAzTakhfif=fldTedadJoz*fldFeeJoz
--

update #TempShow Set fldTedadCarton=Convert(varchar,floor(convert(decimal(18,4),fldTedadJoz) / convert(decimal(18,4),fldTedadJozKarton))) + '-' + REPLACE( Convert(varchar, { fn MOD(Convert(decimal(18,4), fldTedadJoz), convert(decimal(18,4),fldTedadJozKarton)) } ),'.0000','') 
where fldTedadJozKarton>0


update #TempShow Set fldFeeKolBadAzTakhfif=fldTedadJoz*Convert(decimal(18,4),fldFeeVahed1BadAzTakhfif)

update #TempShow set #TempShow.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#TempShow.fldCodeKala and tblkalatranslate.fldLanguage=@Language

if (@userCode >= 0 )
begin

select fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldTedadJoz,fldTedadJozKarton,fldTedadCarton,Convert(decimal(18,4),fldFeeJoz) as fldFeeJoz,Convert(decimal(18,4),fldFeeYekCarton) as fldFeeYekCarton,Convert(decimal(18,4),fldFeeVahed1BadAzTakhfif) as fldFeeVahed1BadAzTakhfif,
Convert(decimal(18,4),fldFeeVahed2BadAzTakhfif) as fldFeeVahed2BadAzTakhfif,Convert(decimal(18,4),fldFeeKolBadAzTakhfif) as fldFeeKolBadAzTakhfif,Convert(decimal(18,4),fldArzeshAfzode) as fldArzeshAfzode,Convert(decimal(18,4),fldGhymatMasrafkonande) as fldGhymatMasrafkonande,fldGroupId,fldGroupName,fldCodeAnbar,fldNameAnbar,fldSharhKala 
from #TempShow where fldTedadJoz<>0
--select Sum(Convert(float,fldTedadJoz)*Convert(float,fldFeeVahed1BadAzTakhfif)) as Sss from #TempShow

end
else begin

declare @accesscodes table(code bigint)

insert into @accesscodes(code)  
exec ZSP_GetKalaAccesses @userCode



select fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,fldTedadJoz,fldTedadJozKarton,fldTedadCarton,Convert(decimal(18,4),fldFeeJoz) as fldFeeJoz,Convert(decimal(18,4),fldFeeYekCarton) as fldFeeYekCarton,Convert(decimal(18,4),fldFeeVahed1BadAzTakhfif) as fldFeeVahed1BadAzTakhfif,
Convert(decimal(18,4),fldFeeVahed2BadAzTakhfif) as fldFeeVahed2BadAzTakhfif,Convert(decimal(18,4),fldFeeKolBadAzTakhfif) as fldFeeKolBadAzTakhfif,Convert(decimal(18,4),fldArzeshAfzode) as fldArzeshAfzode,Convert(decimal(18,4),fldGhymatMasrafkonande) as fldGhymatMasrafkonande,fldGroupId,fldGroupName,fldCodeAnbar,fldNameAnbar,fldSharhKala 
from #TempShow where fldTedadJoz<>0 and fldGroupId in (select code from @accesscodes)
end




END

GO
/****** Object:  StoredProcedure [dbo].[z149ShowList_ReportAmalKardMoshtari]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[z149ShowList_ReportAmalKardMoshtari]

	@ListGroupAshkhas varchar(5000)=null,
	@ListCodeAshkhas varchar(5000)=null,
	@ListCodeAnbar varchar(5000)=null
as
   
	
BEGIN
	
	SET NOCOUNT ON;


-- ----------------------------------------
-- -----------------------------------------TempTable In Below Code

IF OBJECT_ID('tempdb..#TempRizeAmalKardMoshtari') IS NOT NULL
 begin
	delete from     #TempRizeAmalKardMoshtari
	delete from     #TempShowAmalKardMoshtari
	delete from     #TempSumForoosh
	delete from     #TempSumBargashtAzForoosh		
	delete from 	#TempSumCheckDaryafti
	delete from 	#TempSumCheckBargashti
	delete from 	#TempSumCheckVosoolShode
	delete from 	#TempSumCheckVosoolShodeNaShode
	delete from 	#TempSumCheckPardakhti
	delete from     #TempGroupAshkhas
	delete from     #TempAnbar
 end
else 
--Nvarchar(30) COLLATE Persian_100_CI_AS,
begin

create table #TempGroupAshkhas(
fldCodeTafzili bigint,
fldCodeGroup bigint, 
fldGroupName nvarchar(50)  COLLATE Persian_100_CI_AS
)
create table #TempAnbar(
fldCodeTafzili bigint,
fldCodeAnbar bigint, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS
)
create table #TempSumForoosh(
fldCodeTafzili bigint,
fldMablaghForoosh decimal,
fldTedadJozForoosh decimal
)
create table #TempSumBargashtAzForoosh(
fldCodeTafzili bigint,
fldMablaghBargasht decimal,
fldTedadJozBargasht decimal
)
create table #TempSumCheckDaryafti(
fldCodeTafzili bigint,
fldMablaghCheckDaryafti decimal
)
create table #TempSumCheckBargashti(
fldCodeTafzili bigint,
fldMablaghCheckBargashti decimal
)
create table #TempSumCheckVosoolShode(
fldCodeTafzili bigint,
fldMablaghCheckVosoolShode decimal
)
create table #TempSumCheckVosoolShodeNaShode(
fldCodeTafzili bigint,
fldMablaghCheckVosoolShodeNaShode decimal
)
create table #TempSumCheckPardakhti(
fldCodeTafzili bigint,
fldMablaghCheckPardakhti decimal
)

Create table #TempRizeAmalKardMoshtari(
fldCodeTafzili bigint,
fldSharheTafzili nvarchar(200) COLLATE Persian_100_CI_AS,
fldTedadJozForoosh decimal,
fldMablaghForoosh decimal,
fldTedadJozBargasht decimal,
fldMablaghBargasht decimal,
fldMablaghCheckDaryafti decimal,
fldMablaghCheckBargashti decimal,
fldMablaghCheckVosoolShode decimal,
fldMablaghCheckVosoolNaShode decimal,
fldMablaghCheckPardakhti decimal,
fldCodeGroupAshkhas Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldGroupNameAshkhas Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS
)
Create table #TempShowAmalKardMoshtari(
fldCodeTafzili Nvarchar(30) COLLATE Persian_100_CI_AS,
fldSharheTafzili nvarchar(200) COLLATE Persian_100_CI_AS,
fldTedadJozForoosh decimal,
fldMablaghForoosh decimal,
fldTedadJozBargasht decimal,
fldMablaghBargasht decimal,
fldMablaghCheckDaryafti decimal,
fldMablaghCheckBargashti decimal,
fldMablaghCheckVosoolShode decimal,
fldMablaghCheckVosoolNaShode decimal,
fldMablaghCheckPardakhti decimal,
fldCodeGroupAshkhas Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldGroupNameAshkhas Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS
)

end


insert into #TempSumForoosh(fldCodeTafzili,fldMablaghForoosh,fldTedadJozForoosh)
			(select fldCodeTafsili,Sum(fldMablaghKhales),Sum(fldTedadJoz) from tblRizeForoosh group by fldCodeTafsili)

insert into #TempSumBargashtAzForoosh(fldCodeTafzili,fldMablaghBargasht,fldTedadJozBargasht)
			(select fldCodeTafsili,Sum(fldMablaghKhales),Sum(fldTedadJoz) from tblRizeBargashtAzForoosh group by fldCodeTafsili)

insert into #TempSumCheckDaryafti(fldCodeTafzili,fldMablaghCheckDaryafti)
			(select fldCodeSahebeCheck,Sum(fldMablagh) from tblCheckeDaryafti group by fldCodeSahebeCheck)

insert into #TempSumCheckBargashti(fldCodeTafzili,fldMablaghCheckBargashti)
			(select fldCodeSahebeCheck,Sum(fldMablagh) from tblCheckeDaryafti where fldVaziat='Bargasht' group by fldCodeSahebeCheck )

insert into #TempSumCheckVosoolShode(fldCodeTafzili,fldMablaghCheckVosoolShode)
			(select fldCodeSahebeCheck,Sum(fldMablagh) from tblCheckeDaryafti where fldVaziat='Vosool' group by fldCodeSahebeCheck )

insert into #TempSumCheckVosoolShodeNaShode(fldCodeTafzili,fldMablaghCheckVosoolShodeNaShode)
			(select fldCodeSahebeCheck,Sum(fldMablagh) from tblCheckeDaryafti where fldVaziat<>'Vosool' group by fldCodeSahebeCheck )

insert into #TempSumCheckPardakhti(fldCodeTafzili,fldMablaghCheckPardakhti)
			(select fldCodeSahebeCheck,Sum(fldMablagh) from tblCheckePardakhti  group by fldCodeSahebeCheck )

insert into #TempGroupAshkhas(fldCodeTafzili,fldCodeGroup,fldGroupName)
(SELECT        tblRizeForoosh.fldCodeTafsili, tblGroupAshkhas.fldGroupId, tblGroupAshkhas.fldGroupName
FROM            tblTafzili INNER JOIN
                         tblRizeForoosh ON tblTafzili.fldCodeTafziliBigInt = tblRizeForoosh.fldCodeTafsili INNER JOIN
                         tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
GROUP BY tblRizeForoosh.fldCodeTafsili, tblGroupAshkhas.fldGroupId, tblGroupAshkhas.fldGroupName)


insert into #TempAnbar(fldCodeTafzili,fldCodeAnbar,fldNameAnbar)
(SELECT        tblRizeForoosh.fldCodeTafsili, tblRizeForoosh.fldCodeAnbar, tblAnbar.fldNameAnbar
FROM            tblTafzili INNER JOIN
                         tblRizeForoosh ON tblTafzili.fldCodeTafziliBigInt = tblRizeForoosh.fldCodeTafsili INNER JOIN
                         tblAnbar ON tblRizeForoosh.fldCodeAnbar = tblAnbar.fldCode
GROUP BY tblRizeForoosh.fldCodeTafsili, tblRizeForoosh.fldCodeAnbar, tblAnbar.fldNameAnbar)



insert into #TempRizeAmalKardMoshtari(
						fldCodeTafzili,
						fldSharheTafzili,
						fldTedadJozForoosh,				
						fldMablaghForoosh,
						fldMablaghBargasht,
						fldTedadJozBargasht,
						fldMablaghCheckDaryafti,
						fldMablaghCheckBargashti,
						fldMablaghCheckVosoolShode,
						fldMablaghCheckVosoolNaShode,
						fldMablaghCheckPardakhti
						)

select #TempSumForoosh.fldCodeTafzili,
		    tblTafzili.fldSharheTafzili,
			fldTedadJozForoosh,
			fldMablaghForoosh,			
		   0 as fldMablaghBargasht,
		   0 as fldTedadJozBargasht,			
		   0 as fldMablaghCheckDaryafti,
		   0 as fldMablaghCheckBargashti,
		   0 as fldMablaghCheckVosoolShode,
		   0 as fldMablaghCheckVosoolNaShode,
		   0 as fldMablaghCheckPardakhti

from #TempSumForoosh inner join
	tblTafzili on #TempSumForoosh.fldCodeTafzili=tblTafzili.fldCodeTafzili 
-------SumBargashtAzForoosh---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghBargasht = #TempSumBargashtAzForoosh.fldMablaghBargasht,
		#TempRizeAmalKardMoshtari.fldTedadJozBargasht = #TempSumBargashtAzForoosh.fldTedadJozBargasht
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumBargashtAzForoosh on #TempSumBargashtAzForoosh.fldCodeTafzili=SI.fldCodeTafzili 

-------CheckDaryafti---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghCheckDaryafti =#TempSumCheckDaryafti.fldMablaghCheckDaryafti
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumCheckDaryafti on #TempSumCheckDaryafti.fldCodeTafzili=SI.fldCodeTafzili 
-------CheckBargashti---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghCheckBargashti =#TempSumCheckBargashti.fldMablaghCheckBargashti
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumCheckBargashti on #TempSumCheckBargashti.fldCodeTafzili=SI.fldCodeTafzili 
-------CheckVosoolShode---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghCheckVosoolShode =#TempSumCheckVosoolShode.fldMablaghCheckVosoolShode
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumCheckVosoolShode on #TempSumCheckVosoolShode.fldCodeTafzili=SI.fldCodeTafzili 
				
-------CheckVosoolShodeNaShode---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghCheckVosoolNaShode =#TempSumCheckVosoolShodeNaShode.fldMablaghCheckVosoolShodeNaShode
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumCheckVosoolShodeNaShode on #TempSumCheckVosoolShodeNaShode.fldCodeTafzili=SI.fldCodeTafzili 
-------CheckPardakhti---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghCheckPardakhti =#TempSumCheckPardakhti.fldMablaghCheckPardakhti
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumCheckPardakhti on #TempSumCheckPardakhti.fldCodeTafzili=SI.fldCodeTafzili 
-------GroupAshkhas---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldCodeGroupAshkhas =#TempGroupAshkhas.fldCodeGroup,
		#TempRizeAmalKardMoshtari.fldGroupNameAshkhas=#TempGroupAshkhas.fldGroupName
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempGroupAshkhas on #TempGroupAshkhas.fldCodeTafzili=SI.fldCodeTafzili 
-------GroupAshkhas---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldCodeAnbar =#TempAnbar.fldCodeAnbar,
		#TempRizeAmalKardMoshtari.fldNameAnbar =#TempAnbar.fldNameAnbar
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempAnbar on #TempAnbar.fldCodeTafzili=SI.fldCodeTafzili 
					

insert into #TempShowAmalKardMoshtari(
		fldCodeTafzili,
	fldSharheTafzili,
	fldTedadJozForoosh,
	fldMablaghForoosh,
	fldTedadJozBargasht,
	fldMablaghBargasht,
	fldMablaghCheckDaryafti,
	fldMablaghCheckBargashti,
	fldMablaghCheckVosoolShode,
	fldMablaghCheckVosoolNaShode,
	fldMablaghCheckPardakhti,
	fldCodeGroupAshkhas, 
	fldGroupNameAshkhas,
	fldCodeAnbar, 
	fldNameAnbar )
	(select  
	fldCodeTafzili,
	fldSharheTafzili,
	fldTedadJozForoosh,
	fldMablaghForoosh,
	fldTedadJozBargasht,
	fldMablaghBargasht,
	fldMablaghCheckDaryafti,
	fldMablaghCheckBargashti,
	fldMablaghCheckVosoolShode,
	fldMablaghCheckVosoolNaShode,
	fldMablaghCheckPardakhti,
	fldCodeGroupAshkhas, 
	fldGroupNameAshkhas,
	fldCodeAnbar, 
	fldNameAnbar 
from #TempRizeAmalKardMoshtari
	)


		   if @ListCodeAshkhas <>''
		    begin
		     declare @StrDeleteAshkhas varchar(5000)
		      set  @StrDeleteAshkhas='delete from #TempShowAmalKardMoshtari 
		            where fldCodeTafzili NOT IN ('+ @ListCodeAshkhas +')'
					execute(@StrDeleteAshkhas)
		    end


			if @ListGroupAshkhas<>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShowAmalKardMoshtari
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end

			if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempShowAmalKardMoshtari
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


		 select  
	fldCodeTafzili,
	fldSharheTafzili,
	fldTedadJozForoosh,
	fldMablaghForoosh,
	fldTedadJozBargasht,
	fldMablaghBargasht,
	fldMablaghCheckDaryafti,
	fldMablaghCheckBargashti,
	fldMablaghCheckVosoolShode,
	fldMablaghCheckVosoolNaShode,
	fldMablaghCheckPardakhti,
	fldCodeGroupAshkhas, 
	fldGroupNameAshkhas,
	fldCodeAnbar, 
	fldNameAnbar 
from #TempShowAmalKardMoshtari
 



END

 

GO
/****** Object:  StoredProcedure [dbo].[z149ShowList_ReportAmalKardMoshtari_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z149ShowList_ReportAmalKardMoshtari_WithUserCode]
	--چندزبانه
	@ListGroupAshkhas varchar(5000)=null,
	@ListCodeAshkhas varchar(5000)=null,
	@ListCodeAnbar varchar(5000)=null,
	@userCode bigint
as
   
	
BEGIN
	
	SET NOCOUNT ON;


-- ----------------------------------------
-- -----------------------------------------TempTable In Below Code

IF OBJECT_ID('tempdb..#TempRizeAmalKardMoshtari') IS NOT NULL
 begin
	delete from     #TempRizeAmalKardMoshtari
	delete from     #TempShowAmalKardMoshtari
	delete from     #TempSumForoosh
	delete from     #TempSumBargashtAzForoosh		
	delete from 	#TempSumCheckDaryafti
	delete from 	#TempSumCheckBargashti
	delete from 	#TempSumCheckVosoolShode
	delete from 	#TempSumCheckVosoolShodeNaShode
	delete from 	#TempSumCheckPardakhti
	delete from     #TempGroupAshkhas
	delete from     #TempAnbar
 end
else 
--Nvarchar(30) COLLATE Persian_100_CI_AS,
begin

create table #TempGroupAshkhas(
fldCodeTafzili bigint,
fldCodeGroup bigint, 
fldGroupName nvarchar(50)  COLLATE Persian_100_CI_AS
)
create table #TempAnbar(
fldCodeTafzili bigint,
fldCodeAnbar bigint, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS
)
create table #TempSumForoosh(
fldCodeTafzili bigint,
fldMablaghForoosh decimal(18 ,4),
fldTedadJozForoosh decimal(18 ,4)
)
create table #TempSumBargashtAzForoosh(
fldCodeTafzili bigint,
fldMablaghBargasht decimal(18 ,4),
fldTedadJozBargasht decimal(18 ,4)
)
create table #TempSumCheckDaryafti(
fldCodeTafzili bigint,
fldMablaghCheckDaryafti decimal(18 ,4)
)
create table #TempSumCheckBargashti(
fldCodeTafzili bigint,
fldMablaghCheckBargashti decimal(18 ,4)
)
create table #TempSumCheckVosoolShode(
fldCodeTafzili bigint,
fldMablaghCheckVosoolShode decimal(18 ,4)
)
create table #TempSumCheckVosoolShodeNaShode(
fldCodeTafzili bigint,
fldMablaghCheckVosoolShodeNaShode decimal(18 ,4)
)
create table #TempSumCheckPardakhti(
fldCodeTafzili bigint,
fldMablaghCheckPardakhti decimal(18 ,4)
)

Create table #TempRizeAmalKardMoshtari(
fldCodeTafzili bigint,
fldSharheTafzili nvarchar(200) COLLATE Persian_100_CI_AS,
fldTedadJozForoosh decimal(18 ,4),
fldMablaghForoosh decimal(18 ,4),
fldTedadJozBargasht decimal(18 ,4),
fldMablaghBargasht decimal(18 ,4),
fldMablaghCheckDaryafti decimal(18 ,4),
fldMablaghCheckBargashti decimal(18 ,4),
fldMablaghCheckVosoolShode decimal(18 ,4),
fldMablaghCheckVosoolNaShode decimal(18 ,4),
fldMablaghCheckPardakhti decimal(18 ,4),
fldCodeGroupAshkhas Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldGroupNameAshkhas Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS
)
Create table #TempShowAmalKardMoshtari(
fldCodeTafzili Nvarchar(30) COLLATE Persian_100_CI_AS,
fldSharheTafzili nvarchar(200) COLLATE Persian_100_CI_AS,
fldTedadJozForoosh decimal(18 ,4),
fldMablaghForoosh decimal(18 ,4),
fldTedadJozBargasht decimal(18 ,4),
fldMablaghBargasht decimal(18 ,4),
fldMablaghCheckDaryafti decimal(18 ,4),
fldMablaghCheckBargashti decimal(18 ,4),
fldMablaghCheckVosoolShode decimal(18 ,4),
fldMablaghCheckVosoolNaShode decimal(18 ,4),
fldMablaghCheckPardakhti decimal(18 ,4),
fldCodeGroupAshkhas Nvarchar(30) COLLATE Persian_100_CI_AS, 
fldGroupNameAshkhas Nvarchar(200) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint, 
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS
)

end


insert into #TempSumForoosh(fldCodeTafzili,fldMablaghForoosh,fldTedadJozForoosh)
			(select fldCodeTafsili,Sum(fldMablaghKhales),Sum(fldTedadJoz) from tblRizeForoosh group by fldCodeTafsili)

insert into #TempSumBargashtAzForoosh(fldCodeTafzili,fldMablaghBargasht,fldTedadJozBargasht)
			(select fldCodeTafsili,Sum(fldMablaghKhales),Sum(fldTedadJoz) from tblRizeBargashtAzForoosh group by fldCodeTafsili)

insert into #TempSumCheckDaryafti(fldCodeTafzili,fldMablaghCheckDaryafti)
			(select fldCodeSahebeCheck,Sum(fldMablagh) from tblCheckeDaryafti group by fldCodeSahebeCheck)

insert into #TempSumCheckBargashti(fldCodeTafzili,fldMablaghCheckBargashti)
			(select fldCodeSahebeCheck,Sum(fldMablagh) from tblCheckeDaryafti where fldVaziat='Bargasht' group by fldCodeSahebeCheck )

insert into #TempSumCheckVosoolShode(fldCodeTafzili,fldMablaghCheckVosoolShode)
			(select fldCodeSahebeCheck,Sum(fldMablagh) from tblCheckeDaryafti where fldVaziat='Vosool' group by fldCodeSahebeCheck )

insert into #TempSumCheckVosoolShodeNaShode(fldCodeTafzili,fldMablaghCheckVosoolShodeNaShode)
			(select fldCodeSahebeCheck,Sum(fldMablagh) from tblCheckeDaryafti where fldVaziat<>'Vosool' group by fldCodeSahebeCheck )

insert into #TempSumCheckPardakhti(fldCodeTafzili,fldMablaghCheckPardakhti)
			(select fldCodeSahebeCheck,Sum(fldMablagh) from tblCheckePardakhti  group by fldCodeSahebeCheck )

insert into #TempGroupAshkhas(fldCodeTafzili,fldCodeGroup,fldGroupName)
(SELECT        tblRizeForoosh.fldCodeTafsili, tblGroupAshkhas.fldGroupId, tblGroupAshkhas.fldGroupName
FROM            tblTafzili INNER JOIN
                         tblRizeForoosh ON tblTafzili.fldCodeTafziliBigInt = tblRizeForoosh.fldCodeTafsili INNER JOIN
                         tblGroupAshkhas ON tblTafzili.fldCodeGroup = tblGroupAshkhas.fldGroupId
GROUP BY tblRizeForoosh.fldCodeTafsili, tblGroupAshkhas.fldGroupId, tblGroupAshkhas.fldGroupName)


insert into #TempAnbar(fldCodeTafzili,fldCodeAnbar,fldNameAnbar)
(SELECT        tblRizeForoosh.fldCodeTafsili, tblRizeForoosh.fldCodeAnbar, tblAnbar.fldNameAnbar
FROM            tblTafzili INNER JOIN
                         tblRizeForoosh ON tblTafzili.fldCodeTafziliBigInt = tblRizeForoosh.fldCodeTafsili INNER JOIN
                         tblAnbar ON tblRizeForoosh.fldCodeAnbar = tblAnbar.fldCode
GROUP BY tblRizeForoosh.fldCodeTafsili, tblRizeForoosh.fldCodeAnbar, tblAnbar.fldNameAnbar)



insert into #TempRizeAmalKardMoshtari(
						fldCodeTafzili,
						fldSharheTafzili,
						fldTedadJozForoosh,				
						fldMablaghForoosh,
						fldMablaghBargasht,
						fldTedadJozBargasht,
						fldMablaghCheckDaryafti,
						fldMablaghCheckBargashti,
						fldMablaghCheckVosoolShode,
						fldMablaghCheckVosoolNaShode,
						fldMablaghCheckPardakhti
						)

select #TempSumForoosh.fldCodeTafzili,
		    tblTafzili.fldSharheTafzili,
			fldTedadJozForoosh,
			fldMablaghForoosh,			
		   0 as fldMablaghBargasht,
		   0 as fldTedadJozBargasht,			
		   0 as fldMablaghCheckDaryafti,
		   0 as fldMablaghCheckBargashti,
		   0 as fldMablaghCheckVosoolShode,
		   0 as fldMablaghCheckVosoolNaShode,
		   0 as fldMablaghCheckPardakhti

from #TempSumForoosh inner join
	tblTafzili on #TempSumForoosh.fldCodeTafzili=tblTafzili.fldCodeTafzili 
-------SumBargashtAzForoosh---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghBargasht = #TempSumBargashtAzForoosh.fldMablaghBargasht,
		#TempRizeAmalKardMoshtari.fldTedadJozBargasht = #TempSumBargashtAzForoosh.fldTedadJozBargasht
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumBargashtAzForoosh on #TempSumBargashtAzForoosh.fldCodeTafzili=SI.fldCodeTafzili 

-------CheckDaryafti---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghCheckDaryafti =#TempSumCheckDaryafti.fldMablaghCheckDaryafti
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumCheckDaryafti on #TempSumCheckDaryafti.fldCodeTafzili=SI.fldCodeTafzili 
-------CheckBargashti---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghCheckBargashti =#TempSumCheckBargashti.fldMablaghCheckBargashti
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumCheckBargashti on #TempSumCheckBargashti.fldCodeTafzili=SI.fldCodeTafzili 
-------CheckVosoolShode---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghCheckVosoolShode =#TempSumCheckVosoolShode.fldMablaghCheckVosoolShode
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumCheckVosoolShode on #TempSumCheckVosoolShode.fldCodeTafzili=SI.fldCodeTafzili 
				
-------CheckVosoolShodeNaShode---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghCheckVosoolNaShode =#TempSumCheckVosoolShodeNaShode.fldMablaghCheckVosoolShodeNaShode
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumCheckVosoolShodeNaShode on #TempSumCheckVosoolShodeNaShode.fldCodeTafzili=SI.fldCodeTafzili 
-------CheckPardakhti---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldMablaghCheckPardakhti =#TempSumCheckPardakhti.fldMablaghCheckPardakhti
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempSumCheckPardakhti on #TempSumCheckPardakhti.fldCodeTafzili=SI.fldCodeTafzili 
-------GroupAshkhas---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldCodeGroupAshkhas =#TempGroupAshkhas.fldCodeGroup,
		#TempRizeAmalKardMoshtari.fldGroupNameAshkhas=#TempGroupAshkhas.fldGroupName
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempGroupAshkhas on #TempGroupAshkhas.fldCodeTafzili=SI.fldCodeTafzili 
-------GroupAshkhas---------
update #TempRizeAmalKardMoshtari 
		set 
		#TempRizeAmalKardMoshtari.fldCodeAnbar =#TempAnbar.fldCodeAnbar,
		#TempRizeAmalKardMoshtari.fldNameAnbar =#TempAnbar.fldNameAnbar
		from
		#TempRizeAmalKardMoshtari SI
		INNER JOIN
		#TempAnbar on #TempAnbar.fldCodeTafzili=SI.fldCodeTafzili 
	
	
-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################


insert into #TempShowAmalKardMoshtari(
		fldCodeTafzili,
	fldSharheTafzili,
	fldTedadJozForoosh,
	fldMablaghForoosh,
	fldTedadJozBargasht,
	fldMablaghBargasht,
	fldMablaghCheckDaryafti,
	fldMablaghCheckBargashti,
	fldMablaghCheckVosoolShode,
	fldMablaghCheckVosoolNaShode,
	fldMablaghCheckPardakhti,
	fldCodeGroupAshkhas, 
	fldGroupNameAshkhas,
	fldCodeAnbar, 
	fldNameAnbar )
	(select  
	fldCodeTafzili,
	fldSharheTafzili,
	fldTedadJozForoosh,
	fldMablaghForoosh,
	fldTedadJozBargasht,
	fldMablaghBargasht,
	fldMablaghCheckDaryafti,
	fldMablaghCheckBargashti,
	fldMablaghCheckVosoolShode,
	fldMablaghCheckVosoolNaShode,
	fldMablaghCheckPardakhti,
	fldCodeGroupAshkhas, 
	fldGroupNameAshkhas,
	fldCodeAnbar, 
	fldNameAnbar 
from #TempRizeAmalKardMoshtari
	)


		   if @ListCodeAshkhas <>''
		    begin
		     declare @StrDeleteAshkhas varchar(5000)
		      set  @StrDeleteAshkhas='delete from #TempShowAmalKardMoshtari 
		            where fldCodeTafzili NOT IN ('+ @ListCodeAshkhas +')'
					execute(@StrDeleteAshkhas)
		    end


			if @ListGroupAshkhas<>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShowAmalKardMoshtari
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end

			if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempShowAmalKardMoshtari
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


		 select  
	fldCodeTafzili,
	fldSharheTafzili,
	fldTedadJozForoosh,
	fldMablaghForoosh,
	fldTedadJozBargasht,
	fldMablaghBargasht,
	fldMablaghCheckDaryafti,
	fldMablaghCheckBargashti,
	fldMablaghCheckVosoolShode,
	fldMablaghCheckVosoolNaShode,
	fldMablaghCheckPardakhti,
	fldCodeGroupAshkhas, 
	fldGroupNameAshkhas,
	fldCodeAnbar, 
	fldNameAnbar 
from #TempShowAmalKardMoshtari
where 
-- ##################### Group Ashkhas Limitation
fldCodeTafzili in (select code from @accessCodes)
 



END

 

GO
/****** Object:  StoredProcedure [dbo].[z150ListeKalaBedooneMojoodi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z150ListeKalaBedooneMojoodi]
	-- Add the parameters for the stored procedure here
	
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.

create table #TempShowMojoodi2(

fldCodeKala bigint,
SumVorood decimal(18, 2),
SumKhorooj decimal(18, 2),
SumMande decimal(18, 2),
Mande nvarchar(18),
mandeeee nvarchar(10),
fldTedadDarKarton decimal(18, 2),
fldTedadDarKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadVoroodKarton decimal(18, 2),
fldTedadVoroodKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldTedadKhoroojKarton decimal(18, 2),
fldTedadKhoroojKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh decimal(18, 2),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldKharid bigint,
fldFeeForoosh money,
fldArzesheMojoodi bigint
)

Insert into #TempShowMojoodi2
	(fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,SumMande,fldTedadDarKarton, fldGroupName, fldTedadDarKartonShow)
SELECT        dbo.tblKala.fldCodeKala , dbo.tblKala.fldNameKala, dbo.tblVahed.fldNameVahed, tblVahed_1.fldNameVahed AS fldNameVahed2,0 as SumMande,0,'',''                         
FROM            dbo.tblKala INNER JOIN
                         dbo.tblVahed ON dbo.tblKala.fldCodeVahed1 = dbo.tblVahed.ID INNER JOIN
                         dbo.tblVahed AS tblVahed_1 ON dbo.tblKala.fldCodeVahed2 = tblVahed_1.ID


select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,Mande as SumMande,fldFeeForoosh,fldCodeGroup,fldTedadDarKartonShow from #TempShowMojoodi2	 



END

GO
/****** Object:  StoredProcedure [dbo].[z150ShowList_ReportAshkhasFactorNashode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z150ShowList_ReportAshkhasFactorNashode]

	@ListGroupAshkhas varchar(5000)=null,
	@DateAz varchar(50),
	@DateTa varchar(50)
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;



create table #TempShow(
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldGroupAshkhasShow Nvarchar(100) COLLATE Persian_100_CI_AS,
fldAddress Nvarchar(200) COLLATE Persian_100_CI_AS,
fldShomare Nvarchar(30) COLLATE Persian_100_CI_AS
)
delete #TempShow

insert into #TempShow(fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas,fldGroupAshkhasShow,fldAddress)
SELECT   fldCodeTafzili,fldSharheTafzili, fldCodeGroup,dbo.tblGroupAshkhas.fldGroupName, fldAddress                
FROM            tblTafzili inner join
				dbo.tblGroupAshkhas on dbo.tblTafzili.fldCodeGroup=dbo.tblGroupAshkhas.fldGroupId
WHERE        (fldCodeTafzili NOT IN
                             (SELECT DISTINCT fldCodeTafsili
                                FROM tblRizeForoosh where fldType='foroosh' and fldDate between @DateAz and @DateTa)) AND (fldCodeNoe = '1')



		if @ListGroupAshkhas<>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end

	
	
SELECT  ROW_NUMBER() OVER(ORDER BY fldCodeTafzili) as fldRadif,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas,fldGroupAshkhasShow,fldAddress               
FROM            #TempShow 

END

 

GO
/****** Object:  StoredProcedure [dbo].[z150ShowList_ReportAshkhasFactorNashode_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[z150ShowList_ReportAshkhasFactorNashode_WithUserCode]

	@ListGroupAshkhas varchar(5000)=null,
	@DateAz varchar(50),
	@DateTa varchar(50),
	@userCode bigint
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;



create table #TempShow(
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldGroupAshkhasShow Nvarchar(100) COLLATE Persian_100_CI_AS,
fldAddress Nvarchar(200) COLLATE Persian_100_CI_AS,
fldShomare Nvarchar(30) COLLATE Persian_100_CI_AS
)
delete #TempShow

insert into #TempShow(fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas,fldGroupAshkhasShow,fldAddress)
SELECT   fldCodeTafzili,fldSharheTafzili, fldCodeGroup,dbo.tblGroupAshkhas.fldGroupName, fldAddress                
FROM            tblTafzili inner join
				dbo.tblGroupAshkhas on dbo.tblTafzili.fldCodeGroup=dbo.tblGroupAshkhas.fldGroupId
WHERE        (fldCodeTafzili NOT IN
                             (SELECT DISTINCT fldCodeTafsili
                                FROM tblRizeForoosh where fldType='foroosh' and fldDate between @DateAz and @DateTa)) AND (fldCodeNoe = '1')



		if @ListGroupAshkhas<>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end

	
-- ###############################################################
-- ###########Group Ashkhas Limitation############################
-- ###############################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ###############################################################
	
SELECT  ROW_NUMBER() OVER(ORDER BY fldCodeTafzili) as fldRadif,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas,fldGroupAshkhasShow,fldAddress               
FROM            #TempShow 
where fldCodeTafzili in (select code from @accessCodes)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z151BahayeTamamShodeForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z151BahayeTamamShodeForoosh]
--چندزبانه
@lngDateFrom bigint,
@lngDateUntil bigint,
@SumBahayeTamamShode decimal (18, 4) output,
@MablagheEbtedayeDore decimal (18, 4) output,
@MablagheKharidTeyeDore decimal (18, 4) output,
@MablagheBargashtAzKharid decimal (18, 4) output,
@MablaghePayanDore decimal (18, 4) output
	
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

        create table #tmpGardesheKala
		(
			CodeKala bigint,
			CodeGroup bigint,
			CodeAnbar bigint,
			NameGroup nvarchar(100) collate Persian_100_CI_AS,
			NameKala nvarchar(100) collate Persian_100_CI_AS,
			NameAnbar nvarchar(100) collate Persian_100_CI_AS,
			TedadDarKarton decimal (18, 4),

			MablagheEbteda decimal (18, 4),
			TedadeEbteda decimal (18, 4),
			TedadKartonEbteda nvarchar(100) collate Persian_100_CI_AS,

			MablagheKharid decimal (18, 4),
			TedadeKharid decimal (18, 4),
			TedadKartonKharid nvarchar(100) collate Persian_100_CI_AS,

			MablagheBargashtAzKharid decimal (18, 4),
			TedadeBargashtAzKharid decimal (18, 4),
			TedadKartonBargashtAzKharid nvarchar(100) collate Persian_100_CI_AS,

			MablagheKhalesKharid decimal (18, 4),
			TedadeKhalesKharid decimal (18, 4),
			TedadKartonKhalesKharid nvarchar(100) collate Persian_100_CI_AS,

			MablagheForoosh decimal (18, 4),
			TedadeForoosh decimal (18, 4),
			TedadeKartonForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablagheBargashtAzForoosh decimal (18, 4),
			TedadeBargashtAzForoosh decimal (18, 4),
			TedadeKartonBargashtAzForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablagheKhalesForoosh decimal (18, 4),
			TedadeKhalesForoosh decimal (18, 4),
			TedadKartonKhalesForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablaghePayan decimal (18, 4),
			TedadePayan decimal (18, 4),
			TedadeKartonPayan nvarchar(100) collate Persian_100_CI_AS,

			BahayeTamamShodeForoosh decimal (18, 4),
			SoodeKala decimal (18, 4)
		)


		insert into #tmpGardesheKala
			(CodeKala,CodeGroup,NameKala,TedadDarKarton)
			Select  fldCodeKala,fldCodeGroup,fldNameKala,fldTedadJoz 
				from  tblKala

		--update #tmpGardesheKala set TedadeEbteda=Sum(tblRizeKharid.fldTedadeJoz) from tblRizeKharid where #tmpGardesheKala.CodeKala=tblRizeKharid.fldCodeKala group by tblRizeKharid.fldCodeKala

		update           #tmpGardesheKala
			set NameGroup = tblGroupKala.fldGroupName from tblGroupKala Where CodeGroup=tblGroupKala.fldGroupId

		--####ابتدای دوره####
		update           #tmpGardesheKala
			set tedadeEbteda=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizekharid Where fldType='mojodi_ebteda_doreh'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala

		
		update #tmpGardesheKala 
			set MablagheEbteda=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldFeeKol) as Sum1 from tblRizekharid Where fldType='mojodi_ebteda_doreh'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 
			--mojodi_ebteda_doreh



		----کارتن و عدد
		--update #tmpGardesheKala Set TedadKartonEbteda=Convert(varchar,floor(tedadeEbteda / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeEbteda, TedadDarKarton) } ) 
		--	where TedadDarKarton>0
		--######################
		--######خرید#####
		update           #tmpGardesheKala
			set tedadekharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizekharid Where fldType='kharid'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set Mablaghekharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizekharid Where fldType='kharid'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		----کارتن و عدد
		--update #tmpGardesheKala Set TedadKartonkharid=Convert(varchar,floor(TedadeKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeKharid, TedadDarKarton) } ) 
		--	where TedadDarKarton>0
		--#######################
		--######برگشت از خرید#####
		update           #tmpGardesheKala
			set tedadeBargashtAzkharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeBargashtAzkharid Where fldType='bargashtazkharid'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheBargashtAzKharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeBargashtAzkharid Where fldType='bargashtazkharid'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		----کارتن و عدد
		--update #tmpGardesheKala Set TedadKartonBargashtAzkharid=Convert(varchar,floor(TedadeBargashtAzKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeBargashtAzKharid, TedadDarKarton) } ) 
		--	where TedadDarKarton>0
		--#######################

		--######فروش#####
		update           #tmpGardesheKala
			set TedadeForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeForoosh Where fldType='Foroosh'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeForoosh Where fldType='Foroosh'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		----کارتن و عدد
		--update #tmpGardesheKala Set TedadeKartonForoosh=Convert(varchar,floor(TedadeForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeForoosh, TedadDarKarton) } ) 
		--	where TedadDarKarton>0

	--######برگشت از فروش#####
		update           #tmpGardesheKala
			set TedadeBargashtAzForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeBargashtAzForoosh Where fldType='bargashtazforoosh'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheBargashtAzForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeBargashtAzForoosh Where fldType='bargashtazforoosh'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		----کارتن و عدد
		--update #tmpGardesheKala Set TedadeKartonForoosh=Convert(varchar,floor(TedadeForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeForoosh, TedadDarKarton) } ) 
		--	where TedadDarKarton>0
		--#######################
		--#######################
		update           #tmpGardesheKala
			set TedadeEbteda=0 Where TedadeEbteda is null  
		update           #tmpGardesheKala
			set TedadeKharid=0 Where TedadeKharid is null
		update           #tmpGardesheKala
			set TedadeBargashtAzKharid=0 Where TedadeBargashtAzKharid is null
		update           #tmpGardesheKala
			set TedadeForoosh=0 Where TedadeForoosh is null
		update           #tmpGardesheKala
			set TedadeBargashtAzForoosh=0 Where TedadeBargashtAzForoosh is null
		update           #tmpGardesheKala
			set MablagheEbteda=0 Where MablagheEbteda is null
		update           #tmpGardesheKala
			set MablagheKharid=0 Where MablagheKharid is null
		update           #tmpGardesheKala
			set MablagheForoosh=0 Where MablagheForoosh is null
		update           #tmpGardesheKala
			set MablaghePayan=0 Where MablaghePayan is null
		update           #tmpGardesheKala
			set bahayetamamshodeforoosh=0 Where bahayetamamshodeforoosh is null
		update           #tmpGardesheKala
			set SoodeKala=0 Where SoodeKala is null
		update           #tmpGardesheKala
			set MablagheBargashtAzKharid=0 Where MablagheBargashtAzKharid is null


		--######خالص خرید#######
		update           #tmpGardesheKala
			set TedadeKhalesKharid=TedadeKharid-TedadeBargashtAzKharid
		update #tmpGardesheKala Set TedadKartonKhalesKharid=Convert(varchar,floor(TedadeKhalesKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(TedadeKhalesKharid, TedadDarKarton) } ) 
			where TedadDarKarton>0

		update #tmpGardesheKala Set MablagheKhalesKharid=MablagheKharid-MablagheBargashtAzKharid

		--######خالص فروش#######
		update           #tmpGardesheKala
			set TedadeKhalesForoosh=TedadeForoosh-TedadeBargashtAzForoosh
		update #tmpGardesheKala Set TedadKartonKhalesForoosh=Convert(varchar,floor(TedadeKhalesForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(TedadeKhalesForoosh, TedadDarKarton) } ) 
			where TedadDarKarton>0

		update #tmpGardesheKala Set MablagheKhalesForoosh=MablagheForoosh-MablagheBargashtAzForoosh
		--######پایان دوره#####
		update           #tmpGardesheKala
			set tedadePayan=TedadeEbteda+TedadeKharid+TedadeBargashtAzForoosh-TedadeForoosh-TedadeBargashtAzKharid
		update #tmpGardesheKala
			set MablaghePayan=(MablagheEbteda+MablagheKharid)/(TedadeEbteda+TedadeKharid)*tedadePayan
			where TedadeEbteda+TedadeKharid>0
		----کارتن و عدد
		--update #tmpGardesheKala Set TedadeKartonPayan=Convert(varchar,floor(TedadePayan/ TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadePayan, TedadDarKarton) } ) 
		--	where TedadDarKarton>0
		--#######################

		--####انتقال بین انبار و تولید کنترل شود#######

		   
		delete  #tmpGardesheKala where  TedadePayan<0 

		


		update #tmpGardesheKala set BahayeTamamShodeForoosh=MablagheEbteda+MablagheKharid-MablagheBargashtAzKharid-MablaghePayan

		--select BahayeTamamShodeForoosh,MablagheEbteda,MablagheKharid,MablagheBargashtAzKharid,MablaghePayan from #tmpGardesheKala
		--select Sum(MablagheEbteda)   from #tmpGardesheKala
		--select BahayeTamamShodeForoosh from #tmpGardesheKala
		--update #tmpGardesheKala set SoodeKala=MablagheForoosh-BahayeTamamShodeForoosh
		set @MablagheEbtedayeDore=(Select SUM(MablagheEbteda) from #tmpGardesheKala )
		set @MablagheKharidTeyeDore=(Select SUM(MablagheKharid) from #tmpGardesheKala )
		set @MablagheBargashtAzKharid=(Select SUM(MablagheBargashtAzKharid) from #tmpGardesheKala )
		set @MablaghePayanDore=(Select SUM(MablaghePayan) from #tmpGardesheKala )

		update           #tmpGardesheKala
			set bahayetamamshodeforoosh=0 Where bahayetamamshodeforoosh is null

		set @SumBahayeTamamShode=(Select SUM(BahayeTamamShodeForoosh) from #tmpGardesheKala)

		--Select MablagheEbteda,MablagheKharid,MablaghePayan, SUM(BahayeTamamShodeForoosh) as SumBahayeTamamShodeForooshrafte from #tmpGardesheKala
		--Select ROW_NUMBER() OVER(ORDER BY CodeKala ) AS Row, CodeKala,NameGroup,NameKala,TedadDarKarton,TedadeEbteda,TedadKartonEbteda,MablagheEbteda,
		--	TedadeKharid,TedadKartonKharid,MablagheKharid,TedadeForoosh,TedadeKartonForoosh,MablagheForoosh,TedadePayan,TedadeKartonPayan,MablaghePayan,BahayeTamamShodeForoosh ,SoodeKala
		--from #tmpGardesheKala
	
END

GO
/****** Object:  StoredProcedure [dbo].[z151BahayeTamamShodeForooshType1]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z151BahayeTamamShodeForooshType1]
--چندزبانه
@lngDateFrom bigint,
@lngDateUntil bigint,
@SumBahayeTamamShode decimal (18, 4) output,
@MablagheEbtedayeDore decimal (18, 4) output,
@MablagheKharidTeyeDore decimal (18, 4) output,
@MablagheBargashtAzKharid decimal (18, 4) output,
@MablaghePayanDore decimal (18, 4) output
	
AS
IF 1 = 0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

        create table #tmpGardesheKala
		(
			CodeKala bigint,
			CodeGroup bigint,
			CodeAnbar bigint,
			NameGroup nvarchar(100) collate Persian_100_CI_AS,
			NameKala nvarchar(100) collate Persian_100_CI_AS,
			NameAnbar nvarchar(100) collate Persian_100_CI_AS,
			TedadDarKarton decimal (18, 4),

			MablagheEbteda decimal (18, 4),
			TedadeEbteda decimal (18, 4),
			TedadKartonEbteda nvarchar(100) collate Persian_100_CI_AS,

			MablagheKharid decimal (18, 4),
			TedadeKharid decimal (18, 4),
			TedadKartonKharid nvarchar(100) collate Persian_100_CI_AS,

			MablagheBargashtAzKharid decimal (18, 4),
			TedadeBargashtAzKharid decimal (18, 4),
			TedadKartonBargashtAzKharid nvarchar(100) collate Persian_100_CI_AS,

			MablagheKhalesKharid decimal (18, 4),
			TedadeKhalesKharid decimal (18, 4),
			TedadKartonKhalesKharid nvarchar(100) collate Persian_100_CI_AS,

			MablagheForoosh decimal (18, 4),
			TedadeForoosh decimal (18, 4),
			TedadeKartonForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablagheBargashtAzForoosh decimal (18, 4),
			TedadeBargashtAzForoosh decimal (18, 4),
			TedadeKartonBargashtAzForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablagheKhalesForoosh decimal (18, 4),
			TedadeKhalesForoosh decimal (18, 4),
			TedadKartonKhalesForoosh nvarchar(100) collate Persian_100_CI_AS,

			MablaghePayan decimal (18, 4),
			TedadePayan decimal (18, 4),
			TedadeKartonPayan nvarchar(100) collate Persian_100_CI_AS,

			BahayeTamamShodeForoosh decimal (18, 4),
			SoodeKala decimal (18, 4)
		)

		delete #tmpGardesheKala
		insert into #tmpGardesheKala
			(CodeKala,CodeGroup,NameKala,TedadDarKarton)
			Select  fldCodeKala,fldCodeGroup,fldNameKala,fldTedadJoz 
				from  tblKala

		--update #tmpGardesheKala set TedadeEbteda=Sum(tblRizeKharid.fldTedadeJoz) from tblRizeKharid where #tmpGardesheKala.CodeKala=tblRizeKharid.fldCodeKala group by tblRizeKharid.fldCodeKala

		update           #tmpGardesheKala
			set NameGroup = tblGroupKala.fldGroupName from tblGroupKala Where CodeGroup=tblGroupKala.fldGroupId

		--####ابتدای دوره####
		update           #tmpGardesheKala
			set tedadeEbteda=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizekharid Where fldType='mojodi_ebteda_doreh'  group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala

		
		update #tmpGardesheKala 
			set MablagheEbteda=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldFeeKol) as Sum1 from tblRizekharid Where fldType='mojodi_ebteda_doreh'   group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 
			--mojodi_ebteda_doreh



		----کارتن و عدد
		--update #tmpGardesheKala Set TedadKartonEbteda=Convert(varchar,floor(tedadeEbteda / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeEbteda, TedadDarKarton) } ) 
		--	where TedadDarKarton>0
		--######################
		--######خرید#####
		update           #tmpGardesheKala
			set tedadekharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizekharid Where fldType='kharid'   group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set Mablaghekharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizekharid Where fldType='kharid'   group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		----کارتن و عدد
		--update #tmpGardesheKala Set TedadKartonkharid=Convert(varchar,floor(TedadeKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeKharid, TedadDarKarton) } ) 
		--	where TedadDarKarton>0
		--#######################
		--######برگشت از خرید#####
		update           #tmpGardesheKala
			set tedadeBargashtAzkharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeBargashtAzkharid Where fldType='bargashtazkharid'   group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheBargashtAzKharid=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeBargashtAzkharid Where fldType='bargashtazkharid'   group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		----کارتن و عدد
		--update #tmpGardesheKala Set TedadKartonBargashtAzkharid=Convert(varchar,floor(TedadeBargashtAzKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeBargashtAzKharid, TedadDarKarton) } ) 
		--	where TedadDarKarton>0
		--#######################

		--######فروش#####
		update           #tmpGardesheKala
			set TedadeForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeForoosh Where fldType='Foroosh'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeForoosh Where fldType='Foroosh'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		----کارتن و عدد
		--update #tmpGardesheKala Set TedadeKartonForoosh=Convert(varchar,floor(TedadeForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeForoosh, TedadDarKarton) } ) 
		--	where TedadDarKarton>0

	--######برگشت از فروش#####
		update           #tmpGardesheKala
			set TedadeBargashtAzForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldTedadJoz) as Sum1 from tblRizeBargashtAzForoosh Where fldType='bargashtazforoosh'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 

		
		update #tmpGardesheKala 
			set MablagheBargashtAzForoosh=tbl1.Sum1 from  (Select fldCodeKala,Sum(fldMablaghKhales) as Sum1 from tblRizeBargashtAzForoosh Where fldType='bargashtazforoosh'  and (fldDate between @lngDateFrom and @lngDateUntil ) group by fldCodeKala) as tbl1
			Where #tmpGardesheKala.CodeKala=tbl1.fldCodeKala 			

		----کارتن و عدد
		--update #tmpGardesheKala Set TedadeKartonForoosh=Convert(varchar,floor(TedadeForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadeForoosh, TedadDarKarton) } ) 
		--	where TedadDarKarton>0
		--#######################
		--#######################
		update           #tmpGardesheKala
			set TedadeEbteda=0 Where TedadeEbteda is null  
		update           #tmpGardesheKala
			set TedadeKharid=0 Where TedadeKharid is null
		update           #tmpGardesheKala
			set TedadeBargashtAzKharid=0 Where TedadeBargashtAzKharid is null
		update           #tmpGardesheKala
			set TedadeForoosh=0 Where TedadeForoosh is null
		update           #tmpGardesheKala
			set TedadeBargashtAzForoosh=0 Where TedadeBargashtAzForoosh is null
		update           #tmpGardesheKala
			set MablagheEbteda=0 Where MablagheEbteda is null
		update           #tmpGardesheKala
			set MablagheKharid=0 Where MablagheKharid is null
		update           #tmpGardesheKala
			set MablagheForoosh=0 Where MablagheForoosh is null
		update           #tmpGardesheKala
			set MablaghePayan=0 Where MablaghePayan is null
		update           #tmpGardesheKala
			set bahayetamamshodeforoosh=0 Where bahayetamamshodeforoosh is null
		update           #tmpGardesheKala
			set SoodeKala=0 Where SoodeKala is null
		update           #tmpGardesheKala
			set MablagheBargashtAzKharid=0 Where MablagheBargashtAzKharid is null


		--######خالص خرید#######
		update           #tmpGardesheKala
			set TedadeKhalesKharid=TedadeEbteda+TedadeKharid-TedadeBargashtAzKharid

			
		update #tmpGardesheKala Set TedadKartonKhalesKharid=Convert(varchar,floor(TedadeKhalesKharid / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(TedadeKhalesKharid, TedadDarKarton) } ) 
			where TedadDarKarton>0

		update #tmpGardesheKala Set MablagheKhalesKharid=MablagheEbteda+MablagheKharid-MablagheBargashtAzKharid

		--######خالص فروش#######
		update           #tmpGardesheKala
			set TedadeKhalesForoosh=TedadeForoosh-TedadeBargashtAzForoosh
		update #tmpGardesheKala Set TedadKartonKhalesForoosh=Convert(varchar,floor(TedadeKhalesForoosh / TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(TedadeKhalesForoosh, TedadDarKarton) } ) 
			where TedadDarKarton>0

		update #tmpGardesheKala Set MablagheKhalesForoosh=MablagheForoosh-MablagheBargashtAzForoosh
		--######پایان دوره#####
		update           #tmpGardesheKala
			set tedadePayan=TedadeEbteda+TedadeKharid+TedadeBargashtAzForoosh-TedadeForoosh-TedadeBargashtAzKharid
		update #tmpGardesheKala
			set MablaghePayan=(MablagheEbteda+MablagheKharid)/(TedadeEbteda+TedadeKharid)*tedadePayan
			where TedadeEbteda+TedadeKharid>0
		----کارتن و عدد
		--update #tmpGardesheKala Set TedadeKartonPayan=Convert(varchar,floor(TedadePayan/ TedadDarKarton)) + '-' +  +  Convert(varchar, { fn MOD(tedadePayan, TedadDarKarton) } ) 
		--	where TedadDarKarton>0
		--#######################

		--####انتقال بین انبار و تولید کنترل شود#######

		update #tmpGardesheKala set BahayeTamamShodeForoosh=(MablagheKhalesKharid/TedadeKhalesKharid)*TedadeKhalesForoosh  from #tmpGardesheKala where TedadeKhalesKharid>0  
		update #tmpGardesheKala set Mablaghekharid=0
		update #tmpGardesheKala set MablaghePayan=0
		
		delete  #tmpGardesheKala where  TedadePayan<0 

		


		--update #tmpGardesheKala set BahayeTamamShodeForoosh=MablagheEbteda+MablagheKharid-MablagheBargashtAzKharid-MablaghePayan

		--select BahayeTamamShodeForoosh,MablagheEbteda,MablagheKharid,MablagheBargashtAzKharid,MablaghePayan from #tmpGardesheKala
		--select Sum(MablagheEbteda)   from #tmpGardesheKala
		--select BahayeTamamShodeForoosh from #tmpGardesheKala
		--update #tmpGardesheKala set SoodeKala=MablagheForoosh-BahayeTamamShodeForoosh
		set @MablagheEbtedayeDore=(Select SUM(MablagheEbteda) from #tmpGardesheKala )
		set @MablagheKharidTeyeDore=(Select SUM(MablagheKharid) from #tmpGardesheKala )
		set @MablagheBargashtAzKharid=(Select SUM(MablagheBargashtAzKharid) from #tmpGardesheKala )
		set @MablaghePayanDore=(Select SUM(MablaghePayan) from #tmpGardesheKala )

		update           #tmpGardesheKala
			set bahayetamamshodeforoosh=0 Where bahayetamamshodeforoosh is null

		set @SumBahayeTamamShode=(Select SUM(BahayeTamamShodeForoosh) from #tmpGardesheKala)

		--Select MablagheEbteda,MablagheKharid,MablaghePayan, SUM(BahayeTamamShodeForoosh) as SumBahayeTamamShodeForooshrafte from #tmpGardesheKala
		--Select ROW_NUMBER() OVER(ORDER BY CodeKala ) AS Row, CodeKala,NameGroup,NameKala,TedadDarKarton,TedadeEbteda,TedadKartonEbteda,MablagheEbteda,
		--	TedadeKharid,TedadKartonKharid,MablagheKharid,TedadeForoosh,TedadeKartonForoosh,MablagheForoosh,TedadePayan,TedadeKartonPayan,MablaghePayan,BahayeTamamShodeForoosh ,SoodeKala
		--from #tmpGardesheKala
	
END

GO
/****** Object:  StoredProcedure [dbo].[z151ShowList_ReportAmalkardeVisitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z151ShowList_ReportAmalkardeVisitor]
--چندزبانه
	@ListVisitor varchar(5000)=null,
	@DateAz varchar(50),
	@DateTa varchar(50)
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 


create table #TempShow(
fldCodeVisitor bigint,
fldNameVisitor NVarchar(100) COLLATE Persian_100_CI_AS,
fldTedadForooshJoz decimal(18 ,4),
fldMablaghForoosh decimal(18 ,4),
fldTedadBargashtiJoz decimal(18 ,4),
fldMablaghBargashti decimal(18 ,4),
fldTedadForooshKhalesJoz decimal(18 ,4),
fldMablaghForooshKhales decimal(18 ,4)
)

create table #TempSumForoosh(
fldCodeTafzili bigint,
fldMablaghForoosh decimal(18, 4),
fldTedadJozForoosh decimal(18 ,4),

)

create table #TempSumBargashtAzForoosh(
fldCodeTafzili bigint,
fldMablaghBargasht decimal(18 ,4),
fldTedadJozBargasht decimal(18 ,4),

)

create table #TempSumKhalesForoosh(
fldCodeTafzili bigint,
fldMablaghKhales decimal(18 ,4),
fldTedadJozKhales decimal(18 ,4),

)

	delete from #TempShow
	delete from #TempSumForoosh
	delete from #TempSumBargashtAzForoosh
	delete from #TempSumKhalesForoosh


insert into #TempSumForoosh(fldCodeTafzili,fldMablaghForoosh,fldTedadJozForoosh)
			(select fldCodeVasete,Sum(fldMablaghKhales),Sum(fldTedadJoz) from tblRizeForoosh   where fldDate between @DateAz and @DateTa  group by fldCodeVasete )

insert into #TempSumBargashtAzForoosh(fldCodeTafzili,fldMablaghBargasht,fldTedadJozBargasht)
			(select fldCodeVasete,Sum(fldMablaghKhales),Sum(fldTedadJoz) from tblRizeBargashtAzForoosh   where fldDate between @DateAz and @DateTa  group by fldCodeVasete )

insert into #TempSumKhalesForoosh(fldCodeTafzili,fldMablaghKhales,fldTedadJozKhales)
			(select dbo.viewLoadVizitor.fldCodeTafzili,0,0 from dbo.viewLoadVizitor)

update R Set R.fldMablaghKhales=(#TempSumForoosh.fldMablaghForoosh)-(#TempSumBargashtAzForoosh.fldMablaghBargasht),
								R.fldTedadJozKhales=(#TempSumForoosh.fldTedadJozForoosh)-(#TempSumBargashtAzForoosh.fldTedadJozBargasht)
								from #TempSumKhalesForoosh as R 
								inner join #TempSumForoosh on R.fldCodeTafzili=#TempSumForoosh.fldCodeTafzili
								inner join #TempSumBargashtAzForoosh on R.fldCodeTafzili=#TempSumBargashtAzForoosh.fldCodeTafzili

--update R Set R.fldMablaghKhales=(#TempSumForoosh.fldMablaghForoosh),
--								R.fldTedadJozKhales=(#TempSumForoosh.fldTedadJozForoosh)
--								from #TempSumKhalesForoosh as R 
--								inner join #TempSumForoosh on R.fldCodeTafzili=#TempSumForoosh.fldCodeTafzili						
--								where R.fldMablaghKhales=0

	


insert into #TempShow(
fldCodeVisitor,
fldNameVisitor,
fldTedadForooshJoz,
fldMablaghForoosh,
fldTedadBargashtiJoz ,
fldMablaghBargashti ,
fldTedadForooshKhalesJoz ,
fldMablaghForooshKhales )
	(select #TempSumKhalesForoosh.fldCodeTafzili,dbo.viewLoadVizitor.fldSharheTafzili
	,0 as fldTedadJozForoosh
	,0 as fldMablaghForoosh
	,0 as fldTedadBargashtiJoz 
	,0 as fldMablaghBargashti 
	,0 as fldTedadJozKhales
	,0 as fldMablaghKhales	
	from #TempSumKhalesForoosh inner join
	dbo.viewLoadVizitor on dbo.viewLoadVizitor.fldCodeTafzili=#TempSumKhalesForoosh.fldCodeTafzili 	
			)

update R Set R.fldMablaghForoosh=(#TempSumForoosh.fldMablaghForoosh),
			 R.fldTedadForooshJoz=(#TempSumForoosh.fldTedadJozForoosh)
								from #TempShow as R 
								inner join #TempSumForoosh on R.fldCodeVisitor=#TempSumForoosh.fldCodeTafzili						


update R Set R.fldMablaghBargashti=(#TempSumBargashtAzForoosh.fldMablaghBargasht),
			 R.fldTedadBargashtiJoz=(#TempSumBargashtAzForoosh.fldTedadJozBargasht)
								from #TempShow as R 
								inner join #TempSumBargashtAzForoosh on R.fldCodeVisitor=#TempSumBargashtAzForoosh.fldCodeTafzili						

update R Set R.fldMablaghForooshKhales =(#TempSumKhalesForoosh.fldMablaghKhales),
			 R.fldTedadForooshKhalesJoz=(#TempSumKhalesForoosh.fldTedadJozKhales)
								from #TempShow as R 
								inner join #TempSumKhalesForoosh on R.fldCodeVisitor=#TempSumKhalesForoosh.fldCodeTafzili						

		if @ListVisitor<>''
		      begin
		       declare @StrDeleteVisitor varchar(5000)
		        set  @StrDeleteVisitor='delete from #TempShow 
		            where '+ @ListVisitor + ''
					execute(@StrDeleteVisitor)
		      end

	

SELECT  ROW_NUMBER() OVER(ORDER BY fldCodeVisitor) as fldRadif,fldCodeVisitor,fldNameVisitor,fldTedadForooshJoz,fldMablaghForoosh,
fldTedadBargashtiJoz ,fldMablaghBargashti ,fldTedadForooshKhalesJoz ,fldMablaghForooshKhales             
FROM            #TempShow 

END

 

GO
/****** Object:  StoredProcedure [dbo].[z152ShowList_ReportSarResidKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z152ShowList_ReportSarResidKharid]
--چندزبانه
	@ListAshkhas varchar(5000)=null,
	@DateFactorAz varchar(50),
	@DateFactorTa varchar(50),
	@DateSarResidAz varchar(50),
	@DateSarResidTa varchar(50)
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 


create table #TempShow(
fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablaghKharid decimal(18,4),
fldDateShow  Nvarchar(20) collate Persian_100_CI_AS,
fldDate bigint,
fldDateSarResidShow  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSarResid bigint
)

create table #TempSumKharid(
fldShomareFactor bigint,
fldMablaghKharid decimal(18,4)

)
	delete from #TempShow
	delete from #TempSumKharid

	insert into #TempSumKharid(fldShomareFactor,fldMablaghKharid)
	select fldShomareFactor,sum(fldMablaghKhales)as fldMablaghKharid from tblRizeKharid
	group by fldShomareFactor




	insert into #TempShow(fldShomareFactor,fldCodeTafzili,fldSharhTafzili,fldMablaghKharid,fldDateShow,fldDate,fldDateSarResidShow,fldDateSarResid)
	select distinct dbo.tblRizeKharid.fldShomareFactor
	,dbo.tblRizeKharid.fldCodeTafsili
	,dbo.tblTafzili.fldSharheTafzili,0,
	SUBSTRING(CONVERT(nvarchar, tblKharid.fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblKharid.fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblKharid.fldDate), 7, 2) AS fldDateShow,
							 dbo.tblKharid.fldDate,
	SUBSTRING(CONVERT(nvarchar, tblKharid.fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblKharid.fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblKharid.fldDateSarResid), 7, 2) AS fldDateSarResidShow,
							 dbo.tblKharid.fldDateSarResid
	 from dbo.tblRizeKharid inner join
		dbo.tblTafzili on dbo.tblRizeKharid.fldCodeTafsili=dbo.tblTafzili.fldCodeTafzili inner join
		dbo.tblKharid on dbo.tblRizeKharid.fldShomareFactor=dbo.tblKharid.fldShomareFactorKharid 
		where dbo.tblKharid.fldDate between @DateFactorAz and @DateFactorTa and
			  dbo.tblKharid.fldDateSarResid between @DateSarResidAz and @DateSarResidTa
		
		update R Set R.fldMablaghKharid=(#TempSumKharid.fldMablaghKharid)	
								from #TempShow as R 
								inner join #TempSumKharid on R.fldShomareFactor=#TempSumKharid.fldShomareFactor						

		if @ListAshkhas <>''
		      begin
		       declare @StrDeleteVisitor varchar(5000)
		        set  @StrDeleteVisitor='delete from #TempShow 
		            where '+ @ListAshkhas + ''
					execute(@StrDeleteVisitor)
		      end

	

SELECT  ROW_NUMBER() OVER(ORDER BY fldCodeTafzili) as fldRadif,fldShomareFactor,fldCodeTafzili,fldSharhTafzili,fldMablaghKharid,fldDateShow,fldDate,
fldDateSarResidShow,fldDateSarResid   
FROM  #TempShow  

END

 

GO
/****** Object:  StoredProcedure [dbo].[z153ShowList_ReportAmalKardKarbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z153ShowList_ReportAmalKardKarbar]

	@ListKarbaran varchar(5000)=null,
	@Listform varchar(5000)=null,
	@DateAz varchar(50),
	@DateTa varchar(50)
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 


create table #TempShow(

fldCodeKarbar bigint,
fldShomare bigint,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameForm NVarchar(100) COLLATE Persian_100_CI_AS,
NoeAmaliat NVarchar(100) COLLATE Persian_100_CI_AS,
fldDateShow  Nvarchar(20) collate Persian_100_CI_AS,
fldDate bigint,
fldTime  Nvarchar(20) collate Persian_100_CI_AS
)


	delete from #TempShow
update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')
insert into #TempShow(fldCodeKarbar,fldNameKarbar,fldNameForm,NoeAmaliat,fldShomare,fldDateShow,fldDate,fldTime)
select fldCodeKarbar,fldNameKarbar,fldNameForm,NoeAmaliat,fldShomare,
							SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDateShow,			
							 fldDate,fldTime from tblAmalkardekarbar
							 where fldDate between @DateAz and @DateTa
	

	update #TempShow set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update #TempShow set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')


	if @ListKarbaran <>''
   begin
		 declare @StrDeleteKarbaran varchar(5000)
	     set  @StrDeleteKarbaran='delete from #TempShow 
		 where '+ @ListKarbaran + ''
		execute(@StrDeleteKarbaran)
	end

	if @Listform <>''
		      begin
		       declare @StrDeleteForms varchar(5000)
		        set  @StrDeleteForms='delete from #TempShow 
		            where '+ @Listform + ''
					execute(@StrDeleteForms)
		      end

SELECT  ROW_NUMBER() OVER(ORDER BY fldCodeKarbar) as fldRadif, fldCodeKarbar ,fldNameKarbar,fldNameForm,NoeAmaliat,fldShomare,fldDateShow,fldDate,
fldTime    
FROM  #TempShow  

END

 

GO
/****** Object:  StoredProcedure [dbo].[z154ShowList_Forms]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[z154ShowList_Forms]

	
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;

	select Distinct fldNameForm from tblAmalkardekarbar

END

 

GO
/****** Object:  StoredProcedure [dbo].[z155ShowList_ReportPishfactor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z155ShowList_ReportPishfactor]
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@ListVisitor varchar(5000)=null,
	@DateFactorAz varchar(50),
	@DateFactorTa varchar(50),
	@DatePishFactorAz varchar(50),
	@DatePishFactorTa varchar(50),
	@MablaghAz decimal, 
	@MablaghTa decimal
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 
 create table #TempSumPishForoosh(
fldShomareFactor bigint,
fldMablaghForoosh decimal,
fldCodeAnbar bigint

)
 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)
 create table #TempListDateTimeSabtFactor(
fldShomareFactor bigint,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS
)
create table #TempListDateTimeSabtPishFactor(
fldShomareFactor bigint,
fldTimeSabtPishFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtPishFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS
)
 create table #TempListDateFactor(
fldShomareFactor bigint,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS
)
create table #TempListDatePishFactor(
fldShomareFactor bigint,
fldDatePishFactor  Nvarchar(20) collate Persian_100_CI_AS
)

create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDatePishFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldTimeSabtPishFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtPishFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeVisitor bigint,
fldNameVisitor NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
fldShomareFactorForoosh bigint,
fldLock nvarchar(5)
)

create table #tempPishFactor(
fldShomareFactorForoosh bigint,
fldShomarePishFactor bigint
)

	delete from #TempListDateTimeSabtFactor
	delete from #TempListDateTimeSabtPishFactor
	delete from #TempShow
	delete from #TempSumPishForoosh
	delete from #TempListCodeAnbar

update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizePishForoosh where fldRadif=1)

insert into #TempSumPishForoosh(fldShomareFactor,fldMablaghForoosh,fldCodeAnbar)
			(select tblRizePishForoosh.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizePishForoosh  group by fldShomareFactor )


insert into #TempListDateTimeSabtFactor(fldShomareFactor,fldTimeSabtFactor,fldDateSabtFactor)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblAmalkardekarbar where fldNameForm='فروش' and NoeAmaliat=N'جدید' and fldDate Between @DateFactorAz and @DateFactorTa)
			--select * from #TempListDateTimeFactor

insert into #TempListDateTimeSabtPishFactor(fldShomareFactor,fldTimeSabtPishFactor,fldDateSabtPishFactor,fldNameKarbar)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldNameKarbar from tblAmalkardekarbar where fldNameForm=N'پیش فاکتور' and NoeAmaliat=N'جدید' and fldDate Between @DatePishFactorAz and @DateFactorTa)

insert into #TempListDateFactor(fldShomareFactor,fldDateFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizeForoosh)
			--select * from tblRizeForoosh

insert into #TempListDatePishFactor(fldShomareFactor,fldDatePishFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizePishForoosh)
			
			--select * from #TempListDateTimePishFactor
			--select * from tblAmalkardekarbar
update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumPishForoosh as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldDatePishFactor,fldTimeSabtFactor,fldTimeSabtPishFactor,fldDateSabtFactor,fldDateSabtPishFactor,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldNameKarbar,fldLock)
 select DISTINCT  tblRizePishForoosh.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,'' as fldDateFactor,'' as fldDatePishFactor,
						'' as fldTimeFacttor,'' as fldTimePishFacttor,'' as fldDateFacttor,'' as fldDatePishFacttor,
					tblRizePishForoosh.fldCodeVasete ,''as fldNameVisitor,#TempSumPishForoosh.fldCodeAnbar,'' as fldNameAnbar,'' as fldNameKarbar,tblPishForoosh.fldLock
							  
 from tblRizePishForoosh 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizePishForoosh.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumPishForoosh on #TempSumPishForoosh.fldShomareFactor=tblRizePishForoosh.fldShomareFactor
 inner join tblPishForoosh on #TempSumPishForoosh.fldShomareFactor=tblPishForoosh.fldShomareFactorForoosh




							--SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDateShow

	update R Set R.fldMablagh=(#TempSumPishForoosh.fldMablaghForoosh)
								from #TempShow as R 
								inner join #TempSumPishForoosh on R.fldShomareFactor=#TempSumPishForoosh.fldShomareFactor						
	update R Set R.fldNameVisitor=(viewLoadVizitor.fldSharheTafzili)
								from #TempShow as R 
								inner join viewLoadVizitor on R.fldCodeVisitor=viewLoadVizitor.fldCodeTafzili						

	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode
									
	update R Set R.fldTimeSabtFactor=(#TempListDateTimeSabtFactor.fldTimeSabtFactor),R.fldDateSabtFactor=(#TempListDateTimeSabtFactor.fldDateSabtFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor
	
	update R Set R.fldTimeSabtPishFactor=(#TempListDateTimeSabtPishFactor.fldTimeSabtPishFactor),R.fldDateSabtPishFactor=(#TempListDateTimeSabtPishFactor.fldDateSabtPishFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtPishFactor on R.fldShomareFactor=#TempListDateTimeSabtPishFactor.fldShomareFactor
	
	update R Set R.fldNameKarbar=(#TempListDateTimeSabtPishFactor.fldNameKarbar)
								from #TempShow as R 
								inner join #TempListDateTimeSabtPishFactor on R.fldShomareFactor=#TempListDateTimeSabtPishFactor.fldShomareFactor

	update R Set R.fldDateFactor=(#TempListDateFactor.fldDateFactor)
								from #TempShow as R 
								inner join #TempListDateFactor on R.fldShomareFactor=#TempListDateFactor.fldShomareFactor
	update R Set R.fldDatePishFactor=(#TempListDatePishFactor.fldDatePishFactor)
								from #TempShow as R 
								inner join #TempListDatePishFactor on R.fldShomareFactor=#TempListDatePishFactor.fldShomareFactor
	
	update #TempShow set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempShow.fldCodetafzili=tbl5.fldCodeTafzili

	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end

	if @ListVisitor <>''
		      begin
		       declare @StrDeleteVisitor varchar(5000)
		        set  @StrDeleteVisitor='delete from #TempShow 
		            where '+ @ListVisitor + ''
					execute(@StrDeleteVisitor)
		      end

	 if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end

	insert into #tempPishFactor
		(fldShomareFactorForoosh,fldShomarepishfactor)
	Select  fldShomareFactorForoosh,fldShomarepishfactor from tblForoosh Where (isnumeric(fldShomarePishFactor) = 1) and fldShomarePishFactor<>0


	update #TempShow set fldShomareFactorForoosh=#tempPishFactor.fldShomareFactorForoosh from #tempPishFactor Where #TempShow.fldShomareFactor=#tempPishFactor.fldShomarePishFactor
	
	--update #TempShow set fldShomarePishfactor =tblForoosh.fldShomarePishFactor from tblForoosh Where 
SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldDatePishFactor,fldTimeSabtFactor,fldTimeSabtPishFactor,fldDateSabtFactor,fldDateSabtPishFactor,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldNameKarbar,fldShomareTell,fldShomareFactorForoosh  
,fldLock FROM  #TempShow where 
fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z155ShowList_ReportPishfactor_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z155ShowList_ReportPishfactor_WithUserCode]
--چندزبانه
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@ListVisitor varchar(5000)=null,
	@ListFeeDynamic varchar(5000)=null,
	@DateFactorAz varchar(50),
	@DateFactorTa varchar(50),
	@DatePishFactorAz varchar(50),
	@DatePishFactorTa varchar(50),
	@MablaghAz decimal(18,4), 
	@MablaghTa decimal(18,4),
	@UserCode bigint
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 
 create table #TempSumPishForoosh(
fldShomareFactor bigint,
fldMablaghForoosh decimal(18,4),
fldCodeAnbar bigint

)
 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)
 create table #TempListDateTimeSabtFactor(
fldShomareFactor bigint,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS
)
create table #TempListDateTimeSabtPishFactor(
fldShomareFactor bigint,
fldTimeSabtPishFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtPishFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS
)
 create table #TempListDateFactor(
fldShomareFactor bigint,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS
)
create table #TempListDatePishFactor(
fldShomareFactor bigint,
fldDatePishFactor  Nvarchar(20) collate Persian_100_CI_AS
)

create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal(18,4),
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDatePishFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldTimeSabtPishFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtPishFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeVisitor bigint,
fldNameVisitor NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeFeeDynamic bigint,
fldNameFeeDynamic NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
fldShomareFactorForoosh bigint,
fldLock nvarchar(5)
)

create table #tempPishFactor(
fldShomareFactorForoosh bigint,
fldShomarePishFactor bigint
)

	delete from #TempListDateTimeSabtFactor
	delete from #TempListDateTimeSabtPishFactor
	delete from #TempShow
	delete from #TempSumPishForoosh
	delete from #TempListCodeAnbar

update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizePishForoosh where fldRadif=1)

insert into #TempSumPishForoosh(fldShomareFactor,fldMablaghForoosh,fldCodeAnbar)
			(select tblRizePishForoosh.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizePishForoosh  group by fldShomareFactor )


insert into #TempListDateTimeSabtFactor(fldShomareFactor,fldTimeSabtFactor,fldDateSabtFactor)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblAmalkardekarbar where fldNameForm='فروش' and NoeAmaliat=N'جدید' and fldDate Between @DateFactorAz and @DateFactorTa)
			--select * from #TempListDateTimeFactor

insert into #TempListDateTimeSabtPishFactor(fldShomareFactor,fldTimeSabtPishFactor,fldDateSabtPishFactor,fldNameKarbar)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldNameKarbar from tblAmalkardekarbar where fldNameForm=N'پیش فاکتور' and NoeAmaliat=N'جدید' and fldDate Between @DatePishFactorAz and @DateFactorTa)

insert into #TempListDateFactor(fldShomareFactor,fldDateFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizeForoosh)
			--select * from tblRizeForoosh

insert into #TempListDatePishFactor(fldShomareFactor,fldDatePishFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizePishForoosh)
			
			--select * from #TempListDateTimePishFactor
			--select * from tblAmalkardekarbar
update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumPishForoosh as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldDatePishFactor,fldTimeSabtFactor,fldTimeSabtPishFactor,fldDateSabtFactor,fldDateSabtPishFactor,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldCodeFeeDynamic,fldNameFeeDynamic,fldNameKarbar,fldLock)
 select DISTINCT  tblRizePishForoosh.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,'' as fldDateFactor,'' as fldDatePishFactor,
						'' as fldTimeFacttor,'' as fldTimePishFacttor,'' as fldDateFacttor,'' as fldDatePishFacttor,
					tblRizePishForoosh.fldCodeVasete ,''as fldNameVisitor,#TempSumPishForoosh.fldCodeAnbar,'' as fldNameAnbar,tblPishForoosh.fldCodeFeeDynamic,'' as fldNameFeeDynamic,'' as fldNameKarbar,tblPishForoosh.fldLock
							  
 from tblRizePishForoosh 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizePishForoosh.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumPishForoosh on #TempSumPishForoosh.fldShomareFactor=tblRizePishForoosh.fldShomareFactor
 inner join tblPishForoosh on #TempSumPishForoosh.fldShomareFactor=tblPishForoosh.fldShomareFactorForoosh




							--SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDateShow

	update R Set R.fldMablagh=(#TempSumPishForoosh.fldMablaghForoosh)
								from #TempShow as R 
								inner join #TempSumPishForoosh on R.fldShomareFactor=#TempSumPishForoosh.fldShomareFactor						
	update R Set R.fldNameVisitor=(viewLoadVizitor.fldSharheTafzili)
								from #TempShow as R 
								inner join viewLoadVizitor on R.fldCodeVisitor=viewLoadVizitor.fldCodeTafzili						

	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode
									
	update R Set R.fldTimeSabtFactor=(#TempListDateTimeSabtFactor.fldTimeSabtFactor),R.fldDateSabtFactor=(#TempListDateTimeSabtFactor.fldDateSabtFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor
	
	update R Set R.fldTimeSabtPishFactor=(#TempListDateTimeSabtPishFactor.fldTimeSabtPishFactor),R.fldDateSabtPishFactor=(#TempListDateTimeSabtPishFactor.fldDateSabtPishFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtPishFactor on R.fldShomareFactor=#TempListDateTimeSabtPishFactor.fldShomareFactor
	
	update R Set R.fldNameKarbar=(#TempListDateTimeSabtPishFactor.fldNameKarbar)
								from #TempShow as R 
								inner join #TempListDateTimeSabtPishFactor on R.fldShomareFactor=#TempListDateTimeSabtPishFactor.fldShomareFactor

	update R Set R.fldDateFactor=(#TempListDateFactor.fldDateFactor)
								from #TempShow as R 
								inner join #TempListDateFactor on R.fldShomareFactor=#TempListDateFactor.fldShomareFactor
	update R Set R.fldDatePishFactor=(#TempListDatePishFactor.fldDatePishFactor)
								from #TempShow as R 
								inner join #TempListDatePishFactor on R.fldShomareFactor=#TempListDatePishFactor.fldShomareFactor
	
	update #TempShow set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempShow.fldCodetafzili=tbl5.fldCodeTafzili
	
	update R Set R.fldNameFeeDynamic=(tblSharheFeeDynamic.fldSharh)
								from #TempShow as R 
								inner join tblSharheFeeDynamic on R.fldCodeFeeDynamic=tblSharheFeeDynamic.fldCodeSharh
	
	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end

	if @ListVisitor <>''
		      begin
		       declare @StrDeleteVisitor varchar(5000)
		        set  @StrDeleteVisitor='delete from #TempShow 
		            where '+ @ListVisitor + ''
					execute(@StrDeleteVisitor)
		      end

	 if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end
			  	  	if @ListFeeDynamic <>''
		      begin
		       declare @StrDeleteFeeDynamic varchar(5000)
		        set  @StrDeleteFeeDynamic='delete from #TempShow 
		            where '+ @ListFeeDynamic + ''
					execute(@StrDeleteFeeDynamic)
		      end


	insert into #tempPishFactor
		(fldShomareFactorForoosh,fldShomarepishfactor)
	Select  fldShomareFactorForoosh,fldShomarepishfactor from tblForoosh Where (isnumeric(fldShomarePishFactor) = 1) and fldShomarePishFactor<>0


	update #TempShow set fldShomareFactorForoosh=#tempPishFactor.fldShomareFactorForoosh from #tempPishFactor Where #TempShow.fldShomareFactor=#tempPishFactor.fldShomarePishFactor

-- ########################################################	

declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 

-- ########################################################

	--update #TempShow set fldShomarePishfactor =tblForoosh.fldShomarePishFactor from tblForoosh Where 
SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldDatePishFactor,fldTimeSabtFactor,fldTimeSabtPishFactor,fldDateSabtFactor,fldDateSabtPishFactor,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldNameFeeDynamic,fldNameKarbar,fldShomareTell,fldShomareFactorForoosh  
,fldLock FROM  #TempShow where 
fldCodeTafzili in (select code from @accessCodes) and
fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z156ReportMojoodieBetfkikAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z156ReportMojoodieBetfkikAnbar]
--چندزبانه
  @language nvarchar(10),
  @userCode bigint,
  @MiangineKharid decimal(18,4) output,
  @JameTedad decimal(18,4) output,
  @lngSumTedadeKolSahih decimal(18,4) output,
  @Order int,
  @Filter int,
  @ListCodeKala varchar(5000)=null,
  @ListCodeAnbar varchar(5000)=null,
  @ListGroupKala varchar(5000)=null,
  @DateTa bigint,
  @NoeFeeKharid int,
  @NoeAnbar nvarchar(20)
--@nn nvarchar(10)

as

IF 1=0 BEGIN
 SET FMTONLY OFF
END

begin
 

create table #TempMojoodieAnbar(

fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadeVorood decimal(18,4),
fldTedadeKhorooj decimal(18,4),
fldTedadeJoz decimal(18,4), 
fldTedadeKol decimal(18,4), 
fldTedadDarKarton decimal(18,4),
fldCodeAnbar int,
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldNogheSefaresh decimal(18,4),
fldFeeForoosh money,
fldLastFee money,
fldMianginFee money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldDate bigint,
fldType nvarchar(20) COLLATE Persian_100_CI_AS
)


create table #TempShowMojoodi(

fldCodeKala bigint,
SumVorood decimal(18,4),
SumVoroodT nvarchar(200),
SumKhorooj decimal(18,4),
SumKhoroojT nvarchar(200),
SumMande decimal(18,4),
Mande nvarchar(200),
fldTedadDarKarton decimal(18, 4),
fldTedadKartonadadSahih decimal(18,4),
fldTedadDarKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,
fldCodeAnbar int,
fldNameAnbar Nvarchar(200) COLLATE Persian_100_CI_AS, 
fldTedadVoroodKarton decimal(18,4),
fldTedadVoroodKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,
fldTedadKhoroojKarton decimal(18,4),
fldTedadKhoroojKartonShow nvarchar(70) COLLATE Persian_100_CI_AS,
fldGroupName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeGroup bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldNogheSefaresh decimal(18,4),
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
fldKharid decimal(18,4),
fldArzesheMojoodi decimal(18,4),
fldFeeForoosh decimal(18,4)
)




INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeKharid Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJozNahaei, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeTolid where fldType='mahsool' and flddate<=@DateTa



INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol,fldTedadDarKarton,fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,fldTedadJoz, 0 , - 1 , - 1 ,0, fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, 0 AS Expr8, 
                         fldFeeJoz, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzForoosh Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeForoosh Where flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJozAvalie , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeTolid where fldType='avalie' and flddate<=@DateTa


INSERT INTO #TempMojoodieAnbar
                         (fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
                         fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',- 1 , fldCodeKala, 'NameKala' ,0,fldTedadJoz , - 1 , - 1 , fldCodeAnbar, 'NameAnbar' AS Expr6, - 1 AS Expr7, fldFeeJoz , 
                         0, - 1 AS Expr9, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,fldDate,fldType
FROM            tblRizeBargashtAzKharid Where flddate<=@DateTa


update #TempMojoodieAnbar set fldFeeForoosh=tblKala.fldFeeForoosh from tblKala Where #TempMojoodieAnbar.fldCodeKala=tblKala.fldCodeKala

Insert into #TempMojoodieAnbar
	(fldGroupName,fldCodeGroup, fldCodeKala, fldNameKala,fldTedadeVorood, fldTedadeKhorooj, fldTedadeJoz, fldTedadeKol, fldCodeAnbar, fldNameAnbar, fldNogheSefaresh, fldFeeForoosh, 
		fldLastFee, fldMianginFee, fldNameVahed, fldNameVahed2,fldDate,fldType)
SELECT        'NameGroup',fldCodeGroup, fldCodeKala, fldNameKala ,0,0 , - 1 , - 1 , 0, '' , fldNogheSefaresh, fldFeeForoosh, 
                         0, 0, 'NameVahed1' AS Expr10, 'NameVahed2' AS Expr11,0,''
FROM            tblKala
	where fldCodeKala Not In (Select fldCodeKala from #TempMojoodieAnbar)



if @NoeAnbar='amani'
	delete #TempMojoodieAnbar where fldCodeAnbar<2000
else
	delete #TempMojoodieAnbar where fldCodeAnbar>=2000


UPDATE       #TempMojoodieAnbar
SET                fldCodeGroup = tblKala.fldCodeGroup, fldNameKala = tblKala.fldNameKala, fldNogheSefaresh = tblKala.fldNogheSefaresh,fldTedadDarKarton=tblKala.fldTedadJoz
FROM            tblKala INNER JOIN
                         #TempMojoodieAnbar ON tblKala.fldCodeKala = #TempMojoodieAnbar.fldCodeKala


UPDATE       #TempMojoodieAnbar
SET                #TempMojoodieAnbar.fldGroupName = tblGroupKala.fldGroupName
FROM            tblGroupKala INNER JOIN
                         #TempMojoodieAnbar ON #TempMojoodieAnbar.fldCodeGroup = tblGroupKala.fldGroupId


UPDATE       #TempMojoodieAnbar
SET                fldNameAnbar = tblAnbar.fldNameAnbar
FROM            tblAnbar INNER JOIN
                         #TempMojoodieAnbar ON tblAnbar.fldCode = #TempMojoodieAnbar.fldCodeAnbar


update #tempMojoodieAnbar set #tempMojoodieAnbar.fldNameVahed=tbl2.fldNameVahed,#tempMojoodieAnbar.fldNameVahed2=tbl2.fldNameVahed2
from
(SELECT    fldCodeKala,    tbl1.fldCodeVahed1, tbl1.fldCodeVahed2, tbl1.fldNameVahed, tblVahed.fldNameVahed AS fldNameVahed2
FROM            tblVahed INNER JOIN
                             (SELECT       fldCodeKala, tblKala.fldCodeVahed1, tblKala.fldCodeVahed2, tblVahed_1.fldNameVahed
                                FROM            tblKala INNER JOIN
                                                         tblVahed AS tblVahed_1 ON tblKala.fldCodeVahed1 = tblVahed_1.ID) AS tbl1 ON tblVahed.ID = tbl1.fldCodeVahed2) tbl2

where tbl2.fldCodeKala=#tempMojoodieAnbar.fldCodeKala


if @ListCodeAnbar <>''
		     begin
		      declare @StrDeleteAnbar varchar(5000)
		       set  @StrDeleteAnbar='delete from #TempMojoodieAnbar 
		            where fldCodeAnbar NOT IN ('+ @ListCodeAnbar +')'
					execute(@StrDeleteAnbar)
		     end


--فيلتر تا قبل از تاريخ 



Insert into #TempShowMojoodi
	(SumVorood,SumKhorooj,SumMande,fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala,fldCodeAnbar,fldNameAnbar, fldNogheSefaresh, fldNameVahed, fldNameVahed2,fldKharid,fldFeeForoosh)
SELECT        SUM(fldTedadeVorood) AS SumTedadeVorood, SUM(fldTedadeKhorooj) AS SumTedadeKhorooj, SUM(fldTedadeVorood - fldTedadeKhorooj) AS sumMande, 
                         fldTedadDarKarton, fldGroupName, fldCodeGroup, fldCodeKala, fldNameKala,fldCodeAnbar,fldNameAnbar, fldNogheSefaresh, fldNameVahed, fldNameVahed2,0,fldFeeForoosh
FROM            #TempMojoodieAnbar
GROUP BY fldCodeKala,fldCodeAnbar,fldNameAnbar, fldGroupName, fldCodeGroup, fldNameKala, fldNogheSefaresh, fldNameVahed, fldNameVahed2, fldTedadDarKarton, fldTedadeKol,fldFeeForoosh



update #TempShowMojoodi set Mande=SumMande,SumVoroodT=SumVorood,SumKhoroojT=SumKhorooj
update #TempShowMojoodi set Mande=LEFT(Convert(varchar,SumMande),len(Convert(varchar,SumMande))-3) where right(convert(varchar,SumMande),2)='00'
update #TempShowMojoodi set SumVoroodT=LEFT(Convert(varchar,SumVorood),len(Convert(varchar,SumVorood))-3) where right(convert(varchar,SumVorood),2)='00'
update #TempShowMojoodi set SumKhoroojT=LEFT(Convert(varchar,SumKhorooj),len(Convert(varchar,SumKhorooj))-3) where right(convert(varchar,SumKhorooj),2)='00'

--update #TempShowMojoodi set fldTedadDarKarton=1
--	where fldTedadDarKarton=0

--تعداد کارتن و تعداد جز در آن
update #TempShowMojoodi set fldNameVahed2='' where fldTedadDarKarton=0

update #TempShowMojoodi Set fldTedadDarKartonShow=Convert(varchar,floor(SumMande / fldTedadDarKarton)) + ' ' + fldNameVahed2 + REPLACE(CONVERT(VARCHAR, CONVERT(decimal(18,4), Convert(varchar, { fn MOD(Convert(decimal(18,4), SumMande), fldTedadDarKarton) } )), 2), '.0000', '') + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadVoroodKartonShow=Convert(varchar,  floor(SumVorood / fldTedadDarKarton)) + ' ' + fldNameVahed2 + REPLACE(CONVERT(VARCHAR, CONVERT(decimal(18,4),  Convert(varchar, { fn MOD(Convert(decimal(18,4),SumVorood), fldTedadDarKarton) } )), 2), '.0000', '') + ' ' + fldNameVahed
	where fldTedadDarKarton>0

update #TempShowMojoodi Set fldTedadKhoroojKartonShow=Convert(varchar,floor(SumKhorooj / fldTedadDarKarton)) + ' ' + fldNameVahed2 + REPLACE(CONVERT(VARCHAR, CONVERT(decimal(18,4),  Convert(varchar, { fn MOD(Convert(decimal(18,4),Sumkhorooj), fldTedadDarKarton) } )), 2), '.0000', '')  + ' ' + fldNameVahed
	where fldTedadDarKarton>0


set @MiangineKharid=0


--update #TempShowMojoodi set fldKharid=tbl1.Miangin from
--(SELECT        ROUND(SUM(fldFeeKol) / SUM(fldTedadJoz), 0) AS Miangin, fldCodeKala
--FROM            tblRizeKharid
----WHERE        (fldShomareFactor > 0)
--GROUP BY fldCodeKala) as tbl1
--where tbl1.fldCodeKala=#TempShowMojoodi.fldCodeKala

if @NoeFeeKharid=1 --ميانگين خريد
	update #TempShowMojoodi set fldKharid=tbl1.Miangin from
	(SELECT        ROUND(SUM(fldFeeKol) / SUM(fldTedadJoz), 4) AS Miangin, fldCodeKala
	FROM            tblRizeKharid
	where fldType<>'enteghal'
	GROUP BY fldCodeKala
	having         SUM(fldTedadJoz)>0 
	) as tbl1
	where tbl1.fldCodeKala=#TempShowMojoodi.fldCodeKala 
else --آخرين في خريد
	begin
	--update #TempShowMojoodi set fldKharid=ROUND((tblRizeKharid.fldFeeKol) / (tblRizeKharid.fldTedadJoz), 0) from
	--	tblRizeKharid where  tblRizeKharid.fldCodeKala=#TempShowMojoodi.fldCodeKala 
	
		DECLARE @t TABLE(
		UniqueID INT,
		fldCodeKala bigint,
		[Fee] decimal(18,4),
		fldDate BIGINT
		);

		insert into @t
			(fldCodeKala,[Fee],fldDate)
			Select fldCodeKala, ROUND((tblRizeKharid.fldFeeKol) / (tblRizeKharid.fldTedadJoz), 4),fldDATE from tblRizeKharid where fldType<>'enteghal' and tblRizeKharid.fldTedadJoz>0


		update #TempShowMojoodi set fldKharid=tbl1.[Fee] from 
			(SELECT t.fldCodeKala,MAX(t.[Fee]) AS [Fee],t.fldDate
			FROM @t t
			INNER JOIN(
				SELECT MAX(fldDate) AS fldDate,fldCodeKala
				FROM @t t
				GROUP BY fldCodeKala
			) AS t2
			ON t.[fldDate] = t2.[fldDate] 
				AND t.[fldCodeKala] = t2.fldCodeKala
			GROUP BY 
				t.fldCodeKala,
				t.[fldDate])as tbl1
			Where #TempShowMojoodi.fldCodeKala=tbl1.fldCodeKala

	end
	



--پاک کردن گروه کالاهايي که در گزارش نيستن
if @ListGroupKala<>''
		   begin
		     declare @StrDeleteGroupKala varchar(5000)
		       set  @StrDeleteGroupKala='delete from #TempShowMojoodi 
		            where '+ @ListGroupKala + ''
					execute(@StrDeleteGroupKala)
					end
--پاک کردن کالاهايي که در گزارش نيستن
if @ListCodeKala<>''
		  begin
		   declare @StrDeleteKala varchar(5000)
		    set  @StrDeleteKala='delete from #TempShowMojoodi 
		            where fldCodeKala NOT IN ('+ @ListCodeKala +')'
					execute(@StrDeleteKala)
		  end
--نقطه سفارش
if @Filter=1 	
	begin
		delete from #TempShowMojoodi Where fldNogheSefaresh < SumMande 
		delete from #TempShowMojoodi Where fldNogheSefaresh = 0 
	end
--موجودي مثبت
if @Filter=2
		delete from #TempShowMojoodi Where  SumMande <=0
		

--موجودي منفي
if @Filter=3
		delete from #TempShowMojoodi Where  SumMande >=0

--موجودي صفر
if @Filter=4
		delete from #TempShowMojoodi Where  SumMande <>0


--محاسبه ارزش موجودي هر کالا
update #TempShowMojoodi set fldArzesheMojoodi=0
update #TempShowMojoodi set fldArzesheMojoodi=SumMande*fldKharid where SumMande>0

--محاسبه ميانگين خريد هرکالا
set @MiangineKharid= (Select Sum(fldKharid*SumMande) as SumMiangin from #TempShowMojoodi Where SumMande>0)


if   @MiangineKharid is null
	set @MiangineKharid=0

	

	

Set @JameTedad=(Select Sum(SumMande)  from #TempShowMojoodi Where SumMande>0)
update #TempShowMojoodi set fldTedadKartonadadSahih=(SumMande/fldTedadDarKarton)   Where fldTedadDarKarton>0
update #TempShowMojoodi set fldTedadKartonadadSahih=0 Where isnumeric(fldTedadKartonadadSahih)=0
set @lngSumTedadeKolSahih=(Select Sum(fldTedadKartonadadSahih) from #TempShowMojoodi)


if   @JameTedad is null
	set @JameTedad=0

			--if @ListGroupAshkhas<>''
		 --     begin
		 --      declare @StrDeleteGroupAshkhas varchar(5000)
		 --       set  @StrDeleteGroupAshkhas='delete from #TempRizeForoosh 
		 --           where '+ @ListGroupAshkhas + ''
			--		execute(@StrDeleteGroupAshkhas)
		 --     end


 declare @StrOrder nvarchar(50)
 declare @Select nvarchar(500)

 update #TempShowMojoodi set #TempShowMojoodi.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#TempShowMojoodi.fldCodeKala and tblkalatranslate.fldLanguage=@Language

 Set @Select='Select fldCodeKala ,fldNameKala,fldCodeAnbar,fldNameAnbar,fldNameVahed,fldNameVahed2,SumVoroodT as SumVorood,SumKhoroojT as SumKhorooj,Mande as SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
	fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid,fldArzesheMojoodi,fldFeeForoosh from #TempShowMojoodi '

--Select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumVorood,SumKhorooj,SumMande ,fldTedadDarKarton   ,fldTedadDarKartonShow ,
	--fldTedadVoroodKartonShow ,fldTedadKhoroojKartonShow ,fldCodeGroup,fldGroupName   ,fldNogheSefaresh,fldKharid ,@strorder
	--from #TempShowMojoodi

	
 -- Group Kala access
 Declare @value nvarchar(100)
  set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPKALA')
  if @value = 'True'
  Begin -- Limitation Active  
	-- Get All AccessKala
	declare @accesskala table(code bigint)
	insert into @accesskala 
	exec [ZSP_GetKalaAccesses] @userCode

	delete #TempShowMojoodi where fldCodeGroup not in (select code from @accesskala)
 end

if @Order=0 
	set @StrOrder=' order by fldCodeKala'
else if @order=1 	
	set @StrOrder=' order by fldNameKala'
else if @order=2 
	set @StrOrder=' order by Convert(dec,SumMande) desc'
else if @order=3 
	set @StrOrder=' order by Convert(dec,SumMande) asc'
else if @order=4 
	set @StrOrder=' order by fldKharid desc'	
else if @order=5 
	set @StrOrder=' order by fldKharid asc'
else if @Order=6
	set @StrOrder=' order by fldGroupName desc'
else if @Order=7
	set @StrOrder=' order by fldCodeGroup asc'

execute(@select + @StrOrder)

	 

end

GO
/****** Object:  StoredProcedure [dbo].[z156TekrariRanandeAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[z156TekrariRanandeAnbar]

@fldCodeAnbar int,
@fldCodeRanande int,
@fldRadif int,
@IDType int,
@flagOUt int output
as
begin
	declare @RowCount int
	if @IDType=0  --براي مقايسه براي حالت اضافه کردن رکورد
		begin
		 set @RowCount=(Select Count(fldCodeRanande) from tblEtesalRanandeBeAnbar  Where fldCodeRanande=@fldCodeRanande and fldCodeAnbar= @fldCodeAnbar and fldRadif<>@fldRadif)
		 set @flagOUt=@RowCount
		end
	else
		begin --براي مقايسه با غيرخودش در حالت ويرايش
		   set	@RowCount=(Select  Count(fldCodeRanande) from tblEtesalRanandeBeAnbar  Where fldCodeRanande=@fldCodeRanande and fldCodeAnbar= @fldCodeAnbar and fldRadif<>@fldRadif)
		   set @flagOUt=@RowCount
		end
end

GO
/****** Object:  StoredProcedure [dbo].[z157ReportAmalkardeRanande]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z157ReportAmalkardeRanande] 
--چندزبانه
	@ListGroupAshkhas varchar(5000)=null ,
	@ListCodeAshkhas varchar(5000)=null,
	@ListCodeRanande varchar(5000)=null,
	@CodeTafzili nvarchar(10),		
	@DataType varchar(40),
	@DateSarResidAz bigint,
	@DateSarResidTa bigint,
	@DateFactorAz bigint,
	@DateFactorTa bigint,
	@BedooneRanande bigint,
	@FlagSort Varchar(15),
	@SumFactorha decimal(18, 4) output,
	@SumMandeFactor decimal(18, 4) output,
	@SumMandeTasfieNashode decimal(18, 4) output,
	@SumKoleDaryafti decimal(18, 4) output
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


CREATE TABLE #tmpDaryaftTasfie4(	
	[fldShomareFactor] [bigint]  null,
	[SumDaryafti] decimal(18, 4) null,
	[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldMablagheFactor] decimal(18, 4),
	[fldMablagheMande] decimal(18, 4),
	[fldMablagheBargashti] decimal(18, 4),
	[fldmandeTasfienashode] decimal(18, 4),
	[fldSharh][nvarchar](100) COLLATE Persian_100_CI_AS,	
	[fldDateSarResid][bigint],
	[fldDateFactor][bigint],
	[fldCodeAnbar][bigint],
	[ShomareDaryaft][bigint],
	[SharheTafzili][nvarchar](200) COLLATE Persian_100_CI_AS,
	[codeTafzili][nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldCodeGroupAshkhas][bigint],
	[NameGroup][nvarchar](80) COLLATE Persian_100_CI_AS,
	[fldCodeRanande][bigint],
	[NameRanande][nvarchar](200) COLLATE Persian_100_CI_AS,
	[Tell][nvarchar](50) COLLATE Persian_100_CI_AS,
	[Adress][nvarchar](200) COLLATE Persian_100_CI_AS

	)

	--insert into #tmpDaryaftTasfie4
	--	(fldShomareFactor,SumDaryafti)
	--Select fldShomareFactor ,Sum(fldMablagh) as SumDaryafti from tblRizeTasfie   Group by fldShomareFactor
		
	
	
	insert into #tmpDaryaftTasfie4
			(fldShomareFactor,SumDaryafti,fldMablagheBargashti,fldCodeTafzili,fldMablagheFactor,fldMablagheMande,fldCodeAnbar,fldSharh,fldDateSarResid,fldDateFactor)
			Select distinct dbo.tblTasfie.fldShomareFactor,0,0,fldCodeTafzili,fldMablagheFactor, fldMablagheMandeFactor, fldCodeAnbar ,fldSharh,fldDateSarResid, dbo.tblRizeForoosh.fldDate		
				from  dbo.tblTasfie INNER JOIN
				dbo.tblRizeForoosh  on dbo.tblTasfie.fldShomareFactor=dbo.tblRizeForoosh.fldShomareFactor 

		if @Codetafzili<>0 --يعني همه افراد
			delete #tmpDaryaftTasfie4 where fldCodeTafzili<>@CodeTafzili

	update #tmpDaryaftTasfie4
		set SumDaryafti=tbl1.sumDaryafti from		
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfie where fldShomareDaryaft<>0 group by fldShomareFactor ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor) as tbl1
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl1.fldShomareFactor
	
	--بروزرساني جمع مبلغ برگشتي bm
	update #tmpDaryaftTasfie4
	set fldMablagheBargashti=tbl11.fldMablagheBargashti from
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as fldMablagheBargashti from tblRizeTasfie where fldShomareBargashtAzForoosh<>0 group by fldShomareFactor ) as tbl11
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl11.fldShomareFactor

		
	
	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
	--	(Select fldShomareFactor, fldMablagh from tblRizeTasfie Where fldShomareDaryaft=@ShomareDaryaft) as tbl2
	--	where tbl2.fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor

	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null
	update   #tmpDaryaftTasfie4 set fldmandeTasfienashode=fldMablagheMande-SumDaryafti - fldMablagheBargashti

	--بروزرساني کد تفصيلي
	update #tmpDaryaftTasfie4 set codeTafzili=tbl3.fldCodeTafsili from
		(SELECT        tblForoosh.fldShomareFactorForoosh,tblRizeForoosh.fldCodeTafsili
			FROM            tblForoosh INNER JOIN
                         tblRizeForoosh ON tblForoosh.fldShomareFactorForoosh = tblRizeForoosh.fldShomareFactor) as tbl3
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl3.fldShomareFactorForoosh

	--بروزرساني نام تفصيلي
	update #tmpDaryaftTasfie4 set SharheTafzili=tbl5.fldSharheTafzili from
		(Select fldSharheTafzili,fldCodeTafzili from tblTafzili) as tbl5
		where #tmpDaryaftTasfie4.codeTafzili=tbl5.fldCodeTafzili

	--بروزرساني کدگروه 
	update #tmpDaryaftTasfie4 set fldCodeGroupAshkhas=tbl6.fldCodeGroup from
		(Select fldCodeGroup,fldCodeTafzili from tblTafzili) as tbl6
		Where #tmpDaryaftTasfie4.fldCodeTafzili=tbl6.fldCodeTafzili
	
	--بروزرساني نام گروه
	update #tmpDaryaftTasfie4  set NameGroup=tbl6.fldGroupName from
		(Select fldGroupId,fldGroupName from tblGroupAshkhas) as tbl6
		where tbl6.fldGroupId=#tmpDaryaftTasfie4.fldCodeGroupAshkhas
	

	--بروزرساني کد راننده ها	
	update #tmpDaryaftTasfie4  set fldCodeRanande = tbl7.fldCodeRanande from
		(SELECT        tblForoosh.fldShomareFactorForoosh , tblEtesalRanandeBeAnbar.fldCodeRanande
			FROM            tblForoosh  inner join
							 tblRizeForoosh ON tblRizeForoosh.fldShomareFactor=tblForoosh.fldShomareFactorForoosh INNER JOIN						  
							 tblEtesalRanandeBeAnbar ON tblRizeForoosh.fldCodeAnbar=tblEtesalRanandeBeAnbar.fldCodeAnbar
							 ) as tbl7
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl7.fldShomareFactorForoosh 

	--بروزرساني نام راننده ها
	update #tmpDaryaftTasfie4  set NameRanande =tbl8.fldSharheTafzili from
		(	SELECT        ID, fldSharheTafzili, fldCodeTafzili, fldOstan, fldCity, fldAddress
				FROM            tblTafzili
					Where fldCodeNoe='0' and fldNoePersonel='ranande') as tbl8
		where #tmpDaryaftTasfie4.fldCodeRanande=tbl8.fldCodeTafzili 
	
	--update #tmpDaryaftTasfie4 set fldCodeRanande=0 where NameRanande='' 
 if @BedooneRanande=0
 begin
	delete #tmpDaryaftTasfie4 where fldCodeRanande is  null	
 end
 else
 begin
	 delete #tmpDaryaftTasfie4 where fldCodeRanande is Not null
 end

		
	--بروزرساني تاريخ سررسيد bm
	update #tmpDaryaftTasfie4 set fldDateSarResid=tbl9.fldDateSarResid from
	(select fldDateSarResid,fldShomareFactor from tblTasfie) as tbl9
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl9.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateSarResid< @DateSarResidAz
		delete #tmpDaryaftTasfie4 Where fldDateSarResid> @DateSarResidTa
	--بروزرساني تاريخ فاکتور bm
	update #tmpDaryaftTasfie4 set fldDateFactor=tbl10.fldDate from
	(select fldDate,fldShomareFactor from tblRizeForoosh) as tbl10
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl10.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateFactor< @DateFactorAz
		delete #tmpDaryaftTasfie4 Where fldDateFactor> @DateFactorTa

	 if @ListCodeAshkhas <>''
		begin
			declare @StrDeleteAshkhas varchar(5000)
			set  @StrDeleteAshkhas='delete from #tmpDaryaftTasfie4 
				where fldCodeTafzili NOT IN ('+ @ListCodeAshkhas +')'
				execute(@StrDeleteAshkhas)
		end


		if @ListGroupAshkhas<>''
			begin
			declare @StrDeleteGroupAshkhas varchar(5000)
			set  @StrDeleteGroupAshkhas='delete from #tmpDaryaftTasfie4 
				where '+ @ListGroupAshkhas + ''
				execute(@StrDeleteGroupAshkhas)
			end


		if @ListCodeRanande <>''
			begin
					declare @StrDeleteRanande varchar(5000)
					set  @StrDeleteRanande='delete from #tmpDaryaftTasfie4 
						where '+ @ListCodeRanande + ''
						execute(@StrDeleteRanande)		
			end
		
		

	if @DataType='tasfienashode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande=(SumDaryafti+fldMablagheBargashti)
		end
	if @DataType='tasfieshode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande<>(SumDaryafti+fldMablagheBargashti)
		end
	--بروزرساني تلفن
	declare @EmpCodeTafzili nvarchar(10)
	declare EmpCursor Cursor For
		Select fldCodetafzili from #tmpDaryaftTasfie4  With (NoLock) 
			open EmpCursor
			fetch Next From EmpCursor into @EmpCodeTafzili
			While @@Fetch_Status = 0
				Begin				
					update #tmpDaryaftTasfie4  set  Tell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
							where fldCodetafzili=@EmpCodeTafzili
					Fetch Next from EmpCursor
				into @EmpCodeTafzili
			end
	Close EmpCurSor
	Deallocate EmpCursor


	update #tmpDaryaftTasfie4  set Adress =tbl9.fldAddress from
		(Select fldAddress,fldCodeTafzili from tblTafzili) as tbl9			
		where #tmpDaryaftTasfie4.fldCodeTafzili=tbl9.fldCodeTafzili

	set @sumMandeTasfieNashode=(Select Sum(fldmandeTasfienashode) as SumMande from #tmpDaryaftTasfie4)
	if @sumMandeTasfieNashode is null
		set @sumMandeTasfieNashode=0

	set @SumFactorha=(Select Sum(fldMablagheFactor) as SumMande from #tmpDaryaftTasfie4)
	if @SumFactorha is null
		set @SumFactorha=0

	set @SumMandeFactor=(Select Sum(fldMablagheMande) as SumMande from #tmpDaryaftTasfie4)
	if @SumMandeFactor is null
		set @SumMandeFactor=0


	set @SumKoleDaryafti=(Select Sum(SumDaryafti) as SumMande from #tmpDaryaftTasfie4)
	if @SumKoleDaryafti is null
		set @SumKoleDaryafti=0

	


	if @FlagSort='date'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2)  as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0
				order by fldDateSarResid,fldShomareFactor
		
	if @FlagSort='factor'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by fldShomareFactor,fldDateSarResid
	
	if @FlagSort='bk'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0		
				order by fldmandeTasfienashode desc--,fldShomareFactor,fldDateSarResid

	if @FlagSort='kb'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
				NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by Convert(decimal(18, 4),fldmandeTasfienashode) asc--,fldShomareFactor,fldDateSarResid

	else if @FlagSort='nameshakhs'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by SharheTafzili asc,fldShomareFactor,fldDateSarResid

	else if @FlagSort='namegorooh'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) +'f' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameGroup asc,fldShomareFactor,fldDateSarResid

	else --@FlagSort='namevizitor'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) + 'g' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameRanande asc,fldShomareFactor,fldDateSarResid

	--declare @strEXE nvarchar(300),@Order nvarchar(100)

	--if @FlagSort='date'
	--	set @order=' order by fldDateSarResid,fldShomareFactor'
	--set @strEXE='Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheMande,SumDaryafti,fldmandeTasfienashode, 		
	--	fldDateSarResid ,NameVasete,Tell,Adress,fldSharh from #tmpDaryaftTasfie4' + @order			


END
GO
/****** Object:  StoredProcedure [dbo].[z157ReportAmalkardeRanande_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z157ReportAmalkardeRanande_WithUserCode] 
--چندزبانه
	@ListGroupAshkhas varchar(5000)=null ,
	@ListCodeAshkhas varchar(5000)=null,
	@ListCodeRanande varchar(5000)=null,
	@CodeTafzili nvarchar(10),		
	@DataType varchar(40),
	@DateSarResidAz bigint,
	@DateSarResidTa bigint,
	@DateFactorAz bigint,
	@DateFactorTa bigint,
	@BedooneRanande bigint,
	@FlagSort Varchar(15),
	@SumFactorha decimal(18 ,4) output,
	@SumMandeFactor decimal(18 ,4) output,
	@SumMandeTasfieNashode decimal(18 ,4) output,
	@SumKoleDaryafti decimal(18 ,4) output,
	@userCode bigint
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


CREATE TABLE #tmpDaryaftTasfie4(	
	[fldShomareFactor] [bigint]  null,
	[SumDaryafti] decimal(18, 4) null,
	[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldMablagheFactor] decimal(18 ,4),
	[fldMablagheMande] decimal(18 ,4),
	[fldMablagheBargashti] decimal(18 ,4),
	[fldmandeTasfienashode] decimal(18 ,4),
	[fldSharh][nvarchar](100) COLLATE Persian_100_CI_AS,	
	[fldDateSarResid][bigint],
	[fldDateFactor][bigint],
	[fldCodeAnbar][bigint],
	[ShomareDaryaft][bigint],
	[SharheTafzili][nvarchar](200) COLLATE Persian_100_CI_AS,
	[codeTafzili][nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldCodeGroupAshkhas][bigint],
	[NameGroup][nvarchar](80) COLLATE Persian_100_CI_AS,
	[fldCodeRanande][bigint],
	[NameRanande][nvarchar](200) COLLATE Persian_100_CI_AS,
	[Tell][nvarchar](50) COLLATE Persian_100_CI_AS,
	[Adress][nvarchar](200) COLLATE Persian_100_CI_AS

	)

	--insert into #tmpDaryaftTasfie4
	--	(fldShomareFactor,SumDaryafti)
	--Select fldShomareFactor ,Sum(fldMablagh) as SumDaryafti from tblRizeTasfie   Group by fldShomareFactor
		
	
	
	insert into #tmpDaryaftTasfie4
			(fldShomareFactor,SumDaryafti,fldMablagheBargashti,fldCodeTafzili,fldMablagheFactor,fldMablagheMande,fldCodeAnbar,fldSharh,fldDateSarResid,fldDateFactor)
			Select distinct dbo.tblTasfie.fldShomareFactor,0,0,fldCodeTafzili,fldMablagheFactor, fldMablagheMandeFactor, fldCodeAnbar ,fldSharh,fldDateSarResid, dbo.tblRizeForoosh.fldDate		
				from  dbo.tblTasfie INNER JOIN
				dbo.tblRizeForoosh  on dbo.tblTasfie.fldShomareFactor=dbo.tblRizeForoosh.fldShomareFactor 

		if @Codetafzili<>0 --يعني همه افراد
			delete #tmpDaryaftTasfie4 where fldCodeTafzili<>@CodeTafzili

	update #tmpDaryaftTasfie4
		set SumDaryafti=tbl1.sumDaryafti from		
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfie where fldShomareDaryaft<>0 group by fldShomareFactor ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor) as tbl1
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl1.fldShomareFactor
	
	--بروزرساني جمع مبلغ برگشتي bm
	update #tmpDaryaftTasfie4
	set fldMablagheBargashti=tbl11.fldMablagheBargashti from
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as fldMablagheBargashti from tblRizeTasfie where fldShomareBargashtAzForoosh<>0 group by fldShomareFactor ) as tbl11
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl11.fldShomareFactor

		
	
	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
	--	(Select fldShomareFactor, fldMablagh from tblRizeTasfie Where fldShomareDaryaft=@ShomareDaryaft) as tbl2
	--	where tbl2.fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor

	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null
	update   #tmpDaryaftTasfie4 set fldmandeTasfienashode=fldMablagheMande-SumDaryafti - fldMablagheBargashti

	--بروزرساني کد تفصيلي
	update #tmpDaryaftTasfie4 set codeTafzili=tbl3.fldCodeTafsili from
		(SELECT        tblForoosh.fldShomareFactorForoosh,tblRizeForoosh.fldCodeTafsili
			FROM            tblForoosh INNER JOIN
                         tblRizeForoosh ON tblForoosh.fldShomareFactorForoosh = tblRizeForoosh.fldShomareFactor) as tbl3
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl3.fldShomareFactorForoosh

	--بروزرساني نام تفصيلي
	update #tmpDaryaftTasfie4 set SharheTafzili=tbl5.fldSharheTafzili from
		(Select fldSharheTafzili,fldCodeTafzili from tblTafzili) as tbl5
		where #tmpDaryaftTasfie4.codeTafzili=tbl5.fldCodeTafzili

	--بروزرساني کدگروه 
	update #tmpDaryaftTasfie4 set fldCodeGroupAshkhas=tbl6.fldCodeGroup from
		(Select fldCodeGroup,fldCodeTafzili from tblTafzili) as tbl6
		Where #tmpDaryaftTasfie4.fldCodeTafzili=tbl6.fldCodeTafzili
	
	--بروزرساني نام گروه
	update #tmpDaryaftTasfie4  set NameGroup=tbl6.fldGroupName from
		(Select fldGroupId,fldGroupName from tblGroupAshkhas) as tbl6
		where tbl6.fldGroupId=#tmpDaryaftTasfie4.fldCodeGroupAshkhas
	

	--بروزرساني کد راننده ها	
	update #tmpDaryaftTasfie4  set fldCodeRanande = tbl7.fldCodeRanande from
		(SELECT        tblForoosh.fldShomareFactorForoosh , tblEtesalRanandeBeAnbar.fldCodeRanande
			FROM            tblForoosh  inner join
							 tblRizeForoosh ON tblRizeForoosh.fldShomareFactor=tblForoosh.fldShomareFactorForoosh INNER JOIN						  
							 tblEtesalRanandeBeAnbar ON tblRizeForoosh.fldCodeAnbar=tblEtesalRanandeBeAnbar.fldCodeAnbar
							 ) as tbl7
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl7.fldShomareFactorForoosh 

	--بروزرساني نام راننده ها
	update #tmpDaryaftTasfie4  set NameRanande =tbl8.fldSharheTafzili from
		(	SELECT        ID, fldSharheTafzili, fldCodeTafzili, fldOstan, fldCity, fldAddress
				FROM            tblTafzili
					Where fldCodeNoe='0' and fldNoePersonel='ranande') as tbl8
		where #tmpDaryaftTasfie4.fldCodeRanande=tbl8.fldCodeTafzili 
	
	--update #tmpDaryaftTasfie4 set fldCodeRanande=0 where NameRanande='' 
 if @BedooneRanande=0
 begin
	delete #tmpDaryaftTasfie4 where fldCodeRanande is  null	
 end
 else
 begin
	 delete #tmpDaryaftTasfie4 where fldCodeRanande is Not null
 end

		
	--بروزرساني تاريخ سررسيد bm
	update #tmpDaryaftTasfie4 set fldDateSarResid=tbl9.fldDateSarResid from
	(select fldDateSarResid,fldShomareFactor from tblTasfie) as tbl9
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl9.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateSarResid< @DateSarResidAz
		delete #tmpDaryaftTasfie4 Where fldDateSarResid> @DateSarResidTa
	--بروزرساني تاريخ فاکتور bm
	update #tmpDaryaftTasfie4 set fldDateFactor=tbl10.fldDate from
	(select fldDate,fldShomareFactor from tblRizeForoosh) as tbl10
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl10.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateFactor< @DateFactorAz
		delete #tmpDaryaftTasfie4 Where fldDateFactor> @DateFactorTa

	 if @ListCodeAshkhas <>''
		begin
			declare @StrDeleteAshkhas varchar(5000)
			set  @StrDeleteAshkhas='delete from #tmpDaryaftTasfie4 
				where fldCodeTafzili NOT IN ('+ @ListCodeAshkhas +')'
				execute(@StrDeleteAshkhas)
		end


		if @ListGroupAshkhas<>''
			begin
			declare @StrDeleteGroupAshkhas varchar(5000)
			set  @StrDeleteGroupAshkhas='delete from #tmpDaryaftTasfie4 
				where '+ @ListGroupAshkhas + ''
				execute(@StrDeleteGroupAshkhas)
			end


		if @ListCodeRanande <>''
			begin
					declare @StrDeleteRanande varchar(5000)
					set  @StrDeleteRanande='delete from #tmpDaryaftTasfie4 
						where '+ @ListCodeRanande + ''
						execute(@StrDeleteRanande)		
			end
		
		

	if @DataType='tasfienashode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande=(SumDaryafti+fldMablagheBargashti)
		end
	if @DataType='tasfieshode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande<>(SumDaryafti+fldMablagheBargashti)
		end
	--بروزرساني تلفن
	declare @EmpCodeTafzili nvarchar(10)
	declare EmpCursor Cursor For
		Select fldCodetafzili from #tmpDaryaftTasfie4  With (NoLock) 
			open EmpCursor
			fetch Next From EmpCursor into @EmpCodeTafzili
			While @@Fetch_Status = 0
				Begin				
					update #tmpDaryaftTasfie4  set  Tell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
							where fldCodetafzili=@EmpCodeTafzili
					Fetch Next from EmpCursor
				into @EmpCodeTafzili
			end
	Close EmpCurSor
	Deallocate EmpCursor


	update #tmpDaryaftTasfie4  set Adress =tbl9.fldAddress from
		(Select fldAddress,fldCodeTafzili from tblTafzili) as tbl9			
		where #tmpDaryaftTasfie4.fldCodeTafzili=tbl9.fldCodeTafzili

	set @sumMandeTasfieNashode=(Select Sum(fldmandeTasfienashode) as SumMande from #tmpDaryaftTasfie4)
	if @sumMandeTasfieNashode is null
		set @sumMandeTasfieNashode=0

	set @SumFactorha=(Select Sum(fldMablagheFactor) as SumMande from #tmpDaryaftTasfie4)
	if @SumFactorha is null
		set @SumFactorha=0

	set @SumMandeFactor=(Select Sum(fldMablagheMande) as SumMande from #tmpDaryaftTasfie4)
	if @SumMandeFactor is null
		set @SumMandeFactor=0


	set @SumKoleDaryafti=(Select Sum(SumDaryafti) as SumMande from #tmpDaryaftTasfie4)
	if @SumKoleDaryafti is null
		set @SumKoleDaryafti=0


-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################
delete from #tmpDaryaftTasfie4 where fldCodeTafzili not in (select code from @accessCodes)
-- ##########################################################
	


	if @FlagSort='date'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2)  as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0
				order by fldDateSarResid,fldShomareFactor
		
	if @FlagSort='factor'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by fldShomareFactor,fldDateSarResid
	
	if @FlagSort='bk'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0		
				order by fldmandeTasfienashode desc--,fldShomareFactor,fldDateSarResid

	if @FlagSort='kb'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
				NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by Convert(decimal(18 ,4),fldmandeTasfienashode) asc--,fldShomareFactor,fldDateSarResid

	else if @FlagSort='nameshakhs'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by SharheTafzili asc,fldShomareFactor,fldDateSarResid

	else if @FlagSort='namegorooh'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) +'f' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameGroup asc,fldShomareFactor,fldDateSarResid

	else --@FlagSort='namevizitor'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) + 'g' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameRanande asc,fldShomareFactor,fldDateSarResid

	--declare @strEXE nvarchar(300),@Order nvarchar(100)

	--if @FlagSort='date'
	--	set @order=' order by fldDateSarResid,fldShomareFactor'
	--set @strEXE='Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheMande,SumDaryafti,fldmandeTasfienashode, 		
	--	fldDateSarResid ,NameVasete,Tell,Adress,fldSharh from #tmpDaryaftTasfie4' + @order			


END
GO
/****** Object:  StoredProcedure [dbo].[z158ReportFactorBeTafkikRanande]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[z158ReportFactorBeTafkikRanande] 
	@ListGroupAshkhas varchar(5000)=null ,
	@ListCodeAshkhas varchar(5000)=null,
	@ListCodeRanande varchar(5000)=null,
	@CodeTafzili nvarchar(10),		
	@DataType varchar(40),
	@DateSarResidAz bigint,
	@DateSarResidTa bigint,
	@DateFactorAz bigint,
	@DateFactorTa bigint,
	@BedooneRanande bigint,
	@FlagSort Varchar(15),
	@SumFactorha bigint output,
	@SumMandeFactor bigint output,
	@SumMandeTasfieNashode bigint output,
	@SumKoleDaryafti bigint output
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


CREATE TABLE #tmpDaryaftTasfie4(	
	[fldShomareFactor] [bigint]  null,
	[SumDaryafti] [bigint] null,
	[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldMablagheFactor][bigint],
	[fldMablagheMande][bigint],
	[fldMablagheBargashti][bigint],
	[fldmandeTasfienashode] [bigint],
	[fldSharh][nvarchar](100) COLLATE Persian_100_CI_AS,	
	[fldDateSarResid][bigint],
	[fldDateFactor][bigint],
	[fldCodeAnbar][bigint],
	[ShomareDaryaft][bigint],
	[SharheTafzili][nvarchar](200) COLLATE Persian_100_CI_AS,
	[codeTafzili][nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldCodeGroupAshkhas][bigint],
	[NameGroup][nvarchar](80) COLLATE Persian_100_CI_AS,
	[fldCodeRanande][bigint],
	[NameRanande][nvarchar](200) COLLATE Persian_100_CI_AS,
	[Tell][nvarchar](50) COLLATE Persian_100_CI_AS,
	[Adress][nvarchar](200) COLLATE Persian_100_CI_AS

	)

	--insert into #tmpDaryaftTasfie4
	--	(fldShomareFactor,SumDaryafti)
	--Select fldShomareFactor ,Sum(fldMablagh) as SumDaryafti from tblRizeTasfie   Group by fldShomareFactor
		
	
	
	insert into #tmpDaryaftTasfie4
			(fldShomareFactor,SumDaryafti,fldMablagheBargashti,fldCodeTafzili,fldMablagheFactor,fldMablagheMande,fldCodeAnbar,fldSharh,fldDateSarResid,fldDateFactor)
			Select distinct dbo.tblTasfie.fldShomareFactor,0,0,fldCodeTafzili,fldMablagheFactor, fldMablagheMandeFactor, fldCodeAnbar ,fldSharh,fldDateSarResid, dbo.tblRizeForoosh.fldDate		
				from  dbo.tblTasfie INNER JOIN
				dbo.tblRizeForoosh  on dbo.tblTasfie.fldShomareFactor=dbo.tblRizeForoosh.fldShomareFactor 

		if @Codetafzili<>0 --يعني همه افراد
			delete #tmpDaryaftTasfie4 where fldCodeTafzili<>@CodeTafzili

	update #tmpDaryaftTasfie4
		set SumDaryafti=tbl1.sumDaryafti from		
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfie where fldShomareDaryaft<>0 group by fldShomareFactor ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor) as tbl1
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl1.fldShomareFactor
	
	--بروزرساني جمع مبلغ برگشتي bm
	update #tmpDaryaftTasfie4
	set fldMablagheBargashti=tbl11.fldMablagheBargashti from
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as fldMablagheBargashti from tblRizeTasfie where fldShomareBargashtAzForoosh<>0 group by fldShomareFactor ) as tbl11
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl11.fldShomareFactor

		
	
	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
	--	(Select fldShomareFactor, fldMablagh from tblRizeTasfie Where fldShomareDaryaft=@ShomareDaryaft) as tbl2
	--	where tbl2.fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor

	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null
	update   #tmpDaryaftTasfie4 set fldmandeTasfienashode=fldMablagheMande-SumDaryafti - fldMablagheBargashti

	--بروزرساني کد تفصيلي
	update #tmpDaryaftTasfie4 set codeTafzili=tbl3.fldCodeTafsili from
		(SELECT        tblForoosh.fldShomareFactorForoosh,tblRizeForoosh.fldCodeTafsili
			FROM            tblForoosh INNER JOIN
                         tblRizeForoosh ON tblForoosh.fldShomareFactorForoosh = tblRizeForoosh.fldShomareFactor) as tbl3
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl3.fldShomareFactorForoosh

	--بروزرساني نام تفصيلي
	update #tmpDaryaftTasfie4 set SharheTafzili=tbl5.fldSharheTafzili from
		(Select fldSharheTafzili,fldCodeTafzili from tblTafzili) as tbl5
		where #tmpDaryaftTasfie4.codeTafzili=tbl5.fldCodeTafzili

	--بروزرساني کدگروه 
	update #tmpDaryaftTasfie4 set fldCodeGroupAshkhas=tbl6.fldCodeGroup from
		(Select fldCodeGroup,fldCodeTafzili from tblTafzili) as tbl6
		Where #tmpDaryaftTasfie4.fldCodeTafzili=tbl6.fldCodeTafzili
	
	--بروزرساني نام گروه
	update #tmpDaryaftTasfie4  set NameGroup=tbl6.fldGroupName from
		(Select fldGroupId,fldGroupName from tblGroupAshkhas) as tbl6
		where tbl6.fldGroupId=#tmpDaryaftTasfie4.fldCodeGroupAshkhas
	

	--بروزرساني کد راننده ها	
	update #tmpDaryaftTasfie4  set fldCodeRanande = tbl7.fldCodeRanande from
		(SELECT        tblForoosh.fldShomareFactorForoosh , tblHavaleGoroohi.fldCodeRanande
			FROM            tblForoosh   INNER JOIN						  
							 tblHavaleGoroohi ON tblForoosh.fldShomareFactorForoosh =tblHavaleGoroohi.fldShomareFactor  
												 
							 ) as tbl7
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl7.fldShomareFactorForoosh 

	--بروزرساني نام راننده ها
	update #tmpDaryaftTasfie4  set NameRanande =tbl8.fldSharheTafzili from
		(	SELECT        ID, fldSharheTafzili, fldCodeTafzili, fldOstan, fldCity, fldAddress
				FROM            tblTafzili
					Where fldCodeNoe='0' and fldNoePersonel='ranande') as tbl8
		where #tmpDaryaftTasfie4.fldCodeRanande=tbl8.fldCodeTafzili 
	
	--update #tmpDaryaftTasfie4 set fldCodeRanande=0 where NameRanande='' 
 if @BedooneRanande=0
 begin
	delete #tmpDaryaftTasfie4 where fldCodeRanande is  null	
 end
 else
 begin
	 delete #tmpDaryaftTasfie4 where fldCodeRanande is Not null
 end

		
	--بروزرساني تاريخ سررسيد bm
	update #tmpDaryaftTasfie4 set fldDateSarResid=tbl9.fldDateSarResid from
	(select fldDateSarResid,fldShomareFactor from tblTasfie) as tbl9
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl9.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateSarResid< @DateSarResidAz
		delete #tmpDaryaftTasfie4 Where fldDateSarResid> @DateSarResidTa
	--بروزرساني تاريخ فاکتور bm
	update #tmpDaryaftTasfie4 set fldDateFactor=tbl10.fldDate from
	(select fldDate,fldShomareFactor from tblRizeForoosh) as tbl10
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl10.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateFactor< @DateFactorAz
		delete #tmpDaryaftTasfie4 Where fldDateFactor> @DateFactorTa

	 if @ListCodeAshkhas <>''
		begin
			declare @StrDeleteAshkhas varchar(5000)
			set  @StrDeleteAshkhas='delete from #tmpDaryaftTasfie4 
				where fldCodeTafzili NOT IN ('+ @ListCodeAshkhas +')'
				execute(@StrDeleteAshkhas)
		end


		if @ListGroupAshkhas<>''
			begin
			declare @StrDeleteGroupAshkhas varchar(5000)
			set  @StrDeleteGroupAshkhas='delete from #tmpDaryaftTasfie4 
				where '+ @ListGroupAshkhas + ''
				execute(@StrDeleteGroupAshkhas)
			end


		if @ListCodeRanande <>''
			begin
					declare @StrDeleteRanande varchar(5000)
					set  @StrDeleteRanande='delete from #tmpDaryaftTasfie4 
						where '+ @ListCodeRanande + ''
						execute(@StrDeleteRanande)		
			end
		
		

	if @DataType='tasfienashode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande=(SumDaryafti+fldMablagheBargashti)
		end
	if @DataType='tasfieshode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande<>(SumDaryafti+fldMablagheBargashti)
		end
	--بروزرساني تلفن
	declare @EmpCodeTafzili nvarchar(10)
	declare EmpCursor Cursor For
		Select fldCodetafzili from #tmpDaryaftTasfie4  With (NoLock) 
			open EmpCursor
			fetch Next From EmpCursor into @EmpCodeTafzili
			While @@Fetch_Status = 0
				Begin				
					update #tmpDaryaftTasfie4  set  Tell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
							where fldCodetafzili=@EmpCodeTafzili
					Fetch Next from EmpCursor
				into @EmpCodeTafzili
			end
	Close EmpCurSor
	Deallocate EmpCursor


	update #tmpDaryaftTasfie4  set Adress =tbl9.fldAddress from
		(Select fldAddress,fldCodeTafzili from tblTafzili) as tbl9			
		where #tmpDaryaftTasfie4.fldCodeTafzili=tbl9.fldCodeTafzili

	set @sumMandeTasfieNashode=(Select Sum(fldmandeTasfienashode) as SumMande from #tmpDaryaftTasfie4)
	if @sumMandeTasfieNashode is null
		set @sumMandeTasfieNashode=0

	set @SumFactorha=(Select Sum(fldMablagheFactor) as SumMande from #tmpDaryaftTasfie4)
	if @SumFactorha is null
		set @SumFactorha=0

	set @SumMandeFactor=(Select Sum(fldMablagheMande) as SumMande from #tmpDaryaftTasfie4)
	if @SumMandeFactor is null
		set @SumMandeFactor=0


	set @SumKoleDaryafti=(Select Sum(SumDaryafti) as SumMande from #tmpDaryaftTasfie4)
	if @SumKoleDaryafti is null
		set @SumKoleDaryafti=0

	


	if @FlagSort='date'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2)  as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0
				order by fldDateSarResid,fldShomareFactor
		
	if @FlagSort='factor'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by fldShomareFactor,fldDateSarResid
	
	if @FlagSort='bk'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0		
				order by fldmandeTasfienashode desc--,fldShomareFactor,fldDateSarResid

	if @FlagSort='kb'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
				NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by Convert(bigint,fldmandeTasfienashode) asc--,fldShomareFactor,fldDateSarResid

	else if @FlagSort='nameshakhs'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by SharheTafzili asc,fldShomareFactor,fldDateSarResid

	else if @FlagSort='namegorooh'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) +'f' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameGroup asc,fldShomareFactor,fldDateSarResid

	else --@FlagSort='namevizitor'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) + 'g' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameRanande asc,fldShomareFactor,fldDateSarResid

	--declare @strEXE nvarchar(300),@Order nvarchar(100)

	--if @FlagSort='date'
	--	set @order=' order by fldDateSarResid,fldShomareFactor'
	--set @strEXE='Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheMande,SumDaryafti,fldmandeTasfienashode, 		
	--	fldDateSarResid ,NameVasete,Tell,Adress,fldSharh from #tmpDaryaftTasfie4' + @order			


END

GO
/****** Object:  StoredProcedure [dbo].[z158ReportFactorBeTafkikRanande_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z158ReportFactorBeTafkikRanande_WithUserCode] 
--چندزبانه
	@ListGroupAshkhas varchar(5000)=null ,
	@ListCodeAshkhas varchar(5000)=null,
	@ListCodeRanande varchar(5000)=null,
	@CodeTafzili nvarchar(10),		
	@DataType varchar(40),
	@DateSarResidAz bigint,
	@DateSarResidTa bigint,
	@DateFactorAz bigint,
	@DateFactorTa bigint,
	@BedooneRanande bigint,
	@FlagSort Varchar(15),
	@SumFactorha decimal(18,4) output,
	@SumMandeFactor decimal(18,4) output,
	@SumMandeTasfieNashode decimal(18,4) output,
	@SumKoleDaryafti decimal(18,4) output,
	@userCode decimal(18,4)
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


CREATE TABLE #tmpDaryaftTasfie4(	
	[fldShomareFactor] [bigint]  null,
	[SumDaryafti] decimal(18,4) null,
	[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldMablagheFactor]decimal(18,4),
	[fldMablagheMande]decimal(18,4),
	[fldMablagheBargashti]decimal(18,4),
	[fldmandeTasfienashode] decimal(18,4),
	[fldSharh][nvarchar](100) COLLATE Persian_100_CI_AS,	
	[fldDateSarResid][bigint],
	[fldDateFactor][bigint],
	[fldCodeAnbar][bigint],
	[ShomareDaryaft]decimal(18,4),
	[SharheTafzili][nvarchar](200) COLLATE Persian_100_CI_AS,
	[codeTafzili][nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldCodeGroupAshkhas][bigint],
	[NameGroup][nvarchar](80) COLLATE Persian_100_CI_AS,
	[fldCodeRanande][bigint],
	[NameRanande][nvarchar](200) COLLATE Persian_100_CI_AS,
	[Tell][nvarchar](50) COLLATE Persian_100_CI_AS,
	[Adress][nvarchar](200) COLLATE Persian_100_CI_AS

	)

	--insert into #tmpDaryaftTasfie4
	--	(fldShomareFactor,SumDaryafti)
	--Select fldShomareFactor ,Sum(fldMablagh) as SumDaryafti from tblRizeTasfie   Group by fldShomareFactor
		
	
	
	insert into #tmpDaryaftTasfie4
			(fldShomareFactor,SumDaryafti,fldMablagheBargashti,fldCodeTafzili,fldMablagheFactor,fldMablagheMande,fldCodeAnbar,fldSharh,fldDateSarResid,fldDateFactor)
			Select distinct dbo.tblTasfie.fldShomareFactor,0,0,fldCodeTafzili,fldMablagheFactor, fldMablagheMandeFactor, fldCodeAnbar ,fldSharh,fldDateSarResid, dbo.tblRizeForoosh.fldDate		
				from  dbo.tblTasfie INNER JOIN
				dbo.tblRizeForoosh  on dbo.tblTasfie.fldShomareFactor=dbo.tblRizeForoosh.fldShomareFactor 

		if @Codetafzili<>0 --يعني همه افراد
			delete #tmpDaryaftTasfie4 where fldCodeTafzili<>@CodeTafzili

	update #tmpDaryaftTasfie4
		set SumDaryafti=tbl1.sumDaryafti from		
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfie where fldShomareDaryaft<>0 group by fldShomareFactor ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor) as tbl1
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl1.fldShomareFactor
	
	--بروزرساني جمع مبلغ برگشتي bm
	update #tmpDaryaftTasfie4
	set fldMablagheBargashti=tbl11.fldMablagheBargashti from
		(Select fldShomareFactor, isnull(Sum(fldMablagh),0) as fldMablagheBargashti from tblRizeTasfie where fldShomareBargashtAzForoosh<>0 group by fldShomareFactor ) as tbl11
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl11.fldShomareFactor

		
	
	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
	--	(Select fldShomareFactor, fldMablagh from tblRizeTasfie Where fldShomareDaryaft=@ShomareDaryaft) as tbl2
	--	where tbl2.fldShomareFactor=#tmpDaryaftTasfie4.fldShomareFactor

	--update #tmpDaryaftTasfie4 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null
	update   #tmpDaryaftTasfie4 set fldmandeTasfienashode=fldMablagheMande-SumDaryafti - fldMablagheBargashti

	--بروزرساني کد تفصيلي
	update #tmpDaryaftTasfie4 set codeTafzili=tbl3.fldCodeTafsili from
		(SELECT        tblForoosh.fldShomareFactorForoosh,tblRizeForoosh.fldCodeTafsili
			FROM            tblForoosh INNER JOIN
                         tblRizeForoosh ON tblForoosh.fldShomareFactorForoosh = tblRizeForoosh.fldShomareFactor) as tbl3
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl3.fldShomareFactorForoosh

	--بروزرساني نام تفصيلي
	update #tmpDaryaftTasfie4 set SharheTafzili=tbl5.fldSharheTafzili from
		(Select fldSharheTafzili,fldCodeTafzili from tblTafzili) as tbl5
		where #tmpDaryaftTasfie4.codeTafzili=tbl5.fldCodeTafzili

	--بروزرساني کدگروه 
	update #tmpDaryaftTasfie4 set fldCodeGroupAshkhas=tbl6.fldCodeGroup from
		(Select fldCodeGroup,fldCodeTafzili from tblTafzili) as tbl6
		Where #tmpDaryaftTasfie4.fldCodeTafzili=tbl6.fldCodeTafzili
	
	--بروزرساني نام گروه
	update #tmpDaryaftTasfie4  set NameGroup=tbl6.fldGroupName from
		(Select fldGroupId,fldGroupName from tblGroupAshkhas) as tbl6
		where tbl6.fldGroupId=#tmpDaryaftTasfie4.fldCodeGroupAshkhas
	

	--بروزرساني کد راننده ها	
	update #tmpDaryaftTasfie4  set fldCodeRanande = tbl7.fldCodeRanande from
		(SELECT        tblForoosh.fldShomareFactorForoosh , tblHavaleGoroohi.fldCodeRanande
			FROM            tblForoosh   INNER JOIN						  
							 tblHavaleGoroohi ON tblForoosh.fldShomareFactorForoosh =tblHavaleGoroohi.fldShomareFactor  
												 
							 ) as tbl7
		where #tmpDaryaftTasfie4.fldShomareFactor=tbl7.fldShomareFactorForoosh 

	--بروزرساني نام راننده ها
	update #tmpDaryaftTasfie4  set NameRanande =tbl8.fldSharheTafzili from
		(	SELECT        ID, fldSharheTafzili, fldCodeTafzili, fldOstan, fldCity, fldAddress
				FROM            tblTafzili
					Where fldCodeNoe='0' and fldNoePersonel='ranande') as tbl8
		where #tmpDaryaftTasfie4.fldCodeRanande=tbl8.fldCodeTafzili 
	
	--update #tmpDaryaftTasfie4 set fldCodeRanande=0 where NameRanande='' 
 if @BedooneRanande=0
 begin
	delete #tmpDaryaftTasfie4 where fldCodeRanande is  null	
 end
 else
 begin
	 delete #tmpDaryaftTasfie4 where fldCodeRanande is Not null
 end

		
	--بروزرساني تاريخ سررسيد bm
	update #tmpDaryaftTasfie4 set fldDateSarResid=tbl9.fldDateSarResid from
	(select fldDateSarResid,fldShomareFactor from tblTasfie) as tbl9
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl9.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateSarResid< @DateSarResidAz
		delete #tmpDaryaftTasfie4 Where fldDateSarResid> @DateSarResidTa
	--بروزرساني تاريخ فاکتور bm
	update #tmpDaryaftTasfie4 set fldDateFactor=tbl10.fldDate from
	(select fldDate,fldShomareFactor from tblRizeForoosh) as tbl10
	where #tmpDaryaftTasfie4.fldShomareFactor=tbl10.fldShomareFactor
		delete #tmpDaryaftTasfie4 Where fldDateFactor< @DateFactorAz
		delete #tmpDaryaftTasfie4 Where fldDateFactor> @DateFactorTa

	 if @ListCodeAshkhas <>''
		begin
			declare @StrDeleteAshkhas varchar(5000)
			set  @StrDeleteAshkhas='delete from #tmpDaryaftTasfie4 
				where fldCodeTafzili NOT IN ('+ @ListCodeAshkhas +')'
				execute(@StrDeleteAshkhas)
		end


		if @ListGroupAshkhas<>''
			begin
			declare @StrDeleteGroupAshkhas varchar(5000)
			set  @StrDeleteGroupAshkhas='delete from #tmpDaryaftTasfie4 
				where '+ @ListGroupAshkhas + ''
				execute(@StrDeleteGroupAshkhas)
			end


		if @ListCodeRanande <>''
			begin
					declare @StrDeleteRanande varchar(5000)
					set  @StrDeleteRanande='delete from #tmpDaryaftTasfie4 
						where '+ @ListCodeRanande + ''
						execute(@StrDeleteRanande)		
			end
		
		

	if @DataType='tasfienashode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande=(SumDaryafti+fldMablagheBargashti)
		end
	if @DataType='tasfieshode'
	begin
		delete from #tmpDaryaftTasfie4 where fldMablagheMande<>(SumDaryafti+fldMablagheBargashti)
		end
	--بروزرساني تلفن
	declare @EmpCodeTafzili nvarchar(10)
	declare EmpCursor Cursor For
		Select fldCodetafzili from #tmpDaryaftTasfie4  With (NoLock) 
			open EmpCursor
			fetch Next From EmpCursor into @EmpCodeTafzili
			While @@Fetch_Status = 0
				Begin				
					update #tmpDaryaftTasfie4  set  Tell=(Select top(1)  fldShomare from tblDaftarcheTell Where fldCodetafzili=@EmpCodeTafzili)
							where fldCodetafzili=@EmpCodeTafzili
					Fetch Next from EmpCursor
				into @EmpCodeTafzili
			end
	Close EmpCurSor
	Deallocate EmpCursor


	update #tmpDaryaftTasfie4  set Adress =tbl9.fldAddress from
		(Select fldAddress,fldCodeTafzili from tblTafzili) as tbl9			
		where #tmpDaryaftTasfie4.fldCodeTafzili=tbl9.fldCodeTafzili

	set @sumMandeTasfieNashode=(Select Sum(fldmandeTasfienashode) as SumMande from #tmpDaryaftTasfie4)
	if @sumMandeTasfieNashode is null
		set @sumMandeTasfieNashode=0

	set @SumFactorha=(Select Sum(fldMablagheFactor) as SumMande from #tmpDaryaftTasfie4)
	if @SumFactorha is null
		set @SumFactorha=0

	set @SumMandeFactor=(Select Sum(fldMablagheMande) as SumMande from #tmpDaryaftTasfie4)
	if @SumMandeFactor is null
		set @SumMandeFactor=0


	set @SumKoleDaryafti=(Select Sum(SumDaryafti) as SumMande from #tmpDaryaftTasfie4)
	if @SumKoleDaryafti is null
		set @SumKoleDaryafti=0

-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################
delete from #tmpDaryaftTasfie4 
where fldCodeTafzili not in (select code from @accessCodes)
-- ##########################################################

	if @FlagSort='date'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2)  as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0
				order by fldDateSarResid,fldShomareFactor
		
	if @FlagSort='factor'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by fldShomareFactor,fldDateSarResid
	
	if @FlagSort='bk'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
			NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4	
				--where fldmandeTasfienashode>0		
				order by fldmandeTasfienashode desc--,fldShomareFactor,fldDateSarResid

	if @FlagSort='kb'
			Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
				substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
				substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
				NameRanande,Tell,Adress,fldSharh
				from #tmpDaryaftTasfie4		
				--where fldmandeTasfienashode>0	
				order by Convert(decimal(18,4),fldmandeTasfienashode) asc--,fldShomareFactor,fldDateSarResid

	else if @FlagSort='nameshakhs'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by SharheTafzili asc,fldShomareFactor,fldDateSarResid

	else if @FlagSort='namegorooh'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) +'f' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameGroup asc,fldShomareFactor,fldDateSarResid

	else --@FlagSort='namevizitor'
		Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheFactor,fldMablagheMande,SumDaryafti,fldMablagheBargashti,fldmandeTasfienashode,
			substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) + 'g' as fldDateSarResid,	
			substring(convert(nvarchar(10),fldDateFactor),0,5) +'/' +substring(convert(nvarchar(10),fldDateFactor),5,2) + '/' + substring(convert(nvarchar(10),fldDateFactor),7,2)  as fldTarikhFactor,					
		NameRanande,Tell,Adress,fldSharh
			from #tmpDaryaftTasfie4		
			--where fldmandeTasfienashode>0	
			order by NameRanande asc,fldShomareFactor,fldDateSarResid

	--declare @strEXE nvarchar(300),@Order nvarchar(100)

	--if @FlagSort='date'
	--	set @order=' order by fldDateSarResid,fldShomareFactor'
	--set @strEXE='Select codeTafzili,SharheTafzili,NameGroup,fldShomareFactor,fldMablagheMande,SumDaryafti,fldmandeTasfienashode, 		
	--	fldDateSarResid ,NameVasete,Tell,Adress,fldSharh from #tmpDaryaftTasfie4' + @order			


END

GO
/****** Object:  StoredProcedure [dbo].[z160ShowList_ReportSefareshVisitor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z160ShowList_ReportSefareshVisitor]


	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@ListVisitor varchar(5000)=null,
	@DateAz varchar(50),
	@DateTa varchar(50),
	@MablaghAz decimal, 
	@MablaghTa decimal
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 
 create table #TempSumPishPishForoosh(
fldShomareFactor bigint,
fldMablaghForoosh decimal,
fldCodeAnbar bigint

)
 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)


create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal,
fldDateShow  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeVisitor bigint,
fldNameVisitor NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldDate bigint,
fldLock nvarchar(5)
)


	delete from #TempShow
	delete from #TempSumPishPishForoosh
	delete from #TempListCodeAnbar
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizePishPishForoosh where fldRadif=1)

insert into #TempSumPishPishForoosh(fldShomareFactor,fldMablaghForoosh,fldCodeAnbar)
			(select tblRizePishPishForoosh.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizePishPishForoosh where fldDate between @DateAz and @DateTa group by fldShomareFactor )

update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumPishPishForoosh as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateShow,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldDate,fldLock)
 select DISTINCT  tblRizePishPishForoosh.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,SUBSTRING(CONVERT(nvarchar,tblRizePishPishForoosh.fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblRizePishPishForoosh.fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblRizePishPishForoosh.fldDate), 7, 2) AS fldDateShow,
							 tblRizePishPishForoosh.fldCodeVasete ,''as fldNameVisitor,#TempSumPishPishForoosh.fldCodeAnbar,'' as fldNameAnbar,
							 tblRizePishPishForoosh.fldDate ,tblPishPishForoosh.fldLock
 from tblRizePishPishForoosh 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizePishPishForoosh.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumPishPishForoosh on #TempSumPishPishForoosh.fldShomareFactor=tblRizePishPishForoosh.fldShomareFactor
  inner join tblPishPishForoosh on tblPishPishForoosh.fldShomareFactorForoosh=tblRizePishPishForoosh.fldShomareFactor

	update R Set R.fldMablagh=(#TempSumPishPishForoosh.fldMablaghForoosh)
								from #TempShow as R 
								inner join #TempSumPishPishForoosh on R.fldShomareFactor=#TempSumPishPishForoosh.fldShomareFactor						
	update R Set R.fldNameVisitor=(viewLoadVizitor.fldSharheTafzili)
								from #TempShow as R 
								inner join viewLoadVizitor on R.fldCodeVisitor=viewLoadVizitor.fldCodeTafzili						

	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode	
	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end

	if @ListVisitor <>''
		      begin
		       declare @StrDeleteVisitor varchar(5000)
		        set  @StrDeleteVisitor='delete from #TempShow 
		            where '+ @ListVisitor + ''
					execute(@StrDeleteVisitor)
		      end
	

SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateShow,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldDate,fldLock   
FROM  #TempShow 
where fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)
END

 




GO
/****** Object:  StoredProcedure [dbo].[z160ShowList_ReportSefareshVisitor_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z160ShowList_ReportSefareshVisitor_WithUserCode]
--چندزبانه

	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@ListVisitor varchar(5000)=null,
	@DateAz varchar(50),
	@DateTa varchar(50),
	@MablaghAz decimal(18,4), 
	@MablaghTa decimal(18,4),
	@userCode bigint
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 
 create table #TempSumPishPishForoosh(
fldShomareFactor bigint,
fldMablaghForoosh decimal(18,4),
fldCodeAnbar bigint

)
 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)


create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal(18,4),
fldDateShow  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeVisitor bigint,
fldNameVisitor NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldDate bigint,
fldLock nvarchar(5)
)


	delete from #TempShow
	delete from #TempSumPishPishForoosh
	delete from #TempListCodeAnbar
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizePishPishForoosh where fldRadif=1)

insert into #TempSumPishPishForoosh(fldShomareFactor,fldMablaghForoosh,fldCodeAnbar)
			(select tblRizePishPishForoosh.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizePishPishForoosh where fldDate between @DateAz and @DateTa group by fldShomareFactor )

update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumPishPishForoosh as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateShow,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldDate,fldLock)
 select DISTINCT  tblRizePishPishForoosh.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,SUBSTRING(CONVERT(nvarchar,tblRizePishPishForoosh.fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblRizePishPishForoosh.fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblRizePishPishForoosh.fldDate), 7, 2) AS fldDateShow,
							 tblRizePishPishForoosh.fldCodeVasete ,''as fldNameVisitor,#TempSumPishPishForoosh.fldCodeAnbar,'' as fldNameAnbar,
							 tblRizePishPishForoosh.fldDate ,tblPishPishForoosh.fldLock
 from tblRizePishPishForoosh 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizePishPishForoosh.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumPishPishForoosh on #TempSumPishPishForoosh.fldShomareFactor=tblRizePishPishForoosh.fldShomareFactor
  inner join tblPishPishForoosh on tblPishPishForoosh.fldShomareFactorForoosh=tblRizePishPishForoosh.fldShomareFactor

	update R Set R.fldMablagh=(#TempSumPishPishForoosh.fldMablaghForoosh)
								from #TempShow as R 
								inner join #TempSumPishPishForoosh on R.fldShomareFactor=#TempSumPishPishForoosh.fldShomareFactor						
	update R Set R.fldNameVisitor=(viewLoadVizitor.fldSharheTafzili)
								from #TempShow as R 
								inner join viewLoadVizitor on R.fldCodeVisitor=viewLoadVizitor.fldCodeTafzili						

	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode	
	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end

	if @ListVisitor <>''
		      begin
		       declare @StrDeleteVisitor varchar(5000)
		        set  @StrDeleteVisitor='delete from #TempShow 
		            where '+ @ListVisitor + ''
					execute(@StrDeleteVisitor)
		      end

-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################	

SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateShow,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldDate,fldLock   
FROM  #TempShow 
where 
fldCodeTafzili in (select code from @accessCodes) and
fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)
END

 




GO
/****** Object:  StoredProcedure [dbo].[z161InsertSabteVamInfo]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z161InsertSabteVamInfo]
						@fldCodeArz int,
						@fldNerkheArz decimal(18,4),
						@language nvarchar(10),
						@fldTypemohasebe int,
						@fldShomareFactor bigint,
                        @fldCodeTafzili bigint,
                        @fldMablaghVam decimal(18,4),
                        @fldTedadAghsat bigint,
                        @fldShomareVam bigint,
                        @fldDarsadKarmozd decimal(18,4),
                        @fldDate bigint,
                        @fldTypeDorePardakht Nvarchar(50),
                        @fldDorePardakht Nvarchar(50),
                        @fldMablaghKarmozd decimal,
                        @fldMablaghVamBaKarmozd decimal,
                        @fldMablaghGhestBaKarmozd decimal,
                        @fldMablaghghestGhablAzKarmozd decimal,
                        @fldSharh Nvarchar(250),
                        @tblAghsat [dtAghsat-Multi]  readonly,
                        @strResult1 nvarchar(30) output,
						@strResult2 bigint output
as 


IF 1=0 BEGIN
 SET FMTONLY OFF
END

begin


begin transaction AddRecord
		begin try

declare @MaxShomareVam bigint
declare @ShomareSanad bigint	
declare @SharhSanad nvarchar(50) 
declare @day nvarchar(50)  
declare @month nvarchar(50) 
declare @year nvarchar(50) 

Set @MaxShomareVam=(select Max(fldShomareVam) from tblVamInfo)
if isnumeric(@MaxShomareVam)=0
	set @MaxShomareVam=0

Set @year=SUBSTRING(CONVERT(nvarchar,@fldDate), 1, 4)
Set @month=SUBSTRING(CONVERT(nvarchar,@fldDate), 5, 2)
Set @day=SUBSTRING(CONVERT(nvarchar,@fldDate), 7, 2)
Set @SharhSanad=(Select * from dbo.translatefunction(@LANGUAGE,18290)) + ' '+ Convert(varchar, @MaxShomareVam+1)

declare @Date6Ragham bigint,@TooleKol bigint,@Toolemoin bigint,@CodeMoinHesabhayeDaryaftani nvarchar(100),@CodeMoinKarmozdeVam nvarchar(100),@CodeKolkham nvarchar(10),@CodeMoinKham nvarchar(10)

--execute Date6Raghami @fldDate,@Date6Ragham output

set @Date6Ragham = @fldDate


execute  ToolePaye 'kol',@TooleKol output
execute  ToolePaye 'moin',@Toolemoin output
execute codemoinhesab 'hesabhayedaryaftani' ,@CodeMoinHesabhayeDaryaftani output
set @CodeKolkham =substring(@CodeMoinHesabhayeDaryaftani, 1, @TooleKol)
set @CodeMoinKham =substring(@CodeMoinHesabhayeDaryaftani,@TooleKol+1,@Toolemoin)


--INSERT INTO tblTest7
--                         ( fld2)
--VALUES        (@Date6Ragham)

execute InsertSanadfirstMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,@fldCodeTafzili,NULL,@fldMablaghKarmozd,0,1,@Date6Ragham,@SharhSanad,@day,@month,@year,'vam',Null,@ShomareSanad output;


execute codemoinhesab 'karmozdevam' ,@CodeMoinKarmozdeVam output
set @CodeKolkham =substring(@CodeMoinKarmozdeVam, 1, @TooleKol)
set @CodeMoinKham =substring(@CodeMoinKarmozdeVam,@TooleKol+1,@Toolemoin)


execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @CodeKolkham,@CodeMoinKham,@CodeMoinHesabhayeDaryaftani,NUll,NULL,0,@fldMablaghKarmozd,2,@Date6Ragham,@SharhSanad,@day,@month,@year,'vam',Null,@ShomareSanad;
	

insert into tblVamInfo(fldTypemohasebe,
						fldShomareVam,
						fldShomareFactor,
						fldShomareSanad,
						fldCodeTafzili,
						fldMablaghVam,
						fldTedadAghsat,
						fldDate,
						fldDarsadKarmozd,
						fldTypeDorePardakht,
						fldDorePardakht,
						fldMablaghKarmozd,
						 fldMablaghVamBaKarmozd,
                        fldMablaghghestGhablAzKarmozd,
                        fldMablaghGhestBaKarmozd,
						fldSharh)
Values(@fldTypemohasebe,
	   @MaxShomareVam+1,
	   @fldShomareFactor,
	   @ShomareSanad,
	   @fldCodeTafzili,
	   @fldMablaghVam,
	   @fldTedadAghsat,
	   @fldDate,
	   @fldDarsadKarmozd,
	   @fldTypeDorePardakht,
	   @fldDorePardakht,
	   @fldMablaghKarmozd,
       @fldMablaghVamBaKarmozd,
       @fldMablaghghestGhablAzKarmozd,
	   @fldMablaghGhestBaKarmozd,
	   @fldSharh
	   
)

		insert into tblDaftarcheAghsat(fldRadif,
									   fldShomareVam,
									   fldDateSarResid,									  
									   fldMablaghGhest
									  )
		(select fldRadif,
				@MaxShomareVam+1,		
				fldDateSarResid,		
				fldMablaghGhest		
				from @tblAghsat)

			
	commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult1='error'
			    set @strResult2=(SELECT Error_Line() AS ErrorNumber)
				rollback transaction AddRecord;
			end
		end catch
		
end
GO
/****** Object:  StoredProcedure [dbo].[z162ShowtblDaftarcheAghsat]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[z162ShowtblDaftarcheAghsat]					
                        @fldShomareVam bigint
                        
as 


IF 1=0 BEGIN
 SET FMTONLY OFF
END

begin

select fldRadif,fldMablaghGhest,SUBSTRING(CONVERT(nvarchar,fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 7, 2) AS fldDateSarResid from tblDaftarcheAghsat
 where fldShomareVam=@fldShomareVam

		
end

GO
/****** Object:  StoredProcedure [dbo].[z163ReportBetfkikGroupKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z163ReportBetfkikGroupKala]
  @ListGroupKala varchar(5000)=null,
  @ListCodeAshkhas varchar(5000)=null,
  @ListCodeAnbar varchar(5000)=null,
  @DateAz bigint,
  @DateTa bigint


as

IF 1=0 BEGIN
 SET FMTONLY OFF
END

begin
 

create table #TempBetfkikGroupKala(
Id bigint,
fldCodeTafzili bigint,
fldSharhTafzili nvarchar(150) COLLATE Persian_100_CI_AS,
fldCodeKala bigint,
fldSharhKala nvarchar(150) COLLATE Persian_100_CI_AS,
fldGroupKalaName nvarchar(150) COLLATE Persian_100_CI_AS,
fldCodeGroupKala bigint,
fldNameGroupAshkhas nvarchar(150) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldAnbarName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint,
fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
fldMablaghKhalesForoosh decimal,
fldDate bigint,
fldAddress nvarchar(1000) COLLATE Persian_100_CI_AS
)

--create table #TempShowBetfkikGroupKala(
--Id bigint,
--fldCodeTafzili bigint,
--fldSharhTafzili nvarchar(150) COLLATE Persian_100_CI_AS,
--fldCodeKala bigint,
--fldSharhKala nvarchar(150) COLLATE Persian_100_CI_AS,
--fldGroupKalaName nvarchar(150) COLLATE Persian_100_CI_AS,
--fldCodeGroupKala bigint,
--fldNameGroupAshkhas nvarchar(150) COLLATE Persian_100_CI_AS,
--fldCodeGroupAshkhas bigint,
--fldAnbarName nvarchar(50) COLLATE Persian_100_CI_AS,
--fldCodeAnbar bigint,
--fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
--fldMablaghKhalesForoosh decimal,
--fldDate bigint,
--fldAddress nvarchar(1000) COLLATE Persian_100_CI_AS
--)

delete #TempBetfkikGroupKala
--delete #TempShowBetfkikGroupKala

insert into #TempBetfkikGroupKala(Id,fldCodeTafzili
				,fldSharhTafzili
				,fldCodeKala
				,fldSharhKala
				,fldCodeGroupKala
				,fldGroupKalaName
				,fldCodeGroupAshkhas
				,fldNameGroupAshkhas
				,fldCodeAnbar
				,fldAnbarName
				,fldMablaghKhalesForoosh
				,fldDate
				,fldAddress)
select tblRizeForoosh.ID,tblRizeForoosh.fldCodeTafsili,tblTafzili.fldSharheTafzili,tblKala.fldCodeKala,fldSharhKala,'' as fldCodeGroupKala,'' as fldGroupKalaName,'' as fldCodeGroupAshkhas
,'' as fldNameGroupAshkhas,tblRizeForoosh.fldCodeAnbar,tblAnbar.fldNameAnbar,tblRizeForoosh.fldMablaghKhales,tblRizeForoosh.fldDate,tblTafzili.fldAddress
from tblRizeForoosh INNER JOIN
                         tblKala ON tblKala.fldCodeKala = tblRizeForoosh.fldCodeKala Inner Join
						 tblTafzili ON tblTafzili.fldCodeTafzili=tblRizeForoosh.fldCodeTafsili inner join
						 tblAnbar ON tblAnbar.fldCode=tblRizeForoosh.fldCodeAnbar 				
						where fldDate between @DateAz and @DateTa

						





update #TempBetfkikGroupKala set fldCodeGroupKala=tblGroupKala.fldGroupId, fldGroupKalaName=tblGroupKala.fldGroupName 
from tblKala inner join tblGroupKala ON tblGroupKala.fldGroupId=tblKala.fldCodeGroup
where tblKala.fldCodeKala=#TempBetfkikGroupKala.fldCodeKala	

update #TempBetfkikGroupKala set fldCodeGroupAshkhas=tblGroupAshkhas.fldGroupId,fldNameGroupAshkhas=tblGroupAshkhas.fldGroupName
from tblTafzili inner join tblGroupAshkhas ON tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
where tblTafzili.fldCodeTafzili=#TempBetfkikGroupKala.fldCodeTafzili	
	
	
update #TempBetfkikGroupKala set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempBetfkikGroupKala.fldCodetafzili=tbl5.fldCodeTafzili


--پاک کردن گروه کالاهایی که در گزارش نیستن
if @ListGroupKala<>''
	begin
		declare @StrDeleteGroupKala varchar(5000)
		set  @StrDeleteGroupKala='delete from #TempBetfkikGroupKala
		where '+ @ListGroupKala + ''
		execute(@StrDeleteGroupKala)
	end
if @ListCodeAshkhas<>''
	begin
		declare @StrDeleteGroupAshkhas varchar(5000)
		set  @StrDeleteGroupAshkhas='delete from #TempBetfkikGroupKala 
		where '+ @ListCodeAshkhas + ''
		execute(@StrDeleteGroupAshkhas)
	end
if @ListCodeAnbar<>''
	begin
		declare @StrDeleteAnbar varchar(5000)
		set  @StrDeleteAnbar='delete from #TempBetfkikGroupKala
		where '+ @ListCodeAnbar + ''
		execute(@StrDeleteAnbar)
	end
	



						 
--insert into #TempShowBetfkikGroupKala(fldMablaghKhalesForoosh,fldCodeTafzili,fldSharhTafzili
--,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldAddress)
--select sum(fldMablaghKhalesForoosh) as fldmSum,fldCodeTafzili,fldSharhTafzili
--,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldAddress from #TempBetfkikGroupKala
--Group By fldCodeTafzili,fldSharhTafzili
--,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldAddress


	
--update #TempShowBetfkikGroupKala set fldCodeTafzili=#TempBetfkikGroupKala.fldCodeTafzili
--	from #TempBetfkikGroupKala where #TempShowBetfkikGroupKala.fldSharhTafzili=#TempBetfkikGroupKala.fldSharhTafzili

--update #TempShowBetfkikGroupKala set fldCodeAnbar=#TempBetfkikGroupKala.fldCodeAnbar,fldAnbarName=#TempBetfkikGroupKala.fldAnbarName
--	from #TempBetfkikGroupKala where #TempShowBetfkikGroupKala.fldCodeTafzili=#TempBetfkikGroupKala.fldCodeTafzili

	
	

--update #TempShowBetfkikGroupKala set fldCodeGroupKala=#TempBetfkikGroupKala.fldCodeGroupKala, fldGroupKalaName=#TempBetfkikGroupKala.fldGroupKalaName 
--from #TempBetfkikGroupKala 
--where #TempBetfkikGroupKala.fldCodeTafzili=#TempBetfkikGroupKala.fldCodeTafzili	




 declare @StrOrder nvarchar(50)
 declare @Select nvarchar(500)



 Set @Select='select sum(fldMablaghKhalesForoosh) as fldMablaghKhalesForoosh,fldCodeTafzili,fldSharhTafzili
,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldShomareTell,fldAddress from #TempBetfkikGroupKala
Group By fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldShomareTell,fldAddress '

--Select fldCodeTafzili,fldSharhTafzili,fldCodeGroupKala,fldGroupKalaName
--,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldCodeAnbar,fldAnbarName,fldShomareTell,fldMablaghKhalesForoosh,fldAddress from #TempShowBetfkikGroupKala 

--if @Order=0 
--	set @StrOrder=' order by fldMablaghKhalesForoosh desc'
--else if @Order=1 	
--	set @StrOrder=' order by fldMablaghKhalesForoosh asc'

execute(@select)

	 

end







GO
/****** Object:  StoredProcedure [dbo].[z163ReportBetfkikGroupKala_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z163ReportBetfkikGroupKala_WithUserCode]
--چندزبانه
  --@language nvarchar(10),	
  @ListGroupKala varchar(5000)=null,
  @ListCodeAshkhas varchar(5000)=null,
  @ListCodeAnbar varchar(5000)=null,
  @DateAz bigint,
  @DateTa bigint,
  @userCode bigint

as

IF 1=0 BEGIN
 SET FMTONLY OFF
END

begin
 

create table #TempBetfkikGroupKala(
Id bigint,
fldCodeTafzili bigint,
fldSharhTafzili nvarchar(150) COLLATE Persian_100_CI_AS,
fldCodeKala bigint,
fldSharhKala nvarchar(150) COLLATE Persian_100_CI_AS,
fldGroupKalaName nvarchar(150) COLLATE Persian_100_CI_AS,
fldCodeGroupKala bigint,
fldNameGroupAshkhas nvarchar(150) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldAnbarName nvarchar(50) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint,
fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
fldMablaghKhalesForoosh decimal(18,4),
fldDate bigint,
fldAddress nvarchar(1000) COLLATE Persian_100_CI_AS
)

--create table #TempShowBetfkikGroupKala(
--Id bigint,
--fldCodeTafzili bigint,
--fldSharhTafzili nvarchar(150) COLLATE Persian_100_CI_AS,
--fldCodeKala bigint,
--fldSharhKala nvarchar(150) COLLATE Persian_100_CI_AS,
--fldGroupKalaName nvarchar(150) COLLATE Persian_100_CI_AS,
--fldCodeGroupKala bigint,
--fldNameGroupAshkhas nvarchar(150) COLLATE Persian_100_CI_AS,
--fldCodeGroupAshkhas bigint,
--fldAnbarName nvarchar(50) COLLATE Persian_100_CI_AS,
--fldCodeAnbar bigint,
--fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
--fldMablaghKhalesForoosh decimal,
--fldDate bigint,
--fldAddress nvarchar(1000) COLLATE Persian_100_CI_AS
--)

delete #TempBetfkikGroupKala
--delete #TempShowBetfkikGroupKala

-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################


insert into #TempBetfkikGroupKala(Id,fldCodeTafzili
				,fldSharhTafzili
				,fldCodeKala
				,fldSharhKala
				,fldCodeGroupKala
				,fldGroupKalaName
				,fldCodeGroupAshkhas
				,fldNameGroupAshkhas
				,fldCodeAnbar
				,fldAnbarName
				,fldMablaghKhalesForoosh
				,fldDate
				,fldAddress)
select tblRizeForoosh.ID,tblRizeForoosh.fldCodeTafsili,tblTafzili.fldSharheTafzili,tblKala.fldCodeKala,fldSharhKala,'' as fldCodeGroupKala,'' as fldGroupKalaName,'' as fldCodeGroupAshkhas
,'' as fldNameGroupAshkhas,tblRizeForoosh.fldCodeAnbar,tblAnbar.fldNameAnbar,tblRizeForoosh.fldMablaghKhales,tblRizeForoosh.fldDate,tblTafzili.fldAddress
from tblRizeForoosh INNER JOIN
                         tblKala ON tblKala.fldCodeKala = tblRizeForoosh.fldCodeKala Inner Join
						 tblTafzili ON tblTafzili.fldCodeTafzili=tblRizeForoosh.fldCodeTafsili inner join
						 tblAnbar ON tblAnbar.fldCode=tblRizeForoosh.fldCodeAnbar 				
						where 
						fldCodeTafsili in (select code from @accessCodes) and
						fldDate between @DateAz and @DateTa

						





update #TempBetfkikGroupKala set fldCodeGroupKala=tblGroupKala.fldGroupId, fldGroupKalaName=tblGroupKala.fldGroupName 
from tblKala inner join tblGroupKala ON tblGroupKala.fldGroupId=tblKala.fldCodeGroup
where tblKala.fldCodeKala=#TempBetfkikGroupKala.fldCodeKala	

update #TempBetfkikGroupKala set fldCodeGroupAshkhas=tblGroupAshkhas.fldGroupId,fldNameGroupAshkhas=tblGroupAshkhas.fldGroupName
from tblTafzili inner join tblGroupAshkhas ON tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
where tblTafzili.fldCodeTafzili=#TempBetfkikGroupKala.fldCodeTafzili	
	
	
update #TempBetfkikGroupKala set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempBetfkikGroupKala.fldCodetafzili=tbl5.fldCodeTafzili


--پاک کردن گروه کالاهایی که در گزارش نیستن
if @ListGroupKala<>''
	begin
		declare @StrDeleteGroupKala varchar(5000)
		set  @StrDeleteGroupKala='delete from #TempBetfkikGroupKala
		where '+ @ListGroupKala + ''
		execute(@StrDeleteGroupKala)
	end
if @ListCodeAshkhas<>''
	begin
		declare @StrDeleteGroupAshkhas varchar(5000)
		set  @StrDeleteGroupAshkhas='delete from #TempBetfkikGroupKala 
		where '+ @ListCodeAshkhas + ''
		execute(@StrDeleteGroupAshkhas)
	end
if @ListCodeAnbar<>''
	begin
		declare @StrDeleteAnbar varchar(5000)
		set  @StrDeleteAnbar='delete from #TempBetfkikGroupKala
		where '+ @ListCodeAnbar + ''
		execute(@StrDeleteAnbar)
	end
	



						 
--insert into #TempShowBetfkikGroupKala(fldMablaghKhalesForoosh,fldCodeTafzili,fldSharhTafzili
--,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldAddress)
--select sum(fldMablaghKhalesForoosh) as fldmSum,fldCodeTafzili,fldSharhTafzili
--,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldAddress from #TempBetfkikGroupKala
--Group By fldCodeTafzili,fldSharhTafzili
--,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldAddress


	
--update #TempShowBetfkikGroupKala set fldCodeTafzili=#TempBetfkikGroupKala.fldCodeTafzili
--	from #TempBetfkikGroupKala where #TempShowBetfkikGroupKala.fldSharhTafzili=#TempBetfkikGroupKala.fldSharhTafzili

--update #TempShowBetfkikGroupKala set fldCodeAnbar=#TempBetfkikGroupKala.fldCodeAnbar,fldAnbarName=#TempBetfkikGroupKala.fldAnbarName
--	from #TempBetfkikGroupKala where #TempShowBetfkikGroupKala.fldCodeTafzili=#TempBetfkikGroupKala.fldCodeTafzili

	
	

--update #TempShowBetfkikGroupKala set fldCodeGroupKala=#TempBetfkikGroupKala.fldCodeGroupKala, fldGroupKalaName=#TempBetfkikGroupKala.fldGroupKalaName 
--from #TempBetfkikGroupKala 
--where #TempBetfkikGroupKala.fldCodeTafzili=#TempBetfkikGroupKala.fldCodeTafzili	




 declare @StrOrder nvarchar(50)
 declare @Select nvarchar(500)

 
 --update #TempBetfkikGroupKala set #TempBetfkikGroupKala.fldNameKala = tblKalaTranslate.fldTranslate from tblkalaTranslate where tblkalaTranslate.fldCodeKala=#TempBetfkikGroupKala.fldCodeKala and tblkalatranslate.fldLanguage=@Language

 Set @Select='select sum(fldMablaghKhalesForoosh) as fldMablaghKhalesForoosh,fldCodeTafzili,fldSharhTafzili
,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldShomareTell,fldAddress from #TempBetfkikGroupKala
Group By fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldShomareTell,fldAddress '

--Select fldCodeTafzili,fldSharhTafzili,fldCodeGroupKala,fldGroupKalaName
--,fldCodeGroupAshkhas,fldNameGroupAshkhas,fldCodeAnbar,fldAnbarName,fldShomareTell,fldMablaghKhalesForoosh,fldAddress from #TempShowBetfkikGroupKala 

--if @Order=0 
--	set @StrOrder=' order by fldMablaghKhalesForoosh desc'
--else if @Order=1 	
--	set @StrOrder=' order by fldMablaghKhalesForoosh asc'

execute(@select)

	 

end







GO
/****** Object:  StoredProcedure [dbo].[z164ShowReportDaryaft]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z164ShowReportDaryaft]

	@ListAshkhas varchar(5000)=null,
	@ListVasete varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@DateAz varchar(50),
	@DateTa varchar(50),
	@MablaghAzNaghd decimal, 
	@MablaghTaNaghd decimal,
	@MablaghAzBank decimal,
	@MablaghTaBank decimal,  
	@MablaghAzCheck decimal, 
	@MablaghTaCheck decimal, 
	@MablaghAzTakhfif decimal, 
	@MablaghTaTakhfif decimal, 
	@MablaghAzKol decimal,  
	@MablaghTaKol decimal


as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;

create table #TempShowSandough(
fldCodeSandough bigint,
fldNameSandough NVarchar(100) COLLATE Persian_100_CI_AS
)
create table #TempShowBank(
fldCodeBank bigint,
fldNameBank NVarchar(100) COLLATE Persian_100_CI_AS

)

create table #TempSumKol(
fldShomareDaryaft bigint,
fldMablaghKol decimal

)
create table #TempShowCheck(
fldShomareDaryaft bigint,
fldMablaghCheck decimal
)
create table #TempShowUser1(
id bigint,
fldCodeUser bigint,
fldNameUser NVarchar(100) COLLATE Persian_100_CI_AS,
fldRadif bigint,
fldShomareDaryaft bigint

)

create table #TempShowUser2(
id bigint,
fldCodeUser bigint,
fldNameUser NVarchar(100) COLLATE Persian_100_CI_AS,
fldRadif bigint,
fldShomareDaryaft bigint

)

create table #TempShow(
fldShomareDaryaft bigint,
fldShomareSanad bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldDate NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeSandough bigint,
fldNameSandough NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablaghSandough decimal,
fldCodeBank bigint,
fldNameBank NVarchar(100) COLLATE Persian_100_CI_AS,
fldShenaseBank bigint,
fldMablaghBank decimal,
fldMablaghCheck decimal,
fldMablaghKol decimal,
fldCodeUser bigint,
fldNameUser NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablaghTakhfif decimal,
fldSharhTakhfif NVarchar(200) COLLATE Persian_100_CI_AS,
fldCodeVasete bigint,
fldNameVasete NVarchar(100) COLLATE Persian_100_CI_AS
)
delete #TempShow
delete #TempShowBank


update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')

insert into #TempShow(fldShomareDaryaft,fldShomareSanad,fldCodeTafzili,fldSharhTafzili,
fldDate,fldCodeSandough,fldNameSandough,fldMablaghSandough,fldCodeBank,fldNameBank,fldShenaseBank,
fldMablaghBank,fldMablaghCheck ,
fldMablaghKol,fldCodeUser,fldNameUser,fldMablaghTakhfif,fldSharhTakhfif,fldCodeVasete,fldNameVasete)
select distinct dbo.tblDaryaft.fldShomareDaryaft,dbo.tblDaryaft.fldShomareSanad,
dbo.tblDaryaft.fldCodeTafzili,dbo.tblTafzili.fldSharheTafzili,
SUBSTRING(CONVERT(nvarchar,dbo.tblSodooreSanad.fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, dbo.tblSodooreSanad.fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, dbo.tblSodooreSanad.fldDate), 7, 2) AS fldDate
,NULL as fldCodeSandough,Null as fldNameSandough,0 as fldMablaghSandough,NULL as fldCodeBank,'' as fldNameBank,NULL as fldShenaseBank
,0 as fldMablaghBank,0 as fldMablaghCheck ,0 as fldMablaghKol
,NULL as fldCodeUser,'' as fldNameUser,0 as fldMablaghTakhfif,'' as fldSharhTakhfif,dbo.tblSodooreSanad.fldVasete,'' as fldNameVasete
from tblDaryaft
inner join dbo.tblTafzili on dbo.tblDaryaft.fldCodeTafzili=dbo.tblTafzili.fldCodeTafzili
inner join dbo.tblSodooreSanad on dbo.tblDaryaft.fldShomareSanad=dbo.tblSodooreSanad.fldSanad  and dbo.tblSodooreSanad.fldDate between @DateAz and @DateTa


if @ListAshkhas<>''
		      begin
		       declare @StrDeleteAshkhas varchar(5000)
		        set  @StrDeleteAshkhas='delete from #TempShow 
		            where '+ @ListAshkhas + ''
					execute(@StrDeleteAshkhas)
		      end


		if @ListVasete<>''
		      begin
		       declare @StrDeleteVaste varchar(5000)
		        set  @StrDeleteVaste='delete from #TempShow 
		            where '+ @ListVasete + ''
					execute(@StrDeleteVaste)
		      end




		

	--select * from tblDaryaft
	--select * from tblSodooreSanad
	--select * from #TempShow
	--select * from #TempShowBank
	--------------------------Sandough-------------------------------------
	
		declare @strCodeKoleHesabSandough nvarchar(10)
	execute CodeKolHesab 'sandoogh',@strCodeKoleHesabSandough output
		UPDATE       #TempShow
		SET                fldCodeSandough = tblSodooreSanad.fldMoinKamel,				
						   fldMablaghSandough= tblSodooreSanad.fldBedehkar				
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad 
								where tblSodooreSanad.fldCodeKol=@strCodeKoleHesabSandough


insert into #TempShowSandough(fldCodeSandough,fldNameSandough)


	select[fldCodeHesab], [fldSharheHesab] as fldNameBank from tblKolMoin
		where  fldCodeHesab like @strCodeKoleHesabSandough + '%' and len(fldCodeHesab)>len(@strCodeKoleHesabSandough)
		order by fldCodeHesab
				
		UPDATE       #TempShow
		SET                fldNameSandough = #TempShowSandough.fldNameSandough
																   			
		FROM           #TempShowSandough    INNER JOIN
								#TempShow  ON #TempShowSandough.fldCodeSandough=#TempShow.fldCodeSandough 
							
--------------------------------Bank----------------------------------------
			declare @strCodeKoleHesab nvarchar(10)
	execute CodeKolHesab 'bank',@strcodekolehesab output
			
			UPDATE       #TempShow
		SET                fldCodeBank = tblSodooreSanad.fldMoinKamel,	
						  fldShenaseBank=tblSodooreSanad.fldShomarePeigiri,
						  fldMablaghBank=tblSodooreSanad.fldBedehkar											   			
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldCodeKol=@strCodeKoleHesab
								where tblSodooreSanad.fldCodeKol=@strCodeKoleHesab

 	

	insert into #TempShowBank(fldNameBank,fldCodeBank)
	select [fldSharheHesab] as fldNameBank,[fldCodeHesab] from tblKolMoin
		where  fldCodeHesab like @strCodeKoleHesab + '%' and len(fldCodeHesab)>len(@strCodeKoleHesab)
		order by fldCodeHesab
				
		UPDATE       #TempShow
		SET                fldNameBank = #TempShowBank.fldNameBank
																   			
		FROM           #TempShowBank    INNER JOIN
								#TempShow  ON #TempShowBank.fldCodeBank=#TempShow.fldCodeBank 
								
-----------------------------------Check-------------------------------------------
declare @strCodeKoleHesabCheack nvarchar(10)
	execute CodeKolHesab 'check',@strCodeKoleHesabCheack output
		
	insert into #TempShowCheck(fldShomareDaryaft,fldMablaghCheck)
	select  #TempShow.fldShomareDaryaft,tblSodooreSanad.fldBedehkar from  tblSodooreSanad    INNER JOIN
		#TempShow  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheack
			where tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheack

			--select * from tblCheckeDaryafti


	UPDATE       #TempShow
		SET              
						  fldMablaghCheck=#TempShowCheck.fldMablaghCheck
		FROM           #TempShow   INNER JOIN
								#TempShowCheck  ON #TempShowCheck.fldShomareDaryaft=#TempShow.fldShomareDaryaft 
								where #TempShowCheck.fldShomareDaryaft=#TempShow.fldShomareDaryaft


	--insert into #TempShowCheck(fldShomareDaryaft,fldShomareCheck,fldShomareHesab,fldDateSarResidCheck)
	--			select fldShomareDaryaft,fldShomareCheck,fldShomareHesab,
	--						 SUBSTRING(CONVERT(nvarchar,fldDateSarResid), 1, 4) + '/' +
	--						 SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 5, 2) + '/' + 
	--						 SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 7, 2) AS fldDateSarResid
	--			from tblCheckeDaryafti

	--UPDATE       #TempShow
	--	SET              
	--					  fldShomareCheck=#TempShowCheck.fldShomareCheck	,
	--					  fldShomareHesab=#TempShowCheck.fldShomareHesab,
	--					  fldDateSarResidCheck=#TempShowCheck.fldDateSarResidCheck								   			
	--	FROM           #TempShow   INNER JOIN
	--							#TempShowCheck  ON #TempShow.fldShomareDaryaft=#TempShowCheck.fldShomareDaryaft
	--							where #TempShow.fldShomareDaryaft=#TempShowCheck.fldShomareDaryaft

--------------------------------Takhfif----------------------------------	
declare @strCodeKoleHesabTakhfif nvarchar(10)
	execute CodeMoinHesab'takhfifatedaryaft',@strCodeKoleHesabTakhfif output

	UPDATE       #TempShow
		SET              
						  fldMablaghTakhfif=tblSodooreSanad.fldBedehkar,
						  fldSharhTakhfif=tblSodooreSanad.fldSharh
						  							   			
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldMoinKamel=@strCodeKoleHesabTakhfif
								where tblSodooreSanad.fldMoinKamel=@strCodeKoleHesabTakhfif
----------------------------Vasete--------------------------------------
	UPDATE       #TempShow
		SET              
					
						  fldNameVasete=viewLoadVizitor.fldSharheTafzili
						  							   			
		FROM           #TempShow   INNER JOIN
								viewLoadVizitor  ON viewLoadVizitor.fldCodeTafzili=#TempShow.fldCodeVasete 

-----------------------------Karbar-------------------------------------


delete #TempShowUser1
delete #TempShowUser2



insert into #TempShowUser1(id,fldShomareDaryaft)
SELECT         MAX(id) AS id ,fldShomare
FROM            tblAmalkardekarbar 
WHERE        (fldNameForm = 'دریافت') AND (NoeAmaliat = 'جدید')
GROUP BY fldShomare
ORDER BY fldShomare

insert into #TempShowUser2(id,fldShomareDaryaft)
SELECT         MAX(id) AS id ,fldShomare
FROM            tblAmalkardekarbar 
WHERE        (fldNameForm = 'دریافت') AND (NoeAmaliat = 'ویرایش')
GROUP BY fldShomare
ORDER BY fldShomare

insert into #TempShowUser1(id,fldShomareDaryaft)
select id,fldShomareDaryaft from #TempShowUser2 where fldShomareDaryaft not in(select fldShomareDaryaft from #TempShowUser1)

update #TempShowUser1 Set 
	fldCodeUser=tblAmalkardekarbar.fldCodeKarbar,
	fldNameUser=tblAmalkardekarbar.fldNameKarbar					  							   			
		FROM           #TempShowUser1   INNER JOIN
								tblAmalkardekarbar  ON tblAmalkardekarbar.fldShomare=#TempShowUser1.fldShomareDaryaft
								where tblAmalkardekarbar.id=#TempShowUser1.id


update #TempShow Set 
	fldCodeUser=#TempShowUser1.fldCodeUser,
	fldNameUser=#TempShowUser1.fldNameUser
						  							   			
		FROM           #TempShow   INNER JOIN
								#TempShowUser1  ON #TempShowUser1.fldShomareDaryaft=#TempShow.fldShomareDaryaft  



			  if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end
-----------------------------fldMablaghKol----------------------------------------
declare @SumMablaghKol decimal


UPDATE       #TempShow
		SET              
						  fldMablaghKol=(#TempShow.fldMablaghSandough+#TempShow.fldMablaghBank+#TempShow.fldMablaghCheck-#TempShow.fldMablaghTakhfif)
						  
						  							   			
		FROM           #TempShow


-----------------------------------------------------------------------------------------------------------------------------------------------


SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareDaryaft) as fldRadif,fldShomareDaryaft,fldShomareSanad,fldCodeTafzili,fldSharhTafzili,fldDate  
,fldCodeSandough,fldNameSandough,fldMablaghSandough,fldCodeBank,fldNameBank,fldShenaseBank,
fldMablaghBank,fldMablaghCheck,fldMablaghKol,fldCodeUser,fldNameUser,fldMablaghTakhfif,fldSharhTakhfif,fldCodeVasete,fldNameVasete  
FROM #TempShow  
where 
fldMablaghSandough between CONVERT(nvarchar(50),@MablaghAzNaghd) and CONVERT(nvarchar(50),@MablaghTaNaghd) and 
fldMablaghBank between  CONVERT(nvarchar(50),@MablaghAzBank)  and CONVERT(nvarchar(50),@MablaghTaBank) and 
fldMablaghCheck between  CONVERT(nvarchar(50),@MablaghAzCheck)  and CONVERT(nvarchar(50),@MablaghTaCheck) and 
fldMablaghTakhfif between  CONVERT(nvarchar(50),@MablaghAzTakhfif)  and CONVERT(nvarchar(50),@MablaghTaTakhfif) and 
fldMablaghKol between  CONVERT(nvarchar(50),@MablaghAzKol)  and CONVERT(nvarchar(50),@MablaghTaKol)



END

 


GO
/****** Object:  StoredProcedure [dbo].[z164ShowReportDaryaft_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z164ShowReportDaryaft_WithUserCode]
--چندزبانه
	@ListAshkhas varchar(5000)=null,
	@ListVasete varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@DateAz varchar(50),
	@DateTa varchar(50),
	@MablaghAzNaghd decimal(18,4), 
	@MablaghTaNaghd decimal(18,4),
	@MablaghAzBank decimal(18,4),
	@MablaghTaBank decimal(18,4),  
	@MablaghAzCheck decimal(18,4), 
	@MablaghTaCheck decimal(18,4), 
	@MablaghAzTakhfif decimal(18,4), 
	@MablaghTaTakhfif decimal(18,4), 
	@MablaghAzKol decimal(18,4),  
	@MablaghTaKol decimal(18,4),
	@userCode bigint


as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;

create table #TempShowSandough(
fldCodeSandough bigint,
fldNameSandough NVarchar(100) COLLATE Persian_100_CI_AS
)
create table #TempShowBank(
fldCodeBank bigint,
fldNameBank NVarchar(100) COLLATE Persian_100_CI_AS

)

create table #TempSumKol(
fldShomareDaryaft bigint,
fldMablaghKol decimal(18,4)

)
create table #TempShowCheck(
fldShomareDaryaft bigint,
fldMablaghCheck decimal(18,4)
)
create table #TempShowUser1(
id bigint,
fldCodeUser bigint,
fldNameUser NVarchar(100) COLLATE Persian_100_CI_AS,
fldRadif bigint,
fldShomareDaryaft bigint

)

create table #TempShowUser2(
id bigint,
fldCodeUser bigint,
fldNameUser NVarchar(100) COLLATE Persian_100_CI_AS,
fldRadif bigint,
fldShomareDaryaft bigint

)

create table #TempShow(
fldShomareDaryaft bigint,
fldShomareSanad bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(200) COLLATE Persian_100_CI_AS,
fldDate NVarchar(150) COLLATE Persian_100_CI_AS,
fldCodeSandough bigint,
fldNameSandough NVarchar(150) COLLATE Persian_100_CI_AS,
fldMablaghSandough decimal(18,4),
fldCodeBank bigint,
fldNameBank NVarchar(150) COLLATE Persian_100_CI_AS,
fldShenaseBank nvarchar(50),
fldMablaghBank decimal(18,4),
fldMablaghCheck decimal(18,4),
fldMablaghKol decimal(18,4),
fldCodeUser bigint,
fldNameUser NVarchar(150) COLLATE Persian_100_CI_AS,
fldMablaghTakhfif decimal(18,4),
fldSharhTakhfif NVarchar(200) COLLATE Persian_100_CI_AS,
fldCodeVasete bigint,
fldNameVasete NVarchar(100) COLLATE Persian_100_CI_AS
)
delete #TempShow
delete #TempShowBank


update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')

insert into #TempShow(fldShomareDaryaft,fldShomareSanad,fldCodeTafzili,fldSharhTafzili,
fldDate,fldCodeSandough,fldNameSandough,fldMablaghSandough,fldCodeBank,fldNameBank,fldShenaseBank,
fldMablaghBank,fldMablaghCheck ,
fldMablaghKol,fldCodeUser,fldNameUser,fldMablaghTakhfif,fldSharhTakhfif,fldCodeVasete,fldNameVasete)
select distinct dbo.tblDaryaft.fldShomareDaryaft,dbo.tblDaryaft.fldShomareSanad,
dbo.tblDaryaft.fldCodeTafzili,dbo.tblTafzili.fldSharheTafzili,
SUBSTRING(CONVERT(nvarchar,dbo.tblSodooreSanad.fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, dbo.tblSodooreSanad.fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, dbo.tblSodooreSanad.fldDate), 7, 2) AS fldDate
,NULL as fldCodeSandough,Null as fldNameSandough,0 as fldMablaghSandough,NULL as fldCodeBank,'' as fldNameBank,NULL as fldShenaseBank
,0 as fldMablaghBank,0 as fldMablaghCheck ,0 as fldMablaghKol
,NULL as fldCodeUser,'' as fldNameUser,0 as fldMablaghTakhfif,'' as fldSharhTakhfif,dbo.tblSodooreSanad.fldVasete,'' as fldNameVasete
from tblDaryaft
inner join dbo.tblTafzili on dbo.tblDaryaft.fldCodeTafzili=dbo.tblTafzili.fldCodeTafzili
inner join dbo.tblSodooreSanad on dbo.tblDaryaft.fldShomareSanad=dbo.tblSodooreSanad.fldSanad  and dbo.tblSodooreSanad.fldDate between @DateAz and @DateTa


if @ListAshkhas<>''
		      begin
		       declare @StrDeleteAshkhas varchar(5000)
		        set  @StrDeleteAshkhas='delete from #TempShow 
		            where '+ @ListAshkhas + ''
					execute(@StrDeleteAshkhas)
		      end


		if @ListVasete<>''
		      begin
		       declare @StrDeleteVaste varchar(5000)
		        set  @StrDeleteVaste='delete from #TempShow 
		            where '+ @ListVasete + ''
					execute(@StrDeleteVaste)
		      end




		

	--select * from tblDaryaft
	--select * from tblSodooreSanad
	--select * from #TempShow
	--select * from #TempShowBank
	--------------------------Sandough-------------------------------------
	
		declare @strCodeKoleHesabSandough nvarchar(10)
	execute CodeKolHesab 'sandoogh',@strCodeKoleHesabSandough output
		UPDATE       #TempShow
		SET                fldCodeSandough = tblSodooreSanad.fldMoinKamel,				
						   fldMablaghSandough= tblSodooreSanad.fldBedehkar				
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad 
								where tblSodooreSanad.fldCodeKol=@strCodeKoleHesabSandough


insert into #TempShowSandough(fldCodeSandough,fldNameSandough)


	select[fldCodeHesab], [fldSharheHesab] as fldNameBank from tblKolMoin
		where  fldCodeHesab like @strCodeKoleHesabSandough + '%' and len(fldCodeHesab)>len(@strCodeKoleHesabSandough)
		order by fldCodeHesab
				
		UPDATE       #TempShow
		SET                fldNameSandough = #TempShowSandough.fldNameSandough
																   			
		FROM           #TempShowSandough    INNER JOIN
								#TempShow  ON #TempShowSandough.fldCodeSandough=#TempShow.fldCodeSandough 
							
--------------------------------Bank----------------------------------------
			declare @strCodeKoleHesab nvarchar(10)
	execute CodeKolHesab 'bank',@strcodekolehesab output
			
			UPDATE       #TempShow
		SET                fldCodeBank = tblSodooreSanad.fldMoinKamel,	
						  fldShenaseBank=tblSodooreSanad.fldShomarePeigiri,
						  fldMablaghBank=tblSodooreSanad.fldBedehkar											   			
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldCodeKol=@strCodeKoleHesab
								where tblSodooreSanad.fldCodeKol=@strCodeKoleHesab

 	

	insert into #TempShowBank(fldNameBank,fldCodeBank)
	select [fldSharheHesab] as fldNameBank,[fldCodeHesab] from tblKolMoin
		where  fldCodeHesab like @strCodeKoleHesab + '%' and len(fldCodeHesab)>len(@strCodeKoleHesab)
		order by fldCodeHesab
				
		UPDATE       #TempShow
		SET                fldNameBank = #TempShowBank.fldNameBank
																   			
		FROM           #TempShowBank    INNER JOIN
								#TempShow  ON #TempShowBank.fldCodeBank=#TempShow.fldCodeBank 
								
-----------------------------------Check-------------------------------------------
declare @strCodeKoleHesabCheack nvarchar(10)
	execute CodeKolHesab 'check',@strCodeKoleHesabCheack output
		
	insert into #TempShowCheck(fldShomareDaryaft,fldMablaghCheck)
	select  #TempShow.fldShomareDaryaft,tblSodooreSanad.fldBedehkar from  tblSodooreSanad    INNER JOIN
		#TempShow  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheack
			where tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheack

			--select * from tblCheckeDaryafti


	UPDATE       #TempShow
		SET              
						  fldMablaghCheck=#TempShowCheck.fldMablaghCheck
		FROM           #TempShow   INNER JOIN
								#TempShowCheck  ON #TempShowCheck.fldShomareDaryaft=#TempShow.fldShomareDaryaft 
								where #TempShowCheck.fldShomareDaryaft=#TempShow.fldShomareDaryaft


	--insert into #TempShowCheck(fldShomareDaryaft,fldShomareCheck,fldShomareHesab,fldDateSarResidCheck)
	--			select fldShomareDaryaft,fldShomareCheck,fldShomareHesab,
	--						 SUBSTRING(CONVERT(nvarchar,fldDateSarResid), 1, 4) + '/' +
	--						 SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 5, 2) + '/' + 
	--						 SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 7, 2) AS fldDateSarResid
	--			from tblCheckeDaryafti

	--UPDATE       #TempShow
	--	SET              
	--					  fldShomareCheck=#TempShowCheck.fldShomareCheck	,
	--					  fldShomareHesab=#TempShowCheck.fldShomareHesab,
	--					  fldDateSarResidCheck=#TempShowCheck.fldDateSarResidCheck								   			
	--	FROM           #TempShow   INNER JOIN
	--							#TempShowCheck  ON #TempShow.fldShomareDaryaft=#TempShowCheck.fldShomareDaryaft
	--							where #TempShow.fldShomareDaryaft=#TempShowCheck.fldShomareDaryaft

--------------------------------Takhfif----------------------------------	
declare @strCodeKoleHesabTakhfif nvarchar(10)
	execute CodeMoinHesab'takhfifatedaryaft',@strCodeKoleHesabTakhfif output

	UPDATE       #TempShow
		SET              
						  fldMablaghTakhfif=tblSodooreSanad.fldBedehkar,
						  fldSharhTakhfif=tblSodooreSanad.fldSharh
						  							   			
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldMoinKamel=@strCodeKoleHesabTakhfif
								where tblSodooreSanad.fldMoinKamel=@strCodeKoleHesabTakhfif
----------------------------Vasete--------------------------------------
	UPDATE       #TempShow
		SET              
					
						  fldNameVasete=viewLoadVizitor.fldSharheTafzili
						  							   			
		FROM           #TempShow   INNER JOIN
								viewLoadVizitor  ON viewLoadVizitor.fldCodeTafzili=#TempShow.fldCodeVasete 

-----------------------------Karbar-------------------------------------


delete #TempShowUser1
delete #TempShowUser2



insert into #TempShowUser1(id,fldShomareDaryaft)
SELECT         MAX(id) AS id ,fldShomare
FROM            tblAmalkardekarbar 
WHERE        (fldNameForm = 'دریافت') AND (NoeAmaliat = 'جدید')
GROUP BY fldShomare
ORDER BY fldShomare

insert into #TempShowUser2(id,fldShomareDaryaft)
SELECT         MAX(id) AS id ,fldShomare
FROM            tblAmalkardekarbar 
WHERE        (fldNameForm = 'دریافت') AND (NoeAmaliat = 'ویرایش')
GROUP BY fldShomare
ORDER BY fldShomare

insert into #TempShowUser1(id,fldShomareDaryaft)
select id,fldShomareDaryaft from #TempShowUser2 where fldShomareDaryaft not in(select fldShomareDaryaft from #TempShowUser1)

update #TempShowUser1 Set 
	fldCodeUser=tblAmalkardekarbar.fldCodeKarbar,
	fldNameUser=tblAmalkardekarbar.fldNameKarbar					  							   			
		FROM           #TempShowUser1   INNER JOIN
								tblAmalkardekarbar  ON tblAmalkardekarbar.fldShomare=#TempShowUser1.fldShomareDaryaft
								where tblAmalkardekarbar.id=#TempShowUser1.id


update #TempShow Set 
	fldCodeUser=#TempShowUser1.fldCodeUser,
	fldNameUser=#TempShowUser1.fldNameUser
						  							   			
		FROM           #TempShow   INNER JOIN
								#TempShowUser1  ON #TempShowUser1.fldShomareDaryaft=#TempShow.fldShomareDaryaft  



			  if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end
-----------------------------fldMablaghKol----------------------------------------
declare @SumMablaghKol decimal(18,4)


UPDATE       #TempShow
		SET              
						  fldMablaghKol=(#TempShow.fldMablaghSandough+#TempShow.fldMablaghBank+#TempShow.fldMablaghCheck-#TempShow.fldMablaghTakhfif)
						  
						  							   			
		FROM           #TempShow


-----------------------------------------------------------------------------------------------------------------------------------------------
-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################
delete from #TempShow where fldCodeTafzili not in (select code from @accessCodes)
-- ##########################################################


SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareDaryaft) as fldRadif,fldShomareDaryaft,fldShomareSanad,fldCodeTafzili,fldSharhTafzili,fldDate  
,fldCodeSandough,fldNameSandough,fldMablaghSandough,fldCodeBank,fldNameBank,fldShenaseBank,
fldMablaghBank,fldMablaghCheck,fldMablaghKol,fldCodeUser,fldNameUser,fldMablaghTakhfif,fldSharhTakhfif,fldCodeVasete,fldNameVasete  
FROM #TempShow  
where 
fldMablaghSandough between CONVERT(nvarchar(50),@MablaghAzNaghd) and CONVERT(nvarchar(50),@MablaghTaNaghd) and 
fldMablaghBank between  CONVERT(nvarchar(50),@MablaghAzBank)  and CONVERT(nvarchar(50),@MablaghTaBank) and 
fldMablaghCheck between  CONVERT(nvarchar(50),@MablaghAzCheck)  and CONVERT(nvarchar(50),@MablaghTaCheck) and 
fldMablaghTakhfif between  CONVERT(nvarchar(50),@MablaghAzTakhfif)  and CONVERT(nvarchar(50),@MablaghTaTakhfif) and 
fldMablaghKol between  CONVERT(nvarchar(50),@MablaghAzKol)  and CONVERT(nvarchar(50),@MablaghTaKol)



END

 

GO
/****** Object:  StoredProcedure [dbo].[z165ShowReportPardakht]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z165ShowReportPardakht]

	@ListAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@DateAz varchar(50),
	@DateTa varchar(50),
	@MablaghAzNaghd decimal, 
	@MablaghTaNaghd decimal,
	@MablaghAzBank decimal,
	@MablaghTaBank decimal,  
	@MablaghAzCheck decimal, 
	@MablaghTaCheck decimal, 
	@MablaghAzTakhfif decimal, 
	@MablaghTaTakhfif decimal, 
	@MablaghAzKol decimal,  
	@MablaghTaKol decimal


as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;

create table #TempShowSandough(
fldCodeSandough bigint,
fldNameSandough NVarchar(100) COLLATE Persian_100_CI_AS
)
create table #TempShowBank(
fldCodeBank bigint,
fldNameBank NVarchar(100) COLLATE Persian_100_CI_AS

)

create table #TempSumKol(
fldShomarePardakht bigint,
fldMablaghKol decimal

)
create table #TempShowCheck(
fldShomarePardakht bigint,
fldMablaghCheck decimal

)

create table #TempShowUser1(
id bigint,
fldCodeUser bigint,
fldNameUser NVarchar(100) COLLATE Persian_100_CI_AS,
fldRadif bigint,
fldShomarePardakht bigint

)

create table #TempShowUser2(
id bigint,
fldCodeUser bigint,
fldNameUser NVarchar(100) COLLATE Persian_100_CI_AS,
fldRadif bigint,
fldShomarePardakht bigint

)

create table #TempShow(
fldShomarePardakht bigint,
fldShomareSanad bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldDate NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeSandough bigint,
fldNameSandough NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablaghSandough decimal,
fldCodeBank bigint,
fldNameBank NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablaghBank decimal,
fldMablaghCheck decimal,
fldMablaghCheckVagozari decimal,
fldMablaghKol decimal,
fldCodeUser bigint,
fldNameUser NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablaghTakhfif decimal,
fldSharhTakhfif NVarchar(200) COLLATE Persian_100_CI_AS
)
delete #TempShow
delete #TempShowBank


update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')

insert into #TempShow(fldShomarePardakht,fldShomareSanad,fldCodeTafzili,fldSharhTafzili,
fldDate,fldCodeSandough,fldNameSandough,fldMablaghSandough,fldCodeBank,fldNameBank,
fldMablaghBank,fldMablaghCheck ,fldMablaghCheckVagozari,
fldMablaghKol,fldCodeUser,fldNameUser,fldMablaghTakhfif,fldSharhTakhfif)
select distinct dbo.tblPardakht.fldShomarePardakht,dbo.tblPardakht.fldShomareSanad,
dbo.tblPardakht.fldCodeTafzili,dbo.tblTafzili.fldSharheTafzili,
SUBSTRING(CONVERT(nvarchar,dbo.tblSodooreSanad.fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, dbo.tblSodooreSanad.fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, dbo.tblSodooreSanad.fldDate), 7, 2) AS fldDate
,NULL as fldCodeSandough,Null as fldNameSandough,0 as fldMablaghSandough,NULL as fldCodeBank,'' as fldNameBank
,0 as fldMablaghBank,0 as fldMablaghCheck,0 as fldMablaghCheckVagozari ,0 as fldMablaghKol
,NULL as fldCodeUser,'' as fldNameUser,0 as fldMablaghTakhfif,'' as fldSharhTakhfif
from tblPardakht
inner join dbo.tblTafzili on dbo.tblPardakht.fldCodeTafzili=dbo.tblTafzili.fldCodeTafzili
inner join dbo.tblSodooreSanad on dbo.tblPardakht.fldShomareSanad=dbo.tblSodooreSanad.fldSanad  and dbo.tblSodooreSanad.fldDate between @DateAz and @DateTa







		if @ListAshkhas<>''
		      begin
		       declare @StrDeleteAshkhas varchar(5000)
		        set  @StrDeleteAshkhas='delete from #TempShow 
		            where '+ @ListAshkhas + ''
					execute(@StrDeleteAshkhas)
		      end


			
	--select * from tblDaryaft
	--select * from tblSodooreSanad
	--select * from #TempShow
	--select * from #TempShowBank
	--------------------------Sandough-------------------------------------
			declare @strCodeKoleHesabSandough nvarchar(10)
	execute CodeKolHesab 'sandoogh',@strCodeKoleHesabSandough output

		UPDATE       #TempShow
		SET                fldCodeSandough = tblSodooreSanad.fldMoinKamel,				
						   fldMablaghSandough= tblSodooreSanad.fldBestankar				
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad 
								where tblSodooreSanad.fldCodeKol=@strCodeKoleHesabSandough


	
insert into #TempShowSandough(fldCodeSandough,fldNameSandough)


	select[fldCodeHesab], [fldSharheHesab] as fldNameBank from tblKolMoin
		where  fldCodeHesab like @strCodeKoleHesabSandough + '%' and len(fldCodeHesab)>len(@strCodeKoleHesabSandough)
		order by fldCodeHesab
				
		UPDATE       #TempShow
		SET                fldNameSandough = #TempShowSandough.fldNameSandough
																   			
		FROM           #TempShowSandough    INNER JOIN
								#TempShow  ON #TempShowSandough.fldCodeSandough=#TempShow.fldCodeSandough 
							
--------------------------------Bank----------------------------------------
			declare @strCodeKoleHesab nvarchar(10)
	execute CodeKolHesab 'bank',@strcodekolehesab output
		
			UPDATE       #TempShow
		SET                fldCodeBank = tblSodooreSanad.fldMoinKamel,
						  fldMablaghBank=tblSodooreSanad.fldBestankar											   			
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldCodeKol=@strcodekolehesab
								where tblSodooreSanad.fldCodeKol=@strcodekolehesab

 	

	insert into #TempShowBank(fldNameBank,fldCodeBank)
	select [fldSharheHesab],[fldCodeHesab] from tblKolMoin
		Where fldCodeHesab<>@strcodekolehesab and left(fldCodeHesab,len(@strcodekolehesab))=@strcodekolehesab
		order by fldCodeHesab
				
		UPDATE       #TempShow
		SET                fldNameBank = #TempShowBank.fldNameBank
																   			
		FROM           #TempShowBank    INNER JOIN
								#TempShow  ON #TempShowBank.fldCodeBank=#TempShow.fldCodeBank 
								
-----------------------------------Check-------------------------------------------
declare @strCodeKoleHesabCheack nvarchar(10)
	execute CodeKolHesab 'checkpardakhti',@strCodeKoleHesabCheack output
		
	insert into #TempShowCheck(fldShomarePardakht,fldMablaghCheck)
	select  #TempShow.fldShomarePardakht,tblSodooreSanad.fldBestankar from  tblSodooreSanad    INNER JOIN
		#TempShow  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheack
			where tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheack

			--select * from tblCheckeDaryafti


	UPDATE       #TempShow
		SET              
						  fldMablaghCheck=#TempShowCheck.fldMablaghCheck
		FROM           #TempShow   INNER JOIN
								#TempShowCheck  ON #TempShowCheck.fldShomarePardakht=#TempShow.fldShomarePardakht 
								where #TempShowCheck.fldShomarePardakht=#TempShow.fldShomarePardakht

-----------------------------------CheckVagozari-------------------------------------------
	declare @strCodeKoleHesabCheckVagozari nvarchar(10)
	execute CodeKolHesab 'check',@strCodeKoleHesabCheckVagozari output
	--select @strCodeKoleHesabCheckVagozari
	UPDATE       #TempShow
		SET              
						  fldMablaghCheckVagozari=tblSodooreSanad.fldBestankar									   			
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheckVagozari
								where tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheckVagozari
	


--------------------------------Takhfif----------------------------------	
declare @strCodeKoleHesabTakhfif nvarchar(10)
	execute CodeMoinHesab'takhfifatepardakht',@strCodeKoleHesabTakhfif output
	
	UPDATE       #TempShow
		SET              
						  fldMablaghTakhfif=tblSodooreSanad.fldBestankar,
						  fldSharhTakhfif=tblSodooreSanad.fldSharh
						  							   			
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldMoinKamel=@strCodeKoleHesabTakhfif
								where tblSodooreSanad.fldMoinKamel=@strCodeKoleHesabTakhfif

-----------------------------Karbar-------------------------------------


delete #TempShowUser1
delete #TempShowUser2



insert into #TempShowUser1(id,fldShomarePardakht)
SELECT         MAX(id) AS id ,fldShomare
FROM            tblAmalkardekarbar 
WHERE        (fldNameForm = 'پرداخت') AND (NoeAmaliat = 'جدید')
GROUP BY fldShomare
ORDER BY fldShomare

insert into #TempShowUser2(id,fldShomarePardakht)
SELECT         MAX(id) AS id ,fldShomare
FROM            tblAmalkardekarbar 
WHERE        (fldNameForm = 'پرداخت') AND (NoeAmaliat = 'ویرایش')
GROUP BY fldShomare
ORDER BY fldShomare

insert into #TempShowUser1(id,fldShomarePardakht)
select id,fldShomarePardakht from #TempShowUser2 where fldShomarePardakht not in(select fldShomarePardakht from #TempShowUser1)

update #TempShowUser1 Set 
	fldCodeUser=tblAmalkardekarbar.fldCodeKarbar,
	fldNameUser=tblAmalkardekarbar.fldNameKarbar					  							   			
		FROM           #TempShowUser1   INNER JOIN
								tblAmalkardekarbar  ON tblAmalkardekarbar.fldShomare=#TempShowUser1.fldShomarePardakht
								where tblAmalkardekarbar.id=#TempShowUser1.id


update #TempShow Set 
	fldCodeUser=#TempShowUser1.fldCodeUser,
	fldNameUser=#TempShowUser1.fldNameUser
						  							   			
		FROM           #TempShow   INNER JOIN
								#TempShowUser1  ON #TempShowUser1.fldShomarePardakht=#TempShow.fldShomarePardakht  


  	  if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end
-----------------------------fldMablaghKol----------------------------------------
declare @SumMablaghKol decimal


UPDATE       #TempShow
		SET              
						  fldMablaghKol=(#TempShow.fldMablaghSandough+#TempShow.fldMablaghBank+#TempShow.fldMablaghCheck-#TempShow.fldMablaghTakhfif)
						  
						  							   			
		FROM           #TempShow


-----------------------------------------------------------------------------------------------------------------------------------------------
SELECT  ROW_NUMBER() OVER(ORDER BY fldShomarePardakht) as fldRadif,fldShomarePardakht,fldShomareSanad,fldCodeTafzili,fldSharhTafzili,fldDate  
,fldCodeSandough,fldNameSandough,fldMablaghSandough,fldCodeBank,fldNameBank,
fldMablaghBank,fldMablaghCheck ,fldMablaghCheckVagozari,
fldMablaghKol,fldCodeUser,fldNameUser,fldMablaghTakhfif,fldSharhTakhfif
FROM #TempShow  where 
fldMablaghSandough between CONVERT(nvarchar(50),@MablaghAzNaghd) and CONVERT(nvarchar(50),@MablaghTaNaghd) and 
fldMablaghBank between  CONVERT(nvarchar(50),@MablaghAzBank)  and CONVERT(nvarchar(50),@MablaghTaBank) and 
fldMablaghCheck between  CONVERT(nvarchar(50),@MablaghAzCheck)  and CONVERT(nvarchar(50),@MablaghTaCheck) and 
fldMablaghTakhfif between  CONVERT(nvarchar(50),@MablaghAzTakhfif)  and CONVERT(nvarchar(50),@MablaghTaTakhfif) and 
fldMablaghKol between  CONVERT(nvarchar(50),@MablaghAzKol)  and CONVERT(nvarchar(50),@MablaghTaKol)



END

 

GO
/****** Object:  StoredProcedure [dbo].[z165ShowReportPardakht_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z165ShowReportPardakht_WithUserCode]

	@ListAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@DateAz varchar(50),
	@DateTa varchar(50),
	@MablaghAzNaghd decimal(18,4), 
	@MablaghTaNaghd decimal(18,4),
	@MablaghAzBank decimal(18,4),
	@MablaghTaBank decimal(18,4),  
	@MablaghAzCheck decimal(18,4), 
	@MablaghTaCheck decimal(18,4), 
	@MablaghAzTakhfif decimal(18,4), 
	@MablaghTaTakhfif decimal(18,4), 
	@MablaghAzKol decimal(18,4),  
	@MablaghTaKol decimal(18,4),
	@userCode bigint

as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;

create table #TempShowSandough(
fldCodeSandough bigint,
fldNameSandough NVarchar(100) COLLATE Persian_100_CI_AS
)
create table #TempShowBank(
fldCodeBank bigint,
fldNameBank NVarchar(100) COLLATE Persian_100_CI_AS

)

create table #TempSumKol(
fldShomarePardakht bigint,
fldMablaghKol decimal(18,4)

)
create table #TempShowCheck(
fldShomarePardakht bigint,
fldMablaghCheck decimal(18,4)

)

create table #TempShowUser1(
id bigint,
fldCodeUser bigint,
fldNameUser NVarchar(100) COLLATE Persian_100_CI_AS,
fldRadif bigint,
fldShomarePardakht bigint

)

create table #TempShowUser2(
id bigint,
fldCodeUser bigint,
fldNameUser NVarchar(100) COLLATE Persian_100_CI_AS,
fldRadif bigint,
fldShomarePardakht bigint

)

create table #TempShow(
fldShomarePardakht bigint,
fldShomareSanad bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldDate NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeSandough bigint,
fldNameSandough NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablaghSandough decimal(18,4),
fldCodeBank bigint,
fldNameBank NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablaghBank decimal(18,4),
fldMablaghCheck decimal(18,4),
fldMablaghCheckVagozari decimal(18,4),
fldMablaghKol decimal(18,4),
fldCodeUser bigint,
fldNameUser NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablaghTakhfif decimal(18,4),
fldSharhTakhfif NVarchar(200) COLLATE Persian_100_CI_AS
)
delete #TempShow
delete #TempShowBank


update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')

insert into #TempShow(fldShomarePardakht,fldShomareSanad,fldCodeTafzili,fldSharhTafzili,
fldDate,fldCodeSandough,fldNameSandough,fldMablaghSandough,fldCodeBank,fldNameBank,
fldMablaghBank,fldMablaghCheck ,fldMablaghCheckVagozari,
fldMablaghKol,fldCodeUser,fldNameUser,fldMablaghTakhfif,fldSharhTakhfif)
select distinct dbo.tblPardakht.fldShomarePardakht,dbo.tblPardakht.fldShomareSanad,
dbo.tblPardakht.fldCodeTafzili,dbo.tblTafzili.fldSharheTafzili,
SUBSTRING(CONVERT(nvarchar,dbo.tblSodooreSanad.fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, dbo.tblSodooreSanad.fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, dbo.tblSodooreSanad.fldDate), 7, 2) AS fldDate
,NULL as fldCodeSandough,Null as fldNameSandough,0 as fldMablaghSandough,NULL as fldCodeBank,'' as fldNameBank
,0 as fldMablaghBank,0 as fldMablaghCheck,0 as fldMablaghCheckVagozari ,0 as fldMablaghKol
,NULL as fldCodeUser,'' as fldNameUser,0 as fldMablaghTakhfif,'' as fldSharhTakhfif
from tblPardakht
inner join dbo.tblTafzili on dbo.tblPardakht.fldCodeTafzili=dbo.tblTafzili.fldCodeTafzili
inner join dbo.tblSodooreSanad on dbo.tblPardakht.fldShomareSanad=dbo.tblSodooreSanad.fldSanad  and dbo.tblSodooreSanad.fldDate between @DateAz and @DateTa







		if @ListAshkhas<>''
		      begin
		       declare @StrDeleteAshkhas varchar(5000)
		        set  @StrDeleteAshkhas='delete from #TempShow 
		            where '+ @ListAshkhas + ''
					execute(@StrDeleteAshkhas)
		      end


			
	--select * from tblDaryaft
	--select * from tblSodooreSanad
	--select * from #TempShow
	--select * from #TempShowBank
	--------------------------Sandough-------------------------------------
			declare @strCodeKoleHesabSandough nvarchar(10)
	execute CodeKolHesab 'sandoogh',@strCodeKoleHesabSandough output

		UPDATE       #TempShow
		SET                fldCodeSandough = tblSodooreSanad.fldMoinKamel,				
						   fldMablaghSandough= tblSodooreSanad.fldBestankar				
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad 
								where tblSodooreSanad.fldCodeKol=@strCodeKoleHesabSandough


	
insert into #TempShowSandough(fldCodeSandough,fldNameSandough)


	select[fldCodeHesab], [fldSharheHesab] as fldNameBank from tblKolMoin
		where  fldCodeHesab like @strCodeKoleHesabSandough + '%' and len(fldCodeHesab)>len(@strCodeKoleHesabSandough)
		order by fldCodeHesab
				
		UPDATE       #TempShow
		SET                fldNameSandough = #TempShowSandough.fldNameSandough
																   			
		FROM           #TempShowSandough    INNER JOIN
								#TempShow  ON #TempShowSandough.fldCodeSandough=#TempShow.fldCodeSandough 
							
--------------------------------Bank----------------------------------------
			declare @strCodeKoleHesab nvarchar(10)
	execute CodeKolHesab 'bank',@strcodekolehesab output
		
			UPDATE       #TempShow
		SET                fldCodeBank = tblSodooreSanad.fldMoinKamel,
						  fldMablaghBank=tblSodooreSanad.fldBestankar											   			
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldCodeKol=@strcodekolehesab
								where tblSodooreSanad.fldCodeKol=@strcodekolehesab

 	

	insert into #TempShowBank(fldNameBank,fldCodeBank)
	select [fldSharheHesab],[fldCodeHesab] from tblKolMoin
		Where fldCodeHesab<>@strcodekolehesab and left(fldCodeHesab,len(@strcodekolehesab))=@strcodekolehesab
		order by fldCodeHesab
				
		UPDATE       #TempShow
		SET                fldNameBank = #TempShowBank.fldNameBank
																   			
		FROM           #TempShowBank    INNER JOIN
								#TempShow  ON #TempShowBank.fldCodeBank=#TempShow.fldCodeBank 
								
-----------------------------------Check-------------------------------------------
declare @strCodeKoleHesabCheack nvarchar(10)
	execute CodeKolHesab 'checkpardakhti',@strCodeKoleHesabCheack output
		
	insert into #TempShowCheck(fldShomarePardakht,fldMablaghCheck)
	select  #TempShow.fldShomarePardakht,tblSodooreSanad.fldBestankar from  tblSodooreSanad    INNER JOIN
		#TempShow  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheack
			where tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheack

			--select * from tblCheckeDaryafti


	UPDATE       #TempShow
		SET              
						  fldMablaghCheck=#TempShowCheck.fldMablaghCheck
		FROM           #TempShow   INNER JOIN
								#TempShowCheck  ON #TempShowCheck.fldShomarePardakht=#TempShow.fldShomarePardakht 
								where #TempShowCheck.fldShomarePardakht=#TempShow.fldShomarePardakht

-----------------------------------CheckVagozari-------------------------------------------
	declare @strCodeKoleHesabCheckVagozari nvarchar(10)
	execute CodeKolHesab 'check',@strCodeKoleHesabCheckVagozari output
	--select @strCodeKoleHesabCheckVagozari
	UPDATE       #TempShow
		SET              
						  fldMablaghCheckVagozari=tblSodooreSanad.fldBestankar									   			
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheckVagozari
								where tblSodooreSanad.fldCodeKol=@strCodeKoleHesabCheckVagozari
	


--------------------------------Takhfif----------------------------------	
declare @strCodeKoleHesabTakhfif nvarchar(10)
	execute CodeMoinHesab'takhfifatepardakht',@strCodeKoleHesabTakhfif output
	
	UPDATE       #TempShow
		SET              
						  fldMablaghTakhfif=tblSodooreSanad.fldBestankar,
						  fldSharhTakhfif=tblSodooreSanad.fldSharh
						  							   			
		FROM           #TempShow   INNER JOIN
								tblSodooreSanad  ON tblSodooreSanad.fldSanad=#TempShow.fldShomareSanad and tblSodooreSanad.fldMoinKamel=@strCodeKoleHesabTakhfif
								where tblSodooreSanad.fldMoinKamel=@strCodeKoleHesabTakhfif

-----------------------------Karbar-------------------------------------


delete #TempShowUser1
delete #TempShowUser2



insert into #TempShowUser1(id,fldShomarePardakht)
SELECT         MAX(id) AS id ,fldShomare
FROM            tblAmalkardekarbar 
WHERE        (fldNameForm = 'پرداخت') AND (NoeAmaliat = 'جدید')
GROUP BY fldShomare
ORDER BY fldShomare

insert into #TempShowUser2(id,fldShomarePardakht)
SELECT         MAX(id) AS id ,fldShomare
FROM            tblAmalkardekarbar 
WHERE        (fldNameForm = 'پرداخت') AND (NoeAmaliat = 'ویرایش')
GROUP BY fldShomare
ORDER BY fldShomare

insert into #TempShowUser1(id,fldShomarePardakht)
select id,fldShomarePardakht from #TempShowUser2 where fldShomarePardakht not in(select fldShomarePardakht from #TempShowUser1)

update #TempShowUser1 Set 
	fldCodeUser=tblAmalkardekarbar.fldCodeKarbar,
	fldNameUser=tblAmalkardekarbar.fldNameKarbar					  							   			
		FROM           #TempShowUser1   INNER JOIN
								tblAmalkardekarbar  ON tblAmalkardekarbar.fldShomare=#TempShowUser1.fldShomarePardakht
								where tblAmalkardekarbar.id=#TempShowUser1.id


update #TempShow Set 
	fldCodeUser=#TempShowUser1.fldCodeUser,
	fldNameUser=#TempShowUser1.fldNameUser
						  							   			
		FROM           #TempShow   INNER JOIN
								#TempShowUser1  ON #TempShowUser1.fldShomarePardakht=#TempShow.fldShomarePardakht  


  	  if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end
-----------------------------fldMablaghKol----------------------------------------
declare @SumMablaghKol decimal


UPDATE       #TempShow
		SET              
						  fldMablaghKol=(#TempShow.fldMablaghSandough+#TempShow.fldMablaghBank+#TempShow.fldMablaghCheck-#TempShow.fldMablaghTakhfif)
						  
						  							   			
		FROM           #TempShow


-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################
delete from #TempShow where fldCodeTafzili not in (select code from @accessCodes)
-- ##########################################################

-----------------------------------------------------------------------------------------------------------------------------------------------
SELECT  ROW_NUMBER() OVER(ORDER BY fldShomarePardakht) as fldRadif,fldShomarePardakht,fldShomareSanad,fldCodeTafzili,fldSharhTafzili,fldDate  
,fldCodeSandough,fldNameSandough,fldMablaghSandough,fldCodeBank,fldNameBank,
fldMablaghBank,fldMablaghCheck ,fldMablaghCheckVagozari,
fldMablaghKol,fldCodeUser,fldNameUser,fldMablaghTakhfif,fldSharhTakhfif
FROM #TempShow  where 
fldMablaghSandough between CONVERT(nvarchar(50),@MablaghAzNaghd) and CONVERT(nvarchar(50),@MablaghTaNaghd) and 
fldMablaghBank between  CONVERT(nvarchar(50),@MablaghAzBank)  and CONVERT(nvarchar(50),@MablaghTaBank) and 
fldMablaghCheck between  CONVERT(nvarchar(50),@MablaghAzCheck)  and CONVERT(nvarchar(50),@MablaghTaCheck) and 
fldMablaghTakhfif between  CONVERT(nvarchar(50),@MablaghAzTakhfif)  and CONVERT(nvarchar(50),@MablaghTaTakhfif) and 
fldMablaghKol between  CONVERT(nvarchar(50),@MablaghAzKol)  and CONVERT(nvarchar(50),@MablaghTaKol)



END

 
GO
/****** Object:  StoredProcedure [dbo].[z166UpdateRowColor]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[z166UpdateRowColor]
@Id bigint,
@CodeColor int,
@FormName Nvarchar(20)


as 

begin
Update tblSodooreSanad Set fldColorBank=@CodeColor where ID=@Id
	if @FormName='Bank'
	begin
	Update tblSodooreSanad Set fldColorBank=@CodeColor where ID=@Id

	end
	else if @FormName='Sandoogh'
	begin
	Update tblSodooreSanad Set fldColorSandoogh=@CodeColor where ID=@Id

	end
	else if @FormName='SooratHesab'
	begin
	Update tblSodooreSanad Set fldColorSooratHesab=@CodeColor where ID=@Id

	end
end

GO
/****** Object:  StoredProcedure [dbo].[z167ShowList_ReportFactorKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[z167ShowList_ReportFactorKharid]
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@DateFactorAz varchar(50),
	@DateFactorTa varchar(50),
	@MablaghAz decimal, 
	@MablaghTa decimal
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 

 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)
 create table #TempListDateTimeSabtFactor(
fldShomareFactor bigint,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS
)

 create table #TempListDateFactor(
fldShomareFactor bigint,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS
)

 create table #TempSumKharid(
fldShomareFactor bigint,
fldMablaghKharid decimal,
fldCodeAnbar bigint

)

create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
fldShomareFactorKharid bigint,
fldLock nvarchar(5)
)


	delete from #TempSumKharid
	delete from #TempListDateTimeSabtFactor
	delete from #TempShow
	delete from #TempListCodeAnbar

update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizeKharid where fldRadif=1)

insert into #TempSumKharid(fldShomareFactor,fldMablaghKharid,fldCodeAnbar)
			(select tblRizeKharid.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizeKharid  group by fldShomareFactor )

--select * from #TempListCodeAnbar
insert into #TempListDateTimeSabtFactor(fldShomareFactor,fldTimeSabtFactor,fldDateSabtFactor,fldNameKarbar)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldNameKarbar from tblAmalkardekarbar where fldNameForm=N'خرید' and NoeAmaliat=N'جدید' )
			--select * from #TempListDateTimeSabtFactor

insert into #TempListDateFactor(fldShomareFactor,fldDateFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizeKharid )
			--select * from tblRizeForoosh

		
			--select * from #TempListDateFactor
		--select * from #TempSumKharid
update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumKharid as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeAnbar,fldNameAnbar,fldNameKarbar,fldLock)
 select DISTINCT  tblRizeKharid.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,'' as fldDateFactor,
						'' as fldTimeFacttor,'' as fldDateFacttor,#TempSumKharid.fldCodeAnbar,'' as fldNameAnbar,'' as fldNameKarbar,tblKharid.fldLock
							  
 from tblRizeKharid 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizeKharid.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumKharid on #TempSumKharid.fldShomareFactor=tblRizeKharid.fldShomareFactor
 inner join tblKharid on tblKharid.fldShomareFactorKharid=tblRizeKharid.fldShomareFactor




							--SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDateShow

	update R Set R.fldMablagh=(#TempSumKharid.fldMablaghKharid)
								from #TempShow as R 
								inner join #TempSumKharid on R.fldShomareFactor=#TempSumKharid.fldShomareFactor						
	
	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode
									
									
	update R Set R.fldTimeSabtFactor=(#TempListDateTimeSabtFactor.fldTimeSabtFactor),R.fldDateSabtFactor=(#TempListDateTimeSabtFactor.fldDateSabtFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor
	
		update R Set R.fldNameKarbar=(#TempListDateTimeSabtFactor.fldNameKarbar)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor

	update R Set R.fldDateFactor=(#TempListDateFactor.fldDateFactor)
								from #TempShow as R 
								inner join #TempListDateFactor on R.fldShomareFactor=#TempListDateFactor.fldShomareFactor

	update #TempShow set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempShow.fldCodetafzili=tbl5.fldCodeTafzili

	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end



	 if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end

	--insert into #tempPishFactor
	--	(fldShomareFactorForoosh,fldShomarepishfactor)
	--Select  fldShomareFactorForoosh,fldShomarepishfactor from tblForoosh Where (isnumeric(fldShomarePishFactor) = 1) and fldShomarePishFactor<>0

	--delete from #TempShow 
	--	 where fldCodeTafzili NOT Like '10000 %'
	--update #TempShow set fldShomareFactorForoosh=#tempPishFactor.fldShomareFactorForoosh from #tempPishFactor Where #TempShow.fldShomareFactor=#tempPishFactor.fldShomarePishFactor
	
	--update #TempShow set fldShomarePishfactor =tblForoosh.fldShomarePishFactor from tblForoosh Where 
SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeAnbar,fldNameAnbar,fldNameKarbar,fldShomareTell
,fldLock FROM  #TempShow where 
fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z167ShowList_ReportFactorKharid_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[z167ShowList_ReportFactorKharid_WithUserCode]
--چندزبانه
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@DateFactorAz varchar(50),
	@DateFactorTa varchar(50),
	@MablaghAz decimal(18,4), 
	@MablaghTa decimal(18,4),
	@UserCode bigint
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 

 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)
 create table #TempListDateTimeSabtFactor(
fldShomareFactor bigint,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeKarbar bigint,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS
)

 create table #TempListDateFactor(
fldShomareFactor bigint,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS
)

 create table #TempSumKharid(
fldShomareFactor bigint,
fldMablaghKharid decimal(18,4),
fldCodeAnbar bigint

)

create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(200) COLLATE Persian_100_CI_AS,
fldMablagh decimal(18,4),
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeKarbar bigint,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
fldShomareFactorKharid bigint,
fldLock nvarchar(10)
)


	delete from #TempSumKharid
	delete from #TempListDateTimeSabtFactor
	delete from #TempShow
	delete from #TempListCodeAnbar

update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizeKharid where fldRadif=1)

insert into #TempSumKharid(fldShomareFactor,fldMablaghKharid,fldCodeAnbar)
			(select tblRizeKharid.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizeKharid  group by fldShomareFactor )

--select * from #TempListCodeAnbar
insert into #TempListDateTimeSabtFactor(fldShomareFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeKarbar,fldNameKarbar)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldCodeKarbar,fldNameKarbar from tblAmalkardekarbar where fldNameForm=N'خرید' and NoeAmaliat=N'جدید' )
			--select * from #TempListDateTimeSabtFactor

insert into #TempListDateFactor(fldShomareFactor,fldDateFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizeKharid )
			--select * from tblRizeForoosh

		
			--select * from #TempListDateFactor
		--select * from #TempSumKharid
update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumKharid as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeAnbar,fldNameAnbar,fldCodeKarbar,fldNameKarbar,fldLock)
 select DISTINCT  tblRizeKharid.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,'' as fldDateFactor,
						'' as fldTimeFacttor,'' as fldDateFacttor,#TempSumKharid.fldCodeAnbar,'' as fldNameAnbar,0 as fldCodeKarbar,'' as fldNameKarbar,tblKharid.fldLock
							  
 from tblRizeKharid 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizeKharid.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumKharid on #TempSumKharid.fldShomareFactor=tblRizeKharid.fldShomareFactor
 inner join tblKharid on tblKharid.fldShomareFactorKharid=tblRizeKharid.fldShomareFactor




							--SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDateShow

	update R Set R.fldMablagh=(#TempSumKharid.fldMablaghKharid)
								from #TempShow as R 
								inner join #TempSumKharid on R.fldShomareFactor=#TempSumKharid.fldShomareFactor						
	
	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode
									
									
	update R Set R.fldTimeSabtFactor=(#TempListDateTimeSabtFactor.fldTimeSabtFactor),R.fldDateSabtFactor=(#TempListDateTimeSabtFactor.fldDateSabtFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor
	
		update R Set R.fldNameKarbar=(#TempListDateTimeSabtFactor.fldNameKarbar),R.fldCodeKarbar=(#TempListDateTimeSabtFactor.fldCodeKarbar)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor

	update R Set R.fldDateFactor=(#TempListDateFactor.fldDateFactor)
								from #TempShow as R 
								inner join #TempListDateFactor on R.fldShomareFactor=#TempListDateFactor.fldShomareFactor

	update #TempShow set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempShow.fldCodetafzili=tbl5.fldCodeTafzili

	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end



	 if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end

	--insert into #tempPishFactor
	--	(fldShomareFactorForoosh,fldShomarepishfactor)
	--Select  fldShomareFactorForoosh,fldShomarepishfactor from tblForoosh Where (isnumeric(fldShomarePishFactor) = 1) and fldShomarePishFactor<>0

	--delete from #TempShow 
	--	 where fldCodeTafzili NOT Like '10000 %'
	--update #TempShow set fldShomareFactorForoosh=#tempPishFactor.fldShomareFactorForoosh from #tempPishFactor Where #TempShow.fldShomareFactor=#tempPishFactor.fldShomarePishFactor
	
	--update #TempShow set fldShomarePishfactor =tblForoosh.fldShomarePishFactor from tblForoosh Where 

-- ########################################################
-- Group Ashkhas Limitation
-- ########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ########################################################

SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeAnbar,fldNameAnbar,fldCodeKarbar,fldNameKarbar,fldShomareTell
,fldLock FROM  #TempShow where 
fldCodeTafzili in (select code from @accessCodes) and
fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z168ShowList_ReportFactorBargashtAzKharid]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[z168ShowList_ReportFactorBargashtAzKharid]
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@DateFactorAz varchar(50),
	@DateFactorTa varchar(50),
	@MablaghAz decimal, 
	@MablaghTa decimal
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 

 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)
 create table #TempListDateTimeSabtFactor(
fldShomareFactor bigint,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS
)

 create table #TempListDateFactor(
fldShomareFactor bigint,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS
)

 create table #TempSumKharid(
fldShomareFactor bigint,
fldMablaghKharid decimal,
fldCodeAnbar bigint

)

create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
fldShomareFactorKharid bigint,
fldLock nvarchar(5)
)


	delete from #TempSumKharid
	delete from #TempListDateTimeSabtFactor
	delete from #TempShow
	delete from #TempListCodeAnbar

update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizeKharid where fldRadif=1)

insert into #TempSumKharid(fldShomareFactor,fldMablaghKharid,fldCodeAnbar)
			(select tblRizeBargashtAzKharid.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizeBargashtAzKharid  group by fldShomareFactor )

--select * from #TempListCodeAnbar
insert into #TempListDateTimeSabtFactor(fldShomareFactor,fldTimeSabtFactor,fldDateSabtFactor,fldNameKarbar)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldNameKarbar from tblAmalkardekarbar where fldNameForm=N'برگشت از خرید' and NoeAmaliat=N'جدید' )
			
	--		select * from tblAmalkardekarbar

insert into #TempListDateFactor(fldShomareFactor,fldDateFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizeBargashtAzKharid )
			--select * from tblRizeForoosh

		
			--select * from #TempListDateFactor
		--select * from #TempSumKharid
update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumKharid as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeAnbar,fldNameAnbar,fldNameKarbar,fldLock)
 select DISTINCT  tblRizeBargashtAzKharid.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,'' as fldDateFactor,
						'' as fldTimeFacttor,'' as fldDateFacttor,#TempSumKharid.fldCodeAnbar,'' as fldNameAnbar,'' as fldNameKarbar,tblBargashtAzKharid.fldLock						  
 from tblRizeBargashtAzKharid 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizeBargashtAzKharid.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumKharid on #TempSumKharid.fldShomareFactor=tblRizeBargashtAzKharid.fldShomareFactor
 inner join tblBargashtAzKharid on tblBargashtAzKharid.fldShomareFactorKharid=tblRizeBargashtAzKharid.fldShomareFactor




							--SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDateShow

	update R Set R.fldMablagh=(#TempSumKharid.fldMablaghKharid)
								from #TempShow as R 
								inner join #TempSumKharid on R.fldShomareFactor=#TempSumKharid.fldShomareFactor						
	
	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode
									
									
	update R Set R.fldTimeSabtFactor=(#TempListDateTimeSabtFactor.fldTimeSabtFactor),R.fldDateSabtFactor=(#TempListDateTimeSabtFactor.fldDateSabtFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor
	
		update R Set R.fldNameKarbar=(#TempListDateTimeSabtFactor.fldNameKarbar)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor

	update R Set R.fldDateFactor=(#TempListDateFactor.fldDateFactor)
								from #TempShow as R 
								inner join #TempListDateFactor on R.fldShomareFactor=#TempListDateFactor.fldShomareFactor

	update #TempShow set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempShow.fldCodetafzili=tbl5.fldCodeTafzili

	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end



	 if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end

	--insert into #tempPishFactor
	--	(fldShomareFactorForoosh,fldShomarepishfactor)
	--Select  fldShomareFactorForoosh,fldShomarepishfactor from tblForoosh Where (isnumeric(fldShomarePishFactor) = 1) and fldShomarePishFactor<>0

	--delete from #TempShow 
	--	 where fldCodeTafzili NOT Like '10000 %'
	--update #TempShow set fldShomareFactorForoosh=#tempPishFactor.fldShomareFactorForoosh from #tempPishFactor Where #TempShow.fldShomareFactor=#tempPishFactor.fldShomarePishFactor
	
	--update #TempShow set fldShomarePishfactor =tblForoosh.fldShomarePishFactor from tblForoosh Where 
SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeAnbar,fldNameAnbar,fldNameKarbar,fldShomareTell
,fldLock FROM  #TempShow where 
fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z168ShowList_ReportFactorBargashtAzKharid_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[z168ShowList_ReportFactorBargashtAzKharid_WithUserCode]
--چندزبانه
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@DateFactorAz varchar(50),
	@DateFactorTa varchar(50),
	@MablaghAz decimal(18,4), 
	@MablaghTa decimal(18,4),
	@userCode bigint
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 

 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)
 create table #TempListDateTimeSabtFactor(
fldShomareFactor bigint,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeKarbar bigint,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS
)

 create table #TempListDateFactor(
fldShomareFactor bigint,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS
)

 create table #TempSumKharid(
fldShomareFactor bigint,
fldMablaghKharid decimal(18,4),
fldCodeAnbar bigint

)

create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(200) COLLATE Persian_100_CI_AS,
fldMablagh decimal(18,4),
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeKarbar bigint,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
fldShomareFactorKharid bigint,
fldLock nvarchar(10)
)


	delete from #TempSumKharid
	delete from #TempListDateTimeSabtFactor
	delete from #TempShow
	delete from #TempListCodeAnbar

update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizeKharid where fldRadif=1)

insert into #TempSumKharid(fldShomareFactor,fldMablaghKharid,fldCodeAnbar)
			(select tblRizeBargashtAzKharid.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizeBargashtAzKharid  group by fldShomareFactor )

--select * from #TempListCodeAnbar
insert into #TempListDateTimeSabtFactor(fldShomareFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeKarbar,fldNameKarbar)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldCodeKarbar,fldNameKarbar from tblAmalkardekarbar where fldNameForm=N'برگشت از خرید' and NoeAmaliat=N'جدید' )
			
	--		select * from tblAmalkardekarbar

insert into #TempListDateFactor(fldShomareFactor,fldDateFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizeBargashtAzKharid )
			--select * from tblRizeForoosh

		
			--select * from #TempListDateFactor
		--select * from #TempSumKharid
update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumKharid as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeAnbar,fldNameAnbar,fldCodeKarbar,fldNameKarbar,fldLock)
 select DISTINCT  tblRizeBargashtAzKharid.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,'' as fldDateFactor,
						'' as fldTimeFacttor,'' as fldDateFacttor,#TempSumKharid.fldCodeAnbar,'' as fldNameAnbar,0 as fldCodeKarbar,'' as fldNameKarbar,tblBargashtAzKharid.fldLock						  
 from tblRizeBargashtAzKharid 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizeBargashtAzKharid.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumKharid on #TempSumKharid.fldShomareFactor=tblRizeBargashtAzKharid.fldShomareFactor
 inner join tblBargashtAzKharid on tblBargashtAzKharid.fldShomareFactorKharid=tblRizeBargashtAzKharid.fldShomareFactor




							--SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDateShow

	update R Set R.fldMablagh=(#TempSumKharid.fldMablaghKharid)
								from #TempShow as R 
								inner join #TempSumKharid on R.fldShomareFactor=#TempSumKharid.fldShomareFactor						
	
	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode
									
									
	update R Set R.fldTimeSabtFactor=(#TempListDateTimeSabtFactor.fldTimeSabtFactor),R.fldDateSabtFactor=(#TempListDateTimeSabtFactor.fldDateSabtFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor
	
		update R Set R.fldNameKarbar=(#TempListDateTimeSabtFactor.fldNameKarbar),R.fldCodeKarbar=(#TempListDateTimeSabtFactor.fldCodeKarbar)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor

	update R Set R.fldDateFactor=(#TempListDateFactor.fldDateFactor)
								from #TempShow as R 
								inner join #TempListDateFactor on R.fldShomareFactor=#TempListDateFactor.fldShomareFactor

	update #TempShow set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempShow.fldCodetafzili=tbl5.fldCodeTafzili

	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end



	 if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end

	--insert into #tempPishFactor
	--	(fldShomareFactorForoosh,fldShomarepishfactor)
	--Select  fldShomareFactorForoosh,fldShomarepishfactor from tblForoosh Where (isnumeric(fldShomarePishFactor) = 1) and fldShomarePishFactor<>0

	--delete from #TempShow 
	--	 where fldCodeTafzili NOT Like '10000 %'
	--update #TempShow set fldShomareFactorForoosh=#tempPishFactor.fldShomareFactorForoosh from #tempPishFactor Where #TempShow.fldShomareFactor=#tempPishFactor.fldShomarePishFactor
	
	--update #TempShow set fldShomarePishfactor =tblForoosh.fldShomarePishFactor from tblForoosh Where 
-- ######################################################################
-- ###################### Group Ashkhas Limitation
-- ######################################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ######################################################################

SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeAnbar,fldNameAnbar,fldCodeKarbar,fldNameKarbar,fldShomareTell
,fldLock FROM  #TempShow where 
fldCodeTafzili in (select code from @accessCodes) and
fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z169ShowList_ReportFactorForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[z169ShowList_ReportFactorForoosh]
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@ListVisitor varchar(5000)=null,
	@MablaghAz decimal, 
	@MablaghTa decimal
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 
 create table #TempSumForoosh(
fldShomareFactor bigint,
fldMablaghForoosh decimal,
fldCodeAnbar bigint

)
 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)
 create table #TempListDateTimeSabtFactor(
fldShomareFactor bigint,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS
)

 create table #TempListDateFactor(
fldShomareFactor bigint,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS

)


create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeVisitor bigint,
fldNameVisitor NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
fldShomareFactorForoosh bigint,
fldLock nvarchar(5)
)



	delete from #TempListDateTimeSabtFactor
	delete from #TempShow
	delete from #TempListCodeAnbar
	delete from #TempSumForoosh

update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizeForoosh where fldRadif=1)

insert into #TempSumForoosh(fldShomareFactor,fldMablaghForoosh,fldCodeAnbar)
			(select tblRizeForoosh.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizeForoosh  group by fldShomareFactor )


insert into #TempListDateTimeSabtFactor(fldShomareFactor,fldTimeSabtFactor,fldDateSabtFactor,fldNameKarbar)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldNameKarbar from tblAmalkardekarbar where fldNameForm='فروش' and NoeAmaliat=N'جدید' )
			
			--select * from #TempListCodeAnbar
			--select * from #TempSumForoosh
			--select * from #TempListDateTimeSabtFactor
			--select * from #TempListDateFactor

insert into #TempListDateFactor(fldShomareFactor,fldDateFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizeForoosh )
			--select * from tblRizeForoosh

update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumForoosh as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldNameKarbar,fldLock)
 select DISTINCT  tblRizeForoosh.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,'' as fldDateFactor,
						'' as fldTimeFacttor,'' as fldDateFacttor,
					tblRizeForoosh.fldCodeVasete ,''as fldNameVisitor,#TempSumForoosh.fldCodeAnbar,'' as fldNameAnbar,'' as fldNameKarbar,tblForoosh.fldLock
							  
 from tblRizeForoosh 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizeForoosh.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumForoosh on #TempSumForoosh.fldShomareFactor=tblRizeForoosh.fldShomareFactor
 inner join tblForoosh on #TempSumForoosh.fldShomareFactor=tblForoosh.fldShomareFactorForoosh




							--SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDateShow

	update R Set R.fldMablagh=(#TempSumForoosh.fldMablaghForoosh)
								from #TempShow as R 
								inner join #TempSumForoosh on R.fldShomareFactor=#TempSumForoosh.fldShomareFactor						
	update R Set R.fldNameVisitor=(viewLoadVizitor.fldSharheTafzili)
								from #TempShow as R 
								inner join viewLoadVizitor on R.fldCodeVisitor=viewLoadVizitor.fldCodeTafzili						

	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode
									
	update R Set R.fldTimeSabtFactor=(#TempListDateTimeSabtFactor.fldTimeSabtFactor),R.fldDateSabtFactor=(#TempListDateTimeSabtFactor.fldDateSabtFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor
	

	update R Set R.fldNameKarbar=(#TempListDateTimeSabtFactor.fldNameKarbar)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor

	update R Set R.fldDateFactor=(#TempListDateFactor.fldDateFactor)
								from #TempShow as R 
								inner join #TempListDateFactor on R.fldShomareFactor=#TempListDateFactor.fldShomareFactor

	update #TempShow set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempShow.fldCodetafzili=tbl5.fldCodeTafzili

	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end

	if @ListVisitor <>''
		      begin
		       declare @StrDeleteVisitor varchar(5000)
		        set  @StrDeleteVisitor='delete from #TempShow 
		            where '+ @ListVisitor + ''
					execute(@StrDeleteVisitor)
		      end

	 if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end

	--insert into #tempFactor
	--	(fldShomareFactorForoosh)
	--Select  fldShomareFactorForoosh from tblForoosh Where (isnumeric(fldShomareFactor) = 1) and fldShomareFactor<>0


	--update #TempShow set fldShomareFactorForoosh=#tempPishFactor.fldShomareFactorForoosh from #tempPishFactor Where #TempShow.fldShomareFactor=#tempPishFactor.fldShomarePishFactor
	
	--update #TempShow set fldShomarePishfactor =tblForoosh.fldShomarePishFactor from tblForoosh Where 
SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldNameKarbar,fldShomareTell  
,fldLock FROM  #TempShow where 
fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z169ShowList_ReportFactorForoosh_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[z169ShowList_ReportFactorForoosh_WithUserCode]
--چندزبانه
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@ListVisitor varchar(5000)=null,
	@ListFeeDynamic varchar(5000)=null,
	@MablaghAz decimal(18, 4), 
	@MablaghTa decimal(18, 4),
	@userCode bigint
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;


	--declare @ListAshkhas varchar(5000)=null
	--declare @ListGroupAshkhas varchar(5000)=null
	--declare @ListKarbaran varchar(5000)=null
	--declare @ListAnbar varchar(5000)=null
	--declare @ListVisitor varchar(5000)=null
	--declare @ListFeeDynamic varchar(5000)=null
	--declare @MablaghAz decimal(18, 4) = 0
	--declare @MablaghTa decimal(18, 4) = 9000000000
	--declare @userCode bigint = 1000
 
 create table #TempSumForoosh(
fldShomareFactor bigint,
fldMablaghForoosh decimal(18, 4),
fldCodeAnbar bigint

)
 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)
 create table #TempListDateTimeSabtFactor(
fldShomareFactor bigint,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeKarbar bigint,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS
)

 create table #TempListDateFactor(
fldShomareFactor bigint,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS

)


create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(150) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(150) COLLATE Persian_100_CI_AS,
fldMablagh decimal(18, 4),
fldDateFactor  Nvarchar(50) collate Persian_100_CI_AS,
fldTimeSabtFactor  Nvarchar(50) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(50) collate Persian_100_CI_AS,
fldCodeVisitor bigint,
fldNameVisitor NVarchar(150) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(150) COLLATE Persian_100_CI_AS,
fldCodeFeeDynamic bigint,
fldNameFeeDynamic NVarchar(150) COLLATE Persian_100_CI_AS,
fldCodeKarbar bigint,
fldNameKarbar NVarchar(150) COLLATE Persian_100_CI_AS,
fldShomareTell nvarchar(50) COLLATE Persian_100_CI_AS,
fldShomareFactorForoosh bigint,
fldLock nvarchar(15)
)



	delete from #TempListDateTimeSabtFactor
	delete from #TempShow
	delete from #TempListCodeAnbar
	delete from #TempSumForoosh

update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizeForoosh where fldRadif=1)

insert into #TempSumForoosh(fldShomareFactor,fldMablaghForoosh,fldCodeAnbar)
			(select tblRizeForoosh.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizeForoosh  group by fldShomareFactor )


insert into #TempListDateTimeSabtFactor(fldShomareFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeKarbar,fldNameKarbar)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldCodeKarbar,fldNameKarbar from tblAmalkardekarbar where fldNameForm='فروش' and NoeAmaliat=N'جدید' )
			
			--select * from #TempListCodeAnbar
			--select * from #TempSumForoosh
			--select * from #TempListDateTimeSabtFactor
			--select * from #TempListDateFactor

insert into #TempListDateFactor(fldShomareFactor,fldDateFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizeForoosh )
			

update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumForoosh as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldCodeFeeDynamic,fldNameFeeDynamic,fldCodeKarbar,fldNameKarbar,fldLock)
 select DISTINCT  tblRizeForoosh.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,'' as fldDateFactor,
						'' as fldTimeFacttor,'' as fldDateFacttor,
					tblRizeForoosh.fldCodeVasete ,''as fldNameVisitor,#TempSumForoosh.fldCodeAnbar,'' as fldNameAnbar,tblForoosh.fldCodeFeeDynamic,'' as fldNameFeeDynamic,0 as fldCodeKarbar,'' as fldNameKarbar,tblForoosh.fldLock
							  
 from tblRizeForoosh 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizeForoosh.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumForoosh on #TempSumForoosh.fldShomareFactor=tblRizeForoosh.fldShomareFactor
 inner join tblForoosh on #TempSumForoosh.fldShomareFactor=tblForoosh.fldShomareFactorForoosh


	

							--SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDateShow

	update R Set R.fldMablagh=(#TempSumForoosh.fldMablaghForoosh)
								from #TempShow as R 
								inner join #TempSumForoosh on R.fldShomareFactor=#TempSumForoosh.fldShomareFactor						
	update R Set R.fldNameVisitor=(viewLoadVizitor.fldSharheTafzili)
								from #TempShow as R 
								inner join viewLoadVizitor on R.fldCodeVisitor=viewLoadVizitor.fldCodeTafzili						

	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode
									
	update R Set R.fldTimeSabtFactor=(#TempListDateTimeSabtFactor.fldTimeSabtFactor),R.fldDateSabtFactor=(#TempListDateTimeSabtFactor.fldDateSabtFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor
	

	update R Set R.fldNameKarbar=(#TempListDateTimeSabtFactor.fldNameKarbar),R.fldCodeKarbar=(#TempListDateTimeSabtFactor.fldCodeKarbar)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor

	update R Set R.fldDateFactor=(#TempListDateFactor.fldDateFactor)
								from #TempShow as R 
								inner join #TempListDateFactor on R.fldShomareFactor=#TempListDateFactor.fldShomareFactor

	update #TempShow set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempShow.fldCodetafzili=tbl5.fldCodeTafzili

	update R Set R.fldNameFeeDynamic=(tblSharheFeeDynamic.fldSharh)
								from #TempShow as R 
								inner join tblSharheFeeDynamic on R.fldCodeFeeDynamic=tblSharheFeeDynamic.fldCodeSharh
	

	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end

	if @ListVisitor <>''
		      begin
		       declare @StrDeleteVisitor varchar(5000)
		        set  @StrDeleteVisitor='delete from #TempShow 
		            where '+ @ListVisitor + ''
					execute(@StrDeleteVisitor)
		      end

	 if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end
			  	if @ListFeeDynamic <>''
		      begin
		       declare @StrDeleteFeeDynamic varchar(5000)
		        set  @StrDeleteFeeDynamic='delete from #TempShow 
		            where '+ @ListFeeDynamic + ''
					execute(@StrDeleteFeeDynamic)
		      end

	--insert into #tempFactor
	--	(fldShomareFactorForoosh)
	--Select  fldShomareFactorForoosh from tblForoosh Where (isnumeric(fldShomareFactor) = 1) and fldShomareFactor<>0


	--update #TempShow set fldShomareFactorForoosh=#tempPishFactor.fldShomareFactorForoosh from #tempPishFactor Where #TempShow.fldShomareFactor=#tempPishFactor.fldShomarePishFactor
	
	--update #TempShow set fldShomarePishfactor =tblForoosh.fldShomarePishFactor from tblForoosh Where 

-- ##########################################
declare @accessCodesTafzili table(code bigint)
insert into @accessCodesTafzili select * from ZF_GetAccessableAshkhasCode(@userCode) 

-- ##########################################
SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldNameFeeDynamic,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldCodeKarbar,fldNameKarbar,fldShomareTell  
,fldLock FROM  #TempShow where 
fldCodeTafzili in (select code from @accessCodesTafzili ) and
fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z170ShowList_ReportfactorBargashtAzForoosh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z170ShowList_ReportfactorBargashtAzForoosh]
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@ListVisitor varchar(5000)=null,
	@MablaghAz decimal, 
	@MablaghTa decimal
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 
 create table #TempSumForoosh(
fldShomareFactor bigint,
fldMablaghForoosh decimal,
fldCodeAnbar bigint

)
 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)
 create table #TempListDateTimeSabtFactor(
fldShomareFactor bigint,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS
)

 create table #TempListDateFactor(
fldShomareFactor bigint,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS
)


create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeVisitor bigint,
fldNameVisitor NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
fldShomareFactorForoosh bigint,
fldLock nvarchar(5)
)



	delete from #TempListDateTimeSabtFactor
	delete from #TempShow
	delete from #TempListCodeAnbar
	delete from #TempSumForoosh

update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizeBargashtAzForoosh where fldRadif=1)

insert into #TempSumForoosh(fldShomareFactor,fldMablaghForoosh,fldCodeAnbar)
			(select tblRizeBargashtAzForoosh.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizeBargashtAzForoosh  group by fldShomareFactor )


insert into #TempListDateTimeSabtFactor(fldShomareFactor,fldTimeSabtFactor,fldDateSabtFactor,fldNameKarbar)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldNameKarbar from tblAmalkardekarbar where fldNameForm='برگشت از فروش' and NoeAmaliat=N'جدید' )
			
			--select * from #TempListCodeAnbar
			--select * from #TempSumForoosh
			--select * from #TempListDateTimeSabtFactor
			--select * from #TempListDateFactor

insert into #TempListDateFactor(fldShomareFactor,fldDateFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizeBargashtAzForoosh)
			--select * from tblRizeBargashtAzForoosh

update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumForoosh as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldNameKarbar,fldLock)
 select DISTINCT  tblRizeBargashtAzForoosh.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,'' as fldDateFactor,
						'' as fldTimeFacttor,'' as fldDateFacttor,
					tblRizeBargashtAzForoosh.fldCodeVasete ,''as fldNameVisitor,#TempSumForoosh.fldCodeAnbar,'' as fldNameAnbar,'' as fldNameKarbar,tblBargashtAzForoosh.fldLock
							  
 from tblRizeBargashtAzForoosh 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizeBargashtAzForoosh.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumForoosh on #TempSumForoosh.fldShomareFactor=tblRizeBargashtAzForoosh.fldShomareFactor
 inner join tblForoosh on #TempSumForoosh.fldShomareFactor=tblForoosh.fldShomareFactorForoosh
 inner join tblBargashtAzForoosh on tblRizeBargashtAzForoosh.fldShomareFactor=tblBargashtAzForoosh.fldShomareFactorForoosh



							--SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDateShow

	update R Set R.fldMablagh=(#TempSumForoosh.fldMablaghForoosh)
								from #TempShow as R 
								inner join #TempSumForoosh on R.fldShomareFactor=#TempSumForoosh.fldShomareFactor						
	update R Set R.fldNameVisitor=(viewLoadVizitor.fldSharheTafzili)
								from #TempShow as R 
								inner join viewLoadVizitor on R.fldCodeVisitor=viewLoadVizitor.fldCodeTafzili						

	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode
									
	update R Set R.fldTimeSabtFactor=(#TempListDateTimeSabtFactor.fldTimeSabtFactor),R.fldDateSabtFactor=(#TempListDateTimeSabtFactor.fldDateSabtFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor
	

	update R Set R.fldNameKarbar=(#TempListDateTimeSabtFactor.fldNameKarbar)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor

	update R Set R.fldDateFactor=(#TempListDateFactor.fldDateFactor)
								from #TempShow as R 
								inner join #TempListDateFactor on R.fldShomareFactor=#TempListDateFactor.fldShomareFactor

	update #TempShow set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempShow.fldCodetafzili=tbl5.fldCodeTafzili

	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end

	if @ListVisitor <>''
		      begin
		       declare @StrDeleteVisitor varchar(5000)
		        set  @StrDeleteVisitor='delete from #TempShow 
		            where '+ @ListVisitor + ''
					execute(@StrDeleteVisitor)
		      end

	 if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end

	--insert into #tempFactor
	--	(fldShomareFactorForoosh)
	--Select  fldShomareFactorForoosh from tblForoosh Where (isnumeric(fldShomareFactor) = 1) and fldShomareFactor<>0


	--update #TempShow set fldShomareFactorForoosh=#tempPishFactor.fldShomareFactorForoosh from #tempPishFactor Where #TempShow.fldShomareFactor=#tempPishFactor.fldShomarePishFactor
	
	--update #TempShow set fldShomarePishfactor =tblForoosh.fldShomarePishFactor from tblForoosh Where 
SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldNameKarbar,fldShomareTell  
,fldLock FROM  #TempShow where 
fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z170ShowList_ReportfactorBargashtAzForoosh_WithUserCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[z170ShowList_ReportfactorBargashtAzForoosh_WithUserCode]
--چندزبانه
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@ListKarbaran varchar(5000)=null,
	@ListAnbar varchar(5000)=null,
	@ListVisitor varchar(5000)=null,
	@MablaghAz decimal(18,4), 
	@MablaghTa decimal(18,4),
	@userCode bigint
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 
 create table #TempSumForoosh(
fldShomareFactor bigint,
fldMablaghForoosh decimal(18,4),
fldCodeAnbar bigint

)
 create table #TempListCodeAnbar(
fldShomareFactor bigint,
fldCodeAnbar bigint

)
 create table #TempListDateTimeSabtFactor(
fldShomareFactor bigint,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeKarbar bigint,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS
)

 create table #TempListDateFactor(
fldShomareFactor bigint,
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS
)


create table #TempShow(

fldShomareFactor bigint,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(200) COLLATE Persian_100_CI_AS,
fldCodeGroupAshkhas bigint,
fldNameGroupAshkhas NVarchar(200) COLLATE Persian_100_CI_AS,
fldMablagh decimal(18,4),
fldDateFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldTimeSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldDateSabtFactor  Nvarchar(20) collate Persian_100_CI_AS,
fldCodeVisitor bigint,
fldNameVisitor NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeAnbar bigint,
fldNameAnbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeKarbar bigint,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldShomareTell nvarchar(20) COLLATE Persian_100_CI_AS,
fldShomareFactorForoosh bigint,
fldLock nvarchar(10)
)



	delete from #TempListDateTimeSabtFactor
	delete from #TempShow
	delete from #TempListCodeAnbar
	delete from #TempSumForoosh

update tblAmalkardekarbar set  fldNAMEForm =Replace(fldNAMEForm,'ي','ی')
update tblAmalkardekarbar set  NoeAmaliat =Replace(NoeAmaliat,'ي','ی')
insert into #TempListCodeAnbar(fldShomareFactor,fldCodeAnbar)
			(select fldShomareFactor,fldCodeAnbar from tblRizeBargashtAzForoosh where fldRadif=1)

insert into #TempSumForoosh(fldShomareFactor,fldMablaghForoosh,fldCodeAnbar)
			(select tblRizeBargashtAzForoosh.fldShomareFactor,Sum(fldMablaghKhales),0 as fldCodeAnbar from tblRizeBargashtAzForoosh  group by fldShomareFactor )


insert into #TempListDateTimeSabtFactor(fldShomareFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeKarbar,fldNameKarbar)
			(select fldShomare,fldTime,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldCodeKarbar,fldNameKarbar from tblAmalkardekarbar where fldNameForm='برگشت از فروش' and NoeAmaliat=N'جدید' )
			
			--select * from #TempListCodeAnbar
			--select * from #TempSumForoosh
			--select * from #TempListDateTimeSabtFactor
			--select * from #TempListDateFactor

insert into #TempListDateFactor(fldShomareFactor,fldDateFactor)
			(select fldShomareFactor,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate from tblRizeBargashtAzForoosh)
			--select * from tblRizeBargashtAzForoosh

update R Set R.fldCodeAnbar=(#TempListCodeAnbar.fldCodeAnbar)
								from #TempSumForoosh as R 
								inner join #TempListCodeAnbar on R.fldShomareFactor=#TempListCodeAnbar.fldShomareFactor


insert into #TempShow(fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldCodeKarbar,fldNameKarbar,fldLock)
 select DISTINCT  tblRizeBargashtAzForoosh.fldShomareFactor,fldCodeTafsili
				,tblTafzili.fldSharheTafzili,dbo.tblGroupAshkhas.fldGroupId
 ,dbo.tblGroupAshkhas.fldGroupName,0 as fldMablagh,'' as fldDateFactor,
						'' as fldTimeFacttor,'' as fldDateFacttor,
					tblRizeBargashtAzForoosh.fldCodeVasete ,''as fldNameVisitor,#TempSumForoosh.fldCodeAnbar,'' as fldNameAnbar,0 as fldCodeKarbar,'' as fldNameKarbar,tblBargashtAzForoosh.fldLock
							  
 from tblRizeBargashtAzForoosh 
 inner join tblTafzili on tblTafzili.fldCodeTafzili=tblRizeBargashtAzForoosh.fldCodeTafsili
 inner join tblGroupAshkhas on tblGroupAshkhas.fldGroupId=tblTafzili.fldCodeGroup
 inner join #TempSumForoosh on #TempSumForoosh.fldShomareFactor=tblRizeBargashtAzForoosh.fldShomareFactor
 inner join tblForoosh on #TempSumForoosh.fldShomareFactor=tblForoosh.fldShomareFactorForoosh
 inner join tblBargashtAzForoosh on tblRizeBargashtAzForoosh.fldShomareFactor=tblBargashtAzForoosh.fldShomareFactorForoosh



							--SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' + 
							-- SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDateShow

	update R Set R.fldMablagh=(#TempSumForoosh.fldMablaghForoosh)
								from #TempShow as R 
								inner join #TempSumForoosh on R.fldShomareFactor=#TempSumForoosh.fldShomareFactor						
	update R Set R.fldNameVisitor=(viewLoadVizitor.fldSharheTafzili)
								from #TempShow as R 
								inner join viewLoadVizitor on R.fldCodeVisitor=viewLoadVizitor.fldCodeTafzili						

	update R Set R.fldNameAnbar=(viewGetAnbarForComboBox.fldNameAnbar)
								from #TempShow as R 
								inner join viewGetAnbarForComboBox on R.fldCodeAnbar=viewGetAnbarForComboBox.fldCode
									
	update R Set R.fldTimeSabtFactor=(#TempListDateTimeSabtFactor.fldTimeSabtFactor),R.fldDateSabtFactor=(#TempListDateTimeSabtFactor.fldDateSabtFactor)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor
	

	update R Set R.fldNameKarbar=(#TempListDateTimeSabtFactor.fldNameKarbar),R.fldCodeKarbar=(#TempListDateTimeSabtFactor.fldCodeKarbar)
								from #TempShow as R 
								inner join #TempListDateTimeSabtFactor on R.fldShomareFactor=#TempListDateTimeSabtFactor.fldShomareFactor

	update R Set R.fldDateFactor=(#TempListDateFactor.fldDateFactor)
								from #TempShow as R 
								inner join #TempListDateFactor on R.fldShomareFactor=#TempListDateFactor.fldShomareFactor

	update #TempShow set fldShomareTell=tbl5.fldShomare from
		(select fldShomare,fldcodetafzili from
			(SELECT         fldcodetafzili,row_number() over (partition by fldcodetafzili order by fldcodetafzili) RowNo,fldShomare
			FROM            [tblDaftarcheTell]) DB_Address
			where RowNo=1) as tbl5 
			where #TempShow.fldCodetafzili=tbl5.fldCodeTafzili

	
	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

	if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #TempShow 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end
	if @ListAnbar <>''
   begin
		 declare @StrDeleteAnbar varchar(5000)
	     set  @StrDeleteAnbar='delete from #TempShow 
		 where '+ @ListAnbar + ''
		execute(@StrDeleteAnbar)
	end

	if @ListVisitor <>''
		      begin
		       declare @StrDeleteVisitor varchar(5000)
		        set  @StrDeleteVisitor='delete from #TempShow 
		            where '+ @ListVisitor + ''
					execute(@StrDeleteVisitor)
		      end

	 if @ListKarbaran<>''
		      begin
		       declare @StrDeleteKarbar varchar(5000)
		        set  @StrDeleteKarbar='delete from #TempShow 
		            where '+ @ListKarbaran + ''
					execute(@StrDeleteKarbar)
		      end

	--insert into #tempFactor
	--	(fldShomareFactorForoosh)
	--Select  fldShomareFactorForoosh from tblForoosh Where (isnumeric(fldShomareFactor) = 1) and fldShomareFactor<>0


	--update #TempShow set fldShomareFactorForoosh=#tempPishFactor.fldShomareFactorForoosh from #tempPishFactor Where #TempShow.fldShomareFactor=#tempPishFactor.fldShomarePishFactor
	
	--update #TempShow set fldShomarePishfactor =tblForoosh.fldShomarePishFactor from tblForoosh Where 

-- ##########################################################
-- #######Group Ashkhas Limitation###########################
-- ##########################################################
declare @accessCodes table(code bigint)
insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 
-- ##########################################################

SELECT  ROW_NUMBER() OVER(ORDER BY fldShomareFactor) as fldRadif, fldShomareFactor ,fldCodeTafzili,fldSharhTafzili,fldCodeGroupAshkhas
,fldNameGroupAshkhas,fldMablagh,fldDateFactor,fldTimeSabtFactor,fldDateSabtFactor,fldCodeVisitor,fldNameVisitor,fldCodeAnbar,fldNameAnbar,fldCodeKarbar,fldNameKarbar,fldShomareTell  
,fldLock FROM  #TempShow where 
fldCodeTafzili in (select code from @accessCodes) and
fldMablagh between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z171ShowList_ReportHazine]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z171ShowList_ReportHazine]
--چندزبانه
	@ListHazine varchar(5000)=null,
	@MablaghAz decimal(18,4), 
	@MablaghTa decimal(18,4)
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 
create table #TempShowHazine(
id bigint,
fldCodeHazune bigint,
fldNameHazine NVarchar(100) COLLATE Persian_100_CI_AS

)




create table #TempShow(
fldShomareHazine bigint,
fldShomareSanad bigint,
fldCodeHazune bigint,
fldNameHazine NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal(18,4),
fldDate NVarchar(100) COLLATE Persian_100_CI_AS,
fldSharh NVarchar(200) COLLATE Persian_100_CI_AS
)


		declare @strCodeKol nvarchar(100)
	execute CodeKolHesab 'hazine', @strCodeKol output
	--execute ShowHesabKolMoin @strCodeKol
	--insert into #TempShowHazine(fldCodeHazune,fldNameHazine,id)
	--select [fldCodeHesab],[fldSharheHesab],[ID] from tblKolMoin
	--	Where fldCodeHesab<>@strCodeKol and left(fldCodeHesab,len(@strCodeKol))=@strCodeKol
	--	order by fldCodeHesab

	declare @lngTooleMoin bigint
	set @lngTooleMoin=(Select top(1) Convert(dec,fldKol)+ Convert(dec,fldMoin) from tblPaye)

	insert into #TempShowHazine(fldCodeHazune,fldNameHazine,id)
	select [fldCodeHesab],[fldSharheHesab],[ID] from tblKolMoin
		Where  SUBSTRING(fldCodeHesab,1,1)=SUBSTRING (@strCodeKol,1,1) AND len(fldCodeHesab)=@lngTooleMoin
		order by fldCodeHesab

		

insert into #TempShow(fldShomareHazine,fldShomareSanad,fldCodeHazune,fldNameHazine,fldMablagh,fldDate,fldSharh)
 select tblPardakht.fldShomarePardakht as fldShomareHazine,fldSanad,#TempShowHazine.fldCodeHazune,#TempShowHazine.fldNameHazine,fldBedehkar,fldDate,fldSharh
  from tblSodooreSanad inner join
  #TempShowHazine on #TempShowHazine.fldCodeHazune=tblSodooreSanad.fldMoinKamel  inner join
  tblPardakht on tblPardakht.fldShomareSanad=tblSodooreSanad.fldSanad
  where tblSodooreSanad.fldType='hazine' and fldBedehkar>0 
  and fldBedehkar between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

  	if @ListHazine <>''
   begin
		 declare @StrDeleteHazine varchar(5000)
	     set  @StrDeleteHazine='delete from #TempShow 
		 where '+ @ListHazine + ''
		execute(@StrDeleteHazine)
	end

select ROW_NUMBER() OVER(ORDER BY fldShomareHazine) as fldRadif, fldShomareHazine,fldShomareSanad,fldCodeHazune,fldNameHazine,fldMablagh
,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' +
SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldSharh from #TempShow

END

 

GO
/****** Object:  StoredProcedure [dbo].[z172ShowList_ReportDaramad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z172ShowList_ReportDaramad]
	@ListDaramad varchar(5000)=null,
	@ListAshkhas varchar(5000)=null,
	@MablaghAz decimal(18,4), 
	@MablaghTa decimal(18,4)
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 
create table #TempShowDaramad(
id bigint,
fldCodeDaramad bigint,
fldNameDaramad NVarchar(100) COLLATE Persian_100_CI_AS

)

create table #TempShow(
fldShomareDaramad bigint,
fldShomareSanad bigint,
fldCodeDaramad bigint,
fldNameDaramad NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeTafzili bigint,
fldSharhTafzili NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal(18,4),
fldDate NVarchar(100) COLLATE Persian_100_CI_AS,
fldSharh NVarchar(200) COLLATE Persian_100_CI_AS
)


		declare @strCodeKol nvarchar(100)
	execute CodeKolHesab 'daramad', @strCodeKol output
--	execute ShowHesabKolMoin @strCodeKol
	insert into #TempShowDaramad(fldCodeDaramad,fldNameDaramad,id)
	select [fldCodeHesab],[fldSharheHesab],[ID] from tblKolMoin
		Where fldCodeHesab<>@strCodeKol and left(fldCodeHesab,len(@strCodeKol))=@strCodeKol
		order by fldCodeHesab

insert into #TempShow(fldShomareDaramad,fldShomareSanad,fldCodeDaramad,fldNameDaramad,fldMablagh,fldCodeTafzili,fldSharhTafzili,fldDate,fldSharh)
 select tblDaryaft.fldShomareDaryaft as fldShomareDaramad,fldSanad,#TempShowDaramad.fldCodeDaramad
 ,#TempShowDaramad.fldNameDaramad,fldBestankar,tblSodooreSanad.fldCodeTafzili,'' as fldSharhTafzili,fldDate,fldSharh
  from tblSodooreSanad inner join
  #TempShowDaramad on #TempShowDaramad.fldCodeDaramad=tblSodooreSanad.fldMoinKamel  inner join
  tblDaryaft on tblDaryaft.fldShomareSanad=tblSodooreSanad.fldSanad
  where tblSodooreSanad.fldType='daramad' and fldBestankar>0 
  and fldBestankar between CONVERT(nvarchar(50),@MablaghAz) and CONVERT(nvarchar(50),@MablaghTa)

  	update R Set R.fldSharhTafzili=(tblTafzili.fldSharheTafzili)
								from #TempShow as R 
								inner join tblTafzili on R.fldCodeTafzili=tblTafzili.fldCodeTafzili	

  	if @ListDaramad <>''
   begin
		 declare @StrDeleteDaramad varchar(5000)
	     set  @StrDeleteDaramad='delete from #TempShow 
		 where '+ @ListDaramad + ''
		execute(@StrDeleteDaramad)
	end				

  	if @ListAshkhas <>''
   begin
		 declare @StrDeleteAshkhas varchar(5000)
	     set  @StrDeleteAshkhas='delete from #TempShow 
		 where '+ @ListAshkhas + ''
		execute(@StrDeleteAshkhas)
	end

select ROW_NUMBER() OVER(ORDER BY fldShomareDaramad) as fldRadif, fldShomareDaramad,fldShomareSanad,fldCodeDaramad,fldNameDaramad,fldMablagh
,fldCodeTafzili,fldSharhTafzili,
SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' +
SUBSTRING(CONVERT(nvarchar,fldDate), 7, 2) AS fldDate,fldSharh from #TempShow

END

 

GO
/****** Object:  StoredProcedure [dbo].[z174ShowList_ReportEnteghalBainAnbar]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[z174ShowList_ReportEnteghalBainAnbar]
	--@ListRanande varchar(5000)=null,
	@ListCodeShakhsTahvilDahande varchar(5000)=null,
	@ListCodeShakhsTahvilGirande varchar(5000)=null,
    @ListCodeAnbarTahvilDahande varchar(5000)=null,
	@ListCodeAnbarTahvilGirande varchar(5000)=null,
	@ListKarbaran varchar(5000)=null
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 



create table #TempShow(
fldShomareEnteghal bigint,

fldCodeShakhsTahvilDahande bigint,
fldNameShakhsTahvilDahande NVarchar(100) COLLATE Persian_100_CI_AS,

fldCodeShakhsTahvilGirande bigint,
fldNameShakhsTahvilGirande NVarchar(100) COLLATE Persian_100_CI_AS,

fldCodeAnbarTahvilDahande bigint,
fldNameAnbarTahvilDahande NVarchar(100) COLLATE Persian_100_CI_AS,

fldCodeAnbarTahvilGirande bigint,
fldNameAnbarTahvilGirande NVarchar(100) COLLATE Persian_100_CI_AS,

fldDate NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeKarbar bigint,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldSharh NVarchar(100) COLLATE Persian_100_CI_AS
)

delete #TempShow



insert into #TempShow(fldShomareEnteghal
					  ,fldCodeShakhsTahvilDahande,fldNameShakhsTahvilDahande
					  ,fldCodeShakhsTahvilGirande,fldNameShakhsTahvilGirande
					  ,fldCodeAnbarTahvilDahande,fldNameAnbarTahvilDahande
					  ,fldCodeAnbarTahvilGirande,fldNameAnbarTahvilGirande
					  ,fldDate
					  ,fldCodeKarbar
					  ,fldNameKarbar
					  ,fldSharh)
select distinct tblRizeForoosh.fldShomareEnteghal
			,tblRizeForoosh.fldCodeTafsili as fldCodeShakhsTahvilDahande
			,'' as fldNameShakhsTahvilDahande
			,tblRizeKharid.fldCodeTafsili as fldCodeShakhsTahvilGirande
			,'' as fldNameShakhsTahvilGirande
			,tblRizeForoosh.fldCodeAnbar as fldCodeAnbarTahvilDahande
			,'' as fldNameAnbarTahvilDahande
			,tblRizeKharid.fldCodeAnbar as fldCodeAnbarTahvilGirande
			,'' as fldNameAnbarTahvilGirande
			,tblRizeForoosh.fldDate
			,tblRizeForoosh.fldCodeUser,'' as fldNameKarbar
			,tblRizeForoosh.fldSharhKala  from tblRizeForoosh inner join
			tblRizeKharid on tblRizeForoosh.fldShomareEnteghal=tblRizeKharid.fldShomareEnteghal
where tblRizeForoosh.fldType='enteghal'

--select fldShomareEnteghal,fldCodeTafsili,fldDate from tblRizeKharid where fldType='enteghal'

--select * from #TempShow

	update R Set R.fldNameShakhsTahvilDahande=(tblTafzili.fldSharheTafzili)
								from #TempShow as R 
								inner join tblTafzili on R.fldCodeShakhsTahvilDahande=tblTafzili.fldCodeTafzili

	update R Set R.fldNameShakhsTahvilGirande=(tblTafzili.fldSharheTafzili)
								from #TempShow as R 
								inner join tblTafzili on R.fldCodeShakhsTahvilGirande=tblTafzili.fldCodeTafzili
								
	update R Set R.fldNameAnbarTahvilDahande=(tblAnbar.fldNameAnbar)
								from #TempShow as R 
								inner join tblAnbar on R.fldCodeAnbarTahvilDahande=tblAnbar.fldCode

	update R Set R.fldNameAnbarTahvilGirande=(tblAnbar.fldNameAnbar)
								from #TempShow as R 
								inner join tblAnbar on R.fldCodeAnbarTahvilGirande=tblAnbar.fldCode
		
		if @ListKarbaran <>''
   begin
		 declare @StrDeleteKarbaran varchar(5000)
	     set  @StrDeleteKarbaran='delete from #TempShow 
		 where '+ @ListKarbaran + ''
		execute(@StrDeleteKarbaran)
	end

	if @ListCodeAnbarTahvilDahande <>''
   begin
		 declare @StrDeleteAnbarTahvilDahande varchar(5000)
	     set  @StrDeleteAnbarTahvilDahande='delete from #TempShow 
		 where '+ @ListCodeAnbarTahvilDahande + ''
		execute(@StrDeleteAnbarTahvilDahande)
	end
	if @ListCodeAnbarTahvilGirande <>''
   begin
		 declare @StrDeleteAnbarTahvilGirande varchar(5000)
	     set  @StrDeleteAnbarTahvilGirande='delete from #TempShow 
		 where '+ @ListCodeAnbarTahvilGirande + ''
		execute(@StrDeleteAnbarTahvilGirande)
	end
	if @ListCodeShakhsTahvilDahande <>''
   begin
		 declare @StrDeleteShakhsTahvilDahande varchar(5000)
	     set  @StrDeleteShakhsTahvilDahande='delete from #TempShow 
		 where '+ @ListCodeShakhsTahvilDahande + ''
		execute(@StrDeleteShakhsTahvilDahande)
	end													

	 	if @ListCodeShakhsTahvilGirande <>''
   begin
		 declare @StrDeleteShakhsTahvilGirande varchar(5000)
	     set  @StrDeleteShakhsTahvilGirande='delete from #TempShow 
		 where '+ @ListCodeShakhsTahvilGirande + ''
		execute(@StrDeleteShakhsTahvilGirande)
	end



select ROW_NUMBER() OVER(ORDER BY fldShomareEnteghal) as fldRadif,
fldShomareEnteghal
,fldNameAnbarTahvilDahande
,fldNameShakhsTahvilDahande
,fldNameAnbarTahvilGirande
,fldNameShakhsTahvilGirande,
SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' +
SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate,fldNameKarbar,fldSharh
,fldCodeShakhsTahvilDahande
,fldCodeKarbar
,fldCodeShakhsTahvilGirande
,fldCodeAnbarTahvilDahande
,fldCodeAnbarTahvilGirande from #TempShow


END

 

GO
/****** Object:  StoredProcedure [dbo].[z175ShowList_ReportAsnad]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z175ShowList_ReportAsnad]
	@AzSanad bigint,
	@TaSanad bigint,
	@AzDate bigint,
	@TaDate bigint,
	@ListKarbaran varchar(5000)=null,
	@ListKarbaranUpdate varchar(5000)=null
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 
	--declare @AzSanad bigint = 1
	--declare @TaSanad bigint = 90000
	--declare @AzDate bigint = 13980101
	--declare @TaDate bigint = 13981231
	--declare @ListKarbaran varchar(5000)= '1006'
	--declare @ListKarbaranUpdate varchar(5000)=null

create table #TempShow(
fldShomareSanad bigint,
fldShomare bigint,
fldDate NVarchar(100) COLLATE Persian_100_CI_AS,
fldType NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeKarbar bigint,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeKarbarUpdate bigint,
fldNameKarbarUpdate NVarchar(100) COLLATE Persian_100_CI_AS,
fldSharh NVarchar(max) COLLATE Persian_100_CI_AS
)

create table #Temp(
fldShomare bigint,
fldShomareSanad bigint,
fldCodeKarbar bigint,
fldType NVarchar(100) COLLATE Persian_100_CI_AS

)
create table #TempKarbarUpdate(
id bigint,
fldShomareSanad bigint,
fldCodeKarbarUpdate bigint,
fldNameKarbarUpdate NVarchar(100) COLLATE Persian_100_CI_AS,
fldType NVarchar(100) COLLATE Persian_100_CI_AS

)


delete #TempShow
delete #TempKarbarUpdate
delete #Temp


----------------------------آخرین تغییرکننده هزینه ، درآمد --------------------------
insert #TempKarbarUpdate(id,fldShomareSanad,fldCodeKarbarUpdate,fldNameKarbarUpdate,fldType)
select distinct id,fldSanad,fldCodeKarbar,fldNameKarbar,fldNameForm from tblAmalkardekarbar 
where NoeAmaliat='ویرایش' and fldSanad<>0 and id in (select max(id) from tblAmalkardekarbar group by fldSanad) order by id desc
----------------------------آخرین تغییرکننده سند-------------------------------
insert #TempKarbarUpdate(id,fldShomareSanad,fldCodeKarbarUpdate,fldNameKarbarUpdate,fldType)
select distinct id,fldShomare,fldCodeKarbar,fldNameKarbar,fldNameForm from tblAmalkardekarbar 
where fldNameForm='سند' and NoeAmaliat=N'ویرایش' and tblAmalkardekarbar.id in (select max(tblAmalkardekarbar.id) from tblAmalkardekarbar inner join
tblSodooreSanad on tblSodooreSanad.fldSanad=tblAmalkardekarbar.fldShomare where fldNameForm='سند' and NoeAmaliat=N'ویرایش' group by fldShomare) order by id desc
----------------------------آخرین تغییرکننده فروش-------------------------------
insert #TempKarbarUpdate(id,fldShomareSanad,fldCodeKarbarUpdate,fldNameKarbarUpdate,fldType)
select  tblAmalkardekarbar.id,tblForoosh.fldShomareSanad,fldCodeKarbar,fldNameKarbar,fldNameForm from tblAmalkardekarbar 
inner join tblForoosh on tblForoosh.fldShomareFactorForoosh=tblAmalkardekarbar.fldShomare
where fldNameForm='فروش' and NoeAmaliat=N'ویرایش' and tblAmalkardekarbar.id in (select max(tblAmalkardekarbar.id) from tblAmalkardekarbar inner join
tblSodooreSanad on tblSodooreSanad.fldSanad=tblAmalkardekarbar.fldShomare where fldNameForm='فروش' and NoeAmaliat=N'ویرایش' group by fldShomare) order by id desc
----------------------------آخرین تغییرکننده برگشت از فروش-------------------------------
insert #TempKarbarUpdate(id,fldShomareSanad,fldCodeKarbarUpdate,fldNameKarbarUpdate,fldType)
select distinct tblAmalkardekarbar.id,tblBargashtAzForoosh.fldShomareSanad,fldCodeKarbar,fldNameKarbar,fldNameForm from tblAmalkardekarbar 
inner join tblBargashtAzForoosh on tblBargashtAzForoosh.fldShomareFactorForoosh=tblAmalkardekarbar.fldShomare
where fldNameForm='برگشت از فروش' and NoeAmaliat=N'ویرایش' and tblAmalkardekarbar.id in (select max(tblAmalkardekarbar.id) from tblAmalkardekarbar inner join
tblSodooreSanad on tblSodooreSanad.fldSanad=tblAmalkardekarbar.fldShomare where fldNameForm='برگشت از فروش' and NoeAmaliat=N'ویرایش' group by fldShomare) order by id desc
----------------------------آخرین تغییرکننده خرید-------------------------------
insert #TempKarbarUpdate(id,fldShomareSanad,fldCodeKarbarUpdate,fldNameKarbarUpdate,fldType)
select distinct tblAmalkardekarbar.id,tblKharid.fldShomareSanad,fldCodeKarbar,fldNameKarbar,fldNameForm from tblAmalkardekarbar 
inner join tblKharid on tblKharid.fldShomareFactorKharid=tblAmalkardekarbar.fldShomare
where fldNameForm=N'خرید' and NoeAmaliat=N'ویرایش' and tblAmalkardekarbar.id in (select max(tblAmalkardekarbar.id) from tblAmalkardekarbar inner join
tblSodooreSanad on tblSodooreSanad.fldSanad=tblAmalkardekarbar.fldShomare where fldNameForm=N'خرید' and NoeAmaliat=N'ویرایش' group by fldShomare) order by id desc
----------------------------آخرین تغییرکننده برگشت از خرید-------------------------------
insert #TempKarbarUpdate(id,fldShomareSanad,fldCodeKarbarUpdate,fldNameKarbarUpdate,fldType)
select distinct tblAmalkardekarbar.id,tblBargashtAzKharid.fldShomareSanad,fldCodeKarbar,fldNameKarbar,fldNameForm from tblAmalkardekarbar 
inner join tblBargashtAzKharid on tblBargashtAzKharid.fldShomareFactorKharid=tblAmalkardekarbar.fldShomare
where fldNameForm=N'برگشت از خرید' and NoeAmaliat=N'ویرایش' and tblAmalkardekarbar.id in (select max(tblAmalkardekarbar.id) from tblAmalkardekarbar inner join
tblSodooreSanad on tblSodooreSanad.fldSanad=tblAmalkardekarbar.fldShomare where fldNameForm=N'برگشت از خرید' and NoeAmaliat=N'ویرایش' group by fldShomare) order by id desc

---------------daryaft----------------------
insert into #Temp(fldShomareSanad,fldShomare,fldCodeKarbar,fldType)
 select distinct fldShomareSanad,fldShomareDaryaft,tblAmalkardekarbar.fldCodeKarbar,'دریافت'as fldType from tblDaryaft
 inner join tblAmalkardekarbar on tblAmalkardekarbar.fldShomare=fldShomareDaryaft
 where fldNameForm='دریافت' and NoeAmaliat='جدید'   

---------------pardakht----------------------
insert into #Temp(fldShomareSanad,fldShomare,fldCodeKarbar,fldType)
select fldShomareSanad,fldShomarePardakht,tblAmalkardekarbar.fldCodeKarbar,'پرداخت'as fldType from tblPardakht
 inner join tblAmalkardekarbar on tblAmalkardekarbar.fldShomare=fldShomarePardakht
 where fldNameForm='پرداخت' and NoeAmaliat='جدید'   

----------------foroosh----------------------
insert into #Temp(fldShomareSanad,fldShomare,fldCodeKarbar,fldType)
	select fldShomareSanad,fldShomareFactorForoosh,tblAmalkardekarbar.fldCodeKarbar,'فروش' as fldType from tblForoosh inner join
  tblAmalkardekarbar on tblAmalkardekarbar.fldShomare=tblForoosh.fldShomareFactorForoosh 
  where tblAmalkardekarbar.fldNameForm='فروش' and NoeAmaliat=N'جدید'   


----------------barghashtAzforoosh----------------------

insert into #Temp(fldShomareSanad,fldShomare,fldCodeKarbar,fldType)
	select fldShomareSanad,fldShomareFactorForoosh,tblAmalkardekarbar.fldCodeKarbar,'برگشت از فروش' as fldType from tblBargashtAzForoosh inner join
  tblAmalkardekarbar on tblAmalkardekarbar.fldShomare=tblBargashtAzForoosh.fldShomareFactorForoosh 
  where tblAmalkardekarbar.fldNameForm='برگشت از فروش' and NoeAmaliat=N'جدید'   
	
----------------kharid----------------------
insert into #Temp(fldShomareSanad,fldShomare,fldCodeKarbar,fldType)
	select fldShomareSanad,fldShomareFactorKharid,tblAmalkardekarbar.fldCodeKarbar,'خرید' as fldType from tblKharid inner join
  tblAmalkardekarbar on tblAmalkardekarbar.fldShomare=tblKharid.fldShomareFactorKharid 
  where tblAmalkardekarbar.fldNameForm=N'خرید' and NoeAmaliat=N'جدید'   
 ----------------bargashtazkharid----------------------
insert into #Temp(fldShomareSanad,fldShomare,fldCodeKarbar,fldType)
	select fldShomareSanad,fldShomareFactorKharid,tblAmalkardekarbar.fldCodeKarbar,'برگشت از خرید' as fldType from tblBargashtAzKharid inner join
  tblAmalkardekarbar on tblAmalkardekarbar.fldShomare=tblBargashtAzKharid.fldShomareFactorKharid 
  where tblAmalkardekarbar.fldNameForm=N'برگشت از خرید' and NoeAmaliat=N'جدید'   
  ----------------hazine----------------------
insert into #Temp(fldShomareSanad,fldShomare,fldCodeKarbar,fldType)
	select distinct tblSodooreSanad.fldSanad,tblAmalkardekarbar.fldShomare,tblAmalkardekarbar.fldCodeKarbar,'هزینه'as fldType from tblSodooreSanad 
	inner join tblAmalkardekarbar on tblAmalkardekarbar.fldSanad=tblSodooreSanad.fldSanad
	where fldType='hazine' and NoeAmaliat='جدید'   
  ----------------daramad----------------------
insert into #Temp(fldShomareSanad,fldShomare,fldCodeKarbar,fldType)
	select distinct tblSodooreSanad.fldSanad,tblAmalkardekarbar.fldShomare,tblAmalkardekarbar.fldCodeKarbar,'درآمد'as fldType from tblSodooreSanad 
	inner join tblAmalkardekarbar on tblAmalkardekarbar.fldSanad=tblSodooreSanad.fldSanad
	where fldType='daramad' and NoeAmaliat='جدید'   

  ----------------sanad----------------------
insert into #Temp(fldShomareSanad,fldShomare,fldCodeKarbar,fldType)
				select distinct fldShomare,fldShomare,fldCodeKarbar,fldNameForm from tblAmalkardekarbar 
				where fldNameForm='سند'   


---------------------------------------------------
 insert into #TempShow(fldShomareSanad,fldShomare,fldDate,fldCodeKarbar,fldNameKarbar,fldType) 

 select fldShomareSanad,fldShomare,0 as fldDate,fldCodeKarbar,'' as fldNameKarbar,fldType  from #Temp
	where fldShomareSanad between @AzSanad and @TaSanad

-----------------------------------------------------
	update R Set R.fldSharh=(tblSodooreSanad.fldSharh)
								from #TempShow as R 
								inner join tblSodooreSanad on R.fldShomareSanad=tblSodooreSanad.fldSanad	
								where tblSodooreSanad.fldSharh<>''					
	
		update R Set R.fldDate=(tblSodooreSanad.fldDate)
								from #TempShow as R 
								inner join tblSodooreSanad on R.fldShomareSanad=tblSodooreSanad.fldSanad
									
		update R Set R.fldNameKarbar=(tblSecurity.fldUser)
								from #TempShow as R 
								inner join tblSecurity on R.fldCodeKarbar=tblSecurity.fldCode	
										


	update R Set R.fldCodeKarbarUpdate=(#TempKarbarUpdate.fldCodeKarbarUpdate),R.fldNameKarbarUpdate=(#TempKarbarUpdate.fldNameKarbarUpdate)
								from #TempShow as R 
								inner join #TempKarbarUpdate on R.fldShomareSanad=#TempKarbarUpdate.fldShomareSanad	
------------------------------------------------------------								
		if @ListKarbaran <>''
   begin
		 declare @StrDeleteKarbaran varchar(5000)
	     set  @StrDeleteKarbaran='delete from #TempShow 
		 where fldCodeKarbar not in ('+ @ListKarbaran + ')'
		execute(@StrDeleteKarbaran)
	end
				if @ListKarbaranUpdate <>''
   begin
		 declare @StrDeleteKarbaranUpdate varchar(5000)
	     set  @StrDeleteKarbaranUpdate='delete from #TempShow 
		 where fldCodeKarbarUpdate not in ('+ @ListKarbaranUpdate + ') or fldNameKarbarUpdate is null '
		execute(@StrDeleteKarbaranUpdate)
	end	
	--delete from #TempShow where fldNameKarbarUpdate <> N'تبلت' or fldNameKarbarUpdate is null
--------------------------------------------------------------
select  ROW_NUMBER() OVER(ORDER BY fldShomareSanad) as fldRadif,
fldShomareSanad
,fldShomare
,SUBSTRING(CONVERT(nvarchar, fldDate), 1, 4) + '/' +
SUBSTRING(CONVERT(nvarchar, fldDate), 5, 2) + '/' +
SUBSTRING(CONVERT(nvarchar, fldDate), 7, 2) AS fldDate
,fldNameKarbar
,fldType
,fldSharh
,fldNameKarbarUpdate
 from #TempShow


END

GO
/****** Object:  StoredProcedure [dbo].[z176ShowList_ReportEnteghalVajh]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z176ShowList_ReportEnteghalVajh]
--چندزبانه
	@ListKarbaran varchar(5000),
    @ListBankSadere varchar(5000),
	@ListSandooghSadere varchar(5000),
	@ListHesabMaghsad varchar(5000),
	@AzMablagh decimal(18,4),
	@TaMablagh decimal(18,4),
	@AzDate bigint,
	@TaDate bigint
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
 


create table #TempShow(
fldShomareEnteghal bigint,
fldSanad bigint,
fldBankSadere bigint,
fldSandooghSadere bigint,
fldHesabMaghsad bigint,
fldBankSadereShow NVarchar(100) COLLATE Persian_100_CI_AS,
fldSandooghSadereShow NVarchar(100) COLLATE Persian_100_CI_AS,
fldHesabMaghsadShow NVarchar(100) COLLATE Persian_100_CI_AS,
fldDate NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal(18,4),
fldCodeKarbar bigint,
fldNameKarbar NVarchar(100) COLLATE Persian_100_CI_AS,
fldSharh NVarchar(100) COLLATE Persian_100_CI_AS
)


	
	declare @strCodeKoleHesab nvarchar(50)
	execute CodeKolHesab 'bank',@strcodekolehesab output

	declare @strCodeKoleHesabSandough nvarchar(10)
	execute CodeKolHesab 'sandoogh',@strCodeKoleHesabSandough output

	insert into #TempShow(fldShomareEnteghal,fldSanad,fldBankSadere,fldSandooghSadere,fldHesabMaghsad,fldBankSadereShow,fldSandooghSadereShow,fldHesabMaghsadShow,fldDate,fldMablagh,fldCodeKarbar,fldNameKarbar,fldSharh)
	select distinct tblAmalkardekarbar.fldShomare,tblSodooreSanad.fldSanad, fldBankSadere = Case When fldCodeKol = @strCodeKoleHesab THEN fldMoinKamel Else 0 END ,
	CASE WHEN fldCodeKol=@strCodeKoleHesabSandough THEN fldMoinKamel ELSE 0 END as  fldSandooghSadere,
	0 as fldHesabMaghsad,'' as fldBankSadereShow,'' as fldSandooghSadereShow,'' as fldHesabMaghsadShow,
	tblAmalkardekarbar.fldDate,fldBestankar as fldMablagh,fldCodeKarbar,fldNameKarbar,tblSodooreSanad.fldSharh
	from tblSodooreSanad inner join tblAmalkardekarbar on tblAmalkardekarbar.fldSanad=tblSodooreSanad.fldSanad 
	where tblSodooreSanad.fldType='enteghalevajh' and fldBestankar>0 and tblAmalkardekarbar.fldDate between @AzDate and @TaDate and tblAmalkardekarbar.NoeAmaliat='جدید'



			UPDATE       #TempShow
		SET                fldHesabMaghsad = tblSodooreSanad.fldMoinKamel
																   			
		FROM           tblSodooreSanad    INNER JOIN
								#TempShow  ON tblSodooreSanad.fldSanad=#TempShow.fldSanad  
								where fldType='enteghalevajh' and fldBedehkar>0

		declare @strCodeKolBank nvarchar(10)
	execute CodeKolHesab 'bank',@strCodeKolBank output

				
		UPDATE       #TempShow
		SET                fldBankSadereShow = tblKolMoin.fldSharheHesab
																   			
		FROM           #TempShow    INNER JOIN
								tblKolMoin  ON tblKolMoin.fldCodeHesab=#TempShow.fldBankSadere 
		Where fldCodeHesab<>@strCodeKolBank and left(fldCodeHesab,len(@strCodeKolBank))=@strCodeKolBank
		
		declare @strCodeKolSandoogh nvarchar(10)
	execute CodeKolHesab 'sandoogh',@strCodeKolSandoogh output

				
		UPDATE       #TempShow
		SET                fldSandooghSadereShow = tblKolMoin.fldSharheHesab
																   			
		FROM           #TempShow    INNER JOIN
								tblKolMoin  ON tblKolMoin.fldCodeHesab=#TempShow.fldSandooghSadere 
		Where fldCodeHesab<>@strCodeKolSandoogh and left(fldCodeHesab,len(@strCodeKolSandoogh))=@strCodeKolSandoogh



	declare @strCodeBank nvarchar(10),@strCodeSandoogh nvarchar(10)
    execute  CodeKolHesab 'sandoogh', @strCodeSandoogh output
	execute  CodeKolHesab 'bank', @strCodeBank output

	UPDATE       #TempShow
		SET                fldHesabMaghsadShow = tblKolMoin.fldSharheHesab
																   			
		FROM           #TempShow    INNER JOIN
								tblKolMoin  ON tblKolMoin.fldCodeHesab=#TempShow.fldHesabMaghsad 
		Where fldCodeHesab<>@strCodeSandoogh and fldCodeHesab<>@strCodeBank  and (left(fldCodeHesab,len(@strCodeSandoogh))=@strCodeSandoogh or left(fldCodeHesab,len(@strCodeBank))=@strCodeBank)
	





 
		if @ListKarbaran <>''
   begin
		 declare @StrDeleteKarbaran varchar(5000)
	     set  @StrDeleteKarbaran='delete from #TempShow 
		 where '+ @ListKarbaran + ''
		execute(@StrDeleteKarbaran)
	end
		if @ListBankSadere <>''
   begin
		 declare @StrDeleteBankSadere varchar(5000)
	     set  @StrDeleteBankSadere='delete from #TempShow 
		 where '+ @ListBankSadere + ''
		execute(@StrDeleteBankSadere)
	end	
		if @ListSandooghSadere <>''
   begin
		 declare @StrDeleteSandooghSadere varchar(5000)
	     set  @StrDeleteSandooghSadere='delete from #TempShow 
		 where '+ @ListSandooghSadere + ''
		execute(@StrDeleteSandooghSadere)
	end			
		if @ListHesabMaghsad <>''
   begin
		 declare @StrDeleteHesabMaghsad varchar(5000)
	     set  @StrDeleteHesabMaghsad='delete from #TempShow 
		 where '+ @ListHesabMaghsad + ''
		execute(@StrDeleteHesabMaghsad)
	end		

select ROW_NUMBER() OVER(ORDER BY fldShomareEnteghal) as fldRadif,
fldShomareEnteghal
,fldSanad
,fldBankSadereShow
,fldSandooghSadereShow
,fldHesabMaghsadShow
,SUBSTRING(CONVERT(nvarchar,fldDate), 1, 4) + '/' +
SUBSTRING(CONVERT(nvarchar,fldDate), 5, 2) + '/' +
SUBSTRING(CONVERT(nvarchar,fldDate), 7, 2) AS fldDate
,fldMablagh
,fldNameKarbar
,fldSharh
 from #TempShow
 where 
fldMablagh between CONVERT(nvarchar(50),@AzMablagh) and CONVERT(nvarchar(50),@TaMablagh)

END

 

GO
/****** Object:  StoredProcedure [dbo].[z177InsertCheckEbtedaDorehPrdakhti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z177InsertCheckEbtedaDorehPrdakhti]
--چندزبانه
@fldCodeArz int,
@fldNerkheArz decimal(18,4),
@fldCodeSahbeCheck bigint,
@fldShomareCheck bigint,
@fldShomareHesab bigint,
@fldDateSarResid int,
@fldDatePardakht int,
@fldCodeBank int,
@fldShobe nvarchar(50),
@fldMablagh decimal (18, 4),
@strShomareSanad nvarchar output
as 

begin
	begin transaction AddRecord
		begin try
		
	declare 
		@intShomareSanad int,
		@strResult nvarchar(50),
		@strResult2 nvarchar(50),
		@strVaziat nvarchar(50),
		@strType nvarchar(50),
		@strRadif1 nvarchar(50),
		@strRadif2 nvarchar(50),
		@count int,
		@fldDate int,
	    @day nvarchar(50),
		@month nvarchar(50),
		@year nvarchar(50),
		@kolVaMoin nvarchar(100),
		@kol nvarchar(50),
		@moin nvarchar(50),
		@Toolkol int,
		@Toolmoin int,
		@KolCheck nvarchar(50),
		@jameMablaghKol decimal (18, 4)
		--execute CodeMoinHesab 'check',@kolVaMoin
		set @kolVaMoin=(Select  fldCodeMoinCheckPardakhti from tblTanzimat )
		set @Toolkol=(select fldKol from tblPaye)
		set @Toolmoin=(select fldMoin from tblPaye)
		set @kol=(SUBSTRING(@kolVaMoin,1,@Toolkol))
		set @moin=(SUBSTRING(@kolVaMoin,@Toolkol+1,@Toolmoin))
		
		set @KolCheck=(Select  fldKolSarmaye from tblTanzimat )
		
		set @count= (Select COUNT(fldShomareCheck) from tblCheckePardakhti where fldShomarePardakht=0)
		set @day=(SUBSTRING(CONVERT(nvarchar, @fldDatePardakht), 7, 2))
		set @month=(SUBSTRING(CONVERT(nvarchar, @fldDatePardakht), 5, 2))
		set @year=(SUBSTRING(CONVERT(nvarchar, @fldDatePardakht), 1,4))
		set @fldDate=@fldDatePardakht--(CONVERT(int, @year+@month+@day))
	
	
 if @count>0
begin

 set @strVaziat='Last'
end
 else
begin

	set @strVaziat='First'
end


if @strVaziat='First'
	begin
	


		set @intShomareSanad=(select MAX(CONVERT(dec, fldSanad)+1)as fldSanad from tblSodooreSanad)
		
		execute InsertSanadfirstMultiArz  @fldCodeArz,@fldNerkheArz, @kol,@moin,@kolVaMoin,@fldCodeSahbeCheck,NULL,0,@fldMablagh,'1',@fldDate,NULL,@day,@month,@year,'checkebtedaPardakhti',0,@intShomareSanad output;
			   
		 execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @KolCheck,NULL,@KolCheck,NULL,'1',@fldMablagh,'0',@strRadif1,@fldDate,NULL,@day,@month,@year,'checkebtedaPardakhti',0,@intShomareSanad;
		--execute InsertSanadfirstMultiArz  @fldCodeArz,@fldNerkheArz, @KolCheck,NULL,@KolCheck,NULL,NULL, @fldMablagh,0,'1',@fldDate,NULL,@day,@month,@year,'ebteda',0,@intShomareSanad;
		
			insert into tblCheckePardakhti (fldCodeArz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,
									fldDatePardakht,fldCodeSahebeCheck,fldShomarePardakht,fldCodeSahebeHesab,fldVasete,fldSanad,fldRadif)
							 Values(@fldCodeArz,@fldNerkheArz,@fldShomareCheck,@fldShomareHesab,@fldDateSarResid,@fldCodeBank,@fldShobe,@fldMablagh,
									@fldDatePardakht,@fldCodeSahbeCheck,0,NULL,'',@intShomareSanad,'1')
	end

if @strVaziat='Last'
	begin
	 	set @intShomareSanad=(select distinct CONVERT(dec, fldSanad) from tblSodooreSanad where fldType='checkebtedaPardakhti')
	    set @strRadif1=(CONVERT(nvarchar, @count+1))
		set @jameMablaghKol=(select sum(fldMablagh)+@fldMablagh from tblCheckePardakhti where fldShomarePardakht=0)
		
		delete from tblSodooreSanad Where fldSanad=@intShomareSanad and fldBedehkar>0
		execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @kol,@moin,@kolVaMoin,@fldCodeSahbeCheck,'1','0',@fldMablagh,@strRadif1,@fldDate,NULL,@day,@month,@year,'checkebtedaPardakhti',0,@intShomareSanad;
	    set @strRadif2=(CONVERT(nvarchar, @count+2))
		execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, @KolCheck,NULL,@KolCheck,NULL,'1',@jameMablaghKol,'0',@strRadif2,@fldDate,NULL,@day,@month,@year,'checkebtedaPardakhti',0,@intShomareSanad;
	--execute InsertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz, '1','1','1',@fldCodeSahbeCheck,'1',@fldMablagh,0,2,971003,NULL,'03','10','97','checkebteda',0,'36';
		
			insert into tblCheckePardakhti (fldCodeArz,fldNerkheArz,fldShomareCheck,fldShomareHesab,fldDateSarResid,fldCodeBank,fldShobe,fldMablagh,
									fldDatePardakht,fldCodeSahebeCheck,fldShomarePardakht,fldCodeSahebeHesab,fldVasete,fldSanad,fldRadif)
							 Values(@fldCodeArz,@fldNerkheArz,@fldShomareCheck,@fldShomareHesab,@fldDateSarResid,@fldCodeBank,@fldShobe,@fldMablagh,
									@fldDatePardakht,@fldCodeSahbeCheck,0,NULL,'',@intShomareSanad,@strRadif1)
	end

		--select * from tblSodooreSanad
	--select * from tblCheckePardakhti
	--delete tblSodooreSanad where fldSanad=83
	update tblSodooreSanad set fldDate= @fldDate,fldDay=@day,fldMonth=@month,fldYear=@year Where fldSanad=@intShomareSanad 	
		
		commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				set @strResult2='error'
				rollback transaction AddRecord;
			end
		end catch
		end

GO
/****** Object:  StoredProcedure [dbo].[z178ShowCheakEbtedaDorehPardakhti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[z178ShowCheakEbtedaDorehPardakhti]

as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;
  create table #TempListCodeBank(
fldCodeBank bigint,
fldNameBank nvarchar(50)
)


	
			 	declare @strCodeKoleHesab nvarchar(10)
				execute CodeKolHesab 'bank',@strcodekolehesab output

		insert into #TempListCodeBank(fldNameBank,fldCodeBank)
	select [fldSharheHesab] as fldNameBank,[fldCodeHesab] from tblKolMoin
		where  fldCodeHesab like @strCodeKoleHesab + '%' and len(fldCodeHesab)>len(@strCodeKoleHesab)
		order by fldCodeHesab

	select dbo.tblCheckePardakhti.ID,fldSanad,fldCodeSahebeCheck,dbo.tblTafzili.fldSharheTafzili,fldShomareCheck,dbo.tblCheckePardakhti.fldShomareHesab,
		                     SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 1, 4) + '/' +
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, tblCheckePardakhti.fldDateSarResid), 7, 2) AS fldDateSarResid,
			#TempListCodeBank.fldNameBank,#TempListCodeBank.fldCodeBank,fldShobe,fldMablagh
		from dbo.tblCheckePardakhti inner join
		#TempListCodeBank on dbo.tblCheckePardakhti.fldCodeBank=#TempListCodeBank.fldCodeBank inner join
		dbo.tblTafzili on dbo.tblCheckePardakhti.fldCodeSahebeCheck=dbo.tblTafzili.fldCodeTafzili
		where dbo.tblCheckePardakhti.fldShomarePardakht=0
END

 

GO
/****** Object:  StoredProcedure [dbo].[z179UpdateCheakEbtedaDorehPardakhti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
CREATE proc [dbo].[z179UpdateCheakEbtedaDorehPardakhti]
--چندزبانه
@fldCodeArz int,
@fldNerkheArz decimal(18,4),
@id bigint,
@fldCodeSahbeCheck bigint,
@fldShomareCheck bigint,
@fldShomareHesab bigint,
@fldDateSarResid int,
@fldDatePardakht int,
@fldCodeBank int,
@fldShobe nvarchar(50),
@fldMablagh decimal (18, 4)
as
begin

	declare @day nvarchar(10),@month nvarchar(10),@year nvarchar(10),@fldDate nvarchar(10)
	declare @strRadif nvarchar(50),
			@jamMablagh decimal (18, 4),
			@strResult nvarchar(50),
			@strResult2 nvarchar(50),
			@intShomareSanad int,
			@count int

				set @strRadif=(select fldRadif from tblCheckePardakhti where ID=@id)
	set @intShomareSanad=(select fldSanad from tblCheckePardakhti where ID=@id)
			
			update tblCheckePardakhti 
			set	
				fldCodeArz=@fldCodeArz,
				fldNerkheArz=@fldNerkheArz,
				fldCodeSahebeCheck=@fldCodeSahbeCheck,
				fldShomareCheck =@fldShomareCheck,
				fldShomareHesab =@fldShomareHesab,
				fldDateSarResid =@fldDateSarResid,
				fldDatePardakht =@fldDatePardakht,
				fldCodeBank =@fldCodeBank,
				fldShobe =@fldShobe,
				fldMablagh =@fldMablagh,
				fldShomarePardakht=0
			where ID =@id 

			set @day=(SUBSTRING(CONVERT(nvarchar, @fldDatePardakht), 7, 2))
			set @month=(SUBSTRING(CONVERT(nvarchar, @fldDatePardakht), 5, 2))
			set @year=(SUBSTRING(CONVERT(nvarchar, @fldDatePardakht), 1, 4))
			set @fldDate=@fldDatePardakht--(CONVERT(int, @year+@month+@day))

			set @jamMablagh=(select sum(fldMablagh) from tblCheckePardakhti where fldShomarePardakht=0)
			update tblSodooreSanad set fldBedehkar=@jamMablagh , fldRadif=@strRadif,fldDate= @fldDate,fldDay=@day,fldMonth=@month,fldYear=@year Where fldSanad=@intShomareSanad and fldBedehkar>0 

			update tblSodooreSanad set fldCodeArz=@fldCodeArz,fldNerkheArz=@fldNerkheArz, fldCodeTafzili=@fldCodeSahbeCheck, fldBestankar=@fldMablagh ,fldDate= @fldDate,fldDay=@day,fldMonth=@month,fldYear=@year Where fldSanad=@intShomareSanad and fldRadif=@strRadif and fldBestankar>0
			
			
			

end 

GO
/****** Object:  StoredProcedure [dbo].[z180DeleteCheakEbtedaDorehPardakhti]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[z180DeleteCheakEbtedaDorehPardakhti]
--چندزبانه
@id bigint

as
begin
	begin transaction AddRecord
		begin try

	declare @strRadif nvarchar(50),
			@jamMablagh decimal (18, 4),
			@strResult nvarchar(50),
			@strResult2 nvarchar(50),
			@intShomareSanad int,
			@count int

	set @strRadif=(select fldRadif from tblCheckePardakhti where id=@id)
	set @intShomareSanad=(select fldSanad from tblCheckePardakhti where id=@id)


	delete from tblSodooreSanad where fldType='checkebtedaPardakhti' and fldRadif=@strRadif

	--delete from tblSodooreSanad Where fldSanad=@intShomareSanad and fldBestankar>0
	delete from tblCheckePardakhti
			 where id=@id
	set @count=(Select COUNT(fldShomareCheck) from tblCheckePardakhti where fldShomarePardakht=0)
	if @count>0
	begin
	set @jamMablagh=(select sum(fldMablagh) from tblCheckePardakhti where fldShomarePardakht=0)
	update tblSodooreSanad set fldBedehkar=@jamMablagh , fldRadif=@strRadif Where fldSanad=@intShomareSanad and fldBedehkar>0
	end
	else
	begin
	delete from tblSodooreSanad Where fldSanad=@intShomareSanad and fldBedehkar>0
	end
		commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult='error'
				set @strResult2='error'
				rollback transaction AddRecord;
			end
		end catch


end 

GO
/****** Object:  StoredProcedure [dbo].[z181ShowCheakPassNashode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE proc [dbo].[z181ShowCheakPassNashode]
--چندزبانه
@Id bigint,
@ShomareBazPas bigint
as

	
IF 1=0 BEGIN
 SET FMTONLY OFF
END
begin
	
	SET NOCOUNT ON;

create table #TempShow(
id bigint,
fldRadif bigint,
fldShomareCheck bigint,
fldShomareHesab bigint,
fldCodeSahebeHesab bigint,
fldCodeSahebeCheck bigint,
fldNameSahebeCheck NVarchar(100) COLLATE Persian_100_CI_AS,
fldDateSarResid NVarchar(100) COLLATE Persian_100_CI_AS,
fldCodeBank bigint,
fldNameBank NVarchar(100) COLLATE Persian_100_CI_AS,
fldShobe NVarchar(100) COLLATE Persian_100_CI_AS,
fldMablagh decimal (18, 4),
fldShomarePass bigint,
fldSanad bigint,
fldShomareBazPas bigint,
fldSanadBazPas bigint
)

 create table #TempListCodeBank(
fldCodeBank bigint,
fldNameBank NVarchar(100) COLLATE Persian_100_CI_AS

)

delete #TempShow


	insert into #TempListCodeBank(fldNameBank,fldCodeBank)	
	 execute ShowBankeMa
		
	if (@Id>0)
	begin
	if (@ShomareBazPas=0)
	begin
		insert into #TempShow(id,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck
	,fldNameSahebeCheck,fldDateSarResid,fldCodeBank,fldNameBank
	,fldShobe,fldMablagh,fldShomarePass,fldSanad,fldShomareBazPas,fldSanadBazPas)
	
	select  tblCheckePardakhti.Id,fldShomareCheck,tblCheckePardakhti.fldShomareHesab
			,fldCodeSahebeCheck,tblTafzili.fldSharheTafzili as fldNameSahebeCheck,
			                 SUBSTRING(CONVERT(nvarchar,fldDateSarResid), 1, 4) + '/' +
						     SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 7, 2) AS fldDateSarResid
			,tblCheckePardakhti.fldCodeBank,#TempListCodeBank.fldNameBank as fldNameBank
			,fldShobe,fldMablagh,fldShomarePass,fldSanad,fldShomareBazPas,fldSanadBazPas
	from tblCheckePardakhti
	inner join tblTafzili on tblCheckePardakhti.fldCodeSahebeCheck=tblTafzili.fldCodeTafzili 
	inner join #TempListCodeBank on tblCheckePardakhti.fldCodeBank=#TempListCodeBank.fldCodeBank
	where tblCheckePardakhti.ID=@Id and fldShomarePass is null and fldShomareBazPas is null

	
	end
	else
	begin
	insert into #TempShow(id,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck
	,fldNameSahebeCheck,fldDateSarResid,fldCodeBank,fldNameBank
	,fldShobe,fldMablagh,fldShomarePass,fldSanad,fldShomareBazPas,fldSanadBazPas)
	
	select  tblCheckePardakhti.Id,fldShomareCheck,tblCheckePardakhti.fldShomareHesab
			,fldCodeSahebeCheck,tblTafzili.fldSharheTafzili as fldNameSahebeCheck,
			                 SUBSTRING(CONVERT(nvarchar,fldDateSarResid), 1, 4) + '/' +
						     SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 7, 2) AS fldDateSarResid
			,tblCheckePardakhti.fldCodeBank,#TempListCodeBank.fldNameBank as fldNameBank
			,fldShobe,fldMablagh,fldShomarePass,fldSanad,fldShomareBazPas,fldSanadBazPas
	from tblCheckePardakhti
	inner join tblTafzili on tblCheckePardakhti.fldCodeSahebeCheck=tblTafzili.fldCodeTafzili 
	inner join #TempListCodeBank on tblCheckePardakhti.fldCodeBank=#TempListCodeBank.fldCodeBank
	where  fldShomareBazPas = @ShomareBazPas and tblCheckePardakhti.ID=@Id

	end
	end
	else
	begin		 
if (@ShomareBazPas=0)
	begin
	insert into #TempShow(id,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck
	,fldNameSahebeCheck,fldDateSarResid,fldCodeBank,fldNameBank
	,fldShobe,fldMablagh,fldShomarePass,fldSanad,fldShomareBazPas,fldSanadBazPas)
	
	select  tblCheckePardakhti.Id,fldShomareCheck,tblCheckePardakhti.fldShomareHesab
			,fldCodeSahebeCheck,tblTafzili.fldSharheTafzili as fldNameSahebeCheck,
			                 SUBSTRING(CONVERT(nvarchar,fldDateSarResid), 1, 4) + '/' +
						     SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 7, 2) AS fldDateSarResid
			,tblCheckePardakhti.fldCodeBank,#TempListCodeBank.fldNameBank as fldNameBank
			,fldShobe,fldMablagh,fldShomarePass,fldSanad,fldShomareBazPas,fldSanadBazPas
	from tblCheckePardakhti
	inner join tblTafzili on tblCheckePardakhti.fldCodeSahebeCheck=tblTafzili.fldCodeTafzili 
	inner join #TempListCodeBank on tblCheckePardakhti.fldCodeBank=#TempListCodeBank.fldCodeBank
	where fldShomarePass is null and fldShomareBazPas is null
	end
	else
		begin
	insert into #TempShow(id,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck
	,fldNameSahebeCheck,fldDateSarResid,fldCodeBank,fldNameBank
	,fldShobe,fldMablagh,fldShomarePass,fldSanad,fldShomareBazPas,fldSanadBazPas)
	
select  tblCheckePardakhti.Id,fldShomareCheck,tblCheckePardakhti.fldShomareHesab
			,fldCodeSahebeCheck,tblTafzili.fldSharheTafzili as fldNameSahebeCheck,
			                 SUBSTRING(CONVERT(nvarchar,fldDateSarResid), 1, 4) + '/' +
						     SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 5, 2) + '/' + 
							 SUBSTRING(CONVERT(nvarchar, fldDateSarResid), 7, 2) AS fldDateSarResid
			,tblCheckePardakhti.fldCodeBank,#TempListCodeBank.fldNameBank as fldNameBank
			,fldShobe,fldMablagh,fldShomarePass,fldSanad,fldShomareBazPas,fldSanadBazPas
	from tblCheckePardakhti
	inner join tblTafzili on tblCheckePardakhti.fldCodeSahebeCheck=tblTafzili.fldCodeTafzili 
	inner join #TempListCodeBank on tblCheckePardakhti.fldCodeBank=#TempListCodeBank.fldCodeBank
	where fldShomareBazPas=@ShomareBazPas and fldShomarePass is null and fldShomareBazPas is not null
	end
end
	select ROW_NUMBER() OVER(ORDER BY Id) as fldRadif,id,fldShomareCheck,fldShomareHesab,fldCodeSahebeCheck,fldNameSahebeCheck,fldDateSarResid,fldCodeBank,fldNameBank,fldShobe,fldMablagh,fldShomarePass,fldSanad,fldShomareBazPas,fldSanadBazPas from #TempShow
end 
GO
/****** Object:  StoredProcedure [dbo].[z182UpdateCheakBazPas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[z182UpdateCheakBazPas]
	@fldCodeArz int,
	@fldNerkheArz decimal(18,4),
	@language nvarchar(10),
	@Id bigint,
	@ShomareBazPas bigint,
	@SanadBazPas bigint,
	@fldSumMablagh decimal,
	@Date bigint,
	@Sharh Nvarchar(200),
	@type nvarchar(50),
	@vaziat nvarchar(50),
	@tblTemp dtCheckBazPasShode  readonly,
	@strResult1 nvarchar(30) output,
	@strResult2 bigint output
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END

 
begin

begin transaction AddRecord
		begin try



	if(@type='Add')
	begin


declare @MaxShomareBazPas bigint
declare @MaxSanadBazPas bigint

Set @MaxShomareBazPas=(select Max(fldShomareBazPas) from tblCheckePardakhti)
Set @MaxSanadBazPas=(select Max(fldSanadBazPas) from tblCheckePardakhti)
	if isnumeric(@MaxShomareBazPas)=0
	set @MaxShomareBazPas=1
	else
	set @MaxShomareBazPas=@MaxShomareBazPas+1

	if (@MaxShomareBazPas<>@ShomareBazPas)
	set @MaxSanadBazPas=@ShomareBazPas

	if isnumeric(@MaxSanadBazPas)=0
	set @MaxSanadBazPas=1
	else
	set @MaxSanadBazPas=@MaxSanadBazPas+1

	if (@MaxSanadBazPas<>@SanadBazPas)
	set @MaxSanadBazPas=@SanadBazPas

	--Declare Info
	DECLARE @counter bigint = 1,
			@count bigint=(Select COUNT(id) from @tblTemp),
			@fldId bigint,
			@ShomareSanad bigint,
			@fldCodeTafzili bigint,
			@fldSarheTafzili nvarchar(50),
			@fldMablagh decimal,
			@fldShomareCheck bigint,
			@lngDate bigint,
			@day nvarchar(50), 
			@month nvarchar(50),
			@year nvarchar(50) 

	WHILE @counter <= @count
	BEGIN
	--Set Info
	set @fldId=(select id from @tblTemp where fldRadif=@counter)
	set @fldCodeTafzili=(select fldCodeSahebeCheck from @tblTemp where fldRadif=@counter)
	set @fldMablagh=(select fldMablagh from @tblTemp where fldRadif=@counter)
	set @fldShomareCheck=(select fldShomareCheck from @tblTemp where fldRadif=@counter)
	--set @lngDate=(select convert(bigint,SUBSTRING(replace(@Date,'/',''),3,6)))
	set @lngDate=@Date
	Set @year=SUBSTRING(CONVERT(nvarchar,@Date), 1, 4)
	Set @month=SUBSTRING(CONVERT(nvarchar,@Date), 5, 2)
	Set @day=SUBSTRING(CONVERT(nvarchar,@Date), 7, 2)
		
		--update BazPas
		if(@vaziat='insert')
		begin
	Update tblCheckePardakhti Set fldShomareBazPas=@MaxShomareBazPas,fldDateBazPas=@Date,fldSharhBazPas=@Sharh,fldSanadBazPas=@MaxSanadBazPas
	where ID=@fldId
		end
		if(@vaziat='update')
		begin
	Update tblCheckePardakhti Set fldShomareBazPas=@ShomareBazPas,fldDateBazPas=@Date,fldSharhBazPas=@Sharh,fldSanadBazPas=@SanadBazPas
	where ID=@fldId
		end
		

		SET @counter = @counter + 1;
	END

	declare @CountCheck bigint,
			@fldMablaghCheck decimal,
			@fldSharh nvarchar(100),
			@TooleKol bigint,
			@Toolemoin bigint,

			@CodeMoin1 nvarchar(100),
			@CodeKolkham1 nvarchar(10),
			@CodeMoinKham1 nvarchar(10),

			@CodeMoin2 nvarchar(100),
			@CodeKolkham2 nvarchar(10),
			@CodeMoinKham2 nvarchar(10)



execute  ToolePaye 'kol',@TooleKol output
execute  ToolePaye 'moin',@Toolemoin output
execute codemoinhesab 'checkpardakhti' ,@CodeMoin1 output
set @CodeKolkham1 =substring(@CodeMoin1, 1, @TooleKol)
set @CodeMoinKham1 =substring(@CodeMoin1,@TooleKol+1,@Toolemoin)

execute  ToolePaye 'kol',@TooleKol output
execute  ToolePaye 'moin',@Toolemoin output
execute codemoinhesab 'hesabhayepardakhtani' ,@CodeMoin2 output
set @CodeKolkham2 =substring(@CodeMoin2, 1, @TooleKol)
set @CodeMoinKham2 =substring(@CodeMoin2,@TooleKol+1,@Toolemoin)

--select @CodeKolkham1
--select @CodeMoinKham1
--select @CodeMoin1

--select @CodeKolkham2
--select @CodeMoinKham2
--select @CodeMoin2

	Set @CountCheck=(Select Count(ID) from tblSodooreSanad where fldSanad=@SanadBazPas)
	Set @fldSharh=(Select * from dbo.translatefunction(@LANGUAGE,18291)) + ' '+ Convert(varchar, @fldShomareCheck)
	--check Sanad
		if(@CountCheck=0)
		begin
			--Insert Sanad	
	execute insertSanadFirstMultiArz  @fldCodeArz,@fldNerkheArz ,  @CodeKolkham1,@CodeMoinKham1,@CodeMoin1,@fldCodeTafzili,'1',@fldSumMablagh,0,1,@lngDate,@fldSharh,@day,@month,@year,'bazpascheckekhodeman',Null,@ShomareSanad output;
	
	execute insertSanadLastMultiArz  @fldCodeArz,@fldNerkheArz , @CodeKolkham2,@CodeMoinKham2,@CodeMoin2,@fldCodeTafzili,'1',0,@fldSumMablagh,2,@lngDate,@fldSharh,@day,@month,@year,'bazpascheckekhodeman',Null,@ShomareSanad;
		end
		else
		begin 
			--Update Sanad
			Set @fldMablaghCheck=(Select fldBedehkar from tblSodooreSanad where fldSanad=@SanadBazPas and fldBestankar=0)
			update tblSodooreSanad Set fldBedehkar=@fldMablaghCheck+@fldSumMablagh where fldSanad=@SanadBazPas and fldBestankar=0
			update tblSodooreSanad Set fldBestankar=@fldMablaghCheck+@fldSumMablagh where fldSanad=@SanadBazPas and fldBedehkar=0
		end

	end
	else if(@type='Remove')
	begin
	--Declare Info
	DECLARE @counter2 bigint = 1,
			@count2 bigint=(Select COUNT(id) from @tblTemp),
			@fldId2 bigint,
			@ShomareSanad2 bigint,
			@fldCodeTafzili2 bigint,
			@fldSarheTafzili2 nvarchar(50),
			@fldMablagh2 decimal,
			@lngDate2 bigint,
			@day2 nvarchar(50), 
			@month2 nvarchar(50),
			@year2 nvarchar(50) 

	WHILE @counter2 <= @count2
	BEGIN
	--Set Info
	set @fldId=(select id from @tblTemp where fldRadif=@counter2)
	set @fldCodeTafzili=(select fldCodeSahebeCheck from @tblTemp where fldRadif=@counter2)
	set @fldMablagh=(select fldMablagh from @tblTemp where fldRadif=@counter2)
	set @lngDate=@Date
	Set @year=SUBSTRING(CONVERT(nvarchar,@Date), 1, 4)
	Set @month=SUBSTRING(CONVERT(nvarchar,@Date), 5, 2)
	Set @day=SUBSTRING(CONVERT(nvarchar,@Date), 7, 2)
		
		--update BazPas
		
	Update tblCheckePardakhti Set fldShomareBazPas=NULL,fldDateBazPas=NULL,fldSharhBazPas=NULL,fldSanadBazPas=NULL
	where ID=@fldId

	
		

		SET @counter2 = @counter2 + 1;
	END
	
			--UPDATE Sanad	
			if (@fldSumMablagh=0)
			begin
				Update tblCheckePardakhti Set fldShomareBazPas=NULL,fldDateBazPas=NULL,fldSharhBazPas=NULL,fldSanadBazPas=NULL
				where fldShomareBazPas=@ShomareBazPas
			delete tblSodooreSanad where fldSanad=@SanadBazPas
			end
			else
			begin
			update tblSodooreSanad set fldBedehkar=@fldSumMablagh where fldBestankar=0 and fldSanad=@SanadBazPas
			update tblSodooreSanad set fldBestankar=@fldSumMablagh where fldBedehkar=0 and fldSanad=@SanadBazPas
			end


	end

	commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult1='error'
			    set @strResult2=(SELECT Error_Line() AS ErrorNumber)
				rollback transaction AddRecord;
			end
		end catch

END

 

GO
/****** Object:  StoredProcedure [dbo].[z183DeleteCheakBazPas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[z183DeleteCheakBazPas]
	@Id bigint,
	@ShomareBazPas bigint,
	@SanadBazPas bigint,
	@strResult1 nvarchar(30) output,
	@strResult2 bigint output
as
   
	
IF 1=0 BEGIN
 SET FMTONLY OFF
END

 
begin

begin transaction AddRecord
		begin try

	
	
	Update tblCheckePardakhti Set fldShomareBazPas=NULL,fldDateBazPas=NULL,fldSharhBazPas=NULL,fldSanadBazPas=NULL
	where fldShomareBazPas=@ShomareBazPas

	delete tblSodooreSanad where fldSanad=@SanadBazPas 

	

	commit transaction AddRecord;
		
		end try
		begin catch
			if @@TranCount>0
			begin
				set @strResult1='error'
			    set @strResult2=(SELECT Error_Line() AS ErrorNumber)
				rollback transaction AddRecord;
			end
		end catch

END

 

GO
/****** Object:  StoredProcedure [dbo].[z184ShowListeAghsat]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[z184ShowListeAghsat] 
	@CodeTafzili nvarchar(10),
	@ShomareVam bigint,
	@DateFrom bigint,
	@DateTo bigint
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN


CREATE TABLE #tmpDaryaftTasfie2(
	[fldRadif] [bigint]  null,
	[fldShomareVam] [bigint]  null,
	[fldShomareGhest] [bigint]  null,
	[SumDaryafti] [bigint] null,
	[fldCodeTafzili] [nvarchar](10) COLLATE Persian_100_CI_AS,
	[fldMablagheMande][bigint],	
	[fldMablagheDaryaftiJari] [bigint],
	[fldDateSarResid][bigint],
	[ShomareDaryaft][bigint]

	)

		
		if (@ShomareVam=0)
		begin
			insert into #tmpDaryaftTasfie2
		(fldRadif,fldShomareVam,fldShomareGhest,SumDaryafti,fldCodeTafzili,fldMablagheMande,fldDateSarResid)
		Select ROW_NUMBER() OVER(ORDER BY tblDaftarcheAghsat.Id) as fldRadif,  tblDaftarcheAghsat.fldShomareVam,fldRadif,0,fldCodeTafzili, tblDaftarcheAghsat.fldMablaghGhest,fldDateSarResid
			from tblDaftarcheAghsat
			inner join tblVamInfo
			on tblDaftarcheAghsat.fldShomareVam =tblVamInfo.fldShomareVam
			where fldCodeTafzili = @CodeTafzili 
		end
		else
		begin
			insert into #tmpDaryaftTasfie2
		(fldRadif,fldShomareVam,fldShomareGhest,SumDaryafti,fldCodeTafzili,fldMablagheMande,fldDateSarResid)
		Select ROW_NUMBER() OVER(ORDER BY tblDaftarcheAghsat.Id) as fldRadif, tblDaftarcheAghsat.fldShomareVam,fldRadif,0,fldCodeTafzili, tblDaftarcheAghsat.fldMablaghGhest,fldDateSarResid
			from tblDaftarcheAghsat
			inner join tblVamInfo
			on tblDaftarcheAghsat.fldShomareVam =tblVamInfo.fldShomareVam
			where fldCodeTafzili = @CodeTafzili and tblDaftarcheAghsat.fldShomareVam=@ShomareVam
		end

	update #tmpDaryaftTasfie2
		set SumDaryafti=tbl1.sumDaryafti from		
		(Select fldShomareVam,fldShomareGhest, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfieVam group by fldShomareVam,fldShomareGhest ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie2.fldShomareFactor) as tbl1
		where #tmpDaryaftTasfie2.fldShomareVam=tbl1.fldShomareVam and  #tmpDaryaftTasfie2.fldShomareGhest=tbl1.fldShomareGhest


		update #tmpDaryaftTasfie2 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
			(Select fldShomareVam,fldShomareGhest, fldMablagh from tblRizeTasfieVam) as tbl2
			where tbl2.fldShomareVam=#tmpDaryaftTasfie2.fldShomareVam and tbl2.fldShomareGhest=#tmpDaryaftTasfie2.fldShomareGhest
	

	update #tmpDaryaftTasfie2 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null

	Select fldRadif, fldShomareVam,fldShomareGhest,SumDaryafti,fldMablagheMande,fldMablagheDaryaftiJari,
		substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid
		from #tmpDaryaftTasfie2
			where fldMablagheMande>SumDaryafti or fldMablagheDaryaftiJari>0 
		order by fldRadif

END
GO
/****** Object:  StoredProcedure [dbo].[z185ShowListeJameAghsat]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z185ShowListeJameAghsat] 
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
CREATE TABLE #tmpDaryaftTasfie2(
	[fldRadif] [bigint]  null,
	[fldShomareVam] [bigint]  null,
	[fldShomareGhest] [bigint]  null,
	[SumDaryafti] [bigint] null,
	[fldCodeGroup] [bigint] null,
	[fldNameGroup] [nvarchar](80) COLLATE Persian_100_CI_AS,
	[fldCodeTafzili] [bigint] null,
	[fldSharhTafzili] [nvarchar](200) COLLATE Persian_100_CI_AS,
	[fldDate][bigint],
	[fldMablagheMande][bigint],	
	[fldMablagheDaryaftiJari] [bigint],
	[fldDateSarResid][bigint],
	[ShomareDaryaft][bigint]

	)

	insert into #tmpDaryaftTasfie2
	(fldShomareVam,fldShomareGhest,SumDaryafti,fldCodeGroup,fldCodeTafzili,fldDate,fldMablagheMande,fldDateSarResid)
	Select   tblDaftarcheAghsat.fldShomareVam,fldRadif,0 as SumDaryafti,0 as fldCodeGroup,fldCodeTafzili, tblVamInfo.fldDate,tblDaftarcheAghsat.fldMablaghGhest,fldDateSarResid
	from tblDaftarcheAghsat
	inner join tblVamInfo
	on tblDaftarcheAghsat.fldShomareVam =tblVamInfo.fldShomareVam
		
	update #tmpDaryaftTasfie2
	set SumDaryafti=tbl1.sumDaryafti from		
	(Select fldShomareVam,fldShomareGhest, isnull(Sum(fldMablagh),0) as sumDaryafti from tblRizeTasfieVam group by fldShomareVam,fldShomareGhest ) as tbl1 -- Where fldShomareFactor=#tmpDaryaftTasfie2.fldShomareFactor) as tbl1
	where #tmpDaryaftTasfie2.fldShomareVam=tbl1.fldShomareVam and  #tmpDaryaftTasfie2.fldShomareGhest=tbl1.fldShomareGhest

	update #tmpDaryaftTasfie2 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
	(Select fldShomareVam,fldShomareGhest, fldMablagh from tblRizeTasfieVam) as tbl2
	where tbl2.fldShomareVam=#tmpDaryaftTasfie2.fldShomareVam and tbl2.fldShomareGhest=#tmpDaryaftTasfie2.fldShomareGhest
	
	update #tmpDaryaftTasfie2 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null

	update #tmpDaryaftTasfie2 set fldCodeGroup=tblTafzili.fldCodeGroup from #tmpDaryaftTasfie2
	inner join tblTafzili on #tmpDaryaftTasfie2.fldCodeTafzili=tblTafzili.fldCodeTafzili

	update #tmpDaryaftTasfie2 set fldNameGroup=tblGroupAshkhas.fldGroupName from #tmpDaryaftTasfie2
	inner join tblGroupAshkhas on #tmpDaryaftTasfie2.fldCodeGroup=tblGroupAshkhas.fldGroupId

	update #tmpDaryaftTasfie2 set fldSharhTafzili=tblTafzili.fldSharheTafzili from #tmpDaryaftTasfie2
	inner join tblTafzili on #tmpDaryaftTasfie2.fldCodeTafzili=tblTafzili.fldCodeTafzili

	if @ListAshkhas <>''
	   begin
			 declare @StrDeleteAshkhas varchar(5000)
			 set  @StrDeleteAshkhas='delete from #tmpDaryaftTasfie2 
			 where '+ @ListAshkhas + ''
			execute(@StrDeleteAshkhas)
		end


		if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #tmpDaryaftTasfie2 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end

	Select  fldShomareVam,fldShomareGhest,SumDaryafti,fldCodeGroup,fldNameGroup,fldCodeTafzili,fldSharhTafzili,fldMablagheMande,fldMablagheDaryaftiJari,
	substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate,
		substring(convert(nvarchar(10),fldDateSarResid),0,5) +'/' +substring(convert(nvarchar(10),fldDateSarResid),5,2) + '/' + substring(convert(nvarchar(10),fldDateSarResid),7,2) as fldDateSarResid
		from #tmpDaryaftTasfie2
			where fldMablagheMande>SumDaryafti or fldMablagheDaryaftiJari>0 
		group by fldShomareVam,fldShomareGhest,SumDaryafti,fldCodeGroup,fldNameGroup,fldCodeTafzili,fldSharhTafzili,fldMablagheMande,fldMablagheDaryaftiJari,fldDate,fldDateSarResid

END
GO
/****** Object:  StoredProcedure [dbo].[z186ShowListeAghsatMoavaghe]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
create PROCEDURE [dbo].[z186ShowListeAghsatMoavaghe] 
	@ListAshkhas varchar(5000)=null,
	@ListGroupAshkhas varchar(5000)=null,
	@TaDateSarResid bigint
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN

CREATE TABLE #tmpDaryaftTasfie1(
	[fldRadif] [bigint]  null,
	[fldShomareVam] [bigint]  null,
	[fldTedadAghsat] [bigint]  null,
	[fldCodeGroup] [bigint] null,
	[fldNameGroup] [nvarchar](200) COLLATE Persian_100_CI_AS,
	[fldCodeTafzili] [bigint] null,
	[fldSharhTafzili] [nvarchar](80) COLLATE Persian_100_CI_AS,
	[fldDate][nvarchar](18) COLLATE Persian_100_CI_AS,
	[fldMablagheGhest][decimal],
	[fldMablagheMande][decimal],		
	[fldMablagheDaryaftiJari] [decimal],
	[fldDateSarResid] [bigint] null,
	[fldShomareGhest] [bigint] null
	)

	CREATE TABLE #tmpDaryaftTasfie2(
	[fldRadif] [bigint]  null,
	[fldShomareVam] [bigint]  null,
	[fldTedadAghsat] [bigint]  null,
	[fldCodeGroup] [bigint] null,
	[fldNameGroup] [nvarchar](200) COLLATE Persian_100_CI_AS,
	[fldCodeTafzili] [bigint] null,
	[fldSharhTafzili] [nvarchar](80) COLLATE Persian_100_CI_AS,
	[fldDate][bigint],
	[fldMablagheGhest][decimal],
	[fldMablagheMande][decimal],		
	[fldMablagheDaryaftiJari] [decimal],
	[fldDateSarResid] [bigint] null,
	[fldShomareGhest] [bigint] null
	)

	CREATE TABLE #tmpTedadAghsat(
	[fldShomareVam] [bigint]  null,
	[fldTedadAghsat] [bigint]  null
	)

	insert into #tmpDaryaftTasfie2
	(fldShomareVam,fldTedadAghsat ,fldCodeGroup,fldCodeTafzili,fldDate,fldMablagheGhest,fldMablagheMande,fldMablagheDaryaftiJari,fldDateSarResid,fldShomareGhest)
	Select tblDaftarcheAghsat.fldShomareVam,0 as fldTedadAghsat,0 as fldCodeGroup,fldCodeTafzili,fldDate,tblDaftarcheAghsat.fldMablaghGhest,0 as fldMablagheMande,0 as fldMablagheDaryaftiJari,tblDaftarcheAghsat.fldDateSarResid,tblDaftarcheAghsat.fldRadif as fldShomareGhest
	from tblDaftarcheAghsat
	inner join tblVamInfo
	on tblDaftarcheAghsat.fldShomareVam =tblVamInfo.fldShomareVam 
	where fldDateSarResid<=@TaDateSarResid
		

	
	update #tmpDaryaftTasfie2 set fldMablagheDaryaftiJari=tbl2.fldMablagh from
	(Select fldShomareVam,fldShomareGhest,sum(fldMablagh) as fldMablagh from tblRizeTasfieVam group by fldShomareVam,fldShomareGhest ) as tbl2
	where tbl2.fldShomareVam=#tmpDaryaftTasfie2.fldShomareVam and #tmpDaryaftTasfie2.fldShomareGhest=tbl2.fldShomareGhest

	--select * from #tmpDaryaftTasfie2
	
	--update #tmpDaryaftTasfie2 set fldMablagheDaryaftiJari=0 where fldMablagheDaryaftiJari is null

	update #tmpDaryaftTasfie2 set fldMablagheMande=(tbl2.fldMablagheGhest-tbl2.fldMablagheDaryaftiJari) from
	(Select fldShomareVam,fldShomareGhest,fldMablagheGhest,fldMablagheDaryaftiJari from #tmpDaryaftTasfie2
	) as tbl2
	where tbl2.fldShomareVam=#tmpDaryaftTasfie2.fldShomareVam and tbl2.fldShomareGhest=#tmpDaryaftTasfie2.fldShomareGhest

	update #tmpDaryaftTasfie2 set fldCodeGroup=tblTafzili.fldCodeGroup from #tmpDaryaftTasfie2
	inner join tblTafzili on #tmpDaryaftTasfie2.fldCodeTafzili=tblTafzili.fldCodeTafzili

	update #tmpDaryaftTasfie2 set fldNameGroup=tblGroupAshkhas.fldGroupName from #tmpDaryaftTasfie2
	inner join tblGroupAshkhas on #tmpDaryaftTasfie2.fldCodeGroup=tblGroupAshkhas.fldGroupId



	update #tmpDaryaftTasfie2 set fldSharhTafzili=tblTafzili.fldSharheTafzili from #tmpDaryaftTasfie2
	inner join tblTafzili on #tmpDaryaftTasfie2.fldCodeTafzili=tblTafzili.fldCodeTafzili

	insert into #tmpTedadAghsat(fldShomareVam,fldTedadAghsat)
	select fldShomareVam,COUNT(*) fldTedadAghsat from #tmpDaryaftTasfie2 where fldMablagheMande>0
	group by fldShomareVam



	update #tmpDaryaftTasfie2 set fldTedadAghsat=tbl2.fldTedadAghsat from
	(Select fldShomareVam,fldTedadAghsat from #tmpTedadAghsat) as tbl2
	where tbl2.fldShomareVam=#tmpDaryaftTasfie2.fldShomareVam 
	  

	if @ListAshkhas <>''
	   begin
			 declare @StrDeleteAshkhas varchar(5000)
			 set  @StrDeleteAshkhas='delete from #tmpDaryaftTasfie2 
			 where '+ @ListAshkhas + ''
			execute(@StrDeleteAshkhas)
		end


		if @ListGroupAshkhas <>''
		      begin
		       declare @StrDeleteGroupAshkhas varchar(5000)
		        set  @StrDeleteGroupAshkhas='delete from #tmpDaryaftTasfie2 
		            where '+ @ListGroupAshkhas + ''
					execute(@StrDeleteGroupAshkhas)
		      end


		insert into #tmpDaryaftTasfie1
	(fldShomareVam,fldTedadAghsat ,fldCodeGroup,fldNameGroup,fldCodeTafzili,fldSharhTafzili,fldMablagheGhest,fldMablagheMande,fldMablagheDaryaftiJari,fldDate)
		Select  fldShomareVam,#tmpDaryaftTasfie2.fldTedadAghsat,fldCodeGroup,fldNameGroup,fldCodeTafzili,fldSharhTafzili,fldMablagheGhest,fldMablagheMande*fldTedadAghsat   as fldMablagheMande,fldMablagheDaryaftiJari,
		substring(convert(nvarchar(10),fldDate),0,5) +'/' +substring(convert(nvarchar(10),fldDate),5,2) + '/' + substring(convert(nvarchar(10),fldDate),7,2) as fldDate
		from #tmpDaryaftTasfie2 
		where    #tmpDaryaftTasfie2.fldMablagheMande>0 
		group by fldShomareVam,#tmpDaryaftTasfie2.fldTedadAghsat,fldCodeGroup,fldNameGroup,fldCodeTafzili,fldSharhTafzili,fldMablagheGhest,fldMablagheMande,fldMablagheDaryaftiJari,fldDate

		update #tmpDaryaftTasfie1 set fldMablagheMande=tbl2.fldMablagheMande from
		 (Select fldShomareVam,fldMablagheMande,fldMablagheDaryaftiJari from #tmpDaryaftTasfie1 where fldMablagheDaryaftiJari=0) as tbl2	
		where tbl2.fldShomareVam=#tmpDaryaftTasfie1.fldShomareVam and #tmpDaryaftTasfie1.fldMablagheDaryaftiJari>0

		update #tmpDaryaftTasfie1 set fldMablagheMande=(tbl2.fldMablagheGhest*tbl2.fldTedadAghsat)-tbl2.fldMablagheDaryaftiJari from
		 (Select fldShomareVam,fldMablagheGhest,fldMablagheDaryaftiJari,fldTedadAghsat from #tmpDaryaftTasfie1 where fldMablagheDaryaftiJari>0) as tbl2	
		where tbl2.fldShomareVam=#tmpDaryaftTasfie1.fldShomareVam and #tmpDaryaftTasfie1.fldMablagheDaryaftiJari>0

		update #tmpDaryaftTasfie1 set fldMablagheMande=tbl2.fldMablagheMande, fldMablagheDaryaftiJari=tbl2.fldMablagheDaryaftiJari from
		 (Select fldShomareVam,fldMablagheMande,fldMablagheDaryaftiJari from #tmpDaryaftTasfie1 where fldMablagheDaryaftiJari>0) as tbl2	
		where tbl2.fldShomareVam=#tmpDaryaftTasfie1.fldShomareVam and #tmpDaryaftTasfie1.fldMablagheDaryaftiJari=0
	

	select distinct fldShomareVam,fldTedadAghsat ,fldCodeGroup,fldNameGroup,fldCodeTafzili
	,fldSharhTafzili,fldMablagheGhest,fldMablagheMande
	,fldMablagheDaryaftiJari,fldDate from #tmpDaryaftTasfie1
	
		
		--delete #tmpDaryaftTasfie1
		--delete #tmpDaryaftTasfie2
		--delete #tmpTedadAghsat
END
GO
/****** Object:  StoredProcedure [dbo].[z186Translate]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z186Translate] 
	@Lang nvarchar(10),
	@Form nvarchar(40)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT        ID, fldCode, fldLanguage, fldForm, fldMatn
			FROM            tblTranslateMain 
			where fldLanguage=@Lang and fldForm=@Form
			
END

GO
/****** Object:  StoredProcedure [dbo].[z187TranslateKalame]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[z187TranslateKalame] 
	@Language nvarchar(10),
	@CodeMessage bigint,
	@Message nvarchar(max) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	set @message=(SELECT fldMatn
					FROM  tblTranslateMain 
						where fldLanguage=@Language and fldCode=@CodeMessage)
			
END

GO
/****** Object:  StoredProcedure [dbo].[z188TranslateKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[z188TranslateKala] 

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	select tblKalaTranslate.ID,tblKalaTranslate.fldCodeKala,tblKala.fldNameKala,fldLanguage,fldTranslate
	 from tblKalaTranslate inner join
	 tblKala on tblKala.fldCodeKala=tblKalaTranslate.fldCodeKala
			
END

GO
/****** Object:  StoredProcedure [dbo].[z189InsertTranslateKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[z189InsertTranslateKala] 
( @fldCodeKala bigint,
  @fldLanguage nvarchar(20),
  @fldTranslate nvarchar(50)
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	insert into tblKalaTranslate(fldCodeKala,fldLanguage,fldTranslate)
	values(@fldCodeKala,@fldLanguage,@fldTranslate)
			
END

GO
/****** Object:  StoredProcedure [dbo].[z190InsertTarheTakhfifNaghdi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[z190InsertTarheTakhfifNaghdi]

@ID bigint,
@fldMablagh decimal,
@fldDarsad decimal,
@countRow bigint

as 

begin

	insert into tblTarheTakhfifNaghdi
		values(@ID,@fldMablagh,@fldDarsad,@countRow)

end
GO
/****** Object:  StoredProcedure [dbo].[z190UpdateTranslateKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[z190UpdateTranslateKala] 
( @fldCodeKala bigint,
  @fldLanguage nvarchar(20),
  @fldTranslate nvarchar(20),
  @fldCurrentLanguage nvarchar(20)
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	update tblKalaTranslate set fldLanguage=@fldLanguage,fldTranslate=@fldTranslate
	where fldCodeKala=@fldCodeKala and fldLanguage=@fldCurrentLanguage
			
END

GO
/****** Object:  StoredProcedure [dbo].[z191UpdateTblTarheTakhfifNaghdi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[z191UpdateTblTarheTakhfifNaghdi]

@ID int,
@fldMablagh decimal,
@fldDarsad decimal,
@flagOut int output
as

begin
	Update tblTarheTakhfifNaghdi Set fldMablagh =@fldMablagh,fldDarsad=@fldDarsad Where countRow=@id
	set @flagOut=1
end
GO
/****** Object:  StoredProcedure [dbo].[z192DeleteTblTarheTakhfifNaghdi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc  [dbo].[z192DeleteTblTarheTakhfifNaghdi]
@ID as int

as
begin
Delete from tblTarheTakhfifNaghdi Where ID=@ID
end
GO
/****** Object:  StoredProcedure [dbo].[z193ShowtblTarheTakhfifNaghdi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create proc [dbo].[z193ShowtblTarheTakhfifNaghdi]
--@CodeKolHesab nvarChar(8)
as
begin
	select Id,fldMablagh,fldDarsad,countRow from tblTarheTakhfifNaghdi
	ORDER BY fldMablagh 
		--Where fldCodeHesab<>@CodeKolHesab and left(fldCodeHesab,len(@CodeKolHesab))=@CodeKolHesab
		
end
GO
/****** Object:  StoredProcedure [dbo].[z194TekrariMablaghTakhfifNaghdi]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[z194TekrariMablaghTakhfifNaghdi]

@MablaghTakhfif bigint,
@ID int,
@IDType int,
@flagOUt int output
as
begin
	declare @RowCountTakhfif int
	if @IDType=0  --براي مقايسه براي حالت اضافه کردن رکورد
		begin
		 set @RowCountTakhfif=(Select Count(fldMablagh) from tblTarheTakhfifNaghdi  Where fldMablagh=@MablaghTakhfif)
		 set @flagOUt=@RowCountTakhfif
		end
	else
		begin --براي مقايسه با غيرخودش در حالت ويرايش
		   set	@RowCountTakhfif=(Select  Count(fldMablagh) from tblTarheTakhfifNaghdi  Where fldMablagh=@MablaghTakhfif and countRow<>@ID)
		   set @flagOUt=@RowCountTakhfif
		end
end
GO
/****** Object:  StoredProcedure [dbo].[ZSP_Apply_Raffles]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[ZSP_Apply_Raffles]
	-- Add the parameters for the stored procedure here
	@TotalCost bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	
	
	
	  select top 1 fldcodekala,fldcount,fldprice from tbleshantionmablagy 
  where fldprice <= @TotalCost 
  order by fldprice desc
END

GO
/****** Object:  StoredProcedure [dbo].[ZSP_GetAccessableAshkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[ZSP_GetAccessableAshkhas] 
	-- Add the parameters for the stored procedure here
	@usercode bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

  Declare @value nvarchar(100)
  set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPASHKHAS')
  if @value = 'True'
  Begin -- Limitation Active

    declare @groups table(fldCode bigint)
	 insert into @groups(fldCode)
	 exec ZSP_GetGroupAshkhasAccess @usercode


 select fldCodeTafzili,fldCodeGroup,fldSharheTafzili from tblTafzili as t
 inner join @groups as g on convert(nvarchar(100),t.fldCodeGroup) like convert(nvarchar(100),g.fldCode) + '%'
 group by fldCodeTafzili,fldCodeGroup,fldSharheTafzili
 order by CONVERT(varchar,fldCodeTafzili )


  End
  else
  Begin -- Limitation DeActive
	select fldCodeTafzili,fldCodeGroup,fldSharheTafzili from tblTafzili
	order by CONVERT(varchar,fldCodeTafzili )
  End
	 
END

GO
/****** Object:  StoredProcedure [dbo].[ZSP_GetAccessableGroupAshkhas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[ZSP_GetAccessableGroupAshkhas] 
	-- Add the parameters for the stored procedure here
	@usercode bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

  Declare @value nvarchar(100)
  set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPASHKHAS')
  if @value = 'True'
  Begin -- Limitation Active

  declare @tbl table(fldCode bigint) -- in fact -> fldCodeGroupKala 
 insert @tbl(fldCode)
 exec ZSP_GetGroupAshkhasAccess @usercode
 
 select fldGroupId,fldGroupName from tblGroupAshkhas 
 where  fldGroupId in(select fldCode from @tbl)
 order by CONVERT(varchar,fldGroupId )

  End
  else
  Begin -- Limitation DeActive
	select fldGroupId,fldGroupName from tblGroupAshkhas
	order by CONVERT(varchar,fldGroupId )
  End
	 
END

GO
/****** Object:  StoredProcedure [dbo].[ZSP_GetAccessableGroupKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ZSP_GetAccessableGroupKala] 
	-- Add the parameters for the stored procedure here
	@usercode bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

  Declare @value nvarchar(100)
  set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPKALA')
  if @value = 'True'
  Begin -- Limitation Active

  declare @tbl table(fldkalacode bigint) -- in fact -> fldCodeGroupKala 
 insert @tbl(fldkalacode)
 exec ZSP_GetKalaAccesses @usercode
 
 select fldGroupId,fldGroupName from viewShowListSmallGroupKala where  fldGroupId in(select fldkalacode from @tbl)

  End
  else
  Begin -- Limitation DeActive
	select fldGroupId,fldGroupName from viewShowListSmallGroupKala
  End


 
	 
END

GO
/****** Object:  StoredProcedure [dbo].[ZSP_GetAccessableGroupKalaCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[ZSP_GetAccessableGroupKalaCode] 
	-- Add the parameters for the stored procedure here
	@usercode bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

  Declare @value nvarchar(100)
  set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPKALA')
  if @value = 'True'
  Begin -- Limitation Active


 declare @tbl table(fldkalacode bigint) -- in fact -> fldCodeGroupKala 
 insert @tbl(fldkalacode)
 --exec ZSP_GetKalaAccesses @usercode--to ignore nested insert
 select fldKalaCode from tblGroupKalaAccess where fldUserCode = @usercode 


  select fldGroupId as code from viewShowListSmallGroupKala 
 where  fldGroupId in(select fldkalacode from @tbl)
 order by CONVERT(varchar,fldGroupId )

  End
  else
  Begin -- Limitation DeActive
	select fldGroupId as code from viewShowListSmallGroupKala
	order by CONVERT(varchar,fldGroupId )
  End

END

GO
/****** Object:  StoredProcedure [dbo].[ZSP_GetAccessableKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ZSP_GetAccessableKala] 
	-- Add the parameters for the stored procedure here
	@language nvarchar(10),
	@userCode bigint ,
	@int1 bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @kalas table(
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldFeeForoosh money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
SumMande Nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadDarKartonShow Nvarchar(100) COLLATE Persian_100_CI_AS
)
 insert into @kalas(fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,SumMande,fldFeeForoosh,fldCodeGroup,fldTedadDarKartonShow)
 exec ShowListMojoodieAnbarForForoosh @language,@int1
 

  Declare @value nvarchar(100)
  set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPKALA')
  if @value = 'True'
  Begin -- Limitation Active

	  declare @groups table(fldKalaCode bigint)
	 insert into @groups(fldKalaCode)
	 exec ZSP_GetKalaAccesses @usercode
	 
	 --select * from @groups -- 100,101
	 
	 select fldCodeKala,fldCodeGroup,fldNameKala from @kalas
	 as k inner join @groups as g on convert(nvarchar(100),k.fldCodeGroup) like convert(nvarchar(100),g.fldKalaCode) + '%'
	 group by fldCodeKala ,fldCodeGroup,fldNameKala 
	  order by fldCodeKala 

  End -- end of limitation
  else
  Begin
	select fldCodeKala,fldCodeGroup,fldNameKala from @kalas
  End
 
End

GO
/****** Object:  StoredProcedure [dbo].[ZSP_GetFUllMemberAccessableKala]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[ZSP_GetFUllMemberAccessableKala] 
	-- Add the parameters for the stored procedure here
	@userCode bigint ,
	@int1 bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @kalas table(
fldCodeGroup bigint,
fldCodeKala bigint,
fldNameKala nvarchar(100) COLLATE Persian_100_CI_AS,
fldFeeForoosh money,
fldNameVahed NVarchar(100) COLLATE Persian_100_CI_AS,
fldNameVahed2 Nvarchar(100) COLLATE Persian_100_CI_AS,
SumMande Nvarchar(100) COLLATE Persian_100_CI_AS,
fldTedadDarKartonShow Nvarchar(100) COLLATE Persian_100_CI_AS
)
 insert into @kalas(fldCodeKala,fldNameKala,fldNameVahed,fldNameVahed2,SumMande,fldFeeForoosh,fldCodeGroup,fldTedadDarKartonShow)
 exec ShowListMojoodieAnbarForForoosh @int1
 

  Declare @value nvarchar(100)
  set @value =( select fldMeghdar from tblTanzimateKol  where fldType = 'ACCESSGROUPKALA')
  if @value = 'True'
  Begin -- Limitation Active

	  declare @groups table(fldKalaCode bigint)
	 insert into @groups(fldKalaCode)
	 exec ZSP_GetKalaAccesses @usercode
	 
	 --select * from @groups -- 100,101
	 
	 select fldCodeKala ,fldNameKala,fldNameVahed,fldNameVahed2,SumMande,fldFeeForoosh,fldCodeGroup,fldTedadDarKartonShow
	 from @kalas
	 as k inner join @groups as g on convert(nvarchar(100),k.fldCodeGroup) like convert(nvarchar(100),g.fldKalaCode) + '%'
	 group by fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumMande,fldFeeForoosh,fldCodeGroup,fldTedadDarKartonShow 
	 order by fldCodeKala 

  End -- end of limitation
  else
  Begin
	select fldCodeKala ,fldNameKala,fldNameVahed ,fldNameVahed2,SumMande,fldFeeForoosh,fldCodeGroup,fldTedadDarKartonShow
	from @kalas

  End
 
End

GO
/****** Object:  StoredProcedure [dbo].[ZSP_GetGroupAshkhasAccess]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ZSP_GetGroupAshkhasAccess]
	-- Add the parameters for the stored procedure here
	@userCode bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	select fldGroupCode from tblGroupAshkhasAccess where fldUserCode = @userCode 
END

GO
/****** Object:  StoredProcedure [dbo].[ZSP_GetKalaAccesses]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[ZSP_GetKalaAccesses]
	-- Add the parameters for the stored procedure here
	@userCode bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	select fldKalaCode from tblGroupKalaAccess where fldUserCode = @userCode 
END

GO
/****** Object:  StoredProcedure [dbo].[ZSP_GrantGroupAshkhasAccess]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[ZSP_GrantGroupAshkhasAccess]
	-- Add the parameters for the stored procedure here
	@userCode Bigint,
	@GroupCodes [dbo].[ZT_CodeNumbers] READONLY
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	-- Set Group kala access
	Begin Transaction
	Begin Try
		 delete from tblGroupAshkhasAccess where fldUserCode = @userCode

		insert into tblGroupAshkhasAccess(fldUserCode,fldGroupCode)
			select @userCode,CodeNumber from @GroupCodes

		Commit Transaction
	End Try
	Begin Catch
		Rollback Transaction
	End Catch
    -- Insert statements for procedure here
	--select @userCode,[CodeNumber] from @GroupCodes
END

GO
/****** Object:  StoredProcedure [dbo].[ZSP_GrantKalaAccess]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ZSP_GrantKalaAccess]
	-- Add the parameters for the stored procedure here
	@userCode bigint
	,@kalaCodes varchar(max)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    
	Begin Transaction GKA
	 Begin Try

	  declare @tmp table(number bigint)
	  insert into @tmp(number)
		select number from dbo.ZF_SplitString(@kalaCodes,',')

	  delete from tblGroupKalaAccess where fldUserCode = @userCode

	  insert into tblGroupKalaAccess(fldUserCode,fldKalaCode)
			select @userCode,number from @tmp
	  
	  Commit Transaction GKA
	 End Try
	 Begin Catch
		Rollback Transaction GKA
	 End Catch




END

GO
/****** Object:  StoredProcedure [dbo].[ZSP_KallaAccess]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[ZSP_KallaAccess]
	-- Add the parameters for the stored procedure here
	@userCode Bigint,
	@kalaCodes [dbo].[ZT_CodeNumbers] READONLY
AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	Begin Transaction
	Begin Try
		 delete from tblGroupKalaAccess where fldUserCode = @userCode

		insert into tblGroupKalaAccess(fldUserCode,fldKalaCode)
			select @userCode,CodeNumber from @kalaCodes

		Commit Transaction
	End Try
	Begin Catch
		Rollback Transaction
	End Catch
    -- Insert statements for procedure here
	select @userCode,[CodeNumber] from @kalaCodes
END

GO
/****** Object:  StoredProcedure [dbo].[ZSP_LastEnteghalCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[ZSP_LastEnteghalCode]
	-- Add the parameters for the stored procedure here
	@Code bigint out
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @tmp bigint = (select max(fldShomareEnteghal) from tblCheckeDaryafti)
	if @tmp is Null or @tmp =  0 
	begin
	 set @Code = 1
	end
	else
	begin
	 set @Code = @tmp + 1
	end
END

GO
/****** Object:  StoredProcedure [dbo].[ZSP_LastOdatCode]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[ZSP_LastOdatCode]
	-- Add the parameters for the stored procedure here
	@Code bigint out
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @tmp bigint = (select max(fldShomareOdat) from tblCheckeDaryafti)
	if @tmp is Null or @tmp =  0 
	begin
	 set @Code = 1
	end
	else
	begin
	 set @Code = @tmp + 1
	end
END
GO
/****** Object:  StoredProcedure [dbo].[ZSP_viewFilter_Tbl_Tafsili_in_Form_Enteghal_Kalas]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ZSP_viewFilter_Tbl_Tafsili_in_Form_Enteghal_Kalas]
	-- Add the parameters for the stored procedure here
	@userCode bigint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.

	SET NOCOUNT ON;

	declare @accessCodes table(code bigint)
	insert into @accessCodes select * from ZF_GetAccessableAshkhasCode(@userCode) 

	SELECT  fldCodeTafzili, fldSharheTafzili,fldAddress,fldGroupName
	FROM   viewFilter_Tbl_Tafsili_in_Form_Enteghal_Kala
	where fldCodeTafzili in (select code from @accessCodes)


END
GO
/****** Object:  StoredProcedure [dbo].[ZSP_VisitorSalaryAndPorsant]    Script Date: 09/25/2022 02:23:41 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[ZSP_VisitorSalaryAndPorsant] 
	-- Add the parameters for the stored procedure here
	@tafzili nvarchar(max),
	@moien nvarchar(max),
	@fromDate bigint,
	@toDate bigint
AS
BEGIN
	SELECT fldCodeTafzili,fldMoinKamel,sum(fldBedehkar) as Bedehkar,sum(fldBestankar) as Bestankar	
	from tblSodooreSanad 
	where 
	(fldDate between @fromDate and @toDate) 
	and
	fldMoinKamel in (select number from ZF_SplitString(@moien,','))  --(61201,31303,31303,31399,61111,31399)
	and fldCodeTafzili in (select number from ZF_SplitString(@tafzili,','))

	group by fldMoinKamel,fldCodeTafzili
END

GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[20] 2[22] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblDaftarcheTell"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 135
               Right = 222
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblTafzili"
            Begin Extent = 
               Top = 12
               Left = 460
               Bottom = 249
               Right = 643
            End
            DisplayFlags = 280
            TopColumn = 7
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 12
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'Join_TblTell_TblTafzili'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'Join_TblTell_TblTafzili'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblKala"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 135
               Right = 265
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'lstlistKala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'lstlistKala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[42] 4[10] 2[17] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblGroupAshkhas"
            Begin Extent = 
               Top = 51
               Left = 38
               Bottom = 199
               Right = 208
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblTafzili"
            Begin Extent = 
               Top = 27
               Left = 279
               Bottom = 257
               Right = 462
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblDaftarcheTell"
            Begin Extent = 
               Top = 39
               Left = 619
               Bottom = 230
               Right = 789
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'View_2'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'View_2'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[44] 4[8] 2[5] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblTafzili"
            Begin Extent = 
               Top = 1
               Left = 369
               Bottom = 397
               Right = 552
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblDaftarcheTell"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 135
               Right = 208
            End
            DisplayFlags = 280
            TopColumn = 2
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 19
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'View_ListAshkhas_In_ReportFactorForosh_AliJahangard'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'View_ListAshkhas_In_ReportFactorForosh_AliJahangard'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[21] 2[15] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblChap"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 135
               Right = 224
            End
            DisplayFlags = 280
            TopColumn = 4
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'View_TblChap'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'View_TblChap'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[20] 2[11] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblTafzili"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 135
               Right = 221
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewFilter_Tbl_Tafsili_in_Form_Enteghal_Kala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewFilter_Tbl_Tafsili_in_Form_Enteghal_Kala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[21] 2[11] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblAnbar"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 149
               Right = 208
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewGetAnbarForComboBox'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewGetAnbarForComboBox'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[20] 2[11] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblEshantionGroupTabaghati"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 220
               Right = 243
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblKala"
            Begin Extent = 
               Top = 6
               Left = 281
               Bottom = 236
               Right = 508
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblGroupKala"
            Begin Extent = 
               Top = 6
               Left = 546
               Bottom = 205
               Right = 716
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 2700
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewGetListEshantionGroupTabalaghtis'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewGetListEshantionGroupTabalaghtis'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[37] 4[13] 2[27] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblEshantionTabaghati"
            Begin Extent = 
               Top = 15
               Left = 308
               Bottom = 224
               Right = 513
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblKala"
            Begin Extent = 
               Top = 11
               Left = 602
               Bottom = 235
               Right = 800
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblKala_1"
            Begin Extent = 
               Top = 25
               Left = 32
               Bottom = 235
               Right = 230
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 10
         Width = 284
         Width = 1935
         Width = 1500
         Width = 2355
         Width = 2130
         Width = 2310
         Width = 2445
         Width = 2385
         Width = 2310
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewGetListEshantionTabalaghti'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewGetListEshantionTabalaghti'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[34] 4[16] 2[27] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblKala"
            Begin Extent = 
               Top = 128
               Left = 483
               Bottom = 353
               Right = 710
            End
            DisplayFlags = 280
            TopColumn = 8
         End
         Begin Table = "tblRizeForoosh"
            Begin Extent = 
               Top = 0
               Left = 213
               Bottom = 370
               Right = 443
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblAnbar"
            Begin Extent = 
               Top = 12
               Left = 628
               Bottom = 124
               Right = 798
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblVahed"
            Begin Extent = 
               Top = 11
               Left = 4
               Bottom = 106
               Right = 174
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblVahed_2"
            Begin Extent = 
               Top = 152
               Left = 19
               Bottom = 247
               Right = 189
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 28
         Width = 284
         Width = 1500
         Width = 2280
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
   ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewGridForoosh'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane2', @value=N'      Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewGridForoosh'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=2 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewGridForoosh'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[20] 2[10] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblKala"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 186
               Right = 265
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewListBarcodeKala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewListBarcodeKala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[42] 4[21] 2[25] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblKala (dbo)"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 135
               Right = 265
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblVahed (dbo)"
            Begin Extent = 
               Top = 6
               Left = 303
               Bottom = 101
               Right = 473
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblVahed_1"
            Begin Extent = 
               Top = 6
               Left = 482
               Bottom = 101
               Right = 652
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewListKalaInFactorForoosh'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewListKalaInFactorForoosh'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblKala"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 135
               Right = 236
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblVahed"
            Begin Extent = 
               Top = 6
               Left = 274
               Bottom = 101
               Right = 444
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblVahed_1"
            Begin Extent = 
               Top = 6
               Left = 482
               Bottom = 101
               Right = 652
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewListKalaInFactorForooshByFldCodeGroup'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewListKalaInFactorForooshByFldCodeGroup'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[20] 2[11] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblKala"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 222
               Right = 236
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewListSmallKala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewListSmallKala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblTafzili"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 135
               Right = 265
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblTafzili_1"
            Begin Extent = 
               Top = 6
               Left = 303
               Bottom = 135
               Right = 530
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewLoadVizitor'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewLoadVizitor'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[33] 4[19] 2[23] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblGroupAshkhas"
            Begin Extent = 
               Top = 61
               Left = 41
               Bottom = 192
               Right = 227
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblTafzili"
            Begin Extent = 
               Top = 48
               Left = 274
               Bottom = 177
               Right = 473
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblDaftarcheTell"
            Begin Extent = 
               Top = 45
               Left = 615
               Bottom = 250
               Right = 801
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 12
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewReportListAshkhasByIN'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewReportListAshkhasByIN'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[12] 2[19] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblRizeForoosh"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 209
               Right = 268
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblAnbar"
            Begin Extent = 
               Top = 0
               Left = 287
               Bottom = 112
               Right = 457
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblTafzili"
            Begin Extent = 
               Top = 28
               Left = 512
               Bottom = 157
               Right = 695
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblKala"
            Begin Extent = 
               Top = 27
               Left = 738
               Bottom = 156
               Right = 965
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblGroupAshkhas"
            Begin Extent = 
               Top = 159
               Left = 486
               Bottom = 254
               Right = 656
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblGroupKala"
            Begin Extent = 
               Top = 138
               Left = 38
               Bottom = 233
               Right = 208
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 30
         Width = 284
         Width = 1500
     ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewRizeForooshEhsan'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane2', @value=N'    Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewRizeForooshEhsan'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=2 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewRizeForooshEhsan'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[42] 4[10] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblKala (dbo)"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 213
               Right = 265
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblVahed (dbo)"
            Begin Extent = 
               Top = 6
               Left = 303
               Bottom = 101
               Right = 473
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblVahed_1"
            Begin Extent = 
               Top = 72
               Left = 516
               Bottom = 167
               Right = 686
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewShowListKala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewShowListKala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblGroupKala"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 105
               Right = 208
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewShowListSmallGroupKala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewShowListSmallGroupKala'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[4] 4[34] 2[23] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblRizeForoosh"
            Begin Extent = 
               Top = 64
               Left = 341
               Bottom = 288
               Right = 571
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblTafzili_1"
            Begin Extent = 
               Top = 11
               Left = 660
               Bottom = 248
               Right = 843
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblTafzili"
            Begin Extent = 
               Top = 20
               Left = 82
               Bottom = 280
               Right = 265
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewtestforoosh'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewtestforoosh'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[20] 2[7] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblTafzili"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 214
               Right = 221
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tblDaftarcheTell"
            Begin Extent = 
               Top = 20
               Left = 466
               Bottom = 254
               Right = 636
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewtestShomaretel'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viewtestShomaretel'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[42] 4[21] 2[28] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tblCheckePardakhti"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 135
               Right = 240
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 19
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1485
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1980
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viwListeCheckHayePasNashode'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'viwListeCheckHayePasNashode'
GO



Delete from tblListeDastrasi
GO

SET IDENTITY_INSERT [dbo].[tblListeDastrasi] ON 

GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (1, 100, N'فروش', 1, 1, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (2, 101, N'امکان حذف فاکتور فروش', 1, 2, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (3, 102, N'امکان ویرایش فاکتور فروش', 1, 3, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (4, 103, N'امکان ثبت فاکتور فروش اعتباری', 1, 4, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (5, 104, N'امکان مشاهده آرشیو فاکتور فروش', 1, 5, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (6, 105, N'امکان تغییر فی فروش', 1, 6, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (7, 106, N'امکان مشاهده انبار جاری در فاکتور فروش', 1, 7, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (8, 107, N'امکان مشاهده انبار کل در فاکتور فروش', 1, 8, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (9, 108, N'امکان ویرایش فاکتور توسط کاربر دیگر', 1, 9, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10, 201, N'امکان حذف فاکتور خرید', 2, 2, N'خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (11, 202, N'امکان ویرایش فاکتور خرید', 2, 3, N'خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (12, 200, N'خرید', 2, 1, N'خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10012, 109, N'امکان مشاهده آخرین فی خرید در فرم فروش', 1, 10, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10015, 203, N'امکان مشاهده آرشیو فاکتور خرید', 2, 4, N'خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10016, 204, N'امکان مشاهده انبار جاری در فاکتور خرید', 2, 5, N'خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10017, 205, N'امکان مشاهده انبار کل در فاکتور خرید', 2, 6, N'خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10018, 206, N'امکان ثبت فاکتور خرید اعتباری', 2, 7, N'خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10019, 207, N'امکان مشاهده آخرین فی خرید در فرم خرید', 2, 8, N'خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10020, 300, N'برگشت از فروش', 3, 1, N'برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10021, 301, N'امکان حذف برگشت از فروش', 3, 2, N'برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10022, 302, N'امکان ویرایش برگشت از فروش', 3, 3, N'برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10023, 303, N'امکان ثبت برگشت از فروش اعتباری', 3, 4, N'برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10024, 304, N'امکان مشاهده آرشیو برگشت از فروش', 3, 5, N'برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10025, 305, N'امکان تغییر فی فروش', 3, 6, N'برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10026, 306, N'امکان مشاهده انبار جاری در برگشت از فروش', 3, 7, N'برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (10027, 307, N'امکان مشاهده انبار کل در برگشت از فروش', 3, 8, N'برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (20012, 400, N'برگشت از خرید', 4, 1, N'برگشت از خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (20013, 401, N'امکان حذف برگشت از خرید', 4, 2, N'برگشت از خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (20014, 402, N'امکان ویرایش برگشت از خرید', 4, 3, N'برگشت از خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (20015, 403, N'امکان مشاهده آرشیو برگشت از خرید', 4, 4, N'برگشت از خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (20016, 404, N'امکان مشاهده انبار جاری در برگشت از خرید', 4, 5, N'برگشت از خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (20017, 405, N'امکان مشاهده انبار کل در برگشت از خرید', 4, 6, N'برگشت از خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (20018, 406, N'امکان ثبت برگشت از خرید اعتباری', 4, 7, N'برگشت از خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (20019, 407, N'امکان مشاهده آخرین فی خرید در برگشت از خرید', 4, 8, N'برگشت از خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30013, 500, N'پیش فاکتور', 5, 1, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30014, 550, N'سفارشات ویزیتورها', 6, 1, N'سفارشات ویزیتورها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30015, 600, N'مسیر حرکت ویزیتورها', 7, 1, N'مسیر حرکت ویزیتورها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30016, 610, N'انتقال کالا بین انبار', 8, 1, N'انتقال کالا بین انبار')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30017, 640, N'حواله گروهی', 9, 1, N'حواله گروهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30018, 650, N'دریافت مطالبات', 10, 1, N'دریافت مطالبات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30019, 670, N'پرداخت بدهی', 11, 1, N'پرداخت بدهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30020, 690, N'گزارش صندوق', 12, 1, N'گزارش صندوق')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30021, 700, N'ثبت درآمدها', 13, 1, N'ثبت درآمدها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30022, 710, N'ثبت هزینه ها', 14, 1, N'ثبت هزینه ها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30023, 720, N'انتقال وجه', 15, 1, N'انتقال وجه')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30024, 730, N'گزارش جامع فروش', 16, 1, N'گزارش جامع فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30025, 740, N'گزارش جامع خرید', 17, 1, N'گزارش جامع خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30026, 750, N'گزارش جامع برگشت از فروش', 18, 1, N'گزارش جامع برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30027, 760, N'گزارش جامع برگشت از خرید', 19, 1, N'گزارش جامع برگشت از خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30028, 770, N'سند حسابداری', 20, 1, N'سند حسابداری')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30029, 780, N'ثبت سفارش غذا', 21, 1, N'ثبت سفارش غذا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30030, 790, N'محل چاپگر', 22, 1, N'محل چاپگر')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30031, 795, N'معرفی مشترکین', 23, 1, N'معرفی مشترکین')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30032, 800, N'خدمات کارواش', 24, 1, N'خدمات کارواش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30033, 805, N'معرفی راننده', 25, 1, N'معرفی راننده')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30035, 815, N'گزارش عملکرد راننده', 27, 1, N'گزارش عملکرد راننده')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30036, 820, N'ثبت سرویس', 28, 1, N'ثبت سرویس')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30037, 830, N'معرفی مسیر', 29, 1, N'معرفی مسیر')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30038, 835, N'در انتظار آی دی کالر', 30, 1, N'در انتظار آی دی کالر')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30039, 840, N'معرفی دسته چک', 36, 1, N'بانک')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30040, 845, N'ثبت وصول چک', 36, 1, N'بانک')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30041, 850, N'واگذاری چک به بانک', 36, 1, N'بانک')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30042, 855, N'ثبت پاس چک خودمان', 36, 1, N'بانک')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30043, 860, N'گزارش بانک', 36, 1, N'بانک')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30044, 865, N'گزارش چکهای دریافتی', 36, 1, N'بانک')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30045, 870, N'گزارش لیست اشخاص', 37, 1, N'گزارش لیست اشخاص')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30046, 875, N'گزارش لیست کالا', 38, 1, N'گزارش لیست کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30047, 880, N'گزارش موجودی انبار', 39, 1, N'گزارش موجودی انبار')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30048, 885, N'کاردکس کالا', 40, 1, N'کاردکس کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30049, 890, N'گزارش صورتحساب اشخاص', 41, 1, N'گزارش صورتحساب اشخاص')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30050, 895, N'لیست بدهکاران', 42, 1, N'لیست بدهکاران')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30051, 900, N'لیست بستانکاران', 43, 1, N'لیست بستانکاران')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30052, 905, N'مانده حساب اشخاص', 44, 1, N'مانده حساب اشخاص')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30053, 910, N'عملکرد تفصیلی', 45, 1, N'عملکرد تفصیلی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30054, 915, N'فاکتورهای تسویه نشده', 46, 1, N'فاکتورهای تسویه نشده')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30055, 920, N'معرفی کالا', 47, 1, N'معرفی کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30056, 925, N'گروه بندی کالا', 48, 1, N'گروه بندی کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30057, 930, N'موجودی ابتدای دوره', 49, 1, N'موجودی ابتدای دوره')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30058, 935, N'واحد اندازه گیری', 50, 1, N'واحد اندازه گیری')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30059, 940, N'معرفی اشخاص', 51, 1, N'معرفی اشخاص')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30060, 945, N'گروه بنده اشخاص', 52, 1, N'گروه بنده اشخاص')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30061, 950, N'دفترچه تلفن', 53, 1, N'دفترچه تلفن')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30062, 955, N'مانده ابتدای دوره اشخاص', 54, 1, N'مانده ابتدای دوره اشخاص')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30063, 960, N'معرفی ویزیتور', 55, 1, N'معرفی ویزیتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30064, 965, N'معرفی سوپروایزر', 56, 1, N'معرفی سوپروایزر')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30065, 970, N'معرفی بانک مشتری', 57, 1, N'معرفی بانک مشتری')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30066, 975, N'معرفی بانک ما', 58, 1, N'معرفی بانک ما')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30067, 980, N'معرفی صندوق', 59, 1, N'معرفی صندوق')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30068, 985, N'معرفی انبار', 60, 1, N'معرفی انبار')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30069, 990, N'معرفی هزینه ها', 61, 1, N'معرفی هزینه ها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30070, 995, N'معرفی درآمدها', 62, 1, N'معرفی درآمدها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30071, 1000, N'پشتیبان گیری اطلاعات', 63, 1, N'پشتیبان گیری اطلاعات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30072, 1005, N'بازیابی اطلاعات', 63, 1, N'بازیابی اطلاعات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30073, 1010, N'پاک کردن اطلاعات', 63, 1, N'پاک کردن اطلاعات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30074, 1015, N'تنظیمات چاپ', 63, 1, N'تنظیمات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30075, 1020, N'تنظیمات قفل', 63, 2, N'تنظیمات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30076, 1025, N'تنظیمات خرید و فروش', 63, 3, N'تنظیمات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30077, 1030, N'بروزرسانی اطلاعات تبلت', 63, 4, N'تنظیمات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30078, 1035, N'بروزرسانی ', 63, 5, N'تنظیمات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30079, 1040, N'ایجاد حساب جدید', 63, 6, N'تنظیمات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30080, 731, N'نمودار فروش', 16, 2, N'گزارش جامع فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30081, 732, N'نمودار مقایسه ای ویزیتورها', 16, 3, N'گزارش جامع فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30082, 866, N'هشدار سررسید چک', 36, 1, N'بانک')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30083, 1041, N'تنظیمات کلی', 63, 7, N'تنظیمات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30084, 771, N'سود و زیان', 65, 1, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30085, 110, N'ثبت فاکتور به تاریخ غیرجاری', 1, 11, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30086, 111, N'محسابه سود فاکتور', 1, 12, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30087, 112, N'سود کالا در فاکتور ', 1, 13, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30088, 711, N'گزارش هزینه ها ', 14, 2, N'ثبت هزینه ها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30089, 113, N'امکان حذف فاکتور به تاریخ غیرجاری', 1, 14, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30090, 114, N'امکان ویرایش فاکتور به تاریخ غیرجاری', 1, 15, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30092, 115, N'ثبت فاکتور فقط به تاریخ جاری', 1, 16, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30093, 501, N'امکان حذف پیش فاکتور', 5, 2, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30094, 503, N'امکان ویرایش پیش فاکتور به تاریخ غیرجاری', 5, 4, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30095, 502, N'امکان ویرایش پیش فاکتور', 5, 3, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30096, 504, N'امکان حذف پیش فاکتور به تاریخ غیرجاری', 5, 5, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30097, 208, N'امکان حذف فاکتور خرید به تاریخ غیرجاری', 2, 9, N'خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30098, 209, N'امکان ویرایش فاکتور خرید به تاریخ غیرجاری', 2, 10, N'خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30100, 308, N'امکان حذف برگشت از فروش به تاریخ غیرجاری', 3, 9, N'برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30101, 309, N'امکان ویرایش برگشت از فروش به تاریخ غیرجاری', 3, 10, N'برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30102, 1042, N'امکان حذف انتقال کالا بین انبار', 64, 1, N'انتقال کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30103, 1043, N'امکان ویرایش انتقال کالا بین انبار', 64, 2, N'انتقال کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30104, 1044, N'امکان حذف انتقال کالا به تاریخ غیرجاری', 64, 3, N'انتقال کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30105, 1045, N'امکان ویرایش انتقال کالا به تاریخ غیرجاری', 64, 4, N'انتقال کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30106, 641, N'ثبت حواله گروهی', 9, 2, N'حواله گروهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30107, 642, N'حذف حواله گروهی', 9, 3, N'حواله گروهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30108, 643, N'ثبت حواله گروهی به تاریخ غیرجاری', 9, 4, N'حواله گروهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30109, 671, N'ثبت پرداخت بدهی', 11, 2, N'پرداخت بدهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30110, 672, N'حذف پرداخت بدهی', 11, 3, N'پرداخت بدهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30111, 673, N'ثبت پرداخت بدهی به تاریخ غیرجاری', 11, 4, N'پرداخت بدهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30112, 651, N'ثبت دریافت مطالبات', 10, 2, N'دریافت مطالبات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30113, 652, N'ثبت  دریافت بدهی به تاریخ غیرجاری', 10, 3, N'دریافت مطالبات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30114, 675, N'ویرایش پرداخت بدهی', 11, 6, N'پرداخت بدهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30115, 653, N'ویرایش دریافت مطالبات', 10, 4, N'دریافت مطالبات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30116, 654, N'حذف دریافت مطالبات', 10, 5, N'دریافت مطالبات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30117, 701, N'امکان حذف درآمد', 13, 2, N'ثبت درآمدها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30118, 702, N'امکان ویرایش درآمد', 13, 3, N'ثبت درآمدها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30119, 703, N' امکان حذف درآمد به تاریخ غیرجاری', 13, 4, N'ثبت درآمدها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30120, 655, N' امکان حذف دریافت بدهی به تاریخ غیرجاری', 10, 6, N'دریافت مطالبات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30121, 674, N' امکان حذف پرداخت بدهی به تاریخ غیرجاری', 11, 5, N'پرداخت بدهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30122, 644, N'امکان حذف حواله گروهی به تاریخ غیرجاری', 9, 5, N'حواله گروهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30123, 806, N'حذف راننده', 25, 2, N'معرفی راننده')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30124, 807, N'ویرایش راننده', 25, 3, N'معرفی راننده')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30125, 951, N'حذف در دفترچه تلفن', 53, 2, N'دفترچه تلفن')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30126, 952, N'ویرایش در دفترچه تلفن', 53, 3, N'دفترچه تلفن')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30127, 921, N'امکان حذف کالا', 47, 2, N'معرفی کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30128, 922, N'امکان ویرایش کالا', 47, 3, N'معرفی کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30129, 876, N'چاپ لیست کالا', 38, 2, N'گزارش لیست کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30130, 941, N'امکان حذف در معرفی اشخاص', 51, 2, N'معرفی اشخاص')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30131, 942, N'امکان ویرایش در معرفی اشخاص', 51, 3, N'معرفی اشخاص')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30132, 981, N'چاپ گزارش لیست اشخاص', 37, 2, N'گزارش لیست اشخاص')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30133, 1050, N'معرفی تفصیلی', 65, 7, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30134, 1051, N'ویرایش تفصیلی', 65, 2, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30135, 1052, N'حذف تفصیلی', 65, 3, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30136, 923, N'اشانتیون کالا', 47, 4, N'معرفی کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30137, 924, N'چند قیمته', 47, 5, N'معرفی کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30138, 1200, N'چند قیمته دینامیک', 47, 6, N'معرفی کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30139, 926, N'اشانتیون گروهی کالا', 48, 7, N'گروه بندی کالا')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30140, 961, N'پورسانت هدفمند', 55, 1, N'معرفی ویزیتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30141, 962, N'ویرایش پورسانت هدفمند', 55, 2, N'معرفی ویزیتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30142, 963, N'حذف پورسانت هدفمند', 55, 3, N'معرفی ویزیتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30143, 964, N'گزارش پورسانت هدفمند واریز', 55, 4, N'معرفی ویزیتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30144, 1053, N'معرفی کل و معین', 65, 4, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30145, 1054, N'ویرایش کل و معین', 65, 5, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30146, 1055, N'حذف کل و معین', 65, 6, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30147, 1210, N'تخفیفات مبلغی', 66, 1, N'تخفیفات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30148, 1211, N'ویرایش تخفیفات مبلغی', 66, 2, N'تخفیفات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30149, 1212, N'حذف تخفیفات مبلغی', 66, 3, N'تخفیفات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30150, 1213, N'تخفیفات حجمی', 66, 4, N'تخفیفات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30151, 1214, N'ویرایش تخفیفات حجمی', 66, 5, N'تخفیفات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30152, 1215, N'حذف تخفیفات حجمی', 66, 6, N'تخفیفات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30154, 1056, N'دفتر روزنامه', 65, 7, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30155, 1057, N'دفتر کل', 65, 8, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30156, 116, N'اصلاح کلی فاکتورها', 1, 17, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30157, 117, N'مرتب سازی فاکتورها', 1, 18, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30158, 505, N'تبدیل پیش فاکتورهایی که موجودی منفی دارند', 5, 6, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30159, 1201, N'قفل کردن فاکتورها', 63, 8, N'تنظیمات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30160, 506, N'اجازه تبدیل مجدد پیش فاکتور', 5, 7, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30161, 507, N'اجازه اصلاح پیش فاکتوری که به فاکتور تبدیل گردیده است', 5, 8, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30162, 118, N'چاپ فاکتور قبل از ثبت نهایی فاکتور', 1, 19, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30163, 119, N'پیش نمایش قبل از ثبت نهایی فاکتور', 1, 20, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30164, 508, N'چاپ پیش فاکتور قبل از ثبت نهایی', 5, 9, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30165, 509, N'پیش نمایش قبل از ثبت نهایی پیش فاکتور', 5, 10, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30166, 1046, N'معرفی سریع کالا در فرم ها', 63, 10, N'تنظیمات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30167, 1047, N'معرفی سریع اشخاص در فرم ها', 63, 9, N'تنظیمات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30168, 691, N'گزارش جامع دریافت', 12, 2, N'گزارشات دریافت و پرداخت')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30169, 692, N'گزارش جامع پرداخت', 12, 3, N'گزارشات دریافت و پرداخت')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30171, 120, N'مجوز حذف فاکتور مربوط به کاربر دیگر', 1, 21, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30172, 121, N'مجوز ویرایش فاکتور مربوط به کاربر دیگر', 1, 22, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30173, 510, N'مجوز حذف پیش فاکتور مربوط به کاربر دیگر', 5, 11, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30174, 511, N'مجوز ویرایش پیش فاکتور مربوط به کاربر دیگر', 5, 12, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30179, 1058, N'صدور سند حسابداری به تاریخ غیر جاری', 65, 9, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30180, 1048, N'دسترسی به گروه کالا', 63, 11, N'تنظیمات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30181, 656, N'مجوز ثبت تخفیف در دریافت', 11, 6, N'دریافت مطالبات')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30182, 512, N'نمایش موجودی انبار جاری', 5, 13, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30183, 513, N'نمایش موجودی همه انبارها', 5, 14, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30184, 676, N'مجوز تخفیف در پرداخت', 12, 6, N'پرداخت بدهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30185, 122, N'مجوز ثبت تخفیف برای هر قلم کالا', 1, 23, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30186, 514, N'مجوز ثبت تخفیف برای هر قلم کالا', 5, 15, N'پیش فاکتور')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30187, 1216, N'تولید', 67, 1, N'تولید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30188, 1217, N'حذف در تولید', 67, 2, N'تولید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30189, 1218, N'ویرایش در تولید', 67, 3, N'تولید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30190, 1219, N'فرمول تولید', 67, 4, N'تولید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30191, 1220, N'حذف در فرمول تولید', 67, 5, N'تولید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30192, 1221, N'ویرایش در فرمول تولید', 67, 6, N'تولید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30193, 1059, N'گزارش لیستی اسناد', 65, 10, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30194, 1060, N'مرورحسابها', 65, 11, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30195, 1061, N'ترازآزمایشی', 65, 12, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30196, 1062, N'ترازنامه', 65, 13, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30197, 1063, N'جستجو در اسناد', 65, 14, N'مالی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30199, 712, N'مرور هزینه ها', 14, 3, N'ثبت هزینه ها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30200, 713, N'گزارش لیستی هزینه ها', 14, 4, N'ثبت هزینه ها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30201, 704, N'گزارش لیستی درآمد', 13, 5, N'ثبت درآمدها')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30202, 123, N'گزارش لیستی فروش', 1, 24, N'فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30203, 310, N'گزارش لیستی برگشت از فروش', 3, 11, N'برگشت از فروش')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30205, 210, N'گزارش لیستی خرید', 2, 11, N'خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30206, 408, N'گزارش لیستی برگشت از خرید', 4, 9, N'برگشت از خرید')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30207, 645, N'گزارش لیستی بین انبار', 9, 6, N'حواله گروهی')
GO
INSERT [dbo].[tblListeDastrasi] ([ID], [fldCode], [fldName], [fldGroup], [fldOrder], [fldNameGroup]) VALUES (30208, 721, N'گزارش لیستی انتقال وجه', 15, 2, N'انتقال وجه')
GO
SET IDENTITY_INSERT [dbo].[tblListeDastrasi] OFF
GO












SET IDENTITY_INSERT [dbo].[tblVersionInfo] ON 

INSERT [dbo].[tblVersionInfo] ([ID], [fldVersion], [fldTozihat]) VALUES (1, 7.21, N'قابليتهاي جديد اضافه شده در اين نسخه نسبت به نسخه قبل


1-توسعه حواله گروهی
2-توسعه گزارش فاکتورهای تسویه نشده
3-نمودار واریز ویزیتورها
4-طرح تخفیفات مبلغی
5-طرح تخفیفات حجمی
6-گزارش تسویه حواله گروهی
7-پورسانت هدفمند یا طبقاتی
8-گزارش پورسانت هدفمند
9-توسعه مسیر حرکت ویزیتور
10-فیلمهای آموزشی
11-منوساز اختصاصی')
SET IDENTITY_INSERT [dbo].[tblVersionInfo] OFF
/****** Object:  StoredProcedure [dbo].[aaaa]    Script Date: 09/14/2018 07:04:54 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



Delete from tblMenuMaster
GO

SET IDENTITY_INSERT [dbo].[tblMenuMaster] ON 

GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (242, N'اتصال به جی اس ام مودم', N'connectgsm')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (278, N'ارسال پیام به ویزیتور', N'sendnotification')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (162, N'اشانتیون گروهی کالا', N'eshantiongoroohi')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (247, N'اصلاح کلی فاکتورها', N'eslahekoliefactorha')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (254, N'انبارگردانی', N'moghayeratanbar')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (151, N'انتقال کالا بین انبار', N'enteghalekala')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (187, N'انتقال وجه', N'enteghalevajh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (239, N'بازیابی اطلاعات', N'restor')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (176, N'برگشت از خرید', N'bargashtazkharid')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (173, N'برگشت از فروش', N'bargashtazforoosh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (164, N'برگشت اسناد قطعی شده', N'bargashteghatei')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (204, N'برگشت چک دریافتی', N'bargashtecheck')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (280, N'برگشت چک فروخته شده', N'BargashteCheckeVagozarShode')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (181, N'پرداخت', N'pardakht')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (238, N'پشتیبانی اطلاعات', N'backup')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (160, N'پورسانت طبقاتی فروش', N'poorsantetabaghatiforoosh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (161, N'پورسانت طبقاتی واریز', N'poorsantetabaghativariz')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (174, N'پیش فاکتور', N'pishfactor')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (157, N'تخفیفات حجمی', N'takhfifatehajmi')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (156, N'تخفیفات مبلغی', N'takhfifatemablaghi')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (288, N'تخفیفات نقدی', N'takhfifatenaghdi')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (265, N'ترازنامه', N'tarazname')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (243, N'تنظیمات چاپ', N'tanzimatechap')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (244, N'تنظیمات خرید و فروش', N'kharidvaforoosh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (153, N'تولید', N'tolid')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (201, N'ثبت پاس چکهای خودمان', N'sabtepasecheck')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (183, N'ثبت درآمد', N'sabtedaramad')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (261, N'ثبت وام', N'vam')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (203, N'ثبت وصول چک', N'sabtevosoolecheck')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (185, N'ثبت هزینه', N'sabtehazine')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (283, N'جستجو در اسناد', N'jostojoo')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (196, N'چاپ چک', N'chapecheck')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (256, N'چاپ سند', N'chapesanad')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (248, N'چند قیمته دینامیک', N'multifeedynamic')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (170, N'حذف اسناد', N'deleteasnad')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (177, N'حواله گروهی-توزیع', N'havalegoroohi')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (180, N'دریافت', N'daryaft')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (279, N'دسترسی به گروه اشخاص', N'DastrasigroupAshkhas')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (264, N'دسترسی به گروه کالا', N'dastrasibegroupkala')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (168, N'دفتر روزنامه', N'daftareroozname')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (167, N'دفتر کل', N'daftekol')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (226, N'دفترچه تلفن', N'daftarchetelephon')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (178, N'سفارشات ویزیتورها', N'sefareshatevizitor')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (171, N'سند افتتاحیه', N'eftetahie')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (195, N'سند حسابداری', N'sanadehesabdari')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (249, N'شرح چند قیمته دینامیک', N'sharhemultifee')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (166, N'شرکت جدید', N'newcompany')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (212, N'صورتحساب اشخاص', N'soorathesabeashkhas')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (245, N'طراحی بارکد', N'barcode')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (251, N'طرح تخفیفات حجمی', N'takhfifatehajmi')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (289, N'طرح تخفیفات حجمی گروهی کالا', N'tarhetakhfifatehajmigoroohiekala')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (252, N'طرح تخفیفات مبلغی', N'takhfifatemablaghi')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (237, N'طول حسابهای پایه', N'toolepaye')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (208, N'عملکرد تفصیلی', N'almalkardetafzili')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (163, N'عملکرد مراکز', N'amalkardemarakez')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (281, N'عودت چک فروخته شه', N'OdateCheck')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (175, N'فاکتور خرید', N'kharid')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (206, N'فاکتورهای تسویه نشده', N'factorhayetasfienashode')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (154, N'فرمول تولید', N'formooletolid')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (172, N'فروش', N'foroosh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (215, N'کاردکس کالا', N'kardeks')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (165, N'کنترل اسناد', N'controleasnad')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (216, N'گردش کالا', N'gardeshekala')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (285, N'گزارش اقساط معوقه', N'reportaghsatmoavaghe')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (200, N'گزارش بانک', N'reportbank')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (260, N'گزارش به تفکیک گروه کالا (برند)', N'betafkikeberand')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (155, N'گزارش پورسانت هدفمند', N'reportpoorsanthadafmand')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (284, N'گزارش جامع اقساط', N'reportjameaghsat')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (192, N'گزارش جامع برگشت از خرید', N'reportbargashtazkharid')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (193, N'گزارش جامع برگشت از فروش', N'reportbargashtazforoosh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (263, N'گزارش جامع پرداخت', N'reportpardakht')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (191, N'گزارش جامع خرید', N'reportkharid')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (262, N'گزارش جامع دریافت', N'reportdaryaft')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (188, N'گزارش جامع فروش', N'reportforoosh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (198, N'گزارش چکهای پرداختی', N'reportcheckpardakhti')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (197, N'گزارش چکهای دریافتی', N'reportcheckdaryafti')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (253, N'گزارش خالص فروش', N'reportkhalesforoosh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (184, N'گزارش درآمد', N'reportdaramad')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (250, N'گزارش سود کالا', N'reportsoodekala')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (277, N'گزارش سود کالا به تفکیک برند(گروه کالا)-فایفو', N'reportsoodgroupkalafifo')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (194, N'گزارش سود و زیان', N'soodozian')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (182, N'گزارش صندوق', N'reportsandoogh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (213, N'گزارش عملکرد تفصیلی مراکز', N'reportamalkardetafziliemarakez')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (149, N'گزارش عملکرد ویزیتور2', N'reportamalkardevizitor2')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (257, N'گزارش عملکرد ویزیتورها', N'reportamalkardevizitor')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (218, N'گزارش لیست اشخاص', N'listeashkhas')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (272, N'گزارش لیست انتقال کالا بین انبار', N'reportlistenteghalekala')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (276, N'گزارش لیستی اسناد', N'reportlistasnad')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (275, N'گزارش لیستی انتقال وجه', N'reportlistenteghalevajh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (268, N'گزارش لیستی برگشت از خرید', N'reportlistbargashtazkharid')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (270, N'گزارش لیستی برگشت از فروش', N'reportlistbargashtazforoosh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (271, N'گزارش لیستی پیش فاکتور', N'reportlistpishfactor')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (267, N'گزارش لیستی خرید', N'reportlistkharid')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (273, N'گزارش لیستی درآمد', N'reportlistdaramad')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (269, N'گزارش لیستی فروش', N'reportlistforoosh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (274, N'گزارش لیستی هزینه', N'reportlisthazine')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (214, N'گزارش موجودی انبار', N'reportanbar')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (186, N'گزارش هزینه', N'reporthazine')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (255, N'لیست اشخاص فاکتور نشده', N'reportlisteashkhasefactornashode')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (210, N'لیست بدهکاران', N'listebedehkaran')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (211, N'لیست بستانکاران', N'listebestankaran')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (217, N'لیست کالا', N'listekala')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (225, N'مانده ابتدای دوره اشخاص', N'mandeebtedaashkhas')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (209, N'مانده حساب اشخاص', N'mandehesaeashkhas')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (241, N'محدودیت دسترسی تبلت', N'dastresietablet')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (246, N'مرتب سازی فاکتورها به ترتیب تاریخ', N'moratabsazifactortarikh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (169, N'مرور حسابها', N'moroorehesabha')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (259, N'مرور هزینه ها', N'moroorehazineha')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (179, N'مسیر حرکت ویزیتور', N'masireharekatevizitor')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (227, N'معرفی اشخاص', N'moarefieashkhas')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (234, N'معرفی انبار', N'anbar')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (232, N'معرفی بانک ما', N'bankema')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (233, N'معرفی بانک مشتری', N'bankemoshtari')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (230, N'معرفی تفصیلی', N'moarefietafzili')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (258, N'معرفی چاپگر', N'moarefiechapgar')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (158, N'معرفی حسابهای کل و معین', N'kolmoin')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (236, N'معرفی درآمدها', N'moarefiedaramadha')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (205, N'معرفی دسته چک', N'dastecheck')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (229, N'معرفی راننده', N'moarefieranande')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (159, N'معرفی سوپر وایزر', N'supervizor')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (231, N'معرفی صندوق', N'moarefiesandoogh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (240, N'معرفی کاربر', N'moarefiekarbar')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (222, N'معرفی کالا', N'moarefiekala')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (228, N'معرفی گروه اشخاص', N'groupashkhas')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (223, N'معرفی گروه کالا', N'groupkala')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (224, N'معرفی ویزیتور', N'vizitor')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (235, N'معرفی هزینه ها', N'moarefiehazineha')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (221, N'موجودی ابتدای دوره', N'mojoodieebtedayedooreanbar')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (286, N'موقعیت مکانی مشتریان روی نقشه', N'locationmoshtari')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (282, N'نرخنامه - لیست کالا چندقیمته دینامیک', N'chapkalaMulti')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (266, N'نرم افزار مالی', N'mali')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (189, N'نمودار فروش', N'nemoodareforoosh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (190, N'نمودار مقایسه ای ویزیتورها', N'nemoodaremoghayeseeivizitorha')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (152, N'نمودار واریزی ویزیتورها', N'nemoodarevarizevizitor')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (220, N'واحد اندازه گیری', N'vahedeandazegiri')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (202, N'واگذاری چک به بانک', N'vagozariecheck')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (150, N'وضعیت تسویه حواله', N'vaziatetasfiehavale')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (219, N'هشدار سررسید تاریخ', N'lalarmetarikh')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (199, N'هشدار سررسید چک', N'alarmecheck')
GO
INSERT [dbo].[tblMenuMaster] ([ID], [fldNameFarsi], [fldNameEnglish]) VALUES (207, N'هشدار سررسید فاکتورهای تسویه نشده', N'alarmsarresiefactor')
GO
SET IDENTITY_INSERT [dbo].[tblMenuMaster] OFF
GO








create TRIGGER [dbo].[tgDeleteOnlineBargashtAzForoosh]
   on [dbo].[tblBargashtAzForoosh]
after delete 

AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


	 Begin Try	
				declare	@fldCode bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='BargashtAzForoosh' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

					
				select @fldCode=fldShomareFactorForoosh from  deleted

				if @fldCode is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCode,'BargashtAzForoosh')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end



    -- Insert statements for trigger here
end catch
END




/****** Object:  Trigger [dbo].[tgInsertUpdateOnlineBargashtAzForoosh]    Script Date: 12/9/2019 11:44:13 AM ******/
SET ANSI_NULLS ON
GO
ALTER TABLE [dbo].[tblBargashtAzForoosh] ENABLE TRIGGER [tgDeleteOnlineBargashtAzForoosh]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlineBargashtAzForoosh]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgInsertUpdateOnlineBargashtAzForoosh]
on [dbo].[tblBargashtAzForoosh]
   AFTER  insert ,Update
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	declare @Code bigint
	Select @Code=fldShomareFactorForoosh from inserted
	update tblBargashtAzForoosh Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldShomareFactorForoosh=@Code

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@Code and fldtype='BargashtAzForoosh'
		--end
END
GO
ALTER TABLE [dbo].[tblBargashtAzForoosh] ENABLE TRIGGER [tgInsertUpdateOnlineBargashtAzForoosh]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnlineBargashtAzKharid]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgDeleteOnlineBargashtAzKharid]
   on [dbo].[tblBargashtAzKharid]
after delete 

AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


	 Begin Try	
				declare	@fldCode bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='TblBargashtAzKharid' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

					
				select @fldCode=fldShomareFactorKharid from  deleted

				if @fldCode is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCode,'TblBargashtAzKharid')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end



    -- Insert statements for trigger here
end catch
END


GO
ALTER TABLE [dbo].[tblBargashtAzKharid] ENABLE TRIGGER [tgDeleteOnlineBargashtAzKharid]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlineBargashtAzKharid]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgInsertUpdateOnlineBargashtAzKharid]
on [dbo].[tblBargashtAzKharid]
   AFTER  insert ,Update
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	declare @Code bigint
	Select @Code=fldShomareFactorKharid from inserted
	update tblBargashtAzKharid Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldShomareFactorKharid=@Code

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@Code and fldtype='TblBargashtAzKharid'
		--end
END
GO
ALTER TABLE [dbo].[tblBargashtAzKharid] ENABLE TRIGGER [tgInsertUpdateOnlineBargashtAzKharid]
GO


/****** Object:  Trigger [dbo].[tgDeleteDaftarcheTellOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE trigger [dbo].[tgDeleteDaftarcheTellOnline]
on [dbo].[tblDaftarcheTell]
after delete 
	as


	--Begin Transaction InsertRow
	Begin Try	

			declare	@fldCode bigint,@cnt bigint
			declare @Shomare nvarchar(50)
			set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='daftarchetell' )
	
			if @cnt is null
				set @cnt=1
			else
				set @cnt+=1



			select @fldCode=fldCodeTafzili from  deleted
			select @Shomare=fldShomare from  deleted
			--insert into tblTest7(fld2)
			--	values(@fldCode)
			if @Shomare is not null
				insert into tblDeleteList(id,fldCode,fldtype,fldShomare)
					values(@cnt,@fldCode,'daftarchetell',@Shomare)

end try
Begin Catch
--if @@TRANCOUNT > 0 
--	begin 
--		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
--	end

End Catch
GO
ALTER TABLE [dbo].[tblDaftarcheTell] ENABLE TRIGGER [tgDeleteDaftarcheTellOnline]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdatedaftarchetellOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgInsertUpdatedaftarchetellOnline]
on [dbo].[tblDaftarcheTell]
after  insert ,Update
	as

	declare @Code bigint
	Select @Code=fldCodeTafzili from inserted
	update tblDaftarcheTell Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldCodeTafzili=@Code


	delete tblDeletelist where fldCode=@Code and fldtype='daftarchetell'
		--end
GO
ALTER TABLE [dbo].[tblDaftarcheTell] ENABLE TRIGGER [tgInsertUpdatedaftarchetellOnline]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnlineDaryaft]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgDeleteOnlineDaryaft]
on [dbo].[tblDaryaft]
after delete 
	as
	--IF not EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	--		CREATE TABLE [dbo].[tblKalaOnline](
	--			[fldCodeKala] [bigint] NOT NULL,
	--			[id] [bigint] NOT NULL
	--			)

	--	end

	   --Begin Transaction InsertRow
	      Begin Try	
				declare	@fldCodeKala bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='Daryaft' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

				select @fldCodeKala=fldShomareDaryaft from  deleted

				if @fldCodeKala is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCodeKala,'Daryaft')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end

End Catch



GO
ALTER TABLE [dbo].[tblDaryaft] ENABLE TRIGGER [tgDeleteOnlineDaryaft]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlineDaryaft]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgInsertUpdateOnlineDaryaft]
on [dbo].[tblDaryaft]
after  insert ,Update
	as


	--IF NOT EXISTS(
	--  SELECT *
	--	  FROM INFORMATION_SCHEMA.COLUMNS
	--	  WHERE 
	--		TABLE_NAME = 'tblKala'
	--		AND COLUMN_NAME = 'fldDateUpdate')
	--	BEGIN
	--	  ALTER TABLE tblKala
	--		ADD fldDateUpdate DateTime
	--	END;

	declare @CodeKala bigint
	Select @CodeKala=fldShomareDaryaft from inserted
	update [tblDaryaft]  Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldShomareDaryaft=@CodeKala

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@CodeKala and fldtype='Daryaft'
		--end
GO
ALTER TABLE [dbo].[tblDaryaft] ENABLE TRIGGER [tgInsertUpdateOnlineDaryaft]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnlineForoosh]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgDeleteOnlineForoosh]
   on [dbo].[tblForoosh]
after delete 

AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


	 Begin Try	
				declare	@fldCode bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='Foroosh' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

					
				select @fldCode=fldShomareFactorForoosh from  deleted

				if @fldCode is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCode,'Foroosh')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end



    -- Insert statements for trigger here
end catch
END



GO
ALTER TABLE [dbo].[tblForoosh] ENABLE TRIGGER [tgDeleteOnlineForoosh]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlineForoosh]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgInsertUpdateOnlineForoosh]
on [dbo].[tblForoosh]
   AFTER  insert ,Update
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	declare @Code bigint
	Select @Code=fldShomareFactorForoosh from inserted
	update tblForoosh Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldShomareFactorForoosh=@Code

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@Code and fldtype='Foroosh'
		--end
END
GO
ALTER TABLE [dbo].[tblForoosh] ENABLE TRIGGER [tgInsertUpdateOnlineForoosh]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnlineGroupAshkhase]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgDeleteOnlineGroupAshkhase]
   on [dbo].[tblGroupAshkhas]
after delete 

AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


	 Begin Try	
				declare	@fldCode bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='GroupAshkhas' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

				select @fldCode=fldGroupId from  deleted

				if @fldCode is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCode,'GroupAshkhas')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end



    -- Insert statements for trigger here
end catch
END


GO
ALTER TABLE [dbo].[tblGroupAshkhas] ENABLE TRIGGER [tgDeleteOnlineGroupAshkhase]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlineGroupAshkhas]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgInsertUpdateOnlineGroupAshkhas]
on [dbo].[tblGroupAshkhas]
   AFTER  insert ,Update
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	declare @Code bigint
	Select @Code=fldGroupId from inserted
	update tblGroupAshkhas Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldGroupId=@Code

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@Code and fldtype='GroupAshkhas'
		--end
END
GO
ALTER TABLE [dbo].[tblGroupAshkhas] ENABLE TRIGGER [tgInsertUpdateOnlineGroupAshkhas]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnlineGroupKala]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgDeleteOnlineGroupKala]
   on [dbo].[tblGroupKala]
after delete 

AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


	 Begin Try	
				declare	@fldCode bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='GroupKala' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

				select @fldCode=fldGroupId from  deleted

				if @fldCode is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCode,'GroupKala')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end



    -- Insert statements for trigger here
end catch
END

GO
ALTER TABLE [dbo].[tblGroupKala] ENABLE TRIGGER [tgDeleteOnlineGroupKala]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlineGroupKala]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgInsertUpdateOnlineGroupKala]
on [dbo].[tblGroupKala]
   AFTER  insert ,Update
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	declare @code bigint
	Select @code=fldGroupId from inserted
	update tblGroupKala Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldGroupId=@code

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@code and fldtype='GroupKala'
		--end
END
GO
ALTER TABLE [dbo].[tblGroupKala] ENABLE TRIGGER [tgInsertUpdateOnlineGroupKala]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE trigger [dbo].[tgDeleteOnline]
on [dbo].[tblKala]
after delete 
	as
	--IF not EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	--		CREATE TABLE [dbo].[tblKalaOnline](
	--			[fldCodeKala] [bigint] NOT NULL,
	--			[id] [bigint] NOT NULL
	--			)

	--	end

	   --Begin Transaction InsertRow
	      Begin Try	
				declare	@fldCodeKala bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='kala' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

				select @fldCodeKala=fldCodeKala from  deleted

				if @fldCodeKala is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCodeKala,'kala')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end

End Catch
GO
ALTER TABLE [dbo].[tblKala] ENABLE TRIGGER [tgDeleteOnline]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE trigger [dbo].[tgInsertUpdateOnline]
on [dbo].[tblKala]
after  insert ,Update
	as


	--IF NOT EXISTS(
	--  SELECT *
	--	  FROM INFORMATION_SCHEMA.COLUMNS
	--	  WHERE 
	--		TABLE_NAME = 'tblKala'
	--		AND COLUMN_NAME = 'fldDateUpdate')
	--	BEGIN
	--	  ALTER TABLE tblKala
	--		ADD fldDateUpdate DateTime
	--	END;

	declare @CodeKala bigint
	Select @CodeKala=fldCodeKala from inserted
	update tblKala Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldCodeKala=@CodeKala

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@CodeKala and fldtype='kala'
		--end
GO
ALTER TABLE [dbo].[tblKala] ENABLE TRIGGER [tgInsertUpdateOnline]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnlineKharid]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgDeleteOnlineKharid]
   on [dbo].[tblKharid]
after delete 

AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


	 Begin Try	
				declare	@fldCode bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='Kharid' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

					
				select @fldCode=fldShomareFactorKharid from  deleted

				if @fldCode is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCode,'Kharid')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end



    -- Insert statements for trigger here
end catch
END



GO
ALTER TABLE [dbo].[tblKharid] ENABLE TRIGGER [tgDeleteOnlineKharid]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlineKharid]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgInsertUpdateOnlineKharid]
on [dbo].[tblKharid]
   AFTER  insert ,Update
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	declare @Code bigint
	Select @Code=fldShomareFactorKharid from inserted
	update tblKharid Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldShomareFactorKharid=@Code

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@Code and fldtype='Kharid'
		--end
END
GO
ALTER TABLE [dbo].[tblKharid] ENABLE TRIGGER [tgInsertUpdateOnlineKharid]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnlineMultiFeeDynamic]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgDeleteOnlineMultiFeeDynamic]
on [dbo].[tblMultiFeeDynamic]
after delete 
	as
	--IF not EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	--		CREATE TABLE [dbo].[tblKalaOnline](
	--			[fldCodeKala] [bigint] NOT NULL,
	--			[id] [bigint] NOT NULL
	--			)

	--	end

	   --Begin Transaction InsertRow
	      Begin Try	

				declare	@fldCode bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='MultiFeeDynamic' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

				select @fldCode=(CONVERT(nvarchar,fldCodeSharh) + CONVERT(nvarchar,fldCodeKala))  from  deleted

				if @fldCode is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCode,'MultiFeeDynamic')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end

End Catch



GO
ALTER TABLE [dbo].[tblMultiFeeDynamic] ENABLE TRIGGER [tgDeleteOnlineMultiFeeDynamic]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlineMultiFeeDynamic]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgInsertUpdateOnlineMultiFeeDynamic]
on [dbo].[tblMultiFeeDynamic]
after  insert ,Update
	as


	--IF NOT EXISTS(
	--  SELECT *
	--	  FROM INFORMATION_SCHEMA.COLUMNS
	--	  WHERE 
	--		TABLE_NAME = 'tblKala'
	--		AND COLUMN_NAME = 'fldDateUpdate')
	--	BEGIN
	--	  ALTER TABLE tblKala
	--		ADD fldDateUpdate DateTime
	--	END;

	declare @Code bigint
	Select @Code= (CONVERT(nvarchar,fldCodeSharh) + CONVERT(nvarchar,fldCodeKala)) from inserted
	update [tblMultiFeeDynamic] Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) 
	where (CONVERT(nvarchar,fldCodeSharh) + CONVERT(nvarchar,fldCodeKala)) = @Code

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@Code and fldtype='MultiFeeDynamic'
		--end
GO
ALTER TABLE [dbo].[tblMultiFeeDynamic] ENABLE TRIGGER [tgInsertUpdateOnlineMultiFeeDynamic]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnlinePardakht]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgDeleteOnlinePardakht]
on [dbo].[tblPardakht]
after delete 
	as
	--IF not EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	--		CREATE TABLE [dbo].[tblKalaOnline](
	--			[fldCodeKala] [bigint] NOT NULL,
	--			[id] [bigint] NOT NULL
	--			)

	--	end

	   --Begin Transaction InsertRow
	      Begin Try	
				declare	@fldCodeKala bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='Pardakht' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

				select @fldCodeKala=fldShomarePardakht from  deleted

				if @fldCodeKala is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCodeKala,'Pardakht')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end

End Catch


GO
ALTER TABLE [dbo].[tblPardakht] ENABLE TRIGGER [tgDeleteOnlinePardakht]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlinePardakht]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgInsertUpdateOnlinePardakht]
on [dbo].[tblPardakht]
after  insert ,Update
	as


	--IF NOT EXISTS(
	--  SELECT *
	--	  FROM INFORMATION_SCHEMA.COLUMNS
	--	  WHERE 
	--		TABLE_NAME = 'tblKala'
	--		AND COLUMN_NAME = 'fldDateUpdate')
	--	BEGIN
	--	  ALTER TABLE tblKala
	--		ADD fldDateUpdate DateTime
	--	END;

	declare @CodeKala bigint
	Select @CodeKala=fldShomarePardakht from inserted
	update tblPardakht  Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldShomarePardakht=@CodeKala

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@CodeKala and fldtype='Pardakht'
		--end
GO
ALTER TABLE [dbo].[tblPardakht] ENABLE TRIGGER [tgInsertUpdateOnlinePardakht]
GO
/****** Object:  Trigger [dbo].[tgDeleteRizeBargastAzForooshOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE trigger [dbo].[tgDeleteRizeBargastAzForooshOnline]
on [dbo].[tblRizeBargashtAzForoosh]
after delete 
	as

	--Begin Transaction InsertRow
	--Begin Try	

	declare	@ShomareForoosh bigint,@cnt bigint

	set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='rizebargashtazforoosh' )
	
	if @cnt is null
		set @cnt=1
	else
		set @cnt+=1

	select @ShomareForoosh=fldShomareFactor from  deleted

	if @ShomareForoosh is not null
		insert into tblDeleteList(id,fldCode,fldtype)
			values(@cnt,@ShomareForoosh,'rizebargashtazforoosh')

--end try
--Begin Catch
----if @@TRANCOUNT > 0 
--	begin 
--		--RollBack Transaction InsertRow;
--		declare @m bigint
--		set @m=1
--	end

--End Catch
GO
ALTER TABLE [dbo].[tblRizeBargashtAzForoosh] ENABLE TRIGGER [tgDeleteRizeBargastAzForooshOnline]
GO
/****** Object:  Trigger [dbo].[tgInsertRizeBargashtAzForooshOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgInsertRizeBargashtAzForooshOnline]
on [dbo].[tblRizeBargashtAzForoosh]
after  insert ,Update
	as


	declare @ShomareForoosh bigint
	Select @ShomareForoosh=fldShomareFactor from inserted
	update tblRizeBargashtAzForoosh Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldShomareFactor=@ShomareForoosh

	delete tblDeletelist where fldCode=@ShomareForoosh and fldtype='rizebargashtazforoosh'
		--end
GO
ALTER TABLE [dbo].[tblRizeBargashtAzForoosh] ENABLE TRIGGER [tgInsertRizeBargashtAzForooshOnline]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnlineRizeBargashtAzKharid]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgDeleteOnlineRizeBargashtAzKharid]
   on [dbo].[tblRizeBargashtAzKharid]
after delete 

AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


	 Begin Try	
				declare	@fldCode bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='BargashtAzKharid' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

					
				select @fldCode=fldShomareFactor from  deleted

				if @fldCode is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCode,'BargashtAzKharid')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end



    -- Insert statements for trigger here
end catch
END


GO
ALTER TABLE [dbo].[tblRizeBargashtAzKharid] ENABLE TRIGGER [tgDeleteOnlineRizeBargashtAzKharid]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlineRizeBargashtAzKharid]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create TRIGGER [dbo].[tgInsertUpdateOnlineRizeBargashtAzKharid]
on [dbo].[tblRizeBargashtAzKharid]
   AFTER  insert ,Update
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	declare @Code bigint
	Select @Code=fldShomareFactor from inserted
	update [tblRizeBargashtAzKharid] Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldShomareFactor=@Code

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@Code and fldtype='BargashtAzKharid'
		--end
END
GO
ALTER TABLE [dbo].[tblRizeBargashtAzKharid] ENABLE TRIGGER [tgInsertUpdateOnlineRizeBargashtAzKharid]
GO
/****** Object:  Trigger [dbo].[tgDeleteRizeForooshOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE trigger [dbo].[tgDeleteRizeForooshOnline]
on [dbo].[tblRizeForoosh]
after delete 
	as

	--Begin Transaction InsertRow
	Begin Try	

	declare	@ShomareForoosh bigint,@cnt bigint

	set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='rizeforoosh' )
	
	if @cnt is null
		set @cnt=1
	else
		set @cnt+=1

	select @ShomareForoosh=fldShomareFactor from  deleted

	if @ShomareForoosh is not null
		insert into tblDeleteList(id,fldCode,fldtype)
			values(@cnt,@ShomareForoosh,'rizeforoosh')

end try
Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end

End Catch
GO
ALTER TABLE [dbo].[tblRizeForoosh] ENABLE TRIGGER [tgDeleteRizeForooshOnline]
GO
/****** Object:  Trigger [dbo].[tgInsertRizeForooshOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgInsertRizeForooshOnline]
on [dbo].[tblRizeForoosh]
after  insert ,Update
	as


	declare @ShomareForoosh bigint
	Select @ShomareForoosh=fldShomareFactor from inserted
	update tblRizeForoosh Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldShomareFactor=@ShomareForoosh

	delete tblDeletelist where fldCode=@ShomareForoosh and fldtype='rizeforoosh'
		--end
GO
ALTER TABLE [dbo].[tblRizeForoosh] ENABLE TRIGGER [tgInsertRizeForooshOnline]
GO
/****** Object:  Trigger [dbo].[tgDeleteRizekharidOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE trigger [dbo].[tgDeleteRizekharidOnline]
on [dbo].[tblRizeKharid]
after delete 
	as


	--Begin Transaction InsertRow
	Begin Try	
		declare	@ShomareForoosh bigint,@cnt bigint

		set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='rizekharid' )
	
		if @cnt is null
			set @cnt=1
		else
			set @cnt+=1

		select @ShomareForoosh=fldShomareFactor from  deleted

		if @ShomareForoosh is not null
			insert into tblDeleteList(id,fldCode,fldtype)
				values(@cnt,@ShomareForoosh,'rizekharid')

end try
Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end

End Catch
GO
ALTER TABLE [dbo].[tblRizeKharid] ENABLE TRIGGER [tgDeleteRizekharidOnline]
GO
/****** Object:  Trigger [dbo].[tgInsertRizekharidOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgInsertRizekharidOnline]
on [dbo].[tblRizeKharid]
after  insert ,Update
	as


	declare @ShomareForoosh bigint
	Select @ShomareForoosh=fldShomareFactor from inserted
	update tblRizekharid Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldShomareFactor=@ShomareForoosh

	delete tblDeletelist where fldCode=@ShomareForoosh and fldtype='rizekharid'
		--end
GO
ALTER TABLE [dbo].[tblRizeKharid] ENABLE TRIGGER [tgInsertRizekharidOnline]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnlineSharheFeeDynamic]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgDeleteOnlineSharheFeeDynamic]
on [dbo].[tblSharheFeeDynamic]
after delete 
	as
	--IF not EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	--		CREATE TABLE [dbo].[tblKalaOnline](
	--			[fldCodeKala] [bigint] NOT NULL,
	--			[id] [bigint] NOT NULL
	--			)

	--	end

	   --Begin Transaction InsertRow
	      Begin Try	

				declare	@fldCode bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='SharheFeeDynamic' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

				select @fldCode=fldCodeSharh from  deleted
				
				--delete tblTest7
				--INSERT        iNTO              tblTest7( fld2)
				--VALUES        (@fldCode)		

				if @fldCode is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCode,'SharheFeeDynamic')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end

End Catch


GO
ALTER TABLE [dbo].[tblSharheFeeDynamic] ENABLE TRIGGER [tgDeleteOnlineSharheFeeDynamic]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlineSharheFeeDynamic]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgInsertUpdateOnlineSharheFeeDynamic]
on [dbo].[tblSharheFeeDynamic]
after  insert ,Update
	as


	--IF NOT EXISTS(
	--  SELECT *
	--	  FROM INFORMATION_SCHEMA.COLUMNS
	--	  WHERE 
	--		TABLE_NAME = 'tblKala'
	--		AND COLUMN_NAME = 'fldDateUpdate')
	--	BEGIN
	--	  ALTER TABLE tblKala
	--		ADD fldDateUpdate DateTime
	--	END;

	declare @code bigint
	Select @code= fldCodeSharh from inserted
	update [tblSharheFeeDynamic] Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldCodeSharh =@code

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@code and fldtype='SharheFeeDynamic'
		--end
GO
ALTER TABLE [dbo].[tblSharheFeeDynamic] ENABLE TRIGGER [tgInsertUpdateOnlineSharheFeeDynamic]
GO
/****** Object:  Trigger [dbo].[tgDeleteSanadOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE trigger [dbo].[tgDeleteSanadOnline]
on [dbo].[tblSodooreSanad]
after delete 
	as

	--Begin Transaction InsertRow
	Begin Try	
			declare	@Sanad bigint,@cnt bigint

			set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='sanad' )
	
			if @cnt is null
				set @cnt=1
			else
				set @cnt+=1

			select @Sanad=fldSanad from  deleted

			if @Sanad is not null
				insert into tblDeleteList(id,fldCode,fldtype)
					values(@cnt,@Sanad,'sanad')

	end try
Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end

End Catch
GO
ALTER TABLE [dbo].[tblSodooreSanad] ENABLE TRIGGER [tgDeleteSanadOnline]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateSodooreSanadOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgInsertUpdateSodooreSanadOnline]
on [dbo].[tblSodooreSanad]
after  insert ,Update
	as


	declare @Sanad bigint
	Select @Sanad=fldSanad from inserted
	update tblSodooreSanad Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldSanad=@Sanad

	delete tblDeletelist where fldCode=@Sanad and fldtype='sanad'
		--end
GO
ALTER TABLE [dbo].[tblSodooreSanad] ENABLE TRIGGER [tgInsertUpdateSodooreSanadOnline]
GO
/****** Object:  Trigger [dbo].[tgDeleteTafziliOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE trigger [dbo].[tgDeleteTafziliOnline]
on [dbo].[tblTafzili]
after delete 
	as
	--IF not EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	--		CREATE TABLE [dbo].[tblKalaOnline](
	--			[fldCodeKala] [bigint] NOT NULL,
	--			[id] [bigint] NOT NULL
	--			)

	--	end

	--Begin Transaction InsertRow
	      Begin Try	
					declare	@fldCode bigint,@cnt bigint

					set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='ashkhas' )
	
					if @cnt is null
						set @cnt=1
					else
						set @cnt+=1

					select @fldCode=fldCodeTafzili from  deleted

					if @fldCode is not null
						insert into tblDeleteList(id,fldCode,fldtype)
							values(@cnt,@fldCode,'ashkhas')

			end try
Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end

End Catch
GO
ALTER TABLE [dbo].[tblTafzili] ENABLE TRIGGER [tgDeleteTafziliOnline]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateTafziliOnline]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[tgInsertUpdateTafziliOnline]
on [dbo].[tblTafzili]
after  insert ,Update
	as


	declare @Code bigint
	Select @Code=fldCodeTafzili from inserted
	update tblTafzili Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where fldCodeTafzili=@Code

	delete tblDeletelist where fldCode=@Code and fldtype='ashkhas'
		--end
GO
ALTER TABLE [dbo].[tblTafzili] ENABLE TRIGGER [tgInsertUpdateTafziliOnline]
GO
/****** Object:  Trigger [dbo].[tgDeleteOnlinetblVahed]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create TRIGGER [dbo].[tgDeleteOnlinetblVahed]
   on [dbo].[tblVahed]
after delete 

AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


	 Begin Try	
				declare	@fldCode bigint,@cnt bigint

				set @cnt=(Select count(fldCode) from tblDeleteList where fldtype='Vahed' )
	
				if @cnt is null
					set @cnt=1
				else
					set @cnt+=1

				select @fldCode=ID  from  deleted

				if @fldCode is not null
					insert into tblDeleteList(id,fldCode,fldtype)
						values(@cnt,@fldCode,'Vahed')

			End Try

Begin Catch
--if @@TRANCOUNT > 0 
	begin 
		--RollBack Transaction InsertRow;
		declare @m bigint
		set @m=1
	end



    -- Insert statements for trigger here
end catch
END
GO
ALTER TABLE [dbo].[tblVahed] ENABLE TRIGGER [tgDeleteOnlinetblVahed]
GO
/****** Object:  Trigger [dbo].[tgInsertUpdateOnlinetblVahed]    Script Date: 4/26/2020 3:08:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[tgInsertUpdateOnlinetblVahed]
on [dbo].[tblVahed]
   AFTER  insert ,Update
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	declare @code bigint
	Select @code=ID from inserted
	update tblVahed Set fldDateUpdate=(Select CONVERT(VARCHAR(19), GETDATE(), 120)) where ID=@code

	--IF  EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
 --          WHERE TABLE_NAME = N'tblKalaOnline')
	--	BEGIN
	delete tblDeletelist where fldCode=@code and fldtype='Vahed'
		--end
END
GO
ALTER TABLE [dbo].[tblVahed] ENABLE TRIGGER [tgInsertUpdateOnlinetblVahed]
GO


