  
/****** Object:  Table [dbo].[BookMarkTypeList]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BookMarkTypeList]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[BookMarkTypeList](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[UserId] [int] NOT NULL,
	[BookMarkTypeId] [int] NOT NULL,
	[BookMarkTypeName] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_BookMarkTypeList] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[CampainChiper]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CampainChiper]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[CampainChiper](
	[CampainId] [int] IDENTITY(1,1) NOT NULL,
	[PortalId] [int] NOT NULL,
	[Name] [nvarchar](100) NULL,
	[StartDate] [datetime] NULL,
	[EndDate] [datetime] NULL,
	[DiscountType] [int] NULL,
	[DiscountAmount] [decimal](18, 4) NULL,
	[UserFilter] [nvarchar](300) NULL,
	[UserFilterDesc] [nvarchar](300) NULL,
	[StokFilter] [nvarchar](300) NULL,
	[StokFilterDesc] [nvarchar](300) NULL,
	[MinQty] [int] NULL,
	[MinPrice] [decimal](18, 4) NULL,
	[ApplyOrder] [int] NULL,
	[IsActive] [bit] NULL,
	[Details] [varchar](5000) NULL,
	[IsKdvDahil] [bit] NULL,
	[MinPriceCart] [decimal](18, 4) NULL,
 CONSTRAINT [PK_CampainChiper] PRIMARY KEY CLUSTERED 
(
	[CampainId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Download]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Download]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Download](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[ProductId] [int] NOT NULL,
	[DownloadGuid] [nvarchar](50) NULL,
	[DownloadUrlActive] [bit] NULL,
	[DownloadUrl] [nvarchar](250) NULL,
	[ContentType] [nvarchar](50) NULL,
	[FileName] [nvarchar](250) NULL,
	[Extension] [nvarchar](25) NULL,
	[DownloadFileName] [nvarchar](250) NULL,
	[DownloadCount] [int] NULL,
	[CreatedDate] [datetime] NULL,
	[ItemRate] [decimal](18, 1) NULL,
	[LivePreviewUrl] [nvarchar](250) NULL,
	[CreatedUserId] [int] NULL,
	[IsActive] [bit] NULL,
	[FileSize] [int] NULL,
 CONSTRAINT [PK_Download] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[DownloadItemRate]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DownloadItemRate]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[DownloadItemRate](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[DIND] [int] NULL,
	[UserId] [int] NULL,
	[ItemRate] [int] NULL,
	[CreatedDate] [datetime] NULL,
	[UpdateDate] [datetime] NULL,
	[Comments] [nvarchar](400) NULL,
 CONSTRAINT [PK_DownloadItemRate] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[DownloadUserList]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DownloadUserList]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[DownloadUserList](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[DIND] [int] NULL,
	[PIND] [int] NULL,
	[UIND] [int] NULL,
	[DownloadDate] [datetime] NULL,
	[DownloadCount] [int] NULL,
	[UpdateDate] [datetime] NULL,
 CONSTRAINT [PK_DownloadUserList] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[PosterDefinitions]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PosterDefinitions]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[PosterDefinitions](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[PosterName] [nvarchar](50) NULL,
	[PosterCode] [nvarchar](50) NULL,
	[IsActive] [bit] NULL,
 CONSTRAINT [PK_PosterDefinitions] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[PosterSubDefinitions]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PosterSubDefinitions]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[PosterSubDefinitions](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[PosterId] [int] NULL,
	[PosterSubName] [nvarchar](50) NULL,
	[PosterSubCode] [nvarchar](50) NULL,
	[IsActive] [bit] NULL,
	[ApplyOrder] [int] NULL,
 CONSTRAINT [PK_PosterSubDefinitions] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[PosterSubDefinitionValues]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PosterSubDefinitionValues]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[PosterSubDefinitionValues](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[ValueName] [nvarchar](50) NULL,
	[IsActive] [bit] NULL,
	[ApplyOrder] [int] NULL,
	[PosterSubId] [int] NULL,
 CONSTRAINT [PK_PosterSubDefinitionValues] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[PriceCategories]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PriceCategories]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[PriceCategories](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[PriceCatName] [nvarchar](50) NULL,
	[PriceCatCode] [nvarchar](50) NULL,
	[Status] [bit] NULL,
	[ApplyOrder] [int] NULL,
	[StartDate] [datetime] NULL,
	[EndDate] [datetime] NULL,
	[UserFilter] [nvarchar](150) NULL,
	[UserFilterDesc] [nvarchar](150) NULL,
	[CatFilter] [nvarchar](150) NULL,
	[CatFilterDesc] [nvarchar](150) NULL,
	[Details] [nvarchar](250) NULL,
	[IsForGuests] [bit] NULL,
 CONSTRAINT [PK_PriceCategories] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[PrimeExtreList]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PrimeExtreList]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[PrimeExtreList](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[UserId] [int] NOT NULL,
	[DiscountType] [bit] NOT NULL,
	[DiscountAmount] [decimal](18, 2) NOT NULL,
	[OrderDetailId] [int] NOT NULL,
	[TotalPrime] [decimal](18, 2) NOT NULL,
	[CreatedDate] [date] NOT NULL,
	[Status] [bit] NOT NULL,
 CONSTRAINT [PK_PrimeExtreList] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[ProductPosterOptionValues]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ProductPosterOptionValues]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ProductPosterOptionValues](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[ProductId] [int] NULL,
	[PosterSubId] [int] NULL,
	[PosterSubValues] [nvarchar](500) NULL,
	[UnitId] [int] NULL,
	[Qty] [decimal](18, 2) NULL,
	[Price] [decimal](18, 2) NULL,
	[Currency] [int] NULL,
 CONSTRAINT [PK_ProductPosterOptionValues] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[S_BillFields]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[S_BillFields]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[S_BillFields](
	[FaturaAlanId] [int] IDENTITY(1,1) NOT NULL,
	[Baslik] [nvarchar](max) NULL,
	[VtAdi] [nvarchar](max) NULL,
	[Durum] [int] NOT NULL,
	[DuzenlemeTarihi] [smalldatetime] NULL,
	[EklenmeTarihi] [smalldatetime] NULL,
 CONSTRAINT [PK_Ext_S_FaturaAlanlari] PRIMARY KEY CLUSTERED 
(
	[FaturaAlanId] 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]
END
GO
/****** Object:  Table [dbo].[S_BillingPageSize]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[S_BillingPageSize]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[S_BillingPageSize](
	[SayfaBoyutId] [int] IDENTITY(1,1) NOT NULL,
	[Baslik] [nvarchar](150) NOT NULL,
	[Genislik] [nvarchar](50) NOT NULL,
	[Yukseklik] [nvarchar](50) NOT NULL,
	[Durum] [int] NOT NULL,
	[FontAdi] [nvarchar](50) NULL,
	[FontBoyutu] [nvarchar](50) NULL,
	[DuzenlemeTarihi] [smalldatetime] NULL,
	[EklenmeTarihi] [smalldatetime] NULL,
 CONSTRAINT [PK_Ext_S_FaturaSayfaBoyutu] PRIMARY KEY CLUSTERED 
(
	[SayfaBoyutId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[S_BillingStructure]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[S_BillingStructure]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[S_BillingStructure](
	[SayfaId] [int] IDENTITY(1,1) NOT NULL,
	[SayfaBoyutId] [int] NOT NULL,
	[Baslik] [nvarchar](max) NULL,
	[FontAdi] [nvarchar](150) NOT NULL,
	[FontBoyutu] [nvarchar](50) NOT NULL,
	[Varsayilan] [int] NULL,
 CONSTRAINT [PK_Ext_S_FaturaSayfaYapisi] PRIMARY KEY CLUSTERED 
(
	[SayfaId] 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]
END
GO
/****** Object:  Table [dbo].[S_InvoiceTemplate]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[S_InvoiceTemplate]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[S_InvoiceTemplate](
	[FaturaSablonuId] [int] IDENTITY(1,1) NOT NULL,
	[SayfaBoyutId] [int] NOT NULL,
	[Baslik] [nvarchar](max) NULL,
	[UstBilgi] [nvarchar](max) NOT NULL,
	[AltBilgi] [nvarchar](max) NOT NULL,
	[Ayrintilar] [nvarchar](max) NULL,
	[Durum] [int] NOT NULL,
	[EklenmeTarihi] [datetime] NULL,
	[DuzenlemeTarihi] [datetime] NULL,
	[Varsayilan] [int] NOT NULL,
 CONSTRAINT [PK_Ext_S_FaturaSablonu] PRIMARY KEY CLUSTERED 
(
	[FaturaSablonuId] 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]
END
GO
/****** Object:  Table [dbo].[S_InvoiceTemplateSettings]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[S_InvoiceTemplateSettings]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[S_InvoiceTemplateSettings](
	[SablonAlanId] [int] IDENTITY(1,1) NOT NULL,
	[FaturaSablonuId] [int] NOT NULL,
	[FaturaAlanId] [int] NOT NULL,
	[Yukseklik] [nvarchar](max) NULL,
	[Genislik] [nvarchar](max) NOT NULL,
	[Konum] [nvarchar](max) NOT NULL,
	[Bolum] [nvarchar](50) NULL,
	[Durum] [int] NOT NULL,
	[DuzenlemeTarihi] [datetime] NULL,
	[EklenmeTarihi] [datetime] NULL,
 CONSTRAINT [PK_Ext_S_FaturaSablonAlanlari] PRIMARY KEY CLUSTERED 
(
	[SablonAlanId] 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]
END
GO
/****** Object:  Table [dbo].[SaleBonusDefinitions]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SaleBonusDefinitions]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[SaleBonusDefinitions](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[BonusName] [nvarchar](50) NOT NULL,
	[IsActive] [bit] NULL,
	[ApplyOrder] [int] NULL,
	[StartDate] [datetime] NULL,
	[EndDate] [datetime] NULL,
	[BonusType] [int] NULL,
	[BonusAmount] [decimal](18, 4) NULL,
	[UserFilter] [nvarchar](250) NULL,
	[StokFilter] [nvarchar](250) NULL,
	[UserFilterDesc] [nvarchar](250) NULL,
	[StokFilterDesc] [nvarchar](250) NULL,
	[Description] [nvarchar](1000) NULL,
	[OrderTypes] [nvarchar](100) NULL,
	[IsQuota] [bit] NULL,
 CONSTRAINT [PK_SaleBonusDefinitions] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[SaleQuotaDefinitions]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SaleQuotaDefinitions]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[SaleQuotaDefinitions](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[BonusId] [int] NOT NULL,
	[TotalAmount] [decimal](18, 4) NULL,
	[BonusType] [int] NULL,
	[BonusAmount] [decimal](18, 4) NULL,
	[IsKdvDahil] [bit] NULL,
 CONSTRAINT [PK_SaleQuotaDefinitions] PRIMARY KEY CLUSTERED 
(
	[IND] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[TimeCostWork]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TimeCostWork]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[TimeCostWork](
	[TimeCostId] [int] IDENTITY(1,1) NOT NULL,
	[StartDate] [smalldatetime] NULL,
	[AssignedUserId] [int] NULL,
	[UserId] [int] NULL,
	[ProductId] [int] NULL,
	[ProjectId] [int] NULL,
	[JobID] [int] NULL,
	[Description] [nvarchar](500) NULL,
	[CreatedDate] [smalldatetime] NULL,
	[UpdatedDate] [smalldatetime] NULL,
	[CreatedUserId] [int] NULL,
	[UpdatedUserId] [int] NULL,
	[Time] [decimal](18, 2) NULL,
 CONSTRAINT [PK_TimeCostWork] PRIMARY KEY CLUSTERED 
(
	[TimeCostId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Index [ProductId]    Script Date: 28.9.2016 10:41:25 ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[Download]') AND name = N'ProductId')
CREATE NONCLUSTERED INDEX [ProductId] ON [dbo].[Download]
(
	[ProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_Download_DownloadCount]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[Download] ADD  CONSTRAINT [DF_Download_DownloadCount]  DEFAULT ((0)) FOR [DownloadCount]
END

GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_Download_CreatedDate]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[Download] ADD  CONSTRAINT [DF_Download_CreatedDate]  DEFAULT (getdate()) FOR [CreatedDate]
END

GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_Download_ItemRate]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[Download] ADD  CONSTRAINT [DF_Download_ItemRate]  DEFAULT ((0)) FOR [ItemRate]
END

GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_DownloadItemRate_CreatedDate]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[DownloadItemRate] ADD  CONSTRAINT [DF_DownloadItemRate_CreatedDate]  DEFAULT (getdate()) FOR [CreatedDate]
END

GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_DownloadUserList_DownloadDate]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[DownloadUserList] ADD  CONSTRAINT [DF_DownloadUserList_DownloadDate]  DEFAULT (getdate()) FOR [DownloadDate]
END

GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_DownloadUserList_DownloadCount]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[DownloadUserList] ADD  CONSTRAINT [DF_DownloadUserList_DownloadCount]  DEFAULT ((0)) FOR [DownloadCount]
END

GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_TimeCostWork_CreatedDate]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[TimeCostWork] ADD  CONSTRAINT [DF_TimeCostWork_CreatedDate]  DEFAULT (getdate()) FOR [CreatedDate]
END

GO
/****** Object:  Trigger [dbo].[DownloadItemRate_Delete]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[DownloadItemRate_Delete]'))
EXEC dbo.sp_executesql @statement = N'CREATE TRIGGER [dbo].[DownloadItemRate_Delete]
ON [dbo].[DownloadItemRate]
FOR  DELETE 
AS
  
DECLARE @prodId int,@userId int,@rowNumber int,@qty int,@total decimal(18,1)

SELECT @prodId=deleted.DIND  from deleted 

SELECT @total = Sum(ItemRate) / COUNT(*) FROM DownloadItemRate WHERE DownloadItemRate.DIND=@prodId
 
UPDATE Download 
    SET ItemRate = @total
WHERE Download.ProductId = @prodId
 ' 
GO
/****** Object:  Trigger [dbo].[DownloadItemRate_InsertTrigger]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[DownloadItemRate_InsertTrigger]'))
EXEC dbo.sp_executesql @statement = N'CREATE TRIGGER [dbo].[DownloadItemRate_InsertTrigger]
ON [dbo].[DownloadItemRate]
FOR INSERT AS

DECLARE @prodId int,@userId int,@rowNumber int,@qty int,@total decimal(18,1)

SELECT @prodId=inserted.DIND  from inserted 

SELECT @total = Sum(ItemRate) / COUNT(*) FROM DownloadItemRate WHERE DownloadItemRate.DIND=@prodId
 
UPDATE Download 
    SET ItemRate = @total
WHERE Download.ProductId = @prodId
 ' 
GO
/****** Object:  Trigger [dbo].[DownloadItemRate_UpdateTrigger]    Script Date: 28.9.2016 10:41:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[DownloadItemRate_UpdateTrigger]'))
EXEC dbo.sp_executesql @statement = N'CREATE TRIGGER [dbo].[DownloadItemRate_UpdateTrigger]
ON [dbo].[DownloadItemRate]
FOR UPDATE AS
 
 DECLARE @prodId int,@userId int,@rowNumber int,@qty int,@total decimal(18,1)

SELECT @prodId=inserted.DIND  from inserted 

SELECT @total = Sum(ItemRate) / COUNT(*) FROM DownloadItemRate WHERE DownloadItemRate.DIND=@prodId
 
UPDATE Download 
    SET ItemRate = @total
WHERE Download.ProductId = @prodId
 ' 
GO



/****** Object:  Trigger [dbo].[Categories_Delete]    Script Date: 20.8.2016 12:12:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Categories_Delete]
ON [dbo].[Categories]
FOR  DELETE 
AS

declare @DeletedLinage nvarchar(100)

select @DeletedLinage=Lineage from deleted

delete  CategoryDescription where Lineage like (@DeletedLinage + '%')

delete  Categories where Lineage like (@DeletedLinage + '%')


GO

ALTER TABLE FlowerDetails  ALTER COLUMN RecieverName nvarchar(50)
ALTER TABLE FlowerDetails  ALTER COLUMN Adress nvarchar(200)
ALTER TABLE FlowerDetails  ALTER COLUMN CartName nvarchar(50)
ALTER TABLE FlowerDetails  ALTER COLUMN CartMessage nvarchar(200)

ALTER TABLE FlowerOrderDetail  ALTER COLUMN RecieverName nvarchar(50) 
ALTER TABLE FlowerOrderDetail  ALTER COLUMN Adress nvarchar(200)
ALTER TABLE FlowerOrderDetail  ALTER COLUMN CartName nvarchar(50)
ALTER TABLE FlowerOrderDetail  ALTER COLUMN CartMessage nvarchar(200)


ALTER TABLE OrderDetails ADD SecretCode1 nvarchar(100) 
ALTER TABLE OrderDetails ADD SecretCode2 nvarchar(100) 
    
ALTER TABLE PriceList ADD Discount2 decimal(18,2) default(0) 
ALTER TABLE PriceList ADD Discount3 decimal(18,2) default(0) 
ALTER TABLE PriceList ADD Discount4 decimal(18,2) default(0) 
ALTER TABLE PriceList ADD Discount5 decimal(18,2) default(0) 


ALTER TABLE PriceProducts  ALTER COLUMN StokFilter nvarchar(500)
ALTER TABLE PriceProducts  ALTER COLUMN StokFilterDesc nvarchar(500)

ALTER TABLE ProductImages ADD ShowOrder int default(1)

ALTER TABLE ServicesJob ADD ProjectID int 

ALTER TABLE Users ADD Image nvarchar(100)   DEFAULT(N'user.gif')

GO 
 
/****** Object:  View [dbo].[AdminProductList]    Script Date: 19.9.2016 17:45:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER VIEW [dbo].[AdminProductList]
AS
SELECT        dbo.Categories.CatId, dbo.Categories.CatName, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.Products.StockQty, dbo.ProductMarks.MarKName, dbo.Products.SellNoneStock, 
                         dbo.Products.ShowInMainPage, dbo.ProductMarks.MarkId, dbo.Products.ShowOrder, dbo.Products.Code11, dbo.Products.Code12, dbo.Products.Code13, dbo.Products.Code14, dbo.Products.Code15, 
                         dbo.Products.IsActive, dbo.Products.Code1, dbo.Products.PortalId, dbo.Products.CreatedDate, dbo.Categories.Lineage, dbo.Products.WizardId, dbo.Products.IsPenta, dbo.Products.CampainId, 
                         dbo.Products.UserId, dbo.Products.ProductSizeOptionId, dbo.Products.AuthorId, dbo.Products.ProviderId, dbo.Authors.AuthorId AS Expr1, dbo.Authors.AuthorName, dbo.Publishers.PublisherId, 
                         dbo.Publishers.PublisherName, dbo.Products.Code2, dbo.Products.Code3, dbo.Products.Code4, dbo.Products.Code5, dbo.Products.Code6, dbo.Products.Code7, dbo.Products.Code8, dbo.Products.Code9, 
                         dbo.Products.Code10, dbo.Products.StokType, dbo.Products.CompanyId, dbo.ProductDescription.ProductName, dbo.ProductDescription.Description, dbo.ProductDescription.Details, 
                         dbo.ProductDescription.LanguageId, dbo.ProductDescription.LanguageCode, dbo.Products.IsDeleted
FROM            dbo.Products INNER JOIN
                         dbo.ProductDescription ON dbo.Products.ProductId = dbo.ProductDescription.ProductId INNER JOIN
                         dbo.Language ON dbo.ProductDescription.LanguageId = dbo.Language.LanguageId LEFT OUTER JOIN
                         dbo.Publishers ON dbo.Products.PublisherId = dbo.Publishers.PublisherId LEFT OUTER JOIN
                         dbo.Authors ON dbo.Products.AuthorId = dbo.Authors.AuthorId LEFT OUTER JOIN
                         dbo.ProductCategories INNER JOIN
                         dbo.Categories ON dbo.ProductCategories.CategoryId = dbo.Categories.CatId ON dbo.Products.ProductId = dbo.ProductCategories.ProductId LEFT OUTER JOIN
                         dbo.ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId


GO


 
/****** Object:  View [dbo].[AdminProductListAll]    Script Date: 19.9.2016 17:45:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER VIEW [dbo].[AdminProductListAll]
AS
SELECT        dbo.Categories.CatId, dbo.Categories.CatName, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.Products.StockQty, dbo.ProductMarks.MarKName, dbo.ProductUnits.BirimAdi, 
                         dbo.ProductUnits.SatisFiyati0, dbo.ProductUnits.SatisFiyati1, dbo.ProductUnits.SatisFiyati2, dbo.ProductUnits.SatisFiyati3, dbo.Products.SellNoneStock, dbo.Categories.PortalId, dbo.ProductMarks.MarkId, 
                         dbo.Products.Code1, dbo.ProductUnits.Desi, dbo.Products.IsActive, Currencys_0.CurrencyName AS Pb0, Currencys_1.CurrencyName AS Pb1, Currencys_2.CurrencyName AS Pb2, 
                         Currencys_3.CurrencyName AS Pb3, dbo.ProductUnits.UnitId, dbo.Products.ShowInMainPage, dbo.ProductUnits.LastModified, dbo.ProductUnits.AlisFiyati, Currencys_A.CurrencyName AS APb, 
                         dbo.Categories.Lineage, dbo.Products.CampainId, dbo.Products.IsPenta, dbo.ProductImages.Path, dbo.ProductImages.IsDefault, dbo.Products.CreatedDate, dbo.ProductUsages.UsageId, 
                         dbo.ProductAlternatives.AlternativeId, dbo.Products.WizardId, dbo.ProductAttributes.ProductAttributesId, dbo.Products.ShowOrder, dbo.Products.ForceCargo, dbo.Products.HizliKargo, dbo.Products.AyniGunTeslim, 
                         dbo.Products.IndirimliUrun, dbo.Products.FirsatUrunu, dbo.Products.SokFiyatliUrun, dbo.Products.EnUcuzUrun, dbo.Products.OzelUrun, dbo.Products.SinirliSayidaUrun, dbo.Products.YeniUrun, 
                         dbo.Products.HediyeliUrun, Currencys_P.CurrencyName AS PPb, dbo.ProductUnits.PiyasaFiyati, Currencys_4.CurrencyName AS Pb4, dbo.ProductUnits.SatisFiyati4, Currencys_5.CurrencyName AS Pb5, 
                         dbo.ProductUnits.SatisFiyati5, dbo.Products.UserId, dbo.Products.ProductSizeOptionId, dbo.Products.ProviderId, dbo.Authors.AuthorName, dbo.Authors.AuthorId, dbo.Publishers.PublisherId, 
                         dbo.Publishers.PublisherName, dbo.Products.AuthorId AS Expr2, dbo.Products.Code2, dbo.Products.Code3, dbo.Products.Code4, dbo.Products.Code5, dbo.Products.Code6, dbo.Products.Code7, 
                         dbo.Products.Code8, dbo.Products.Code9, dbo.Products.Code10, dbo.Products.Code11, dbo.Products.Code12, dbo.Products.Code13, dbo.Products.Code14, dbo.Products.Code15, dbo.ProductUnits.Barcode, 
                         dbo.Products.StokType, dbo.ProductUnits.KdvDahil, dbo.Products.CompanyId, dbo.Products.KdvId, dbo.ProductDescription.ProductName, dbo.ProductDescription.LanguageId, 
                         dbo.ProductDescription.LanguageCode, dbo.Products.IsDeleted
FROM            dbo.Products INNER JOIN
                         dbo.Currencys AS Currencys_A INNER JOIN
                         dbo.Currencys AS Currencys_2 INNER JOIN
                         dbo.ProductUnits INNER JOIN
                         dbo.Currencys AS Currencys_1 ON dbo.ProductUnits.Pb1 = Currencys_1.CurrencyId ON Currencys_2.CurrencyId = dbo.ProductUnits.Pb2 INNER JOIN
                         dbo.Currencys AS Currencys_3 ON dbo.ProductUnits.Pb3 = Currencys_3.CurrencyId ON Currencys_A.CurrencyId = dbo.ProductUnits.APb INNER JOIN
                         dbo.Currencys AS Currencys_0 ON dbo.ProductUnits.Pb0 = Currencys_0.CurrencyId ON dbo.Products.ProductId = dbo.ProductUnits.ProductId INNER JOIN
                         dbo.Currencys AS Currencys_P ON dbo.ProductUnits.PPb = Currencys_P.CurrencyId INNER JOIN
                         dbo.Currencys AS Currencys_4 ON dbo.ProductUnits.Pb4 = Currencys_4.CurrencyId INNER JOIN
                         dbo.Currencys AS Currencys_5 ON dbo.ProductUnits.Pb5 = Currencys_5.CurrencyId INNER JOIN
                         dbo.ProductDescription ON dbo.Products.ProductId = dbo.ProductDescription.ProductId INNER JOIN
                         dbo.Language ON dbo.ProductDescription.LanguageId = dbo.Language.LanguageId LEFT OUTER JOIN
                         dbo.Authors ON dbo.Products.AuthorId = dbo.Authors.AuthorId LEFT OUTER JOIN
                         dbo.Publishers ON dbo.Products.PublisherId = dbo.Publishers.PublisherId LEFT OUTER JOIN
                         dbo.ProductAttributes ON dbo.Products.ProductId = dbo.ProductAttributes.ProductId LEFT OUTER JOIN
                         dbo.ProductAlternatives ON dbo.Products.ProductId = dbo.ProductAlternatives.ProductId LEFT OUTER JOIN
                         dbo.ProductUsages ON dbo.Products.ProductId = dbo.ProductUsages.ProductId LEFT OUTER JOIN
                         dbo.ProductImages ON dbo.Products.ProductId = dbo.ProductImages.ProductId LEFT OUTER JOIN
                         dbo.ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId LEFT OUTER JOIN
                         dbo.ProductCategories INNER JOIN
                         dbo.Categories ON dbo.ProductCategories.CategoryId = dbo.Categories.CatId ON dbo.Products.ProductId = dbo.ProductCategories.ProductId


GO




  

/****** Object:  View [dbo].[AdminProductListAll2]    Script Date: 19.9.2016 17:46:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER VIEW [dbo].[AdminProductListAll2]
AS
SELECT        dbo.Categories.CatId, dbo.Categories.CatName, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.Products.StockQty, dbo.ProductMarks.MarKName, dbo.ProductUnits.BirimAdi, 
                         dbo.ProductUnits.SatisFiyati0, dbo.ProductUnits.SatisFiyati1, dbo.ProductUnits.SatisFiyati2, dbo.ProductUnits.SatisFiyati3, dbo.Products.SellNoneStock, dbo.Categories.PortalId, dbo.ProductMarks.MarkId, 
                         dbo.Products.Code1, dbo.ProductUnits.Desi, dbo.Products.IsActive, Currencys_0.CurrencyName AS Pb0, Currencys_1.CurrencyName AS Pb1, Currencys_2.CurrencyName AS Pb2, 
                         Currencys_3.CurrencyName AS Pb3, dbo.ProductUnits.UnitId, dbo.Products.ShowInMainPage, dbo.ProductUnits.LastModified, dbo.ProductUnits.EskiAlisFiyati, dbo.ProductUnits.AlisFiyati, 
                         Currencys_A.CurrencyName AS APb, dbo.Categories.Lineage, dbo.Products.CampainId, dbo.Products.IsPenta, dbo.ProductImages.Path, dbo.ProductImages.IsDefault, dbo.Products.CreatedDate, 
                         dbo.ProductUsages.UsageId, dbo.ProductAlternatives.AlternativeId, dbo.Products.WizardId, dbo.ProductAttributes.ProductAttributesId, dbo.Products.ShowOrder, dbo.Products.Code2, dbo.Products.Code3, 
                         dbo.Products.Code4, dbo.Products.Code5, dbo.Products.Code6, dbo.Products.Code7, dbo.Products.Code8, dbo.Products.Code9, dbo.Products.Code10, dbo.Products.Code11, dbo.Products.Code12, 
                         dbo.Products.Code13, dbo.Products.Code14, dbo.Products.Code15, Currencys_4.CurrencyName AS Pb4, Currencys_5.CurrencyName AS Pb5, Currencys_P.CurrencyName AS PPb, dbo.ProductUnits.SatisFiyati4, 
                         dbo.ProductUnits.SatisFiyati5, dbo.Products.HizliKargo, dbo.Products.AyniGunTeslim, dbo.Products.IndirimliUrun, dbo.Products.FirsatUrunu, dbo.Products.SokFiyatliUrun, dbo.Products.EnUcuzUrun, 
                         dbo.Products.OzelUrun, dbo.Products.SinirliSayidaUrun, dbo.Products.YeniUrun, dbo.Products.HediyeliUrun, dbo.ProductUnits.PiyasaFiyati, dbo.Products.AuthorId, dbo.Products.PublisherId, 
                         dbo.ProductDescription.LanguageId, dbo.ProductDescription.LanguageCode, dbo.ProductDescription.ProductName, dbo.ProductDescription.Description, dbo.ProductDescription.Details, 
                         dbo.Products.ProviderId
FROM            dbo.Products INNER JOIN
                         dbo.Currencys AS Currencys_A INNER JOIN
                         dbo.Currencys AS Currencys_2 INNER JOIN
                         dbo.ProductUnits INNER JOIN
                         dbo.Currencys AS Currencys_1 ON dbo.ProductUnits.Pb1 = Currencys_1.CurrencyId ON Currencys_2.CurrencyId = dbo.ProductUnits.Pb2 INNER JOIN
                         dbo.Currencys AS Currencys_3 ON dbo.ProductUnits.Pb3 = Currencys_3.CurrencyId ON Currencys_A.CurrencyId = dbo.ProductUnits.APb INNER JOIN
                         dbo.Currencys AS Currencys_0 ON dbo.ProductUnits.Pb0 = Currencys_0.CurrencyId ON dbo.Products.ProductId = dbo.ProductUnits.ProductId INNER JOIN
                         dbo.Currencys AS Currencys_4 ON dbo.ProductUnits.Pb4 = Currencys_4.CurrencyId INNER JOIN
                         dbo.Currencys AS Currencys_5 ON dbo.ProductUnits.Pb5 = Currencys_5.CurrencyId INNER JOIN
                         dbo.Currencys AS Currencys_P ON Currencys_A.CurrencyId = Currencys_P.CurrencyId INNER JOIN
                         dbo.ProductDescription ON dbo.Products.ProductId = dbo.ProductDescription.ProductId LEFT OUTER JOIN
                         dbo.ProductAttributes ON dbo.Products.ProductId = dbo.ProductAttributes.ProductId LEFT OUTER JOIN
                         dbo.ProductAlternatives ON dbo.Products.ProductId = dbo.ProductAlternatives.ProductId LEFT OUTER JOIN
                         dbo.ProductUsages ON dbo.Products.ProductId = dbo.ProductUsages.ProductId LEFT OUTER JOIN
                         dbo.ProductImages ON dbo.Products.ProductId = dbo.ProductImages.ProductId LEFT OUTER JOIN
                         dbo.ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId LEFT OUTER JOIN
                         dbo.ProductCategories INNER JOIN
                         dbo.Categories ON dbo.ProductCategories.CategoryId = dbo.Categories.CatId ON dbo.Products.ProductId = dbo.ProductCategories.ProductId


GO



 
/****** Object:  View [dbo].[OrdersList]    Script Date: 19.9.2016 17:46:37 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[OrdersList]
AS
SELECT        dbo.OrderStatus.StatusName, dbo.users.FullName, dbo.users.Email, dbo.Orders.OrderId, dbo.Orders.PortalId, dbo.Orders.CustomerId, dbo.Orders.OrderDate, dbo.Orders.ShipDate, dbo.Orders.PaymentType, 
                         dbo.Orders.IsPayed, dbo.Orders.Note, dbo.Orders.ShippingName, dbo.Orders.ShippingPhoneNo, dbo.Orders.ShippingMobilePhone, dbo.Orders.ShippingCityName, dbo.Orders.ShippingAddress, 
                         dbo.Orders.ShippingCargoName, dbo.Orders.BillingName, dbo.Orders.BillingPhoneNo, dbo.Orders.BillingMobilePhoneNo, dbo.Orders.BillingTaxOffice, dbo.Orders.BillingTaxNo, dbo.Orders.BillingCityName, 
                         dbo.Orders.BillingAddress, dbo.Orders.ToplamTutar, dbo.Orders.KdvDahilToplam, dbo.Orders.ToplamKdv, dbo.Orders.ToplamKargo, dbo.Orders.KargoDahilToplamTutar, dbo.Orders.ToplamIndirim, 
                         dbo.Orders.CekIndirimi, dbo.Orders.ToplamAgirlik, dbo.Orders.ToplamHacim, dbo.Orders.ToplamDesi, dbo.Orders.BankName, dbo.Orders.CardName, dbo.Orders.Taksit, dbo.Orders.BankaToplamTutar, 
                         dbo.Orders.BankPayType, dbo.Orders.Status, dbo.Orders.Integreted, dbo.Orders.ToplamPuan, dbo.Orders.Ip, dbo.Orders.LastChange, dbo.users.TCKimlikNo, dbo.users.MobilePhone, dbo.Orders.Aciklama, 
                         dbo.Orders.Aciklama2, dbo.Orders.OrderCode, dbo.Orders.OrderPaymentCode, dbo.Orders.CurrencyRate, dbo.Orders.CurrencyCode, dbo.Orders.CustomerCurrencyRate, dbo.Orders.CustomerCurrencyCode, 
                         dbo.Orders.TaksitRate, dbo.Orders.ShippingCountryName, dbo.Orders.ShippingDistrictName, dbo.Orders.BillingCountryName, dbo.Orders.BillingDistrictName, dbo.Orders.MultiAdminID, dbo.Orders.ShippingMail, 
                         dbo.users.UserType
FROM            dbo.Orders LEFT OUTER JOIN
                         dbo.users ON dbo.Orders.CustomerId = dbo.users.UserId LEFT OUTER JOIN
                         dbo.OrderStatus ON dbo.Orders.Status = dbo.OrderStatus.StatusId

GO

 
/****** Object:  View [dbo].[ProductList]    Script Date: 19.9.2016 17:46:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER VIEW [dbo].[ProductList]
AS
SELECT        dbo.Categories.CatId, dbo.CategoryDescription.CatName, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.ProductDescription.ProductName, dbo.ProductDescription.Description, dbo.Products.StockQty, 
                         dbo.ProductKdvs.KdvRate, dbo.ProductImages.Path, dbo.ProductMarks.MarKName, dbo.ProductUnits.BirimAdi, dbo.ProductUnits.Carpan, dbo.ProductUnits.SatisFiyati0, dbo.ProductUnits.SatisFiyati1, 
                         dbo.ProductUnits.SatisFiyati2, dbo.ProductUnits.SatisFiyati3, dbo.ProductUnits.Isk1, dbo.ProductUnits.Isk2, dbo.ProductUnits.Isk3, Currencys_0.CurrencyName AS Currency0, 
                         Currencys_1.CurrencyName AS Currency1, Currencys_2.CurrencyName AS Currency2, Currencys_3.CurrencyName AS Currency3, Currencys_0.Rate AS Rate0, Currencys_1.Rate AS Rate1, 
                         Currencys_2.Rate AS Rate2, Currencys_3.Rate AS Rate3, dbo.Products.SellNoneStock, dbo.Products.ShowInMainPage, dbo.Categories.PortalId, dbo.ProductMarks.MarkId, dbo.Products.ShowOrder, 
                         dbo.ProductDescription.Details, dbo.CategoryDescription.Url AS CatUrl, dbo.ProductDescription.Url AS ProductUrl, dbo.Categories.Lineage, dbo.Products.SearchKeywords, dbo.Products.Hit, 
                         dbo.Products.PublisherId, Currencys_P.CurrencyName AS CurrencyP, dbo.ProductUnits.PiyasaFiyati, Currencys_4.CurrencyName AS Currency4, Currencys_5.CurrencyName AS Currency5, 
                         Currencys_4.Rate AS Rate4, Currencys_5.Rate AS Rate5, Currencys_P.Rate AS RateP, Currencys_A.Rate AS RateA, dbo.ProductUnits.SatisFiyati4, dbo.ProductUnits.SatisFiyati5, dbo.ProductUnits.Isk4, 
                         dbo.ProductUnits.Isk5, dbo.Products.HizliKargo, dbo.Products.AyniGunTeslim, dbo.Products.IndirimliUrun, dbo.Products.FirsatUrunu, dbo.Products.SokFiyatliUrun, dbo.Products.EnUcuzUrun, 
                         dbo.Products.OzelUrun, dbo.Products.SinirliSayidaUrun, dbo.Products.YeniUrun, dbo.Products.HediyeliUrun, dbo.Products.StartDate, dbo.Products.EndDate, dbo.Categories.StartDate AS CStartDate, 
                         dbo.Categories.EndDate AS CEndDate, dbo.Products.ProductSizeOptionId, dbo.ProductUnits.Barcode, dbo.Products.Code15, dbo.Products.Code14, dbo.Products.Code13, dbo.Products.Code12, 
                         dbo.Products.Code11, dbo.Products.Code10, dbo.Products.Code9, dbo.Products.Code8, dbo.Products.Code7, dbo.Products.Code6, dbo.Products.Code5, dbo.Products.Code4, dbo.Products.Code3, 
                         dbo.Products.Code2, dbo.Products.Code1, dbo.Products.StokType, dbo.ProductUnits.AlisFiyati, dbo.Products.AuthorId, dbo.Products.CompanyId, dbo.Products.IsActive, 
                         dbo.ProductImages.Description AS ImageDesc, dbo.ProductDescription.LanguageCode, dbo.Products.IsDeleted
FROM            dbo.ProductCategories INNER JOIN
                         dbo.Categories ON dbo.ProductCategories.CategoryId = dbo.Categories.CatId INNER JOIN
                         dbo.ProductUnits INNER JOIN
                         dbo.Products ON dbo.ProductUnits.ProductId = dbo.Products.ProductId ON dbo.ProductCategories.ProductId = dbo.Products.ProductId INNER JOIN
                         dbo.ProductKdvs ON dbo.Products.KdvId = dbo.ProductKdvs.KdvId INNER JOIN
                         dbo.ProductImages ON dbo.Products.ProductId = dbo.ProductImages.ProductId AND dbo.ProductImages.IsDefault = 1 INNER JOIN
                         dbo.CategoryDescription ON dbo.Categories.CatId = dbo.CategoryDescription.CatId INNER JOIN
                         dbo.ProductDescription ON dbo.ProductCategories.ProductId = dbo.ProductDescription.ProductId INNER JOIN
                         dbo.Language ON dbo.CategoryDescription.LanguageId = dbo.Language.LanguageId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_5 ON dbo.ProductUnits.Pb5 = Currencys_5.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_4 ON dbo.ProductUnits.Pb4 = Currencys_4.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_P ON dbo.ProductUnits.PPb = Currencys_P.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_0 ON dbo.ProductUnits.Pb0 = Currencys_0.CurrencyId LEFT OUTER JOIN
                         dbo.ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_2 ON dbo.ProductUnits.Pb2 = Currencys_2.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_1 ON dbo.ProductUnits.Pb1 = Currencys_1.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_3 ON dbo.ProductUnits.Pb3 = Currencys_3.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_A ON dbo.ProductUnits.APb = Currencys_A.CurrencyId
WHERE        (dbo.Categories.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (Currencys_1.CurrencyId > 0) AND 
                         (NOT (dbo.ProductImages.Path = N'')) AND (ISNULL(dbo.Categories.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Categories.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) 
                         AND (ISNULL(dbo.Products.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Products.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (Currencys_1.CurrencyId > 0) AND 
                         (NOT (dbo.ProductImages.Path = N'')) AND (ISNULL(dbo.Categories.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Categories.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) 
                         AND (ISNULL(dbo.Products.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Products.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) AND (dbo.ProductMarks.IsActive = 1) AND 
                         (dbo.ProductMarks.MarkId IS NULL)


GO


 
/****** Object:  View [dbo].[ProductListDetails]    Script Date: 19.9.2016 17:47:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[ProductListDetails]
AS
SELECT        dbo.Categories.CatId, dbo.Categories.CatName, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.Products.ProductName, dbo.Products.Description, dbo.Products.StockQty, dbo.ProductKdvs.KdvRate, 
                         dbo.ProductImages.Path, dbo.ProductMarks.MarKName, dbo.ProductUnits.BirimAdi, dbo.ProductUnits.Carpan, dbo.ProductUnits.SatisFiyati0, dbo.ProductUnits.SatisFiyati1, dbo.ProductUnits.SatisFiyati2, 
                         dbo.ProductUnits.SatisFiyati3, dbo.ProductUnits.Isk1, dbo.ProductUnits.Isk2, dbo.ProductUnits.Isk3, dbo.Currencys.CurrencyName AS Currency0, Currencys_1.CurrencyName AS Currency1, 
                         Currencys_2.CurrencyName AS Currency2, Currencys_3.CurrencyName AS Currency3, dbo.Currencys.Rate AS Rate0, Currencys_1.Rate AS Rate1, Currencys_2.Rate AS Rate2, Currencys_3.Rate AS Rate3, 
                         dbo.Products.SellNoneStock, dbo.Products.ShowInMainPage, dbo.Categories.PortalId, dbo.ProductMarks.MarkId, dbo.Products.ShowOrder, dbo.Products.Details, dbo.Products.Code14, dbo.Products.Code15, 
                         dbo.Categories.Url AS CatUrl, dbo.Products.Url AS ProductUrl, dbo.Categories.Lineage, dbo.ProductUnits.UnitId, dbo.ProductUnits.IsDefault AS UnitsIsDefault, dbo.Products.WizardId, dbo.Products.CampainId, 
                         dbo.Products.ForceCargo, dbo.Products.Code1, dbo.Products.Code2, dbo.Products.Code3, dbo.Products.Code4, dbo.Products.Code5, dbo.Products.Code6, dbo.Products.Code7, dbo.Products.Code8, 
                         dbo.Products.Code9, dbo.Products.Code13, dbo.Products.Code12, dbo.Products.Code11, dbo.Products.Code10
FROM            dbo.ProductCategories INNER JOIN
                         dbo.Categories ON dbo.ProductCategories.CategoryId = dbo.Categories.CatId INNER JOIN
                         dbo.ProductUnits INNER JOIN
                         dbo.Products ON dbo.ProductUnits.ProductId = dbo.Products.ProductId ON dbo.ProductCategories.ProductId = dbo.Products.ProductId INNER JOIN
                         dbo.ProductKdvs ON dbo.Products.KdvId = dbo.ProductKdvs.KdvId INNER JOIN
                         dbo.ProductImages ON dbo.Products.ProductId = dbo.ProductImages.ProductId LEFT OUTER JOIN
                         dbo.Currencys ON dbo.ProductUnits.Pb0 = dbo.Currencys.CurrencyId LEFT OUTER JOIN
                         dbo.ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_2 ON dbo.ProductUnits.Pb2 = Currencys_2.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_1 ON dbo.ProductUnits.Pb1 = Currencys_1.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_3 ON dbo.ProductUnits.Pb3 = Currencys_3.CurrencyId
WHERE        (dbo.Categories.IsActive = 1) AND (dbo.ProductMarks.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND 
                         (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (dbo.ProductUnits.IsActive = 1) AND (dbo.ProductMarks.MarkId IS NULL)

GO


 
/****** Object:  View [dbo].[ProductListDetails5]    Script Date: 19.9.2016 17:47:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[ProductListDetails5]
AS
SELECT        dbo.Categories.CatId, dbo.Categories.CatName, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.Products.StockQty, dbo.ProductKdvs.KdvRate, dbo.ProductImages.Path, dbo.ProductMarks.MarKName, 
                         dbo.ProductUnits.BirimAdi, dbo.ProductUnits.Carpan, dbo.ProductUnits.SatisFiyati0, dbo.ProductUnits.SatisFiyati1, dbo.ProductUnits.SatisFiyati2, dbo.ProductUnits.SatisFiyati3, dbo.ProductUnits.Isk1, 
                         dbo.ProductUnits.Isk2, dbo.ProductUnits.Isk3, Currencys_0.CurrencyName AS Currency0, Currencys_1.CurrencyName AS Currency1, Currencys_2.CurrencyName AS Currency2, 
                         Currencys_3.CurrencyName AS Currency3, Currencys_0.Rate AS Rate0, Currencys_1.Rate AS Rate1, Currencys_2.Rate AS Rate2, Currencys_3.Rate AS Rate3, dbo.Products.SellNoneStock, 
                         dbo.Products.ShowInMainPage, dbo.Categories.PortalId, dbo.ProductMarks.MarkId, dbo.Products.ShowOrder, dbo.Products.Code14, dbo.Products.Code15, dbo.Categories.Url AS CatUrl, dbo.Categories.Lineage, 
                         dbo.ProductUnits.UnitId, dbo.ProductUnits.IsDefault AS UnitsIsDefault, dbo.Products.WizardId, dbo.Products.CampainId, dbo.Products.ForceCargo, dbo.ProductUnits.Desi, dbo.Products.StartDate, 
                         dbo.Products.EndDate, dbo.Products.Code10, dbo.Products.Code11, dbo.Products.Code12, dbo.Products.Code13, dbo.Publishers.PublisherId, dbo.Publishers.PublisherName, dbo.Authors.AuthorId, 
                         dbo.Authors.AuthorName, Currencys_4.CurrencyName AS Currency4, Currencys_4.Rate AS Rate4, Currencys_5.CurrencyName AS Currency5, Currencys_5.Rate AS Rate5, dbo.ProductUnits.SatisFiyati4, 
                         dbo.ProductUnits.SatisFiyati5, dbo.ProductUnits.Isk4, dbo.ProductUnits.Isk5, Currencys_P.CurrencyName AS CurrencyP, Currencys_P.Rate AS RateP, dbo.ProductUnits.PiyasaFiyati, dbo.Products.HizliKargo, 
                         dbo.Products.AyniGunTeslim, dbo.Products.IndirimliUrun, dbo.Products.FirsatUrunu, dbo.Products.SokFiyatliUrun, dbo.Products.EnUcuzUrun, dbo.Products.OzelUrun, dbo.Products.SinirliSayidaUrun, 
                         dbo.Products.YeniUrun, dbo.Products.HediyeliUrun, dbo.Categories.StartDate AS CStartDate, dbo.Categories.EndDate AS CEndDate, dbo.Products.ProductSizeOptionId, dbo.ProductUnits.AlisFiyati, 
                         Currencys_A.AlisRate, dbo.Products.Code1, dbo.Products.Code2, dbo.Products.Code3, dbo.Products.Code4, dbo.ProductDescription.ProductName, dbo.ProductDescription.Description, 
                         dbo.ProductDescription.Details, dbo.ProductDescription.Url, dbo.ProductDescription.LanguageCode, dbo.Products.Code5, dbo.Products.Code6, dbo.Products.Code7, dbo.Products.Code8, 
                         dbo.Products.Code9
FROM            dbo.ProductCategories INNER JOIN
                         dbo.Categories ON dbo.ProductCategories.CategoryId = dbo.Categories.CatId INNER JOIN
                         dbo.ProductUnits INNER JOIN
                         dbo.Products ON dbo.ProductUnits.ProductId = dbo.Products.ProductId ON dbo.ProductCategories.ProductId = dbo.Products.ProductId INNER JOIN
                         dbo.ProductKdvs ON dbo.Products.KdvId = dbo.ProductKdvs.KdvId INNER JOIN
                         dbo.ProductImages ON dbo.Products.ProductId = dbo.ProductImages.ProductId INNER JOIN
                         dbo.ProductDescription ON dbo.ProductCategories.ProductId = dbo.ProductDescription.ProductId INNER JOIN
                         dbo.Language ON dbo.ProductDescription.LanguageId = dbo.Language.LanguageId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_P ON dbo.ProductUnits.PPb = Currencys_P.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_5 ON dbo.ProductUnits.Pb5 = Currencys_5.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_4 ON dbo.ProductUnits.Pb4 = Currencys_4.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_A ON dbo.ProductUnits.APb = Currencys_A.CurrencyId LEFT OUTER JOIN
                         dbo.Publishers ON dbo.Products.PublisherId = dbo.Publishers.PublisherId LEFT OUTER JOIN
                         dbo.Authors ON dbo.Products.AuthorId = dbo.Authors.AuthorId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_0 ON dbo.ProductUnits.Pb0 = Currencys_0.CurrencyId LEFT OUTER JOIN
                         dbo.ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_2 ON dbo.ProductUnits.Pb2 = Currencys_2.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_1 ON dbo.ProductUnits.Pb1 = Currencys_1.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_3 ON dbo.ProductUnits.Pb3 = Currencys_3.CurrencyId
WHERE        (dbo.Categories.IsActive = 1) AND (dbo.ProductMarks.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND 
                         (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) AND (ISNULL(dbo.Categories.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Categories.EndDate, DATEADD(day, 1, 
                         GETDATE())) >= GETDATE()) AND (ISNULL(dbo.Products.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Products.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND 
                         (dbo.ProductUnits.IsActive = 1) AND (ISNULL(dbo.Categories.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Categories.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) AND 
                         (ISNULL(dbo.Products.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Products.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) AND (dbo.ProductMarks.MarkId IS NULL) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductMarks.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND 
                         (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) AND (ISNULL(dbo.Categories.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Categories.EndDate, DATEADD(day, 1, 
                         GETDATE())) >= GETDATE()) AND (ISNULL(dbo.Products.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Products.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND 
                         (dbo.ProductUnits.IsActive = 1) AND (ISNULL(dbo.Categories.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Categories.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) AND 
                         (ISNULL(dbo.Products.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Products.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) AND (dbo.ProductMarks.MarkId IS NULL) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductMarks.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND 
                         (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) AND (ISNULL(dbo.Categories.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Categories.EndDate, DATEADD(day, 1, 
                         GETDATE())) >= GETDATE()) AND (ISNULL(dbo.Products.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Products.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND 
                         (dbo.ProductUnits.IsActive = 1) AND (ISNULL(dbo.Categories.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Categories.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) AND 
                         (ISNULL(dbo.Products.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Products.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) AND (dbo.ProductMarks.MarkId IS NULL) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductMarks.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND 
                         (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) AND (ISNULL(dbo.Categories.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Categories.EndDate, DATEADD(day, 1, 
                         GETDATE())) >= GETDATE()) AND (ISNULL(dbo.Products.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Products.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND 
                         (dbo.ProductUnits.IsActive = 1) AND (ISNULL(dbo.Categories.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Categories.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) AND 
                         (ISNULL(dbo.Products.StartDate, DATEADD(day, - 1, GETDATE())) <= GETDATE()) AND (ISNULL(dbo.Products.EndDate, DATEADD(day, 1, GETDATE())) >= GETDATE()) AND (dbo.ProductMarks.MarkId IS NULL)

GO


 
/****** Object:  View [dbo].[ProductListDetails5a]    Script Date: 19.9.2016 17:48:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[ProductListDetails5a]
AS
SELECT        dbo.Categories.CatId, dbo.Categories.CatName, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.Products.ProductName, dbo.Products.Description, dbo.Products.StockQty, dbo.ProductKdvs.KdvRate, 
                         dbo.ProductImages.Path, dbo.ProductMarks.MarKName, dbo.ProductUnits.BirimAdi, dbo.ProductUnits.Carpan, dbo.ProductUnits.SatisFiyati0, dbo.ProductUnits.SatisFiyati1, dbo.ProductUnits.SatisFiyati2, 
                         dbo.ProductUnits.SatisFiyati3, dbo.ProductUnits.Isk1, dbo.ProductUnits.Isk2, dbo.ProductUnits.Isk3, Currencys_0.CurrencyName AS Currency0, Currencys_1.CurrencyName AS Currency1, 
                         Currencys_2.CurrencyName AS Currency2, Currencys_3.CurrencyName AS Currency3, Currencys_0.Rate AS Rate0, Currencys_1.Rate AS Rate1, Currencys_2.Rate AS Rate2, Currencys_3.Rate AS Rate3, 
                         dbo.Products.SellNoneStock, dbo.Products.ShowInMainPage, dbo.Categories.PortalId, dbo.ProductMarks.MarkId, dbo.Products.ShowOrder, dbo.Products.Details, dbo.Products.Code14, dbo.Products.Code15, 
                         dbo.Categories.Url AS CatUrl, dbo.Products.Url AS ProductUrl, dbo.Categories.Lineage, dbo.ProductUnits.UnitId, dbo.ProductUnits.IsDefault AS UnitsIsDefault, dbo.Products.WizardId, dbo.Products.CampainId, 
                         dbo.Products.ForceCargo, dbo.ProductUnits.Desi, dbo.Products.Code10, dbo.Products.Code11, dbo.Products.Code12, dbo.Products.Code13, dbo.Publishers.PublisherId, dbo.Publishers.PublisherName, 
                         dbo.Authors.AuthorId, dbo.Authors.AuthorName, Currencys_4.CurrencyName AS Currency4, Currencys_4.Rate AS Rate4, Currencys_5.CurrencyName AS Currency5, Currencys_5.Rate AS Rate5, 
                         dbo.ProductUnits.SatisFiyati4, dbo.ProductUnits.SatisFiyati5, dbo.ProductUnits.Isk4, dbo.ProductUnits.Isk5, Currencys_P.CurrencyName AS CurrencyP, Currencys_P.Rate AS RateP, dbo.ProductUnits.PiyasaFiyati,
                          dbo.Products.EndDate, dbo.Products.StartDate, dbo.Categories.IsActive AS CatIsActive, dbo.ProductMarks.IsActive AS MarkIsActive, dbo.Products.IsActive AS ProductIsActive, dbo.Products.HediyeliUrun, 
                         dbo.Products.YeniUrun, dbo.Products.SinirliSayidaUrun, dbo.Products.OzelUrun, dbo.Products.EnUcuzUrun, dbo.Products.SokFiyatliUrun, dbo.Products.FirsatUrunu, dbo.Products.IndirimliUrun, 
                         dbo.Products.AyniGunTeslim, dbo.Products.HizliKargo, dbo.Products.PublisherId AS Expr1, dbo.Products.AuthorId AS Expr2, dbo.ProductUnits.AlisFiyati, Currencys_A.AlisRate, dbo.Products.Code1, 
                         dbo.Products.Code2, dbo.Products.Code3, dbo.Products.Code4, dbo.Products.Code5, dbo.Products.Code6, dbo.Products.Code7, dbo.Products.Code8, dbo.Products.Code9
FROM            dbo.ProductCategories INNER JOIN
                         dbo.Categories ON dbo.ProductCategories.CategoryId = dbo.Categories.CatId INNER JOIN
                         dbo.ProductUnits INNER JOIN
                         dbo.Products ON dbo.ProductUnits.ProductId = dbo.Products.ProductId ON dbo.ProductCategories.ProductId = dbo.Products.ProductId INNER JOIN
                         dbo.ProductKdvs ON dbo.Products.KdvId = dbo.ProductKdvs.KdvId INNER JOIN
                         dbo.ProductImages ON dbo.Products.ProductId = dbo.ProductImages.ProductId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_P ON dbo.ProductUnits.PPb = Currencys_P.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_5 ON dbo.ProductUnits.Pb5 = Currencys_5.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_4 ON dbo.ProductUnits.Pb4 = Currencys_4.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_A ON dbo.ProductUnits.APb = Currencys_A.CurrencyId LEFT OUTER JOIN
                         dbo.Publishers ON dbo.Products.PublisherId = dbo.Publishers.PublisherId LEFT OUTER JOIN
                         dbo.Authors ON dbo.Products.AuthorId = dbo.Authors.AuthorId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_0 ON dbo.ProductUnits.Pb0 = Currencys_0.CurrencyId LEFT OUTER JOIN
                         dbo.ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_2 ON dbo.ProductUnits.Pb2 = Currencys_2.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_1 ON dbo.ProductUnits.Pb1 = Currencys_1.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_3 ON dbo.ProductUnits.Pb3 = Currencys_3.CurrencyId
WHERE        (dbo.ProductImages.IsDefault = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) OR
                         (dbo.ProductImages.IsDefault = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) AND (dbo.ProductMarks.MarkId IS NULL) OR
                         (dbo.ProductImages.IsDefault = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) OR
                         (dbo.ProductImages.IsDefault = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) AND (dbo.ProductMarks.MarkId IS NULL) OR
                         (dbo.ProductImages.IsDefault = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) OR
                         (dbo.ProductImages.IsDefault = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) AND (dbo.ProductMarks.MarkId IS NULL) OR
                         (dbo.ProductImages.IsDefault = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) OR
                         (dbo.ProductImages.IsDefault = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND (dbo.ProductUnits.IsActive = 1) AND (dbo.ProductMarks.MarkId IS NULL)

GO


 
/****** Object:  View [dbo].[ProductListLigth]    Script Date: 19.9.2016 17:48:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



ALTER VIEW [dbo].[ProductListLigth]
AS
SELECT        dbo.Categories.CatId, dbo.Categories.CatName, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.Products.ProductName, dbo.ProductImages.Path, dbo.ProductMarks.MarKName, 
                         dbo.ProductUnits.SatisFiyati1, Currencys_1.CurrencyName AS Currency1, Currencys_1.Rate AS Rate1, dbo.Categories.PortalId, dbo.ProductMarks.MarkId, dbo.Categories.Url AS CatUrl, 
                         dbo.Products.Url AS ProductUrl, dbo.Categories.Lineage, dbo.Products.SearchKeywords, dbo.Products.StockQty, dbo.Products.Hit, dbo.ProductKdvs.KdvRate, dbo.Products.HizliKargo, 
                         dbo.Products.AyniGunTeslim, dbo.Products.IndirimliUrun, dbo.Products.FirsatUrunu, dbo.Products.SokFiyatliUrun, dbo.Products.EnUcuzUrun, dbo.Products.OzelUrun, dbo.Products.SinirliSayidaUrun, 
                         dbo.Products.YeniUrun, dbo.Products.HediyeliUrun, dbo.ProductMarks.Image AS MarkImage, dbo.Products.CompanyId,dbo.Products.Code1
FROM            dbo.ProductCategories INNER JOIN
                         dbo.Categories ON dbo.ProductCategories.CategoryId = dbo.Categories.CatId INNER JOIN
                         dbo.ProductUnits INNER JOIN
                         dbo.Products ON dbo.ProductUnits.ProductId = dbo.Products.ProductId ON dbo.ProductCategories.ProductId = dbo.Products.ProductId INNER JOIN
                         dbo.ProductImages ON dbo.Products.ProductId = dbo.ProductImages.ProductId INNER JOIN
                         dbo.ProductKdvs ON dbo.Products.KdvId = dbo.ProductKdvs.KdvId LEFT OUTER JOIN
                         dbo.ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_1 ON dbo.ProductUnits.Pb1 = Currencys_1.CurrencyId
WHERE        (dbo.Categories.IsActive = 1) AND (dbo.ProductMarks.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) OR
          (dbo.Categories.IsActive = 1) AND (dbo.ProductImages.IsDefault = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1) AND (NOT (dbo.ProductImages.Path = N'')) AND (Currencys_1.CurrencyId > 0) AND (dbo.ProductMarks.MarkId IS NULL)  



GO


 
/****** Object:  View [dbo].[ProductsExportAllOptions]    Script Date: 19.9.2016 17:49:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




ALTER VIEW [dbo].[ProductsExportAllOptions]
AS
SELECT        TOP (100) PERCENT P.IsActive AS UrunAktif, P.ProductCode AS StokKodu, P.ProductName AS StokAdi, P.Description AS StokAciklama, P.Details, P.CompanyId,
                         REPLACE(P.StockQty, '.', ',') AS StokEnvanter, ISNULL
                             ((SELECT        TOP 1 Path
                                 FROM            (SELECT        ROW_NUMBER() OVER (ORDER BY ProductId ASC) AS rownumber, Path, ProductId
                                 FROM            ProductImages
                                 WHERE        (ProductId = P.ProductId)) AS Images
WHERE        rownumber = 1), '') AS StokResmi, ISNULL
    ((SELECT        TOP 1 Path
        FROM            (SELECT        ROW_NUMBER() OVER (ORDER BY ProductId ASC) AS rownumber, Path, ProductId
        FROM            ProductImages
        WHERE        (ProductId = P.ProductId)) AS Images
WHERE        rownumber = 2), '') AS StokResmi2, ISNULL
    ((SELECT        TOP 1 Path
        FROM            (SELECT        ROW_NUMBER() OVER (ORDER BY ProductId ASC) AS rownumber, Path, ProductId
        FROM            ProductImages
        WHERE        (ProductId = P.ProductId)) AS Images
WHERE        rownumber = 3), '') AS StokResmi3, ISNULL
    ((SELECT        TOP 1 Path
        FROM            (SELECT        ROW_NUMBER() OVER (ORDER BY ProductId ASC) AS rownumber, Path, ProductId
        FROM            ProductImages
        WHERE        (ProductId = P.ProductId)) AS Images
WHERE        rownumber = 4), '') AS StokResmi4, ISNULL
    ((SELECT        TOP 1 Path
        FROM            (SELECT        ROW_NUMBER() OVER (ORDER BY ProductId ASC) AS rownumber, Path, ProductId
        FROM            ProductImages
        WHERE        (ProductId = P.ProductId)) AS Images
WHERE        rownumber = 5), '') AS StokResmi5, ISNULL
    ((SELECT        TOP 1 Path
        FROM            (SELECT        ROW_NUMBER() OVER (ORDER BY ProductId ASC) AS rownumber, Path, ProductId
        FROM            ProductImages
        WHERE        (ProductId = P.ProductId)) AS Images
WHERE        rownumber = 6), '') AS StokResmi6, ISNULL
    ((SELECT        TOP 1 Path
        FROM            (SELECT        ROW_NUMBER() OVER (ORDER BY ProductId ASC) AS rownumber, Path, ProductId
        FROM            ProductImages
        WHERE        (ProductId = P.ProductId)) AS Images
WHERE        rownumber = 7), '') AS StokResmi7, ISNULL
    ((SELECT        TOP 1 Path
        FROM            (SELECT        ROW_NUMBER() OVER (ORDER BY ProductId ASC) AS rownumber, Path, ProductId
        FROM            ProductImages
        WHERE        (ProductId = P.ProductId)) AS Images
WHERE        rownumber = 8), '') AS StokResmi8, ISNULL(dbo.ProductMarks.MarKName, N'') AS MarkaAdi, REPLACE(dbo.ProductKdvs.KdvRate, '.', ',') AS KdvOrani, 
dbo.ProductUnits.BirimAdi, REPLACE(dbo.ProductUnits.Carpan, '.', ',') AS BirimCarpan, REPLACE(dbo.ProductUnits.SatisFiyati0, '.', ',') AS SatisFiyati0, 
Currencys_0.CurrencyName AS SatisFiyati0ParaBirimi, REPLACE(dbo.ProductUnits.SatisFiyati1, '.', ',') AS SatisFiyati1, dbo.ProductUnits.Isk1, 
Currencys_1.CurrencyName AS SatisFiyati1ParaBirimi, REPLACE(dbo.ProductUnits.SatisFiyati2, '.', ',') AS SatisFiyati2, dbo.ProductUnits.Isk2, 
Currencys_2.CurrencyName AS SatisFiyati2ParaBirimi, REPLACE(dbo.ProductUnits.SatisFiyati3, '.', ',') AS SatisFiyati3, dbo.ProductUnits.Isk3, 
Currencys_3.CurrencyName AS SatisFiyati3ParaBirimi, REPLACE(dbo.ProductUnits.AlisFiyati, '.', ',') AS AlisFiyati, Currencys_A.CurrencyName AS AlisFiyatiParaBirimi, 
REPLACE(dbo.ProductUnits.En, '.', ',') AS En, REPLACE(dbo.ProductUnits.Boy, '.', ',') AS Boy, REPLACE(dbo.ProductUnits.Yukseklik, '.', ',') AS Yukseklik, 
REPLACE(dbo.ProductUnits.Agirlik, '.', ',') AS Agirlik, REPLACE(dbo.ProductUnits.Desi, '.', ',') AS Desi, P.PortalId, P.SellNoneStock, P.Code1 AS Kod1, P.Code2 AS Kod2, 
P.Code3 AS Kod3, P.Code4 AS Kod4, P.Code5 AS Kod5, P.Code6 AS Kod6, P.Code7 AS Kod7, P.Code8 AS Kod8, P.Code9 AS Kod9, P.Code10 AS Kod10, P.Code11 AS Kod11, 
P.Code12 AS Kod12, P.Code13 AS Kod13, P.Code14 AS Kod14, P.Code15 AS Kod15, P.ProductId AS UrunId, P.CreatedDate, P.ProviderStockCode AS UreticiKodu, 
dbo.Categories.Lineage, P.Url, dbo.Categories.Lineage2, P.SearchKeywords, dbo.ProductMarks.MarkId, P.IsPenta AS XmlTedarikciId, REPLACE(dbo.ProductUnits.PiyasaFiyati, 
'.', ',') AS PiyasaFiyati, Currencys_P.CurrencyName AS PiyasaFiyatiParaBirimi, Currencys_4.CurrencyName AS SatisFiyati4ParaBirimi, REPLACE(dbo.ProductUnits.SatisFiyati4, 
'.', ',') AS SatisFiyati4, dbo.ProductUnits.Isk4, Currencys_5.CurrencyName AS SatisFiyati5ParaBirimi, REPLACE(dbo.ProductUnits.SatisFiyati5, '.', ',') AS SatisFiyati5, 
dbo.ProductUnits.Isk5, P.HizliKargo, P.AyniGunTeslim, P.IndirimliUrun, P.FirsatUrunu, P.SokFiyatliUrun, P.EnUcuzUrun, P.OzelUrun, P.SinirliSayidaUrun, P.YeniUrun, 
P.HediyeliUrun, dbo.Providers.ProviderName AS TedarikciAdi, Providers_1.ProviderName AS XmlTedarikciAdi, dbo.Categories.CatId AS CatId1, dbo.Categories.Depth, 
P.ProductSizeOptionId, dbo.ProductUnits.Barcode  , ProductDescription.LanguageCode
FROM            dbo.ProductKdvs RIGHT OUTER JOIN
                         dbo.Providers RIGHT OUTER JOIN
                         dbo.Providers AS Providers_1 RIGHT OUTER JOIN
                         dbo.Products AS P ON Providers_1.ProviderId = P.IsPenta ON dbo.Providers.ProviderId = P.ProviderId LEFT OUTER JOIN
						 dbo.ProductDescription ON P.ProductId = ProductDescription.ProductId LEFT OUTER JOIN
                         dbo.Categories RIGHT OUTER JOIN
                         dbo.ProductCategories ON dbo.Categories.CatId = dbo.ProductCategories.CategoryId ON P.ProductId = dbo.ProductCategories.ProductId ON 
                         dbo.ProductKdvs.KdvId = P.KdvId LEFT OUTER JOIN
                         dbo.ProductMarks ON P.MarkId = dbo.ProductMarks.MarkId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_0 RIGHT OUTER JOIN
                         dbo.Currencys AS Currencys_4 INNER JOIN
                         dbo.ProductUnits ON Currencys_4.CurrencyId = dbo.ProductUnits.Pb4 INNER JOIN
                         dbo.Currencys AS Currencys_5 ON dbo.ProductUnits.Pb5 = Currencys_5.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_P ON dbo.ProductUnits.PPb = Currencys_P.CurrencyId ON Currencys_0.CurrencyId = dbo.ProductUnits.Pb0 LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_3 ON dbo.ProductUnits.Pb3 = Currencys_3.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_2 ON dbo.ProductUnits.Pb2 = Currencys_2.CurrencyId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_A ON dbo.ProductUnits.APb = Currencys_A.CurrencyId ON P.ProductId = dbo.ProductUnits.ProductId LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_1 ON dbo.ProductUnits.Pb1 = Currencys_1.CurrencyId
WHERE        (NOT (P.Url IS NULL)) AND (NOT (dbo.ProductUnits.UnitId IS NULL))




GO

 

/****** Object:  View [dbo].[ProductSizeList]    Script Date: 19.9.2016 17:49:50 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER VIEW [dbo].[ProductSizeList]
AS
SELECT        dbo.ProductSizes.ProductSizeId, dbo.ProductSizes.ProductId, dbo.ProductSizes.ProductSizeOptionId, dbo.ProductSizeOptions.ProductSizeOptionName, 
                         dbo.ProductSizeOptionsValues.ProductSizeOptionsValueName AS Size0Name, dbo.ProductSizeOptionsValues.ProductSizeOptionsPath AS Size0Path, ISNULL(dbo.ProductImages.ImageId, 0) AS ProductImageId, 
                         ProductSizeOptionsValues_1.ProductSizeOptionsValueName AS Size1Name, ProductSizeOptionsValues_2.ProductSizeOptionsValueName AS Size2Name, dbo.ProductSizes.StockQty, dbo.ProductSizes.Barcode, 
                         dbo.ProductImages.Path AS ProductImagePath, dbo.ProductSizeOptions.ProductSize0Active, dbo.ProductSizeOptions.ProductSize1Active, dbo.ProductSizeOptions.ProductSize2Active, dbo.Products.ProductCode, 
                         dbo.ProductSizeOptions.ProductSize0Name, dbo.ProductSizeOptions.ProductSize1Name, dbo.ProductSizeOptions.ProductSize2Name, dbo.ProductSizes.ProductSize0ValueId, 
                         dbo.ProductSizes.ProductSize1ValueId, dbo.ProductSizes.ProductSize2ValueId, dbo.ProductSizes.StockQty2, dbo.ProductSizes.StockChange, dbo.ProductSizes.StockCheck, dbo.ProductSizes.StokEndDate, 
                         ProductSizeOptionsValues_1.ProductSizeOptionShowOrder AS ShowOrder, dbo.ProductSizes.AlternativeMarketsVariantId, dbo.ProductSizeOptionsValues.NebimRenkKodu AS ProductSize0Code, 
                         ProductSizeOptionsValues_1.NebimRenkKodu AS ProductSize1Code, ProductSizeOptionsValues_2.NebimRenkKodu AS ProductSize2Code
FROM            dbo.ProductSizes INNER JOIN
                         dbo.ProductSizeOptions ON dbo.ProductSizes.ProductSizeOptionId = dbo.ProductSizeOptions.ProductSizeOptionId INNER JOIN
                         dbo.ProductSizeOptionsValues ON dbo.ProductSizes.ProductSize0ValueId = dbo.ProductSizeOptionsValues.ProductSizeOptionsValueId INNER JOIN
                         dbo.Products ON dbo.ProductSizes.ProductId = dbo.Products.ProductId LEFT OUTER JOIN
                         dbo.ProductSizeOptionsValues AS ProductSizeOptionsValues_1 ON dbo.ProductSizes.ProductSize1ValueId = ProductSizeOptionsValues_1.ProductSizeOptionsValueId LEFT OUTER JOIN
                         dbo.ProductSizeOptionsValues AS ProductSizeOptionsValues_2 ON dbo.ProductSizes.ProductSize2ValueId = ProductSizeOptionsValues_2.ProductSizeOptionsValueId LEFT OUTER JOIN
                         dbo.ProductImages ON dbo.ProductSizes.ProductImageId = dbo.ProductImages.ImageId


GO

 
/****** Object:  StoredProcedure [dbo].[AddTimeCostWork]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AddTimeCostWork]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [dbo].[AddTimeCostWork]
( 
@StartDate smalldatetime,
@AssignedUserId   int,
@UserId   int,
@ProductId int,
@ProjectId int,
@JobID int,
@Description nvarchar(500),
@CreatedUserId int,
@UpdatedUserId int,
@Time decimal(18,2),
@TimeCostId int,
@DailyWorkingHours decimal(18,2),
@IND      int OUTPUT
)
AS
IF NOT EXISTS ( Select * from TimeCostWork where TimeCostId=@TimeCostId and CONVERT(DATE,StartDate)=CONVERT(DATE,@StartDate) And AssignedUserId=@AssignedUserId)
BEGIN
If @Time <= @DailyWorkingHours
BEGIN
INSERT Into TimeCostWork(StartDate,AssignedUserId,UserId,ProductId,ProjectId,
JobID,Description,UpdatedDate,CreatedUserId,UpdatedUserId,Time)
VALUES(@StartDate,@AssignedUserId,@UserId,@ProductId,@ProjectId,
@JobID,@Description,GETDATE(),@CreatedUserId,@UpdatedUserId,@Time) SELECT
    @IND = @@Identity
End
ELSE Begin Select @IND -1 End 
END

ELSE
BEGIN

If @TimeCostId>0
BEGIN

If Exists( Select TimeCostId From TimeCostWork Where AssignedUserId=@AssignedUserId And CONVERT(DATE,StartDate)=CONVERT(DATE,@StartDate) And
(Select ISNULL(SUM(Time),0) + @Time From TimeCostWork Where AssignedUserId=@AssignedUserId And CONVERT(DATE,StartDate)=CONVERT(DATE,@StartDate) And TimeCostId<>@TimeCostId) <= @DailyWorkingHours)
Begin Update TimeCostWork Set StartDate=@StartDate,UserId=@UserId,
ProductId=@ProductId,ProjectId=@ProjectId,
JobID=@JobID,Description=@Description,UpdatedDate=GETDATE(),UpdatedUserId=@UpdatedUserId,Time=@Time
Where TimeCostId=@TimeCostId Select @IND=@TimeCostId End
Else Begin Select @IND=-1 End
END
ELSE
If Exists( Select TimeCostId From TimeCostWork Where AssignedUserId=@AssignedUserId And CONVERT(DATE,StartDate)=CONVERT(DATE,GETDATE()) And
(Select ISNULL(SUM(Time),0) + @Time From TimeCostWork Where AssignedUserId=@AssignedUserId And CONVERT(DATE,StartDate)=CONVERT(DATE,GETDATE())) <= @DailyWorkingHours)
Begin INSERT Into TimeCostWork(StartDate,AssignedUserId,UserId,ProductId,ProjectId,
JobID,Description,UpdatedDate,CreatedUserId,UpdatedUserId,Time)
VALUES(@StartDate,@AssignedUserId,@UserId,@ProductId,@ProjectId,
@JobID,@Description,GETDATE(),@CreatedUserId,@UpdatedUserId,@Time) End
Else Begin Select @IND=-1 End

END

' 
END
GO
/****** Object:  StoredProcedure [dbo].[AddUpdateBrand]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AddUpdateBrand]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[AddUpdateBrand]
(
 
    @PortalId int,
    @MarKName   nvarchar(50),
    @Image   nvarchar(50),
    @Details   varchar(4000),
    @Details2   varchar(4000),
    @IsActive bit=1,
    @Title  nvarchar(100),
    @Desc  nvarchar(255),
    @Keywords  nvarchar(255),
    @SearchKeywords  varchar(1000),
    @MarkId      int OUTPUT
)
AS
IF NOT EXISTS ( select * from ProductMarks where MarKName=@MarKName and PortalId=@PortalId )
BEGIN
INSERT INTO ProductMarks
(

    MarKName,Image,PortalId,Details,Details2,IsActive,Title,[Desc],Keywords
)

VALUES
(
    @MarKName,@Image,@PortalId,@Details,@Details2,@IsActive,@Title,@Desc,@Keywords
)
SELECT
    @MarkId = @@Identity
END
ELSE
BEGIN 
   Update ProductMarks set MarKName=@MarKName,Image=@Image,PortalId=@PortalId,Details=@Details,Details2=@Details2,IsActive=@IsActive,Title=@Title,[Desc]=@Desc,Keywords=@Keywords
   where MarkId=(select top 1 MarkId from ProductMarks where MarKName=@MarKName and PortalId=@PortalId )
   
   select  top 1 @MarkId=MarkId from ProductMarks where MarKName=@MarKName and PortalId=@PortalId 

END
' 
END
GO
/****** Object:  StoredProcedure [dbo].[AddUpdateCargo]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AddUpdateCargo]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[AddUpdateCargo]
(
 
    @PortalId int,
    @CargoName   nvarchar(50),
    @CargoMail   nvarchar(50),
    @CashOnDelivery bit,
    @IsOnline bit=0,
    @ValueAdd decimal=0,
    @ValueAddKK decimal=0,
    @IsActive bit=1,
    @ShowOrder tinyint=0,
    @ShowDetails bit=0,
    @PriceForCountry bit=0,
    @DefaultPrice decimal=0,
    @CargoId      int OUTPUT
)
AS
IF NOT EXISTS ( select * from Cargo where CargoName=@CargoName and PortalId=@PortalId )
BEGIN
INSERT INTO Cargo
(

    CargoName,CargoMail,PortalId,CashOnDelivery,IsOnline,ValueAdd,IsActive,ShowOrder,ShowDetails,PriceForCountry,DefaultPrice,ValueAddKK
)

VALUES
(
    @CargoName,@CargoMail,@PortalId,@CashOnDelivery,@IsOnline,@ValueAdd,@IsActive,@ShowOrder,@ShowDetails,@PriceForCountry,@DefaultPrice,@ValueAddKK
)
SELECT
    @CargoId = @@Identity
END
ELSE
BEGIN 
   Update Cargo set DefaultPrice=@DefaultPrice,PriceForCountry=@PriceForCountry,ShowDetails=@ShowDetails,ShowOrder=@ShowOrder,
   CargoName=@CargoName,CargoMail=@CargoMail,CashOnDelivery=@CashOnDelivery,IsOnline=@IsOnline,ValueAdd=@ValueAdd,IsActive=@IsActive,ValueAddKK=@ValueAddKK  
   where CargoId=(select CargoId from Cargo where CargoName=@CargoName and PortalId=@PortalId)
   
   select   @CargoId = CargoId from Cargo where CargoName=@CargoName and PortalId=@PortalId

END
' 
END
GO
/****** Object:  StoredProcedure [dbo].[GetCategoryForPrice]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetCategoryForPrice]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE  PROCEDURE [dbo].[GetCategoryForPrice]
(

  @CartId NVARCHAR(50),
  @CatId INT,
  @PortalId INT,
  @LangCode nvarchar(50),
  @Lic BIT
)
AS

DECLARE  @UserFilter NVARCHAR(500) 
DECLARE  @IND  INT
DECLARE  @CatFilter NVARCHAR(500) 
DECLARE  @IsForGuests  BIT
DECLARE  @result NVARCHAR(50)
DECLARE  @result2 NVARCHAR(500)
DECLARE  @parent NVARCHAR(500)=''''
 
DECLARE CRS_USER CURSOR FOR
 
Select UserFilter,IND,CatFilter,IsForGuests from PriceCategories where Status=1 And StartDate<=GETDATE() And EndDate>=GETDATE() and @Lic=1 order by ApplyOrder 
set @result2=''''

OPEN CRS_USER

FETCH NEXT FROM CRS_USER INTO @UserFilter ,@IND,@CatFilter,@IsForGuests

WHILE @@FETCH_STATUS =0
    BEGIN
 
    declare @ExcludedList nvarchar(4000)
	
	IF (@IsForGuests=1 AND ISNUMERIC(@CartId) = 0)
	BEGIN
		set @result2 = @result2 + @CatFilter 
	END
	IF @UserFilter=''''
	BEGIN
		set @UserFilter=''1=1''
	END
	IF ISNUMERIC(@CartId)=1
	BEGIN
		set @ExcludedList=''select  @result =count(UserId) from users where '' + @UserFilter + ''  and UserId='' + @CartId  
		EXEC sp_executesql @ExcludedList, N''@result VARCHAR(100) OUTPUT'' , @result output 
		if @result >0
		begin 
		    set @result2 = @result2 + @CatFilter 
		end
	END

	
	
    FETCH NEXT FROM CRS_USER INTO @UserFilter,@IND,@CatFilter,@IsForGuests

END

CLOSE CRS_USER
DEALLOCATE CRS_USER
  

  set @result2= @result2 + ''0''
  set @parent= ''/'' + CONVERT(nvarchar,@CatId) + ''/%''
  --select @result2
  --select @parent
   
IF @CatId=0

BEGIN
 
	SELECT c.Depth,c.CatImg,c.Parent,d.CatId,d.CatName,d.Url as CatUrl,d.Lineage,d.Description,d.Description2,c.ChildCount,c.ShowMenu1  
	
	FROM Categories as c inner join CategoryDescription as d on c.CatId =d.CatId  
	
	WHERE IsActive=1 and IsVisible=1 and PortalId=@PortalId and (c.depth=0 or c.depth=1) and d.CatId Not IN(select IntValue from  dbo.CsvToInt(@result2)) and d.LanguageCode=@LangCode  ORDER BY c.Lineage,c.Parent,isnull(ShowOrder,100),c.CatName

END

ELSE IF @CatId=-1
BEGIN

	
	SELECT c.Depth,c.CatImg,c.Parent,d.CatId,d.CatName,d.Url as CatUrl,d.Lineage,d.Description,d.Description2,c.ChildCount,c.ShowMenu1  
	
	FROM Categories as c inner join CategoryDescription as d on c.CatId =d.CatId   
	
	WHERE IsActive=1 and (c.depth=0 or c.depth=1) and IsVisible=1 and c.Parent=0 and c.CatId Not IN(select IntValue from  dbo.CsvToInt(@result2))  
	
	ORDER BY c.Lineage,c.Parent, isnull(ShowOrder,100),CatName

END

ELSE  
BEGIN 
 
	SELECT c.Depth,c.CatImg,c.Parent,d.CatId,d.CatName,d.Url as CatUrl,d.Lineage,d.Description,d.Description2,c.ChildCount,c.ShowMenu1  
	
	FROM Categories as c inner join CategoryDescription as d on c.CatId =d.CatId  
	
	WHERE IsActive=1 and IsVisible=1 and PortalId=@PortalId and  c.Lineage like @parent  and d.LanguageCode=@LangCode and d.CatId Not IN(select IntValue from  dbo.CsvToInt(@result2))   
	
	ORDER BY c.Lineage,c.Parent,isnull(ShowOrder,100),c.CatName 

END
' 
END
GO
/****** Object:  StoredProcedure [dbo].[GetCategoryListForPrice]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetCategoryListForPrice]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE  PROCEDURE [dbo].[GetCategoryListForPrice]
(

  @CartId NVARCHAR(50), 
  @PortalId INT,
  @Lic BIT
)
AS

DECLARE  @UserFilter NVARCHAR(500) 
DECLARE  @IND  INT
DECLARE  @CatFilter NVARCHAR(500) 
DECLARE  @IsForGuests  BIT
DECLARE  @result NVARCHAR(50)
DECLARE  @result2 NVARCHAR(500)
 
DECLARE CRS_USER CURSOR FOR
 
Select UserFilter,IND,CatFilter,IsForGuests from PriceCategories where Status=1 And StartDate<=GETDATE() And EndDate>=GETDATE() and @Lic=1 order by ApplyOrder 
set @result2=''''

OPEN CRS_USER

FETCH NEXT FROM CRS_USER INTO @UserFilter ,@IND,@CatFilter,@IsForGuests

WHILE @@FETCH_STATUS =0
    BEGIN
 
    declare @ExcludedList nvarchar(4000)
	
	IF (@IsForGuests=1 AND ISNUMERIC(@CartId) = 0)
	BEGIN
		set @result2 = @result2 + @CatFilter 
	END
	IF @UserFilter=''''
	BEGIN
		set @UserFilter=''1=1''
	END
	IF ISNUMERIC(@CartId)=1
	BEGIN
		set @ExcludedList=''select  @result =count(UserId) from users where '' + @UserFilter + ''  and UserId='' + @CartId  
		EXEC sp_executesql @ExcludedList, N''@result VARCHAR(100) OUTPUT'' , @result output 
		if @result >0
		begin 
		    set @result2 = @result2 + @CatFilter 
		end
	END

	
	
    FETCH NEXT FROM CRS_USER INTO @UserFilter,@IND,@CatFilter,@IsForGuests

END

CLOSE CRS_USER
DEALLOCATE CRS_USER
  

  set @result2= @result2 + ''0''
   
SELECT @result2
' 
END
GO
/****** Object:  StoredProcedure [dbo].[OrdersAddBasket]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OrdersAddBasket]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [dbo].[OrdersAddBasket]
(
    @PortalId					int,
    @CartId					nvarchar(50),
    @OrderDate				 datetime='''',
    @PaymenType				 nvarchar(30),
    @IsPayed				bit,
    @Note					nvarchar(400),
    @ShippingName			nvarchar(100),
    @ShippingPhoneNo		 nvarchar(15),
    @ShippingMobilePhoneNo	 nvarchar(15),
    @ShippingCityName		nvarchar(50),
    @ShippingAddress		 nvarchar(400),
    @ShippingCargoName		 nvarchar(50),
    @BillingName			nvarchar(100),
	@BillingPhoneNo		 nvarchar(15),
    @BillingMobilePhoneNo	 nvarchar(15),
    @BillingTaxOffice		nvarchar(50),
    @BillingTaxNo			nvarchar(20),
    @BillingCityName		nvarchar(50),
    @BillingAddress			nvarchar(400),
    @ToplamTutar			decimal(18,4),
    @KdvDahilToplam			decimal(18,4),
    @ToplamKdv				decimal(18,4),
    @ToplamKargo			decimal(18,4),
    @KargoDahilToplamTutar	decimal(18,4),
    @CekIndirimi	decimal(18,2)=0,
    @ToplamIndirim			decimal(18,2),
    @ToplamAgirlik			decimal(18,2),
    @ToplamHacim			decimal(18,2),
    @ToplamDesi decimal(18,2)=0,
    @ShoppingCartDefId  int,
    @BankName				 nvarchar(50),
    @BankId				 int,
    @CardName	nvarchar(50)='''',
    @Taksit					 int,
    @BankaToplamTutar		  decimal(18,4),
    @BankPayType			 nvarchar(10),
    @StokIndirim int,  
	@StokIskActive bit,
	@Status int =0,
	@ToplamPuan decimal(18,2),
	@CampainId int=0,
	@Ip varchar(16)='''',
	@OrderCode varchar(10)='''',
	@OrderPaymentCode nvarchar(50)='''',
    @CurrencyRate		  decimal(10, 4),
    @CurrencyCode		  nvarchar(10)='''',
    @CustomerCurrencyRate		  decimal(10, 4),
    @CustomerCurrencyCode		  nvarchar(10)='''',
    @TaksitRate		  decimal(10, 2),
	@ShippingCountryName		  nvarchar(50)='''',
    @ShippingDistrictName		  nvarchar(50)='''',
    @BillingCountryName		  nvarchar(50)='''',
    @BillingDistrictName		  nvarchar(50)='''',
	@ShippingMail nvarchar(100),
	@ChiperProId int,
	@ProIskType int,
	@ProIsk decimal(18,4),
    @OrderId				 int OUTPUT
)
AS
set @OrderDate=getdate()
BEGIN TRAN AddOrder

--Create the Order  -
INSERT INTO Orders
(
PortalId		,
CustomerId		,
OrderDate		,
PaymentType		,
IsPayed			,
Note			,	
ShippingName		,	
ShippingPhoneNo		,
ShippingMobilePhone,	
ShippingCityName	,	
ShippingAddress		,
ShippingCargoName	,	
BillingName		,
BillingPhoneNo	,
BillingMobilePhoneNo	,
BillingTaxOffice	,	
BillingTaxNo		,	
BillingCityName		,
BillingAddress		,	
ToplamTutar		,
KdvDahilToplam		,	
ToplamKdv		,	
ToplamKargo		,
KargoDahilToplamTutar	,	
ToplamIndirim		,	
ToplamAgirlik		,	
ToplamHacim		,
ToplamDesi,
BankName		,
BankId			,
CardName,
Taksit			, 
BankaToplamTutar,
BankPayType,
Status,
CekIndirimi,
ToplamPuan,
Ip,
OrderCode,
OrderPaymentCode,
CurrencyRate,
CurrencyCode,
CustomerCurrencyRate,
CustomerCurrencyCode,
TaksitRate,
ShippingCountryName,
ShippingDistrictName,
BillingCountryName,
BillingDistrictName,ShippingMail
 )
VALUES
(   
@PortalId		,
case when (isnumeric(@CartId)=1 and @CartId>0 ) then @CartId else -1 end, 
--@CartId			,
@OrderDate			,
@PaymenType			,
@IsPayed			,
@Note				,
@ShippingName			,
@ShippingPhoneNo		,
@ShippingMobilePhoneNo		,
@ShippingCityName		,
@ShippingAddress		,
@ShippingCargoName		,
@BillingName			,
@BillingPhoneNo		 ,
@BillingMobilePhoneNo	 ,
@BillingTaxOffice		,
@BillingTaxNo			,
@BillingCityName		,
@BillingAddress			,
@ToplamTutar			,
@KdvDahilToplam			,
@ToplamKdv			,
@ToplamKargo			,
@KargoDahilToplamTutar		,
@ToplamIndirim			,
@ToplamAgirlik			,
@ToplamHacim			,
@ToplamDesi,
@BankName			,
@BankId				,
@CardName,
@Taksit				,
@BankaToplamTutar  		,
@BankPayType	,
@Status,
@CekIndirimi,
@ToplamPuan,
@Ip,
@OrderCode,
@OrderPaymentCode,
@CurrencyRate,
@CurrencyCode,
@CustomerCurrencyRate,
@CustomerCurrencyCode,
@TaksitRate,
@ShippingCountryName,
@ShippingDistrictName,
@BillingCountryName,
@BillingDistrictName,@ShippingMail
)

SELECT
    @OrderId = @@Identity    

--Copy items from given shopping cart to OrdersDetail table 
INSERT INTO OrderDetails
(
OrderId,
ProductId,
ProductCode,
ProductName,
BirimAdi,
Carpan,
SatisFiyati,
Pb1,
Rate,
BirimIndirimOrani,
CariIndirimOrani,
AlisFiyati,
APb,
AlisRate,
Quantity,
Provider,
KdvRate,
MarkName,
Status,
Description,
ProviderId,
XmlProviderId,
UserId,
AltinGram,
ProductSizeId,
ProductBarcode,
ProductSizeBarcode,
StorageId
)
SELECT    @OrderId, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.Products.ProductName, dbo.ProductUnits.BirimAdi, dbo.ProductUnits.Carpan,
''SatisFiyati''= case products.ProductId when @ChiperProId then dbo.CalcYuzdeIndirim(case (@StokIndirim) when 0 then SatisFiyati1+ShoppingCart.ValueAdd when 1 then SatisFiyati1+ShoppingCart.ValueAdd when 2 then SatisFiyati2+ShoppingCart.ValueAdd when 3 then SatisFiyati3+ShoppingCart.ValueAdd when 4 then SatisFiyati4+ShoppingCart.ValueAdd when 5 then SatisFiyati5+ShoppingCart.ValueAdd end,@ProIsk) else
 case (@StokIndirim) when 0 then SatisFiyati1+ShoppingCart.ValueAdd when 1 then SatisFiyati1+ShoppingCart.ValueAdd when 2 then SatisFiyati2+ShoppingCart.ValueAdd when 3 then SatisFiyati3+ShoppingCart.ValueAdd when 4 then SatisFiyati4+ShoppingCart.ValueAdd when 5 then SatisFiyati5+ShoppingCart.ValueAdd end end, 
 ''Pb1''= case (@StokIndirim) when 0 then ProductUnits.Pb1 when 1 then ProductUnits.Pb1 when 2 then ProductUnits.Pb2 when 3 then ProductUnits.Pb3 when 4 then ProductUnits.Pb4 when 5 then ProductUnits.Pb5 end, 
                      Currencys_2.Rate,   
 case when (@StokIskActive >0 ) then case when (@StokIndirim=0) then 0 when (@StokIndirim=1) then Isk1 when (@StokIndirim=2) then Isk2 when (@StokIndirim=3) then Isk3 when (@StokIndirim=4) then Isk4 when (@StokIndirim=5) then Isk5 end else 0 end as Indirim ,  
 case when (isnumeric(@CartId)=1) then (select DisAccount from Users where UserId=@CartId) else 0 end, dbo.ProductUnits.AlisFiyati, dbo.ProductUnits.APb, Currencys_1.Rate AS Rate, 
dbo.ShoppingCart.Qty, dbo.Products.Provider, dbo.ProductKdvs.KdvRate,ProductMarks.MarKName,@Status,dbo.ShoppingCart.Description,Products.IsPenta,Products.ProviderId,Products.UserId,isnull(SatisFiyati5,0),ShoppingCart.ProductSizeId,
ProductUnits.Barcode,
Case When (ShoppingCart.ProductSizeId>0) Then (Select Barcode From ProductSizes where ProductSizeId=ShoppingCart.ProductSizeId) else '''' end as ProductSizeBarcode,StorageId 


FROM         dbo.Currencys Currencys_2 INNER JOIN
                      dbo.ProductUnits ON Currencys_2.CurrencyId = case (@StokIndirim) when 0 then ProductUnits.Pb1 when 1 then ProductUnits.Pb1 when 2 then ProductUnits.Pb2 when 3 then ProductUnits.Pb3 when 4 then ProductUnits.Pb4 when 5 then ProductUnits.Pb5 end INNER JOIN
                      dbo.Products INNER JOIN
     dbo.ProductKdvs ON dbo.Products.KdvId = dbo.ProductKdvs.KdvId INNER JOIN
                      dbo.ShoppingCart ON dbo.Products.ProductId = dbo.ShoppingCart.ProdId ON dbo.ProductUnits.UnitId = dbo.ShoppingCart.PriceId INNER JOIN
                      dbo.Currencys Currencys_1 ON dbo.ProductUnits.APb = Currencys_1.CurrencyId
                      LEFT OUTER JOIN
                      FCampains ON Products.CampainId = FCampains.FCampainId LEFT OUTER JOIN
                      dbo.ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId
where ShoppingCart.CartId = @CartId and  (dbo.Products.IsActive = 1) AND 
(dbo.Products.IsDeleted = 0) AND 
(dbo.Products.StockQty > 0 or Products.SellNoneStock=1) AND 
(dbo.ProductUnits.IsActive = 1) AND (ShoppingCartDefId =@ShoppingCartDefId) --and isnull(Products.CampainId,0)=@CampainId
AND ((ISNULL(FCampains.FCampainId, 0) = @CampainId) or (FCampains.IsActive=0) or (FCampains.StartDate > getdate()) or (FCampains.EndDate < Getdate()) )

--Removeof  items from user''s shopping cart 
EXEC ShoppingCartEmpty5 @CartId,@ShoppingCartDefId,@CampainId

COMMIT TRAN AddOrder

--ShoppingCartList 1 ,''N'' 
' 
END
GO
/****** Object:  StoredProcedure [dbo].[ProductListHomeC2C]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ProductListHomeC2C]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [dbo].[ProductListHomeC2C]
(
      @CartId nvarchar(50),
      @StokIndirim int,
      @StokIskActive bit,
      @LangCode nvarchar(50), 
      @UserIndirim int =0,
      @CatId int,
      @MarkId int=0,
	  @auth int=0,
      @pub int=0,
      @CompanyId int=0,
	  @Top int=300  
)
AS


CREATE TABLE #TempTable(PriceId int,Price nvarchar(20),DiscountType int,Discount decimal(18,2),Discount2 decimal(18,2),Discount3 decimal(18,2),Discount4 decimal(18,2),Discount5 decimal(18,2),ProductId int) 

INSERT #TempTable(PriceId,Price,DiscountType,Discount,Discount2,Discount3,Discount4,Discount5,ProductId)

EXEC dbo.ShoppingCartPriceList @CartId,0
 
IF @CatId >0

BEGIN

SELECT distinct top (@Top) cc.IND as ''ShopId'',ShopNickName,pl.StartDate,pl.ProductSizeOptionId,pl.EndDate,pl.PiyasaFiyati,pl.SatisFiyati0,pl.SatisFiyati1,pl.SatisFiyati2,pl.SatisFiyati3,pl.SatisFiyati4,
pl.SatisFiyati5,pl.Isk1,pl.Isk2,pl.Isk3,pl.Isk4,pl.Isk5,pl.CurrencyP,pl.Currency0,pl.Currency1,pl.Currency2,pl.Currency3,pl.Currency4,pl.Currency5,pl.RateP,
pl.Rate0,pl.Rate1,pl.Rate2,pl.Rate3,pl.Rate4,pl.Rate5, pl.CatId,pl.CatName,pl.CatUrl,pl.ProductId,pl.ProductCode,pl.ProductName,pl.StockQty,pl.ShowOrder,
pl.KdvRate,pl.Path,pl.ImageDesc,pl.MarkId,pl.MarKName,pl.BirimAdi,pl.Carpan,pl.ProductUrl,  
''CurrencyName''= CASE (@StokIndirim) when 0 then Currency1 when 1 then Currency1 when 2 then Currency2 when 3 then Currency3 when 4 then Currency4 when 5 then Currency5 end, 
''Rate''= CASE (@StokIndirim) when 0 then Rate1 when 1 then Rate1 when 2 then Rate2 when 3 then Rate3 when 4 then Rate4 when 5 then Rate5 end,   
pl.SellNoneStock,pl.Code8,pl.Code9,pl.Code10,pl.Code11,pl.Code12,pl.Code13,pl.Code14,pl.Code15,@UserIndirim as ''UserIndirim'',
@StokIskActive as ''StokIskActive'',pl.HizliKargo,pl.EnUcuzUrun,pl.AyniGunTeslim,pl.OzelUrun,pl.IndirimliUrun,pl.SinirliSayidaUrun,pl.FirsatUrunu,pl.YeniUrun,pl.SokFiyatliUrun,pl.HediyeliUrun, 
isnull(Product3dPictures.Url,'''') as Url,isnull(0,0) as prodid,(select top 1 Path from ProductImages where ProductId=pl.ProductId and IsDefault=0) as Url2,
(SELECT isnull(avg(Rating),0) FROM Reviews  WHERE   ProdId = pl.ProductId and IsActive=1) as Review,
''SatisFiyati''=CASE WHEN  Temp.PriceId IS NULL THEN 
			 (case (@StokIndirim) WHEN 0 then SatisFiyati1 WHEN 1 THEN SatisFiyati1 WHEN 2 THEN SatisFiyati2 WHEN 3 THEN SatisFiyati3 WHEN 4 THEN SatisFiyati4 WHEN 5 THEN SatisFiyati5 END )
		 ELSE 
			 (CASE (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))) WHEN 0 THEN dbo.CalcPrice(SatisFiyati1,Temp.DiscountType,Temp.Discount) WHEN 1 THEN dbo.CalcPrice(SatisFiyati1,Temp.DiscountType,Temp.Discount) WHEN 2 THEN dbo.CalcPrice(SatisFiyati2,Temp.DiscountType,Temp.Discount) WHEN 3 THEN dbo.CalcPrice(SatisFiyati3,Temp.DiscountType,Temp.Discount) WHEN 4 THEN dbo.CalcPrice(SatisFiyati4,Temp.DiscountType,Temp.Discount) WHEN 5 THEN dbo.CalcPrice(SatisFiyati5,Temp.DiscountType,Temp.Discount) END )
		 END,
		 ''Indirim''= CASE WHEN  Temp.PriceId IS NULL THEN
			 (CASE WHEN (@StokIskActive >0 ) THEN CASE WHEN (@StokIndirim=0) THEN 0 WHEN (@StokIndirim=1) THEN Isk1 WHEN (@StokIndirim=2) THEN Isk2 WHEN (@StokIndirim=3) THEN Isk3 WHEN (@StokIndirim=4) THEN Isk4 WHEN (@StokIndirim=5) THEN Isk5 END ELSE 0 END)
		 ELSE 
			 (CASE WHEN (@StokIskActive >0 ) THEN CASE WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=0) THEN 0 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=1) THEN Isk1 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=2) THEN Isk2 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=3) THEN Isk3 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=4) THEN Isk4 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=5) THEN Isk5 END ELSE 0 END)
		 END 
FROM 

ProductList pl INNER JOIN 
C2CShopSettings as cc on cc.ShopId=pl.CompanyId LEFT OUTER JOIN 
#TempTable AS Temp ON Temp.ProductId = pl.ProductId LEFT OUTER JOIN 
Product3dPictures ON pl.ProductId = 0 

WHERE pl.Lineage like ''%/'' + CAST(@CatId as nvarchar) + ''/%'' and pl.LanguageCode=@LangCode and pl.IsDeleted=0 and 
(ISNULL(pl.MarkId,0) = case @MarkId when 0 then ISNULL(pl.MarkId,0) else @MarkId end) and 
(ISNULL(pl.AuthorId,0) = case @auth when 0 then ISNULL(pl.AuthorId,0) else @auth end) and 
(ISNULL(pl.PublisherId,0) = case @pub when 0 then ISNULL(pl.PublisherId,0) else @pub end) and 
(cc.ShopId = case @CompanyId when 0 then cc.ShopId else @CompanyId end)  

END

ELSE

BEGIN


declare @ShowInMainPage int =1
if @MarkId<>0 or @auth<>0 or @pub<>0 
begin
	set @Top = 300
	set @ShowInMainPage=-1
end
else 
begin
	set @Top = 50
	set @ShowInMainPage=1
end

SELECT distinct top (@Top)  cc.IND as ''ShopId'',ShopNickName, pl.StartDate,pl.ProductSizeOptionId,pl.EndDate,pl.PiyasaFiyati,pl.SatisFiyati0,pl.SatisFiyati1,pl.SatisFiyati2,pl.SatisFiyati3,pl.SatisFiyati4,
pl.SatisFiyati5,pl.Isk1,pl.Isk2,pl.Isk3,pl.Isk4,pl.Isk5,pl.CurrencyP,pl.Currency0,pl.Currency1,pl.Currency2,pl.Currency3,pl.Currency4,pl.Currency5,pl.RateP,
pl.Rate0,pl.Rate1,pl.Rate2,pl.Rate3,pl.Rate4,pl.Rate5, pl.CatId,pl.CatName,pl.CatUrl,pl.ProductId,pl.ProductCode,pl.ProductName,pl.StockQty,pl.ShowOrder,
pl.KdvRate,pl.Path,pl.ImageDesc,pl.MarkId,pl.MarKName,pl.BirimAdi,pl.Carpan,pl.ProductUrl,  
''CurrencyName''= CASE (@StokIndirim) when 0 then Currency1 when 1 then Currency1 when 2 then Currency2 when 3 then Currency3 when 4 then Currency4 when 5 then Currency5 end, 
''Rate''= CASE (@StokIndirim) when 0 then Rate1 when 1 then Rate1 when 2 then Rate2 when 3 then Rate3 when 4 then Rate4 when 5 then Rate5 end,   
pl.SellNoneStock,pl.Code8,pl.Code9,pl.Code10,pl.Code11,pl.Code12,pl.Code13,pl.Code14,pl.Code15,@UserIndirim as ''UserIndirim'',
@StokIskActive as ''StokIskActive'',pl.HizliKargo,pl.EnUcuzUrun,pl.AyniGunTeslim,pl.OzelUrun,pl.IndirimliUrun,pl.SinirliSayidaUrun,pl.FirsatUrunu,pl.YeniUrun,pl.SokFiyatliUrun,pl.HediyeliUrun, 
isnull(Product3dPictures.Url,'''') as Url,isnull(0,0) as prodid,(select top 1 Path from ProductImages where ProductId=pl.ProductId and IsDefault=0) as Url2,
(SELECT isnull(avg(Rating),0) FROM Reviews  WHERE   ProdId = pl.ProductId and IsActive=1) as Review,
''SatisFiyati''=CASE WHEN  Temp.PriceId IS NULL THEN 
			 (case (@StokIndirim) WHEN 0 then SatisFiyati1 WHEN 1 THEN SatisFiyati1 WHEN 2 THEN SatisFiyati2 WHEN 3 THEN SatisFiyati3 WHEN 4 THEN SatisFiyati4 WHEN 5 THEN SatisFiyati5 END )
		 ELSE 
			 (CASE (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))) WHEN 0 THEN dbo.CalcPrice(SatisFiyati1,Temp.DiscountType,Temp.Discount) WHEN 1 THEN dbo.CalcPrice(SatisFiyati1,Temp.DiscountType,Temp.Discount) WHEN 2 THEN dbo.CalcPrice(SatisFiyati2,Temp.DiscountType,Temp.Discount) WHEN 3 THEN dbo.CalcPrice(SatisFiyati3,Temp.DiscountType,Temp.Discount) WHEN 4 THEN dbo.CalcPrice(SatisFiyati4,Temp.DiscountType,Temp.Discount) WHEN 5 THEN dbo.CalcPrice(SatisFiyati5,Temp.DiscountType,Temp.Discount) END )
		 END,
		 ''Indirim''= CASE WHEN  Temp.PriceId IS NULL THEN
			 (CASE WHEN (@StokIskActive >0 ) THEN CASE WHEN (@StokIndirim=0) THEN 0 WHEN (@StokIndirim=1) THEN Isk1 WHEN (@StokIndirim=2) THEN Isk2 WHEN (@StokIndirim=3) THEN Isk3 WHEN (@StokIndirim=4) THEN Isk4 WHEN (@StokIndirim=5) THEN Isk5 END ELSE 0 END)
		 ELSE 
			 (CASE WHEN (@StokIskActive >0 ) THEN CASE WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=0) THEN 0 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=1) THEN Isk1 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=2) THEN Isk2 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=3) THEN Isk3 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=4) THEN Isk4 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=5) THEN Isk5 END ELSE 0 END)
		 END 
FROM 

ProductList pl  INNER JOIN 
C2CShopSettings as cc on cc.ShopId=pl.CompanyId LEFT OUTER JOIN 
#TempTable AS Temp ON Temp.ProductId = pl.ProductId LEFT OUTER JOIN 
Product3dPictures ON pl.ProductId = Product3dPictures.ProductId 
WHERE pl.ShowInMainPage = case @ShowInMainPage when 1 then 1 else ShowInMainPage  end  and pl.IsDeleted=0 and pl.CatId=(select min(p2.CategoryId) from ProductCategories p2 where  pl.ProductId=p2.ProductId)  and 
pl.LanguageCode=@LangCode and 
(ISNULL(pl.MarkId,0) = case @MarkId when 0 then ISNULL(pl.MarkId,0) else @MarkId end) and 
(ISNULL(pl.AuthorId,0) = case @auth when 0 then ISNULL(pl.AuthorId,0) else @auth end) and 
(ISNULL(pl.PublisherId,0) = case @pub when 0 then ISNULL(pl.PublisherId,0) else @pub end)  and (cc.ShopId = case @CompanyId when 0 then cc.ShopId else @CompanyId end)  
END

DROP TABLE #TempTable

' 
END
GO
/****** Object:  StoredProcedure [dbo].[ProductListHomePrice]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ProductListHomePrice]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [dbo].[ProductListHomePrice]
(
      @CartId nvarchar(50),
      @StokIndirim int,
      @StokIskActive bit,
      @LangCode nvarchar(50), 
      @UserIndirim int =0,
      @CatId int,
      @MarkId int=0,
      @auth int=0,
      @pub int=0,
      @m1 int=0,
      @m2 int=0,
      @year int=0,
	  @SearchKeywords nvarchar(50),
	  @Filter nvarchar(500),  
	  @Top int=300  
)
AS

declare @PriceId  int
declare @ProdId  int
declare @PriceCode  nvarchar(50)
declare @UserFilter nvarchar(500) 
declare @StokFilter nvarchar(500) 

declare @result nvarchar(50)
declare @resultstok nvarchar(50)

CREATE TABLE #TempTable(PriceId int,Price nvarchar(20),DiscountType int,Discount decimal(18,2),Discount2 decimal(18,2),Discount3 decimal(18,2),Discount4 decimal(18,2),Discount5 decimal(18,2),ProductId int primary key not null) 
 

DECLARE CRS_CART CURSOR FOR

select Distinct ProdId from ShoppingCart  where  CartId=@CartId  

OPEN CRS_CART

FETCH NEXT FROM CRS_CART INTO @ProdId  

WHILE @@FETCH_STATUS =0
       BEGIN
 

		DECLARE CRS_USER CURSOR FOR
		
		select UserFilter,PriceId,StokFilter,PriceCode   from PriceList where  Status=1 and StartDate <= getdate() and Enddate >= Getdate() and PriceId<>1 and ApplyOrder<>0  order by ApplyOrder 
		
		OPEN CRS_USER
		
		FETCH NEXT FROM CRS_USER INTO @UserFilter ,@PriceId,@StokFilter,@PriceCode
		
		WHILE @@FETCH_STATUS =0
		       BEGIN
		 
		declare @ExcludedList nvarchar(4000)
		declare @tempCartId int = -1
		
		if ISNUMERIC(@CartId) = 1
		begin 
		set @tempCartId=@CartId 
		end
		
		IF @UserFilter=''''
		BEGIN 
		    SET @UserFilter=''1=1'' 
		END
		 
		
		set @ExcludedList=''select  @result =count(UserId) from users where '' + @UserFilter + ''  and UserId='' + CONVERT(nvarchar(100),@tempCartId)
		--select @ExcludedList
		EXEC sp_executesql @ExcludedList, N''@result VARCHAR(100) OUTPUT'' , @result output
		   
		  
		
		
		declare @StokList nvarchar(4000)
		set @StokList=''select  @resultstok =count(ProductId) from AdminProductList where '' + @StokFilter + ''  and ProductId='' + CONVERT(nvarchar(50), @ProdId)
		EXEC sp_executesql @StokList, N''@resultstok VARCHAR(100) OUTPUT'' , @resultstok output
		    
		if   @result >0 and @resultstok >0 
		begin
		  INSERT INTO #TempTable (PriceId,Price,DiscountType,Discount,Discount2,Discount3,Discount4,Discount5,ProductId)
		  select top 1 PriceId,Price,DiscountType,Discount,Discount2,Discount3,Discount4,Discount5,@ProdId  from PriceList where PriceCode=@PriceCode and  @result >0 and @resultstok >0   order by ApplyOrder 
		  break
		end
             FETCH NEXT FROM CRS_USER INTO @UserFilter,@PriceId,@StokFilter,@PriceCode

       END

       CLOSE CRS_USER
DEALLOCATE CRS_USER



 FETCH NEXT FROM CRS_CART INTO @ProdId

       END

       CLOSE CRS_CART
DEALLOCATE CRS_CART

 
declare @str nvarchar(max)

declare @ShowInMainPage int =1
if @MarkId<>0 or @auth<>0 or @pub<>0  or @SearchKeywords<>'''' or @CatId<>0 
begin
	set @Top = 300
	set @ShowInMainPage=-1
end
else 
begin
	set @Top = 50
	set @ShowInMainPage=1
end

set @str = ''SELECT distinct top '' + CONVERT(nvarchar,@Top) + '' pl.StartDate,pl.ProductSizeOptionId,pl.EndDate,pl.PiyasaFiyati,pl.SatisFiyati0,pl.SatisFiyati1,pl.SatisFiyati2,pl.SatisFiyati3,pl.SatisFiyati4, '' + 
	+ ''		pl.SatisFiyati5,pl.Isk1,pl.Isk2,pl.Isk3,pl.Isk4,pl.Isk5,pl.CurrencyP,pl.Currency0,pl.Currency1,pl.Currency2,pl.Currency3,pl.Currency4,pl.Currency5,pl.RateP, '' + 
	+ ''		pl.Rate0,pl.Rate1,pl.Rate2,pl.Rate3,pl.Rate4,pl.Rate5, pl.CatId,pl.CatName,pl.CatUrl,pl.ProductId,pl.ProductCode,pl.ProductName,pl.StockQty,pl.ShowOrder, '' + 
	+ ''		pl.KdvRate,pl.Path,pl.ImageDesc,pl.MarkId,pl.MarKName,pl.BirimAdi,pl.Carpan,pl.ProductUrl,   '' + 
	+ ''		''''CurrencyName''''= CASE ('' + convert(nvarchar,@StokIndirim) + '') when 0 then Currency1 when 1 then Currency1 when 2 then Currency2 when 3 then Currency3 when 4 then Currency4 when 5 then Currency5 end,  '' + 
	+ ''		''''Rate''''= CASE ('' + convert(nvarchar,@StokIndirim) + '') when 0 then Rate1 when 1 then Rate1 when 2 then Rate2 when 3 then Rate3 when 4 then Rate4 when 5 then Rate5 end,    '' + 
	+ ''		pl.SellNoneStock,pl.Code8,pl.Code9,pl.Code10,pl.Code11,pl.Code12,pl.Code13,pl.Code14,pl.Code15,'' + convert(nvarchar,@UserIndirim) + '' as ''''UserIndirim'''', '' + 
	+ ''		'' + convert(nvarchar,@StokIskActive) + '' as ''''StokIskActive'''',pl.HizliKargo,pl.EnUcuzUrun,pl.AyniGunTeslim,pl.OzelUrun,pl.IndirimliUrun,pl.SinirliSayidaUrun,pl.FirsatUrunu,pl.YeniUrun,pl.SokFiyatliUrun,pl.HediyeliUrun,  '' + 
	+ ''		isnull(Product3dPictures.Url,'''''''') as Url,isnull(0,0) as prodid,(select top 1 Path from ProductImages where ProductId=pl.ProductId and IsDefault=0) as Url2, '' + 
	+ ''		(SELECT isnull(avg(Rating),0) FROM Reviews  WHERE   ProdId = pl.ProductId and IsActive=1) as Review, '' + 
	+ ''		''''SatisFiyati''''=CASE WHEN  Temp.PriceId IS NULL THEN  '' + 
	+ ''					 (case ('' + convert(nvarchar,@StokIndirim) + '') WHEN 0 then SatisFiyati1 WHEN 1 THEN SatisFiyati1 WHEN 2 THEN SatisFiyati2 WHEN 3 THEN SatisFiyati3 WHEN 4 THEN SatisFiyati4 WHEN 5 THEN SatisFiyati5 END ) '' + 
	+ ''				 ELSE  '' + 
	+ ''					  (CASE (convert(int,Replace(Temp.Price,''''SatisFiyati'''',''''''''))) WHEN 0 THEN dbo.CalcPrice5(SatisFiyati1,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5)  '' + 
	+ ''					   WHEN 1 THEN dbo.CalcPrice5(SatisFiyati1,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5)  '' + 
	+ ''					   WHEN 2 THEN dbo.CalcPrice5(SatisFiyati2,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5)  '' + 
	+ ''					   WHEN 3 THEN dbo.CalcPrice5(SatisFiyati3,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5)  '' + 
	+ ''					   WHEN 4 THEN dbo.CalcPrice5(SatisFiyati4,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5)  '' + 
	+ ''					   WHEN 5 THEN dbo.CalcPrice5(SatisFiyati5,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) END ) '' + 
	+ ''				 END, '' + 
	+ ''				 ''''Indirim''''= CASE WHEN  Temp.PriceId IS NULL THEN '' + 
	+ ''					 (CASE WHEN ('' + convert(nvarchar,@StokIskActive) + '' >0 ) THEN CASE WHEN ('' + convert(nvarchar,@StokIndirim) + ''=0) THEN 0 WHEN ('' + convert(nvarchar,@StokIndirim) + ''=1) THEN Isk1 WHEN ('' + convert(nvarchar,@StokIndirim) + ''=2) THEN Isk2 WHEN ('' + convert(nvarchar,@StokIndirim) + ''=3) THEN Isk3 WHEN ('' + convert(nvarchar,@StokIndirim) + ''=4) THEN Isk4 WHEN ('' + convert(nvarchar,@StokIndirim) + ''=5) THEN Isk5 END ELSE 0 END) '' + 
	+ ''				 ELSE  '' + 
	+ ''					 (CASE WHEN ('' + convert(nvarchar,@StokIskActive) + '' >0 ) THEN CASE WHEN (convert(int,Replace(Temp.Price,''''SatisFiyati'''',''''''''))=0) THEN 0 WHEN (convert(int,Replace(Temp.Price,''''SatisFiyati'''',''''''''))=1) THEN Isk1 WHEN (convert(int,Replace(Temp.Price,''''SatisFiyati'''',''''''''))=2) THEN Isk2 WHEN (convert(int,Replace(Temp.Price,''''SatisFiyati'''',''''''''))=3) THEN Isk3 WHEN (convert(int,Replace(Temp.Price,''''SatisFiyati'''',''''''''))=4) THEN Isk4 WHEN (convert(int,Replace(Temp.Price,''''SatisFiyati'''',''''''''))=5) THEN Isk5 END ELSE 0 END) '' + 
	+ ''				 END '' + 
	+ ''		FROM  '' +  
	+ ''		ProductList pl LEFT OUTER JOIN  '' + 
	+ ''		#TempTable AS Temp ON Temp.ProductId = pl.ProductId LEFT OUTER JOIN  '' + 
	+ ''		Product3dPictures ON pl.ProductId = 0  LEFT OUTER JOIN  '' + 
	+ ''		MotorProductDesc as m ON pl.ProductId = m.ProductId   ''    

	if @SearchKeywords <> '''' 
		set @str= @str + ''INNER JOIN  dbo.SplitString(@SearchKeywords,'''' '''') as ss ON ((pl.ProductName  like ''''%'''' + ss.Name + ''''%'''' COLLATE SQL_Latin1_General_CP1_CI_AI or pl.SearchKeywords '' +
		'' like ''''%'''' + ss.Name + ''''%'''' COLLATE SQL_Latin1_General_CP1_CI_AI) or    (pl.ProductCode  like ''''%'''' + ss.Name + ''''%'''' COLLATE SQL_Latin1_General_CP1_CI_AI or pl.SearchKeywords  like ''''%'''' + ss.Name + ''''%'''' COLLATE SQL_Latin1_General_CP1_CI_AI)) ''


	set @str= @str + ''     WHERE pl.ShowInMainPage = case '' + convert(nvarchar,@ShowInMainPage) + '' when 1 then 1 else ShowInMainPage  end   and  pl.LanguageCode='''''' + @LangCode + '''''' '' + @Filter  
	IF @CatId >0
		set @str=@str + '' and pl.CatId='' + convert(nvarchar,@CatId) 
		 
	if @m1 > 0
		set @str = @str + '' and ISNULL(m.MarkId,0)= '' + convert(nvarchar,@m1)

	if @m2 > 0
		set @str = @str + '' and ISNULL(m.ModelId,0)= '' + convert(nvarchar,@m2)

	if @year > 0
		set @str = @str + '' and ISNULL(m.YearId,0)= ''	+ convert(nvarchar,@year)

	if @MarkId > 0
		set @str = @str + '' and ISNULL(pl.MarkId,0)= ''	+ convert(nvarchar,@MarkId)

	if @auth > 0
		set @str = @str + '' and ISNULL(pl.AuthorId,0)= ''	+ convert(nvarchar,@auth)

	if @pub > 0
		set @str = @str + '' and ISNULL(pl.PublisherId,0)= ''	+ convert(nvarchar,@pub)

	EXEC sp_executesql  @str, N''@result VARCHAR(100) OUTPUT'' , @result output 
 

DROP TABLE #TempTable


' 
END
GO
/****** Object:  StoredProcedure [dbo].[SalesmanPrimeList]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SalesmanPrimeList]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[SalesmanPrimeList]
	 @CartId NVARCHAR(50) ,
	 @Filter NVARCHAR(300) ='''',
	 @FilterOrderId INT
AS

DECLARE @OrderDetailsId INT
DECLARE @ProductId INT
DECLARE @SatisFiyati DECIMAL(18,4)
DECLARE @Rate DECIMAL(18,4)
DECLARE @Carpan INT
DECLARE @Quantity DECIMAL(18,4)
DECLARE @ToplamIndirim DECIMAL(18,4)
DECLARE @KargoDahilToplamTutar DECIMAL(18,4)
DECLARE @BankaToplamTutar DECIMAL(18,4)
DECLARE @BirimIndirim DECIMAL(18,4)
DECLARE @Status2 INT
DECLARE @OrderId INT
DECLARE @OrderCode NVARCHAR(50)
DECLARE @CustomerId INT
DECLARE @KdvRate DECIMAL(18,4)
DECLARE @ProductName NVARCHAR(100) 
DECLARE @StatusName2 NVARCHAR(100)
DECLARE @Description NVARCHAR(300)
DECLARE @OrderDate DATETIME

DECLARE @IND INT
DECLARE @BonusType INT
DECLARE @BonusAmount DECIMAL(18,4)
DECLARE @UserFilter NVARCHAR(300)
DECLARE @StokFilter NVARCHAR(300)
DECLARE @OrderTypes NVARCHAR(100)
DECLARE @IsQuota INT
DECLARE @PaymentType NVARCHAR(100)
 

BEGIN
    CREATE TABLE #TempPrimeTable(OrderId int,OrderCode nvarchar(50) ,CustomerId int,CampainId int,OrderDetailsId int,SatisFiyati decimal(18,4),Rate decimal(18,4),Carpan int,Quantity decimal(18,4),ToplamIndirim decimal(18,4),
	BankaToplamTutar decimal(18,4),KargoDahilToplamTutar decimal(18,4),BirimIndirim decimal(18,4),BonusType INT,BonusAmount decimal(18,4),IsQuota int,Kdv decimal(18,4),
	ProductId int,ProductName nvarchar(100),Description nvarchar(300),Status2 int,StatusName2 nvarchar(100),OrderDate datetime,PaymentType nvarchar(100))  
	DECLARE @result INT
    SET @result=0
	DECLARE @tempCartId int = -1
					
	IF ISNUMERIC(@CartId) = 1
	BEGIN 
	    SET @tempCartId=@CartId 
	END
	 

	IF @Filter=''''
	BEGIN 
	    SET @Filter=''0'' 
	END

	DECLARE CRS_ORDER CURSOR FOR
	  
	SELECT DISTINCT oo.OrderId,oo.OrderCode,oo.OrderDetailsId,oo.ProductId,oo.ProductName,oo.SatisFiyati,oo.Rate,oo.Carpan,oo.Quantity,oo.ToplamIndirim,oo.KargoDahilToplamTutar,oo.BankaToplamTutar,oo.Status2, 
    (select sum(o.Quantity * o.Carpan * o.SatisFiyati)  from OrdersListDetails as o where o.OrderId=oo.OrderId) as ''BirimIndirim'',oo.CustomerId,oo.KdvRate,oo.Description,oo.StatusName2,
	oo.OrderDate,oo.PaymentType   from OrdersListDetails as oo
	LEFT JOIN SalePlasiyerUser ON oo.CustomerId=SalePlasiyerUser.userId  WHERE (PlasiyerId=@tempCartId OR oo.CustomerId=@tempCartId) and 
	(oo.OrderId=case @FilterOrderId when 0 then oo.OrderId else @FilterOrderId end) and 
	(OrderDetailsId=case convert(int,@Filter) when 0 then OrderDetailsId else convert(int,@Filter) end)
	 
	OPEN CRS_ORDER 

	FETCH NEXT FROM CRS_ORDER  INTO @OrderId,@OrderCode,@OrderDetailsId,@ProductId,@ProductName,@SatisFiyati,@Rate,@Carpan,@Quantity,@ToplamIndirim,@KargoDahilToplamTutar,@BankaToplamTutar,@Status2,@BirimIndirim,@CustomerId,@KdvRate,@Description,@StatusName2,@OrderDate,@PaymentType

	WHILE @@FETCH_STATUS =0
       BEGIN
	    
			DECLARE CRS_PRIME CURSOR FOR
	  
			SELECT IND,BonusType,BonusAmount,UserFilter,StokFilter,OrderTypes,IsQuota FROM SaleBonusDefinitions 
			WHERE IsActive=1 AND '','' + OrderTypes LIKE  ''%,'' + CONVERT(NVARCHAR(50),@Status2) + '',%'' AND StartDate<= @OrderDate AND EndDate >= @OrderDate   ORDER BY ApplyOrder 
			
			OPEN CRS_PRIME 

			FETCH NEXT FROM CRS_PRIME  INTO @IND,@BonusType,@BonusAmount,@UserFilter,@StokFilter,@OrderTypes,@IsQuota

			WHILE @@FETCH_STATUS =0
			   BEGIN
			   		DECLARE @ExcludedList nvarchar(4000) 
			   		DECLARE @ExcludedListPro nvarchar(4000) 
					
					IF @UserFilter=''''
					BEGIN 
					    SET @UserFilter=''1=1'' 
					END

					IF @StokFilter=''''
					BEGIN 
					    SET @StokFilter=''PortalId = 0'' 
					END

					SET @ExcludedList=''select  @result =count(SalePlasiyerUser.UserId) from SalePlasiyerUser inner join users on SalePlasiyerUser.PlasiyerId=users.UserId where '' + @UserFilter + ''  and (SalePlasiyerUser.UserId='' + CONVERT(nvarchar(50),@tempCartId) + '' or SalePlasiyerUser.PlasiyerId='' + CONVERT(nvarchar(50),@tempCartId) + '')''
					EXEC sp_executesql @ExcludedList, N''@result int OUTPUT'' , @result output

					IF @result >0
					BEGIN
						SET @ExcludedListPro=''select  @result =count(UserId) from AdminProductList where '' + @StokFilter  + ''  and ProductId='' + CONVERT(nvarchar(50),@ProductId)
					    EXEC sp_executesql @ExcludedListPro, N''@result int OUTPUT'' , @result output
					    IF @result >0
						BEGIN
							INSERT INTO #TempPrimeTable (OrderId,OrderCode,CustomerId,CampainId,OrderDetailsId,SatisFiyati,Rate,Carpan,Quantity,ToplamIndirim,BankaToplamTutar,KargoDahilToplamTutar,BirimIndirim,BonusType,BonusAmount,IsQuota,Kdv,ProductId,ProductName,Description,Status2,StatusName2,OrderDate,PaymentType)  
							VALUES (@OrderId,@OrderCode,@CustomerId,@IND,@OrderDetailsId,@SatisFiyati,@Rate,@Carpan,@Quantity,@ToplamIndirim,@BankaToplamTutar,@KargoDahilToplamTutar,@BirimIndirim,@BonusType,@BonusAmount,@IsQuota,@KdvRate,@ProductId,@ProductName,@Description,@Status2,@StatusName2,@OrderDate,@PaymentType)
							BREAK 
						END
					END

			    FETCH NEXT FROM CRS_PRIME  INTO @IND,@BonusType,@BonusAmount,@UserFilter,@StokFilter,@OrderTypes,@IsQuota

			   END 

			CLOSE CRS_PRIME 

			DEALLOCATE CRS_PRIME 
		 
        FETCH NEXT FROM CRS_ORDER  INTO @OrderId,@OrderCode,@OrderDetailsId,@ProductId,@ProductName,@SatisFiyati,@Rate,@Carpan,@Quantity,@ToplamIndirim,@KargoDahilToplamTutar,@BankaToplamTutar,@Status2,@BirimIndirim,@CustomerId,@KdvRate,@Description,@StatusName2,@OrderDate,@PaymentType

       END

	CLOSE CRS_ORDER 

	DEALLOCATE CRS_ORDER 

	SET @Filter=''where OrderDetailsId=''  + @Filter

	DECLARE @ExcludedResult nvarchar(4000) 
	SET @ExcludedResult=''select   #TempPrimeTable.*,''''NetPrice''''=  
	case when (KargoDahilToplamTutar - BankaToplamTutar) > 0 then (SatisFiyati * Quantity )-((KargoDahilToplamTutar - BankaToplamTutar )/BirimIndirim * SatisFiyati * Quantity )   
	else (SatisFiyati * Quantity ) end,Name,FullName,CompanyCode,Email from  #TempPrimeTable inner join Users on CustomerId=UserId '' + 
	@Filter + '' order by OrderDate desc''
	
	EXEC sp_executesql @ExcludedResult 

	 --select  * from #TempPrimeTable 
 
    drop table #TempPrimeTable
 

END
' 
END
GO
/****** Object:  StoredProcedure [dbo].[ShoppingCartChiperCampainList]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ShoppingCartChiperCampainList]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[ShoppingCartChiperCampainList] 
    @CartId nvarchar(50), 
    @totalAmount decimal(18,4), 
    @totalAmountWithTax decimal(18,4), 
    @ShoppingCartDefId int,  
    @Lang int, 
    @FCampainId int, 
	@Result2 int output,
	@ResultType int output,
	@ResultAmount  money output, 
    @IsKdv int output
AS
 
declare @CampainId  int
declare @UserFilter nvarchar(300) 
declare @result int
declare @StokFilter nvarchar(500) 
declare @MinQty int
declare @Name nvarchar(500) 
declare @Details nvarchar(500) 
declare @Type int
declare @Amount Money
 

CREATE TABLE #TempCampainTable(CampainId int,Name nvarchar(100),Details nvarchar(500))  


SET @Result2=0
SET @ResultType=0
SET @ResultAmount=0
SET @IsKdv=0

DECLARE CRS_CART CURSOR FOR

select CampainId,UserFilter,StokFilter,MinQty,Name,Details,DiscountType,DiscountAmount,IsKdvDahil  from CampainChiper 
where IsActive=1 and StartDate < GETDATE() and EndDate > GETDATE() and 
(MinPrice <= case IsKdvDahil when 0 then @totalAmount else @totalAmountWithTax end)  
--MinPriceCart <= @totalAmount

OPEN CRS_CART

FETCH NEXT FROM CRS_CART INTO @CampainId,@UserFilter,@StokFilter,@MinQty,@Name,@Details,@Type,@Amount,@IsKdv 

WHILE @@FETCH_STATUS =0
       BEGIN

	   
		DECLARE @ExcludedList nvarchar(4000) 
		DECLARE @tempCartId int = -1
		
		IF ISNUMERIC(@CartId) = 1
		BEGIN 
		    SET @tempCartId=@CartId 
		END
		IF @UserFilter=''''
		BEGIN 
		    SET @UserFilter=''1=1'' 
		END
		 
		IF @StokFilter=''''
		BEGIN 
		    SET @StokFilter=''PortalId = 0'' 
		END

		SET @ExcludedList=''select  @result =count(UserId) from users where '' + @UserFilter + ''  and UserId='' + CONVERT(nvarchar(50),@tempCartId)
		EXEC sp_executesql @ExcludedList, N''@result int OUTPUT'' , @result output
		IF @result > 0
		BEGIN
		     declare   @myTable table(RecordId int,Qty int,Carpan int,Description nvarchar(100))
			 declare @StokList nvarchar(4000)
 
			 set @StokList=''(select distinct ShoppingCart.RecordId,ShoppingCart.Qty,Carpan,ShoppingCart.Description  from AdminProductList inner join ShoppingCart on AdminProductList.ProductId=ShoppingCart.ProdId  INNER JOIN'' +   
			 '' ProductUnits ON dbo.ProductUnits.UnitId = dbo.ShoppingCart.PriceId where CartId='''''' + CONVERT(nvarchar(50),@tempCartId) + '''''' and ShoppingCartDefId='' + CONVERT(nvarchar(50),@ShoppingCartDefId) + 
			 '' and AdminProductList.CampainId='' + CONVERT(nvarchar(50),@FCampainId) +  '' and LanguageId='' + CONVERT(nvarchar(50),@Lang) + '' and '' +  @StokFilter + '')''
			  
			   
			  insert into @myTable  EXEC sp_executesql @StokList
			  
			  declare @RecordId  int
			  declare @Qty  int
			  declare @Carpan  int
			  declare @TotalQty  int

			  set @TotalQty=0

			  DECLARE CRS_PROD CURSOR FOR

			  select RecordId,Qty,Carpan from @myTable 
			  
			  OPEN CRS_PROD
			  
			  FETCH NEXT FROM CRS_PROD INTO @RecordId,@Qty,@Carpan  
			  
			  WHILE @@FETCH_STATUS =0
			         BEGIN
						   set  @TotalQty = @TotalQty + (@Qty * @Carpan)
			  			   FETCH NEXT FROM CRS_PROD INTO @RecordId,@Qty,@Carpan 
					 END
					
					CLOSE CRS_PROD
					
					DEALLOCATE CRS_PROD
    
			  if @TotalQty >= @MinQty
			  BEGIN 
			      insert into #TempCampainTable (CampainId,Name,Details) values (@CampainId,@Name,@Details)
				  IF @Result2=0
				  BEGIN 
				      SET @Result2=@CampainId
					  SET @ResultType= @Type
					  SET @ResultAmount = CONVERT(Money, @Amount)
					  SET @IsKdv = @IsKdv
				  END
			  END

		--select * from @myTable  
		END
    
        FETCH NEXT FROM CRS_CART INTO @CampainId,@UserFilter,@StokFilter,@MinQty,@Name,@Details,@Type,@Amount,@IsKdv 

       END

CLOSE CRS_CART

DEALLOCATE CRS_CART

select * from  #TempCampainTable 
 
drop table #TempCampainTable
 
' 
END
GO
/****** Object:  UserDefinedFunction [dbo].[CalcPrice5]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CalcPrice5]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'
-- Function
CREATE FUNCTION [dbo].[CalcPrice5] 
	(
	@value decimal(18,4)=0,
	@type int=0,
	@rate1 decimal(18,4)=0,
	@rate2 decimal(18,4)=0,
	@rate3 decimal(18,4)=0,
	@rate4 decimal(18,4)=0,
	@rate5 decimal(18,4)=0
	)
RETURNS decimal(18,4)
AS

BEGIN
	declare @retvalue decimal(18,4)
	
		
		set @retvalue = dbo.CalcPrice(dbo.CalcPrice(dbo.CalcPrice(dbo.CalcPrice(dbo.CalcPrice(@value,@type,@rate1),@type,@rate2),@type,@rate3),@type,@rate4),@type,@rate5)
				 
	RETURN @retvalue
END	 
	 

 
  
 

' 
END

GO
/****** Object:  UserDefinedFunction [dbo].[CsvToInt]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CsvToInt]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'Create Function [dbo].[CsvToInt] ( @Array varchar(1000)) 
returns @IntTable table (IntValue int) 
AS 
Begin 
  Declare @separator char(1) 
  Set @separator = '','' 
  Declare @separator_position int 
  Declare @array_value varchar(1000)
  Set @array = @array + '','' 
  While patindex(''%,%'' , @array) <> 0 
  Begin 
     SELECT @separator_position = patindex(''%,%'' , @array) 
     SELECT @array_value = left(@array, @separator_position - 1) 
     Insert @IntTable Values (Cast(@array_value as int)) 
     SELECT @array = stuff(@array, 1, @separator_position, '''') 
  End 
Return 
End ' 
END

GO
/****** Object:  UserDefinedFunction [dbo].[GetCatList]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetCatList]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N' CREATE FUNCTION [dbo].[GetCatList] 
(
	@CartId nvarchar(50) 
)
RETURNS NVARCHAR(500)

AS
 
BEGIN
declare  @UserFilter nvarchar(500) 
declare  @IND  int
declare  @CatFilter nvarchar(500) 
declare  @IsForGuests  bit
declare  @result int=0
declare  @result2 nvarchar(500)

DECLARE CRS_USER CURSOR FOR
 
Select UserFilter,IND,CatFilter,IsForGuests from PriceCategories where Status=1 And StartDate<=GETDATE() And EndDate>=GETDATE()  order by ApplyOrder 
set @result2=''''

OPEN CRS_USER

FETCH NEXT FROM CRS_USER INTO @UserFilter ,@IND,@CatFilter,@IsForGuests

WHILE @@FETCH_STATUS =0
    BEGIN
 
    declare @ExcludedList nvarchar(4000)
	
	IF (@IsForGuests=1 AND ISNUMERIC(@CartId) = 0)
	BEGIN
		set @result2 = @result2 + @CatFilter 
	END

	IF ISNUMERIC(@CartId)=1
	BEGIN
		set @ExcludedList=''select  @result =count(UserId) from users where '' + @UserFilter + ''  and UserId='' + @CartId  
		EXEC sp_executesql @ExcludedList, N''@result INT OUTPUT'' , @result   
		 if @result >0
		 begin 
		   set @result2 = @result2 + @CatFilter 
		 end
	END

	
	
    FETCH NEXT FROM CRS_USER INTO @UserFilter,@IND,@CatFilter,@IsForGuests

END

CLOSE CRS_USER
DEALLOCATE CRS_USER

RETURN @result2 

END


' 
END

GO
/****** Object:  UserDefinedFunction [dbo].[SplitString]    Script Date: 28.9.2016 10:46:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SplitString]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'CREATE FUNCTION [dbo].[SplitString] ( @stringToSplit VARCHAR(MAX) , @val VARCHAR(1) )
RETURNS
 @returnList TABLE ([Name] [nvarchar] (500))
AS
BEGIN

 DECLARE @name NVARCHAR(255)
 DECLARE @pos INT

 WHILE CHARINDEX(@val, @stringToSplit) > 0
 BEGIN
  SELECT @pos  = CHARINDEX(@val, @stringToSplit)  
  SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)

  INSERT INTO @returnList 
  SELECT @name

  SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
 END

 INSERT INTO @returnList
 SELECT @stringToSplit

 RETURN
END' 
END

GO


 
/****** Object:  StoredProcedure [dbo].[AddUser3]    Script Date: 8.9.2016 11:56:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[AddUser3]
(
@PortalId		int,
@UserType		int,
@FullName		nvarchar(100),
@UserSex		int=-2,
@Phone1			nvarchar(14)='',
@Phone2			nvarchar(14)='',
@FaxNumber		nvarchar(14),
@MobilePhone	nvarchar(14),
@CityId			int,
@Addres			nvarchar(400),
@CompanyName	nvarchar(100),
@CompanyPhone1	nvarchar(14),
@CompanyPhone2	nvarchar(14)='',
@CompanyEmail	nvarchar(100),
@CompanyCityId	int,
@CompanyId		int,
@CompanyAddres	nvarchar(400),
@CompanyWeb		nvarchar(100)='',
@Name			nvarchar(50),
@Email			nvarchar(100),
@Password		nvarchar(20),
@TCKimlikNo	    varchar(11)='',
@BirthDate		smalldatetime=null,
@Profession	    varchar(25)='',
@WhereToHear	varchar(50)='',
@Education		varchar(25)='',
@IsActive		bit,
@IsSupplier		bit,
@QapelID		nvarchar(100),
@QapelToken		nvarchar(100),
@UserId			int OUTPUT
)
AS
set @UserId=-1
if not EXISTS(Select UserId From Users Where Email=@Email and PortalId=@PortalId)
begin
INSERT INTO Users
(
PortalId,		
UserType,		
FullName,		
UserSex	,	
Phone1	,		
Phone2	,	
FaxNumber,	
MobilePhone	,
CityId		,	
Addres		,
CompanyId	,
CompanyName	,
CompanyPhone1,	
CompanyPhone2,	
CompanyEmail,	
CompanyCityId,	
CompanyAddres,	
CompanyWeb,
Name	,		
Email	,		
Password,
TCKimlikNo,
BirthDate,
Profession,
WhereToHear,
Education,
ActivationCode,
IsActive,
IsSupplier,
QapelID,
QapelToken
)

VALUES
(
@PortalId		,
@UserType		,
@FullName		,
@UserSex		,
@Phone1			,
@Phone2			,
@FaxNumber		,
@MobilePhone	,
@CityId			,
@Addres			,
@CompanyId		,
@CompanyName	,
@CompanyPhone1	,
@CompanyPhone2	,
@CompanyEmail	,
@CompanyCityId	,
@CompanyAddres	,
@CompanyWeb		,
@Name			,
@Email			,
@Password,		
@TCKimlikNo,
@BirthDate,
@Profession,
@WhereToHear,
@Education,
NEWID(),
@IsActive,
@IsSupplier,
@QapelID,
@QapelToken
)

EXEC AddSupplierPreviliges @@Identity,0,0,'0,0','0,0','0,0','0,0','0,0','0,0','0,0','0,0','0,0','0,0','0,0','0,0','0,0','0,0','0,0',1

SELECT
    @UserId = @@Identity
    end
else
begin
select @UserId = 0
end

GO

 
/****** Object:  StoredProcedure [dbo].[GetAlternativeProducts]    Script Date: 8.9.2016 11:56:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER  PROCEDURE [dbo].[GetAlternativeProducts]
(
@ProductId int,
@StokIndirim int,
@StokIskActive bit
)

AS
SELECT     dbo.Products.ProductId,dbo.Products.ProductCode, dbo.Products.ProductName, Products.Url, Products.StockQty,Products.SellNoneStock,KdvRate,
'SatisFiyati'= case (@StokIndirim) when 0 then SatisFiyati1 when 1 then SatisFiyati1 when 2 then SatisFiyati2 when 3 then SatisFiyati3 when 4 then SatisFiyati4 when 5 then SatisFiyati5 end,  case when (@StokIskActive >0 ) then case when (@StokIndirim=0) then 0 when (@StokIndirim=1) then Isk1 when (@StokIndirim=2) then Isk2 when (@StokIndirim=3) then Isk3 when (@StokIndirim=4) then Isk4 when (@StokIndirim=5) then Isk5 end else 0 end as Indirim ,
dbo.Currencys.CurrencyName,Currencys.Rate,Products.Code14,ProductImages.Path
FROM         dbo.Products INNER JOIN
dbo.ProductUnits ON dbo.Products.ProductId = dbo.ProductUnits.ProductId INNER JOIN
dbo.ProductKdvs  ON dbo.Products.KdvId = dbo.ProductKdvs.KdvId  INNER JOIN
dbo.ProductImages  ON dbo.Products.ProductId = dbo.ProductImages.ProductId INNER JOIN
dbo.Currencys ON case (@StokIndirim) when 0 then ProductUnits.Pb1 when 1 then ProductUnits.Pb1 when 2 then ProductUnits.Pb2 when 3 then ProductUnits.Pb3 when 4 then ProductUnits.Pb4 when 5 then ProductUnits.Pb5 end
--dbo.ProductUnits.Pb1
= dbo.Currencys.CurrencyId
WHERE     (NOT (dbo.Products.ProductId = @ProductId))
and (dbo.Products.ProductId in(Select ProductId from ProductAlternatives where AProductId=@ProductId)
or dbo.Products.ProductId in(Select AProductId from ProductAlternatives where ProductId=@ProductId))
AND (dbo.ProductUnits.IsDefault = 1) and (dbo.ProductImages.IsDefault = 1) AND dbo.Products.IsActive=1


GO

 
/****** Object:  StoredProcedure [dbo].[GetGroupProducts]    Script Date: 8.9.2016 11:57:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER  PROCEDURE [dbo].[GetGroupProducts]
(
@ProductId int,
@StokIndirim int,
@StokIskActive bit,
@Type bit
)

AS
SELECT  distinct  Products.ProductSizeOptionId ,ProductSize0Name,ProductSize1Name,ProductSize2Name, ProductGroups.StockQty AS 'GroupStockQty',ProductGroups.UnitName,dbo.Products.ProductId,dbo.Products.ProductCode, dbo.Products.ProductName, Products.Url, Products.StockQty,Products.SellNoneStock,
'SatisFiyati'= case (@StokIndirim) when 0 then SatisFiyati1 when 1 then SatisFiyati1 when 2 then SatisFiyati2 when 3 then SatisFiyati3 when 4 then SatisFiyati4 when 5 then SatisFiyati5 end,  case when (@StokIskActive >0 ) then case when (@StokIndirim=0) then 0 when (@StokIndirim=1) then Isk1 when (@StokIndirim=2) then Isk2 when (@StokIndirim=3) then Isk3 when (@StokIndirim=4) then Isk4 when (@StokIndirim=5) then Isk5 end else 0 end as Indirim ,
dbo.Currencys.CurrencyName,Currencys.Rate,Products.Code14,ProductImages.Path
FROM         dbo.Products INNER JOIN
dbo.ProductUnits ON dbo.Products.ProductId = dbo.ProductUnits.ProductId INNER JOIN
dbo.ProductImages  ON dbo.Products.ProductId = dbo.ProductImages.ProductId INNER JOIN
dbo.Currencys ON case (@StokIndirim) when 0 then ProductUnits.Pb1 when 1 then ProductUnits.Pb1 when 2 then ProductUnits.Pb2 when 3 then ProductUnits.Pb3 when 4 then ProductUnits.Pb4 when 5 then ProductUnits.Pb5 end = dbo.Currencys.CurrencyId 
INNER JOIN ProductGroups ON dbo.Products.ProductId = ProductGroups.GProductId
LEFT JOIN ProductSizeList ON ProductSizeList.ProductId=ProductGroups.GProductId 
WHERE     (NOT (dbo.Products.ProductId = @ProductId))
and (dbo.Products.ProductId in(Select ProductId from ProductGroups where GProductId=@ProductId AND ProductGroups.Type = @Type)
or dbo.Products.ProductId in(Select GProductId from ProductGroups where ProductId=@ProductId AND ProductGroups.Type = @Type))
AND (dbo.ProductUnits.IsDefault = 1) and (dbo.ProductImages.IsDefault = 1) AND dbo.Products.IsActive=1 AND ProductGroups.Type = @Type

GO

 
/****** Object:  StoredProcedure [dbo].[GetProductDetails]    Script Date: 8.9.2016 11:58:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[GetProductDetails]

	(
		@PortalId int,
		@ProductId int,
		@StokIndirim int,
		@StokIskActive bit,
		@UnitId int=0
	)

AS
if @UnitId=0
begin
	SELECT
	Details,ProductId, ProductCode, ProductName,Description, StockQty,KdvRate, Path, MarKName,BirimAdi, Carpan,SatisFiyati0,Rate0,Currency0,
    'SatisFiyati'= case (@StokIndirim) when 0 then SatisFiyati1 when 1 then SatisFiyati1 when 2 then SatisFiyati2 when 3 then SatisFiyati3 end,  case when (@StokIskActive >0 ) then case when (@StokIndirim=0) then 0 when (@StokIndirim=1) then Isk1 when (@StokIndirim=2) then Isk2 when (@StokIndirim=3) then Isk3 end else 0 end as Indirim ,  
    'CurrencyName'= case (@StokIndirim) when 0 then Currency1 when 1 then Currency1 when 2 then Currency2 when 3 then Currency3 end,
    'Rate'= case (@StokIndirim) when 0 then Rate1 when 1 then Rate1 when 2 then Rate2 when 3 then Rate3 end,
    SellNoneStock,Code1,Code2,Code3,Code4,Code5,Code6,Code7,Code8,Code9,Code10,Code11,Code12,Code13,Code14,Code15,WizardId,CampainId,ForceCargo
FROM       ProductListDetails 
WHERE   (UnitsIsDefault = 1) AND  (ProductId = @ProductId) and (PortalId = @PortalId) 
end
else
begin
SELECT     
	Details,ProductId, ProductCode, ProductName,Description, StockQty,KdvRate, Path, MarKName,BirimAdi, Carpan,SatisFiyati0,Rate0,Currency0,
    'SatisFiyati'= case (@StokIndirim) when 0 then SatisFiyati1 when 1 then SatisFiyati1 when 2 then SatisFiyati2 when 3 then SatisFiyati3 end,  case when (@StokIskActive >0 ) then case when (@StokIndirim=0) then 0 when (@StokIndirim=1) then Isk1 when (@StokIndirim=2) then Isk2 when (@StokIndirim=3) then Isk3 end else 0 end as Indirim ,  
    'CurrencyName'= case (@StokIndirim) when 0 then Currency1 when 1 then Currency1 when 2 then Currency2 when 3 then Currency3 end,
    'Rate'= case (@StokIndirim) when 0 then Rate1 when 1 then Rate1 when 2 then Rate2 when 3 then Rate3 end,
    SellNoneStock,Code1,Code2,Code3,Code4,Code5,Code6,Code7,Code8,Code9,Code10,Code11,Code12,Code13,Code14,Code15,WizardId,CampainId,ForceCargo
FROM       ProductListDetails 
WHERE    (UnitId = @UnitId) AND  (ProductId = @ProductId) and (PortalId = @PortalId) 
end




GO

 
/****** Object:  StoredProcedure [dbo].[GetProductDetails5]    Script Date: 8.9.2016 11:58:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[GetProductDetails5]

	(
		@PortalId int,
		@ProductId int,
		@StokIndirim int,
		@LangCode nvarchar(50),
		@StokIskActive bit,
		@UnitId int=0
	)

AS
if @UnitId=0
begin
	SELECT
	Details,ProductId, ProductCode, ProductName,Description, StockQty,KdvRate, Path,MarkId, MarkName,BirimAdi,AlisFiyati,AlisRate, Carpan,PiyasaFiyati,SatisFiyati0,Rate0,Currency0,
    'SatisFiyati'= case (@StokIndirim) when 0 then SatisFiyati1 when 1 then SatisFiyati1 when 2 then SatisFiyati2 when 3 then SatisFiyati3 when 4 then SatisFiyati4 when 5 then SatisFiyati5 end,  case when (@StokIskActive >0 ) then case when (@StokIndirim=0) then 0 when (@StokIndirim=1) then Isk1 when (@StokIndirim=2) then Isk2 when (@StokIndirim=3) then Isk3 when (@StokIndirim=4) then Isk4 when (@StokIndirim=5) then Isk5 end else 0 end as Indirim ,  
    'CurrencyName'= case (@StokIndirim) when 0 then Currency1 when 1 then Currency1 when 2 then Currency2 when 3 then Currency3 when 4 then Currency4 when 5 then Currency5 end,
    'Rate'= case (@StokIndirim) when 0 then Rate1 when 1 then Rate1 when 2 then Rate2 when 3 then Rate3 when 4 then Rate4 when 5 then Rate5 end,
    SellNoneStock,Code1,Code2,Code3,Code4,Code5,Code6,Code7,Code8,Code9,Code10,Code11,Code12,Code13,Code14,Code15,WizardId,CampainId,ForceCargo,Desi,CStartDate,CEndDate,StartDate,EndDate,PiyasaFiyati,SatisFiyati1,SatisFiyati2,SatisFiyati3,SatisFiyati4,SatisFiyati5,Isk1,Isk2,Isk3,Isk4,Isk5,CurrencyP,Currency1,Currency2,Currency3,Currency4,Currency5,RateP,Rate1,Rate2,Rate3,Rate4,Rate5,AuthorId,AuthorName,PublisherId,PublisherName,HizliKargo,EnUcuzUrun,AyniGunTeslim,OzelUrun,IndirimliUrun,SinirliSayidaUrun,FirsatUrunu,YeniUrun,SokFiyatliUrun,HediyeliUrun,ProductSizeOptionId
FROM       ProductListDetails5 
WHERE   (UnitsIsDefault = 1) AND  (ProductId = @ProductId) and (LanguageCode=@LangCode) and (PortalId = @PortalId) 
end
else
begin
SELECT     
	Details,ProductId, ProductCode, ProductName,Description, StockQty,KdvRate, Path,MarkId,MarKName,BirimAdi,AlisFiyati,AlisRate, Carpan,PiyasaFiyati,SatisFiyati0,Rate0,Currency0,
    'SatisFiyati'= case (@StokIndirim) when 0 then SatisFiyati1 when 1 then SatisFiyati1 when 2 then SatisFiyati2 when 3 then SatisFiyati3 when 4 then SatisFiyati4 when 5 then SatisFiyati5 end,  case when (@StokIskActive >0 ) then case when (@StokIndirim=0) then 0 when (@StokIndirim=1) then Isk1 when (@StokIndirim=2) then Isk2 when (@StokIndirim=3) then Isk3 when (@StokIndirim=4) then Isk4 when (@StokIndirim=5) then Isk5 end else 0 end as Indirim ,  
    'CurrencyName'= case (@StokIndirim) when 0 then Currency1 when 1 then Currency1 when 2 then Currency2 when 3 then Currency3 when 4 then Currency4 when 5 then Currency5 end,
    'Rate'= case (@StokIndirim) when 0 then Rate1 when 1 then Rate1 when 2 then Rate2 when 3 then Rate3 when 4 then Rate4 when 5 then Rate5 end,
    SellNoneStock,Code1,Code2,Code3,Code4,Code5,Code6,Code7,Code8,Code9,Code10,Code11,Code12,Code13,Code14,Code15,WizardId,CampainId,ForceCargo,Desi,CStartDate,CEndDate,StartDate,EndDate,PiyasaFiyati,SatisFiyati1,SatisFiyati2,SatisFiyati3,SatisFiyati4,SatisFiyati5,Isk1,Isk2,Isk3,Isk4,Isk5,CurrencyP,Currency1,Currency2,Currency3,Currency4,Currency5,RateP,Rate1,Rate2,Rate3,Rate4,Rate5,AuthorId,AuthorName,PublisherId,PublisherName,HizliKargo,EnUcuzUrun,AyniGunTeslim,OzelUrun,IndirimliUrun,SinirliSayidaUrun,FirsatUrunu,YeniUrun,SokFiyatliUrun,HediyeliUrun,ProductSizeOptionId
FROM       ProductListDetails5 
WHERE    (UnitId = @UnitId) AND  (ProductId = @ProductId)  and (LanguageCode=@LangCode)  and (PortalId = @PortalId) 
end
 
GO

  
/****** Object:  StoredProcedure [dbo].[GetProductDetails5a]    Script Date: 8.9.2016 12:04:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[GetProductDetails5a]

	(
		@PortalId int,
		@ProductId int,
		@StokIndirim int,
		@StokIskActive bit, 
		@UnitId int=0
	)

AS
if @UnitId=0
begin
	SELECT
	Details,ProductId, ProductCode, ProductName,Description, StockQty,KdvRate, Path,MarkId, MarkName,BirimAdi, Carpan,AlisFiyati,AlisRate,PiyasaFiyati,SatisFiyati0,Rate0,Currency0,
    'SatisFiyati'= case (@StokIndirim) when 0 then SatisFiyati1 when 1 then SatisFiyati1 when 2 then SatisFiyati2 when 3 then SatisFiyati3 when 4 then SatisFiyati4 when 5 then SatisFiyati5 end,  case when (@StokIskActive >0 ) then case when (@StokIndirim=0) then 0 when (@StokIndirim=1) then Isk1 when (@StokIndirim=2) then Isk2 when (@StokIndirim=3) then Isk3 when (@StokIndirim=4) then Isk4 when (@StokIndirim=5) then Isk5 end else 0 end as Indirim ,  
    'CurrencyName'= case (@StokIndirim) when 0 then Currency1 when 1 then Currency1 when 2 then Currency2 when 3 then Currency3 when 4 then Currency4 when 5 then Currency5 end,
    'Rate'= case (@StokIndirim) when 0 then Rate1 when 1 then Rate1 when 2 then Rate2 when 3 then Rate3 when 4 then Rate4 when 5 then Rate5 end,
    SellNoneStock,Code1,Code2,Code3,Code4,Code5,Code6,Code7,Code8,Code9,Code10,Code11,Code12,Code13,Code14,Code15,WizardId,CampainId,ForceCargo,Desi,StartDate,EndDate,PiyasaFiyati,SatisFiyati1,SatisFiyati2,SatisFiyati3,SatisFiyati4,SatisFiyati5,Isk1,Isk2,Isk3,Isk4,Isk5,CurrencyP,Currency1,Currency2,Currency3,Currency4,Currency5,RateP,Rate1,Rate2,Rate3,Rate4,Rate5,AuthorId,AuthorName,PublisherId,PublisherName,CatIsActive,MarkIsActive,ProductIsActive,HizliKargo,EnUcuzUrun,AyniGunTeslim,OzelUrun,IndirimliUrun,SinirliSayidaUrun,FirsatUrunu,YeniUrun,SokFiyatliUrun,HediyeliUrun
FROM       ProductListDetails5a 
WHERE   (UnitsIsDefault = 1) AND  (ProductId = @ProductId) and (PortalId = @PortalId)  
end
else
begin
SELECT     
	Details,ProductId, ProductCode, ProductName,Description, StockQty,KdvRate, Path,MarkId, MarKName,BirimAdi, Carpan,AlisFiyati,AlisRate,PiyasaFiyati,SatisFiyati0,Rate0,Currency0,
    'SatisFiyati'= case (@StokIndirim) when 0 then SatisFiyati1 when 1 then SatisFiyati1 when 2 then SatisFiyati2 when 3 then SatisFiyati3 when 4 then SatisFiyati4 when 5 then SatisFiyati5 end,  case when (@StokIskActive >0 ) then case when (@StokIndirim=0) then 0 when (@StokIndirim=1) then Isk1 when (@StokIndirim=2) then Isk2 when (@StokIndirim=3) then Isk3 when (@StokIndirim=4) then Isk4 when (@StokIndirim=5) then Isk5 end else 0 end as Indirim ,  
    'CurrencyName'= case (@StokIndirim) when 0 then Currency1 when 1 then Currency1 when 2 then Currency2 when 3 then Currency3 when 4 then Currency4 when 5 then Currency5 end,
    'Rate'= case (@StokIndirim) when 0 then Rate1 when 1 then Rate1 when 2 then Rate2 when 3 then Rate3 when 4 then Rate4 when 5 then Rate5 end,
    SellNoneStock,Code1,Code2,Code3,Code4,Code5,Code6,Code7,Code8,Code9,Code10,Code11,Code12,Code13,Code14,Code15,WizardId,CampainId,ForceCargo,Desi,StartDate,EndDate,PiyasaFiyati,SatisFiyati1,SatisFiyati2,SatisFiyati3,SatisFiyati4,SatisFiyati5,Isk1,Isk2,Isk3,Isk4,Isk5,CurrencyP,Currency1,Currency2,Currency3,Currency4,Currency5,RateP,Rate1,Rate2,Rate3,Rate4,Rate5,AuthorId,AuthorName,PublisherId,PublisherName,CatIsActive,MarkIsActive,ProductIsActive,HizliKargo,EnUcuzUrun,AyniGunTeslim,OzelUrun,IndirimliUrun,SinirliSayidaUrun,FirsatUrunu,YeniUrun,SokFiyatliUrun,HediyeliUrun
FROM       ProductListDetails5a 
WHERE    (UnitId = @UnitId) AND  (ProductId = @ProductId) and (PortalId = @PortalId) 
end
 
 
GO
  
 
/****** Object:  StoredProcedure [dbo].[OrdersAddN11]    Script Date: 8.9.2016 11:59:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[OrdersAddN11]
(
    @PortalId					int,
    @CartId					nvarchar(50),
    @OrderDate				 datetime,
    @PaymenType				 nvarchar(30),
    @IsPayed				bit,
    @Note					nvarchar(400),
    @ShippingName			nvarchar(100),
    @ShippingPhoneNo		 nvarchar(15),
    @ShippingMobilePhoneNo	 nvarchar(15),
    @ShippingCityName		nvarchar(50),
    @ShippingAddress		 nvarchar(400),
    @ShippingCargoName		 nvarchar(50),
    @BillingName			nvarchar(100),
	@BillingPhoneNo		 nvarchar(15),
    @BillingMobilePhoneNo	 nvarchar(15),
    @BillingTaxOffice		nvarchar(50),
    @BillingTaxNo			nvarchar(20),
    @BillingCityName		nvarchar(50),
    @BillingAddress			nvarchar(400),
    @ToplamTutar			decimal(18,4),
    @KdvDahilToplam			decimal(18,4),
    @ToplamKdv				decimal(18,4),
    @ToplamKargo			decimal(18,4),
    @KargoDahilToplamTutar	decimal(18,4),
    @CekIndirimi	decimal(18,4)=0,
    @ToplamIndirim			decimal(18,4),
    @ToplamAgirlik			decimal(18,4),
    @ToplamHacim			decimal(18,4),
    @ToplamDesi decimal(18,4)=0,
    @ShoppingCartDefId  int=0,
    @BankName				 nvarchar(50),
    @BankId				 int,
    @CardName	nvarchar(50)='',
    @Taksit					 int,
    @BankaToplamTutar		  decimal(18,4),
    @BankPayType			 nvarchar(10),
    @StokIndirim int,  
	@StokIskActive bit,
	@Status int =0,
	@ToplamPuan decimal(18,4),
	@CampainId int=0,
	@Ip varchar(16)='',
	@OrderCode varchar(10)='',
	@OrderPaymentCode nvarchar(50)='',
    @CurrencyRate		  decimal(10, 4),
    @CurrencyCode		  nvarchar(10)='',
    @CustomerCurrencyRate		  decimal(10, 4),
    @CustomerCurrencyCode		  nvarchar(10)='',
    @TaksitRate		  decimal(10, 4),
	@ShippingCountryName		  nvarchar(50)='',
    @ShippingDistrictName		  nvarchar(50)='',
    @BillingCountryName		  nvarchar(50)='',
    @BillingDistrictName		  nvarchar(50)='',
	@ShippingMail nvarchar(100),
    @OrderId				 int OUTPUT
)
AS 
BEGIN TRAN AddOrder

--Create the Order  -
INSERT INTO Orders
(
PortalId		,
CustomerId		,
OrderDate		,
PaymentType		,
IsPayed			,
Note			,	
ShippingName		,	
ShippingPhoneNo		,
ShippingMobilePhone,	
ShippingCityName	,	
ShippingAddress		,
ShippingCargoName	,	
BillingName		,
BillingPhoneNo	,
BillingMobilePhoneNo	,
BillingTaxOffice	,	
BillingTaxNo		,	
BillingCityName		,
BillingAddress		,	
ToplamTutar		,
KdvDahilToplam		,	
ToplamKdv		,	
ToplamKargo		,
KargoDahilToplamTutar	,	
ToplamIndirim		,	
ToplamAgirlik		,	
ToplamHacim		,
ToplamDesi,
BankName		,
BankId			,
CardName,
Taksit			, 
BankaToplamTutar,
BankPayType,
Status,
CekIndirimi,
ToplamPuan,
Ip,
OrderCode,
OrderPaymentCode,
CurrencyRate,
CurrencyCode,
CustomerCurrencyRate,
CustomerCurrencyCode,
TaksitRate,
ShippingCountryName,
ShippingDistrictName,
BillingCountryName,
BillingDistrictName,ShippingMail
 )
VALUES
(   
@PortalId		,
case when (isnumeric(@CartId)=1 and @CartId>0 ) then @CartId else -1 end, 
--@CartId			,
@OrderDate			,
@PaymenType			,
@IsPayed			,
@Note				,
@ShippingName			,
@ShippingPhoneNo		,
@ShippingMobilePhoneNo		,
@ShippingCityName		,
@ShippingAddress		,
@ShippingCargoName		,
@BillingName			,
@BillingPhoneNo		 ,
@BillingMobilePhoneNo	 ,
@BillingTaxOffice		,
@BillingTaxNo			,
@BillingCityName		,
@BillingAddress			,
@ToplamTutar			,
@KdvDahilToplam			,
@ToplamKdv			,
@ToplamKargo			,
@KargoDahilToplamTutar		,
@ToplamIndirim			,
@ToplamAgirlik			,
@ToplamHacim			,
@ToplamDesi,
@BankName			,
@BankId				,
@CardName,
@Taksit				,
@BankaToplamTutar  		,
@BankPayType	,
@Status,
@CekIndirimi,
@ToplamPuan,
@Ip,
@OrderCode,
@OrderPaymentCode,
@CurrencyRate,
@CurrencyCode,
@CustomerCurrencyRate,
@CustomerCurrencyCode,
@TaksitRate,
@ShippingCountryName,
@ShippingDistrictName,
@BillingCountryName,
@BillingDistrictName,@ShippingMail
)

SELECT
    @OrderId = @@Identity   

-- Add N11 Order - Proticaret Orders Table

COMMIT TRAN AddOrder

--ShoppingCartList 1 ,'N' 


GO

 
/****** Object:  StoredProcedure [dbo].[OrdersDetailAddN11]    Script Date: 8.9.2016 12:00:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[OrdersDetailAddN11]
(
    @PortalId					int,
    @CartId					nvarchar(50),
    @OrderDate				 datetime='',
    @Note					nvarchar(400),   
    @ToplamTutar			decimal(18,2),    
    @StokIndirim int,  
	@StokIskActive bit,
	@Status int =0,
	@ProductId int,
	@ProductUnitId int,
	@ProductSizeId int,
	@ProductSizeDescription nvarchar(400),
	@Qty int,	
    @OrderId  int
)
   AS
--Copy items from given shopping cart to OrdersDetail table 
INSERT INTO OrderDetails
(
OrderId, 
ProductId,
ProductCode,
ProductName,
BirimAdi,
Carpan,
SatisFiyati,
Pb1,
Rate,
BirimIndirimOrani,
CariIndirimOrani,
AlisFiyati,
APb,
AlisRate,
Quantity,
Provider,
KdvRate,
MarkName,
Status,
Description,
ProviderId,
XmlProviderId,
UserId,
AltinGram,
ProductSizeId,
ProductBarcode,
ProductSizeBarcode
)
SELECT    @OrderId, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.Products.ProductName, dbo.ProductUnits.BirimAdi, dbo.ProductUnits.Carpan,
'SatisFiyati'= @ToplamTutar, 
 'Pb1'= case (@StokIndirim) when 0 then ProductUnits.Pb1 when 1 then ProductUnits.Pb1 when 2 then ProductUnits.Pb2 when 3 then ProductUnits.Pb3 when 4 then ProductUnits.Pb4 when 5 then ProductUnits.Pb5 end, 
                      Currencys_2.Rate,   
  0  as Indirim ,  
 case when (isnumeric(@CartId)=1) then (select DisAccount from Users where UserId=@CartId) else 0 end, dbo.ProductUnits.AlisFiyati, dbo.ProductUnits.APb, Currencys_1.Rate AS Rate, 
@Qty, dbo.Products.Provider, dbo.ProductKdvs.KdvRate,ProductMarks.MarKName,@Status,@ProductSizeDescription,Products.IsPenta,Products.ProviderId,Products.UserId,isnull(ProductUnits.SatisFiyati5,0),
@ProductSizeId,
ProductUnits.Barcode,
Case When (@ProductSizeId>0) Then (Select Barcode From ProductSizes where ProductSizeId=@ProductSizeId) else '' end as ProductSizeBarcode


from Products INNER JOIN  dbo.ProductUnits on Products.ProductId=ProductUnits.ProductId INNER JOIN
                      dbo.ProductKdvs ON dbo.Products.KdvId = dbo.ProductKdvs.KdvId
					  INNER JOIN
                      dbo.Currencys Currencys_1 ON dbo.ProductUnits.APb = Currencys_1.CurrencyId
                      LEFT OUTER JOIN
                      FCampains ON Products.CampainId = FCampains.FCampainId LEFT OUTER JOIN
                      dbo.ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId
					  INNER JOIN  dbo.Currencys Currencys_2 
					  ON Currencys_2.CurrencyId = case (0) when 0 then ProductUnits.Pb1 when 1 then ProductUnits.Pb1 when 2 then ProductUnits.Pb2 
					  when 3 then ProductUnits.Pb3 when 4 then ProductUnits.Pb4 when 5 then ProductUnits.Pb5 end



					  where Products.ProductId=@ProductId and ProductUnits.UnitId=@ProductUnitId  and 
					  (dbo.Products.IsActive = 1) AND 
(dbo.Products.IsDeleted = 0) AND 
(dbo.Products.StockQty > 0 or Products.SellNoneStock=1) AND 
(dbo.ProductUnits.IsActive = 1)




update OrderDetails set SatisFiyati=CAST((SatisFiyati/Rate) AS DECIMAL(18,4)) where OrderID=@OrderId and ProductId=@ProductId

GO
   
/****** Object:  StoredProcedure [dbo].[ProductListHome]    Script Date: 4.10.2016 10:23:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[ProductListHome]
(
      @CartId nvarchar(50),
      @StokIndirim int,
      @StokIskActive bit,
      @LangCode nvarchar(50), 
      @UserIndirim int =0,
      @CatId int,
      @MarkId int=0,
      @auth int=0,
      @pub int=0,
      @m1 int=0,
      @m2 int=0,
      @year int=0,
	  @SearchKeywords nvarchar(50),
	  @Filter nvarchar(500),  
	  @Top int=300  
)
AS

declare @PriceId  int
declare @ProdId  int
declare @PriceCode  nvarchar(50)
declare @UserFilter nvarchar(500) 
declare @StokFilter nvarchar(500) 

declare @result nvarchar(50)
declare @resultstok nvarchar(50)

CREATE TABLE #TempTable(PriceId int,Price nvarchar(20),DiscountType int,Discount decimal(18,2),Discount2 decimal(18,2),Discount3 decimal(18,2),Discount4 decimal(18,2),Discount5 decimal(18,2),ProductId int primary key not null) 
 

DECLARE CRS_CART CURSOR FOR

select Distinct ProdId from ShoppingCart  where  CartId=@CartId  

OPEN CRS_CART

FETCH NEXT FROM CRS_CART INTO @ProdId  

WHILE @@FETCH_STATUS =0
       BEGIN
 

		DECLARE CRS_USER CURSOR FOR
		
		select UserFilter,PriceId,StokFilter,PriceCode   from PriceList where  Status=1 and StartDate <= getdate() and Enddate >= Getdate() and PriceId<>1 and ApplyOrder<>0  order by ApplyOrder 
		
		OPEN CRS_USER
		
		FETCH NEXT FROM CRS_USER INTO @UserFilter ,@PriceId,@StokFilter,@PriceCode
		
		WHILE @@FETCH_STATUS =0
		       BEGIN
		 
		declare @ExcludedList nvarchar(4000)
		declare @tempCartId int = -1
		
		if ISNUMERIC(@CartId) = 1
		begin 
		set @tempCartId=@CartId 
		end
		
		IF @UserFilter=''
		BEGIN 
		    SET @UserFilter='1=1' 
		END
		 
		
		set @ExcludedList='select  @result =count(UserId) from users where ' + @UserFilter + '  and UserId=' + CONVERT(nvarchar(100),@tempCartId)
		--select @ExcludedList
		EXEC sp_executesql @ExcludedList, N'@result VARCHAR(100) OUTPUT' , @result output
		   
		  
		
		
		declare @StokList nvarchar(4000)
		set @StokList='select  @resultstok =count(ProductId) from AdminProductList where ' + @StokFilter + '  and ProductId=' + CONVERT(nvarchar(50), @ProdId)
		EXEC sp_executesql @StokList, N'@resultstok VARCHAR(100) OUTPUT' , @resultstok output
		    
		if   @result >0 and @resultstok >0 
		begin
		  INSERT INTO #TempTable (PriceId,Price,DiscountType,Discount,Discount2,Discount3,Discount4,Discount5,ProductId)
		  select top 1 PriceId,Price,DiscountType,Discount,Discount2,Discount3,Discount4,Discount5,@ProdId  from PriceList where PriceCode=@PriceCode and  @result >0 and @resultstok >0   order by ApplyOrder 
		  break
		end
             FETCH NEXT FROM CRS_USER INTO @UserFilter,@PriceId,@StokFilter,@PriceCode

       END

       CLOSE CRS_USER
DEALLOCATE CRS_USER



 FETCH NEXT FROM CRS_CART INTO @ProdId

       END

       CLOSE CRS_CART
DEALLOCATE CRS_CART

 
declare @str nvarchar(max)

declare @ShowInMainPage int =1
if @MarkId<>0 or @auth<>0 or @pub<>0  or @SearchKeywords<>'' or @CatId<>0 
begin
	set @Top = 300
	set @ShowInMainPage=-1
end
else 
begin
	set @Top = 50
	set @ShowInMainPage=1
end

set @str = 'SELECT distinct top ' + CONVERT(nvarchar,@Top) + ' pl.StartDate,pl.ProductSizeOptionId,pl.EndDate,pl.PiyasaFiyati,pl.SatisFiyati0,pl.SatisFiyati1,pl.SatisFiyati2,pl.SatisFiyati3,pl.SatisFiyati4, ' + 
	+ '		pl.SatisFiyati5,pl.Isk1,pl.Isk2,pl.Isk3,pl.Isk4,pl.Isk5,pl.CurrencyP,pl.Currency0,pl.Currency1,pl.Currency2,pl.Currency3,pl.Currency4,pl.Currency5,pl.RateP, ' + 
	+ '		pl.Rate0,pl.Rate1,pl.Rate2,pl.Rate3,pl.Rate4,pl.Rate5, pl.CatId,pl.CatName,pl.CatUrl,pl.ProductId,pl.ProductCode,pl.ProductName,pl.StockQty,pl.ShowOrder, ' + 
	+ '		pl.KdvRate,pl.Path,pl.ImageDesc,pl.MarkId,pl.MarKName,pl.BirimAdi,pl.Carpan,pl.ProductUrl,   ' + 
	+ '		''CurrencyName''= CASE (' + convert(nvarchar,@StokIndirim) + ') when 0 then Currency1 when 1 then Currency1 when 2 then Currency2 when 3 then Currency3 when 4 then Currency4 when 5 then Currency5 end,  ' + 
	+ '		''Rate''= CASE (' + convert(nvarchar,@StokIndirim) + ') when 0 then Rate1 when 1 then Rate1 when 2 then Rate2 when 3 then Rate3 when 4 then Rate4 when 5 then Rate5 end,    ' + 
	+ '		pl.SellNoneStock,pl.Code8,pl.Code9,pl.Code10,pl.Code11,pl.Code12,pl.Code13,pl.Code14,pl.Code15,' + convert(nvarchar,@UserIndirim) + ' as ''UserIndirim'', ' + 
	+ '		' + convert(nvarchar,@StokIskActive) + ' as ''StokIskActive'',pl.HizliKargo,pl.EnUcuzUrun,pl.AyniGunTeslim,pl.OzelUrun,pl.IndirimliUrun,pl.SinirliSayidaUrun,pl.FirsatUrunu,pl.YeniUrun,pl.SokFiyatliUrun,pl.HediyeliUrun,  ' + 
	+ '		isnull(Product3dPictures.Url,'''') as Url,isnull(0,0) as prodid,(select top 1 Path from ProductImages where ProductId=pl.ProductId and IsDefault=0) as Url2, ' + 
	+ '		(SELECT isnull(avg(Rating),0) FROM Reviews  WHERE   ProdId = pl.ProductId and IsActive=1) as Review, ' + 
	+ '		''SatisFiyati''=CASE WHEN  Temp.PriceId IS NULL THEN  ' + 
	+ '					 (case (' + convert(nvarchar,@StokIndirim) + ') WHEN 0 then SatisFiyati1 WHEN 1 THEN SatisFiyati1 WHEN 2 THEN SatisFiyati2 WHEN 3 THEN SatisFiyati3 WHEN 4 THEN SatisFiyati4 WHEN 5 THEN SatisFiyati5 END ) ' + 
	+ '				 ELSE  ' + 
	+ '					  (CASE (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))) WHEN 0 THEN dbo.CalcPrice5(SatisFiyati1,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5)  ' + 
	+ '					   WHEN 1 THEN dbo.CalcPrice5(SatisFiyati1,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5)  ' + 
	+ '					   WHEN 2 THEN dbo.CalcPrice5(SatisFiyati2,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5)  ' + 
	+ '					   WHEN 3 THEN dbo.CalcPrice5(SatisFiyati3,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5)  ' + 
	+ '					   WHEN 4 THEN dbo.CalcPrice5(SatisFiyati4,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5)  ' + 
	+ '					   WHEN 5 THEN dbo.CalcPrice5(SatisFiyati5,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) END ) ' + 
	+ '				 END, ' + 
	+ '				 ''Indirim''= CASE WHEN  Temp.PriceId IS NULL THEN ' + 
	+ '					 (CASE WHEN (' + convert(nvarchar,@StokIskActive) + ' >0 ) THEN CASE WHEN (' + convert(nvarchar,@StokIndirim) + '=0) THEN 0 WHEN (' + convert(nvarchar,@StokIndirim) + '=1) THEN Isk1 WHEN (' + convert(nvarchar,@StokIndirim) + '=2) THEN Isk2 WHEN (' + convert(nvarchar,@StokIndirim) + '=3) THEN Isk3 WHEN (' + convert(nvarchar,@StokIndirim) + '=4) THEN Isk4 WHEN (' + convert(nvarchar,@StokIndirim) + '=5) THEN Isk5 END ELSE 0 END) ' + 
	+ '				 ELSE  ' + 
	+ '					 (CASE WHEN (' + convert(nvarchar,@StokIskActive) + ' >0 ) THEN CASE WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=0) THEN 0 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=1) THEN Isk1 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=2) THEN Isk2 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=3) THEN Isk3 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=4) THEN Isk4 WHEN (convert(int,Replace(Temp.Price,''SatisFiyati'',''''))=5) THEN Isk5 END ELSE 0 END) ' + 
	+ '				 END ' + 
	+ '		FROM  ' +  
	+ '		ProductList pl LEFT OUTER JOIN  ' + 
	+ '		#TempTable AS Temp ON Temp.ProductId = pl.ProductId LEFT OUTER JOIN  ' + 
	+ '		Product3dPictures ON pl.ProductId = 0  LEFT OUTER JOIN  ' + 
	+ '		MotorProductDesc as m ON pl.ProductId = m.ProductId   '    

	if @SearchKeywords <> '' 
		set @str= @str + 'INNER JOIN  dbo.SplitString(''' + @SearchKeywords + ''','' '') as ss ON ((pl.ProductName  like ''%'' + ss.Name + ''%'' COLLATE SQL_Latin1_General_CP1_CI_AI or pl.SearchKeywords ' +
		' like ''%'' + ss.Name + ''%'' COLLATE SQL_Latin1_General_CP1_CI_AI) or    (pl.ProductCode  like ''%'' + ss.Name + ''%'' COLLATE SQL_Latin1_General_CP1_CI_AI or pl.SearchKeywords  like ''%'' + ss.Name + ''%'' COLLATE SQL_Latin1_General_CP1_CI_AI)) '


	set @str= @str + '     WHERE pl.ShowInMainPage = case ' + convert(nvarchar,@ShowInMainPage) + ' when 1 then 1 else ShowInMainPage  end   and  pl.LanguageCode=''' + @LangCode + ''' ' + @Filter  
	IF @CatId >0
		set @str=@str + ' and pl.CatId=' + convert(nvarchar,@CatId) 
		 
	if @m1 > 0
		set @str = @str + ' and ISNULL(m.MarkId,0)= ' + convert(nvarchar,@m1)

	if @m2 > 0
		set @str = @str + ' and ISNULL(m.ModelId,0)= ' + convert(nvarchar,@m2)

	if @year > 0
		set @str = @str + ' and ISNULL(m.YearId,0)= '	+ convert(nvarchar,@year)

	if @MarkId > 0
		set @str = @str + ' and ISNULL(pl.MarkId,0)= '	+ convert(nvarchar,@MarkId)

	if @auth > 0
		set @str = @str + ' and ISNULL(pl.AuthorId,0)= '	+ convert(nvarchar,@auth)

	if @pub > 0
		set @str = @str + ' and ISNULL(pl.PublisherId,0)= '	+ convert(nvarchar,@pub)

	EXEC sp_executesql  @str, N'@result VARCHAR(100) OUTPUT' , @result output 
 

DROP TABLE #TempTable





GO

 
/****** Object:  StoredProcedure [dbo].[ShoppingCartList5]    Script Date: 8.9.2016 12:00:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[ShoppingCartList5]
(
    @CartId nvarchar(50),
    @ShoppingCartDefId int,
    @StokIndirim int,
    @StokIskActive bit,
	@LangCode nvarchar(50),
    @CampainId int 
)
AS



CREATE TABLE #TempTable(PriceId int,Price nvarchar(20),DiscountType int,Discount decimal(18,2),Discount2 decimal(18,2),Discount3 decimal(18,2),Discount4 decimal(18,2),Discount5 decimal(18,2),ProductId int) 

INSERT #TempTable(PriceId,Price,DiscountType,Discount,Discount2,Discount3,Discount4,Discount5,ProductId)

EXEC dbo.ShoppingCartPriceList @CartId,0



if @CampainId>0
Begin

SELECT   Distinct dbo.ShoppingCart.RecordId,dbo.ShoppingCart.CartTimeOut,dbo.ShoppingCart.CartId, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.ProductDescription.ProductName, dbo.Products.StockQty, 
		 dbo.ProductKdvs.KdvRate,ProductUnits.UnitId, dbo.ProductUnits.BirimAdi, dbo.ProductUnits.Carpan,dbo.ShoppingCart.Qty, dbo.ProductUnits.Agirlik, dbo.ProductUnits.Hacim, 
		 dbo.Products.Provider,dbo.ProductUnits.Desi,ProductMarks.MarKName,Products.StockQty -(dbo.ProductUnits.Carpan*dbo.ShoppingCart.Qty) as Kalan,
		 Products.Url,ISNULL((SELECT Path FROM ProductImages AS p2 WHERE p2.ImageId=ProductSizes.ProductImageId ),Path) AS 'Path',dbo.ShoppingCart.Description,dbo.ShoppingCart.Attributes,dbo.ShoppingCart.ValueAdd,dbo.ShoppingCart.ValueAddNoQty,dbo.ShoppingCart.NewDesi, 
         Currencys.Rate AS Rate0, ProductUnits.SatisFiyati0,Products.ForceCargo,ProductUnits.AlisFiyati,ProductUnits.Barcode,ProductSizes.Barcode as ProductSizeBarcode, Currencys_2.CurrencyName, Currencys_2.Rate,
		 'SatisFiyati'=CASE 
		 WHEN  (CASE   WHEN (m.IsActive=1 and (m.StartQty <= ShoppingCart.Qty ) and (m.EndQty >= Qty ) and m.UnitName=BirimAdi) THEN m.UnitPrice  END) is not   null  THEN m.UnitPrice 
		 WHEN  Temp.PriceId IS NULL THEN 
			 (case (@StokIndirim) WHEN 0 then SatisFiyati1 WHEN 1 THEN SatisFiyati1 WHEN 2 THEN SatisFiyati2 WHEN 3 THEN SatisFiyati3 WHEN 4 THEN SatisFiyati4 WHEN 5 THEN SatisFiyati5 END )
		 ELSE 
			 (CASE (convert(int,Replace(Temp.Price,'SatisFiyati',''))) WHEN 0 THEN dbo.CalcPrice5(SatisFiyati1,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 1 THEN dbo.CalcPrice5(SatisFiyati1,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 2 THEN dbo.CalcPrice5(SatisFiyati2,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 3 THEN dbo.CalcPrice5(SatisFiyati3,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 4 THEN dbo.CalcPrice5(SatisFiyati4,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 5 THEN dbo.CalcPrice5(SatisFiyati5,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) END )
		 END,
		 'Indirim'= CASE WHEN  Temp.PriceId IS NULL THEN
			 (CASE WHEN (@StokIskActive >0 ) THEN CASE WHEN (@StokIndirim=0) THEN 0 WHEN (@StokIndirim=1) THEN Isk1 WHEN (@StokIndirim=2) THEN Isk2 WHEN (@StokIndirim=3) THEN Isk3 WHEN (@StokIndirim=4) THEN Isk4 WHEN (@StokIndirim=5) THEN Isk5 END ELSE 0 END)
		 ELSE 
			 (CASE WHEN (@StokIskActive >0 ) THEN CASE WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=0) THEN 0 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=1) THEN Isk1 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=2) THEN Isk2 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=3) THEN Isk3 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=4) THEN Isk4 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=5) THEN Isk5 END ELSE 0 END)
		 END


FROM    ShoppingCart INNER JOIN 
		Products ON dbo.Products.ProductId = dbo.ShoppingCart.ProdId INNER JOIN
		ProductUnits ON dbo.ProductUnits.UnitId = dbo.ShoppingCart.PriceId INNER JOIN
		ProductKdvs  ON dbo.Products.KdvId = dbo.ProductKdvs.KdvId INNER JOIN
		ProductDescription ON ProductDescription.ProductId = Products.ProductId  INNER JOIN  
		ProductImages ON Products.ProductId=ProductImages.ProductId LEFT OUTER JOIN 
		ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId LEFT OUTER JOIN 
		ProductSizes ON ShoppingCart.ProductSizeId=ProductSizes.ProductSizeId INNER JOIN
		Currencys ON ProductUnits.Pb0 = Currencys.CurrencyId  LEFT OUTER JOIN 
		MultiDiscountList as m ON m.ProductId = dbo.ProductUnits.ProductId  AND m.UnitName = dbo.ProductUnits.BirimAdi and ShoppingCart.Qty>=m.StartQty  and ShoppingCart.Qty<=m.EndQty  LEFT OUTER JOIN 
		#TempTable AS Temp ON Temp.ProductId = ProductUnits.ProductId  INNER JOIN
		dbo.Currencys Currencys_2 ON Currencys_2.CurrencyId = 
		CASE WHEN  Temp.PriceId IS NULL THEN 
			CASE (@StokIndirim) WHEN 0 THEN ProductUnits.Pb1 WHEN 1 THEN ProductUnits.Pb1 WHEN 2 THEN ProductUnits.Pb2 WHEN 3 THEN ProductUnits.Pb3 WHEN 4 THEN ProductUnits.Pb4 WHEN 5 THEN ProductUnits.Pb5 END
		ELSE
			CASE (convert(int,Replace(Temp.Price,'SatisFiyati',''))) WHEN 0 THEN ProductUnits.Pb1 WHEN 1 THEN ProductUnits.Pb1 WHEN 2 THEN ProductUnits.Pb2 WHEN 3 THEN ProductUnits.Pb3 WHEN 4 THEN ProductUnits.Pb4 WHEN 5 THEN ProductUnits.Pb5 END
		END


WHERE    (ProductImages.IsDefault=1) AND
 (dbo.Products.IsActive = 1) AND 
(dbo.Products.IsDeleted = 0) AND 
(dbo.Products.StockQty > 0 OR dbo.Products.SellNoneStock =1 ) AND 
(dbo.ProductUnits.IsActive = 1) AND
(dbo.ShoppingCart.ShoppingCartDefId =@ShoppingCartDefId) AND
(dbo.ShoppingCart.CartId = @CartId) AND (Products.CampainId=@CampainId ) AND (ProductDescription.LanguageCode=@LangCode) 

end
else if @CampainId=0
Begin

SELECT   Distinct dbo.ShoppingCart.RecordId,dbo.ShoppingCart.CartTimeOut,dbo.ShoppingCart.CartId, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.ProductDescription.ProductName, dbo.Products.StockQty, 
		 dbo.ProductKdvs.KdvRate,ProductUnits.UnitId, dbo.ProductUnits.BirimAdi, dbo.ProductUnits.Carpan,dbo.ShoppingCart.Qty, dbo.ProductUnits.Agirlik, dbo.ProductUnits.Hacim, 
		 dbo.Products.Provider,dbo.ProductUnits.Desi,ProductMarks.MarKName,Products.StockQty -(dbo.ProductUnits.Carpan*dbo.ShoppingCart.Qty) as Kalan,
		 Products.Url,ISNULL((SELECT Path FROM ProductImages AS p2 WHERE p2.ImageId=ProductSizes.ProductImageId ),Path) AS 'Path',dbo.ShoppingCart.Description,dbo.ShoppingCart.Attributes,dbo.ShoppingCart.ValueAdd,dbo.ShoppingCart.ValueAddNoQty,dbo.ShoppingCart.NewDesi, 
         Currencys.Rate AS Rate0, ProductUnits.SatisFiyati0,Products.ForceCargo,ProductUnits.AlisFiyati,ProductUnits.Barcode,ProductSizes.Barcode as ProductSizeBarcode, Currencys_2.CurrencyName, Currencys_2.Rate,
		 'SatisFiyati'=CASE
		 WHEN  (CASE   WHEN (m.IsActive=1 and (m.StartQty <= ShoppingCart.Qty ) and (m.EndQty >= Qty ) and m.UnitName=BirimAdi) THEN m.UnitPrice  END) is not   null  THEN m.UnitPrice 
		 WHEN  Temp.PriceId IS NULL THEN 
			 (CASE (@StokIndirim) WHEN 0 then SatisFiyati1 WHEN 1 THEN SatisFiyati1 WHEN 2 THEN SatisFiyati2 WHEN 3 THEN SatisFiyati3 WHEN 4 THEN SatisFiyati4 WHEN 5 THEN SatisFiyati5 END )
		 ELSE 
			 (CASE (convert(int,Replace(Temp.Price,'SatisFiyati',''))) WHEN 0 THEN dbo.CalcPrice5(SatisFiyati1,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 1 THEN dbo.CalcPrice5(SatisFiyati1,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 2 THEN dbo.CalcPrice5(SatisFiyati2,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 3 THEN dbo.CalcPrice5(SatisFiyati3,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 4 THEN dbo.CalcPrice5(SatisFiyati4,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 5 THEN dbo.CalcPrice5(SatisFiyati5,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) END )
		 END,
		 'Indirim'= CASE WHEN  Temp.PriceId IS NULL THEN
			 (CASE WHEN (@StokIskActive >0 ) THEN CASE WHEN (@StokIndirim=0) THEN 0 WHEN (@StokIndirim=1) THEN Isk1 WHEN (@StokIndirim=2) THEN Isk2 WHEN (@StokIndirim=3) THEN Isk3 WHEN (@StokIndirim=4) THEN Isk4 WHEN (@StokIndirim=5) THEN Isk5 END ELSE 0 END)
		 ELSE 
			 (CASE WHEN (@StokIskActive >0 ) THEN CASE WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=0) THEN 0 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=1) THEN Isk1 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=2) THEN Isk2 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=3) THEN Isk3 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=4) THEN Isk4 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=5) THEN Isk5 END ELSE 0 END)
		 END


FROM    ShoppingCart INNER JOIN 
		Products ON dbo.Products.ProductId = dbo.ShoppingCart.ProdId INNER JOIN
		ProductUnits ON dbo.ProductUnits.UnitId = dbo.ShoppingCart.PriceId INNER JOIN
		ProductKdvs  ON dbo.Products.KdvId = dbo.ProductKdvs.KdvId INNER JOIN
		ProductDescription ON ProductDescription.ProductId = Products.ProductId  INNER JOIN  
		ProductImages ON Products.ProductId=ProductImages.ProductId LEFT OUTER JOIN
        FCampains ON Products.CampainId = FCampains.FCampainId LEFT OUTER JOIN
		ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId LEFT OUTER JOIN 
		ProductSizes ON ShoppingCart.ProductSizeId=ProductSizes.ProductSizeId INNER JOIN
		Currencys ON ProductUnits.Pb0 = Currencys.CurrencyId  LEFT OUTER JOIN 
		MultiDiscountList as m ON m.ProductId = dbo.ProductUnits.ProductId  AND m.UnitName = dbo.ProductUnits.BirimAdi and ShoppingCart.Qty>=m.StartQty  and ShoppingCart.Qty<=m.EndQty  LEFT OUTER JOIN 
		#TempTable AS Temp ON Temp.ProductId = ProductUnits.ProductId  INNER JOIN
		dbo.Currencys Currencys_2 ON Currencys_2.CurrencyId = 
		CASE WHEN  Temp.PriceId IS NULL THEN 
			CASE (@StokIndirim) WHEN 0 THEN ProductUnits.Pb1 WHEN 1 THEN ProductUnits.Pb1 WHEN 2 THEN ProductUnits.Pb2 WHEN 3 THEN ProductUnits.Pb3 WHEN 4 THEN ProductUnits.Pb4 WHEN 5 THEN ProductUnits.Pb5 END
		ELSE
			CASE (convert(int,Replace(Temp.Price,'SatisFiyati',''))) WHEN 0 THEN ProductUnits.Pb1 WHEN 1 THEN ProductUnits.Pb1 WHEN 2 THEN ProductUnits.Pb2 WHEN 3 THEN ProductUnits.Pb3 WHEN 4 THEN ProductUnits.Pb4 WHEN 5 THEN ProductUnits.Pb5 END
		END


WHERE    (ProductImages.IsDefault=1) AND
 (dbo.Products.IsActive = 1) AND 
(dbo.Products.IsDeleted = 0) AND 
(dbo.Products.StockQty > 0 OR dbo.Products.SellNoneStock =1 ) AND 
(dbo.ProductUnits.IsActive = 1) AND
(dbo.ShoppingCart.ShoppingCartDefId =@ShoppingCartDefId) AND
(dbo.ShoppingCart.CartId = @CartId)  AND 
((ISNULL(FCampains.FCampainId, 0) = 0) or (FCampains.IsActive=0) or (FCampains.StartDate > getdate()) or (FCampains.EndDate < Getdate()) ) AND (ProductDescription.LanguageCode=@LangCode) 

end
else
Begin
SELECT   Distinct dbo.ShoppingCart.RecordId,dbo.ShoppingCart.CartTimeOut,dbo.ShoppingCart.CartId, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.ProductDescription.ProductName, dbo.Products.StockQty, 
		 dbo.ProductKdvs.KdvRate,ProductUnits.UnitId, dbo.ProductUnits.BirimAdi, dbo.ProductUnits.Carpan,dbo.ShoppingCart.Qty, dbo.ProductUnits.Agirlik, dbo.ProductUnits.Hacim, 
		 dbo.Products.Provider,dbo.ProductUnits.Desi,ProductMarks.MarKName,Products.StockQty -(dbo.ProductUnits.Carpan*dbo.ShoppingCart.Qty) as Kalan,
		 Products.Url,ISNULL((SELECT Path FROM ProductImages AS p2 WHERE p2.ImageId=ProductSizes.ProductImageId ),Path) AS 'Path',dbo.ShoppingCart.Description,dbo.ShoppingCart.Attributes,dbo.ShoppingCart.ValueAdd,dbo.ShoppingCart.ValueAddNoQty,dbo.ShoppingCart.NewDesi, 
         Currencys.Rate AS Rate0, ProductUnits.SatisFiyati0,Products.ForceCargo,ProductUnits.AlisFiyati,ProductUnits.Barcode,ProductSizes.Barcode as ProductSizeBarcode, Currencys_2.CurrencyName, Currencys_2.Rate,
		 'SatisFiyati'=CASE WHEN  Temp.PriceId IS NULL THEN 
			 (case (@StokIndirim) WHEN 0 then SatisFiyati1 WHEN 1 THEN SatisFiyati1 WHEN 2 THEN SatisFiyati2 WHEN 3 THEN SatisFiyati3 WHEN 4 THEN SatisFiyati4 WHEN 5 THEN SatisFiyati5 END )
		 ELSE 
			 (CASE (convert(int,Replace(Temp.Price,'SatisFiyati',''))) WHEN 0 THEN dbo.CalcPrice5(SatisFiyati1,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 1 THEN dbo.CalcPrice5(SatisFiyati1,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 2 THEN dbo.CalcPrice5(SatisFiyati2,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 3 THEN dbo.CalcPrice5(SatisFiyati3,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 4 THEN dbo.CalcPrice5(SatisFiyati4,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) 
			 WHEN 5 THEN dbo.CalcPrice5(SatisFiyati5,Temp.DiscountType,Temp.Discount,Temp.Discount2,Temp.Discount3,Temp.Discount4,Temp.Discount5) END )
		 END,
		 'Indirim'= CASE WHEN  Temp.PriceId IS NULL THEN
			 (CASE WHEN (@StokIskActive >0 ) THEN CASE WHEN (@StokIndirim=0) THEN 0 WHEN (@StokIndirim=1) THEN Isk1 WHEN (@StokIndirim=2) THEN Isk2 WHEN (@StokIndirim=3) THEN Isk3 WHEN (@StokIndirim=4) THEN Isk4 WHEN (@StokIndirim=5) THEN Isk5 END ELSE 0 END)
		 ELSE 
			 (CASE WHEN (@StokIskActive >0 ) THEN CASE WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=0) THEN 0 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=1) THEN Isk1 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=2) THEN Isk2 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=3) THEN Isk3 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=4) THEN Isk4 WHEN (convert(int,Replace(Temp.Price,'SatisFiyati',''))=5) THEN Isk5 END ELSE 0 END)
		 END


FROM    ShoppingCart INNER JOIN 
		Products ON dbo.Products.ProductId = dbo.ShoppingCart.ProdId INNER JOIN
		ProductUnits ON dbo.ProductUnits.UnitId = dbo.ShoppingCart.PriceId INNER JOIN
		ProductKdvs  ON dbo.Products.KdvId = dbo.ProductKdvs.KdvId INNER JOIN
		ProductDescription ON ProductDescription.ProductId = Products.ProductId  INNER JOIN  
		ProductImages ON Products.ProductId=ProductImages.ProductId LEFT OUTER JOIN
        FCampains ON Products.CampainId = FCampains.FCampainId LEFT OUTER JOIN
		ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId LEFT OUTER JOIN 
		ProductSizes ON ShoppingCart.ProductSizeId=ProductSizes.ProductSizeId INNER JOIN
		Currencys ON ProductUnits.Pb0 = Currencys.CurrencyId  LEFT OUTER JOIN 
		#TempTable AS Temp ON Temp.ProductId = ProductUnits.ProductId  INNER JOIN
		dbo.Currencys Currencys_2 ON Currencys_2.CurrencyId = 
		CASE WHEN  Temp.PriceId IS NULL THEN 
			CASE (@StokIndirim) WHEN 0 THEN ProductUnits.Pb1 WHEN 1 THEN ProductUnits.Pb1 WHEN 2 THEN ProductUnits.Pb2 WHEN 3 THEN ProductUnits.Pb3 WHEN 4 THEN ProductUnits.Pb4 WHEN 5 THEN ProductUnits.Pb5 END
		ELSE
			CASE (convert(int,Replace(Temp.Price,'SatisFiyati',''))) WHEN 0 THEN ProductUnits.Pb1 WHEN 1 THEN ProductUnits.Pb1 WHEN 2 THEN ProductUnits.Pb2 WHEN 3 THEN ProductUnits.Pb3 WHEN 4 THEN ProductUnits.Pb4 WHEN 5 THEN ProductUnits.Pb5 END
		END


WHERE    (ProductImages.IsDefault=1) AND
 (dbo.Products.IsActive = 1) AND 
(dbo.Products.IsDeleted = 0) AND 
(dbo.Products.StockQty > 0 OR dbo.Products.SellNoneStock =1 ) AND 
(dbo.ProductUnits.IsActive = 1) AND
(dbo.ShoppingCart.ShoppingCartDefId =@ShoppingCartDefId) AND
(dbo.ShoppingCart.CartId = @CartId) AND (ProductDescription.LanguageCode=@LangCode) 
end 

GO

 
/****** Object:  StoredProcedure [dbo].[ShoppingCartMigrate]    Script Date: 8.9.2016 12:01:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[ShoppingCartMigrate]
(
    @OriginalCartId nvarchar(50),
    @NewCartId      nvarchar(50)
)
AS

declare @ProdId int
declare @PriceId int
declare @Qty decimal(18,4)
declare @Description varchar(500)
declare @Attributes varchar(255)
declare @ValueAdd decimal(18,4)
declare @ValueAddNoQty decimal(18,4)
declare @NewDesi decimal(18,2)
declare @ProductSizeId int
declare @CartTimeOut datetime
declare @StorageId int

DECLARE c_cursor CURSOR FOR 
   SELECT ProdId,PriceId,Qty,Description,Attributes,ValueAdd,ValueAddNoQty,NewDesi,ProductSizeId,CartTimeOut,StorageId FROM ShoppingCart  WHERE CartId=@OriginalCartId

   OPEN c_cursor
   FETCH NEXT FROM c_cursor INTO @ProdId,@PriceId,@Qty,@Description,@Attributes,@ValueAdd,@ValueAddNoQty,@NewDesi,@ProductSizeId,@CartTimeOut,@StorageId
 
   WHILE @@FETCH_STATUS = 0
   BEGIN
   declare @RecordId int
   declare @Return bit
	exec ShoppingCartAddItem @NewCartId,0,@ProdId,@Qty,@PriceId,@Description,@Attributes,@ValueAdd,@ValueAddNoQty,@NewDesi,@ProductSizeId,@StorageId,@CartTimeOut,@Return
	
      FETCH NEXT FROM c_cursor INTO @ProdId,@PriceId,@Qty,@Description,@Attributes,@ValueAdd,@ValueAddNoQty,@NewDesi,@ProductSizeId,@CartTimeOut,@StorageId
   END
   
   CLOSE c_cursor
   DEALLOCATE c_cursor
   
DELETE FROM ShoppingCart
WHERE CartId = @OriginalCartId and ShoppingCartDefId=0 and CartId<> @NewCartId   


Update FlowerDetails set CartId=@NewCartId where  CartId = @OriginalCartId 
--exec ShoppingCartEmpty @OriginalCartId,0

GO

  
/****** Object:  StoredProcedure [dbo].[ShoppingCartPriceList]    Script Date: 28.9.2016 10:52:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ShoppingCartPriceList] 
    @CartId nvarchar(50), 
	@Result2 int output
AS
 
declare @PriceId  int
declare @ProdId  int
declare @PriceCode  nvarchar(50)
declare @UserFilter nvarchar(500) 
declare @StokFilter nvarchar(500) 

declare @result nvarchar(50)
declare @resultstok nvarchar(50)
 

CREATE TABLE #TempTable(PriceId int,Price nvarchar(20),DiscountType int,Discount decimal(18,2),Discount2 decimal(18,2),Discount3 decimal(18,2),Discount4 decimal(18,2),Discount5 decimal(18,2),ProductId int) 



DECLARE CRS_CART CURSOR FOR

select Distinct ProdId from ShoppingCart  where  CartId=@CartId  

OPEN CRS_CART

FETCH NEXT FROM CRS_CART INTO @ProdId  

WHILE @@FETCH_STATUS =0
       BEGIN
 

DECLARE CRS_USER CURSOR FOR

select UserFilter,PriceId,StokFilter,PriceCode   from PriceList where  Status=1 and StartDate <= getdate() and Enddate >= Getdate() and PriceId<>1 and ApplyOrder<>0  order by ApplyOrder 

OPEN CRS_USER

FETCH NEXT FROM CRS_USER INTO @UserFilter ,@PriceId,@StokFilter,@PriceCode

WHILE @@FETCH_STATUS =0
       BEGIN
 
declare @ExcludedList nvarchar(4000)
declare @tempCartId int = -1

if ISNUMERIC(@CartId) = 1
begin 
set @tempCartId=@CartId 
end

	
IF @UserFilter=''
BEGIN 
    SET @UserFilter='1=1' 
END

set @ExcludedList='select  @result =count(UserId) from users where ' + @UserFilter + '  and UserId=' + CONVERT(nvarchar(100),@tempCartId  )
EXEC sp_executesql @ExcludedList, N'@result VARCHAR(100) OUTPUT' , @result output
   
  


declare @StokList nvarchar(4000)
set @StokList='select  @resultstok =count(ProductId) from AdminProductList where ' + @StokFilter + '  and ProductId=' + CONVERT(nvarchar(50), @ProdId  )
EXEC sp_executesql @StokList, N'@resultstok VARCHAR(100) OUTPUT' , @resultstok output
    
if   @result >0 and @resultstok >0 
begin
  INSERT INTO #TempTable (PriceId,Price,DiscountType,Discount,Discount2,Discount3,Discount4,Discount5,ProductId)
  select top 1 PriceId,Price,DiscountType,Discount,Discount2,Discount3,Discount4,Discount5,@ProdId  from PriceList where PriceCode=@PriceCode and  @result >0 and @resultstok >0   order by ApplyOrder 
  break
end
             FETCH NEXT FROM CRS_USER INTO @UserFilter,@PriceId,@StokFilter,@PriceCode

       END

       CLOSE CRS_USER
DEALLOCATE CRS_USER



 FETCH NEXT FROM CRS_CART INTO @ProdId

       END

       CLOSE CRS_CART
DEALLOCATE CRS_CART

select * from  #TempTable 
 
drop table #TempTable 


GO

/****** Object:  UserDefinedFunction [dbo].[CalcYuzdeIndirim]    Script Date: 8.9.2016 12:01:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- Function
ALTER FUNCTION [dbo].[CalcYuzdeIndirim] 
	(
	@BigValue decimal(18,4)=0,
	@SmallValue decimal(18,4)=0
	)
RETURNS decimal(18,4)
AS

BEGIN
declare @retvalue decimal(18,4)
SET @retvalue = @BigValue * (100 - @SmallValue) / 100                                         --  ((@SmallValue - @BigValue) / @BigValue * 100) * (-1)
return @retvalue
END

GO
  
UPDATE Portals Set Version='3.3.1'
 
BEGIN TRY

   SET IDENTITY_INSERT [dbo].[ProductStokType] ON 
   INSERT INTO [dbo].[ProductStokType] (StokTypeId,StokTypeName, StokTypeUrl) VALUES (11,N'Dijital','')
   INSERT INTO [dbo].[ProductStokType] (StokTypeId,StokTypeName, StokTypeUrl) VALUES (12,N'Matbaa-Baskı','')
   SET IDENTITY_INSERT [dbo].[ProductStokType] OFF

   ALTER TABLE [dbo].[ProductSizeOptionsValues] DROP CONSTRAINT [DF_ProductSizeOptionsValues_NebimRenkKodu]
   ALTER TABLE [dbo].[ProductSizeOptionsValues]  Alter Column NebimRenkKodu nvarchar(50)
   ALTER TABLE [dbo].[ProductSizeOptionsValues] ADD  CONSTRAINT [DF_ProductSizeOptionsValues_NebimRenkKodu]  DEFAULT ((-1)) FOR [NebimRenkKodu]
 
    
   SET IDENTITY_INSERT [dbo].[S_BillFields] ON 
    
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (1, N'Düzenleme Tarihi', NULL, 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (2, N'Düzenleme Saati', NULL, 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (3, N'Fiili Sevk Tarihi', NULL, 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (4, N'Fatura Adı', N'BillingName', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (5, N'Fatura Adresi', N'BilllingAddress', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (6, N'Fatura İl / İlçe', N'BillingCityName', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (7, N'Fatura Gsm', N'BillingMobilePhoneNo', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (8, N'Teslimat Adı', N'ShippingName', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (9, N'Teslimat Adresi', N'ShippingAddress', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (10, N'Teslimat İl / İlçe', N'ShippingCityName', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (11, N'Teslimat Gsm', N'ShippingMobilePhone', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (12, N'Vergi Dairesi', N'BillingTaxOffice', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (13, N'Vergi No / TC No', N'BillingTaxNo', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (14, N'Stok Kodu', N'ProductCode', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (15, N'Ürün Adı', N'ProductName', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (16, N'Miktar', N'Quantity', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (17, N'Birim Fiyat', N'SatisFiyati1', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (18, N'Fiyat', N'SatisFiyati1', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (19, N'KDV', N'KdvRate', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (21, N'Sipariş No', N'OrderId', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (23, N'Ara Toplam', N'ToplamTutar', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (24, N'KDV Toplam', N'ToplamKdv', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (25, N'Genel Toplam', N'KargoDahilToplamTutar', 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (26, N'Yazı ile Genel Toplam', NULL, 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (27, N'KDV Toplam (%8)', NULL, 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (29, N'KDV Toplam (%18)', NULL, 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (30, N'KDV Yazısı (%8)', NULL, 1, NULL, NULL)
   INSERT [dbo].[S_BillFields] ([FaturaAlanId], [Baslik], [VtAdi], [Durum], [DuzenlemeTarihi], [EklenmeTarihi]) VALUES (31, N'KDV Yazısı (%18)', NULL, 1, NULL, NULL)
    
   SET IDENTITY_INSERT [dbo].[S_BillFields] OFF
    


END TRY
BEGIN CATCH
     
END CATCH
 

 