ALTER TABLE dbo.QuickAccess ADD[FrmEditFactors][bit] NULL;

ALTER TABLE dbo.QuickAccess ADD[FrmUnpaidDemand][bit] NULL;

ALTER TABLE dbo.QuickAccess ADD[FrmGotPaidDemand][bit] NULL;

ALTER TABLE dbo.QuickAccess ADD[FrmAccountingBookReport][bit] NULL;

ALTER TABLE dbo.QuickAccess ADD[FrmDayBook][bit] NULL;

ALTER TABLE dbo.QuickAccess ADD[FrmWhatsUpToday][bit] NULL;

ALTER TABLE dbo.AppUserSettings ADD[ManualDiscount][bit] NULL;

ALTER TABLE dbo.AppUserSettings ADD[CofferMergeItems][bit] NULL;

ALTER TABLE dbo.Estate ADD[MeasureUnit_ID][int] NULL;

ALTER TABLE [dbo].[Estate]  WITH CHECK ADD  CONSTRAINT [FK_Estate_EstateLocation] FOREIGN KEY([EstateLocation_ID]) REFERENCES[dbo].[EstateLocation]([RowID]);

ALTER TABLE [dbo].[Estate]  WITH CHECK ADD  CONSTRAINT [FK_Estate_MeasureUnit] FOREIGN KEY([MeasureUnit_ID]) REFERENCES[dbo].[MeasureUnit]([RowID]);

ALTER TABLE dbo.EstateLocationStock ADD[Branch_ID][int] NULL;

ALTER TABLE [dbo].[EstateLocationStock]  WITH CHECK ADD  CONSTRAINT [FK_EstateLocationStock_Branch] FOREIGN KEY([Branch_ID]) REFERENCES[dbo].[Branch]([RowID]);

ALTER TABLE [dbo].[EstateLocationStock] ADD PRIMARY KEY([RowID]);

ALTER TABLE dbo.Systems ADD[AccountingSystem][bit] NULL;

ALTER TABLE dbo.Systems ADD[StoreSaleSystem][bit] NULL;

ALTER TABLE dbo.Systems ADD[IsServer][bit] NULL;

ALTER TABLE dbo.Systems ADD[UserName][nvarchar](200) NULL;

ALTER TABLE dbo.Systems ADD[Password][nvarchar](200) NULL;

ALTER TABLE dbo.AppSettings ADD[FinancialYear_ID][smallint] NULL;

ALTER TABLE dbo.AppSettings ADD[MonetaryUnitID][tinyint] NULL;

