 
/****** Object:  Table [dbo].[AlternativeMarketsCatVariantSpec]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AlternativeMarketsCatVariantSpec]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[AlternativeMarketsCatVariantSpec](
	[Ind] [int] IDENTITY(1,1) NOT NULL,
	[CatVariantSpecName] [nvarchar](150) NULL,
	[CatVariantSpecNameId] [nvarchar](1500) NULL,
	[CatVariantSpecOrderNumber] [int] NULL,
	[CatVariantSpecValue] [nvarchar](150) NULL,
	[CatVariantSpecValueId] [nvarchar](1500) NULL,
	[CatId] [nvarchar](1500) NULL,
	[CatName] [nvarchar](1500) NULL,
 CONSTRAINT [PK_AlternativeMarketsCatVariantSpec3] 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].[AlternativeMarketsCatVariantSpecRelations]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AlternativeMarketsCatVariantSpecRelations]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[AlternativeMarketsCatVariantSpecRelations](
	[Ind] [int] IDENTITY(1,1) NOT NULL,
	[CatVariantSpecId] [int] NULL,
	[CatVariantSpecName] [nvarchar](150) NULL,
	[CatVariantSpecValue] [nvarchar](150) NULL,
	[ProductSizeOptionsValueId] [int] NULL,
	[ProductSizeOptionsValueName] [nvarchar](500) NULL,
	[ProductSizeOptionsValueNameId] [int] NULL,
 CONSTRAINT [PK_AlternativeMarketsCatVariantSpecRelations] 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].[CategoryDescription]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CategoryDescription]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[CategoryDescription](
	[CatId] [int] NOT NULL,
	[LanguageId] [int] NOT NULL,
	[LanguageCode] [nvarchar](50) NOT NULL,
	[CatName] [nvarchar](250) NULL,
	[Description] [ntext] NULL,
	[Title] [nvarchar](500) NULL,
	[Desc] [nvarchar](500) NULL,
	[Keywords] [nvarchar](500) NULL,
	[Parent] [int] NULL,
	[Depth] [int] NULL,
	[Url] [nvarchar](160) NULL,
	[Description2] [ntext] NULL,
	[SearchKeywords] [nvarchar](1000) NULL,
	[Lineage2] [nvarchar](160) NULL,
	[Lineage] [nvarchar](160) NULL,
 CONSTRAINT [PK_CategoryDescription2] PRIMARY KEY CLUSTERED 
(
	[CatId] ASC,
	[LanguageId] ASC,
	[LanguageCode] 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].[Language]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Language]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Language](
	[LanguageId] [int] IDENTITY(1,1) NOT NULL,
	[LangName] [nvarchar](50) NULL,
	[LangCode] [nvarchar](50) NULL,
	[LangLocale] [nvarchar](150) NULL,
	[LangImg] [nvarchar](150) NULL,
	[LangOrder] [int] NULL,
	[LangStatus] [bit] NULL,
	[LangDefault] [bit] NULL,
 CONSTRAINT [PK_Language] PRIMARY KEY CLUSTERED 
(
	[LanguageId] 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

BEGIN TRY
SET IDENTITY_INSERT [dbo].[Language] ON 
INSERT [dbo].[Language] ([LanguageId], [LangName], [LangCode], [LangLocale], [LangImg], [LangOrder], [LangStatus], [LangDefault]) VALUES (1, N'Türkçe', N'tr', N'tr_TR.UTF-8,tr_TR,tr-tr,turkish', N'tr.png', 1, 1, 1)
SET IDENTITY_INSERT [dbo].[Language] OFF
END TRY
BEGIN CATCH 
END CATCH


GO


/****** Object:  Table [dbo].[MotorMarks]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MotorMarks]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MotorMarks](
	[MarkId] [int] IDENTITY(1,1) NOT NULL,
	[MarkName] [nvarchar](50) NULL,
	[MarkCode] [nvarchar](250) NULL,
	[IsActive] [bit] NULL,
	[ShowOrder] [int] NULL,
	[ContainsYears] [nvarchar](1000) NULL,
	[Detail] [nvarchar](1000) NULL,
	[ContainsYearsDesc] [nvarchar](50) NULL,
 CONSTRAINT [PK_MotorMark] PRIMARY KEY CLUSTERED 
(
	[MarkId] 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].[MotorModels]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MotorModels]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MotorModels](
	[ModelId] [int] IDENTITY(1,1) NOT NULL,
	[ModelName] [nvarchar](50) NULL,
	[ModelCode] [nvarchar](250) NULL,
	[IsActive] [bit] NULL,
	[MarkId] [int] NULL,
	[ShowOrder] [int] NULL,
	[ContainsYears] [nvarchar](1000) NULL,
	[ContainsYearsDesc] [nvarchar](50) NULL,
 CONSTRAINT [PK_MotorModel] PRIMARY KEY CLUSTERED 
(
	[ModelId] 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].[MotorModelsProperty]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MotorModelsProperty]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MotorModelsProperty](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[ModelId] [int] NULL,
	[TypeId] [int] NULL,
	[TypeDetailId] [int] NULL,
	[YearId] [int] NULL,
 CONSTRAINT [PK_MotorModelsProperty] 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].[MotorProductDesc]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MotorProductDesc]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MotorProductDesc](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[ProductId] [int] NULL,
	[YearId] [int] NULL,
	[MarkId] [int] NULL,
	[ModelId] [int] NULL,
	[TypeId] [int] NULL,
	[TypeDetailId] [int] NULL,
 CONSTRAINT [PK_MotorProductDesc] 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].[MotorPropertyType]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MotorPropertyType]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MotorPropertyType](
	[TypeId] [int] IDENTITY(1,1) NOT NULL,
	[TypeName] [nvarchar](50) NULL,
	[TypeCode] [nvarchar](50) NULL,
	[IsActive] [bit] NULL,
	[ShowOrder] [int] NULL,
 CONSTRAINT [PK_MotorPropertyType] PRIMARY KEY CLUSTERED 
(
	[TypeId] 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].[MotorTypeDetails]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MotorTypeDetails]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MotorTypeDetails](
	[TypeDetailId] [int] IDENTITY(1,1) NOT NULL,
	[TypeDetailName] [nvarchar](50) NULL,
	[TypeDetailCode] [nvarchar](50) NULL,
	[IsActive] [bit] NULL,
	[ShowOrder] [int] NULL,
	[TypeId] [int] NULL,
	[ProductDesc] [nvarchar](250) NULL,
	[ProductFilterDesc] [nvarchar](250) NULL,
 CONSTRAINT [PK_MotorTypeDetail] PRIMARY KEY CLUSTERED 
(
	[TypeDetailId] 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].[MotorTypeProducts]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MotorTypeProducts]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MotorTypeProducts](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[TypeDetailId] [int] NULL,
	[AProductId] [int] NULL,
 CONSTRAINT [PK_MotorTypeProducts] 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].[MotorUserList]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MotorUserList]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MotorUserList](
	[IND] [int] IDENTITY(1,1) NOT NULL,
	[UserId] [int] NULL,
	[Name] [nvarchar](50) NULL,
	[YearId] [int] NULL,
	[MarkId] [int] NULL,
	[ModelId] [int] NULL,
	[CreatedDate] [datetime] NULL,
 CONSTRAINT [PK_MotorUserList] 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].[MotorYears]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MotorYears]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MotorYears](
	[YearId] [int] IDENTITY(1,1) NOT NULL,
	[YearName] [nvarchar](50) NULL,
	[YearCode] [nvarchar](250) NULL,
	[IsActive] [bit] NULL,
	[ShowOrder] [int] NULL,
 CONSTRAINT [PK_MotorYear] PRIMARY KEY CLUSTERED 
(
	[YearId] 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].[PaymentsWithoutUser]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PaymentsWithoutUser]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[PaymentsWithoutUser](
	[PaymentId] [int] IDENTITY(1,1) NOT NULL,
	[PortalId] [int] NOT NULL,
	[CustomerTc] [nvarchar](50) NOT NULL,
	[CustomerName] [nvarchar](50) NOT NULL,
	[CustomerTel] [nvarchar](50) NOT NULL,
	[CustomerMail] [nvarchar](50) NOT NULL,
	[PaymentDate] [datetime] NOT NULL,
	[PaymentType] [nvarchar](30) NULL,
	[BankName] [nvarchar](50) NULL,
	[BankPayType] [nvarchar](10) NULL,
	[Taksit] [int] NULL,
	[IsPayed] [bit] NULL,
	[Note] [ntext] NULL,
	[ToplamTutar] [decimal](18, 4) NULL,
	[ToplamGider] [decimal](18, 4) NULL,
	[BankaToplamTutar] [decimal](18, 4) NULL,
	[CustomerCard] [nvarchar](50) NULL,
 CONSTRAINT [PK_PaymentsWithoutUser] PRIMARY KEY CLUSTERED 
(
	[PaymentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[ProductDescription]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ProductDescription]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ProductDescription](
	[ProductId] [int] NOT NULL,
	[LanguageId] [int] NOT NULL,
	[LanguageCode] [nvarchar](50) NOT NULL,
	[ProductName] [nvarchar](250) NULL,
	[Description] [ntext] NULL,
	[Details] [ntext] NULL,
	[SearchKeywords] [nvarchar](1000) NULL,
	[Url] [nvarchar](160) NULL,
 CONSTRAINT [PK_ProductDescription] PRIMARY KEY CLUSTERED 
(
	[ProductId] ASC,
	[LanguageId] ASC,
	[LanguageCode] 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].[SliderDescription]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SliderDescription]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[SliderDescription](
	[SlideId] [int] NOT NULL,
	[SlideTitle] [nvarchar](255) NULL,
	[SlideImage] [nvarchar](255) NULL,
	[SlideUrl] [nvarchar](255) NULL,
	[SlideHtml] [nvarchar](max) NULL,
	[LanguageId] [int] NULL,
	[LanguageCode] [nvarchar](50) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[SpamMailList]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SpamMailList]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[SpamMailList](
	[SpamId] [int] IDENTITY(1,1) NOT NULL,
	[SpamMail] [nvarchar](50) NULL,
	[SpamCount] [int] NULL,
	[CreatedDate] [date] NULL,
	[LastMailTime] [date] NULL
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[TabsDescription]    Script Date: 29.1.2016 16:00:05 ******/
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].[TabsDescription]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[TabsDescription](
	[TabId] [int] NOT NULL,
	[TabName] [nvarchar](50) NULL,
	[ParentId] [int] NULL,
	[LanguageCode] [nvarchar](25) NOT NULL,
	[LanguageId] [int] NOT NULL,
	[Url] [nvarchar](255) NULL,
	[Lineage] [varchar](100) NULL,
	[ChildCount] [int] NULL,
	[Title] [nvarchar](100) NULL,
	[Description] [nvarchar](255) NULL,
	[Keywords] [nvarchar](255) NULL
) ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_CategoryDescription2]    Script Date: 29.1.2016 16:00:05 ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[CategoryDescription]') AND name = N'IX_CategoryDescription2')
CREATE NONCLUSTERED INDEX [IX_CategoryDescription2] ON [dbo].[CategoryDescription]
(
	[CatName] 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
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_SliderDescription]    Script Date: 29.1.2016 16:00:05 ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[SliderDescription]') AND name = N'IX_SliderDescription')
CREATE NONCLUSTERED INDEX [IX_SliderDescription] ON [dbo].[SliderDescription]
(
	[SlideId] ASC,
	[LanguageCode] ASC,
	[LanguageId] 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_MotorUserList_CreatedDate]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[MotorUserList] ADD  CONSTRAINT [DF_MotorUserList_CreatedDate]  DEFAULT (getdate()) FOR [CreatedDate]
