CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW supplyOffer
AS SELECT sl.supplyLineId,
		ti.name tradeItemName,
		i.product_name vbnProductName,
		o.commercialName organizationName,
		sl.`type`,
		sl.salesUnit,
		CAST(slvp.pricePerPiece AS DECIMAL(10, 3)) pricePerPiece,
		sl.numberOfPieces,
		ti.vbnProductCode,
		slpc.packageVbnPackageCode,
		(
			SELECT tic.vbnCode
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 1
					AND it.`expiry_date` IS NULL
				LIMIT 1
		) tag1,
		(
			SELECT v.type_description
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
					JOIN edi.value v ON v.type_value = tic.vbnValueCode
						AND tic.vbnCode = v.type_id
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 1
					AND it.`expiry_date` IS NULL
				LIMIT 1
		) value1,
		(
			SELECT tic.vbnCode
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 2
					AND it.`expiry_date` IS NULL
				LIMIT 1
		) tag2,
		(
			SELECT v.type_description
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
					JOIN edi.value v ON v.type_value = tic.vbnValueCode
						AND tic.vbnCode = v.type_id
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 2
					AND it.`expiry_date` IS NULL
			LIMIT 1
		) value2,
		(
			SELECT tic.vbnCode
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 3
					AND it.`expiry_date` IS NULL
				LIMIT 1
		) tag3,
		(
			SELECT v.type_description
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
					JOIN edi.value v ON v.type_value = tic.vbnValueCode
						AND tic.vbnCode = v.type_id
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 3
					AND it.`expiry_date` IS NULL
				LIMIT 1
		) value3,
		(
			SELECT tic.vbnCode
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 4
					AND it.`expiry_date` IS NULL
				LIMIT 1
		) tag4,
		(
			SELECT v.type_description
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
					JOIN edi.value v ON v.type_value = tic.vbnValueCode
						AND tic.vbnCode = v.type_id
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 4
					AND it.`expiry_date` IS NULL
				LIMIT 1
		) value4,
		(
			SELECT tic.vbnCode
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 5
					AND it.`expiry_date` IS NULL
				LIMIT 1
		) tag5,
		(
			SELECT v.type_description
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
					JOIN edi.value v ON v.type_value = tic.vbnValueCode
						AND tic.vbnCode = v.type_id
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 5
					AND it.`expiry_date` IS NULL
				LIMIT 1
		) value5,
		(
			SELECT tic.vbnCode
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 6
					AND it.`expiry_date` IS NULL
				LIMIT 1
		) tag6,
		(
			SELECT v.type_description
				FROM tradeItemCharacteristic tic
					JOIN edi.item_feature it ON it.feature = tic.vbnCode
					JOIN edi.value v ON v.type_value = tic.vbnValueCode
						AND tic.vbnCode = v.type_id
				WHERE tic.tradeItemId = ti.tradeItemId
					AND it.presentation_order = 6
					AND it.`expiry_date` IS NULL
				LIMIT 1
		) value6,
		sl.deliveryPeriodStartDateTime,
		sl.deliveryPeriodEndDateTime,
		sl.orderPeriodStartDateTime,
		sl.orderPeriodEndDateTime,
		IF(slpc.supplyLineId IS NULL, FALSE, TRUE) hasPackingConfiguration,
		b.description bucketDescription,
		b.x_size,
		b.y_size,
		b.z_size,
		(b.x_size * b.y_size * b.z_size) / 1000000000 volume,
		slpc.piecesPerPackage,
		slpc.packagesPerLayer,
		slpc.layersPerLoadCarrier,
		slpc.transportHeightInCm,
		slpc.loadCarrierType,
		slpc.additionalPricePerPieceCurrency,
		slpc.additionalPricePerPieceValue,
		w.name warehouseName,
		tip.url imageUrl,
		igto.expenseFk,
		igto.intrastatFk,
		igto.originFk,
		IFNULL(idt.itemTypeFk, igto.itemTypeFk) itemTypeFk,
		sl.lastSync
	FROM supplyLine sl
		JOIN supplyLineVolumePrice slvp ON slvp.supplyLineId = sl.supplyLineId
		JOIN organization o ON o.organizationId = sl.organizationId
		JOIN tradeItem ti ON ti.tradeItemId = sl.tradeItemId
		LEFT JOIN tradeItemPhoto tip ON tip.tradeItemId = ti.tradeItemId
			AND tip.`primary`
		JOIN warehouse w ON w.warehouseId = sl.warehouseId
		JOIN supplyLinePackingConfiguration slpc ON slpc.supplyLineId = sl.supplyLineId
		LEFT JOIN edi.bucket b ON b.bucket_id = slpc.packageVbnPackageCode
		JOIN edi.item i ON i.id = ti.vbnProductCode
		LEFT JOIN edi.item_groupToOffer igto ON igto.group_code = i.group_id
		LEFT JOIN edi.item_defaultType idt ON idt.item_id = i.id
	WHERE sl.status = 'AVAILABLE'
		AND NOT sl.isDeleted
		AND NOT ti.isDeleted
		AND NOW() BETWEEN sl.orderPeriodStartDateTime AND sl.orderPeriodEndDateTime
		AND sl.numberOfPieces > 0
	GROUP BY ti.tradeItemId
	ORDER BY sl.lastSync DESC