INSERT [dbo].[Fix_DocHType] ([RowID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (22, N'خرید اموال', NULL, NULL);

INSERT [dbo].[Fix_DocHType] ([RowID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (23, N'فروش اموال', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (2046, 2, N'عملیات - اموال', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (2047, 2, N'عملیات - اموال - ثبت خرید اموال', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (2048, 2, N'عملیات - اموال - ثبت فروش اموال', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (2049, 2, N'عملیات - اموال - ویرایش عملیات اموال', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (5084, 5, N'مجوز افزودن - خرید اموال', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (5085, 5, N'مجوز افزودن - فروش اموال', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (6084, 6, N'مجوز ویرایش - خرید اموال', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (6085, 6, N'مجوز ویرایش - فروش اموال', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (7084, 7, N'مجوز حذف - خرید اموال', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (7085, 7, N'مجوز حذف - فروش اموال', NULL, NULL);

UPDATE dbo.WhDocH SET Branch_ID = 1 WHERE Branch_ID IS NULL;

UPDATE dbo.WhDocD SET Branch_ID = 1 WHERE Branch_ID IS NULL;

ALTER TABLE dbo.ChequePayH ADD[FinancialYear_ID][smallint] NULL;

ALTER TABLE dbo.ChequeRecH ADD[FinancialYear_ID][smallint] NULL;

UPDATE dbo.ChequePayH SET Branch_ID = 1 WHERE Branch_ID IS NULL;

UPDATE dbo.ChequePayD SET Branch_ID = 1 WHERE Branch_ID IS NULL;

UPDATE dbo.ChequeRecH SET Branch_ID = 1 WHERE Branch_ID IS NULL;

UPDATE dbo.ChequeRecD SET Branch_ID = 1 WHERE Branch_ID IS NULL;

INSERT [dbo].[Fix_Acc2Type] ([RowID], [RowName], [RowCode], [RowDesc], [UniqueIdentifierValue]) VALUES (7, N'سرمایه‌گذار', NULL, NULL, NULL);

SET IDENTITY_INSERT dbo.Fix_EhDocType  OFF;

IF NOT EXISTS (SELECT * FROM dbo.Fix_EhDocType WHERE RowID = 3) INSERT [dbo].[Fix_EhDocType] ([RowName], [RowDesc]) VALUES (N'موجودی اول دوره اموال', NULL);

UPDATE [dbo].[ChequePayH] SET FinancialYear_ID = (SELECT MIN(RowID) FROM dbo.FinancialYear) WHERE FinancialYear_ID IS NULL;

UPDATE [dbo].[ChequeRecH] SET FinancialYear_ID = (SELECT MIN(RowID) FROM dbo.FinancialYear) WHERE FinancialYear_ID IS NULL;

ALTER TABLE dbo.EhDocH ADD[Estate_ID][int] NULL;

INSERT [dbo].[Fix_Reports] ([RowID], [RowName], [RowDesc], [sectionid], [Branch_id]) VALUES (35, N'برچسب کالا - نوع یک', N'نوع یک', 15, 1);

INSERT [dbo].[Fix_Reports] ([RowID], [RowName], [RowDesc], [sectionid], [Branch_id]) VALUES (36, N'برچسب کالا - نوع دو', N'نوع دو', 15, 1);

ALTER TABLE dbo.AppUserSettings ADD[CardReaderAutoSettlement][bit] NULL;

ALTER TABLE dbo.AppUserSettings ADD[EditFactors][bit] NULL;


UPDATE dbo.AppUserSettings SET CardReaderAutoSettlement = 'true' WHERE CardReaderAutoSettlement IS NULL;

INSERT [dbo].[Fix_Acc2Type] ([RowID], [RowName]) VALUES (8, N'کارتخوان');

CREATE TABLE [dbo].[CloseCofferH]([RowID] [int] IDENTITY(1,1) NOT NULL, [UserID] [smallint] NOT NULL, [CashSale] [money] NULL, [CardReaderSale] [money] NULL, [Branch_ID] [int] NULL, [Settled] [bit] NOT NULL, [FinancialYear_ID] [smallint] NULL, [FDateInsert] [char](10) NULL, [MDateInsert] [datetime] NULL, [FDateUpdate] [char](10) NULL, [MDateUpdate] [datetime] NULL, [FTimeInsert] [char](5) NULL, [FTimeUpdate] [char](5) NULL, CONSTRAINT [PK_CloseCofferH] PRIMARY KEY CLUSTERED  ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[CloseCofferH]  WITH CHECK ADD  CONSTRAINT [FK_CloseCofferH_Branch] FOREIGN KEY([Branch_ID]) REFERENCES [dbo].[Branch] ([RowID])

ALTER TABLE [dbo].[CloseCofferH] CHECK CONSTRAINT [FK_CloseCofferH_Branch]

ALTER TABLE [dbo].[CloseCofferH]  WITH CHECK ADD  CONSTRAINT [FK_CloseCofferH_FinancialYear] FOREIGN KEY([FinancialYear_ID]) REFERENCES [dbo].[FinancialYear] ([RowID])

ALTER TABLE [dbo].[CloseCofferH] CHECK CONSTRAINT [FK_CloseCofferH_FinancialYear]

CREATE TABLE [dbo].[CloseCofferD]([RowID] [int] IDENTITY(1,1) NOT NULL, [Branch_ID] [int] NULL, [CloseCofferH_ID] [int] NOT NULL, [POS_ID] [int] NOT NULL, [Price] [money] NOT NULL, [RowDesc] [nvarchar](500) NULL, CONSTRAINT [PK_CloseCofferD] PRIMARY KEY CLUSTERED ([RowID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]

ALTER TABLE [dbo].[CloseCofferD] WITH CHECK ADD CONSTRAINT [FK_CloseCofferD_POS] FOREIGN KEY([POS_ID]) REFERENCES [dbo].[POS] ([RowID])

ALTER TABLE [dbo].[CloseCofferD]  WITH CHECK ADD  CONSTRAINT [FK_CloseCofferD_CloseCofferH] FOREIGN KEY([CloseCofferH_ID]) REFERENCES [dbo].[CloseCofferH] ([RowID]) ON DELETE CASCADE

CREATE TABLE [dbo].[CofferSettlementH]([RowID] [int] IDENTITY(1,1) NOT NULL, [TotalCashSale] [money] NULL, [TotalCardReaderSale] [money] NULL, [TotalCashReceived] [money] NULL, [TotalCardReaderReceived] [money] NULL, [Branch_ID] [int] NULL, [FinancialYear_ID] [smallint] NULL, [FDateInsert] [char](10) NULL, [MDateInsert] [datetime] NULL, [FDateUpdate] [char](10) NULL, [MDateUpdate] [datetime] NULL, [FTimeInsert] [char](5) NULL, [FTimeUpdate] [char](5) NULL, CONSTRAINT [PK_CofferSettlementH] PRIMARY KEY CLUSTERED  ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[CofferSettlementH]  WITH CHECK ADD  CONSTRAINT [FK_CofferSettlementH_Branch] FOREIGN KEY([Branch_ID]) REFERENCES [dbo].[Branch] ([RowID])

ALTER TABLE [dbo].[CofferSettlementH] CHECK CONSTRAINT [FK_CofferSettlementH_Branch]

ALTER TABLE [dbo].[CofferSettlementH]  WITH CHECK ADD  CONSTRAINT [FK_CofferSettlementH_FinancialYear] FOREIGN KEY([FinancialYear_ID]) REFERENCES [dbo].[FinancialYear] ([RowID])

ALTER TABLE [dbo].[CofferSettlementH] CHECK CONSTRAINT [FK_CofferSettlementH_FinancialYear]

CREATE TABLE [dbo].[CofferSettlementD]([RowID] [int] IDENTITY(1,1) NOT NULL, [CofferSettlementH_ID] [int] NOT NULL, [UserID] [smallint] NOT NULL, [CashSale] [money] NULL, [CardReaderSale] [money] NULL, [CashReceived] [money] NULL, [CardReaderReceived] [money] NULL, [RowDesc] [nvarchar](500) NULL, CONSTRAINT [PK_CofferSettlementD] PRIMARY KEY CLUSTERED  ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[CofferSettlementD]  WITH CHECK ADD  CONSTRAINT [FK_CofferSettlementD_CofferSettlementH] FOREIGN KEY([CofferSettlementH_ID]) REFERENCES [dbo].[CofferSettlementH] ([RowID])

ALTER TABLE [dbo].[CofferSettlementD] CHECK CONSTRAINT [FK_CofferSettlementD_CofferSettlementH]

CREATE TABLE [dbo].[CofferSettlementPos]([RowID] [int] IDENTITY(1,1) NOT NULL, [CofferSettlementD_ID] [int] NOT NULL, [POS_ID] [int] NOT NULL, [Price] [money] NOT NULL, [RowDesc] [nvarchar](500) NULL, CONSTRAINT [PK_CofferSettlementPos] PRIMARY KEY CLUSTERED  ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[CofferSettlementPos]  WITH CHECK ADD  CONSTRAINT [FK_CofferSettlementPos_CofferSettlementD] FOREIGN KEY([CofferSettlementD_ID]) REFERENCES [dbo].[CofferSettlementD] ([RowID])

ALTER TABLE [dbo].[CofferSettlementPos] CHECK CONSTRAINT [FK_CofferSettlementPos_CofferSettlementD]

ALTER TABLE [dbo].[CofferSettlementPos]  WITH CHECK ADD  CONSTRAINT [FK_CofferSettlementPos_POS] FOREIGN KEY([POS_ID]) REFERENCES [dbo].[POS] ([RowID])

ALTER TABLE [dbo].[CofferSettlementPos] CHECK CONSTRAINT [FK_CofferSettlementPos_POS]

ALTER TABLE [dbo].[CofferSettlementH] ADD[DocNo][bigint] NULL;

ALTER TABLE [dbo].[CofferSettlementH] ADD[DocFDate][char](10) NULL;

ALTER TABLE [dbo].[CofferSettlementH] ADD[DocMDate][datetime] NULL;

ALTER TABLE [dbo].[CofferSettlementH] ADD[RowDesc][nvarchar](500) NULL;

INSERT [dbo].[Fix_Acc2Type] ([RowID], [RowName], [RowCode], [RowDesc], [UniqueIdentifierValue]) VALUES (9, N'کسر و اضافه', 3, NULL, NULL);

ALTER TABLE [dbo].[CloseCofferH] ADD[CashReturnSale][money] NULL;

ALTER TABLE [dbo].[CloseCofferH] ADD[TotalCashSale][money] NULL;

ALTER TABLE [dbo].[CofferSettlementD] ADD[CashReturnSale][money] NULL;

ALTER TABLE [dbo].[CofferSettlementD] ADD[TotalCashSale][money] NULL;

ALTER TABLE [dbo].[CloseCofferH] ADD[Coffer_ID][int] NULL;

ALTER TABLE [dbo].[CofferSettlementD] ADD[Coffer_ID][int] NULL;

INSERT [dbo].[Fix_DocHType] ([RowID], [RowName]) VALUES (24, N'تسویه صندوق');

ALTER TABLE [dbo].[CofferSettlementH] ADD[Treasury_ID][int] NULL;

ALTER TABLE [dbo].[CofferSettlementD] ADD[CloseCofferH_ID][int] NULL;

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (2050, 2, N'عملیات - امور مالی - تسویه صندوق', NULL, NULL);

ALTER TABLE [dbo].[CofferSettlementD] ADD[CloseCofferFDate][nvarchar](100) NULL;

ALTER TABLE [dbo].[ChequePayH] ADD[IsForGuarantee][bit] NULL;

CREATE VIEW [dbo].[AveragePrice] AS SELECT        dbo.WhDocD.Good_ID, CAST(SUM(dbo.WhDocD.PurchaseTotalPrice) / SUM(dbo.WhDocD.CalculatedInputValue) AS int) AS Average, dbo.WhDocH.Fix_WhDocType2_ID, dbo.WhDocD.FinancialYear_ID FROM dbo.WhDocD INNER JOIN dbo.WhDocH ON dbo.WhDocD.WhDocH_ID = dbo.WhDocH.RowID WHERE (dbo.WhDocH.Fix_WhDocType2_ID = 1) GROUP BY dbo.WhDocD.Good_ID, dbo.WhDocH.Fix_WhDocType2_ID, dbo.WhDocD.FinancialYear_ID

CREATE VIEW [dbo].[StockAllWarehouse] AS SELECT SUM(CalculatedInputValue) - SUM(CalculatedOutputValue) AS stock, Good_ID, FinancialYear_ID FROM dbo.WhDocD GROUP BY Good_ID, FinancialYear_ID

CREATE VIEW [dbo].[StockView] AS SELECT        SUM(dbo.WhDocD.CalculatedInputValue) - SUM(dbo.WhDocD.CalculatedOutputValue) AS stock, dbo.WhDocD.Good_ID, dbo.WhDocD.FinancialYear_ID, dbo.WhDocH.Warehouse_ID FROM            dbo.WhDocD INNER JOIN dbo.WhDocH ON dbo.WhDocD.WhDocH_ID = dbo.WhDocH.RowID GROUP BY dbo.WhDocD.Good_ID, dbo.WhDocD.FinancialYear_ID, dbo.WhDocH.Warehouse_ID

ALTER TABLE [dbo].[AppUserSettings] ADD[SkinName][nvarchar](100) NULL;

ALTER TABLE [dbo].[AppUserSettings] ADD[PaletteName][nvarchar](100) NULL;

ALTER TABLE [dbo].[Fix_Acc1] ADD[DefinedBySystem][bit] NOT NULL CONSTRAINT [DF_Fix_Acc1_DefinedBySystem]  DEFAULT ((1));

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (1059, 1, N'تعاریف - حساب‌ها', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (1060, 1, N'تعاریف - حساب‌ها - حساب‌های کل', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (5086, 5, N'مجوز افزودن - حساب‌های کل', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (6086, 6, N'مجوز ویرایش - حساب‌های کل', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (7086, 7, N'مجوز حذف - حساب‌های کل', NULL, NULL);

ALTER TABLE [dbo].[Fix_Acc2Type] ADD[DefinedBySystem][bit] NOT NULL CONSTRAINT [DF_Fix_Acc2Type_DefinedBySystem]  DEFAULT ((1));

ALTER TABLE [dbo].[Fix_Acc2Type] ADD[Fix_Acc1_ID][tinyint] NULL;

ALTER TABLE [dbo].[Fix_Acc2Type] WITH NOCHECK ADD  CONSTRAINT [FK_Fix_Acc2Type_Fix_Acc1] FOREIGN KEY([Fix_Acc1_ID]) REFERENCES [dbo].[Fix_Acc1] ([RowID]) ON DELETE CASCADE;

ALTER TABLE [dbo].[Fix_Acc2Type] NOCHECK CONSTRAINT [Fix_Acc2Type_Fix_Acc1];

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (1061, 1, N'تعاریف - حساب‌ها - حساب‌های معین', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (5087, 5, N'مجوز افزودن - حساب‌های معین', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (6087, 6, N'مجوز ویرایش - حساب‌های معین', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (7087, 7, N'مجوز حذف - حساب‌های معین', NULL, NULL);

CREATE TABLE [dbo].[Fix_Acc3]([RowID] [int] NOT NULL, [RowName] [nvarchar](100) NOT NULL, [Fix_Acc2TypeID] [tinyint] NOT NULL, [RowDesc] [nvarchar](500) NULL, [UniqueIdentifierValue] [uniqueidentifier] NULL, CONSTRAINT [PK_Fix_Acc3] PRIMARY KEY CLUSTERED  ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], CONSTRAINT [UQ_Fix_Acc3_RowName] UNIQUE NONCLUSTERED ([RowName] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[Fix_Acc3]  WITH CHECK ADD  CONSTRAINT [FK_FixAcc3_FixAcc2Type] FOREIGN KEY([Fix_Acc2TypeID]) REFERENCES [dbo].[Fix_Acc2Type] ([RowID])

ALTER TABLE [dbo].[Fix_Acc3] CHECK CONSTRAINT [FK_FixAcc3_FixAcc2Type]

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (1062, 1, N'تعاریف - حساب‌ها - حساب‌های تفصیلی', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (5088, 5, N'مجوز افزودن - حساب‌های تفصیلی', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (6088, 6, N'مجوز ویرایش - حساب‌های تفصیلی', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (7088, 7, N'مجوز حذف - حساب‌های تفصیلی', NULL, NULL);

CREATE VIEW [dbo].[WhDocDItems] AS SELECT dbo.WhDocD.RowID AS ID, dbo.WhDocH.Fix_WhDocType2_ID, dbo.WhDocH.MDate, dbo.WhDocH.FDate, dbo.WhDocH.RowID AS WhDocHID, dbo.Warehouse.RowName AS Warehouse, dbo.Good.RowName AS GoodName, dbo.Good.RowCode AS GoodCode, { fn IFNULL(dbo.WhDocD.GoodSize, '') } AS GoodSize, COALESCE (CASE WHEN (dbo.WhDocD.InputValue) = 0 THEN (WhDocD.OutputValue) ELSE (WhDocD.InputValue) END, 0) AS Quantity, dbo.WhDocD.UnitPrice, { fn IFNULL(dbo.WhDocD.DiscountPercent, 0) } AS DiscountPercent, { fn IFNULL(dbo.WhDocD.DiscountPrice, 0) } AS DiscountPrice, { fn IFNULL(dbo.WhDocD.TaxPercent, 0) } AS TaxPercent, { fn IFNULL(dbo.WhDocD.TaxPrice, 0) } AS TaxPrice, dbo.WhDocD.TotalPrice, { fn IFNULL(dbo.GoodCategory.RowCode, '') } AS GoodCategoryCode, { fn IFNULL(dbo.GoodCategory.RowName, '') } AS GoodCategoryName, { fn IFNULL(dbo.GoodCategory.FullRowCode, '') } AS GoodCategoryFullCode, { fn IFNULL(dbo.GoodCategory.FullRowName, '') } AS GoodCategoryFullName, { fn IFNULL(dbo.Person.RowCode, '') } AS PersonCode, { fn IFNULL(dbo.Person.RowName, '') } AS PersonName, { fn IFNULL(dbo.PersonCategory.RowName, '') } AS PersonCategory, { fn IFNULL(dbo.Province.RowName, '') } AS Province, { fn IFNULL(dbo.County.RowName, '') } AS County, { fn IFNULL(dbo.City.RowName, '') } AS City, dbo.WhDocD.SerialNumber AS Serial, dbo.WhDocD.RowDesc AS Description, dbo.WhDocH.DocNo, dbo.MeasureUnit.RowName AS MeasureUnit FROM dbo.WhDocD LEFT OUTER JOIN dbo.Good ON dbo.WhDocD.Good_ID = dbo.Good.RowID LEFT OUTER JOIN dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID INNER JOIN dbo.WhDocH ON dbo.WhDocD.WhDocH_ID = dbo.WhDocH.RowID INNER JOIN dbo.MeasureUnit ON dbo.WhDocD.MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.Person ON dbo.WhDocH.Person_ID = dbo.Person.RowID LEFT OUTER JOIN dbo.PersonCategory ON dbo.Person.PersonCategory_ID = dbo.PersonCategory.RowID LEFT OUTER JOIN dbo.Province ON dbo.Person.Province_ID = dbo.Province.RowID LEFT OUTER JOIN dbo.County ON dbo.Person.County_ID = dbo.County.RowID LEFT OUTER JOIN dbo.City ON dbo.Person.City_ID = dbo.City.RowID LEFT OUTER JOIN dbo.Warehouse ON dbo.WhDocH.Warehouse_ID = dbo.Warehouse.RowID

ALTER TABLE [dbo].[Good] ADD[CostOfGood][money] NULL;

ALTER TABLE [dbo].[WhDocD] ADD[CostOfGood][money] NULL;

ALTER TABLE dbo.AppUserSettings ADD[CofferMergeItemsWeight][bit] NULL;

ALTER TABLE [dbo].[CofferSettlementH] ADD[InsertedBy] [smallint] NULL;

ALTER TABLE [dbo].[CofferSettlementH] ADD[UpdatedBy] [smallint] NULL;

ALTER TABLE [dbo].[CofferSettlementH] ADD[InsertServerDateTime] [datetime] NULL CONSTRAINT [DF_CofferSettlementH_InsertServerDateTime]  DEFAULT (getdate());

ALTER TABLE [dbo].[CofferSettlementH] ADD[UpdateServerDateTime] [datetime] NULL;

ALTER TABLE [dbo].[CofferSettlementH] ADD[RowUpdateVersion] [smallint] NULL CONSTRAINT [DF_CofferSettlementH_RowUpdateVersion]  DEFAULT ((1));

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (8012, 8, N'سایر مجوزها - مجوز ثبت مستقیم کارتخوان در صندوق فروش', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (8013, 8, N'سایر مجوزها - مجوز فروش بدون رسید در صندوق فروش', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (8014, 8, N'سایر مجوزها - مجوز برگشت از فروش در صندوق فروش', NULL, NULL);

ALTER TABLE [dbo].[AppUserSettings] ADD[CofferPosDirectSending][bit] NULL;

ALTER TABLE [dbo].[AppUserSettings] ADD[CofferSalesWithoutReceipts][bit] NULL;

ALTER TABLE [dbo].[AppUserSettings] ADD[CofferReturnSale][bit] NULL;

CREATE VIEW [dbo].[WarehouseGoodView] AS SELECT dbo.Good.RowID, dbo.Good.RowName, dbo.Good.RowCode, dbo.Warehouse.RowID AS WarehouseID, dbo.Warehouse.RowName AS WarehouseName, dbo.Good.SalePrice, dbo.Good.Main_MeasureUnit_ID, dbo.MeasureUnit.RowName AS Unit FROM dbo.Warehouse CROSS JOIN dbo.Good INNER JOIN dbo.MeasureUnit ON dbo.Good.Main_MeasureUnit_ID = dbo.MeasureUnit.RowID

CREATE VIEW [dbo].[AllWarehouseQuantityView] AS SELECT dbo.WarehouseGoodView.RowID AS ID, dbo.WarehouseGoodView.RowName AS ProductName, dbo.WarehouseGoodView.RowCode AS ProductBarcode, dbo.WarehouseGoodView.WarehouseID AS Warehouse_ID, dbo.WarehouseGoodView.WarehouseName, { fn IFNULL(dbo.StockView.stock, 0) } AS Quantity, dbo.StockView.FinancialYear_ID, dbo.WarehouseGoodView.SalePrice, dbo.WarehouseGoodView.Unit AS SmallestUnit FROM dbo.WarehouseGoodView LEFT OUTER JOIN dbo.StockView ON dbo.WarehouseGoodView.RowID = dbo.StockView.Good_ID AND dbo.WarehouseGoodView.WarehouseID = dbo.StockView.Warehouse_ID

CREATE TABLE [dbo].[RestaurantTableLocations]([RowID] [int] IDENTITY(1,1) NOT NULL, [RowName] [nvarchar](200) NULL, [RowDesc] [nvarchar](400) NULL, CONSTRAINT [PK_RestaurantTableLocations] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

CREATE TABLE [dbo].[RestaurantTables]([RowID] [int] IDENTITY(1,1) NOT NULL, [RowName] [nvarchar](150) NOT NULL, [TableCapacity] [int] NULL, [RowDesc] [nvarchar](400) NULL, [TableLocation_ID] [int] NULL, CONSTRAINT [PK_RestaurantTables] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[RestaurantTables]  WITH CHECK ADD  CONSTRAINT [FK_RestaurantTables_RestaurantTableLocations] FOREIGN KEY([TableLocation_ID]) REFERENCES [dbo].[RestaurantTableLocations] ([RowID])

ALTER TABLE [dbo].[RestaurantTables] CHECK CONSTRAINT [FK_RestaurantTables_RestaurantTableLocations]

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (1063, 1, N'تعاریف - اطلاعات رستورانی', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (1064, 1, N'تعاریف - اطلاعات رستورانی - مکان‌ها', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (1065, 1, N'تعاریف - اطلاعات رستورانی - میزها', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (5089, 5, N'مجوز افزودن - مکان‌ها (اطلاعات رستورانی)', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (6089, 6, N'مجوز ویرایش - مکان‌ها (اطلاعات رستورانی)', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (7089, 7, N'مجوز حذف - مکان‌ها (اطلاعات رستورانی)', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (5090, 5, N'مجوز افزودن - میز‌ها (اطلاعات رستورانی)', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (6090, 6, N'مجوز ویرایش - میزها (اطلاعات رستورانی)', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (7090, 7, N'مجوز حذف - مبزها (اطلاعات رستورانی)', NULL, NULL);

CREATE TABLE [dbo].[AppSystemSettings]([RowID] [int] IDENTITY(1,1) NOT NULL, [System_ID] [int] NOT NULL, [DefaultPos_ID] [int] NULL, [Printer1] [nvarchar](500) NULL, [Printer2] [nvarchar](500) NULL, CONSTRAINT [PK_AppSystemSettings] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[AppSystemSettings]  WITH CHECK ADD  CONSTRAINT [FK_AppSystemSettings_POS] FOREIGN KEY([DefaultPos_ID]) REFERENCES [dbo].[POS] ([RowID])

ALTER TABLE [dbo].[AppSystemSettings] CHECK CONSTRAINT [FK_AppSystemSettings_POS]

ALTER TABLE [dbo].[AppSystemSettings]  WITH CHECK ADD  CONSTRAINT [FK_AppSystemSettings_Systems] FOREIGN KEY([System_ID]) REFERENCES [dbo].[Systems] ([RowID])

ALTER TABLE [dbo].[AppSystemSettings] CHECK CONSTRAINT [FK_AppSystemSettings_Systems]

CREATE VIEW [dbo].[GoodChangesPriceView] AS SELECT dbo.ChangePrice.RowID AS ID, dbo.ChangePrice.Good_id AS GoodID, { fn IFNULL(dbo.Good.RowName, '') } AS GoodName, dbo.ChangePrice.oldprice AS OldPrice, dbo.ChangePrice.newprice AS NewPrice, dbo.ChangePrice.Fdate AS FDate, dbo.ChangePrice.Mdate AS MDate, dbo.UserInfo.RowID AS InsertedBy_ID, dbo.UserInfo.RowName AS InsertedBy FROM dbo.ChangePrice LEFT OUTER JOIN dbo.Good ON dbo.ChangePrice.Good_id = dbo.Good.RowID LEFT OUTER JOIN dbo.UserInfo ON dbo.ChangePrice.User_id = dbo.UserInfo.RowID

ALTER TABLE dbo.EhDocH ADD[Coffer_ID][int] NULL;

ALTER TABLE dbo.EhDocH ADD[SettlePrice][money] NULL;

CREATE TABLE [dbo].[EhDocHBankAccount]([RowID] [int] IDENTITY(1,1) NOT NULL, [Branch_ID] [int] NULL, [EhDocH_ID] [int] NOT NULL, [BankAccount_ID] [int] NOT NULL, [SettlePrice] [money] NOT NULL CONSTRAINT [DF_EhDocHBankAccount_SettlePrice]  DEFAULT ((0)), [UniqueIdentifierValue] [uniqueidentifier] NULL, [IsSend] [tinyint] NULL CONSTRAINT [DF_EhDocHBankAccount_IsSend]  DEFAULT ((0)), [POS_ID] [int] NULL, CONSTRAINT [PK_EhDocHBankAccount] PRIMARY KEY CLUSTERED ([RowID] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[EhDocHBankAccount]  WITH CHECK ADD  CONSTRAINT [FK_EhDocHBankAccount_BankAccount] FOREIGN KEY([BankAccount_ID]) REFERENCES [dbo].[BankAccount] ([RowID])

ALTER TABLE [dbo].[EhDocHBankAccount] CHECK CONSTRAINT [FK_EhDocHBankAccount_BankAccount]

ALTER TABLE [dbo].[EhDocHBankAccount]  WITH CHECK ADD  CONSTRAINT [FK_EhDocHBankAccount_Branch] FOREIGN KEY([Branch_ID]) REFERENCES [dbo].[Branch] ([RowID])

ALTER TABLE [dbo].[EhDocHBankAccount] CHECK CONSTRAINT [FK_EhDocHBankAccount_Branch]

ALTER TABLE [dbo].[EhDocHBankAccount]  WITH CHECK ADD  CONSTRAINT [FK_EhDocHBankAccount_POS] FOREIGN KEY([POS_ID]) REFERENCES [dbo].[POS] ([RowID])

ALTER TABLE [dbo].[EhDocHBankAccount] CHECK CONSTRAINT [FK_EhDocHBankAccount_POS]

ALTER TABLE [dbo].[EhDocHBankAccount]  WITH NOCHECK ADD  CONSTRAINT [FK_EhDocHBankAccount_EhDocH] FOREIGN KEY([EhDocH_ID]) REFERENCES [dbo].[EhDocH] ([RowID]) ON DELETE CASCADE

ALTER TABLE [dbo].[EhDocHBankAccount] NOCHECK CONSTRAINT [FK_EhDocHBankAccount_EhDocH]

CREATE TABLE [dbo].[EhDocHChequePayH]([RowID] [int] IDENTITY(1,1) NOT NULL, [Branch_ID] [int] NULL, [EhDocH_ID] [int] NOT NULL, [ChequePayH_ID] [int] NOT NULL, [UniqueIdentifierValue] [uniqueidentifier] NULL, [IsSend] [tinyint] NULL, CONSTRAINT [PK_EhDocHChequePayH] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[EhDocHChequePayH] ADD  CONSTRAINT [DF_EhDocHChequePayH_IsSend]  DEFAULT ((0)) FOR [IsSend]

ALTER TABLE [dbo].[EhDocHChequePayH]  WITH CHECK ADD  CONSTRAINT [FK_EhDocHChequePayH_Branch] FOREIGN KEY([Branch_ID]) REFERENCES [dbo].[Branch] ([RowID])

ALTER TABLE [dbo].[EhDocHChequePayH] CHECK CONSTRAINT [FK_EhDocHChequePayH_Branch]

ALTER TABLE [dbo].[EhDocHChequePayH]  WITH CHECK ADD  CONSTRAINT [FK_EhDocHChequePayH_ChequePayH] FOREIGN KEY([ChequePayH_ID]) REFERENCES [dbo].[ChequePayH] ([RowID]) ON DELETE CASCADE

ALTER TABLE [dbo].[EhDocHChequePayH] CHECK CONSTRAINT [FK_EhDocHChequePayH_ChequePayH]

ALTER TABLE [dbo].[EhDocHChequePayH]  WITH NOCHECK ADD  CONSTRAINT [FK_EhDocHChequePayH_EhDocH] FOREIGN KEY([EhDocH_ID]) REFERENCES [dbo].[EhDocH] ([RowID]) ON DELETE CASCADE

ALTER TABLE [dbo].[EhDocHChequePayH] NOCHECK CONSTRAINT [FK_EhDocHChequePayH_EhDocH]

CREATE TABLE [dbo].[EhDocHChequeRecH]([RowID] [int] IDENTITY(1,1) NOT NULL, [Branch_ID] [int] NULL, [EhDocH_ID] [int] NOT NULL, [ChequeRecH_ID] [int] NOT NULL, [ChequeRecD_ID] [int] NULL, [UniqueIdentifierValue] [uniqueidentifier] NULL, [IsSend] [tinyint] NULL, CONSTRAINT [PK_EhDocHChequeRecH] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[EhDocHChequeRecH] ADD  CONSTRAINT [DF_EhDocHChequeRecH_IsSend]  DEFAULT ((0)) FOR [IsSend]

ALTER TABLE [dbo].[EhDocHChequeRecH]  WITH CHECK ADD  CONSTRAINT [FK_EhDocHChequeRecH_Branch] FOREIGN KEY([Branch_ID]) REFERENCES [dbo].[Branch] ([RowID])

ALTER TABLE [dbo].[EhDocHChequeRecH] CHECK CONSTRAINT [FK_EhDocHChequeRecH_Branch]

ALTER TABLE [dbo].[EhDocHChequeRecH]  WITH CHECK ADD  CONSTRAINT [FK_EhDocHChequeRecH_ChequeRecD] FOREIGN KEY([ChequeRecD_ID]) REFERENCES [dbo].[ChequeRecD] ([RowID]) ON DELETE CASCADE

ALTER TABLE [dbo].[EhDocHChequeRecH] CHECK CONSTRAINT [FK_EhDocHChequeRecH_ChequeRecD]

ALTER TABLE [dbo].[EhDocHChequeRecH]  WITH CHECK ADD  CONSTRAINT [FK_EhDocHChequeRecH_ChequeRecH] FOREIGN KEY([ChequeRecH_ID]) REFERENCES [dbo].[ChequeRecH] ([RowID]) ON DELETE CASCADE

ALTER TABLE [dbo].[EhDocHChequeRecH] CHECK CONSTRAINT [FK_EhDocHChequeRecH_ChequeRecH]

ALTER TABLE [dbo].[EhDocHChequeRecH]  WITH NOCHECK ADD  CONSTRAINT [FK_EhDocHChequeRecH_EhDocH] FOREIGN KEY([EhDocH_ID]) REFERENCES [dbo].[EhDocH] ([RowID]) ON DELETE CASCADE

ALTER TABLE [dbo].[EhDocHChequeRecH] NOCHECK CONSTRAINT [FK_EhDocHChequeRecH_EhDocH]

ALTER TABLE [dbo].[POS_Result] ADD[EhDocHBankAccount_ID][int] NULL;

CREATE TABLE [dbo].[Fix_DeliveryType]([RowID] [tinyint] NOT NULL, [RowName] [nvarchar](100) NOT NULL, [RowDesc] [nvarchar](500) NULL, CONSTRAINT [PK_Fix_DeliveryType] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

INSERT [dbo].[Fix_DeliveryType] ([RowID], [RowName], [RowDesc]) VALUES (1, N'حضوری', NULL)

INSERT [dbo].[Fix_DeliveryType] ([RowID], [RowName], [RowDesc]) VALUES (2, N'بیرون‌بر', NULL)

ALTER TABLE [dbo].[WhDocH] ADD[Table_ID][int] NULL;

ALTER TABLE [dbo].[WhDocH] ADD[DeliveryType_ID][tinyint] NULL;

ALTER TABLE [dbo].[WhDocH]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocH_Fix_DeliveryType] FOREIGN KEY([DeliveryType_ID]) REFERENCES [dbo].[Fix_DeliveryType] ([RowID])

ALTER TABLE [dbo].[WhDocH] NOCHECK CONSTRAINT [FK_WhDocH_Fix_DeliveryType]

ALTER TABLE [dbo].[WhDocH]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocH_RestaurantTables] FOREIGN KEY([Table_ID]) REFERENCES [dbo].[RestaurantTables] ([RowID])

ALTER TABLE [dbo].[WhDocH] NOCHECK CONSTRAINT [FK_WhDocH_RestaurantTables]

ALTER TABLE [dbo].[Systems] ADD[ActivationCode] [nvarchar](200) NULL;

INSERT [dbo].[Fix_Acc1] ([RowID], [RowName], [RowDesc], [UniqueIdentifierValue], [DefinedBySystem]) VALUES (14, N'اموال', NULL, NULL, 1)

INSERT [dbo].[Fix_Acc2Type] ([RowID], [RowName], [RowCode], [RowDesc], [UniqueIdentifierValue], [DefinedBySystem], [Fix_Acc1_ID]) VALUES (10, N'اموال', NULL, NULL, NULL, 1, NULL)

ALTER TABLE [dbo].[Fix_Acc3] ADD[RowCode] [nchar](10) NULL;

ALTER TABLE [dbo].[Good] ADD[MultipleSalePrice] [bit] NULL;

ALTER TABLE [dbo].[TR_ChequePrintPattern] ADD[PrintFile] [image] NULL;

CREATE TABLE [dbo].[DiscountGroupBranch]([RowID] [int] IDENTITY(1,1) NOT NULL, [RowName] [nvarchar](150) NOT NULL, [Branch_ID] [int] NOT NULL, [IsActive] [bit] NOT NULL, [RowDesc] [nvarchar](500) NULL, [UniqueIdentifierValue] [uniqueidentifier] NULL, [IsSend] [tinyint] NULL, CONSTRAINT [PK_DiscountGroupBranch] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[DiscountGroupBranch]  WITH CHECK ADD  CONSTRAINT [FK_DiscountGroupBranch_Branch] FOREIGN KEY([Branch_ID]) REFERENCES [dbo].[Branch] ([RowID])

ALTER TABLE [dbo].[DiscountGroupBranch] CHECK CONSTRAINT [FK_DiscountGroupBranch_Branch]

CREATE TABLE [dbo].[DiscountGroupBranchGoods]([RowID] [int] IDENTITY(1,1) NOT NULL, [GoodID] [int] NOT NULL, [DiscountPercent] [float] NULL, [DiscountPrice] [money] NULL, [DiscountGroupBranch_ID] [int] NOT NULL, CONSTRAINT [PK_DiscountGroupBranchGoods] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[DiscountGroupBranchGoods]  WITH CHECK ADD  CONSTRAINT [FK_DiscountGroupBranchGoods_DiscountGroupBranch] FOREIGN KEY([DiscountGroupBranch_ID]) REFERENCES [dbo].[DiscountGroupBranch] ([RowID])

ALTER TABLE [dbo].[DiscountGroupBranchGoods] CHECK CONSTRAINT [FK_DiscountGroupBranchGoods_DiscountGroupBranch]

CREATE VIEW [dbo].[EstateStockView] AS SELECT SUM(dbo.EhDocD.CalculatedInputValue) - SUM(dbo.EhDocD.CalculatedOutputValue) AS Stock, dbo.EhDocD.Estate_ID, dbo.EhDocD.FinancialYear_ID, dbo.EhDocH.EstateLocation_ID FROM dbo.EhDocD INNER JOIN dbo.EhDocH ON dbo.EhDocD.EhDocH_ID = dbo.EhDocH.RowID GROUP BY dbo.EhDocD.Estate_ID, dbo.EhDocD.FinancialYear_ID, dbo.EhDocH.EstateLocation_ID

CREATE VIEW [dbo].[EstateLocationView] AS SELECT dbo.Estate.RowID AS EstateID, dbo.Estate.RowName AS EstateName, dbo.Estate.RowCode AS EstateRowCode, dbo.EstateLocation.RowID AS EstateLocationID, dbo.EstateLocation.RowName AS EstateLocationName, dbo.MeasureUnit.RowID AS UnitID, dbo.MeasureUnit.RowName AS Unit FROM dbo.EstateLocation CROSS JOIN dbo.Estate INNER JOIN dbo.MeasureUnit ON dbo.Estate.MeasureUnit_ID = dbo.MeasureUnit.RowID

CREATE VIEW [dbo].[AllEstateLocationView] AS SELECT dbo.EstateLocationView.EstateID, dbo.EstateLocationView.EstateName, dbo.EstateLocationView.EstateRowCode, dbo.EstateLocationView.EstateLocationID, dbo.EstateLocationView.EstateLocationName, dbo.EstateLocationView.UnitID, dbo.EstateLocationView.Unit, dbo.EstateStockView.FinancialYear_ID, { fn IFNULL(dbo.EstateStockView.Stock, 0) } AS Quantity FROM dbo.EstateLocationView LEFT OUTER JOIN dbo.EstateStockView ON dbo.EstateLocationView.EstateID = dbo.EstateStockView.Estate_ID AND dbo.EstateLocationView.EstateLocationID = dbo.EstateStockView.EstateLocation_ID

CREATE VIEW [dbo].[EstateCardexView] AS SELECT [EhDocD].[Estate_ID], [FDate], [MDate], [DocNo], [EhDocH].[RowID] AS EhDocHID, [Fix_EhDocType].[RowName] AS EhDocHType, [Fix_EhDocType].[RowID] AS EhDocHTypeID, [CalculatedInputValue], [CalculatedOutputValue], [EhDocH].[FinancialYear_ID] AS FinancialYearID, [MeasureUnit].[RowName] AS Unit, [EhDocH].[EstateLocation_ID] FROM dbo.EhDocD INNER JOIN dbo.EhDocH ON dbo.EhDocH.RowID = dbo.EhDocD.EhDocH_ID INNER JOIN dbo.Fix_EhDocType ON dbo.Fix_EhDocType.RowID = dbo.EhDocH.Fix_EhDocType_ID INNER JOIN dbo.Estate ON dbo.Estate.RowID = dbo.EhDocD.Estate_ID INNER JOIN dbo.MeasureUnit ON dbo.MeasureUnit.RowID = dbo.Estate.MeasureUnit_ID

INSERT INTO Fix_AppObject ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES (1066, 1, N'تعاریف - کالاها و خدمات - گروه تخفیف شعب')

INSERT INTO Fix_AppObject ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES (5091, 5, N'مجوز افزودن - گروه تخفیف شعب')

INSERT INTO Fix_AppObject ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES (6091, 6, N'مجوز ویرایش - گروه تخفیف شعب')

INSERT INTO Fix_AppObject ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES (7091, 7, N'مجوز حذف - گروه تخفیف شعب')

INSERT INTO Fix_AppObject ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES (3051, 3, N'گزارش‌ها - گزارشات - موجودی اموال')

INSERT INTO Fix_AppObject ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES (3052, 3, N'گزارش‌ها - گزارشات - پرونده اموال')

DROP VIEW GoodView

CREATE VIEW [dbo].[GoodView] AS SELECT dbo.Good.RowID AS ID, dbo.Good.RowName AS ProductName, dbo.Good.RowCode AS ProductBarcode, dbo.Good.PurchasePrice, dbo.Good.SalePrice, dbo.Good.InterestRate, dbo.Good.ConsumerPrice, dbo.Good.OrderPoint, { fn IFNULL(dbo.Good.RowNameEN, '') } AS LatinProductName, { fn IFNULL(dbo.Good.RowNameAlias, '') } AS ProductNickName, dbo.Good.FirstStock, dbo.Good.DiscountPrice, dbo.Good.FDate AS DateInsert, dbo.Good.RowDesc AS Description, dbo.Good.IsHasSize, dbo.Good.IsActive, dbo.Good.MDate, dbo.Good.Fix_GoodType_ID, dbo.Good.RowCode, dbo.Good.MaxCount, dbo.Good.GoodCategory_ID AS GoodCategoryID, dbo.MeasureUnit.RowName AS SmallestUnit, MeasureUnit_1.RowName AS DefaultUnit, { fn IFNULL(dbo.DiscountGroupGood.RowName, '') } AS DiscountGroupGood, { fn IFNULL(dbo.TaxGroupGood.RowName, '') } AS TaxGroupGood, { fn IFNULL(dbo.GoodCategory.RowName, '') } AS CategoryName, dbo.Good.Reversion, { fn IFNULL(dbo.Good.MultipleSalePrice, 0) } AS MultipleSalePrice FROM dbo.Good INNER JOIN dbo.MeasureUnit ON dbo.Good.Main_MeasureUnit_ID = dbo.MeasureUnit.RowID INNER JOIN dbo.MeasureUnit AS MeasureUnit_1 ON dbo.Good.Default_MeasureUnit_ID = MeasureUnit_1.RowID LEFT OUTER JOIN dbo.DiscountGroupGood ON dbo.Good.DiscountGroupGood_ID = dbo.DiscountGroupGood.RowID LEFT OUTER JOIN dbo.TaxGroupGood ON dbo.Good.TaxGroupGood_ID = dbo.TaxGroupGood.RowID LEFT OUTER JOIN dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID

INSERT [dbo].[Fix_Reports] ([RowID], [RowName], [ReportFile], [RowDesc], [UniqueIdentifierValue], [sectionid], [reportstring], [Branch_id]) VALUES (37, N'رسید باز کردن صندوق',  , NULL, NULL, 16, NULL, 1)

INSERT [dbo].[Fix_Reports] ([RowID], [RowName], [ReportFile], [RowDesc], [UniqueIdentifierValue], [sectionid], [reportstring], [Branch_id]) VALUES (38, N'رسید بستن صندوق',  , NULL, NULL, 16, NULL, 1)

INSERT [dbo].[Fix_Reports] ([RowID], [RowName], [ReportFile], [RowDesc], [UniqueIdentifierValue], [sectionid], [reportstring], [Branch_id]) VALUES (39, N'رسید ارسال فاکتور ',  , NULL, NULL, 16, NULL, 1)

INSERT [dbo].[Fix_Reports] ([RowID], [RowName], [ReportFile], [RowDesc], [UniqueIdentifierValue], [sectionid], [reportstring], [Branch_id]) VALUES (40, N'رسید دریافت فاکتور', NULL, NULL, NULL, 16, NULL, 1)

CREATE VIEW [dbo].[EstateMergeStocksView] AS SELECT SUM(CalculatedInputValue) - SUM(CalculatedOutputValue) AS Stock, Estate_ID, FinancialYear_ID FROM dbo.EhDocD GROUP BY Estate_ID, FinancialYear_ID

CREATE VIEW [dbo].[AllEstateMergeStocksView] AS SELECT dbo.Estate.RowID AS EstateID, dbo.Estate.RowName AS EstateName, dbo.Estate.RowCode AS EstateRowCode, dbo.Estate.EstateLocation_ID AS EstateLocationID, dbo.EstateLocation.RowName AS EstateLocationName, dbo.Estate.MeasureUnit_ID AS UnitID, dbo.MeasureUnit.RowName AS Unit, dbo.EstateMergeStocksView.FinancialYear_ID, dbo.EstateMergeStocksView.Stock AS Quantity FROM dbo.Estate INNER JOIN dbo.EstateLocation ON dbo.Estate.EstateLocation_ID = dbo.EstateLocation.RowID INNER JOIN dbo.MeasureUnit ON dbo.Estate.MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.EstateMergeStocksView ON dbo.EstateMergeStocksView.Estate_ID = dbo.Estate.RowID

ALTER TABLE dbo.Coffer ADD ServerRowID int NULL

ALTER TABLE dbo.Good ADD ServerRowID int NULL

ALTER TABLE dbo.Person ADD ServerRowID int NULL

ALTER TABLE dbo.AppUserSettings ADD ServerRowID smallint NULL

ALTER TABLE dbo.AppSettings ADD ServerRowID tinyint NULL

ALTER TABLE dbo.UserInfo ADD ServerRowID smallint NULL

ALTER TABLE dbo.Systems ADD ServerRowID int NULL

ALTER TABLE dbo.Systems ADD Reversion int NULL

ALTER TABLE dbo.POS ADD ServerRowID int NULL

ALTER TABLE dbo.POS ADD Reversion int NULL

ALTER TABLE dbo.CodingSystems ADD ServerRowID smallint NULL

ALTER TABLE dbo.CodingSystems ADD Reversion int NULL

ALTER TABLE dbo.GoodCodingSystems ADD ServerRowID int NULL

ALTER TABLE dbo.GoodCodingSystems ADD Reversion int NULL

ALTER TABLE dbo.MeasureUnit ADD ServerRowID int NULL

ALTER TABLE dbo.MeasureUnit ADD Reversion int NULL

ALTER TABLE dbo.GoodMeasureUnit ADD ServerRowID int NULL

ALTER TABLE dbo.GoodMeasureUnit ADD Reversion int NULL

ALTER TABLE dbo.GoodMeasureUnitCodingSystems ADD ServerRowID int NULL

ALTER TABLE dbo.GoodMeasureUnitCodingSystems ADD Reversion int NULL

ALTER TABLE dbo.Table_Discount ADD ServerRowID int NULL

CREATE TABLE [dbo].[WhDocHTemporary]([RowID] [int] IDENTITY(1,1) NOT NULL, [Branch_ID] [int] NULL, [FinancialYear_ID] [smallint] NOT NULL, [Fix_RecordStatusType_ID] [tinyint] NOT NULL, [Fix_WhDocType1_ID] [tinyint] NOT NULL, [Fix_WhDocType2_ID] [tinyint] NOT NULL, [Warehouse_ID] [smallint] NOT NULL, [Fix_DiscountType_ID] [tinyint] NULL, [Coffer_ID] [int] NULL, [Person_ID] [int] NULL, [ManufactureDocH_ID] [int] NULL, [ShiftDocH_ID] [int] NULL, [WarehouseComputationDocH_ID] [int] NULL, [DocNo] [bigint] NOT NULL, [FDate] [char](10) NOT NULL, [MDate] [datetime] NULL, [DiscountPercent] [float] NULL, [DiscountPrice] [money] NULL, [SettlePrice] [money] NULL, [TaxPercent] [float] NULL, [TaxPrice] [money] NULL, [TotalPrice] [money] NULL, [QuantityInUnitPrice] [money] NULL, [TotalDiscountPriceItems] [money] NULL, [TotalTaxPriceItems] [money] NULL, [TotalIncreasePrice] [money] NULL, [TotalDecreasePrice] [money] NULL, [PayablePrice] [money] NULL, [DefinedBySystem] [bit] NOT NULL, [ReferenceValue] [nvarchar](100) NULL, [RowDesc] [nvarchar](500) NULL, [InsertedBy] [smallint] NULL, [UpdatedBy] [smallint] NULL, [FDateInsert] [char](10) NULL, [FDateUpdate] [char](10) NULL, [FTimeInsert] [char](5) NULL, [FTimeUpdate] [char](5) NULL, [InsertServerDateTime] [datetime] NULL, [UpdateServerDateTime] [datetime] NULL, [RowUpdateVersion] [smallint] NULL, [UniqueIdentifierValue] [uniqueidentifier] NULL, [IsSend] [tinyint] NULL, [Good_ID] [int] NULL, [SumPercentPrice] [money] NULL, [FDateSettlementDeadline] [char](10) NULL, [MDateSettlementDeadline] [datetime] NULL, [IsSettled] [bit] NULL, [OrderH_ID] [bigint] NULL, [Times] [time](0) NULL, [Table_ID] [int] NULL, [DeliveryType_ID] [tinyint] NULL, [ClientRowID] [int] NULL CONSTRAINT [PK_WhDocHTemporary] PRIMARY KEY CLUSTERED([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],) ON [PRIMARY]

CREATE TABLE [dbo].[WhDocDTemporary]([RowID] [int] IDENTITY(1,1) NOT NULL,	[Branch_ID] [int] NULL,	[FinancialYear_ID] [smallint] NOT NULL,	[WhDocH_ID] [int] NOT NULL,	[Good_ID] [int] NOT NULL, [MeasureUnit_ID] [int] NULL, [Main_MeasureUnit_ID] [int] NULL, [Fix_DiscountType_ID] [tinyint] NULL, [InputValue] [float] NOT NULL, [CalculatedInputValue] [float] NOT NULL, [OutputValue] [float] NOT NULL, [CalculatedOutputValue] [float] NOT NULL, [UnitPrice] [money] NOT NULL, [TotalPrice] [money] NOT NULL, [QuantityInUnitPrice] [money] NULL, [DiscountPercent] [float] NULL, [DiscountPrice] [money] NULL,	[PurchaseUnitPrice] [money] NULL, [PurchaseTotalPrice] [money] NULL, [TaxPercent] [float] NULL,	[TaxPrice] [money] NULL, [SaleUnitPrice] [money] NULL, [RowDesc] [nvarchar](500) NULL, [UniqueIdentifierValue] [uniqueidentifier] NULL, [IsSend] [tinyint] NULL, [PurchaseTotalPriceAverage] [money] NULL, [GoodSize] [nvarchar](50) NULL, [SortInDocH] [bigint] NULL, [SerialNumber] [nvarchar](50) NULL, [CostOfGood] [money] NULL, [ClientRowID] [int] NULL, CONSTRAINT [PK_WhDocDTemporary] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

CREATE TABLE [dbo].[WhDocHBankAccountTemporary]( [RowID] [int] IDENTITY(1,1) NOT NULL, [Branch_ID] [int] NULL, [WhDocH_ID] [int] NOT NULL, [BankAccount_ID] [int] NOT NULL, [SettlePrice] [money] NOT NULL, [UniqueIdentifierValue] [uniqueidentifier] NULL, [IsSend] [tinyint] NULL, [POS_ID] [int] NULL, [ClientRowID] [int] NULL, CONSTRAINT [PK_WhDocHBankAccountTemporary] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE dbo.WhDocH ADD ClientRowID int NULL

ALTER TABLE dbo.WhDocD ADD ClientRowID int NULL

ALTER TABLE dbo.Coffer ADD ClientRowID int NULL

ALTER TABLE dbo.Good ADD ClientRowID int NULL

ALTER TABLE dbo.Person ADD ClientRowID int NULL

ALTER TABLE dbo.AppUserSettings ADD ClientRowID smallint NULL

ALTER TABLE dbo.AppSettings ADD ClientRowID tinyint NULL

ALTER TABLE dbo.UserInfo ADD ClientRowID smallint NULL

ALTER TABLE dbo.Systems ADD ClientRowID int NULL

ALTER TABLE dbo.POS ADD ClientRowID int NULL

ALTER TABLE dbo.CodingSystems ADD ClientRowID smallint NULL

ALTER TABLE dbo.GoodCodingSystems ADD ClientRowID int NULL

ALTER TABLE dbo.MeasureUnit ADD ClientRowID int NULL

ALTER TABLE dbo.GoodMeasureUnit ADD ClientRowID int NULL

ALTER TABLE dbo.GoodMeasureUnitCodingSystems ADD ClientRowID int NULL

ALTER TABLE dbo.Table_Discount ADD ClientRowID int NULL

ALTER TABLE dbo.POS ADD IsSend tinyint NULL

ALTER TABLE dbo.Systems ADD IsSend tinyint NULL

CREATE TABLE [dbo].[POSTemporary]( [RowID] [int] IDENTITY(1,1) NOT NULL, [RowName] [nvarchar](100) NOT NULL, [SystemID] [int] NOT NULL, [BankAccountID] [int] NOT NULL, [RowIP] [nvarchar](20) NULL, [Terminal] [nvarchar](100) NULL, [Serial] [nvarchar](100) NULL, [AcceptorCode] [nvarchar](100) NULL, [CompanyID] [int] NULL, [Port] [nvarchar](50) NULL, [IsActive] [bit] NULL, [ServerRowID] [int] NULL, [Reversion] [int] NULL, [ClientRowID] [int] NULL, [IsSend] [tinyint] NULL, CONSTRAINT [PK_POSTemporary] PRIMARY KEY CLUSTERED ( [RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

CREATE TABLE [dbo].[CloseCofferHTemporary]( [RowID] [int] IDENTITY(1,1) NOT NULL, [UserID] [smallint] NOT NULL, [CashSale] [money] NULL, [CardReaderSale] [money] NULL, [Branch_ID] [int] NULL, [Settled] [bit] NOT NULL, [FinancialYear_ID] [smallint] NULL, [FDateInsert] [char](10) NULL, [MDateInsert] [datetime] NULL, [FDateUpdate] [char](10) NULL, [MDateUpdate] [datetime] NULL, [FTimeInsert] [char](5) NULL, [FTimeUpdate] [char](5) NULL, [CashReturnSale] [money] NULL, [TotalCashSale] [money] NULL, [Coffer_ID] [int] NULL, CONSTRAINT [PK_CloseCofferHTemporary] PRIMARY KEY CLUSTERED ( [RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

CREATE TABLE [dbo].[CloseCofferDTemporary]( [RowID] [int] IDENTITY(1,1) NOT NULL, [Branch_ID] [int] NULL, [CloseCofferH_ID] [int] NOT NULL, [POS_ID] [int] NOT NULL, [Price] [money] NOT NULL, [RowDesc] [nvarchar](500) NULL, CONSTRAINT [PK_CloseCofferDTemporary] PRIMARY KEY CLUSTERED ( [RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE Fix_Reports ADD MainRowID int null

ALTER TABLE Fix_Reports DROP CONSTRAINT UQ_Fix_Reports_RowName

CREATE TABLE [dbo].[FailedBarcodes]( [RowID] [int] IDENTITY(1,1) NOT NULL, [Barcode] [nvarchar](50) NOT NULL, [FDate] [char](10) NULL, [MDate] [date] NULL, [UserID] [smallint] NULL, [SystemID] [int] NULL, [FTime] [char](5) NULL,PRIMARY KEY CLUSTERED ( [RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

DROP TABLE BMS_BuildingComplex

DROP TABLE BMS_BuildingUnit

DROP TABLE BMS_BuildingUnitLiabilityD

DROP TABLE BMS_BuildingUnitLiabilityH

DROP TABLE BMS_BuildingUnitOwner

DROP TABLE BMS_BuildingUnitPayment

DROP TABLE BMS_BuildingUnitTenant

DROP TABLE BMS_BuildingUnitType

DROP TABLE BMS_ChargeFormula

DROP TABLE BMS_Expense

DROP TABLE BMS_ExpenseType

DROP TABLE BMS_Fix_CalculationMethod

DROP TABLE BMS_Fix_PaymentType

DROP TABLE BMS_Floor

DROP TABLE BMS_Income

DROP TABLE BMS_IncomeType

DROP TABLE BMS_MonthlyChargeD

DROP TABLE BMS_MonthlyChargeH

CREATE VIEW [dbo].[GoodOtherUnitView] AS SELECT dbo.Good.RowID AS ID, dbo.Good.RowName AS ProductName, dbo.GoodMeasureUnitCodingSystems.RowCode AS ProductBarcode, dbo.GoodMeasureUnit.PurchasePrice, dbo.GoodMeasureUnit.SalePrice, dbo.Good.InterestRate, dbo.Good.ConsumerPrice, dbo.Good.OrderPoint, { fn IFNULL(dbo.Good.RowNameEN, '') } AS LatinProductName, { fn IFNULL(dbo.Good.RowNameAlias, '') } AS ProductNickName, dbo.Good.FirstStock, dbo.Good.DiscountPrice, dbo.Good.FDate AS DateInsert, dbo.Good.RowDesc AS Description, dbo.Good.IsHasSize, dbo.Good.IsActive, dbo.Good.MDate, dbo.Good.Fix_GoodType_ID, dbo.Good.RowCode, dbo.Good.MaxCount, dbo.Good.GoodCategory_ID AS GoodCategoryID, dbo.MeasureUnit.RowName AS SmallestUnit, { fn IFNULL(dbo.DiscountGroupGood.RowName, '') } AS DiscountGroupGood, { fn IFNULL(dbo.TaxGroupGood.RowName, '') } AS TaxGroupGood, { fn IFNULL(dbo.GoodCategory.RowName, '') } AS CategoryName, dbo.Good.Reversion, { fn IFNULL(dbo.Good.MultipleSalePrice, 0) } AS MultipleSalePrice FROM dbo.GoodMeasureUnit INNER JOIN dbo.Good ON dbo.GoodMeasureUnit.Good_ID = dbo.Good.RowID INNER JOIN dbo.MeasureUnit ON dbo.GoodMeasureUnit.MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.DiscountGroupGood ON dbo.Good.DiscountGroupGood_ID = dbo.DiscountGroupGood.RowID LEFT OUTER JOIN dbo.TaxGroupGood ON dbo.Good.TaxGroupGood_ID = dbo.TaxGroupGood.RowID LEFT OUTER JOIN dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID INNER JOIN dbo.GoodMeasureUnitCodingSystems ON dbo.GoodMeasureUnit.RowID = dbo.GoodMeasureUnitCodingSystems.GoodMeasureUnit_ID

ALTER VIEW [dbo].[WhDochView] AS SELECT dbo.WhDocH.RowID AS ID, dbo.Fix_WhDocType2.RowName, dbo.Person.RowName AS PersonName, dbo.Person.RowCode AS PersonCode, dbo.Branch.RowID AS BranchID, dbo.Branch.RowName AS BranchName, dbo.WhDocH.DocNo, dbo.WhDocH.FDate, dbo.WhDocH.MDate, dbo.WhDocH.DiscountPercent, dbo.WhDocH.DiscountPrice, { fn IFNULL(dbo.WhDocH.SettlePrice, 0) } AS CashSettlement, dbo.WhDocH.TaxPercent, dbo.WhDocH.TaxPrice, dbo.WhDocH.TotalPrice, { fn IFNULL(dbo.WhDocH.TotalIncreasePrice, 0) } AS IncreasePrice, { fn IFNULL(dbo.WhDocH.TotalDecreasePrice, 0) } AS DecreasePrice, dbo.WhDocH.PayablePrice, dbo.WhDocH.ReferenceValue AS Reference, dbo.WhDocH.RowDesc AS Description, dbo.WhDocH.FDateUpdate AS UpdateDate, dbo.WhDocH.FTimeInsert AS InsertTime, dbo.WhDocH.FTimeUpdate AS UpdateTime, dbo.WhDocH.FDateSettlementDeadline AS SettlementDeadline, dbo.WhDocH.MDateSettlementDeadline, dbo.UserInfo.RowName AS InsertBy, UserInfo_1.RowName AS UpdateBy, dbo.WhDocH.InsertedBy AS InsertByID, { fn IFNULL(dbo.WhDocHSumPos.sumpos, 0) } AS CardReaderSettlement, { fn IFNULL(dbo.WhDocHBankSum.SumBank, 0) } AS BankAccountSettlement, dbo.Warehouse.RowName AS Warehouse, { fn IFNULL(dbo.WhDocH.IsSettled, 0) } AS IsSettled, { fn IFNULL(dbo.WhDocHSumMarketer.MarketerCommission, 0) } AS MarketerCommission, { fn IFNULL(dbo.WhDocHChequeRecSum.ChequeRec, 0) } + { fn IFNULL(dbo.WhDocHChequePaySum.SumChequePay, 0) } AS ChequeSettlement, dbo.FinancialYear.RowName AS FinancialName, dbo.FinancialYear.RowID AS FinancialYear_ID, dbo.Fix_WhDocType2.RowID AS Fix_WhDocType2_ID, dbo.WhDocH.FDateInsert AS InsertDate, dbo.WhDocH.PayablePrice - ({ fn IFNULL(dbo.WhDocHSumPos.sumpos, 0) } + { fn IFNULL(dbo.WhDocHBankSum.SumBank, 0) } + { fn IFNULL(dbo.WhDocHChequeRecSum.ChequeRec, 0) } + { fn IFNULL(dbo.WhDocHChequePaySum.SumChequePay, 0) } + { fn IFNULL(dbo.WhDocH.SettlePrice, 0) }) AS NotSettledRemainder, dbo.WhDocH.RowUpdateVersion FROM dbo.WhDocH INNER JOIN dbo.Person ON dbo.WhDocH.Person_ID = dbo.Person.RowID INNER JOIN dbo.Branch ON dbo.WhDocH.Branch_ID = dbo.Branch.RowID INNER JOIN dbo.Fix_WhDocType2 ON dbo.WhDocH.Fix_WhDocType2_ID = dbo.Fix_WhDocType2.RowID INNER JOIN dbo.UserInfo ON dbo.WhDocH.InsertedBy = dbo.UserInfo.RowID INNER JOIN dbo.Warehouse ON dbo.WhDocH.Warehouse_ID = dbo.Warehouse.RowID INNER JOIN dbo.FinancialYear ON dbo.WhDocH.FinancialYear_ID = dbo.FinancialYear.RowID LEFT OUTER JOIN dbo.UserInfo AS UserInfo_1 ON dbo.WhDocH.UpdatedBy = UserInfo_1.RowID LEFT OUTER JOIN dbo.WhDocHSumPos ON dbo.WhDocH.RowID = dbo.WhDocHSumPos.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHBankSum ON dbo.WhDocH.RowID = dbo.WhDocHBankSum.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHSumMarketer ON dbo.WhDocH.RowID = dbo.WhDocHSumMarketer.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHChequePaySum ON dbo.WhDocH.RowID = dbo.WhDocHChequePaySum.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHChequeRecSum ON dbo.WhDocH.RowID = dbo.WhDocHChequeRecSum.WhDocH_ID

ALTER TABLE dbo.GoodSalePrice ADD ConsumerPrice money null

ALTER TABLE dbo.WhDocHTemporary ADD PersonMobileNumber nvarchar(200)

ALTER TABLE dbo.WhDocHTemporary ADD PersonName nvarchar(150)

CREATE VIEW [dbo].[PersonRemainderView] AS SELECT SUM(dbo.DocD.BedPrice) AS BedPrice, SUM(dbo.DocD.BesPrice) AS BesPrice, dbo.Person.RowID AS PersonID, SUM(dbo.DocD.BedPrice) - SUM(dbo.DocD.BesPrice) AS Remainder,  COALESCE (CASE WHEN SUM(dbo.DocD.BedPrice) - SUM(dbo.DocD.BesPrice) > 0 THEN 1 ELSE 0 END, 1) AS int FROM dbo.DocD INNER JOIN dbo.Person ON dbo.DocD.Acc2RowID = dbo.Person.RowID WHERE (dbo.DocD.Fix_Acc1_ID = 3) AND (dbo.DocD.Fix_Acc2Type_ID = 1) AND (dbo.Person.Fix_PersonType_ID <> 5) GROUP BY dbo.Person.RowID HAVING (SUM(dbo.DocD.BedPrice) - SUM(dbo.DocD.BesPrice) <> 0)

CREATE VIEW [dbo].[PersonAccountView] AS SELECT dbo.Person.RowName AS Name, dbo.Person.RowCode AS Code, dbo.Person.MobileNumber AS Mobile, dbo.Person.PhoneNumber AS Phone, dbo.Person.NationalCode, dbo.Person.AccountNumber1, dbo.Person.AccountNumber2, dbo.Person.AccountNumber3, dbo.Person.Adress AS Address, dbo.Person.MobileForSMS, dbo.Person.FatherName, dbo.Person.EconomicCode, ABS(dbo.PersonRemainderView.Remainder) AS Remainder, dbo.PersonRemainderView.int, dbo.Person.RowID AS PersonID, { fn IFNULL(dbo.Person.FDateLastSMS_ForDebt, '') } AS LastSmsDate, dbo.Person.MDateLastSMS_ForDebt AS LastSmsDateTime FROM dbo.PersonRemainderView INNER JOIN dbo.Person ON dbo.PersonRemainderView.PersonID = dbo.Person.RowID

CREATE VIEW [dbo].[SalesFactorProfit] AS SELECT dbo.WhDocH.RowID AS ID, dbo.WhDocH.FDate, dbo.WhDocH.DocNo, dbo.Person.RowCode AS PersonCode, dbo.Person.RowName AS PersonName, ISNULL(SUM(dbo.WhDocHInvoiceIncrease.IncreasePrice), 0) AS IncreasePrice, ISNULL(SUM(dbo.WhDocHInvoiceDecrease.DecreasePrice), 0) AS DecreasePrice, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) AS SumGoodsPrice, ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) AS SumDiscountItems, dbo.WhDocH.DiscountPrice, ISNULL(SUM(dbo.WhDocHMarketerPercent.PercentPrice), 0) AS MarketerCommission, dbo.WhDocH.MDate, ISNULL(SUM(dbo.WhDocD.PurchaseTotalPriceAverage), 0) AS SumTotalPriceAverage, ISNULL(SUM(dbo.WhDocD.PurchaseTotalPrice), 0) AS SumPurchaseTotalPrice, ISNULL(SUM(dbo.WhDocD.CostOfGood), 0) AS SumCostOfGood, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.WhDocD.PurchaseTotalPriceAverage), 0)) AS FactorProfitOnAverage, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.WhDocD.PurchaseTotalPrice), 0)) AS FactorProfitOnPurchasePrice, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.WhDocD.CostOfGood), 0)) AS FactorProfitOnCostOfGood, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.WhDocD.PurchaseTotalPriceAverage), 0) + ISNULL(SUM(dbo.WhDocHMarketerPercent.PercentPrice), 0)) AS TotalFactorProfitOnAverage, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.WhDocD.PurchaseTotalPrice), 0) + ISNULL(SUM(dbo.WhDocHMarketerPercent.PercentPrice), 0)) AS TotalFactorProfitOnPurchasePrice, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.WhDocD.CostOfGood), 0) + ISNULL(SUM(dbo.WhDocHMarketerPercent.PercentPrice), 0)) AS TotalFactorProfitOnCostOfGood FROM dbo.WhDocH INNER JOIN dbo.Person ON dbo.WhDocH.Person_ID = dbo.Person.RowID LEFT OUTER JOIN dbo.WhDocD ON dbo.WhDocH.RowID = dbo.WhDocD.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHInvoiceIncrease ON dbo.WhDocH.RowID = dbo.WhDocHInvoiceIncrease.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHInvoiceDecrease ON dbo.WhDocH.RowID = dbo.WhDocHInvoiceDecrease.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHMarketerPercent ON dbo.WhDocH.RowID = dbo.WhDocHMarketerPercent.WhDocH_ID WHERE (dbo.WhDocH.Fix_WhDocType2_ID = 21) GROUP BY dbo.WhDocH.RowID, dbo.WhDocH.FDate, dbo.WhDocH.DocNo, dbo.Person.RowCode, dbo.Person.RowName, dbo.WhDocH.DiscountPrice, dbo.WhDocH.MDate

CREATE VIEW [dbo].[GoodFactorsView] AS SELECT g.RowID AS ID, g.RowCode AS GoodCode, g.RowName AS GoodName, CAST(h.MDate AS DATE) AS MDate, COUNT(d.WhDocH_ID) AS FactorsCount, SUM(d.CalculatedOutputValue) AS CalculatedOutputValue, SUM(d.CalculatedInputValue) AS CalculatedInputValue, SUM(d.TotalPrice) AS TotalPrice, g.SalePrice, d.Branch_ID AS BranchID, g.GoodCategory_ID AS CategoryID, m.RowName AS MeasureUnit, h.Fix_WhDocType2_ID AS WhDocType, p.RowCode AS PersonCode, p.RowName AS PersonName, p.RowID AS PersonID, SUM(h.PayablePrice) AS SumFactorsPayablePrice FROM dbo.WhDocD AS d INNER JOIN dbo.Good AS g ON d.Good_ID = g.RowID INNER JOIN dbo.MeasureUnit AS m ON g.Main_MeasureUnit_ID = m.RowID INNER JOIN dbo.WhDocH AS h ON d.WhDocH_ID = h.RowID INNER JOIN dbo.Person AS p ON h.Person_ID = p.RowID GROUP BY d.Good_ID, g.RowID, g.RowCode, g.RowName, CAST(h.MDate AS DATE), g.SalePrice, d.Branch_ID, g.GoodCategory_ID, m.RowName, h.Fix_WhDocType2_ID, p.RowCode, p.RowName, p.RowID

CREATE TABLE [dbo].[SystemsTemporary]([RowID] [int] IDENTITY(1,1) NOT NULL, [RowName] [nvarchar](100) NOT NULL, [RowIP] [nvarchar](20) NULL, [MacAddress] [nvarchar](100) NULL, [Branch_ID] [int] NULL, [Status] [bit] NOT NULL, [FDateInsert] [char](10) NULL, [FDateUpdate] [char](10) NULL, [FTimeInsert] [char](5) NULL, [FTimeUpdate] [char](5) NULL, [Serial] [nvarchar](100) NULL, [IsActive] [bit] NOT NULL, [AccountingSystem] [bit] NULL, [StoreSaleSystem] [bit] NULL, [IsServer] [bit] NULL, [UserName] [nvarchar](200) NULL, [Password] [nvarchar](200) NULL, [ActivationCode] [nvarchar](200) NULL, [ServerRowID] [int] NULL, [Reversion] [int] NULL, [ClientRowID] [int] NULL, [IsSend] [tinyint] NULL, CONSTRAINT [PK_SystemsTemporary] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER VIEW [dbo].[WhDocDItems] AS SELECT dbo.WhDocD.RowID AS ID, dbo.WhDocH.Fix_WhDocType2_ID, dbo.WhDocH.MDate, dbo.WhDocH.FDate, dbo.WhDocH.RowID AS WhDocHID, dbo.Warehouse.RowName AS Warehouse, dbo.Good.RowName AS GoodName, dbo.Good.RowCode AS GoodCode, { fn IFNULL(dbo.WhDocD.GoodSize, '') } AS GoodSize, COALESCE (CASE WHEN (dbo.WhDocD.InputValue) = 0 THEN (WhDocD.OutputValue) ELSE (WhDocD.InputValue) END, 0) AS Quantity, dbo.WhDocD.UnitPrice, { fn IFNULL(dbo.WhDocD.DiscountPercent, 0) } AS DiscountPercent, { fn IFNULL(dbo.WhDocD.DiscountPrice, 0) } AS DiscountPrice, { fn IFNULL(dbo.WhDocD.TaxPercent, 0) } AS TaxPercent, { fn IFNULL(dbo.WhDocD.TaxPrice, 0) } AS TaxPrice, dbo.WhDocD.TotalPrice, { fn IFNULL(dbo.GoodCategory.RowCode, '') } AS GoodCategoryCode, { fn IFNULL(dbo.GoodCategory.RowName, '') } AS GoodCategoryName, { fn IFNULL(dbo.GoodCategory.FullRowCode, '') } AS GoodCategoryFullCode, { fn IFNULL(dbo.GoodCategory.FullRowName, '') } AS GoodCategoryFullName, { fn IFNULL(dbo.Person.RowCode, '') } AS PersonCode, { fn IFNULL(dbo.Person.RowName, '') } AS PersonName, { fn IFNULL(dbo.PersonCategory.RowName, '') } AS PersonCategory, { fn IFNULL(dbo.Province.RowName, '') } AS Province, { fn IFNULL(dbo.County.RowName, '') } AS County, { fn IFNULL(dbo.City.RowName, '') } AS City, dbo.WhDocD.SerialNumber AS Serial, dbo.WhDocD.RowDesc AS Description, dbo.WhDocH.DocNo, dbo.MeasureUnit.RowName AS MeasureUnit, dbo.WhDocH.Branch_ID AS BranchID FROM dbo.WhDocD LEFT OUTER JOIN dbo.Good ON dbo.WhDocD.Good_ID = dbo.Good.RowID LEFT OUTER JOIN dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID INNER JOIN dbo.WhDocH ON dbo.WhDocD.WhDocH_ID = dbo.WhDocH.RowID INNER JOIN dbo.MeasureUnit ON dbo.WhDocD.MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.Person ON dbo.WhDocH.Person_ID = dbo.Person.RowID LEFT OUTER JOIN dbo.PersonCategory ON dbo.Person.PersonCategory_ID = dbo.PersonCategory.RowID LEFT OUTER JOIN dbo.Province ON dbo.Person.Province_ID = dbo.Province.RowID LEFT OUTER JOIN dbo.County ON dbo.Person.County_ID = dbo.County.RowID LEFT OUTER JOIN dbo.City ON dbo.Person.City_ID = dbo.City.RowID LEFT OUTER JOIN dbo.Warehouse ON dbo.WhDocH.Warehouse_ID = dbo.Warehouse.RowID

IF NOT EXISTS(SELECT * FROM Company WHERE RowName = 'IranKish') INSERT INTO Company ([RowName]) VALUES ('IranKish')

ALTER VIEW [dbo].[WhDocDItems] AS SELECT dbo.WhDocD.RowID AS ID, dbo.WhDocH.Fix_WhDocType2_ID, dbo.WhDocH.MDate, dbo.WhDocH.FDate, dbo.WhDocH.RowID AS WhDocHID, dbo.Warehouse.RowName AS Warehouse, dbo.Good.RowName AS GoodName, dbo.Good.RowCode AS GoodCode, { fn IFNULL(dbo.WhDocD.GoodSize, '') } AS GoodSize, COALESCE (CASE WHEN (dbo.WhDocD.InputValue) = 0 THEN (WhDocD.OutputValue) ELSE (WhDocD.InputValue) END, 0) AS Quantity, dbo.WhDocD.UnitPrice, { fn IFNULL(dbo.WhDocD.DiscountPercent, 0) } AS DiscountPercent, { fn IFNULL(dbo.WhDocD.DiscountPrice, 0) } AS DiscountPrice, { fn IFNULL(dbo.WhDocD.TaxPercent, 0) } AS TaxPercent, { fn IFNULL(dbo.WhDocD.TaxPrice, 0) } AS TaxPrice, dbo.WhDocD.TotalPrice, { fn IFNULL(dbo.GoodCategory.RowCode, '') } AS GoodCategoryCode, { fn IFNULL(dbo.GoodCategory.RowName, '') } AS GoodCategoryName, { fn IFNULL(dbo.GoodCategory.FullRowCode, '') } AS GoodCategoryFullCode, { fn IFNULL(dbo.GoodCategory.FullRowName, '') } AS GoodCategoryFullName, { fn IFNULL(dbo.Person.RowCode, '') } AS PersonCode, { fn IFNULL(dbo.Person.RowName, '') } AS PersonName, { fn IFNULL(dbo.PersonCategory.RowName, '') } AS PersonCategory, { fn IFNULL(dbo.Province.RowName, '') } AS Province, { fn IFNULL(dbo.County.RowName, '') } AS County, { fn IFNULL(dbo.City.RowName, '') } AS City, dbo.WhDocD.SerialNumber AS Serial, dbo.WhDocD.RowDesc AS Description, dbo.WhDocH.DocNo, dbo.MeasureUnit.RowName AS MeasureUnit, dbo.WhDocH.Branch_ID AS BranchID, dbo.Good.RowID AS GoodID FROM dbo.WhDocD LEFT OUTER JOIN dbo.Good ON dbo.WhDocD.Good_ID = dbo.Good.RowID LEFT OUTER JOIN dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID INNER JOIN dbo.WhDocH ON dbo.WhDocD.WhDocH_ID = dbo.WhDocH.RowID INNER JOIN dbo.MeasureUnit ON dbo.WhDocD.MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.Person ON dbo.WhDocH.Person_ID = dbo.Person.RowID LEFT OUTER JOIN dbo.PersonCategory ON dbo.Person.PersonCategory_ID = dbo.PersonCategory.RowID LEFT OUTER JOIN dbo.Province ON dbo.Person.Province_ID = dbo.Province.RowID LEFT OUTER JOIN dbo.County ON dbo.Person.County_ID = dbo.County.RowID LEFT OUTER JOIN dbo.City ON dbo.Person.City_ID = dbo.City.RowID LEFT OUTER JOIN dbo.Warehouse ON dbo.WhDocH.Warehouse_ID = dbo.Warehouse.RowID

ALTER TABLE Systems ADD RestaurantSystems bit null

CREATE VIEW [dbo].[View_Goods] AS SELECT     dbo.Good.RowID AS RowIDGood, dbo.Good.GoodCategory_ID, dbo.Good.Main_MeasureUnit_ID, dbo.Good.Default_MeasureUnit_ID, dbo.Good.Fix_GoodType_ID,   dbo.Good.DiscountGroupGood_ID, dbo.Good.TaxGroupGood_ID, dbo.Good.RowName, dbo.Good.RowCode, dbo.Good.RowNameEN, dbo.Good.RowNameAlias,  dbo.Good.PurchasePrice, dbo.Good.SalePrice, dbo.Good.IMG_1, dbo.Good.OrderPoint, dbo.Good.FirstStock, dbo.Good.DiscountPrice, dbo.Good.IsActive,  dbo.Good.RowDesc, dbo.Good.InsertedBy, dbo.Good.UpdatedBy, dbo.Good.FDateInsert, dbo.Good.FDateUpdate, dbo.Good.FTimeInsert, dbo.Good.FTimeUpdate,  dbo.Good.InsertServerDateTime, dbo.Good.UpdateServerDateTime, dbo.Good.RowUpdateVersion, dbo.Good.ConvertRowID, dbo.Good.ConvertRowCode,  dbo.GoodCodingSystems.RowID AS RowIDCoding, dbo.GoodCodingSystems.Branch_ID, dbo.GoodCodingSystems.Good_ID,  dbo.GoodCodingSystems.CodingSystems_ID, dbo.GoodCodingSystems.RowCode AS RowCodeCoding, dbo.GoodCodingSystems.UniqueIdentifierValue,dbo.GoodCodingSystems.IsSend, dbo.MeasureUnit.RowName AS RowNameMeasureUnit, dbo.Good.ConsumerPrice FROM         dbo.Good INNER JOIN dbo.MeasureUnit ON dbo.Good.Main_MeasureUnit_ID = dbo.MeasureUnit.RowID AND dbo.Good.Default_MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.GoodCodingSystems ON dbo.Good.RowID = dbo.GoodCodingSystems.Good_ID WHERE  (dbo.GoodCodingSystems.RowCode IS NOT NULL)

CREATE VIEW [dbo].[GoodAllWarehouseStock] AS SELECT dbo.Good.RowID, SUM(dbo.GoodWarehouse.Stock) AS quantity FROM dbo.Good INNER JOIN dbo.GoodWarehouse ON dbo.Good.RowID = dbo.GoodWarehouse.Good_ID GROUP BY dbo.Good.RowID

ALTER TABLE [dbo].[AppSettings] ADD[CofferOpeningPrint][bit] NULL;

ALTER TABLE [dbo].[AppSettings] ADD[CofferClosingPrint][bit] NULL;

CREATE TABLE [dbo].[GoodPurchasePriceChanges]([RowID] [bigint] IDENTITY(1,1) NOT NULL, [GoodID] [int] NOT NULL, [OldPrice] [money] NULL, [NewPrice] [money] NULL, [FDate] [nvarchar](50) NULL, [MDate] [datetime] NULL, [InsertTime] [char](5) NULL, [UserID] [int] NULL, [BranchID] [int] NULL, CONSTRAINT [PK_GoodPurchasePriceChanges] PRIMARY KEY CLUSTERED ([RowID] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[GoodPurchasePriceChanges]  WITH CHECK ADD  CONSTRAINT [FK_GoodPurchasePriceChanges_Good] FOREIGN KEY([GoodID]) REFERENCES [dbo].[Good] ([RowID])

ALTER TABLE [dbo].[GoodPurchasePriceChanges] CHECK CONSTRAINT [FK_GoodPurchasePriceChanges_Good]

UPDATE POS SET IsActive = 1 WHERE IsActive IS NULL

INSERT INTO Fix_AppObject VALUES (3053, 3, N'گزارش‌ها - حسابداری - کسر و اضافه صندوقداران', '', NULL)

ALTER VIEW [dbo].[SalesFactorProfit] AS SELECT dbo.WhDocH.RowID AS ID, dbo.WhDocH.FDate, dbo.WhDocH.DocNo, dbo.Person.RowCode AS PersonCode, dbo.Person.RowName AS PersonName, ISNULL(SUM(dbo.WhDocHInvoiceIncrease.IncreasePrice), 0) AS IncreasePrice, ISNULL(SUM(dbo.WhDocHInvoiceDecrease.DecreasePrice), 0) AS DecreasePrice, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) AS SumGoodsPrice, ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) AS SumDiscountItems, dbo.WhDocH.DiscountPrice, ISNULL(SUM(dbo.WhDocHMarketerPercent.PercentPrice), 0) AS MarketerCommission, dbo.WhDocH.MDate, ISNULL(SUM(dbo.AveragePrice.Average), 0) AS SumTotalPriceAverage, ISNULL(SUM(dbo.WhDocD.PurchaseTotalPrice), 0) AS SumPurchaseTotalPrice, ISNULL(SUM(dbo.WhDocD.CostOfGood), 0) AS SumCostOfGood, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.AveragePrice.Average), 0)) AS FactorProfitOnAverage, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.WhDocD.PurchaseTotalPrice), 0)) AS FactorProfitOnPurchasePrice, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.WhDocD.CostOfGood), 0)) AS FactorProfitOnCostOfGood, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.AveragePrice.Average), 0) + ISNULL(SUM(dbo.WhDocHMarketerPercent.PercentPrice), 0)) AS TotalFactorProfitOnAverage, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.WhDocD.PurchaseTotalPrice), 0) + ISNULL(SUM(dbo.WhDocHMarketerPercent.PercentPrice), 0)) AS TotalFactorProfitOnPurchasePrice, ISNULL(SUM(dbo.WhDocD.UnitPrice * dbo.WhDocD.OutputValue), 0) - (ISNULL(SUM(dbo.WhDocD.DiscountPrice), 0) + dbo.WhDocH.DiscountPrice + ISNULL(SUM(dbo.WhDocD.CostOfGood), 0) + ISNULL(SUM(dbo.WhDocHMarketerPercent.PercentPrice), 0)) AS TotalFactorProfitOnCostOfGood FROM dbo.WhDocH INNER JOIN dbo.Person ON dbo.WhDocH.Person_ID = dbo.Person.RowID LEFT OUTER JOIN dbo.WhDocD ON dbo.WhDocH.RowID = dbo.WhDocD.WhDocH_ID LEFT OUTER JOIN dbo.AveragePrice ON dbo.whdocd.Good_ID = dbo.AveragePrice.Good_ID LEFT OUTER JOIN dbo.WhDocHInvoiceIncrease ON dbo.WhDocH.RowID = dbo.WhDocHInvoiceIncrease.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHInvoiceDecrease ON dbo.WhDocH.RowID = dbo.WhDocHInvoiceDecrease.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHMarketerPercent ON dbo.WhDocH.RowID = dbo.WhDocHMarketerPercent.WhDocH_ID WHERE (dbo.WhDocH.Fix_WhDocType2_ID = 21) GROUP BY dbo.WhDocH.RowID, dbo.WhDocH.FDate, dbo.WhDocH.DocNo, dbo.Person.RowCode, dbo.Person.RowName, dbo.WhDocH.DiscountPrice, dbo.WhDocH.MDate

CREATE VIEW [dbo].[FactorDocNumbers] AS SELECT MAX(dbo.WhDocH.DocNo) AS MaxDocNo, Fix_WhDocType2_ID FROM dbo.WhDocH GROUP BY Fix_WhDocType2_ID

ALTER VIEW [dbo].[AveragePrice] AS SELECT G.RowID GoodID, CAST(SUM(D.PurchaseTotalPrice) / COALESCE (CASE WHEN SUM(D.CalculatedInputValue) = 0 THEN 1 ELSE SUM(D.CalculatedInputValue) END, 1) AS money) PurchaseAverage FROM Good G LEFT OUTER JOIN WhDocD D ON G.RowID = D.Good_ID LEFT OUTER JOIN WhDocH H ON D.WhDocH_ID = H.RowID WHERE (H.Fix_WhDocType2_ID = 1 or H.Fix_WhDocType2_ID = 6) GROUP BY G.RowID

DROP VIEW SalesFactorProfit

CREATE VIEW SalesFactorProfit AS SELECT H.RowID ID, H.DocNo, H.FDate, H.MDate, P.RowName PersonName, P.RowCode PersonCode, {fn IFNULL(SUM(Increase.IncreasePrice), 0)} IncreasePrice, { fn IFNULL(SUM(Decrease.DecreasePrice), 0)} DecreasePrice, SUM(D.QuantityInUnitPrice) SumGoodsPrice, SUM(D.DiscountPrice) SumDiscountItems, H.DiscountPrice, {fn IFNULL(SUM(Marketer.PercentPrice), 0)} MarketerCommission, SUM(D.PurchaseTotalPrice) SumPurchaseTotalPrice, {fn IFNULL(SUM(D.CostOfGood), 0)} SumCostOfGood, { fn IFNULL(SUM(D.PurchaseTotalPriceAverage), 0) } AS SumTotalPriceAverage, SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + {fn IFNULL(SUM(D.PurchaseTotalPriceAverage), 0)}) FactorProfitOnAverage, SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + SUM(D.PurchaseTotalPrice)) FactorProfitOnPurchasePrice, SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + {fn IFNULL(SUM(D.CostOfGood), 0)}) FactorProfitOnCostOfGood, SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + SUM(D.DiscountPrice) + {fn IFNULL(SUM(D.PurchaseTotalPriceAverage), 0)} + {fn IFNULL(SUM(Marketer.PercentPrice), 0)}) TotalFactorProfitOnAverage, SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + SUM(D.DiscountPrice) + {fn IFNULL(SUM(D.PurchaseTotalPrice), 0)} + {fn IFNULL(SUM(Marketer.PercentPrice), 0)}) TotalFactorProfitOnPurchasePrice, SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + SUM(D.DiscountPrice) + {fn IFNULL(SUM(D.CostOfGood), 0)} + {fn IFNULL(SUM(Marketer.PercentPrice), 0)}) TotalFactorProfitOnCostOfGood FROM WhDocH H LEFT OUTER JOIN WhDocD D ON H.RowID = D.WhDocH_ID LEFT OUTER JOIN Person P ON H.Person_ID = P.RowID LEFT OUTER JOIN WhDocHInvoiceIncrease Increase ON H.RowID = Increase.WhDocH_ID LEFT OUTER JOIN WhDocHInvoiceDecrease Decrease ON H.RowID = Decrease.WhDocH_ID LEFT OUTER JOIN WhDocHMarketerPercent Marketer ON H.RowID = Marketer.WhDocH_ID WHERE H.Fix_WhDocType2_ID = 21 GROUP BY H.RowID, H.FDate, H.DocNo, P.RowName, P.RowCode, H.DiscountPrice, H.MDate

DROP VIEW GoodStockReportAllWarehouse;

CREATE VIEW [dbo].[GoodStockReportAllWarehouse] AS SELECT dbo.Good.RowCode AS ProductBarcode, dbo.Good.RowName AS ProductName, { fn IFNULL(dbo.Good.RowNameEN, '') } AS LatinProductName, { fn IFNULL(dbo.Good.RowNameAlias, '') } AS ProductNickName, dbo.Good.RowID AS ID, dbo.MeasureUnit.RowName AS SmallestUnit, { fn IFNULL(dbo.GoodCategory.RowName, '') } AS CategoryName, dbo.Good.SalePrice, dbo.Good.PurchasePrice, dbo.StockAllWarehouse.stock * dbo.Good.SalePrice AS ValueBasedOnSale, dbo.StockAllWarehouse.stock * dbo.Good.PurchasePrice AS ValueBasedOnPurchase, dbo.StockAllWarehouse.stock AS Quantity, dbo.AveragePrice.PurchaseAverage, dbo.StockAllWarehouse.stock * dbo.AveragePrice.PurchaseAverage AS ValueBasedOnAverage, dbo.Good.OrderPoint FROM dbo.Good INNER JOIN dbo.MeasureUnit ON dbo.Good.Main_MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.StockAllWarehouse ON dbo.Good.RowID = dbo.StockAllWarehouse.Good_ID LEFT OUTER JOIN dbo.AveragePrice ON dbo.Good.RowID = dbo.AveragePrice.GoodID LEFT OUTER JOIN dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID

DROP VIEW SimpleDocsDetails

DROP VIEW GoodStockReport

CREATE VIEW [dbo].[GoodStockReport] AS SELECT dbo.Good.RowCode AS ProductBarcode, dbo.Good.RowName AS ProductName,dbo.Good.Main_MeasureUnit_ID, { fn IFNULL(dbo.Good.RowNameEN, '') } AS LatinProductName, { fn IFNULL(dbo.Good.RowNameAlias, '') } AS ProductNickName, dbo.AllWarehouseQuantityView.Quantity, dbo.Good.RowID AS ID, dbo.MeasureUnit.RowName AS SmallestUnit, dbo.AllWarehouseQuantityView.Warehouse_ID, dbo.AllWarehouseQuantityView.FinancialYear_ID, { fn IFNULL(dbo.GoodCategory.RowName, '') } AS CategoryName, dbo.Good.SalePrice, dbo.Good.PurchasePrice, dbo.AllWarehouseQuantityView.Quantity * dbo.Good.SalePrice AS ValueBasedOnSale, dbo.AllWarehouseQuantityView.Quantity * dbo.Good.PurchasePrice AS ValueBasedOnPurchase, dbo.AveragePrice.PurchaseAverage, dbo.AllWarehouseQuantityView.Quantity * dbo.AveragePrice.PurchaseAverage AS ValueBasedOnAverage, dbo.Warehouse.RowName AS Warehouse, dbo.Good.OrderPoint FROM dbo.Good INNER JOIN dbo.AllWarehouseQuantityView ON dbo.Good.RowID = dbo.AllWarehouseQuantityView.ID INNER JOIN dbo.Warehouse ON dbo.AllWarehouseQuantityView.Warehouse_ID = dbo.Warehouse.RowID INNER JOIN dbo.MeasureUnit ON dbo.Good.Main_MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.AveragePrice ON dbo.Good.RowID = dbo.AveragePrice.GoodID LEFT OUTER JOIN dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID

ALTER TABLE DocD ADD DocRowDesc nvarchar(500) NULL

UPDATE D SET D.DocRowDesc = {FN IFNULL(W.RowDesc, '')} FROM DocD D INNER JOIN DocH H ON D.DocH_ID = H.RowID LEFT OUTER JOIN WhDocH W ON H.RelateRowID = W.RowID WHERE H.Fix_DocHType_ID >= 1 AND H.Fix_DocHType_ID <= 4

UPDATE D SET D.DocRowDesc = {FN IFNULL(C.RowDesc, '')} FROM DocD D INNER JOIN DocH H ON D.DocH_ID = H.RowID LEFT OUTER JOIN ChequePayD C ON H.RelateRowID = C.RowID WHERE H.Fix_DocHType_ID = 5 OR H.Fix_DocHType_ID = 6

UPDATE D SET D.DocRowDesc = {FN IFNULL(C.RowDesc, '')} FROM DocD D INNER JOIN DocH H ON D.DocH_ID = H.RowID LEFT OUTER JOIN ChequeRecH C ON H.RelateRowID = C.RowID WHERE H.Fix_DocHType_ID = 7

UPDATE D SET D.DocRowDesc = {FN IFNULL(C.RowDesc, '')} FROM DocD D INNER JOIN DocH H ON D.DocH_ID = H.RowID LEFT OUTER JOIN ChequeRecD C ON H.RelateRowID = C.RowID WHERE H.Fix_DocHType_ID >= 8 AND H.Fix_DocHType_ID <= 13

UPDATE D SET D.DocRowDesc = {FN IFNULL(S.RowDesc, '')} FROM DocD D INNER JOIN DocH H ON D.DocH_ID = H.RowID LEFT OUTER JOIN SimpleDoc S ON H.RelateRowID = S.RowID WHERE H.Fix_DocHType_ID = 14

CREATE TABLE [dbo].[DeletedInvoicesH]([RowID] [int] IDENTITY(1,1) NOT NULL, [WhDocHID] [int] NOT NULL, [Branch_ID] [int] NULL, [FinancialYear_ID] [smallint] NOT NULL, [Fix_RecordStatusType_ID] [tinyint] NOT NULL, [Fix_WhDocType1_ID] [tinyint] NOT NULL, [Fix_WhDocType2_ID] [tinyint] NOT NULL, [Warehouse_ID] [smallint] NOT NULL, [Fix_DiscountType_ID] [tinyint] NULL, [Coffer_ID] [int] NULL, [Person_ID] [int] NULL, [ManufactureDocH_ID] [int] NULL, [ShiftDocH_ID] [int] NULL, [WarehouseComputationDocH_ID] [int] NULL, [DocNo] [bigint] NOT NULL, [FDate] [char](10) NOT NULL, [MDate] [datetime] NULL, [DiscountPercent] [float] NULL, [DiscountPrice] [money] NULL, [SettlePrice] [money] NULL, [TaxPercent] [float] NULL, [TaxPrice] [money] NULL, [TotalPrice] [money] NULL, [QuantityInUnitPrice] [money] NULL, [TotalDiscountPriceItems] [money] NULL, [TotalTaxPriceItems] [money] NULL, [TotalIncreasePrice] [money] NULL, [TotalDecreasePrice] [money] NULL, [PayablePrice] [money] NULL, [DefinedBySystem] [bit] NOT NULL, [ReferenceValue] [nvarchar](100) NULL, [RowDesc] [nvarchar](500) NULL, [InsertedBy] [smallint] NULL, [UpdatedBy] [smallint] NULL, [FDateInsert] [char](10) NULL, [FDateUpdate] [char](10) NULL, [FTimeInsert] [char](5) NULL, [FTimeUpdate] [char](5) NULL, [InsertServerDateTime] [datetime] NULL, [UpdateServerDateTime] [datetime] NULL, [RowUpdateVersion] [smallint] NULL, [UniqueIdentifierValue] [uniqueidentifier] NULL, [IsSend] [tinyint] NULL, [Good_ID] [int] NULL, [SumPercentPrice] [money] NULL, [FDateSettlementDeadline] [char](10) NULL, [MDateSettlementDeadline] [datetime] NULL, [IsSettled] [bit] NULL, [OrderH_ID] [bigint] NULL, [Times] [time](0) NULL, [Table_ID] [int] NULL, [DeliveryType_ID] [tinyint] NULL, [ClientRowID] [int] NULL, [DeletedBy] [smallint] NULL, [DeleteFDate] [char](10) NULL, [DeleteMDate] [datetime] NULL, [DeleteTime] [char](5) NULL, CONSTRAINT [PK_DeletedInvoicesH] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

CREATE TABLE [dbo].[DeletedInvoicesD]([RowID] [int] IDENTITY(1,1) NOT NULL, [DeletedInvoicesHID] [int] NOT NULL, [Branch_ID] [int] NULL, [FinancialYear_ID] [smallint] NOT NULL, [WhDocH_ID] [int] NOT NULL, [Good_ID] [int] NOT NULL, [MeasureUnit_ID] [int] NULL, [Main_MeasureUnit_ID] [int] NULL, [Fix_DiscountType_ID] [tinyint] NULL, [InputValue] [float] NOT NULL, [CalculatedInputValue] [float] NOT NULL, [OutputValue] [float] NOT NULL, [CalculatedOutputValue] [float] NOT NULL, [UnitPrice] [money] NOT NULL, [TotalPrice] [money] NOT NULL, [QuantityInUnitPrice] [money] NULL, [DiscountPercent] [float] NULL, [DiscountPrice] [money] NULL, [PurchaseUnitPrice] [money] NULL, [PurchaseTotalPrice] [money] NULL, [TaxPercent] [float] NULL, [TaxPrice] [money] NULL, [SaleUnitPrice] [money] NULL, [RowDesc] [nvarchar](500) NULL, [UniqueIdentifierValue] [uniqueidentifier] NULL, [IsSend] [tinyint] NULL, [PurchaseTotalPriceAverage] [money] NULL, [GoodSize] [nvarchar](50) NULL, [SortInDocH] [bigint] NULL, [SerialNumber] [nvarchar](50) NULL, [CostOfGood] [money] NULL, [ClientRowID] [int] NULL, CONSTRAINT [PK_DeletedInvoicesD] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

ALTER TABLE [dbo].[DeletedInvoicesD]  WITH CHECK ADD  CONSTRAINT [FK_DeletedInvoicesD_DeletedInvoicesH] FOREIGN KEY([DeletedInvoicesHID]) REFERENCES [dbo].[DeletedInvoicesH] ([RowID])

ALTER TABLE [dbo].[DeletedInvoicesD] CHECK CONSTRAINT [FK_DeletedInvoicesD_DeletedInvoicesH]

CREATE VIEW [dbo].[GoodSaleByCategoryView] AS SELECT g.RowID AS ID, g.RowCode AS GoodCode, g.RowName AS GoodName, CAST(h.MDate AS DATE) AS MDate, COUNT(d.WhDocH_ID) AS FactorsCount, SUM(d.CalculatedOutputValue) AS ItemsCount, SUM(d.TotalPrice) AS TotalPrice, g.SalePrice, d.Branch_ID AS BranchID, g.GoodCategory_ID AS CategoryID, m.RowName AS MeasureUnit, h.Fix_WhDocType2_ID AS WhDocType, p.RowCode AS PersonCode, p.RowName AS PersonName FROM dbo.WhDocD AS d INNER JOIN dbo.Good AS g ON d.Good_ID = g.RowID INNER JOIN dbo.MeasureUnit AS m ON g.Main_MeasureUnit_ID = m.RowID INNER JOIN dbo.WhDocH AS h ON d.WhDocH_ID = h.RowID INNER JOIN dbo.Person AS p ON h.Person_ID = p.RowID GROUP BY d.Good_ID, g.RowID, g.RowCode, g.RowName, CAST(h.MDate AS DATE), g.SalePrice, d.Branch_ID, g.GoodCategory_ID, m.RowName, h.Fix_WhDocType2_ID, p.RowCode, p.RowName

CREATE VIEW [dbo].[SimpleDocsDetails] AS SELECT doc.RowID, doc.FDate, doc.MDate,doc.FinancialYear_ID, doc.DocPrice, type.RowName AS DocType, doc.Regard, doc.RowDesc AS Description, doc.Fix_SimpleDocType_ID AS SimpleDocType_ID, CASE WHEN type.RowID = 1 OR type.RowID = 4 OR type.RowID = 6 OR type.RowID = 17 OR type.RowID = 20 THEN CofferDebt.RowName WHEN type.RowID = 2 OR type.RowID = 3 OR type.RowID = 7 OR type.RowID = 14 OR type.RowID = 18 OR type.RowID = 22 OR type.RowID = 23 OR type.RowID = 25 OR type.RowID = 34 OR type.RowID = 36 THEN PersonDebt.RowName WHEN type.RowID = 5 OR type.RowID = 8 OR type.RowID = 9 OR type.RowID = 13 OR type.RowID = 19 OR type.RowID = 21 OR type.RowID = 27 THEN BankDebt.RowName WHEN type.RowID = 10 OR type.RowID = 11 OR type.RowID = 15 OR type.RowID = 29 THEN ExpenseType.RowName WHEN type.RowID = 16 THEN 'فروش' WHEN type.RowID = 26 OR type.RowID = 28 OR type.RowID = 30 OR type.RowID = 32 OR type.RowID = 33 OR type.RowID = 35 THEN TreasuryDebt.RowName ELSE '' END AS Debtor, CASE WHEN type.RowID = 2 OR type.RowID = 3 OR type.RowID = 5 OR type.RowID = 10 OR type.RowID = 17 OR type.RowID = 22 OR type.RowID = 30 THEN CofferCredit.RowName WHEN type.RowID = 1 OR type.RowID = 4 OR type.RowID = 8 OR type.RowID = 13 OR type.RowID = 16 OR type.RowID = 20 OR type.RowID = 21 OR type.RowID = 26 OR type.RowID = 33 OR type.RowID = 35 THEN PersonCredit.RowName WHEN type.RowID = 6 OR type.RowID = 7 OR type.RowID = 9 OR type.RowID = 11 OR type.RowID = 14 OR type.RowID = 23 OR type.RowID = 28 THEN BankCredit.RowName WHEN type.RowID = 15 THEN 'چک' WHEN type.RowID = 25 OR type.RowID = 27 OR type.RowID = 29 OR type.RowID = 32 OR type.RowID = 34 OR type.RowID = 36 THEN TreasuryCredit.RowName WHEN type.RowID = 18 OR type.RowID = 19 THEN IncomeType.RowName ELSE '' END AS Creditor FROM dbo.SimpleDoc AS doc INNER JOIN dbo.Fix_SimpleDocType AS type ON doc.Fix_SimpleDocType_ID = type.RowID LEFT OUTER JOIN dbo.Coffer AS CofferDebt ON doc.AccID_Debtor = CofferDebt.RowID LEFT OUTER JOIN dbo.Person AS PersonDebt ON doc.AccID_Debtor = PersonDebt.RowID LEFT OUTER JOIN dbo.BankAccount AS BankDebt ON doc.AccID_Debtor = BankDebt.RowID LEFT OUTER JOIN dbo.ExpenseType ON doc.AccID_Debtor = dbo.ExpenseType.RowID LEFT OUTER JOIN dbo.Treasury AS TreasuryDebt ON doc.AccID_Debtor = TreasuryDebt.RowID LEFT OUTER JOIN dbo.IncomeType ON doc.AccID_Creditor = dbo.IncomeType.RowID LEFT OUTER JOIN dbo.Coffer AS CofferCredit ON doc.AccID_Creditor = CofferCredit.RowID LEFT OUTER JOIN dbo.Person AS PersonCredit ON doc.AccID_Creditor = PersonCredit.RowID LEFT OUTER JOIN dbo.BankAccount AS BankCredit ON doc.AccID_Creditor = BankCredit.RowID LEFT OUTER JOIN dbo.Treasury AS TreasuryCredit ON doc.AccID_Creditor = TreasuryCredit.RowID

insert Fix_SimpleDocType (RowID, RowName) values (37, N'پرداخت به طرف حساب ترکیبی')

insert Fix_SimpleDocType (RowID, RowName) values (38, N'دریافت از طرف حساب ترکیبی')

update Fix_SimpleDocType set RowName = N'دریافت از طرف حساب ترکیبی' where RowID = 38

CREATE TABLE [dbo].[SimpleDocBankAccount]([RowID] [int] IDENTITY(1,1) NOT NULL, [Branch_ID] [int] NULL, [SimpleDoc_ID] [int] NOT NULL, [BankAccount_ID] [int] NOT NULL, [Price] [money] NOT NULL, [UniqueIdentifierValue] [uniqueidentifier] NULL, [IsSend] [tinyint] NULL, [POS_ID] [int] NULL, CONSTRAINT [PK_SimpleDocBankAccount] PRIMARY KEY CLUSTERED ([RowID] ASC)) ON [PRIMARY]

ALTER TABLE [dbo].[SimpleDocBankAccount]  WITH CHECK ADD  CONSTRAINT [FK_SimpleDocBankAccount_BankAccount] FOREIGN KEY([BankAccount_ID]) REFERENCES [dbo].[BankAccount] ([RowID])

ALTER TABLE [dbo].[SimpleDocBankAccount]  WITH CHECK ADD  CONSTRAINT [FK_SimpleDocBankAccount_Branch] FOREIGN KEY([Branch_ID]) REFERENCES [dbo].[Branch] ([RowID])

ALTER TABLE [dbo].[SimpleDocBankAccount]  WITH CHECK ADD  CONSTRAINT [FK_SimpleDocBankAccount_POS] FOREIGN KEY([POS_ID]) REFERENCES [dbo].[POS] ([RowID])

ALTER TABLE [dbo].[SimpleDocBankAccount]  WITH NOCHECK ADD  CONSTRAINT [FK_SimpleDocBankAccount_SimpleDoc] FOREIGN KEY([SimpleDoc_ID]) REFERENCES [dbo].[SimpleDoc] ([RowID]) ON DELETE CASCADE

alter table InvoiceMemory add MeasureUnitID int null

alter table SendInvoiceD add MeasureUnitID int null

CREATE TABLE SimpleDocChequeRecH(RowID int IDENTITY(1,1) NOT NULL, Branch_ID int NULL, SimpleDoc_ID int NULL, ChequeRecH_ID int NULL, ChequeRecD_ID int NULL, UniqueIdentifierValue uniqueidentifier NULL, IsSend tinyint NULL, CONSTRAINT PK_SimpleDocChequeRecH PRIMARY KEY (RowID), CONSTRAINT FK_SimpleDocChequeRecH_Branch FOREIGN KEY (Branch_ID) REFERENCES Branch(RowID), CONSTRAINT FK_SimpleDocChequeRecH_SimpleDoc FOREIGN KEY (SimpleDoc_ID) REFERENCES SimpleDoc(RowID), CONSTRAINT FK_SimpleDocChequeRecH_ChequeRecH FOREIGN KEY (ChequeRecH_ID) REFERENCES ChequeRecH(RowID), CONSTRAINT FK_SimpleDocChequeRecH_ChequeRecD FOREIGN KEY (ChequeRecD_ID) REFERENCES ChequeRecD(RowID));

insert into Fix_AppObject (RowID, Fix_AppObjectType_ID, RowName) values (5092, 5, N'مجوز افزودن - سند پرداخت به طرف حساب ترکیبی')

insert into Fix_AppObject (RowID, Fix_AppObjectType_ID, RowName) values (6092, 6, N'مجوز ویرایش - سند پرداخت به طرف حساب ترکیبی')

insert into Fix_AppObject (RowID, Fix_AppObjectType_ID, RowName) values (7092, 7, N'مجوز حذف - سند پرداخت به طرف حساب ترکیبی')

insert into Fix_AppObject (RowID, Fix_AppObjectType_ID, RowName) values (5093, 5, N'مجوز افزودن - سند دریافت از طرف حساب ترکیبی')

insert into Fix_AppObject (RowID, Fix_AppObjectType_ID, RowName) values (6093, 6, N'مجوز ویرایش - سند دریافت از طرف حساب ترکیبی')

insert into Fix_AppObject (RowID, Fix_AppObjectType_ID, RowName) values (7093, 7, N'مجوز حذف - سند دریافت از طرف حساب ترکیبی')

alter table WhDocH add DailyDocNo bigint null

delete WhDocD where WhDocH_ID = 0

alter table Person add SubscriptionCode nvarchar(100) null

insert Fix_AppObject (RowID, Fix_AppObjectType_ID, RowName) values(3054, 3, N'گزارش‌ها - ترازنامه')

insert Fix_AppObject (RowID, Fix_AppObjectType_ID, RowName) values(3055, 3, N'گزارش‌ها - ترازنامه - تراز آزمایشی')

insert Fix_SimpleDocType (RowID, RowName) values(39, N'سند افتتاحیه')

insert Fix_DocHType (RowID, RowName) values(25, N'افتتاحیه')

insert Fix_Acc1 (RowID, RowName, DefinedBySystem) values(15, N'موجودی کالاها', 1)

alter table DocD add RelateRowID bigint null

CREATE TABLE Fix_CustomerSearchType(RowID tinyint IDENTITY(1,1) NOT NULL, RowName nvarchar(100) NULL, RowDesc nvarchar(500) NULL, UniqueIdentifierValue uniqueidentifier NULL);

ALTER TABLE AppUserSettings ADD CustomerSearchTypeID TINYINT NULL

if not exists (select RowName from Fix_CustomerSearchType where RowName = N'کد اشتراک') insert Fix_CustomerSearchType (RowName) values(N'کد اشتراک')

if not exists (select RowName from Fix_CustomerSearchType where RowName = N'کد ملی') insert Fix_CustomerSearchType (RowName) values(N'کد ملی')

if not exists (select RowName from Fix_CustomerSearchType where RowName = N'شماره همراه') insert Fix_CustomerSearchType (RowName) values(N'شماره همراه')

ALTER TABLE AppUserSettings ADD AllowPersonSearch BIT NULL

ALTER TABLE Person ADD BankName1 NVARCHAR(300) NULL

ALTER TABLE Person ADD BankName2 NVARCHAR(300) NULL

ALTER TABLE Person ADD BankName3 NVARCHAR(300) NULL

ALTER TABLE Person ADD CreditSelling BIT NULL

ALTER TABLE WhDocD ADD Discount1Percent FLOAT NULL

ALTER TABLE WhDocD ADD Discount1Price money NULL

ALTER TABLE WhDocD ADD Discount2Percent FLOAT NULL

ALTER TABLE WhDocD ADD Discount2Price money NULL

UPDATE Good SET MaxCount = 20000 WHERE MaxCount IS NULL

IF NOT EXISTS (SELECT RowName FROM Fix_CustomerSearchType WHERE RowName = N'همه‌ی موارد') INSERT [dbo].[Fix_CustomerSearchType] (RowName) VALUES (N'همه‌ی موارد')

ALTER TABLE Good ADD SpecialCategory BIT NULL

ALTER TABLE Good ADD ShowInStoreInvoice BIT NULL

ALTER VIEW [dbo].[GoodView] AS SELECT dbo.Good.RowID AS ID, dbo.Good.RowName AS ProductName, dbo.Good.RowCode AS ProductBarcode, dbo.Good.PurchasePrice, dbo.Good.SalePrice, dbo.Good.InterestRate, dbo.Good.ConsumerPrice, dbo.Good.OrderPoint, { fn IFNULL(dbo.Good.RowNameEN, '') } AS LatinProductName, { fn IFNULL(dbo.Good.RowNameAlias, '') } AS ProductNickName, dbo.Good.FirstStock, dbo.Good.DiscountPrice, dbo.Good.FDate AS DateInsert, dbo.Good.RowDesc AS Description, dbo.Good.IsHasSize, dbo.Good.IsActive, dbo.Good.MDate, dbo.Good.Fix_GoodType_ID, dbo.Good.RowCode, dbo.Good.MaxCount, dbo.Good.GoodCategory_ID AS GoodCategoryID, dbo.MeasureUnit.RowName AS SmallestUnit, MeasureUnit_1.RowName AS DefaultUnit, { fn IFNULL(dbo.DiscountGroupGood.RowName, '') } AS DiscountGroupGood, { fn IFNULL(dbo.TaxGroupGood.RowName, '') } AS TaxGroupGood, { fn IFNULL(dbo.GoodCategory.RowName, '') } AS CategoryName, dbo.Good.Reversion, { fn IFNULL(dbo.Good.MultipleSalePrice, 0) } AS MultipleSalePrice FROM dbo.Good LEFT OUTER JOIN dbo.MeasureUnit ON dbo.Good.Main_MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.MeasureUnit AS MeasureUnit_1 ON dbo.Good.Default_MeasureUnit_ID = MeasureUnit_1.RowID LEFT OUTER JOIN dbo.DiscountGroupGood ON dbo.Good.DiscountGroupGood_ID = dbo.DiscountGroupGood.RowID LEFT OUTER JOIN dbo.TaxGroupGood ON dbo.Good.TaxGroupGood_ID = dbo.TaxGroupGood.RowID LEFT OUTER JOIN dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID

ALTER TABLE DocD ADD Color nvarchar(50) NULL

ALTER TABLE [dbo].[CofferSettlementD] ADD [DiscountPrice] money NULL

ALTER VIEW [dbo].[PersonAccountView] AS SELECT dbo.Person.RowName AS Name, dbo.Person.RowCode AS Code, dbo.Person.MobileNumber AS Mobile, dbo.Person.PhoneNumber AS Phone, dbo.Person.NationalCode, dbo.Person.AccountNumber1, dbo.Person.AccountNumber2, dbo.Person.AccountNumber3, dbo.Person.Adress AS Address, dbo.Person.MobileForSMS, dbo.Person.FatherName, dbo.Person.EconomicCode, ABS(dbo.PersonRemainderView.Remainder) AS Remainder, dbo.PersonRemainderView.int, dbo.Person.RowID AS PersonID, { fn IFNULL(dbo.Person.FDateLastSMS_ForDebt, '') } AS LastSmsDate, dbo.Person.MDateLastSMS_ForDebt AS LastSmsDateTime, dbo.Person.Fix_PersonType_ID, dbo.Fix_PersonType.RowName PersonType FROM dbo.PersonRemainderView INNER JOIN dbo.Person ON dbo.PersonRemainderView.PersonID = dbo.Person.RowID LEFT OUTER JOIN dbo.Fix_PersonType ON dbo.Person.Fix_PersonType_ID = dbo.Fix_PersonType.RowID

ALTER TABLE [dbo].[POS] ADD [Protocol] NVARCHAR(100) NULL

INSERT INTO [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES(2051, 2, N'عملیات - فاکتورها - صندوق فروش رستورانی')

INSERT INTO [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES(2052, 2, N'عملیات - امور مالی - سند افتتاحیه')

INSERT INTO [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES(2053, 2, N'عملیات - امور مالی - سند دستی')

INSERT INTO [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES(3056, 3, N'گزارش‌ها - فاکتورها - آمار فروش بر اساس مشتری')

INSERT INTO [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES(3057, 3, N'گزارش‌ها - فاکتورها - آمار فروش کالاها بر اساس دسته‌بندی')

INSERT INTO [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES(3058, 3, N'گزارش‌ها - چک‌ها - پرونده چک')

INSERT INTO [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES(3059, 3, N'گزارش‌ها - چک‌ها - هشدار چک‌های پرداختی')

INSERT INTO [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName]) VALUES(3060, 3, N'گزارش‌ها - چک‌ها - هشدار چک‌های دریافتی')

IF NOT EXISTS(SELECT * FROM Fix_Acc1 WHERE RowName = N'کسر و اضافه') INSERT INTO Fix_Acc1 (RowID, RowName, DefinedBySystem) VALUES(16, N'کسر و اضافه', 1)

UPDATE [dbo].[DocD] SET [Fix_Acc1_ID] = 16, [Fix_Acc2Type_ID] = 0 WHERE Fix_Acc1_ID = 7 AND Fix_Acc2Type_ID = 9

ALTER TABLE [dbo].[CofferSettlementPos] ADD [SystemPrice] money NULL;

ALTER TABLE [dbo].[DocD] ADD [Description] ntext NULL;

CREATE PROCEDURE DeleteDocD(@dochid bigint) AS BEGIN delete from DocD where DocH_ID = @dochid END

DROP VIEW [dbo].[PersonAccountView]

CREATE VIEW [dbo].[PersonAccountView] AS SELECT dbo.Person.RowName AS Name, dbo.Person.RowCode AS Code, dbo.Person.MobileNumber AS Mobile, dbo.Person.PhoneNumber AS Phone, dbo.Person.NationalCode, dbo.Person.AccountNumber1,dbo.Person.AccountNumber2, dbo.Person.AccountNumber3, dbo.Person.Adress AS Address, dbo.Person.MobileForSMS, dbo.Person.FatherName, dbo.Person.EconomicCode, ABS(dbo.PersonRemainderView.Remainder)    AS Remainder, dbo.PersonRemainderView.int, dbo.Person.RowID AS PersonID, { fn IFNULL(dbo.Person.FDateLastSMS_ForDebt, '') } AS LastSmsDate, dbo.Person.MDateLastSMS_ForDebt AS LastSmsDateTime,    dbo.Person.Fix_PersonType_ID, dbo.Fix_PersonType.RowName AS PersonType, dbo.PersonCategory.RowName AS PersonCategory  FROM  dbo.PersonRemainderView INNER JOIN   dbo.Person ON dbo.PersonRemainderView.PersonID = dbo.Person.RowID LEFT OUTER JOIN  dbo.PersonCategory ON dbo.Person.PersonCategory_ID = dbo.PersonCategory.RowID LEFT OUTER JOIN  dbo.Fix_PersonType ON dbo.Person.Fix_PersonType_ID = dbo.Fix_PersonType.RowID 

ALTER TABLE [dbo].[QuickAccess] ADD [FrmBankToCoffer] [bit] null;

ALTER TABLE [dbo].[QuickAccess] ADD [FrmCofferToBank] [bit] null;

ALTER TABLE [dbo].[QuickAccess] ADD [FrmPaymentTheBank] [bit] null;

ALTER TABLE [dbo].[QuickAccess] ADD [FrmPaymentTheCoffer] [bit] null;

ALTER TABLE [dbo].[QuickAccess] ADD [FrmPaymentThetreasury] [bit] null;

ALTER TABLE [dbo].[QuickAccess] ADD [FrmPaymentTheCheq] [bit] null;

ALTER TABLE [dbo].[QuickAccess] ADD [FrmPaymentCombinedAccount] [bit] null;

ALTER TABLE [dbo].[QuickAccess] ADD [FrmReceivedCombinedAccount] [bit] null;

ALTER TABLE [dbo].[QuickAccess] ADD [FrmFinancialDocument] [bit] null;

ALTER TABLE [dbo].[QuickAccess] ADD [FrmSettlementCoffer] [bit] null;

ALTER TABLE [dbo].[QuickAccess] ADD [FrmManualDocument] [bit] null;

ALTER TABLE [dbo].[Fix_Acc3] ADD[RowLevel][int] NULL;

ALTER TABLE [dbo].[Fix_Acc3] ADD[FatherID][int] NULL;

ALTER TABLE [dbo].[Fix_Acc3] ADD[FullName][nvarchar](200) NULL;

ALTER TABLE [dbo].[Fix_Acc3] ADD[FullCode][nvarchar](200) NULL;

ALTER TABLE [dbo].[AppUserSettings] ADD [CofferSale] [bit] null;

ALTER TABLE [dbo].[AppUserSettings] ADD [CofferSaleAlways] [bit] null;

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (3061, 3, N'گزارش ها-سابقه بستن صندوق', NULL, NULL);

CREATE TABLE [dbo].[Fix_CashType]([RowID] [smallint] NOT NULL,[RowName] [nvarchar](50) NULL,[RowDesc] [ntext] NULL,CONSTRAINT [PK_Fix_CashType] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

INSERT [dbo].[Fix_CashType]([RowID],[RowName],[RowDesc]) VALUES(1,N'صندوق',N'صندوق')

CREATE TABLE [dbo].[WhDocHCashPayment]([RowID] [bigint] IDENTITY(1,1) NOT NULL,[WhDoch_ID] [int] NULL,[CashType] [smallint] NULL,[CashAccountID] [int] NULL,[Price] [money] NULL,[RowDesc] [ntext] NULL,[UniqueIdentifierValue] [uniqueidentifier] NULL,[Branch_ID] [int] NULL,CONSTRAINT [PK_WhDocHCashPayment] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

ALTER TABLE [dbo].[WhDocHCashPayment]  WITH CHECK ADD  CONSTRAINT [FK_WhDocHCashPayment_Branch] FOREIGN KEY([Branch_ID])REFERENCES [dbo].[Branch] ([RowID])

ALTER TABLE [dbo].[WhDocHCashPayment] CHECK CONSTRAINT [FK_WhDocHCashPayment_Branch]

ALTER TABLE [dbo].[WhDocHCashPayment]  WITH CHECK ADD  CONSTRAINT [FK_WhDocHCashPayment_Fix_CashType] FOREIGN KEY([CashType])REFERENCES [dbo].[Fix_CashType] ([RowID])

ALTER TABLE [dbo].[WhDocHCashPayment] CHECK CONSTRAINT [FK_WhDocHCashPayment_Fix_CashType]

ALTER TABLE [dbo].[WhDocHCashPayment]  WITH CHECK ADD  CONSTRAINT [FK_WhDocHCashPayment_WhDocH] FOREIGN KEY([WhDoch_ID])REFERENCES [dbo].[WhDocH] ([RowID])

ALTER TABLE [dbo].[WhDocHCashPayment] CHECK CONSTRAINT [FK_WhDocHCashPayment_WhDocH]

ALTER TABLE dbo.WhDocH ADD [RelatedCloseSettlement] NULL;

ALTER TABLE dbo.SimpleDoc ADD [RelatedCloseSettlement] [bigint] NULL;

ALTER TABLE dbo.SimpleDoc ADD[DocumentsPrice] money NULL;

ALTER TABLE dbo.CofferSettlementD ADD[DocumentsPrice] money NULL;

UPDATE Fix_WhDocType2 SET [SortField] = 2 WHERE RowID=1

UPDATE Fix_WhDocType2 SET [SortField] = 4 WHERE RowID=2

UPDATE Fix_WhDocType2 SET [SortField] = 7 WHERE RowID=3

UPDATE Fix_WhDocType2 SET [SortField] = 8 WHERE RowID=4

UPDATE Fix_WhDocType2 SET [SortField] = 6 WHERE RowID=5

UPDATE Fix_WhDocType2 SET [SortField] = 1 WHERE RowID=6

UPDATE Fix_WhDocType2 SET [SortField] = 4 WHERE RowID=21

UPDATE Fix_WhDocType2 SET [SortField] = 3 WHERE RowID=22

UPDATE Fix_WhDocType2 SET [SortField] = 5 WHERE RowID=23

UPDATE Fix_WhDocType2 SET [SortField] = 7 WHERE RowID=24

UPDATE Fix_WhDocType2 SET [SortField] = 9 WHERE RowID=25

UPDATE Fix_WhDocType2 SET [SortField] = 5 WHERE RowID=26

ALTER TABLE dbo.WhDocD ADD [ServiceProvider] [int] NULL;

ALTER TABLE dbo.WhDocH ADD [OfficialBill] [bit] NULL;

ALTER TABLE dbo.AppUserSettings ADD [PaymentPage] [int] NULL;

ALTER TABLE dbo.AppUserSettings ADD [DefultPage] [int] NULL;

DROP VIEW [dbo].[WhDochView]

CREATE VIEW [dbo].[WhDochView] AS SELECT dbo.WhDocH.RowID AS ID, dbo.Fix_WhDocType2.RowName, dbo.Person.RowName AS PersonName, dbo.Person.RowCode AS PersonCode, dbo.Branch.RowID AS BranchID, dbo.Branch.RowName AS BranchName,dbo.WhDocH.DocNo, dbo.WhDocH.FDate, dbo.WhDocH.MDate, dbo.WhDocH.DiscountPercent, dbo.WhDocH.DiscountPrice, { fn IFNULL(dbo.WhDocH.SettlePrice, 0) } AS CashSettlement, dbo.WhDocH.TaxPercent, dbo.WhDocH.TaxPrice, dbo.WhDocH.TotalPrice, { fn IFNULL(dbo.WhDocH.TotalIncreasePrice, 0) } AS IncreasePrice, { fn IFNULL(dbo.WhDocH.TotalDecreasePrice, 0) } AS DecreasePrice, dbo.WhDocH.PayablePrice, dbo.WhDocH.ReferenceValue AS Reference, dbo.WhDocH.RowDesc AS Description, dbo.WhDocH.FDateUpdate AS UpdateDate, dbo.WhDocH.FTimeInsert AS InsertTime, dbo.WhDocH.FTimeUpdate AS UpdateTime, dbo.WhDocH.FDateSettlementDeadline AS SettlementDeadline, dbo.WhDocH.MDateSettlementDeadline, dbo.UserInfo.RowName AS InsertBy, UserInfo_1.RowName AS UpdateBy, dbo.WhDocH.InsertedBy AS InsertByID, { fn IFNULL(dbo.WhDocHSumPos.sumpos, 0) } AS CardReaderSettlement, { fn IFNULL(dbo.WhDocHBankSum.SumBank, 0) } AS BankAccountSettlement, dbo.Warehouse.RowName AS Warehouse, { fn IFNULL(dbo.WhDocH.IsSettled, 0) } AS IsSettled, { fn IFNULL(dbo.WhDocHSumMarketer.MarketerCommission, 0) } AS MarketerCommission, { fn IFNULL(dbo.WhDocHChequeRecSum.ChequeRec, 0)  } + { fn IFNULL(dbo.WhDocHChequePaySum.SumChequePay, 0) } AS ChequeSettlement, dbo.FinancialYear.RowName AS FinancialName, dbo.FinancialYear.RowID AS FinancialYear_ID, dbo.Fix_WhDocType2.RowID AS Fix_WhDocType2_ID, dbo.WhDocH.FDateInsert AS InsertDate, dbo.WhDocH.PayablePrice - ({ fn IFNULL(dbo.WhDocHSumPos.sumpos, 0) } + { fn IFNULL(dbo.WhDocHBankSum.SumBank, 0) } + { fn IFNULL(dbo.WhDocHChequeRecSum.ChequeRec, 0) } + { fn IFNULL(dbo.WhDocHChequePaySum.SumChequePay, 0) } + { fn IFNULL(dbo.WhDocH.SettlePrice, 0) }) AS NotSettledRemainder, dbo.WhDocH.RowUpdateVersion, dbo.WhDocH.OfficialBill FROM  dbo.WhDocH INNER JOIN dbo.Person ON dbo.WhDocH.Person_ID = dbo.Person.RowID INNER JOIN  dbo.Branch ON dbo.WhDocH.Branch_ID = dbo.Branch.RowID INNER JOIN dbo.Fix_WhDocType2 ON dbo.WhDocH.Fix_WhDocType2_ID = dbo.Fix_WhDocType2.RowID INNER JOIN dbo.UserInfo ON dbo.WhDocH.InsertedBy = dbo.UserInfo.RowID INNER JOIN dbo.Warehouse ON dbo.WhDocH.Warehouse_ID = dbo.Warehouse.RowID INNER JOIN dbo.FinancialYear ON dbo.WhDocH.FinancialYear_ID = dbo.FinancialYear.RowID LEFT OUTER JOIN dbo.UserInfo AS UserInfo_1 ON dbo.WhDocH.UpdatedBy = UserInfo_1.RowID LEFT OUTER JOIN dbo.WhDocHSumPos ON dbo.WhDocH.RowID = dbo.WhDocHSumPos.WhDocH_ID LEFT OUTER JOIN  dbo.WhDocHBankSum ON dbo.WhDocH.RowID = dbo.WhDocHBankSum.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHSumMarketer ON dbo.WhDocH.RowID = dbo.WhDocHSumMarketer.WhDocH_ID LEFT OUTER JOIN  dbo.WhDocHChequePaySum ON dbo.WhDocH.RowID = dbo.WhDocHChequePaySum.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHChequeRecSum ON dbo.WhDocH.RowID = dbo.WhDocHChequeRecSum.WhDocH_ID

ALTER TABLE [dbo].[WhDocD]  WITH CHECK ADD  CONSTRAINT [FK_WhDocD_Person] FOREIGN KEY([ServiceProvider]) REFERENCES [dbo].[Person] ([RowID])

ALTER TABLE [dbo].[WhDocD] CHECK CONSTRAINT [FK_WhDocD_Person]

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (3062, 3, N'گزارش ها- انجام دهندگان خدمت', NULL, NULL);

INSERT [dbo].[Fix_AppObject] ([RowID], [Fix_AppObjectType_ID], [RowName], [RowDesc], [UniqueIdentifierValue]) VALUES (3063, 3, N'گزارش ها -گزارش مالیات فاکتورها', NULL, NULL);

ALTER TABLE [dbo].[WhDocH] ADD[CustomerAddress][nvarchar](200) NULL;

ALTER TABLE [dbo].[WhDocH] ADD[DeliveryMan][int] NULL;

ALTER TABLE dbo.AppUserSettings ADD[PicBackGround] [image] NULL;

ALTER TABLE dbo.AppSettings ADD[MineDelay] [float] NULL;

ALTER TABLE dbo.AppSettings ADD[CofferDateTime] [int] NULL;

ALTER TABLE dbo.AppSettings ADD[RegisterType] [int] NULL;

Update dbo.AppSettings set RegisterType = 1;

DROP VIEW [dbo].[DeleteWhcodhView]

CREATE VIEW [dbo].[DeleteWhcodhView] AS SELECT dbo.Fix_WhDocType2.RowName, dbo.Person.RowName AS PersonName, dbo.Person.RowCode AS PersonCode, dbo.Branch.RowID AS BranchID, dbo.Branch.RowName AS BranchName, dbo.DeletedInvoicesH.DocNo,  dbo.DeletedInvoicesH.FDate, dbo.DeletedInvoicesH.MDate, dbo.DeletedInvoicesH.DiscountPercent, dbo.DeletedInvoicesH.DiscountPrice, dbo.DeletedInvoicesH.TaxPercent, dbo.DeletedInvoicesH.TaxPrice,  dbo.DeletedInvoicesH.TotalPrice, dbo.DeletedInvoicesH.PayablePrice, dbo.DeletedInvoicesH.ReferenceValue AS Reference, dbo.DeletedInvoicesH.RowDesc AS Description, dbo.DeletedInvoicesH.FDateUpdate AS UpdateDate,  dbo.DeletedInvoicesH.FTimeInsert AS InsertTime, dbo.DeletedInvoicesH.FTimeUpdate AS UpdateTime, dbo.DeletedInvoicesH.FDateSettlementDeadline AS SettlementDeadline, dbo.DeletedInvoicesH.MDateSettlementDeadline,  dbo.UserInfo.RowName AS InsertBy, UserInfo_2.RowName AS UpdateBy, dbo.DeletedInvoicesH.InsertedBy, dbo.FinancialYear.RowName AS FinancialName, dbo.FinancialYear.RowID AS FinancialYear_ID,  dbo.Fix_WhDocType2.RowID AS Fix_WhDocType2_ID, dbo.DeletedInvoicesH.FDateInsert AS InsertDate, dbo.DeletedInvoicesH.RowUpdateVersion, dbo.DeletedInvoicesH.RowID, dbo.DeletedInvoicesH.DeleteTime, dbo.DeletedInvoicesH.DeleteMDate, dbo.DeletedInvoicesH.DeleteFDate, UserInfo_1.RowName AS deleteby FROM dbo.Person INNER JOIN dbo.DeletedInvoicesH ON dbo.Person.RowID = dbo.DeletedInvoicesH.Person_ID INNER JOIN dbo.Branch ON dbo.DeletedInvoicesH.Branch_ID = dbo.Branch.RowID INNER JOIN dbo.Fix_WhDocType2 ON dbo.DeletedInvoicesH.Fix_WhDocType2_ID = dbo.Fix_WhDocType2.RowID INNER JOIN dbo.FinancialYear ON dbo.DeletedInvoicesH.FinancialYear_ID = dbo.FinancialYear.RowID INNER JOIN dbo.UserInfo ON dbo.DeletedInvoicesH.InsertedBy = dbo.UserInfo.RowID Left outer JOIN dbo.UserInfo AS UserInfo_2 ON dbo.DeletedInvoicesH.UpdatedBy = UserInfo_2.RowID LEFT OUTER JOIN dbo.UserInfo AS UserInfo_1 ON dbo.DeletedInvoicesH.DeletedBy = UserInfo_1.RowID;

ALTER TABLE [dbo].[DeletedInvoicesH]  WITH CHECK ADD  CONSTRAINT [FK_DeletedInvoicesH_Branch] FOREIGN KEY([Branch_ID])REFERENCES [dbo].[Branch] ([RowID]);

ALTER TABLE [dbo].[DeletedInvoicesH] CHECK CONSTRAINT [FK_DeletedInvoicesH_Branch];

ALTER TABLE [dbo].[DeletedInvoicesH]  WITH CHECK ADD  CONSTRAINT [FK_DeletedInvoicesH_FinancialYear] FOREIGN KEY([FinancialYear_ID])REFERENCES [dbo].[FinancialYear] ([RowID]);

ALTER TABLE [dbo].[DeletedInvoicesH] CHECK CONSTRAINT [FK_DeletedInvoicesH_FinancialYear];

ALTER TABLE [dbo].[DeletedInvoicesH]  WITH CHECK ADD  CONSTRAINT [FK_DeletedInvoicesH_Fix_WhDocType2] FOREIGN KEY([Fix_WhDocType2_ID])REFERENCES [dbo].[Fix_WhDocType2] ([RowID]);

ALTER TABLE [dbo].[DeletedInvoicesH] CHECK CONSTRAINT [FK_DeletedInvoicesH_Fix_WhDocType2];

ALTER TABLE [dbo].[DeletedInvoicesH]  WITH CHECK ADD  CONSTRAINT [FK_DeletedInvoicesH_Person] FOREIGN KEY([Person_ID])REFERENCES [dbo].[Person] ([RowID]);

ALTER TABLE [dbo].[DeletedInvoicesH] CHECK CONSTRAINT [FK_DeletedInvoicesH_Person];

ALTER TABLE [dbo].[DeletedInvoicesH]  WITH CHECK ADD  CONSTRAINT [FK_DeletedInvoicesH_UserInfo] FOREIGN KEY([UpdatedBy])REFERENCES [dbo].[UserInfo] ([RowID]);

ALTER TABLE [dbo].[DeletedInvoicesH] CHECK CONSTRAINT [FK_DeletedInvoicesH_UserInfo];

ALTER TABLE [dbo].[DeletedInvoicesH]  WITH CHECK ADD  CONSTRAINT [FK_DeletedInvoicesH_UserInfo1] FOREIGN KEY([DeletedBy])REFERENCES [dbo].[UserInfo] ([RowID]);

ALTER TABLE [dbo].[DeletedInvoicesH] CHECK CONSTRAINT [FK_DeletedInvoicesH_UserInfo1];

ALTER TABLE [dbo].[DeletedInvoicesD]  WITH CHECK ADD  CONSTRAINT [FK_DeletedInvoicesD_DeletedInvoicesH] FOREIGN KEY([DeletedInvoicesHID])REFERENCES [dbo].[DeletedInvoicesH] ([RowID]);

ALTER TABLE [dbo].[DeletedInvoicesD] CHECK CONSTRAINT [FK_DeletedInvoicesD_DeletedInvoicesH];

ALTER TABLE [dbo].[DeletedInvoicesD]  WITH CHECK ADD  CONSTRAINT [FK_DeletedInvoicesD_Good] FOREIGN KEY([Good_ID])REFERENCES [dbo].[Good] ([RowID]);

ALTER TABLE [dbo].[DeletedInvoicesD] CHECK CONSTRAINT [FK_DeletedInvoicesD_Good];

ALTER TABLE [dbo].[DeletedInvoicesD]  WITH CHECK ADD  CONSTRAINT [FK_DeletedInvoicesD_MeasureUnit] FOREIGN KEY([MeasureUnit_ID])REFERENCES [dbo].[MeasureUnit] ([RowID]);

ALTER TABLE [dbo].[DeletedInvoicesD] CHECK CONSTRAINT [FK_DeletedInvoicesD_MeasureUnit];

ALTER TABLE dbo.WhDoch ADD[Color] [nvarchar](50) NULL;

ALTER TABLE dbo.Table_RGfactor ADD[whdochid] [bigint] NULL;

DROP VIEW [dbo].[WhDochView];

CREATE VIEW [dbo].[WhDochView] AS SELECT dbo.WhDocH.RowID AS ID, dbo.Fix_WhDocType2.RowName, dbo.Person.RowName AS PersonName, dbo.Person.RowCode AS PersonCode, dbo.Branch.RowID AS BranchID, dbo.Branch.RowName AS BranchName,  dbo.WhDocH.DocNo, dbo.WhDocH.FDate, dbo.WhDocH.MDate, dbo.WhDocH.DiscountPercent, dbo.WhDocH.DiscountPrice, { fn IFNULL(dbo.WhDocH.SettlePrice, 0) } AS CashSettlement, dbo.WhDocH.TaxPercent,  dbo.WhDocH.TaxPrice, dbo.WhDocH.TotalPrice, { fn IFNULL(dbo.WhDocH.TotalIncreasePrice, 0) } AS IncreasePrice, { fn IFNULL(dbo.WhDocH.TotalDecreasePrice, 0) } AS DecreasePrice, dbo.WhDocH.PayablePrice,  dbo.WhDocH.ReferenceValue AS Reference, dbo.WhDocH.RowDesc AS Description, dbo.WhDocH.FDateUpdate AS UpdateDate, dbo.WhDocH.FTimeInsert AS InsertTime, dbo.WhDocH.FTimeUpdate AS UpdateTime,  dbo.WhDocH.FDateSettlementDeadline AS SettlementDeadline, dbo.WhDocH.MDateSettlementDeadline, dbo.UserInfo.RowName AS InsertBy, UserInfo_1.RowName AS UpdateBy, dbo.WhDocH.InsertedBy AS InsertByID,  { fn IFNULL(dbo.WhDocHSumPos.sumpos, 0) } AS CardReaderSettlement, { fn IFNULL(dbo.WhDocHBankSum.SumBank, 0) } AS BankAccountSettlement, dbo.Warehouse.RowName AS Warehouse,  { fn IFNULL(dbo.WhDocH.IsSettled, 0) } AS IsSettled, { fn IFNULL(dbo.WhDocHSumMarketer.MarketerCommission, 0) } AS MarketerCommission, { fn IFNULL(dbo.WhDocHChequeRecSum.ChequeRec, 0)  } + { fn IFNULL(dbo.WhDocHChequePaySum.SumChequePay, 0) } AS ChequeSettlement, dbo.FinancialYear.RowName AS FinancialName, dbo.FinancialYear.RowID AS FinancialYear_ID,  dbo.Fix_WhDocType2.RowID AS Fix_WhDocType2_ID, dbo.WhDocH.FDateInsert AS InsertDate, dbo.WhDocH.PayablePrice - ({ fn IFNULL(dbo.WhDocHSumPos.sumpos, 0) } + { fn IFNULL(dbo.WhDocHBankSum.SumBank, 0)  } + { fn IFNULL(dbo.WhDocHChequeRecSum.ChequeRec, 0) } + { fn IFNULL(dbo.WhDocHChequePaySum.SumChequePay, 0) } + { fn IFNULL(dbo.WhDocH.SettlePrice, 0) }) AS NotSettledRemainder,  dbo.WhDocH.RowUpdateVersion, dbo.WhDocH.OfficialBill, dbo.WhDocH.Color FROM  dbo.WhDocH INNER JOIN dbo.Person ON dbo.WhDocH.Person_ID = dbo.Person.RowID INNER JOIN dbo.Branch ON dbo.WhDocH.Branch_ID = dbo.Branch.RowID INNER JOIN dbo.Fix_WhDocType2 ON dbo.WhDocH.Fix_WhDocType2_ID = dbo.Fix_WhDocType2.RowID INNER JOIN dbo.UserInfo ON dbo.WhDocH.InsertedBy = dbo.UserInfo.RowID INNER JOIN dbo.Warehouse ON dbo.WhDocH.Warehouse_ID = dbo.Warehouse.RowID INNER JOIN dbo.FinancialYear ON dbo.WhDocH.FinancialYear_ID = dbo.FinancialYear.RowID LEFT OUTER JOIN dbo.UserInfo AS UserInfo_1 ON dbo.WhDocH.UpdatedBy = UserInfo_1.RowID LEFT OUTER JOIN dbo.WhDocHSumPos ON dbo.WhDocH.RowID = dbo.WhDocHSumPos.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHBankSum ON dbo.WhDocH.RowID = dbo.WhDocHBankSum.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHSumMarketer ON dbo.WhDocH.RowID = dbo.WhDocHSumMarketer.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHChequePaySum ON dbo.WhDocH.RowID = dbo.WhDocHChequePaySum.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHChequeRecSum ON dbo.WhDocH.RowID = dbo.WhDocHChequeRecSum.WhDocH_ID;

ALTER TABLE dbo.AppSettings ADD[BackUpPAth] [nvarchar](max) NULL;

CREATE TYPE [dbo].[goodlistnew] AS TABLE([did] [int] NULL,[goodid] [int] NULL,[goodname] [nvarchar](250) NULL,[barcode] [nvarchar](100) NULL,[MeasureUnit] [nvarchar](200) NULL,[MeasureUnitID] [int] NULL,[PurchasePrice] [decimal](18, 0) NULL,[saleprice] [decimal](18, 0) NULL,[sumPurchasePrice] [decimal](18, 0) NULL,[price] [decimal](18, 0) NULL,[DiscountPrice] [decimal](18, 0) NULL,[count] [float] NULL,[calculatedcount] [float] NULL,[sumafterDiscountPrice] [decimal](18, 0) NULL,[sumbeforeDiscountPrice] [decimal](18, 0) NULL)

CREATE TYPE [dbo].[paylist] AS TABLE([rowid] [int] NULL,[bankrowid] [int] NULL,[SettlePrice] [decimal](18, 0) NULL)

drop procedure [dbo].[sp_UseStringList]

Create PROCEDURE [dbo].[sp_UseStringList](@list goodlistnew READONLY,@uid int,@date nvarchar(50),@mdate datetime,@time nvarchar(10),@guid UNIQUEIDENTIFIER,@sum decimal,@cashpay decimal,@quantity decimal,@disc DECIMAL,@DiscountPrice DECIMAL,@pid INT,@bid int,@plist paylist readonly) AS BEGIN begin transaction t1 declare @outputtbl table (rowid int) declare @outputtbl2 table (dochid int) declare @accrid as int , @count as int , @i as int, @countofstock as float,@stcks as float,@branchid as int,@typetasvieh as bit,@bankid as int,@finanal as int,@warehouseid as int, @cofferid as int,@personid as int, @userid as int, @docid as int, @j as int,@goodid as int, @measurementunitid as int, @itemcount as FLOAT,@averages as int, @itemcalculatedcount as FLOAT, @saleprice as decimal, @itemprice as decimal, @itemquantity as decimal, @itemdiscount as decimal, @itempurchase as decimal, @k as int, @browid as int,@spricebank as decimal CREATE TABLE #tempstock ( [did] [int],[goodid] [int],[goodname] [nvarchar](250),[barcode] [nvarchar](100),[MeasureUnit] [nvarchar](200),[MeasureUnitID] [int],[PurchasePrice] [decimal],[saleprice] [decimal],[sumPurchasePrice] [decimal],[price] [decimal], [DiscountPrice] [decimal],[count] [float],[calculatedcount] [float],[sumafterDiscountPrice] [decimal],[sumbeforeDiscountPrice] [decimal],[id] [int] identity ) Begin Try set @accrid=3 set @branchid = @bid set @finanal = (select FinancialYear_ID from appsettings) set @warehouseid = (select Warehouse_ID from AppUserSettings where UserInfo_ID = @uid)  set @cofferid = (select  Coffer_ID from AppUserSettings where UserInfo_ID = @uid) IF(@pid=-1)BEGIN set @personid = (select  DefaultPerson_ID_FactorForosh  from AppUserSettings where UserInfo_ID = @uid) END ELSE BEGIN SET @personid=@pid END set @userid =  @uid insert into #tempstock select * from @list set @i = 1 set @count = (select COUNT(*) from #tempstock) insert into WhDocH (times,branch_id,FinancialYear_ID,Fix_RecordStatusType_ID,Fix_WhDocType1_ID,Fix_WhDocType2_ID,Warehouse_ID,Fix_DiscountType_ID,Coffer_ID,Person_ID,DocNo,FDate,MDate,DiscountPercent,DiscountPrice,SettlePrice,TaxPercent,TaxPrice,TotalPrice,QuantityInUnitPrice,TotalDiscountPriceItems,TotalTaxPriceItems,PayablePrice,DefinedBySystem,InsertedBy,FDateInsert,FTimeInsert,UniqueIdentifierValue,IsSend,IsSettled,SumPercentPrice,TotalIncreasePrice,TotalDecreasePrice) output inserted.RowID into @outputtbl(rowid) values (@time,@branchid,@finanal,1,2,21,@warehouseid,2,@cofferid,@personid,(select max(DocNo) from WhDocH where Fix_WhDocType2_ID = 21) + 1,@date,@mdate,0,@DiscountPrice,@cashpay,0,0,@sum,@quantity,@disc,0,@sum-@DiscountPrice,0,@userid,@date,@time,@guid,0,0,0,0,0) set @docid = (select DocNo from WhDocH where RowID = (select rowid from @outputtbl) and Fix_WhDocType2_ID = 21) set @j = 1 while (@j <= @count) begin set @goodid = (select goodid from #tempstock where id = @j) set @measurementunitid = (select MeasureUnitID from #tempstock where id = @j) set @itemcount = (select [count] from #tempstock where id = @j) set @itemcalculatedcount = (select [calculatedcount] from #tempstock where id = @j) set @saleprice = (select saleprice from #tempstock where id = @j) set @itemprice = (select price from #tempstock where id = @j) set @itemdiscount = (select DiscountPrice from #tempstock where id = @j) set @itempurchase = (select PurchasePrice from #tempstock where id = @j) set @averages = (select PurchaseAverage from AveragePrice where GoodID = @goodid) insert into WhDocD (Branch_Id,FinancialYear_ID,WhDocH_ID,Good_ID,Main_MeasureUnit_ID,MeasureUnit_ID,Fix_DiscountType_ID,InputValue,calculatedinputvalue,OutputValue,CalculatedOutputValue,UnitPrice,TotalPrice,QuantityInUnitPrice,DiscountPercent,DiscountPrice,purchaseunitprice,purchasetotalprice,taxpercent,TaxPrice,SaleUnitPrice,IsSend,PurchaseTotalPriceAverage) values (@branchid,@finanal,(select rowid from @outputtbl),@goodid,@measurementunitid,@measurementunitid,2,0,0,@itemcount,@itemcalculatedcount,@saleprice,(@itemprice * @itemcount),(@saleprice * @itemcount),0,(@itemdiscount * @itemcount),@itempurchase,(@itempurchase * @itemcount),0,0,@saleprice ,0,@averages) set @j = @j + 1 end set @typetasvieh = (select CardReaderAutoSettlement from AppUserSettings where UserInfo_ID = @uid) insert into DocH (branch_id,Fix_DocHType_ID,financialyear_id,RelateRowID,FDate,MDate,RowDesc,UniqueIdentifierValue)  output inserted.RowID into @outputtbl2(dochid) values (@branchid,2,@finanal,(select top(1) rowid from @outputtbl),@date,@mdate,N'فاکتور فروش شماره ' + CAST(@docid as nvarchar) ,@guid) insert into DocD(Branch_ID,doch_id,fix_acc1_id,fix_acc2type_id,Acc2RowID,BedPrice,BesPrice,RowDesc,IsSend) values (@branchid,(select top(1) dochid from @outputtbl2),3,1,@personid,@sum-@DiscountPrice,0,N'فاکتور فروش شماره ' + CAST(@docid as nvarchar),0) insert into DocD(Branch_ID,doch_id,fix_acc1_id,fix_acc2type_id,BedPrice,BesPrice,RowDesc,IsSend)  values (@branchid,(select top(1) dochid from @outputtbl2),2,0,0,@sum-@DiscountPrice,N'فاکتور فروش شماره ' + CAST(@docid as nvarchar),0) if(@cashpay>0) begin insert into DocD(Branch_ID,doch_id,fix_acc1_id,fix_acc2type_id,Acc2RowID,BedPrice,BesPrice,RowDesc,IsSend) values (@branchid,(select top(1) dochid from @outputtbl2),7,3,@cofferid,@cashpay,0,N'فاکتور فروش شماره ' + CAST(@docid as nvarchar)+N'-مبلغ نقدی',0) insert into DocD(Branch_ID,doch_id,fix_acc1_id,fix_acc2type_id,Acc2RowID,BedPrice,BesPrice,RowDesc,IsSend)  values (@branchid,(select top(1) dochid from @outputtbl2),3,1,@personid,0,@cashpay,N'فاکتور فروش شماره ' + CAST(@docid as nvarchar)+N'-مبلغ نقدی',0) end set @k=1 while(@k<=(select COUNT(*) from @plist)) begin set @browid=(select top(1) bankrowid from @plist where rowid=@k)set @spricebank=(select top(1)  SettlePrice from @plist where rowid=@k) if(@typetasvieh = 1) begin insert into DocD(Branch_ID,doch_id,fix_acc1_id,fix_acc2type_id,Acc2RowID,BedPrice,BesPrice,RowDesc,IsSend) values (@branchid,(select top(1) dochid from @outputtbl2),5,2,@browid,@spricebank,0,N'فاکتور فروش شماره ' + CAST(@docid as nvarchar)+N'-مبلغ کارتخوان',0) end else begin insert into DocD(Branch_ID,doch_id,fix_acc1_id,fix_acc2type_id,Acc2RowID,BedPrice,BesPrice,RowDesc,IsSend)  values (@branchid,(select top(1) dochid from @outputtbl2),5,8,@browid,@spricebank,0,N'فاکتور فروش شماره ' + CAST(@docid as nvarchar)+N'-مبلغ کارتخوان',0) end insert into DocD(Branch_ID,doch_id,fix_acc1_id,fix_acc2type_id,Acc2RowID,BedPrice,BesPrice,RowDesc,IsSend)  values (@branchid,(select top(1) dochid from @outputtbl2),3,1,@personid,0,@spricebank,N'فاکتور فروش شماره ' + CAST(@docid as nvarchar)+N'-مبلغ کارتخوان',0) set @bankid = (select BankAccountID from POS where RowID = @browid)insert into WhDocHBankAccount(BankAccount_ID,Branch_ID,SettlePrice,WhDocH_ID,IsSend,POS_ID) values (@bankid,@branchid,@spricebank,(select top(1) rowid from @outputtbl),0,@browid) set @k=@k+1 end DELETE FROM dbo.Table_Lottory WHERE nofactor=@docid INSERT INTO Table_Lottory (nofactor,nolottory,[sum],disc) VALUES(@docid,CAST( CAST(RAND() * 10000 AS INT) AS NVARCHAR(50)) + CAST(@docid AS NVARCHAR(50)),@sum,@DiscountPrice) select DocNo from WhDocH where RowID = (select rowid from @outputtbl)  commit transaction t1 End Try Begin Catch select Error_Message(); rollback transaction t1 return End Catch END

CREATE TABLE [dbo].[WhDocHEdited]([RowID] [int] IDENTITY(1,1) NOT NULL,[Branch_ID] [int] NULL,[FinancialYear_ID] [smallint] NOT NULL,[Fix_RecordStatusType_ID] [tinyint] NOT NULL,[Fix_WhDocType1_ID] [tinyint] NOT NULL,[Fix_WhDocType2_ID] [tinyint] NOT NULL,[Warehouse_ID] [smallint] NOT NULL,[Fix_DiscountType_ID] [tinyint] NULL,[Coffer_ID] [int] NULL,[Person_ID] [int] NULL,[ManufactureDocH_ID] [int] NULL,[ShiftDocH_ID] [int] NULL,[WarehouseComputationDocH_ID] [int] NULL,[DocNo] [bigint] NOT NULL,[FDate] [char](10) NOT NULL,[MDate] [datetime] NULL,[DiscountPercent] [float] NULL,[DiscountPrice] [money] NULL,[SettlePrice] [money] NULL,[TaxPercent] [float] NULL,[TaxPrice] [money] NULL,	[TotalPrice] [money] NULL,[QuantityInUnitPrice] [money] NULL,[TotalDiscountPriceItems] [money] NULL,[TotalTaxPriceItems] [money] NULL,[TotalIncreasePrice] [money] NULL,[TotalDecreasePrice] [money] NULL,[PayablePrice] [money] NULL,[DefinedBySystem] [bit] NOT NULL,[ReferenceValue] [nvarchar](100) NULL,[RowDesc] [nvarchar](500) NULL,[InsertedBy] [smallint] NULL,[UpdatedBy] [smallint] NULL,[FDateInsert] [char](10) NULL,[FDateUpdate] [char](10) NULL,[FTimeInsert] [char](5) NULL,[FTimeUpdate] [char](5) NULL,[InsertServerDateTime] [datetime] NULL,[UpdateServerDateTime] [datetime] NULL,[RowUpdateVersion] [smallint] NULL,[UniqueIdentifierValue] [uniqueidentifier] NULL,[IsSend] [tinyint] NULL,[Good_ID] [int] NULL,[SumPercentPrice] [money] NULL,[FDateSettlementDeadline] [char](10) NULL,[MDateSettlementDeadline] [datetime] NULL,[IsSettled] [bit] NULL,[OrderH_ID] [bigint] NULL,[Times] [time](0) NULL,[Table_ID] [int] NULL,[DeliveryType_ID] [tinyint] NULL,[ClientRowID] [int] NULL,[DailyDocNo] [bigint] NULL,[OfficialBill] [bit] NULL,[CustomerAddress] [nvarchar](200) NULL,[DeliveryMan] [int] NULL,[Color] [nvarchar](50) NULL, CONSTRAINT [PK_WhDocHEdited] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] ALTER TABLE [dbo].[WhDocHEdited] ADD  CONSTRAINT [DF_WhDocHEdited_Fix_RecordStatusType_ID]  DEFAULT ((1)) FOR [Fix_RecordStatusType_ID] ALTER TABLE [dbo].[WhDocHEdited] ADD  CONSTRAINT [DF_WhDocHEdited_DefinedBySystem]  DEFAULT ((0)) FOR [DefinedBySystem] ALTER TABLE [dbo].[WhDocHEdited] ADD  CONSTRAINT [DF_WhDocHEdited_InsertServerDateTime]  DEFAULT (getdate()) FOR [InsertServerDateTime] ALTER TABLE [dbo].[WhDocHEdited] ADD  CONSTRAINT [DF_WhDocHEdited_RowUpdateVersion]  DEFAULT ((1)) FOR [RowUpdateVersion] ALTER TABLE [dbo].[WhDocHEdited] ADD  CONSTRAINT [DF_WhDocHEdited_IsSend]  DEFAULT ((0)) FOR [IsSend] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_Branch] FOREIGN KEY([Branch_ID]) REFERENCES [dbo].[Branch] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_Branch] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_Coffer] FOREIGN KEY([Coffer_ID]) REFERENCES [dbo].[Coffer] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_Coffer] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_FinancialYear] FOREIGN KEY([FinancialYear_ID]) REFERENCES [dbo].[FinancialYear] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_FinancialYear] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_Fix_DeliveryType] FOREIGN KEY([DeliveryType_ID]) REFERENCES [dbo].[Fix_DeliveryType] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_Fix_DeliveryType] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_Fix_DiscountType] FOREIGN KEY([Fix_DiscountType_ID]) REFERENCES [dbo].[Fix_DiscountType] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_Fix_DiscountType] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_Fix_RecordStatusType] FOREIGN KEY([Fix_RecordStatusType_ID]) REFERENCES [dbo].[Fix_RecordStatusType] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_Fix_RecordStatusType] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_Fix_WhDocType1] FOREIGN KEY([Fix_WhDocType1_ID]) REFERENCES [dbo].[Fix_WhDocType1] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_Fix_WhDocType1] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_Fix_WhDocType2] FOREIGN KEY([Fix_WhDocType2_ID]) REFERENCES [dbo].[Fix_WhDocType2] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_Fix_WhDocType2] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_Good] FOREIGN KEY([Good_ID]) REFERENCES [dbo].[Good] ([RowID]) ON DELETE CASCADE ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_Good] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_ManufactureDocH] FOREIGN KEY([ManufactureDocH_ID]) REFERENCES [dbo].[ManufactureDocH] ([RowID]) ON DELETE CASCADE ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_ManufactureDocH] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_OrderH] FOREIGN KEY([OrderH_ID]) REFERENCES [dbo].[OrderH] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_OrderH] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_Person] FOREIGN KEY([Person_ID]) REFERENCES [dbo].[Person] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_Person] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_RestaurantTables] FOREIGN KEY([Table_ID]) REFERENCES [dbo].[RestaurantTables] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_RestaurantTables] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_ShiftDocH] FOREIGN KEY([ShiftDocH_ID]) REFERENCES [dbo].[ShiftDocH] ([RowID]) ON DELETE CASCADE ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_ShiftDocH] ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_Warehouse] FOREIGN KEY([Warehouse_ID]) REFERENCES [dbo].[Warehouse] ([RowID]) ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_Warehouse]ALTER TABLE [dbo].[WhDocHEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocHEdited_WarehouseComputationDocH] FOREIGN KEY([WarehouseComputationDocH_ID]) REFERENCES [dbo].[WarehouseComputationDocH] ([RowID]) ON DELETE CASCADE ALTER TABLE [dbo].[WhDocHEdited] NOCHECK CONSTRAINT [FK_WhDocHEdited_WarehouseComputationDocH]

CREATE TABLE [dbo].[WhDocDEdited]([RowID] [int] IDENTITY(1,1) NOT NULL,[Branch_ID] [int] NULL,[FinancialYear_ID] [smallint] NOT NULL,[WhDocH_ID] [int] NOT NULL,[Good_ID] [int] NOT NULL,[MeasureUnit_ID] [int] NULL,[Main_MeasureUnit_ID] [int] NULL,[Fix_DiscountType_ID] [tinyint] NULL,[InputValue] [float] NOT NULL,[CalculatedInputValue] [float] NOT NULL,[OutputValue] [float] NOT NULL,[CalculatedOutputValue] [float] NOT NULL,[UnitPrice] [money] NOT NULL,[TotalPrice] [money] NOT NULL,[QuantityInUnitPrice] [money] NULL,[DiscountPercent] [float] NULL,[DiscountPrice] [money] NULL,[PurchaseUnitPrice] [money] NULL,[PurchaseTotalPrice] [money] NULL,[TaxPercent] [float] NULL,[TaxPrice] [money] NULL,[SaleUnitPrice] [money] NULL,[RowDesc] [nvarchar](500) NULL,[UniqueIdentifierValue] [uniqueidentifier] NULL,[IsSend] [tinyint] NULL,[PurchaseTotalPriceAverage] [money] NULL,[GoodSize] [nvarchar](50) NULL,[SortInDocH] [bigint] NULL,[SerialNumber] [nvarchar](50) NULL,[CostOfGood] [money] NULL,[ClientRowID] [int] NULL,[Discount1Percent] [float] NULL,[Discount1Price] [money] NULL,[Discount2Percent] [float] NULL,[Discount2Price] [money] NULL,[ServiceProvider] [int] NULL,CONSTRAINT [PK_WhDocDEdited] PRIMARY KEY CLUSTERED ([RowID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] ALTER TABLE [dbo].[WhDocDEdited] ADD  CONSTRAINT [DF_WhDocDEdited_IsSend]  DEFAULT ((0)) FOR [IsSend] ALTER TABLE [dbo].[WhDocDEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocDEdited_Branch] FOREIGN KEY([Branch_ID]) REFERENCES [dbo].[Branch] ([RowID]) ALTER TABLE [dbo].[WhDocDEdited] NOCHECK CONSTRAINT [FK_WhDocDEdited_Branch] ALTER TABLE [dbo].[WhDocDEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocDEdited_FinancialYear] FOREIGN KEY([FinancialYear_ID]) REFERENCES [dbo].[FinancialYear] ([RowID]) ALTER TABLE [dbo].[WhDocDEdited] NOCHECK CONSTRAINT [FK_WhDocDEdited_FinancialYear] ALTER TABLE [dbo].[WhDocDEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocDEdited_Fix_DiscountType] FOREIGN KEY([Fix_DiscountType_ID]) REFERENCES [dbo].[Fix_DiscountType] ([RowID]) ALTER TABLE [dbo].[WhDocDEdited] NOCHECK CONSTRAINT [FK_WhDocDEdited_Fix_DiscountType] ALTER TABLE [dbo].[WhDocDEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocDEdited_Good] FOREIGN KEY([Good_ID]) REFERENCES [dbo].[Good] ([RowID]) ALTER TABLE [dbo].[WhDocDEdited] NOCHECK CONSTRAINT [FK_WhDocDEdited_Good] ALTER TABLE [dbo].[WhDocDEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocDEdited_MeasureUnit_1] FOREIGN KEY([MeasureUnit_ID]) REFERENCES [dbo].[MeasureUnit] ([RowID]) ALTER TABLE [dbo].[WhDocDEdited] NOCHECK CONSTRAINT [FK_WhDocDEdited_MeasureUnit_1] ALTER TABLE [dbo].[WhDocDEdited]  WITH NOCHECK ADD  CONSTRAINT [FK_WhDocDEdited_MeasureUnit_2] FOREIGN KEY([Main_MeasureUnit_ID]) REFERENCES [dbo].[MeasureUnit] ([RowID]) ALTER TABLE [dbo].[WhDocDEdited] NOCHECK CONSTRAINT [FK_WhDocDEdited_MeasureUnit_2] ALTER TABLE [dbo].[WhDocDEdited]  WITH CHECK ADD  CONSTRAINT [FK_WhDocDEdited_Person] FOREIGN KEY([ServiceProvider]) REFERENCES [dbo].[Person] ([RowID]) ALTER TABLE [dbo].[WhDocDEdited] CHECK CONSTRAINT [FK_WhDocDEdited_Person] ALTER TABLE [dbo].[WhDocDEdited]  WITH CHECK ADD  CONSTRAINT [FK_WhDocDEdited_WhDocHEdited] FOREIGN KEY([WhDocH_ID]) REFERENCES [dbo].[WhDocHEdited] ([RowID]) ALTER TABLE [dbo].[WhDocDEdited] CHECK CONSTRAINT [FK_WhDocDEdited_WhDocHEdited]

ALTER TABLE dbo.Good ADD[TransforRate] [money] NULL;

ALTER TABLE dbo.DocD ADD[Note] [ntext] NULL;

ALTER TABLE dbo.AppSystemSettings ADD[Design1] [int] NULL;

ALTER TABLE dbo.AppSystemSettings ADD[Design2] [int] NULL;

DROP VIEW [dbo].[GoodView];

CREATE VIEW [dbo].[GoodView] AS SELECT  dbo.Good.RowID AS ID, dbo.Good.RowName AS ProductName, dbo.Good.RowCode AS ProductBarcode, dbo.Good.PurchasePrice, dbo.Good.SalePrice, dbo.Good.InterestRate, dbo.Good.ConsumerPrice, dbo.Good.OrderPoint,{ fn IFNULL(dbo.Good.RowNameEN, '') } AS LatinProductName, { fn IFNULL(dbo.Good.RowNameAlias, '') } AS ProductNickName, dbo.Good.FirstStock, dbo.Good.DiscountPrice, dbo.Good.FDate AS DateInsert, dbo.Good.RowDesc AS Description, dbo.Good.IsHasSize, dbo.Good.IsActive, dbo.Good.MDate, dbo.Good.Fix_GoodType_ID, dbo.Good.RowCode, dbo.Good.MaxCount, dbo.Good.GoodCategory_ID AS GoodCategoryID, dbo.MeasureUnit.RowName AS SmallestUnit, MeasureUnit_1.RowName AS DefaultUnit, { fn IFNULL(dbo.DiscountGroupGood.RowName, '') } AS DiscountGroupGood, { fn IFNULL(dbo.TaxGroupGood.RowName, '') } AS TaxGroupGood, { fn IFNULL(dbo.GoodCategory.RowName, '') } AS CategoryName, dbo.Good.Reversion, { fn IFNULL(dbo.Good.MultipleSalePrice, 0) } AS MultipleSalePrice, dbo.Good.TransforRate FROM  dbo.Good LEFT OUTER JOIN dbo.MeasureUnit ON dbo.Good.Main_MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.MeasureUnit AS MeasureUnit_1 ON dbo.Good.Default_MeasureUnit_ID = MeasureUnit_1.RowID LEFT OUTER JOIN dbo.DiscountGroupGood ON dbo.Good.DiscountGroupGood_ID = dbo.DiscountGroupGood.RowID LEFT OUTER JOIN dbo.TaxGroupGood ON dbo.Good.TaxGroupGood_ID = dbo.TaxGroupGood.RowID LEFT OUTER JOIN dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID

DROP VIEW [dbo].[PersonRemainderView];

CREATE VIEW [dbo].[PersonRemainderView] AS SELECT  SUM(dbo.DocD.BedPrice) AS BedPrice, SUM(dbo.DocD.BesPrice) AS BesPrice, dbo.Person.RowID AS PersonID, SUM(dbo.DocD.BedPrice) - SUM(dbo.DocD.BesPrice) AS Remainder, COALESCE (CASE WHEN SUM(dbo.DocD.BedPrice) - SUM(dbo.DocD.BesPrice) > 0 THEN 1 ELSE 0 END, 1) AS int, dbo.DocH.FinancialYear_ID FROM  dbo.DocD INNER JOIN dbo.Person ON dbo.DocD.Acc2RowID = dbo.Person.RowID INNER JOIN  dbo.DocH ON dbo.DocD.DocH_ID = dbo.DocH.RowID WHERE (dbo.DocD.Fix_Acc1_ID = 3) AND (dbo.DocD.Fix_Acc2Type_ID = 1) AND (dbo.Person.Fix_PersonType_ID <> 5) GROUP BY dbo.Person.RowID, dbo.DocH.FinancialYear_ID HAVING        (SUM(dbo.DocD.BedPrice) - SUM(dbo.DocD.BesPrice) <> 0)

INSERT [dbo].[Fix_WhDocType2]([RowID],[Fix_WhDocType1_ID],[SortField],[RowName]) VALUES(7,1,1,N'سند افتتاحیه')

INSERT [dbo].[Fix_Acc1]([RowID],[DefinedBySystem],[RowName]) VALUES(18,1,N'موجودی کالا')

INSERT [dbo].[Fix_Acc2Type]([RowID],[DefinedBySystem],[Fix_Acc1_ID],[RowName]) VALUES(15,1,18,N'موجودی کالا')

DROP VIEW [dbo].[PersonAccountView];

CREATE VIEW [dbo].[PersonAccountView] AS SELECT  dbo.Person.RowName AS Name, dbo.Person.RowCode AS Code, dbo.Person.MobileNumber AS Mobile, dbo.Person.PhoneNumber AS Phone, dbo.Person.NationalCode, dbo.Person.AccountNumber1,    dbo.Person.AccountNumber2, dbo.Person.AccountNumber3,dbo.Person.Adress AS Address, dbo.Person.MobileForSMS, dbo.Person.FatherName, dbo.Person.EconomicCode, ABS(dbo.PersonRemainderView.Remainder)  AS Remainder, dbo.PersonRemainderView.int, dbo.Person.RowID AS PersonID, { fn IFNULL(dbo.Person.FDateLastSMS_ForDebt, '') } AS LastSmsDate, dbo.Person.MDateLastSMS_ForDebt AS LastSmsDateTime, dbo.Person.Fix_PersonType_ID, dbo.Fix_PersonType.RowName AS PersonType, dbo.PersonCategory.RowName AS PersonCategory, dbo.PersonRemainderView.FinancialYear_ID FROM  dbo.PersonRemainderView INNER JOIN  dbo.Person ON dbo.PersonRemainderView.PersonID = dbo.Person.RowID LEFT OUTER JOIN dbo.PersonCategory ON dbo.Person.PersonCategory_ID = dbo.PersonCategory.RowID LEFT OUTER JOIN dbo.Fix_PersonType ON dbo.Person.Fix_PersonType_ID = dbo.Fix_PersonType.RowID

DROP VIEW [dbo].[WhdocDWarehouse];

CREATE VIEW [dbo].[WhdocDWarehouse] AS SELECT SUM(CalculatedInputValue) - SUM(CalculatedOutputValue) AS Stock, Good_ID, FinancialYear_ID FROM dbo.WhDocD GROUP BY FinancialYear_ID, Good_ID

DROP VIEW [dbo].[WarehouseGoodView];

DROP VIEW [dbo].[AllWarehouseQuantityView];

DROP VIEW [dbo].[GoodStockReport];

DROP VIEW [dbo].[StockAllWarehouse];

DROP VIEW [dbo].[GoodStockReportAllWarehouse];

CREATE VIEW [dbo].[WarehouseGoodView] AS SELECT   dbo.Good.RowID, dbo.Good.RowName, dbo.Good.RowCode, dbo.Warehouse.RowID AS WarehouseID, dbo.Warehouse.RowName AS WarehouseName, dbo.Good.SalePrice, dbo.Good.Main_MeasureUnit_ID, dbo.MeasureUnit.RowName AS Unit, dbo.FinancialYear.RowID AS financialid FROM   dbo.Warehouse CROSS JOIN  dbo.Good INNER JOIN dbo.MeasureUnit ON dbo.Good.Main_MeasureUnit_ID = dbo.MeasureUnit.RowID CROSS JOIN  dbo.FinancialYear

CREATE VIEW [dbo].[AllWarehouseQuantityView] AS SELECT  TOP (100) PERCENT dbo.WarehouseGoodView.RowID AS ID, dbo.WarehouseGoodView.RowName AS ProductName, dbo.WarehouseGoodView.RowCode AS ProductBarcode,   dbo.WarehouseGoodView.WarehouseID AS Warehouse_ID, dbo.WarehouseGoodView.WarehouseName, { fn IFNULL(dbo.StockView.stock, 0) } AS Quantity, dbo.StockView.FinancialYear_ID, dbo.WarehouseGoodView.SalePrice,   dbo.WarehouseGoodView.Unit AS SmallestUnit, dbo.WarehouseGoodView.financialid FROM  dbo.WarehouseGoodView LEFT OUTER JOIN dbo.StockView ON dbo.WarehouseGoodView.RowID = dbo.StockView.Good_ID AND dbo.WarehouseGoodView.WarehouseID = dbo.StockView.Warehouse_ID AND  dbo.WarehouseGoodView.financialid = dbo.StockView.FinancialYear_ID ORDER BY productname

CREATE VIEW [dbo].[GoodStockReport] AS SELECT  dbo.Good.RowCode AS ProductBarcode, dbo.Good.RowName AS ProductName, { fn IFNULL(dbo.Good.RowNameEN, '') } AS LatinProductName, { fn IFNULL(dbo.Good.RowNameAlias, '') } AS ProductNickName,  dbo.AllWarehouseQuantityView.Quantity, dbo.Good.RowID AS ID, dbo.MeasureUnit.RowName AS SmallestUnit, dbo.AllWarehouseQuantityView.Warehouse_ID, dbo.AllWarehouseQuantityView.FinancialYear_ID,  { fn IFNULL(dbo.GoodCategory.RowName, '') } AS CategoryName, dbo.Good.SalePrice, dbo.Good.PurchasePrice, dbo.AllWarehouseQuantityView.Quantity * dbo.Good.SalePrice AS ValueBasedOnSale,   dbo.AllWarehouseQuantityView.Quantity * dbo.Good.PurchasePrice AS ValueBasedOnPurchase, dbo.AveragePrice.PurchaseAverage,  dbo.AllWarehouseQuantityView.Quantity * dbo.AveragePrice.PurchaseAverage AS ValueBasedOnAverage, dbo.Warehouse.RowName AS Warehouse, dbo.Good.OrderPoint, dbo.Good.Main_MeasureUnit_ID,   dbo.AllWarehouseQuantityView.financialid FROM  dbo.Good INNER JOIN  dbo.AllWarehouseQuantityView ON dbo.Good.RowID = dbo.AllWarehouseQuantityView.ID INNER JOIN  dbo.Warehouse ON dbo.AllWarehouseQuantityView.Warehouse_ID = dbo.Warehouse.RowID INNER JOIN  dbo.MeasureUnit ON dbo.Good.Main_MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.AveragePrice ON dbo.Good.RowID = dbo.AveragePrice.GoodID LEFT OUTER JOIN dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID

CREATE VIEW [dbo].[StockAllWarehouse] AS SELECT   TOP (100) PERCENT dbo.WarehouseGoodView.financialid, dbo.WarehouseGoodView.RowID, { fn IFNULL(dbo.WhdocDWarehouse.Stock, 0) } AS stock, dbo.WhdocDWarehouse.Good_ID,dbo.WhdocDWarehouse.FinancialYear_ID FROM dbo.WarehouseGoodView LEFT OUTER JOIN dbo.WhdocDWarehouse ON dbo.WhdocDWarehouse.Good_ID = dbo.WarehouseGoodView.RowID AND dbo.WarehouseGoodView.financialid = dbo.WhdocDWarehouse.FinancialYear_ID GROUP BY dbo.WarehouseGoodView.financialid, dbo.WarehouseGoodView.RowID, dbo.WhdocDWarehouse.Stock, dbo.WhdocDWarehouse.Good_ID, dbo.WhdocDWarehouse.FinancialYear_ID

CREATE VIEW [dbo].[GoodStockReportAllWarehouse] AS SELECT  dbo.Good.RowCode AS ProductBarcode, dbo.Good.RowName AS ProductName, { fn IFNULL(dbo.Good.RowNameEN, '') } AS LatinProductName, { fn IFNULL(dbo.Good.RowNameAlias, '') } AS ProductNickName,   dbo.Good.RowID AS ID, dbo.MeasureUnit.RowName AS SmallestUnit, { fn IFNULL(dbo.GoodCategory.RowName, '') } AS CategoryName, dbo.Good.SalePrice, dbo.Good.PurchasePrice,  dbo.StockAllWarehouse.stock * dbo.Good.SalePrice AS ValueBasedOnSale, dbo.StockAllWarehouse.stock * dbo.Good.PurchasePrice AS ValueBasedOnPurchase, { fn IFNULL(dbo.StockAllWarehouse.stock, 0) } AS Quantity,   dbo.AveragePrice.PurchaseAverage, dbo.StockAllWarehouse.stock * dbo.AveragePrice.PurchaseAverage AS ValueBasedOnAverage, dbo.Good.OrderPoint, dbo.StockAllWarehouse.financialid,   dbo.StockAllWarehouse.financialid AS Expr1, dbo.StockAllWarehouse.RowID FROM    dbo.Good INNER JOIN  dbo.MeasureUnit ON dbo.Good.Main_MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN  dbo.StockAllWarehouse ON dbo.Good.RowID = dbo.StockAllWarehouse.RowID LEFT OUTER JOIN  dbo.AveragePrice ON dbo.Good.RowID = dbo.AveragePrice.GoodID LEFT OUTER JOIN  dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID

DROP VIEW [dbo].[WhDocDItems];

CREATE VIEW [dbo].[WhDocDItems] AS SELECT dbo.WhDocD.RowID AS ID, dbo.WhDocH.Fix_WhDocType2_ID, dbo.WhDocH.MDate, dbo.WhDocH.FDate, dbo.WhDocH.RowID AS WhDocHID, dbo.Warehouse.RowName AS Warehouse, dbo.Good.RowName AS GoodName, dbo.Good.RowCode AS GoodCode, { fn IFNULL(dbo.WhDocD.GoodSize, '') } AS GoodSize, COALESCE (CASE WHEN (dbo.WhDocD.InputValue) = 0 THEN (WhDocD.OutputValue) ELSE (WhDocD.InputValue) END, 0) AS Quantity, dbo.WhDocD.UnitPrice, { fn IFNULL(dbo.WhDocD.DiscountPercent, 0) } AS DiscountPercent, { fn IFNULL(dbo.WhDocD.DiscountPrice, 0) } AS DiscountPrice, { fn IFNULL(dbo.WhDocD.TaxPercent, 0) } AS TaxPercent, { fn IFNULL(dbo.WhDocD.TaxPrice, 0) } AS TaxPrice, dbo.WhDocD.TotalPrice, { fn IFNULL(dbo.GoodCategory.RowCode, '') } AS GoodCategoryCode, { fn IFNULL(dbo.GoodCategory.RowName, '') } AS GoodCategoryName, { fn IFNULL(dbo.GoodCategory.FullRowCode, '') } AS GoodCategoryFullCode, { fn IFNULL(dbo.GoodCategory.FullRowName, '') } AS GoodCategoryFullName, { fn IFNULL(dbo.Person.RowCode, '') } AS PersonCode, { fn IFNULL(dbo.Person.RowName, '') } AS PersonName, { fn IFNULL(dbo.PersonCategory.RowName, '') } AS PersonCategory, { fn IFNULL(dbo.Province.RowName, '') } AS Province, { fn IFNULL(dbo.County.RowName, '') } AS County, { fn IFNULL(dbo.City.RowName, '') } AS City, dbo.WhDocD.SerialNumber AS Serial, dbo.WhDocD.RowDesc AS Description, dbo.WhDocH.DocNo, dbo.MeasureUnit.RowName AS MeasureUnit, dbo.WhDocH.Branch_ID AS BranchID, dbo.Good.RowID AS GoodID, dbo.WhDocD.PurchaseTotalPrice,dbo.Warehouse.RowID FROM dbo.WhDocD LEFT OUTER JOIN dbo.Good ON dbo.WhDocD.Good_ID = dbo.Good.RowID LEFT OUTER JOIN dbo.GoodCategory ON dbo.Good.GoodCategory_ID = dbo.GoodCategory.RowID INNER JOIN  dbo.WhDocH ON dbo.WhDocD.WhDocH_ID = dbo.WhDocH.RowID INNER JOIN dbo.MeasureUnit ON dbo.WhDocD.MeasureUnit_ID = dbo.MeasureUnit.RowID LEFT OUTER JOIN dbo.Person ON dbo.WhDocH.Person_ID = dbo.Person.RowID LEFT OUTER JOIN dbo.PersonCategory ON dbo.Person.PersonCategory_ID = dbo.PersonCategory.RowID LEFT OUTER JOIN dbo.Province ON dbo.Person.Province_ID = dbo.Province.RowID LEFT OUTER JOIN dbo.County ON dbo.Person.County_ID = dbo.County.RowID LEFT OUTER JOIN dbo.City ON dbo.Person.City_ID = dbo.City.RowID LEFT OUTER JOIN dbo.Warehouse ON dbo.WhDocH.Warehouse_ID = dbo.Warehouse.RowID

INSERT [dbo].[Fix_EhDocType]([RowID],[RowName],[RowDesc]) VALUES(4,N'موجودی افتتاحیه','')

ALTER TABLE dbo.AppSettings ADD[Version] [int] NULL;

ALTER TABLE dbo.AppSettings ADD[Updatefile] varbinary(max) NULL;

ALTER TABLE dbo.AppSettings ADD[Insertmethod] int NULL;

ALTER TABLE dbo.AppSettings ADD[printmethod] int NULL;

ALTER TABLE dbo.AppSystemSettings ADD[Printtype] [int] NULL;

DROP VIEW [dbo].[WhdocDview];

CREATE VIEW [dbo].[WhdocDview] AS SELECT dbo.Good.RowCode AS Barcode, dbo.Good.BriefDescription, dbo.Good.ConsumerPrice, dbo.WhDocD.DiscountPrice, dbo.WhDocH.FTimeInsert, dbo.WhDocH.FDateUpdate, dbo.WhDocH.FDateInsert, dbo.Good.FirstStock,dbo.WhDocH.FTimeUpdate, dbo.Good.FullDescription, dbo.Good.RowID AS GoodID, dbo.GoodCategory.RowName AS GoodCategory, dbo.Good.GoodNameIndexInPrintInvoice, dbo.WhDocH.UpdatedBy, dbo.WhDocH.InsertedBy, dbo.WhDocH.InsertServerDateTime, dbo.Good.InterestRate, dbo.Good.IsHasSize, dbo.Good.IsActive, dbo.Good.IsSpecialSale, dbo.Good.Main_MeasureUnit_ID, dbo.Good.RowName AS Name, dbo.WhDocH.MDate, dbo.Good.RowNameEN AS NameEN, dbo.Good.OrderPoint, dbo.WhDocD.SaleUnitPrice, dbo.WhDocD.UnitPrice, dbo.WhDocD.TotalPrice, dbo.WhDocD.InputValue, dbo.WhDocD.OutputValue, dbo.WhDocH.Fix_WhDocType2_ID, dbo.WhDocH.Fix_WhDocType1_ID, dbo.WhDocD.GoodSize, dbo.WhDocD.PurchaseTotalPriceAverage, dbo.WhDocD.TaxPrice, dbo.WhDocD.TaxPercent, dbo.WhDocD.PurchaseTotalPrice, dbo.WhDocD.PurchaseUnitPrice, dbo.WhDocD.QuantityInUnitPrice, dbo.WhDocD.DiscountPercent, dbo.WhDocD.RowDesc, dbo.Person.RowCode AS PersonCode, dbo.Person.RowName AS PersonName, dbo.Person.RowID, dbo.WhDocH.FDate,  dbo.WhDocH.DocNo, MeasureUnit_1.RowName AS MainMeasureUnit, dbo.MeasureUnit.RowName AS MeasureUnit,(CASE WHEN dbo.WhDocD.CalculatedInputValue = 0 THEN dbo.WhDocD.CalculatedOutputValue ELSE dbo.WhDocD.CalculatedInputValue END) AS quantity FROM dbo.GoodCategory RIGHT OUTER JOIN dbo.MeasureUnit AS MeasureUnit_1 INNER JOIN dbo.WhDocD INNER JOIN dbo.Good ON dbo.WhDocD.Good_ID = dbo.Good.RowID INNER JOIN dbo.MeasureUnit ON dbo.Good.Default_MeasureUnit_ID = dbo.MeasureUnit.RowID AND dbo.Good.Main_MeasureUnit_ID = dbo.MeasureUnit.RowID ON MeasureUnit_1.RowID = dbo.Good.Default_MeasureUnit_ID INNER JOIN dbo.Person INNER JOIN dbo.WhDocH ON dbo.Person.RowID = dbo.WhDocH.Person_ID ON dbo.WhDocD.WhDocH_ID = dbo.WhDocH.RowID ON dbo.GoodCategory.RowID = dbo.Good.GoodCategory_ID

DROP VIEW [dbo].[SalesFactorProfit];

CREATE VIEW [dbo].[SalesFactorProfit] AS SELECT  H.RowID AS ID, H.DocNo, H.FDate, H.MDate, P.RowName AS PersonName, P.RowCode AS PersonCode, { fn IFNULL(SUM(Increase.IncreasePrice), 0) } AS IncreasePrice, { fn IFNULL(SUM(Decrease.DecreasePrice), 0) } AS DecreasePrice, SUM(D.QuantityInUnitPrice) AS SumGoodsPrice, SUM(D.DiscountPrice) AS SumDiscountItems, H.DiscountPrice, { fn IFNULL(SUM(Marketer.PercentPrice), 0) } AS MarketerCommission, SUM(D.PurchaseTotalPrice) AS SumPurchaseTotalPrice, { fn IFNULL(SUM(D.CostOfGood), 0) } AS SumCostOfGood, { fn IFNULL(SUM(D.PurchaseTotalPriceAverage), 0) } AS SumTotalPriceAverage, SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + { fn IFNULL(SUM(D.PurchaseTotalPriceAverage), 0) }) AS FactorProfitOnAverage, SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + SUM(D.PurchaseTotalPrice)) AS FactorProfitOnPurchasePrice,  SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + { fn IFNULL(SUM(D.CostOfGood), 0) }) AS FactorProfitOnCostOfGood, SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + SUM(D.DiscountPrice) + { fn IFNULL(SUM(D.PurchaseTotalPriceAverage), 0) } + { fn IFNULL(SUM(Marketer.PercentPrice), 0) }) AS TotalFactorProfitOnAverage, SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + SUM(D.DiscountPrice) + { fn IFNULL(SUM(D.PurchaseTotalPrice), 0) } + { fn IFNULL(SUM(Marketer.PercentPrice), 0) }) AS TotalFactorProfitOnPurchasePrice, SUM(D.QuantityInUnitPrice) - (H.DiscountPrice + SUM(D.DiscountPrice) + { fn IFNULL(SUM(D.CostOfGood), 0) } + { fn IFNULL(SUM(Marketer.PercentPrice), 0) }) AS TotalFactorProfitOnCostOfGood, H.InsertedBy, dbo.UserInfo.RowName FROM  dbo.WhDocH AS H INNER JOIN dbo.UserInfo ON H.InsertedBy = dbo.UserInfo.RowID LEFT OUTER JOIN dbo.WhDocHMarketerPercent AS Marketer ON H.RowID = Marketer.WhDocH_ID LEFT OUTER JOIN dbo.WhDocD AS D ON H.RowID = D.WhDocH_ID LEFT OUTER JOIN dbo.Person AS P ON H.Person_ID = P.RowID LEFT OUTER JOIN dbo.WhDocHInvoiceIncrease AS Increase ON H.RowID = Increase.WhDocH_ID LEFT OUTER JOIN dbo.WhDocHInvoiceDecrease AS Decrease ON H.RowID = Decrease.WhDocH_ID WHERE  (H.Fix_WhDocType2_ID = 21) GROUP BY H.RowID, H.FDate, H.DocNo, P.RowName, P.RowCode, H.DiscountPrice, H.MDate, H.InsertedBy, dbo.UserInfo.RowName