END

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

GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_SpamMail_SpamCount]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[SpamMailList] ADD  CONSTRAINT [DF_SpamMail_SpamCount]  DEFAULT ((1)) FOR [SpamCount]
END

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

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

GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_PaymentsWithoutUser_Portals]') AND parent_object_id = OBJECT_ID(N'[dbo].[PaymentsWithoutUser]'))
ALTER TABLE [dbo].[PaymentsWithoutUser]  WITH NOCHECK ADD  CONSTRAINT [FK_PaymentsWithoutUser_Portals] FOREIGN KEY([PortalId])
REFERENCES [dbo].[Portals] ([PortalId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_PaymentsWithoutUser_Portals]') AND parent_object_id = OBJECT_ID(N'[dbo].[PaymentsWithoutUser]'))
ALTER TABLE [dbo].[PaymentsWithoutUser] CHECK CONSTRAINT [FK_PaymentsWithoutUser_Portals]
GO
/****** Object:  Trigger [dbo].[CategoryDescription_InsertTrigger]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[CategoryDescription_InsertTrigger]'))
EXEC dbo.sp_executesql @statement = N'CREATE TRIGGER [dbo].[CategoryDescription_InsertTrigger]
ON [dbo].[CategoryDescription]
FOR INSERT AS
UPDATE child
    -- set the depth of this "child" to be the
    -- depth of the parent, plus one.
    SET  
	Depth = ISNULL(parent.Depth + 1,0), 
    -- the Lineage is simply the Lineage of the parent,
    -- plus the child''s Id (and appropriate ''/'' characters
    Lineage = ISNULL(parent.Lineage,''/'') + LTrim(Str(child.CatId)) + ''/'' ,
    Lineage2 = ISNULL(parent.Lineage2,''|'') + LTrim(child.CatName) + ''|'' , 
-- we can''t update the "inserted" table directly,
-- so we find the corresponding child in the
-- "real" table
Url=ISNULL(parent.Url,''/kategori/'') + (dbo.doUrl(child.CatName) + ''/'')

FROM CategoryDescription child INNER JOIN inserted i ON i.CatId=child.CatId AND i.LanguageId=child.LanguageId
-- now, we attempt to find the parent of this
-- "child" - but it might not exist, so these
-- values may well be NULL
LEFT OUTER JOIN CategoryDescription parent ON child.Parent=parent.CatId AND child.LanguageId=parent.LanguageId
' 
GO
/****** Object:  Trigger [dbo].[CategoryDescription_UpdateTrigger]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[CategoryDescription_UpdateTrigger]'))
EXEC dbo.sp_executesql @statement = N'CREATE TRIGGER [dbo].[CategoryDescription_UpdateTrigger]
ON [dbo].[CategoryDescription]
FOR UPDATE AS

UPDATE child
    SET 
  
    Depth = child.Depth - old.Depth + ISNULL(parent.Depth + 1,0),
    Lineage = ISNULL(parent.Lineage,''/'') + LTrim(Str(old.CatId)) + ''/'' + right(child.Lineage, len(child.Lineage) - len(old.Lineage)),
    Lineage2 = ISNULL(parent.Lineage2,''|'') + old.CatName + ''|'' + (right(child.Lineage2, len(child.Lineage2) - len(old.Lineage2))) ,
    
	Url =ISNULL(parent.Url,''/kategori/'') + dbo.doUrl(old.CatName) + ''/'' + dbo.doUrl(right(child.Url, len(child.Url) - len(old.Url))) 
  
FROM CategoryDescription child INNER JOIN inserted old ON child.Lineage LIKE  old.Lineage + ''%''  AND child.LanguageId=old.LanguageId
LEFT OUTER JOIN CategoryDescription parent ON old.Parent=parent.CatId  AND old.LanguageId=parent.LanguageId
' 
GO
/****** Object:  Trigger [dbo].[ProductDescription_InsertTrigger]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[ProductDescription_InsertTrigger]'))
EXEC dbo.sp_executesql @statement = N'CREATE TRIGGER [dbo].[ProductDescription_InsertTrigger]
ON [dbo].[ProductDescription]
FOR  INSERT,Update 
AS 
declare @ProductId int ,@LanguageId int
SELECT @ProductId= ProductId,@LanguageId=LanguageId from Inserted
 

IF UPDATE (ProductName) 
Begin
Update ProductDescription set Url=''/urun/'' + dbo.doUrl(ProductName)  + ''_'' + cast(ProductId as varchar(11)) + ''/'' WHERE LanguageId=@LanguageId AND ProductId = @ProductId
end

' 
GO
/****** Object:  Trigger [dbo].[TabsDescription_InsertTrigger]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TabsDescription_InsertTrigger]'))
EXEC dbo.sp_executesql @statement = N'
CREATE TRIGGER [dbo].[TabsDescription_InsertTrigger]
ON [dbo].[TabsDescription]
FOR INSERT AS
UPDATE child
    
    SET 
    Lineage = ISNULL(parent.Lineage,''/'') + LTrim(Str(child.TabId)) + ''/'' ,
    Url=ISNULL(parent.Url,''/icerik/'') + (dbo.doUrl(child.TabName) + ''/''),
    ChildCount=(Select count(*) from TabsDescription where ParentId=child.TabId)
FROM TabsDescription child INNER JOIN inserted i ON i.TabId=child.TabId AND i.LanguageId=child.LanguageId
LEFT OUTER JOIN TabsDescription parent ON child.ParentId=parent.TabId AND child.LanguageId=parent.LanguageId

' 
GO
/****** Object:  Trigger [dbo].[TabsDescription_UpdateTrigger]    Script Date: 29.1.2016 16:00:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TabsDescription_UpdateTrigger]'))
EXEC dbo.sp_executesql @statement = N'
CREATE TRIGGER [dbo].[TabsDescription_UpdateTrigger]
ON [dbo].[TabsDescription]
FOR UPDATE AS

UPDATE child
    SET 
    Lineage = ISNULL(parent.Lineage,''/'') + LTrim(Str(old.TabId)) + ''/'' + right(child.Lineage, len(child.Lineage) - len(old.Lineage)),
    Url =ISNULL(parent.Url,''/icerik/'') + dbo.doUrl(old.TabName) + ''/'' + dbo.doUrl(right(child.Url, len(child.Url) - len(old.Url))) ,
    ChildCount=(Select count(*) from Tabs where ParentId=child.TabId)
FROM TabsDescription child INNER JOIN inserted old ON child.Lineage LIKE old.Lineage + ''%'' AND child.LanguageId=old.LanguageId
LEFT OUTER JOIN TabsDescription parent ON old.ParentId=parent.TabId  AND old.LanguageId=parent.LanguageId

' 
GO

/****** Object:  Trigger [dbo].[Categories_InsertTrigger]    Script Date: 29.1.2016 16:06:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Categories_InsertTrigger]
ON [dbo].[Categories]
FOR INSERT AS
UPDATE child
    -- set the depth of this "child" to be the
    -- depth of the parent, plus one.
    SET Depth = ISNULL(parent.Depth + 1,0), 
    -- the Lineage is simply the Lineage of the parent,
    -- plus the child's Id (and appropriate '/' characters
    Lineage = ISNULL(parent.Lineage,'/') + LTrim(Str(child.CatId)) + '/' ,
    Lineage2 = ISNULL(parent.Lineage2,'|') + LTrim(child.CatName) + '|' ,
    StartDate=i.StartDate,
    EndDate=i.EndDate,
-- we can't update the "inserted" table directly,
-- so we find the corresponding child in the
-- "real" table
Url=ISNULL(parent.Url,'/kategori/') + (dbo.doUrl(child.CatName) + '/')

FROM Categories child INNER JOIN inserted i ON i.CatId=child.CatId
-- now, we attempt to find the parent of this
-- "child" - but it might not exist, so these
-- values may well be NULL
LEFT OUTER JOIN Categories parent ON child.Parent=parent.CatId


DECLARE @LanguageId int,@LangCode nvarchar(250)

SELECT TOP 1 @LanguageId= LanguageId,@LangCode=LangCode FROM [Language] WHERE LangDefault=1

IF NOT EXISTS(SELECT cd.[CatId] FROM CategoryDescription AS cd INNER JOIN inserted i ON cd.[CatId]=i.[CatId] WHERE cd.[CatId]=i.[CatId] AND cd.[LanguageId]=@LanguageId AND cd.[LanguageCode]=@LangCode
)
BEGIN
INSERT INTO [dbo].[CategoryDescription]([CatId],[LanguageId],[LanguageCode],[CatName],[Title],[Keywords],[Parent],[SearchKeywords])
Select [CatId],@LanguageId,@LangCode,[CatName],[Title],[Keywords],[Parent],[SearchKeywords] From inserted
END

GO

IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'Categories' and column_name = 'ChildCount') ALTER TABLE [dbo].Categories ADD [ChildCount] int constraint DF_Categories_ChildCount default ((0))
GO

/****** Object:  Trigger [dbo].[Categories_UpdateTrigger]    Script Date: 23.2.2016 15:44:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Categories_UpdateTrigger]
ON [dbo].[Categories]
FOR UPDATE AS

UPDATE child
    SET 
 IsActive=case 
when old.IsActive=1 then child.IsActive
when old.IsActive=0 then 0
end,
--    IsVisible=old.IsVisible,
    Depth = child.Depth - old.Depth + ISNULL(parent.Depth + 1,0),
    Lineage = ISNULL(parent.Lineage,'/') + LTrim(Str(old.CatId)) + '/' + right(child.Lineage, len(child.Lineage) - len(old.Lineage)),
    Lineage2 = ISNULL(parent.Lineage2,'|') + old.CatName + '|' + (right(child.Lineage2, len(child.Lineage2) - len(old.Lineage2))) ,
    StartDate=old.StartDate,
    EndDate=old.EndDate,
	ChildCount=(Select Count(*) from Categories where IsActive=1 and IsVisible=1  and Parent=child.CatId ),
	Url =ISNULL(parent.Url,'/kategori/') + dbo.doUrl(old.CatName) + '/' + dbo.doUrl(right(child.Url, len(child.Url) - len(old.Url))) 
    --Url= ISNULL(parent.Url,'/') + (child.CatName + '/')
FROM Categories child INNER JOIN inserted old ON child.Lineage LIKE old.Lineage + '%'
LEFT OUTER JOIN Categories parent ON old.Parent=parent.CatId

UPDATE parent
    SET 
	ChildCount=(Select Count(*) from Categories where IsActive=1 and IsVisible=1  and Parent=parent.CatId ) 

FROM  Categories parent INNER JOIN inserted old ON old.Parent=parent.CatId
 

If UPDATE(Parent) 
BEGIN
	 
	declare @CatId int,@Parents int

  select @Parents=Parent,@CatId=CatId from Inserted
 
	 UPDATE CategoryDescription SET Parent=@Parents  WHERE CatId=@CatId
	  
END

GO

/****** Object:  Trigger [dbo].[Categories_Delete]    Script Date: 23.2.2016 15:43:47 ******/
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 + '%')

update Categories set IsActive=IsActive 


GO 

IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'CosMailRules' and column_name = 'ProductNameFilter') ALTER TABLE [dbo].CosMailRules ADD [ProductNameFilter] nvarchar(500)
GO

IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'CurtainUnitOption' and column_name = 'CurtainPlus') ALTER TABLE [dbo].CurtainUnitOption ADD [CurtainPlus] int not null constraint DF_CurtainUnitOption_CurtainPlus default ((0)) 
GO
  
IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'HtmlText' and column_name = 'LanguageId') ALTER TABLE [dbo].HtmlText ADD [LanguageId] int
GO

IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'HtmlText' and column_name = 'LanguageCode') ALTER TABLE [dbo].HtmlText ADD [LanguageCode] nvarchar(50)
GO


IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'ProductUnits' and column_name = 'PartSale') ALTER TABLE [dbo].ProductUnits ADD [PartSale] bit constraint DF_ProductUnits_PartSale default ((0)) 
GO

IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'C2CShopSettings' and column_name = 'SubMerchantCode') ALTER TABLE [dbo].C2CShopSettings ADD [SubMerchantCode] nvarchar(50)  constraint DF_C2CShopSettings_SubMerchantCode default (N'(())')    
GO


IF EXISTS (SELECT * FROM sys.key_constraints WHERE object_id = OBJECT_ID(N'dbo.PK_HtmlText') AND parent_object_id = OBJECT_ID(N'dbo.HtmlText')) ALTER TABLE [dbo].[HtmlText] DROP CONSTRAINT [PK_HtmlText]
GO
 
BEGIN TRY
ALTER TABLE [dbo].[OrderDetails]  Alter Column Quantity decimal(18,4)
END TRY
BEGIN CATCH 
END CATCH

GO
 
/****** Object:  Trigger [dbo].[UpdateOnDeleteOrder]    Script Date: 29.1.2016 16:28:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER  TRIGGER [dbo].[UpdateOnDeleteOrder]
ON [dbo].[OrderDetails]
FOR Delete
AS 
BEGIN



declare @ProductId int,@ProductSizeId int,@StorageId int,@StockType int
SELECT @ProductId=ProductId,@ProductSizeId=ProductSizeId,@StorageId=StorageId from deleted

if (@ProductSizeId > 0)
Begin
Update ProductSizes set StockQty=isnull(StockQty,0)+(d.Quantity*d.Carpan) from deleted d where d.ProductSizeId=ProductSizes.ProductSizeId and d.Status<>-1
Update Products set StockQty = (Select isnull(sum(StockQty),0) from ProductSizes where StockQty>0 and ProductId=@ProductId) where ProductId=@ProductId
--Update ProductStorages set Quantity=isnull(ProductStorages.Quantity,0)+(d.Quantity*d.Carpan) from deleted d where d.ProductSizeId=ProductStorages.ProductSizeOptionId and d.Status<>-1 
--and ProductStorages.StorageId=@StorageId

End
Else
Begin
Update Products set StockQty=isnull(StockQty,0)+(d.Quantity*d.Carpan) from deleted d where d.ProductId=Products.ProductId and d.Status<>-1
Update ProductStorages set Quantity=isnull(ProductStorages.Quantity,0)+(d.Quantity*d.Carpan) from deleted d where d.ProductId=ProductStorages.ProductId and d.Status<>-1 
and ProductStorages.StorageId=@StorageId
End



----------------------------------------------------------------------------------------------------------------------------------
DECLARE UpdateStok_Cursor CURSOR FOR SELECT ProductId,Quantity*Carpan from deleted

declare @iProductId decimal(18,4),@iQuantity decimal(18,4) 

OPEN UpdateStok_Cursor
FETCH NEXT FROM UpdateStok_Cursor INTO  @iProductId,@iQuantity 

WHILE @@FETCH_STATUS = 0
BEGIN

 
------------------------------------------------
SELECT @StockType=StokType FROM Products WHERE ProductId=@iProductId


if(@StockType=6)
BEGIN
   DECLARE groups CURSOR FOR SELECT GProductId, StockQty , Carpan FROM ProductGroups WHERE ProductId=@iProductId
   declare @GProductId int,@StockQty3 decimal(18,4),@CarpanGroup decimal
   
   OPEN groups
   FETCH NEXT FROM groups into @GProductId,@StockQty3,@CarpanGroup

   WHILE @@FETCH_STATUS=0
   BEGIN
    
	Update Products set Products.StockQty=isnull(Products.StockQty,0) + (@iQuantity * @StockQty3 * @CarpanGroup)
	from  deleted d where d.Status<>-1 and  Products.ProductId=@GProductId 
 

	FETCH NEXT FROM groups  into @GProductId,@StockQty3,@CarpanGroup
    END

	CLOSE groups
	DEALLOCATE groups
	
END
 ----------------------------------------------

 
    FETCH NEXT FROM UpdateStok_Cursor INTO @iProductId,@iQuantity 
    END
    CLOSE UpdateStok_Cursor
    DEALLOCATE UpdateStok_Cursor





-----------------------------------------------------------------------------------------------------------------------------------------


END




GO

 
/****** Object:  Trigger [dbo].[UpdateOnUpdateOrder]    Script Date: 29.1.2016 16:28:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER  TRIGGER [dbo].[UpdateOnUpdateOrder]
ON [dbo].[OrderDetails]
FOR UPDATE
AS 
BEGIN

declare @ProductId int,@ProductSizeId int,@StorageId int,@StockType int
SELECT @ProductId=ProductId,@ProductSizeId=ProductSizeId,@StorageId=StorageId  from Inserted
 
  
if (@ProductSizeId > 0)
Begin
--iptal secilen
Update ProductSizes set StockQty=isnull(StockQty,0) + (i.Quantity*d.Carpan)
from deleted d,inserted i where d.ProductSizeId=ProductSizes.ProductSizeId and i.ProductSizeId=ProductSizes.ProductSizeId
and i.Status=-1 and d.Status<>-1


--iptal degisen
Update ProductSizes set StockQty=isnull(StockQty,0) - (d.Quantity*d.Carpan)
from deleted d,inserted i where d.ProductSizeId=ProductSizes.ProductSizeId and i.ProductSizeId=ProductSizes.ProductSizeId
and d.Status=-1 and i.Status<>-1

Update Products set StockQty = (Select isnull(sum(StockQty),0) from ProductSizes where StockQty>0 and ProductId=@ProductId) where ProductId=@ProductId

--Update ProductStorages set Quantity=isnull(ProductStorages.Quantity ,0) - (d.Quantity*d.Carpan)
--from deleted d,inserted i where d.ProductSizeId=ProductStorages.ProductSizeOptionId  and i.ProductSizeId=ProductStorages.ProductSizeOptionId 
--and d.Status=-1 and i.Status<>-1 and ProductStorages.StorageId=@StorageId 

End
Else
Begin
--iptal secilen
Update Products set StockQty=isnull(StockQty,0) + (i.Quantity*d.Carpan)
from deleted d,inserted i where d.ProductId=Products.ProductId and i.ProductId=Products.ProductId
and i.Status=-1 and d.Status<>-1

--iptal degisen
Update Products set StockQty=isnull(StockQty,0) - (d.Quantity*d.Carpan)
from deleted d,inserted i where d.ProductId=Products.ProductId and i.ProductId=Products.ProductId
and d.Status=-1 and i.Status<>-1

Update ProductStorages set Quantity=isnull(ProductStorages.Quantity ,0) - (d.Quantity*d.Carpan)
from deleted d,inserted i where d.ProductSizeId=ProductStorages.ProductSizeOptionId  and i.ProductSizeId=ProductStorages.ProductSizeOptionId 
and d.Status=-1 and i.Status<>-1 and ProductStorages.StorageId=@StorageId 

Update ProductStorages set Quantity=isnull(ProductStorages.Quantity,0) + (i.Quantity*d.Carpan)
from deleted d,inserted i where d.ProductId=ProductStorages.ProductId and i.ProductId=ProductStorages.ProductId
and i.Status=-1 and d.Status<>-1 and ProductStorages.StorageId=@StorageId 

End
----------------------------------------------------------------------------------------------------------------------------------
DECLARE UpdateStok_Cursor CURSOR FOR SELECT ProductId,Quantity*Carpan from Inserted

declare @iProductId int,@iQuantity int 

OPEN UpdateStok_Cursor
FETCH NEXT FROM UpdateStok_Cursor INTO  @iProductId,@iQuantity 

WHILE @@FETCH_STATUS = 0
BEGIN

 
------------------------------------------------
SELECT @StockType=StokType FROM Products WHERE ProductId=@iProductId


if(@StockType=6)
BEGIN
   DECLARE groups CURSOR FOR SELECT GProductId, StockQty , Carpan FROM ProductGroups WHERE ProductId=@iProductId
   declare @GProductId int,@StockQty3 int,@CarpanGroup decimal
   
   OPEN groups
   FETCH NEXT FROM groups into @GProductId,@StockQty3,@CarpanGroup

   WHILE @@FETCH_STATUS=0
   BEGIN

	--iptal secilen
	Update Products set Products.StockQty=isnull(Products.StockQty,0) + (@iQuantity * @StockQty3 * @CarpanGroup)
	from deleted d,inserted i where  i.Status=-1 and d.Status<>-1 and  Products.ProductId=@GProductId 
	
	--iptal degisen
	Update Products set Products.StockQty=isnull(Products.StockQty,0) - (@iQuantity  * @StockQty3 * @CarpanGroup)
	from deleted d,inserted i where  d.Status=-1 and i.Status<>-1 and Products.ProductId=@GProductId 
		

	FETCH NEXT FROM groups  into @GProductId,@StockQty3,@CarpanGroup
    END

	CLOSE groups
	DEALLOCATE groups
	
END
 ----------------------------------------------

 
    FETCH NEXT FROM UpdateStok_Cursor INTO @iProductId,@iQuantity 
    END
    CLOSE UpdateStok_Cursor
    DEALLOCATE UpdateStok_Cursor





-----------------------------------------------------------------------------------------------------------------------------------------


END

GO

 
/****** Object:  Trigger [dbo].[UpdateStok]    Script Date: 29.1.2016 16:29:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER   TRIGGER [dbo].[UpdateStok]
ON [dbo].[OrderDetails]
FOR INSERT
AS 
BEGIN

-- Start Set

declare @prodId int,@newStockQty decimal(18,4),@StockType int

DECLARE UpdateStok_Cursor CURSOR FOR SELECT ProductId,ProductSizeId,Quantity*Carpan,StorageId from Inserted

declare @ProductId int,@ProductSizeId int,@iQuantity decimal(18,4),@StorageId int

OPEN UpdateStok_Cursor
FETCH NEXT FROM UpdateStok_Cursor INTO  @ProductId,@ProductSizeId,@iQuantity,@StorageId

WHILE @@FETCH_STATUS = 0
BEGIN

 

SELECT @StockType=StokType FROM Products WHERE ProductId=@ProductId

if(@StockType = 6)

BEGIN
DECLARE groups CURSOR FOR
SELECT GProductId, StockQty , Carpan
FROM ProductGroups WHERE ProductId=@ProductId
declare @GProductId int,@StockQty3 decimal(18,4),@CarpanGroup decimal(18,4)

OPEN groups
FETCH NEXT FROM groups into @GProductId,@StockQty3,@CarpanGroup

WHILE @@FETCH_STATUS=0
BEGIN

	
	 update Products set StockQty= StockQty - (@iQuantity * @StockQty3 * @CarpanGroup)
	WHERE ProductId=@GProductId  
	

FETCH NEXT FROM groups  into @GProductId,@StockQty3,@CarpanGroup
END
CLOSE groups
DEALLOCATE groups

	END
-- End Set
	

if (@ProductSizeId > 0)
Begin
Update ProductSizes set StockQty=isnull(StockQty-@iQuantity,0) where ProductSizeId=@ProductSizeId
Update Products set StockQty = (Select isnull(sum(StockQty),0) from ProductSizes where StockQty>0 and ProductId=@ProductId) where ProductId=@ProductId
--textil tamamlan�nca a��lacak elvan
--update ProductStorages set Quantity=isnull(Quantity-@iQuantity,0) where ProductSizeOptionId =@ProductSizeId and ProductId=@ProductId and StorageId=@StorageId
--extra control
declare @StockQty decimal(18,4)
select @StockQty = isnull(sum(StockQty),0) from ProductSizes where StockQty >=0 and ProductSizeId=@ProductSizeId
	if (@StockQty <= 0)
    Begin
    delete dbo.ShoppingCart Where ProductSizeId=@ProductSizeId
	Update Products set StokEndDate=GETDATE() where  ProductId=@ProductId
	Update ProductSizes set StokEndDate=GETDATE() where  ProductSizeId=@ProductSizeId
    End
    else
    begin
    Update dbo.ShoppingCart set Qty=@StockQty Where ProductSizeId=@ProductSizeId
    end
    End
    Else
    Begin

    Update Products set StockQty=StockQty- @iQuantity where ProductId=@ProductId
    update ProductStorages set Quantity=isnull(Quantity-@iQuantity,0) where ProductSizeOptionId =@ProductSizeId and ProductId=@ProductId and StorageId=@StorageId
	declare @StockQty2 decimal(18,4)
    select @StockQty2 = isnull(StockQty,0) from Products where ProductId=@ProductId
		if (@StockQty2 <= 0)
        Begin
	    Update Products set StokEndDate=GETDATE() where  ProductId=@ProductId
        End
    End

    FETCH NEXT FROM UpdateStok_Cursor INTO @ProductId,@ProductSizeId,@iQuantity ,@StorageId
    END
    CLOSE UpdateStok_Cursor
    DEALLOCATE UpdateStok_Cursor

    END




GO


IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'ProductKeywords' and column_name = 'LanguageId') ALTER TABLE [dbo].ProductKeywords ADD [LanguageId] int not null default ((1))
GO

IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'ProductKeywords' and column_name = 'LanguageCode') ALTER TABLE [dbo].ProductKeywords ADD [LanguageCode] nvarchar(50) not null default (('tr'))
GO


/****** Object:  Index [PK_ProductKeywords]    Script Date: 29.1.2016 16:37:19 ******/
ALTER TABLE [dbo].[ProductKeywords] DROP CONSTRAINT [PK_ProductKeywords]
GO



