34 lines
2.8 KiB
SQL
34 lines
2.8 KiB
SQL
-- İ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 REPLACE VIEW public."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" * COALESCE(c."Rate", 1) AS numeric(18, 4)) AS "PriceTry",
|
||
COALESCE(l."DeliveryDate", x."ExpectedArrival") AS "DeliveryDate",
|
||
GREATEST(COALESCE(l."DeliveryDate", x."ExpectedArrival")::date - g."CreationTime"::date, 1) 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" = false
|
||
INNER JOIN "Prc_T_PurchaseRfqLine" l ON l."PurchaseRfqId" = x."Id" AND l."IsDeleted" = false
|
||
LEFT JOIN "Sas_H_Currency" c ON c."Id" = x."CurrencyId"
|
||
WHERE b."IsDeleted" = false
|
||
), "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 numeric(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",
|
||
("IsEligible" = 1 AND ROW_NUMBER() OVER (PARTITION BY "BaseId", "TenderLineId"
|
||
ORDER BY "IsEligible" DESC, "Score" DESC NULLS LAST, "PriceTry", "LeadDays") = 1) AS "IsRecommended",
|
||
"IsSelected", "IsDeleted"
|
||
FROM "Scored";
|