sozsoft-platform/configs/seeds/sql/object/Prc_V_PurchaseRfqComparison.sql
2026-10-11 22:57:23 +03:00

35 lines
2.7 KiB
Transact-SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- İhale kalemlerinin tedarikçi bazında kıyası. BaseId ihaledeki herhangi bir belgedir; her belge aynı tabloyu görür.
-- Fiyat kurla TL'ye çevrilir (Sas_H_Currency.Rate = 1 birimin TL karşılığı), termin ihalenin açılışından gün olarak ölçülür.
-- Puan = Fiyat ağırlığı × en düşük fiyat / fiyat + Termin ağırlığı × en kısa termin / termin (ağırlıklar ihalenin kök belgesinden).
-- Fiyatı girilmemiş, terminsiz, "Teklif Vermedi" ya da iptal edilmiş belgenin satırı puanlanmaz.
CREATE OR ALTER VIEW [dbo].[Prc_V_PurchaseRfqComparison] AS
WITH Lines AS (
SELECT b.Id AS BaseId, l.Id, l.TenantId, COALESCE(l.TenderLineId, l.Id) AS TenderLineId, l.PurchaseRfqId,
x.RfqNo, x.SupplierId, x.Status, x.SupplierResponse, l.ProductId, l.Description, l.Quantity, l.UomId,
l.UnitPrice, x.CurrencyId, CAST(l.UnitPrice * ISNULL(c.Rate, 1) AS decimal(18, 4)) AS PriceTry,
COALESCE(l.DeliveryDate, x.ExpectedArrival) AS DeliveryDate,
CASE WHEN DATEDIFF(day, g.CreationTime, COALESCE(l.DeliveryDate, x.ExpectedArrival)) < 1 THEN 1
ELSE DATEDIFF(day, g.CreationTime, COALESCE(l.DeliveryDate, x.ExpectedArrival)) END AS LeadDays,
g.PriceWeight, g.DeliveryWeight, l.IsSelected, l.IsDeleted,
CASE WHEN x.Status <> 'Cancelled' AND x.SupplierResponse <> 'NotQuoted' AND l.UnitPrice > 0
AND COALESCE(l.DeliveryDate, x.ExpectedArrival) IS NOT NULL THEN 1 ELSE 0 END AS IsEligible
FROM Prc_T_PurchaseRfq b
INNER JOIN Prc_T_PurchaseRfq g ON g.Id = b.TenderGroupId
INNER JOIN Prc_T_PurchaseRfq x ON x.TenderGroupId = b.TenderGroupId AND x.IsDeleted = 0
INNER JOIN Prc_T_PurchaseRfqLine l ON l.PurchaseRfqId = x.Id AND l.IsDeleted = 0
LEFT JOIN Sas_H_Currency c ON c.Id = x.CurrencyId
WHERE b.IsDeleted = 0
), Scored AS (
SELECT Lines.*,
CASE WHEN IsEligible = 1 THEN CAST(
PriceWeight * MIN(CASE WHEN IsEligible = 1 THEN PriceTry END) OVER (PARTITION BY BaseId, TenderLineId) / PriceTry
+ DeliveryWeight * MIN(CASE WHEN IsEligible = 1 THEN LeadDays END) OVER (PARTITION BY BaseId, TenderLineId) * 1.0 / LeadDays
AS decimal(9, 2)) END AS Score
FROM Lines
)
SELECT BaseId, Id, TenantId, TenderLineId, PurchaseRfqId, RfqNo, SupplierId, Status, SupplierResponse, ProductId, Description,
Quantity, UomId, UnitPrice, CurrencyId, PriceTry, DeliveryDate, LeadDays, Score,
CAST(CASE WHEN IsEligible = 1 AND ROW_NUMBER() OVER (PARTITION BY BaseId, TenderLineId
ORDER BY IsEligible DESC, Score DESC, PriceTry, LeadDays) = 1 THEN 1 ELSE 0 END AS bit) AS IsRecommended,
IsSelected, IsDeleted
FROM Scored