SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF (NOT EXISTS (SELECT * 
                 FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_SCHEMA = 'dbo' 
                 AND  TABLE_NAME = 'publ_PurchaseOrderInvoiceQuoteItemFinal'))
BEGIN
	CREATE TABLE [dbo].[publ_PurchaseOrderInvoiceQuoteItemFinal](
		[id] [int] IDENTITY(1,1) NOT NULL,
		[purchaseOrderId] [int] NOT NULL,
		[quoteItemFinalId] [int] NOT NULL,
		[received] INT NULL,
		[invoicePrice] DECIMAL(18,2) NULL,
		[locationBin] [nvarchar](100) NOT NULL,
	 CONSTRAINT [PK_publ_PurchaseOrderInvoiceQuoteItemFinal] PRIMARY KEY CLUSTERED 
	(
		[id] 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


IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE
	TABLE_NAME = 'publ_PurchaseOrders' AND
	COLUMN_NAME = 'currency')
BEGIN
	ALTER TABLE	[dbo].[publ_PurchaseOrders]
		ADD currency NVARCHAR(10) NULL
END
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE
	TABLE_NAME = 'publ_PurchaseOrders' AND
	COLUMN_NAME = 'purchaseOrderParentId')
BEGIN
	ALTER TABLE	[dbo].[publ_PurchaseOrders]
		ADD purchaseOrderParentId int NULL
END
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE
	TABLE_NAME = 'publ_PurchaseOrders' AND
	COLUMN_NAME = 'dueDate')
BEGIN
	ALTER TABLE	[dbo].[publ_PurchaseOrders]
		ADD dueDate date NULL
END
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE
	TABLE_NAME = 'publ_PurchaseOrders' AND
	COLUMN_NAME = 'exchangeRate')
BEGIN
	ALTER TABLE	[dbo].[publ_PurchaseOrders]
		ADD exchangeRate decimal(18,2) NULL
END
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE
	TABLE_NAME = 'publ_PurchaseOrders' AND
	COLUMN_NAME = 'freightCost')
BEGIN
	ALTER TABLE	[dbo].[publ_PurchaseOrders]
		ADD freightCost decimal(18,2) NULL
END
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE
	TABLE_NAME = 'publ_PurchaseOrders' AND
	COLUMN_NAME = 'dutiesCost')
BEGIN
	ALTER TABLE	[dbo].[publ_PurchaseOrders]
		ADD dutiesCost decimal(18,2) NULL
END
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE
	TABLE_NAME = 'publ_PurchaseOrders' AND
	COLUMN_NAME = 'waybillNumber')
BEGIN
	ALTER TABLE	[dbo].[publ_PurchaseOrders]
		ADD waybillNumber NVARCHAR(100) NULL
END
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE
	TABLE_NAME = 'publ_PurchaseOrders' AND
	COLUMN_NAME = 'invoiceTotal')
BEGIN
	ALTER TABLE	[dbo].[publ_PurchaseOrders]
		ADD invoiceTotal decimal(18,2) NULL
END
GO

