IF EXISTS(SELECT 1 FROM sys.views WHERE Name = 'quotes_vQuoteBuyoutSuppliers')
DROP VIEW [dbo].[quotes_vQuoteBuyoutSuppliers]
GO

CREATE VIEW [dbo].[quotes_vQuoteBuyoutSuppliers]
AS
SELECT        
	supplier.itemID
	,supplier.isActive
	,supplier.surfaceId
	,supplier.ParentSurfaceItemId
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_SupplierTradingName
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierTelephone
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierEmail
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierContactPerson
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierVATRate
	,QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierCurrency = 
		CASE WHEN supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierCurrency = '' 
		THEN 'ZAR' 
		ELSE supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierCurrency 
		END
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierExchangeRate
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierDefaultDiscount
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierDefaultLeadtime
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierFreight
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierDuty
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierStatus
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutQuoteType
	,supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierNotes
	,supplier.recId

	,QuoteTypeDisplay = lookupBuyoutQuoteType.display
	,SupplierStatusDisplay = lookupBuyoutSupplierStatus.display
FROM            
	dbo.pal_SurfaceItem AS item 
	JOIN dbo.publ_QuoteBuyoutSuppliers AS supplier ON item.recId = supplier.itemID
	JOIN dbo.quotes_vQuotes AS quote ON supplier.ParentSurfaceItemId = quote.itemID
	LEFT OUTER JOIN dbo.pal_SurfaceLookup AS lookupBuyoutQuoteType ON supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutQuoteType = lookupBuyoutQuoteType.recId
	LEFT OUTER JOIN dbo.pal_SurfaceLookup AS lookupBuyoutSupplierStatus ON supplier.QuoteBuyoutSuppliers_QuoteBuyoutSuppliers_QuoteBuyoutSupplierStatus = lookupBuyoutSupplierStatus.recId
WHERE        
	(item.isActive = 1) 
	AND (item.isDeleted = 0) 
	AND (supplier.isActive = 1)

GO