/****** Object:  Index [PK_ProductKeywords]    Script Date: 29.1.2016 16:36:17 ******/
ALTER TABLE [dbo].[ProductKeywords] ADD  CONSTRAINT [PK_ProductKeywords] PRIMARY KEY CLUSTERED 
(
	[ProductId] ASC,
	[LanguageCode] ASC,
	[LanguageId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO



ALTER TABLE [dbo].[ProductKeywords]  Alter Column Title nvarchar(100) not null

GO

IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'ProductOptionsValues' and column_name = 'ProductOptionsValuesPath') ALTER TABLE [dbo].ProductOptionsValues ADD [ProductOptionsValuesPath] nvarchar(500)  
GO




--FOR PRODUCTS TABLE
 /****** Object:  Trigger [TrgWizard]    Script Date: 8.10.2015 18:01:41 ******/
DROP TRIGGER [dbo].[TrgShowOrder]
GO

/****** Object:  Trigger [TrgStokAlarm]    Script Date: 8.10.2015 18:02:24 ******/
DROP TRIGGER [dbo].[TrgStokAlarm]
GO

 
BEGIN TRY 
DROP INDEX [IX_Products] ON [dbo].[Products]  
DROP INDEX [IX_Products_1] ON [dbo].[Products]
END TRY
BEGIN CATCH 
END CATCH


GO
ALTER TABLE [dbo].[ShoppingCart] DROP CONSTRAINT [FK_ShoppingCart_Products]
GO
ALTER TABLE [dbo].[Reviews] DROP CONSTRAINT [FK_Reviews_Products]
GO

ALTER TABLE [dbo].[ProductWizardValues] DROP CONSTRAINT [FK_ProductWizardValues_Products]
GO

ALTER TABLE [dbo].[ProductUsages] DROP CONSTRAINT [FK_ProductUsages_Products]
GO
ALTER TABLE [dbo].[ProductUnits] DROP CONSTRAINT [FK_ProductUnits_Products]
GO
ALTER TABLE [dbo].[ProductTabs] DROP CONSTRAINT [FK_ProductTabs_Products]
GO
ALTER TABLE [dbo].[ProductSizes] DROP CONSTRAINT [FK_ProductSizes_Products]
GO

ALTER TABLE [dbo].[ProductPropertys] DROP CONSTRAINT [FK_ProductPropertys_Products]
GO
ALTER TABLE [dbo].[ProductKeywords] DROP CONSTRAINT [FK_ProductKeywords_Products]
GO
ALTER TABLE [dbo].[ProductImages] DROP CONSTRAINT [FK_ProductImages_Products]
GO

ALTER TABLE [dbo].[ProductFiles] DROP CONSTRAINT [FK_ProductFiles_Products]
GO

ALTER TABLE [dbo].[ProductAttributes] DROP CONSTRAINT [FK_ProductAttributes_Products]
GO
ALTER TABLE [dbo].[ProductAlternatives] DROP CONSTRAINT [FK_ProductAlternatives_Products]
GO
  
ALTER TABLE [dbo].[Products] DROP CONSTRAINT [PK_Products]
GO

ALTER TABLE [dbo].[Products] DROP CONSTRAINT [DF_Products_StockQty]
GO



BEGIN TRY
ALTER TABLE [dbo].[Products]  Alter Column StockQty decimal(18,4)
END TRY
BEGIN CATCH 
END CATCH

GO


/****** Object:  Trigger [dbo].[TrgWizard]    Script Date: 8.10.2015 17:59:42 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[TrgWizard]
ON [dbo].[Products]
FOR  delete 
AS 

declare @productId int
SELECT @productId= ProductId from deleted
delete ProductWizardValues where ProductId=@productId
delete dbo.ProductCategories where ProductId=@productId


GO



/****** Object:  Trigger [dbo].[TrgStokAlarm]    Script Date: 8.10.2015 17:59:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[TrgStokAlarm]
ON [dbo].[Products]
FOR  UPDATE 
AS 


If UPDATE(StockQty) Or  UPDATE(SellNoneStock) --and ((select Stock from deleted)<1) --and ((select Stock from Inserted)>0)
BEGIN
	-- kullanıcı
	declare @productId int,@oldStock int,@newStock int,@oldSellNoneStock bit,@newSellNoneStock bit

select @newStock=StockQty,@newSellNoneStock=SellNoneStock from Inserted
select @oldStock=StockQty,@oldSellNoneStock=SellNoneStock  from Deleted

	if (@oldStock < 1  and @newStock > 0) or (@newSellNoneStock=1 and @oldSellNoneStock=0)
	begin
	SELECT      @productId= ProductId from Inserted

	 update AlarmList set WillSendMessage=1
	where ProductId=@productId  and AlarmTypeId = 2
	end

	

END

If UPDATE(SellLimitToUser) 
BEGIN
	-- kullanıcı
	declare @SProductId int,@SellLimitToUser int

select @SProductId=ProductId,@SellLimitToUser=isnull(SellLimitToUser,0) from Inserted

	if (@SellLimitToUser > 0) 
	begin
	 update ShoppingCart set Qty=@SellLimitToUser  where ProdId=@SProductId and Qty>@SellLimitToUser
	--insert ErrorLog (Source) values (@SellLimitToUser)
	end

	

END


GO



/****** Object:  Trigger [dbo].[TrgShowOrder]    Script Date: 8.10.2015 18:00:03 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[TrgShowOrder]
ON [dbo].[Products]
FOR  INSERT,Update 
AS 
declare @ProductId int ,@ShowOrder int
SELECT @ProductId= ProductId,@ShowOrder=ShowOrder from Inserted



if (@ShowOrder is null)
BEGIN
		
	 update Products set ShowOrder=@ProductId
	where ProductId=@ProductId  
END

IF UPDATE (ProductName) 
Begin
Update Products set Url='/urun/' + dbo.doUrl(ProductName)  + '_' + cast(ProductId as varchar(11)) + '/' where ProductId in (Select ProductId from Inserted )
end


GO



BEGIN TRY 
ALTER TABLE [dbo].[Products] ADD  CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED 
(
	[ProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
  
CREATE NONCLUSTERED INDEX [IX_Products] ON [dbo].[Products]
(
	[ProductCode] 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]
 
/****** Object:  Index [IX_Products_1]    Script Date: 8.10.2015 18:00:40 ******/
CREATE NONCLUSTERED INDEX [IX_Products_1] ON [dbo].[Products]
(
	[ProductName] 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]
 
END TRY
BEGIN CATCH 
END CATCH
GO
 
 ALTER TABLE [dbo].[ShoppingCart]  WITH NOCHECK ADD  CONSTRAINT [FK_ShoppingCart_Products] FOREIGN KEY([ProdId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[ShoppingCart] CHECK CONSTRAINT [FK_ShoppingCart_Products]
GO

ALTER TABLE [dbo].[Reviews]  WITH NOCHECK ADD  CONSTRAINT [FK_Reviews_Products] FOREIGN KEY([ProdId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[Reviews] CHECK CONSTRAINT [FK_Reviews_Products]
GO
ALTER TABLE [dbo].[ProductWizardValues]  WITH NOCHECK ADD  CONSTRAINT [FK_ProductWizardValues_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([ProductId])
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[ProductWizardValues] CHECK CONSTRAINT [FK_ProductWizardValues_Products]
GO

ALTER TABLE [dbo].[ProductUsages]  WITH NOCHECK ADD  CONSTRAINT [FK_ProductUsages_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[ProductUsages] CHECK CONSTRAINT [FK_ProductUsages_Products]
GO
ALTER TABLE [dbo].[ProductUnits]  WITH NOCHECK ADD  CONSTRAINT [FK_ProductUnits_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[ProductUnits] CHECK CONSTRAINT [FK_ProductUnits_Products]
GO

ALTER TABLE [dbo].[ProductTabs]  WITH CHECK ADD  CONSTRAINT [FK_ProductTabs_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[ProductTabs] CHECK CONSTRAINT [FK_ProductTabs_Products]
GO
ALTER TABLE [dbo].[ProductSizes]  WITH NOCHECK ADD  CONSTRAINT [FK_ProductSizes_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[ProductSizes] CHECK CONSTRAINT [FK_ProductSizes_Products]
GO

ALTER TABLE [dbo].[ProductPropertys]  WITH NOCHECK ADD  CONSTRAINT [FK_ProductPropertys_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[ProductPropertys] CHECK CONSTRAINT [FK_ProductPropertys_Products]
GO

ALTER TABLE [dbo].[ProductKeywords]  WITH NOCHECK ADD  CONSTRAINT [FK_ProductKeywords_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[ProductKeywords] CHECK CONSTRAINT [FK_ProductKeywords_Products]
GO


ALTER TABLE [dbo].[ProductImages]  WITH NOCHECK ADD  CONSTRAINT [FK_ProductImages_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[ProductImages] CHECK CONSTRAINT [FK_ProductImages_Products]
GO
ALTER TABLE [dbo].[ProductFiles]  WITH NOCHECK ADD  CONSTRAINT [FK_ProductFiles_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[ProductFiles] CHECK CONSTRAINT [FK_ProductFiles_Products]
GO
ALTER TABLE [dbo].[ProductAttributes]  WITH NOCHECK ADD  CONSTRAINT [FK_ProductAttributes_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[ProductAttributes] CHECK CONSTRAINT [FK_ProductAttributes_Products]
GO
ALTER TABLE [dbo].[ProductAlternatives]  WITH NOCHECK ADD  CONSTRAINT [FK_ProductAlternatives_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([ProductId])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[ProductAlternatives] CHECK CONSTRAINT [FK_ProductAlternatives_Products]
GO

ALTER TABLE [dbo].[Products] ADD  CONSTRAINT [DF_Products_StockQty]  DEFAULT ((0)) FOR [StockQty]
GO


/****** Object:  Trigger [dbo].[TrgWizard]    Script Date: 29.1.2016 17:01:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[TrgWizard]
ON [dbo].[Products]
FOR  delete 
AS 

declare @productId int
SELECT @productId= ProductId from deleted
delete ProductWizardValues where ProductId=@productId
delete dbo.ProductCategories where ProductId=@productId
delete dbo.ProductDescription  where ProductId=@productId


GO


IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'ProductSizeOptionsValues' and column_name = 'ProductSizeOptionShowOrder') ALTER TABLE [dbo].ProductSizeOptionsValues ADD [ProductSizeOptionShowOrder] int
GO

IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'ProductSizes' and column_name = 'AlternativeMarketsVariantId') ALTER TABLE [dbo].ProductSizes ADD [AlternativeMarketsVariantId] nvarchar(100)
GO

IF not exists (select column_name from INFORMATION_SCHEMA.columns  where table_name = 'Providers' and column_name = 'IsSpecial') ALTER TABLE [dbo].Providers ADD [IsSpecial] int
GO

 

IF NOT EXISTS (SELECT * FROM sys.objects o WHERE o.object_id = object_id(N'[dbo].[DF_Providers_IsSpecial]')  )
BEGIN
    ALTER TABLE [dbo].[Providers] add constraint DF_Providers_IsSpecial default ((0)) for IsSpecial
END

GO

 
ALTER TABLE [dbo].[ShoppingCart] drop constraint DF_ShoppingCart_Quantity  
GO


BEGIN TRY
ALTER TABLE [dbo].[ShoppingCart]  Alter Column Qty decimal(18,4) not null
END TRY
BEGIN CATCH 
END CATCH
GO

IF NOT EXISTS (SELECT * FROM sys.objects o WHERE o.object_id = object_id(N'[dbo].[DF_ShoppingCart_Quantity]')  )
BEGIN
    ALTER TABLE [dbo].[ShoppingCart] add constraint DF_ShoppingCart_Quantity default ((1)) for Qty
END

GO

IF NOT EXISTS (SELECT * FROM sys.objects o WHERE o.object_id = object_id(N'[dbo].[DF_ShoppingCart_CartTimeOut]')  )
BEGIN
    ALTER TABLE [dbo].[ShoppingCart] add constraint DF_ShoppingCart_CartTimeOut default (getdate()) for CartTimeOut
END

GO

IF NOT EXISTS (select * from sys.objects where type = 'TR' and name = 'Slider_Delete')
EXEC dbo.sp_executesql @statement = N'

 
CREATE TRIGGER [dbo].[Slider_Delete]
ON [dbo].[Slider]
FOR  DELETE 
AS

declare @SlideId int

select @SlideId=SlideId from deleted

DELETE  SliderDescription WHERE SlideId=@SlideId'


GO

 
/****** Object:  View [dbo].[AdminProductList]    Script Date: 29.1.2016 18:26:08 ******/
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
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: 29.1.2016 18:26:23 ******/
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
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: 23.2.2016 16:06:43 ******/
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.ProductName, 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
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 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].[OrdersListDetails]    Script Date: 29.1.2016 18:29:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[OrdersListDetails]
AS
SELECT        dbo.users.FullName, dbo.users.Email, dbo.OrderDetails.OrderDetailsId, dbo.OrderDetails.ProductId, dbo.OrderDetails.ProductCode, dbo.OrderDetails.ProductName, dbo.OrderDetails.BirimAdi, 
                         dbo.OrderDetails.Carpan, dbo.OrderDetails.KdvRate, dbo.OrderDetails.SatisFiyati, dbo.OrderDetails.Pb1, dbo.OrderDetails.Rate, dbo.OrderDetails.BirimIndirimOrani, dbo.OrderDetails.CariIndirimOrani, 
                         dbo.OrderDetails.AlisFiyati, dbo.OrderDetails.APb, dbo.OrderDetails.AlisRate, dbo.OrderDetails.Quantity, dbo.Orders.Status, dbo.OrderDetails.Status AS Status2, OrderStatus.StatusName, 
                         OrderStatus2.StatusName AS StatusName2, dbo.OrderDetails.WillSendMessage, dbo.OrderDetails.Provider, dbo.OrderDetails.MarkName, dbo.OrderDetails.Description, Providers_1.ProviderId, 
                         Providers_1.ProviderName, dbo.Providers.ProviderId AS XmlProviderId, dbo.Providers.ProviderName AS XmlProviderName, dbo.Currencys.CurrencyName, dbo.Orders.OrderId, dbo.Orders.PortalId, 
                         dbo.Orders.CustomerId, dbo.Orders.OrderDate, dbo.Orders.ShipDate, dbo.Orders.IsPayed, dbo.Orders.PaymentType, 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.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.Integreted, dbo.Orders.Aciklama, dbo.OrderDetails.UserId, dbo.OrderDetails.ProductSizeBarcode, dbo.OrderDetails.ProductBarcode, 
                         dbo.Orders.OrderCode, dbo.Orders.CurrencyRate, dbo.Orders.CurrencyCode, dbo.Orders.CustomerCurrencyRate, dbo.Orders.CustomerCurrencyCode, dbo.Orders.TaksitRate, dbo.OrderDetails.ProductSizeId
FROM            dbo.Orders INNER JOIN
                         dbo.OrderDetails ON dbo.Orders.OrderId = dbo.OrderDetails.OrderId INNER JOIN
                         dbo.Currencys ON dbo.OrderDetails.Pb1 = dbo.Currencys.CurrencyId LEFT OUTER JOIN
                         dbo.Providers ON dbo.OrderDetails.XmlProviderId = dbo.Providers.ProviderId LEFT OUTER JOIN
                         dbo.Providers AS Providers_1 ON dbo.OrderDetails.ProviderId = Providers_1.ProviderId LEFT OUTER JOIN
                         dbo.OrderStatus AS OrderStatus2 ON dbo.OrderDetails.Status = OrderStatus2.StatusId LEFT OUTER JOIN
                         dbo.users ON dbo.Orders.CustomerId = dbo.users.UserId LEFT OUTER JOIN
                         dbo.OrderStatus AS OrderStatus ON dbo.Orders.Status = OrderStatus.StatusId

GO

  
/****** Object:  View [dbo].[ProductsExportAll]    Script Date: 23.2.2016 16:08:00 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER VIEW [dbo].[ProductsExportAll]
AS
SELECT        TOP (100) PERCENT P.IsActive AS UrunAktif, P.ProductCode AS StokKodu, P.ProductName AS StokAdi, P.Description AS StokAciklama, P.Details, P.StokType, P.StokSubType, 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, dbo.Categories.CategoryCode, 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, dbo.ProductUnits.Barcode, P.ProviderId, 
P.AuthorId, dbo.Authors.AuthorName, dbo.Publishers.PublisherName, dbo.Publishers.PublisherId, P.ProductSizeOptionId, P.UserId, P.CampainId, ProductDescription.LanguageCode
FROM            Authors RIGHT OUTER JOIN
                         Products AS P INNER JOIN
                         dbo.ProductDescription ON P.ProductId = ProductDescription.ProductId LEFT OUTER JOIN
                         Publishers ON P.PublisherId = Publishers.PublisherId ON Authors.AuthorId = P.AuthorId LEFT OUTER JOIN
                         Providers AS Providers_1 ON P.IsPenta = Providers_1.ProviderId LEFT OUTER JOIN
                         Providers ON P.ProviderId = Providers.ProviderId LEFT OUTER JOIN
                         Categories RIGHT OUTER JOIN
                         ProductCategories ON Categories.CatId = ProductCategories.CategoryId ON P.ProductId = ProductCategories.ProductId LEFT OUTER JOIN
                         ProductKdvs ON P.KdvId = ProductKdvs.KdvId LEFT OUTER JOIN
                         ProductMarks ON P.MarkId = ProductMarks.MarkId LEFT OUTER JOIN
                         Currencys AS Currencys_0 RIGHT OUTER JOIN
                         Currencys AS Currencys_4 INNER JOIN
                         ProductUnits ON Currencys_4.CurrencyId = ProductUnits.Pb4 INNER JOIN
                         Currencys AS Currencys_5 ON ProductUnits.Pb5 = Currencys_5.CurrencyId LEFT OUTER JOIN
                         Currencys AS Currencys_P ON ProductUnits.PPb = Currencys_P.CurrencyId ON Currencys_0.CurrencyId = ProductUnits.Pb0 LEFT OUTER JOIN
                         Currencys AS Currencys_3 ON ProductUnits.Pb3 = Currencys_3.CurrencyId LEFT OUTER JOIN
                         Currencys AS Currencys_2 ON ProductUnits.Pb2 = Currencys_2.CurrencyId LEFT OUTER JOIN
                         Currencys AS Currencys_A ON ProductUnits.APb = Currencys_A.CurrencyId ON P.ProductId = ProductUnits.ProductId LEFT OUTER JOIN
                         Currencys AS Currencys_1 ON ProductUnits.Pb1 = Currencys_1.CurrencyId
WHERE        (NOT (P.Url IS NULL)) AND (NOT (dbo.ProductUnits.UnitId IS NULL))


GO




/****** Object:  View [dbo].[ProductList]    Script Date: 29.1.2016 18:30:04 ******/
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
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 (dbo.ProductMarks.IsActive = 1) 
                         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.MarkId IS NULL)


GO
 
  
/****** Object:  View [dbo].[ProductListDetails5]    Script Date: 29.1.2016 18:30: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
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].[ProductSizeList]    Script Date: 29.1.2016 18:31:03 ******/
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
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:  View [dbo].[ProductsSitemap]    Script Date: 29.1.2016 18:31:21 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

/* View*/
ALTER VIEW [dbo].[ProductsSitemap]
AS
SELECT        dbo.Categories.CatId, dbo.Categories.CatName, dbo.Products.ProductId, dbo.Categories.PortalId, dbo.Categories.Url AS CatUrl, dbo.Products.Url AS ProductUrl, dbo.Products.ProductName
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 LEFT OUTER JOIN
                         dbo.Currencys AS Currencys_0 ON dbo.ProductUnits.Pb0 = Currencys_0.CurrencyId
WHERE        (dbo.Categories.IsActive = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1) OR
                         (dbo.Categories.IsActive = 1) AND (dbo.ProductUnits.IsDefault = 1) AND (dbo.Products.IsActive = 1)

GO


 
/****** Object:  StoredProcedure [dbo].[AddAlternativeMarketsCatVariantSpec]    Script Date: 29.1.2016 18:34:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AddAlternativeMarketsCatVariantSpec]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [dbo].[AddAlternativeMarketsCatVariantSpec]
(
@CatVariantSpecName			NVARCHAR(150),
@CatVariantSpecNameId		NVARCHAR(1500),
@CatVariantSpecOrderNumber	INT,
@CatVariantSpecValue		NVARCHAR(1500),
@CatVariantSpecValueId		NVARCHAR(1500),
@CatId						NVARCHAR(1500),
@CatName					NVARCHAR(1500),
@Ind			INT OUTPUT
)
AS


IF NOT EXISTS(SELECT Ind FROM AlternativeMarketsCatVariantSpec WHERE CatVariantSpecName=@CatVariantSpecName AND CatVariantSpecValue=@CatVariantSpecValue)
BEGIN
INSERT INTO [dbo].[AlternativeMarketsCatVariantSpec]
           ([CatVariantSpecName]
           ,[CatVariantSpecNameId]
           ,[CatVariantSpecOrderNumber]
           ,[CatVariantSpecValue]
           ,[CatVariantSpecValueId]
           ,[CatId]
           ,[CatName])
     VALUES
           (@CatVariantSpecName,
            '',''+@CatVariantSpecNameId,
            @CatVariantSpecOrderNumber,
            @CatVariantSpecValue,
            '',''+@CatVariantSpecValueId,
            '',''+@CatId,
			'',''+@CatName)

SELECT
    @Ind = @@Identity
END
ELSE
BEGIN
SELECT @Ind=Ind FROM AlternativeMarketsCatVariantSpec WHERE CatVariantSpecName=@CatVariantSpecName AND CatVariantSpecValue=@CatVariantSpecValue

IF NOT EXISTS(SELECT Ind FROM AlternativeMarketsCatVariantSpec WHERE CatVariantSpecName=@CatVariantSpecName AND CatVariantSpecValue=@CatVariantSpecValue AND CatId LIKE ''%,'' + @CatId + ''%'')
BEGIN

UPDATE AlternativeMarketsCatVariantSpec SET CatVariantSpecNameId=CatVariantSpecNameId + @CatVariantSpecNameId WHERE CatVariantSpecName=@CatVariantSpecName AND CatVariantSpecValue=@CatVariantSpecValue

END

IF NOT EXISTS(SELECT Ind FROM AlternativeMarketsCatVariantSpec WHERE CatVariantSpecName=@CatVariantSpecName AND CatVariantSpecValue=@CatVariantSpecValue AND CatId LIKE ''%,'' + @CatId + ''%'')
BEGIN

UPDATE AlternativeMarketsCatVariantSpec SET CatVariantSpecValueId=CatVariantSpecValueId + @CatVariantSpecValueId WHERE CatVariantSpecName=@CatVariantSpecName AND CatVariantSpecValue=@CatVariantSpecValue

END


IF NOT EXISTS(SELECT Ind FROM AlternativeMarketsCatVariantSpec WHERE CatVariantSpecName=@CatVariantSpecName AND CatVariantSpecValue=@CatVariantSpecValue AND CatId LIKE ''%,'' + @CatId + ''%'')
BEGIN

UPDATE AlternativeMarketsCatVariantSpec SET CatId=CatId + @CatId WHERE CatVariantSpecName=@CatVariantSpecName AND CatVariantSpecValue=@CatVariantSpecValue

END


IF NOT EXISTS(SELECT Ind FROM AlternativeMarketsCatVariantSpec WHERE CatVariantSpecName=@CatVariantSpecName AND CatVariantSpecValue=@CatVariantSpecValue AND CatName LIKE ''%,'' + @CatName + ''%'')
BEGIN

UPDATE AlternativeMarketsCatVariantSpec SET CatName=CatName + @CatName WHERE CatVariantSpecName=@CatVariantSpecName AND CatVariantSpecValue=@CatVariantSpecValue

END

SELECT @Ind
END



' 
END
GO
 
/****** Object:  StoredProcedure [dbo].[ProductListHome]    Script Date: 23.2.2016 16:12:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ProductListHome]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [dbo].[ProductListHome]
(
      @CartId nvarchar(50),
      @StokIndirim int,
      @StokIskActive bit,
      @LangCode nvarchar(50), 
      @UserIndirim int =0,
      @CatId int,
	  @Top int=300  
)
AS



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

INSERT #TempTable(PriceId,Price,DiscountType,Discount,ProductId)

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

BEGIN

SELECT distinct top (@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.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,
''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 LEFT OUTER JOIN 
#TempTable AS Temp ON Temp.ProductId = pl.ProductId LEFT OUTER JOIN 
Product3dPictures ON pl.ProductId = 0 

WHERE pl.CatId=@CatId

END

ELSE

BEGIN

SELECT distinct top 50   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.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,
''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 LEFT OUTER JOIN 
#TempTable AS Temp ON Temp.ProductId = pl.ProductId LEFT OUTER JOIN 
Product3dPictures ON pl.ProductId = Product3dPictures.ProductId 
WHERE pl.ShowInMainPage =1 and pl.CatId=(select min(p2.CategoryId) from ProductCategories p2 where  pl.ProductId=p2.ProductId)
END

DROP TABLE #TempTable' 
END
GO
 
/****** Object:  StoredProcedure [dbo].[ShoppingCartPriceList]    Script Date: 29.1.2016 18:34:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ShoppingCartPriceList]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[ShoppingCartPriceList] 
    @CartId nvarchar(50), 
	@Result2 int output
AS
 
declare @PriceId  int
declare @ProdId  int
declare @PriceCode  nvarchar(50)
declare @UserFilter nvarchar(50) 
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),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)
set @ExcludedList=''select  @result =count(UserId) from users where '' + @UserFilter + ''  and UserId='' + @CartId  
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,ProductId)
  select top 1 PriceId,Price,DiscountType,Discount,@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 

 ' 
END
GO
/****** Object:  UserDefinedFunction [dbo].[CalcPrice]    Script Date: 29.1.2016 18:34:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CalcPrice]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'
-- Function
CREATE FUNCTION [dbo].[CalcPrice] 
	(
	@value decimal(18,4)=0,
	@type int=0,
	@rate decimal(18,4)=0
	)
RETURNS decimal(18,4)
AS

BEGIN
	declare @retvalue decimal(18,4)
	
		IF @type=0
		
			If @rate <> 0 
				SET @retvalue =  (@value - ((@value * @rate) / 100)) 
			ELSE
				SET @retvalue = @value
		
	   
		ELSE
			SET @retvalue = @value - @rate  
			If @retvalue < 0
					SET @retvalue =  0
				 
	RETURN @retvalue
END	 
	 

 
  
 

' 
END

GO
 
/****** Object:  StoredProcedure [dbo].[GetHtmlText]    Script Date: 30.1.2016 09:07:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[GetHtmlText]
(
    @ModuleId int,
	@LanguageId int,
	@LanguageCode nvarchar(25)
)
AS

SELECT
    *

FROM
    HtmlText

WHERE
    ModuleId = @ModuleId
	AND
	LanguageCode=@LanguageCode


GO
 
/****** Object:  StoredProcedure [dbo].[GetPortalSettings5b]    Script Date: 30.1.2016 09:08:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

 
/****** Object:  StoredProcedure [dbo].[GetProductDetails5]    Script Date: 30.1.2016 09:08:45 ******/
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,Code14,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,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].[OrdersAdd6]    Script Date: 30.1.2016 09:09:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[OrdersAdd6]
(
    @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),
    @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 (@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, 
 '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' 
GO

   
/****** Object:  StoredProcedure [dbo].[ShoppingCartAddItem]    Script Date: 23.2.2016 16:17:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[ShoppingCartAddItem]
(
    @CartId nvarchar(50),
    @ShoppingCartDefId as int,
    @ProdId int,
	@Qty decimal(18,4),
	@PriceId int=0, 
	@Description nvarchar(500)='',
	@Attributes nvarchar(255)='',
	@ValueAdd decimal(18,4)=0,
	@ValueAddNoQty decimal(18,4)=0,
	@NewDesi decimal(18,2)=0,
	@ProductSizeId int=0,
	@StorageId int ,
	@CartTimeOut datetime,
	@Return int OUTPUT
)
As
-- do not allow to buy or add product is not in stock (ferit)
DECLARE @Stock decimal(18,4)
Declare @SellNoneStock bit
declare @SellLimitToUser tinyint
declare @Price_Id int
DECLARE @CountItems decimal(18,4)
DECLARE @Carpan decimal(18,4)
DECLARE @InCart decimal(18,4)
DECLARE @PartSale bit
 
if @PriceId=0
begin
select @Price_Id=UnitId,@PartSale=isnull(PartSale,0) from ProductUnits where IsDefault=1 and ProductId=@ProdId
end
else
Begin 
select @Price_Id=UnitId,@PartSale=isnull(PartSale,0) from ProductUnits where UnitId=@PriceId  
end

 

if @ProductSizeId=0
	Begin
	SELECT @Stock = StockQty,@SellNoneStock=SellNoneStock,@SellLimitToUser=isnull(SellLimitToUser,0) FROM Products	WHERE ProductId = @ProdId    
	SELECT @CountItems = isnull(sum(Qty),0) FROM ShoppingCart WHERE ProdId = @ProdId  and ShoppingCartDefId=@ShoppingCartDefId AND CartId = @CartId AND Description=@Description and PriceId=@Price_Id 
	SELECT @InCart = isnull(sum(Qty * Carpan),0) from ShoppingCart as c inner join ProductUnits as u on c.ProdId=u.ProductId  and c.PriceId=u.UnitId  WHERE  ProdId = @ProdId  and ShoppingCartDefId=@ShoppingCartDefId  AND CartId = @CartId 
	SELECT @Carpan=Carpan from ProductUnits  WHERE ProductId  = @ProdId AND UnitId=@Price_Id
	End
Else
	Begin
	SELECT @Stock = ProductSizes.StockQty,@SellNoneStock=SellNoneStock,@SellLimitToUser=isnull(SellLimitToUser,0) FROM Products inner join ProductSizes on Products.ProductId=ProductSizes.ProductId WHERE Products.ProductId = @ProdId and ProductSizeId=@ProductSizeId   
	SELECT @CountItems = isnull(sum(Qty),0) FROM ShoppingCart WHERE ProdId = @ProdId AND PriceId=@Price_Id and ShoppingCartDefId=@ShoppingCartDefId AND CartId = @CartId and ProductSizeId=@ProductSizeId 
	SELECT @InCart = isnull(sum(Qty * Carpan),0) from ShoppingCart as c inner join ProductUnits as u on c.ProdId=u.ProductId  and c.PriceId=u.UnitId  WHERE  ProdId = @ProdId  and ShoppingCartDefId=@ShoppingCartDefId  AND CartId = @CartId  and ProductSizeId=@ProductSizeId 
	SELECT @Carpan=Carpan from ProductUnits  WHERE ProductId  = @ProdId AND UnitId=@Price_Id
	End

 -- do not allow to buy or add product is not alloved max qty  
if @SellLimitToUser>0	
	Begin
		Declare @SellCount int
		select @SellCount=SUM(Quantity * Carpan) from orderList
		where ProductId=@ProdId and CustomerId=@CartId group by CustomerId
		
		if isnull(@SellCount,0)+isnull(@CountItems,0)+ (@Qty *@Carpan) > @SellLimitToUser
			Begin	
			SELECT @Return=2
			UPDATE ShoppingCart SET Qty = @SellLimitToUser 
			WHERE ProdId = @ProdId AND PriceId=@Price_Id AND CartId = @CartId and ShoppingCartDefId=@ShoppingCartDefId and Attributes=@Attributes and Description=@Description
			RETURN 			
			End
	end 
-- do not allow to buy or add product is not in stock  
IF @CountItems > 0  /* There are items - update the current quantity */
 BEGIN
	If ((@Stock >=(@InCart + (@Qty * @Carpan)))or (@SellNoneStock=1))
    	BEGIN    	
		UPDATE ShoppingCart SET Qty = (@Qty + ShoppingCart.Qty)
   		WHERE ProdId = @ProdId AND PriceId=@Price_Id AND CartId = @CartId and ShoppingCartDefId=@ShoppingCartDefId and Attributes=@Attributes and Description=@Description
		SELECT @Return=1
		END
	else
	begin
	SELECT @Return=0
	end
END
ELSE  /* New entry for this Cart.  Add a new record */
BEGIN
	If ((@Stock >=((@Qty * @Carpan) + @InCart)) or (@SellNoneStock=1))
		BEGIN   	
   		 INSERT INTO ShoppingCart (CartId,ShoppingCartDefId,Qty,ProdId,PriceId,Description,Attributes,ValueAdd,NewDesi,ProductSizeId,CartTimeOut,StorageId,ValueAddNoQty)
   		 VALUES (@CartId,@ShoppingCartDefId,@Qty,@ProdId,@Price_Id,@Description,@Attributes,@ValueAdd,@NewDesi,@ProductSizeId,@CartTimeOut,@StorageId,@ValueAddNoQty)
		SELECT @Return=(select top 1 RecordId from ShoppingCart order by RecordId desc)
		END
	ELSE
		SELECT @Return=0
END

 

 
GO

 
/****** Object:  StoredProcedure [dbo].[ShoppingCartList]    Script Date: 30.1.2016 09:10:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[ShoppingCartList]
(
    @CartId nvarchar(50),
    @ShoppingCartDefId int,
    @StokIndirim int,
    @StokIskActive bit
)
AS
SELECT   DISTINCT   dbo.ShoppingCart.RecordId,dbo.ShoppingCart.CartId, dbo.Products.ProductId, dbo.Products.ProductCode, dbo.Products.ProductName, dbo.Products.StockQty, 
                      dbo.ProductKdvs.KdvRate,ProductUnits.UnitId, dbo.ProductUnits.BirimAdi, dbo.ProductUnits.Carpan, 
                     '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 ,  
                      dbo.ShoppingCart.Qty, dbo.ProductUnits.Agirlik, dbo.ProductUnits.Hacim, dbo.Products.Provider, Currencys_2.CurrencyName, Currencys_2.Rate,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
                      , Currencys.Rate AS Rate0, ProductUnits.SatisFiyati0
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 end
                      --dbo.ProductUnits.Pb1 
                      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 ProductImages on Products.ProductId=ProductImages.ProductId  LEFT OUTER JOIN
                      Currencys ON ProductUnits.Pb0 = Currencys.CurrencyId
  LEFT OUTER JOIN
                      dbo.ProductMarks ON dbo.Products.MarkId = dbo.ProductMarks.MarkId
					   LEFT OUTER JOIN ProductSizes on ShoppingCart.ProductSizeId=ProductSizes.ProductSizeId 
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)




GO

 
/****** Object:  StoredProcedure [dbo].[ShoppingCartList5]    Script Date: 30.1.2016 09:10:35 ******/
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),ProductId int) 

INSERT #TempTable(PriceId,Price,DiscountType,Discount,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.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    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.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    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.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    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: 30.1.2016 09:11:02 ******/
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
--Print @ProdId 
--Print @PriceId 
--Print @Qty
--Print '�r�n'
   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   
--exec ShoppingCartEmpty @OriginalCartId,0


GO

 
/****** Object:  StoredProcedure [dbo].[ShoppingCartUpdate2]    Script Date: 30.1.2016 09:11:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[ShoppingCartUpdate2]
(
    @RecordId int,
    @CartId    nvarchar(50),
    @ProdId int,
    @ShoppingCartDefId int,
    @Qty  decimal(18,4),
	@PriceId int=0, 
    @Return tinyint OUTPUT
)
AS
--do not allow to update more quantity than stock (ferit)
declare @Stock decimal(18,4)
declare @SellNoneStock bit
declare @SellLimitToUser tinyint
declare @Price_Id int
declare @ProductSizeId int
DECLARE @CountItems decimal(18,4)
DECLARE @Carpan decimal(18,4)

if @PriceId=0
begin
select @Price_Id=UnitId from ProductUnits where IsDefault=1 and ProductId=@ProdId
end
else
Begin
select @Price_Id=@PriceId 
end

SELECT @Stock = StockQty,@SellNoneStock=SellNoneStock,@SellLimitToUser=isnull(SellLimitToUser,0) FROM Products WHERE ProductId = @ProdId
--Tekstil Control (ferit)
Select @ProductSizeId=ProductSizeId from ShoppingCart where RecordId=@RecordId
if @ProductSizeId=0
	Begin
	SELECT @Stock = StockQty,@SellNoneStock=SellNoneStock,@SellLimitToUser=isnull(SellLimitToUser,0) FROM Products	WHERE ProductId = @ProdId    
	SELECT @CountItems = isnull(sum(Qty * Carpan),0) from ShoppingCart as c inner join ProductUnits as u on c.ProdId=u.ProductId  and c.PriceId=u.UnitId  WHERE  ProdId = @ProdId  and ShoppingCartDefId=@ShoppingCartDefId  AND CartId = @CartId and PriceId<>@Price_Id 
	SELECT @Carpan=Carpan from ProductUnits  WHERE ProductId  = @ProdId AND UnitId=@Price_Id
	End
Else
	Begin
	SELECT @Stock = ProductSizes.StockQty,@SellNoneStock=SellNoneStock,@SellLimitToUser=isnull(SellLimitToUser,0) FROM Products inner join ProductSizes on Products.ProductId=ProductSizes.ProductId WHERE Products.ProductId = @ProdId and ProductSizeId=@ProductSizeId   
	--SELECT @CountItems = isnull(sum(Qty),0) FROM ShoppingCart WHERE  ProdId = @ProdId AND PriceId=@Price_Id and ShoppingCartDefId=@ShoppingCartDefId  AND CartId = @CartId  and ProductSizeId=@ProductSizeId
	SELECT @CountItems = isnull(sum(Qty * Carpan),0) from ShoppingCart as c inner join ProductUnits as u on c.ProdId=u.ProductId  and c.PriceId=u.UnitId  WHERE  ProdId = @ProdId  and ShoppingCartDefId=@ShoppingCartDefId  AND CartId = @CartId and PriceId<>@Price_Id and ProductSizeId=@ProductSizeId
	SELECT @Carpan=Carpan from ProductUnits  WHERE ProductId  = @ProdId AND UnitId=@Price_Id 
	End

  
if @SellLimitToUser>0
Begin
	Declare @SellCount int
	select @SellCount=SUM(Quantity * Carpan) from orderList where ProductId=@ProdId and CustomerId=@CartId group by CustomerId

	if isnull(@SellCount,0)+ (@Qty *@Carpan) > @SellLimitToUser
		Begin
		SELECT @Return=2
		UPDATE ShoppingCart SET Qty = @SellLimitToUser
		WHERE CartId = @CartId AND PriceId=@Price_Id AND ProdId = @ProdId and ShoppingCartDefId=@ShoppingCartDefId and RecordId=@RecordId
		RETURN
	End
end 
-- do not allow to buy or add product is not in stock (ferit)
If ((@Stock >=(@CountItems + (@Qty * @Carpan)))or (@SellNoneStock=1))
--IF ((@Stock >=  @Qty) or (@SellNoneStock=1))
BEGIN
	SELECT @Return=1
	UPDATE ShoppingCart SET Qty = @Qty
	WHERE CartId = @CartId AND PriceId=@Price_Id AND ProdId = @ProdId and ShoppingCartDefId=@ShoppingCartDefId and RecordId=@RecordId
END
ELSE
SELECT @Return=0

 
GO

 
/****** Object:  StoredProcedure [dbo].[UpdateHtmlText2]    Script Date: 30.1.2016 09:11:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[UpdateHtmlText2]
(
    @ModuleId      int,
    @DesktopHtml   ntext,
    @MobileSummary ntext,
    @MobileDetails ntext,
    @OnlyMainPage bit,
    @ShadowPage int,
    @ShadowModule int,
	@LanguageId int,
    @LanguageCode nvarchar(25)
)
AS

IF NOT EXISTS (
    SELECT 
        * 
    FROM 
        HtmlText 
    WHERE 
        ModuleId = @ModuleId 
		AND
		LanguageCode = @LanguageCode
)
INSERT INTO HtmlText (
    ModuleId,
    DesktopHtml,
    MobileSummary,
    MobileDetails,
    OnlyMainPage,
    ShadowPage,
    ShadowModule,
	LanguageId,
	LanguageCode
) 
VALUES (
    @ModuleId,
    @DesktopHtml,
    @MobileSummary,
    @MobileDetails,
    @OnlyMainPage,
    @ShadowPage,
    @ShadowModule,
	@LanguageId,
	@LanguageCode
)
ELSE
UPDATE
    HtmlText

SET
    DesktopHtml   = @DesktopHtml,
    MobileSummary = @MobileSummary,
    MobileDetails = @MobileDetails,
    OnlyMainPage = @OnlyMainPage,
    ShadowPage=@ShadowPage,
    ShadowModule=@ShadowModule

WHERE
    ModuleId = @ModuleId
	AND
	LanguageCode = @LanguageCode


GO


  
/****** Object:  StoredProcedure [dbo].[CopyProduct]    Script Date: 23.2.2016 16:09:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[CopyProduct]
(
    @ProductId int,
    @NewProductId int OUTPUT,
    @NewPath varchar(100) OUTPUT
)
AS
insert into Products(PortalId, CategoryId, Parent, ProductCode, ProductName, Description, Details, ProviderStockCode, ProviderName, MarkId, IsDeleted, IsActive, SellNoneStock, StockQty, ShowInMainPage, ShowInCategoryPage, ShowOrder, Code1, Code2, Code3, Code4, Code5, Code6, Code7, Code8, Code9, Code10, Code11, Code12, Code13, Code14, Code15, Provider, KdvId, Hit, IsPenta, CreatedDate,HizliKargo, AyniGunTeslim, IndirimliUrun, FirsatUrunu, SokFiyatliUrun, EnUcuzUrun, OzelUrun, SinirliSayidaUrun, YeniUrun, HediyeliUrun,ProviderId,UserId,StokType,StokSubType,ProductSizeOptionId)
select PortalId, CategoryId, Parent, NEWID() , ProductName, Description, Details, ProviderStockCode, ProviderName, MarkId, IsDeleted, IsActive, SellNoneStock, StockQty, ShowInMainPage, ShowInCategoryPage, ShowOrder, Code1, Code2, Code3, Code4, Code5, Code6, Code7, Code8, Code9, Code10, Code11, Code12, Code13, Code14, Code15, Provider, KdvId, Hit, IsPenta, GETDATE(),HizliKargo, AyniGunTeslim, IndirimliUrun, FirsatUrunu, SokFiyatliUrun, EnUcuzUrun, OzelUrun, SinirliSayidaUrun, YeniUrun, HediyeliUrun,ProviderId,UserId,StokType,StokSubType,ProductSizeOptionId
FROM Products where ProductId=@ProductId
select @NewProductId=@@Identity

insert into ProductCategories(ProductId,CategoryId)
select @NewProductId,CategoryId FROM ProductCategories where ProductId=@ProductId

insert into ProductUnits(ProductId, Aciklama, BirimAdi, Carpan, AlisFiyati, APb,PiyasaFiyati,PPb,SatisFiyati0,Pb0, SatisFiyati1, Pb1, Isk1, SatisFiyati2, Pb2, Isk2, SatisFiyati3, Pb3, Isk3, SatisFiyati4, Pb4, Isk4, SatisFiyati5, Pb5, Isk5, Barcode, En, Boy, Yukseklik, Agirlik, Hacim, Desi, Indirim1, IsDefault, IsActive, LastModified)
select @NewProductId, Aciklama, BirimAdi, Carpan, AlisFiyati, APb,PiyasaFiyati,PPb,SatisFiyati0,Pb0, SatisFiyati1, Pb1, Isk1, SatisFiyati2, Pb2, Isk2, SatisFiyati3, Pb3, Isk3,SatisFiyati4, Pb4, Isk4, SatisFiyati5, Pb5, Isk5, Barcode, En, Boy, Yukseklik, Agirlik, Hacim, Desi, Indirim1, IsDefault, IsActive, LastModified
from ProductUnits where ProductId=@ProductId

insert into ProductUsages(ProductId,UProductId)
select @NewProductId, UProductId
FROM ProductUsages where ProductId=@ProductId

--insert into ProductImages(ProductId, Description, Path, IsDefault)
--select @NewProductId, Description, Path, IsDefault
--FROM ProductImages where ProductId=@ProductId
select @NewPath=Path FROM ProductImages where ProductId=@ProductId and IsDefault=1

insert into ProductFiles(ProductId, Description, Path, IsDefault)
 select @NewProductId, Description, Path, IsDefault
FROM ProductFiles where ProductId=@ProductId

insert into ProductAlternatives(ProductId, AProductId)
select @NewProductId,AProductId
FROM ProductAlternatives where ProductId=@ProductId

insert into ProductNonAlternatives(ProductId, NAProductId)
select @NewProductId,NAProductId
FROM ProductNonAlternatives where ProductId=@ProductId


INSERT INTO MultiDiscountList (StartQty,EndQty,DiscountType,DiscountAmount,UnitPrice,IsActive,ProductId,UnitId,UnitName)
(SELECT StartQty,EndQty,DiscountType,DiscountAmount,UnitPrice,IsActive,@NewProductId,UnitId,UnitName FROM MultiDiscountList Where ProductId=@ProductId)


INSERT INTO ProductDescription (ProductId,LanguageId,LanguageCode,ProductName,Description,Details,SearchKeywords,Url)
(SELECT @NewProductId,LanguageId,LanguageCode,ProductName,Description,Details,SearchKeywords,Url FROM ProductDescription Where ProductId=@ProductId)


INSERT INTO ProductSizes (ProductId,ProductSizeOptionId,ProductSize0ValueId,ProductSize1ValueId,ProductSize2ValueId,ProductImageId,StockQty,Barcode,StockQty2,StockCheck,StockChange,StokEndDate,AlternativeMarketsVariantId)
(SELECT @NewProductId,ProductSizeOptionId,ProductSize0ValueId,ProductSize1ValueId,ProductSize2ValueId,ProductImageId,StockQty,Barcode,StockQty2,StockCheck,StockChange,StokEndDate,AlternativeMarketsVariantId FROM ProductSizes Where ProductId=@ProductId)

GO
 
 
/****** Object:  StoredProcedure [dbo].[ShoppingCartList5]    Script Date: 3.2.2016 16:25:07 ******/
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),ProductId int) 

INSERT #TempTable(PriceId,Price,DiscountType,Discount,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.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    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.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    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.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    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].[ShoppingCartUpdate2]    Script Date: 3.2.2016 16:25:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[ShoppingCartUpdate2]
(
    @RecordId int,
    @CartId    nvarchar(50),
    @ProdId int,
    @ShoppingCartDefId int,
    @Qty  decimal(18,4),
	@PriceId int=0, 
    @Return tinyint OUTPUT
)
AS
--do not allow to update more quantity than stock (ferit)
declare @Stock decimal(18,4)
declare @SellNoneStock bit
declare @SellLimitToUser tinyint
declare @Price_Id int
declare @ProductSizeId int
DECLARE @CountItems decimal(18,4)
DECLARE @Carpan decimal(18,4)
DECLARE @PartSale bit

if @PriceId=0
begin
select @Price_Id=UnitId,@PartSale=isnull(PartSale,0) from ProductUnits where IsDefault=1 and ProductId=@ProdId
end
else
Begin 
select @Price_Id=UnitId,@PartSale=isnull(PartSale,0) from ProductUnits where UnitId=@PriceId  
end

if (@PartSale = 0 or @ProductSizeId<>0)
 select  @Qty = cast(@Qty as integer)
 

SELECT @Stock = StockQty,@SellNoneStock=SellNoneStock,@SellLimitToUser=isnull(SellLimitToUser,0) FROM Products WHERE ProductId = @ProdId
--Tekstil Control (ferit)
Select @ProductSizeId=ProductSizeId from ShoppingCart where RecordId=@RecordId
if @ProductSizeId=0
	Begin
	SELECT @Stock = StockQty,@SellNoneStock=SellNoneStock,@SellLimitToUser=isnull(SellLimitToUser,0) FROM Products	WHERE ProductId = @ProdId    
	SELECT @CountItems = isnull(sum(Qty * Carpan),0) from ShoppingCart as c inner join ProductUnits as u on c.ProdId=u.ProductId  and c.PriceId=u.UnitId  WHERE  ProdId = @ProdId  and ShoppingCartDefId=@ShoppingCartDefId  AND CartId = @CartId and PriceId<>@Price_Id 
	SELECT @Carpan=Carpan from ProductUnits  WHERE ProductId  = @ProdId AND UnitId=@Price_Id
	End
Else
	Begin
	SELECT @Stock = ProductSizes.StockQty,@SellNoneStock=SellNoneStock,@SellLimitToUser=isnull(SellLimitToUser,0) FROM Products inner join ProductSizes on Products.ProductId=ProductSizes.ProductId WHERE Products.ProductId = @ProdId and ProductSizeId=@ProductSizeId   
	--SELECT @CountItems = isnull(sum(Qty),0) FROM ShoppingCart WHERE  ProdId = @ProdId AND PriceId=@Price_Id and ShoppingCartDefId=@ShoppingCartDefId  AND CartId = @CartId  and ProductSizeId=@ProductSizeId
	SELECT @CountItems = isnull(sum(Qty * Carpan),0) from ShoppingCart as c inner join ProductUnits as u on c.ProdId=u.ProductId  and c.PriceId=u.UnitId  WHERE  ProdId = @ProdId  and ShoppingCartDefId=@ShoppingCartDefId  AND CartId = @CartId and PriceId<>@Price_Id and ProductSizeId=@ProductSizeId
	SELECT @Carpan=Carpan from ProductUnits  WHERE ProductId  = @ProdId AND UnitId=@Price_Id 
	End

  
if @SellLimitToUser>0
Begin
	Declare @SellCount int
	select @SellCount=SUM(Quantity * Carpan) from orderList where ProductId=@ProdId and CustomerId=@CartId group by CustomerId

	if isnull(@SellCount,0)+ (@Qty *@Carpan) > @SellLimitToUser
		Begin
		SELECT @Return=2
		UPDATE ShoppingCart SET Qty = @SellLimitToUser
		WHERE CartId = @CartId AND PriceId=@Price_Id AND ProdId = @ProdId and ShoppingCartDefId=@ShoppingCartDefId and RecordId=@RecordId
		RETURN
	End
end 
-- do not allow to buy or add product is not in stock (ferit)
If ((@Stock >=(@CountItems + (@Qty * @Carpan)))or (@SellNoneStock=1))
--IF ((@Stock >=  @Qty) or (@SellNoneStock=1))
BEGIN
	SELECT @Return=1
	UPDATE ShoppingCart SET Qty = @Qty
	WHERE CartId = @CartId AND PriceId=@Price_Id AND ProdId = @ProdId and ShoppingCartDefId=@ShoppingCartDefId and RecordId=@RecordId
END
ELSE
SELECT @Return=0

 
GO

BEGIN TRY
 INSERT INTO [ProductDescription] ([ProductId],[LanguageId],[LanguageCode], [ProductName], [Description], [Details], [SearchKeywords], [Url]) 
 (SELECT [ProductId], 1, 'tr' , [ProductName], [Description], [Details], [SearchKeywords], [Url] FROM Products) 
END TRY
BEGIN CATCH 
END CATCH

GO

 update ProductKeywords set LanguageId=1,LanguageCode='tr'
 Update HtmlText SET LanguageId=1,LanguageCode='tr'
 
 INSERT INTO [SliderDescription] ([SlideId],[SlideTitle],[SlideImage],[SlideUrl],[SlideHtml],[LanguageId],[LanguageCode])
 SELECT  [SlideId],[SlideTitle],[SlideImage],[SlideUrl],[SlideHtml],1,'tr' FROM Slider
 
 BEGIN TRY
 INSERT INTO [CategoryDescription]([CatId],[LanguageId],[LanguageCode],[CatName],[Title],[Keywords],[Parent],[SearchKeywords])
Select [CatId], 1, 'tr' ,[CatName],[Title],[Keywords],[Parent],[SearchKeywords] From Categories
END TRY
BEGIN CATCH 
END CATCH
GO


UPDATE Portals Set Version='3.3.0'
 
GO
  
/****** Object:  StoredProcedure [dbo].[ProductListHome]    Script Date: 3.3.2016 15:41:41 ******/
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,
	  @Top int=300  
)
AS



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

INSERT #TempTable(PriceId,Price,DiscountType,Discount,ProductId)

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

BEGIN

SELECT distinct top (@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 (@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,
'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 LEFT OUTER JOIN 
#TempTable AS Temp ON Temp.ProductId = pl.ProductId LEFT OUTER JOIN 
Product3dPictures ON pl.ProductId = 0 

WHERE pl.CatId=@CatId and pl.LanguageCode=@LangCode 

END

ELSE

BEGIN

SELECT distinct top 50   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,
'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 LEFT OUTER JOIN 
#TempTable AS Temp ON Temp.ProductId = pl.ProductId LEFT OUTER JOIN 
Product3dPictures ON pl.ProductId = Product3dPictures.ProductId 
WHERE pl.ShowInMainPage =1 and pl.CatId=(select min(p2.CategoryId) from ProductCategories p2 where  pl.ProductId=p2.ProductId)  and pl.LanguageCode=@LangCode 
END

DROP TABLE #TempTable

GO