product performance grouped kpi improvements
This commit is contained in:
@@ -398,12 +398,24 @@ CREATE TABLE IF NOT EXISTS mk_product_performance_stock_daily (
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS mk_product_performance_price_dim (
|
||||
product_code TEXT PRIMARY KEY,
|
||||
item_description TEXT NOT NULL DEFAULT '',
|
||||
kategori TEXT NOT NULL DEFAULT '',
|
||||
askili_yan TEXT NOT NULL DEFAULT '',
|
||||
urun_ilk_grubu TEXT NOT NULL DEFAULT '',
|
||||
urun_ana_grubu TEXT NOT NULL DEFAULT '',
|
||||
urun_alt_grubu TEXT NOT NULL DEFAULT '',
|
||||
cost_price_usd NUMERIC(18,6) NOT NULL DEFAULT 0,
|
||||
base_price_usd NUMERIC(18,6) NOT NULL DEFAULT 0,
|
||||
base_price_try NUMERIC(18,6) NOT NULL DEFAULT 0,
|
||||
last_pricing_date DATE,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
)`,
|
||||
`ALTER TABLE mk_product_performance_price_dim ADD COLUMN IF NOT EXISTS item_description TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE mk_product_performance_price_dim ADD COLUMN IF NOT EXISTS kategori TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE mk_product_performance_price_dim ADD COLUMN IF NOT EXISTS askili_yan TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE mk_product_performance_price_dim ADD COLUMN IF NOT EXISTS urun_ilk_grubu TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE mk_product_performance_price_dim ADD COLUMN IF NOT EXISTS urun_ana_grubu TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE mk_product_performance_price_dim ADD COLUMN IF NOT EXISTS urun_alt_grubu TEXT NOT NULL DEFAULT ''`,
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS mk_product_performance_kpi_daily (
|
||||
kpi_date DATE NOT NULL,
|
||||
@@ -918,25 +930,37 @@ func refreshProductPerformancePrices(ctx context.Context, tx *sql.Tx, productPre
|
||||
count := 0
|
||||
for rows.Next() {
|
||||
var code string
|
||||
var itemDescription, kategori, askiliYan, urunIlkGrubu, urunAnaGrubu, urunAltGrubu string
|
||||
var cost, usd, tryPrice float64
|
||||
var lastPricing sql.NullTime
|
||||
if err := rows.Scan(&code, &cost, &usd, &tryPrice, &lastPricing); err != nil {
|
||||
if err := rows.Scan(&code, &itemDescription, &kategori, &askiliYan, &urunIlkGrubu, &urunAnaGrubu, &urunAltGrubu, &cost, &usd, &tryPrice, &lastPricing); err != nil {
|
||||
return err
|
||||
}
|
||||
askiliYan = cleanProductPerformanceOptionalAttr(askiliYan)
|
||||
urunIlkGrubu = cleanProductPerformanceFirstGroup(urunIlkGrubu)
|
||||
var lp any
|
||||
if lastPricing.Valid {
|
||||
lp = lastPricing.Time
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO mk_product_performance_price_dim (product_code, cost_price_usd, base_price_usd, base_price_try, last_pricing_date, updated_at)
|
||||
VALUES ($1,$2,$3,$4,$5,now())
|
||||
INSERT INTO mk_product_performance_price_dim (
|
||||
product_code, item_description, kategori, askili_yan, urun_ilk_grubu, urun_ana_grubu, urun_alt_grubu,
|
||||
cost_price_usd, base_price_usd, base_price_try, last_pricing_date, updated_at
|
||||
)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,now())
|
||||
ON CONFLICT (product_code) DO UPDATE SET
|
||||
item_description=EXCLUDED.item_description,
|
||||
kategori=EXCLUDED.kategori,
|
||||
askili_yan=EXCLUDED.askili_yan,
|
||||
urun_ilk_grubu=EXCLUDED.urun_ilk_grubu,
|
||||
urun_ana_grubu=EXCLUDED.urun_ana_grubu,
|
||||
urun_alt_grubu=EXCLUDED.urun_alt_grubu,
|
||||
cost_price_usd=EXCLUDED.cost_price_usd,
|
||||
base_price_usd=EXCLUDED.base_price_usd,
|
||||
base_price_try=EXCLUDED.base_price_try,
|
||||
last_pricing_date=EXCLUDED.last_pricing_date,
|
||||
updated_at=now()
|
||||
`, strings.TrimSpace(code), cost, usd, tryPrice, lp); err != nil {
|
||||
`, strings.TrimSpace(code), strings.TrimSpace(itemDescription), strings.TrimSpace(kategori), askiliYan, urunIlkGrubu, strings.TrimSpace(urunAnaGrubu), strings.TrimSpace(urunAltGrubu), cost, usd, tryPrice, lp); err != nil {
|
||||
return err
|
||||
}
|
||||
count++
|
||||
@@ -959,6 +983,30 @@ func RebuildProductPerformanceKPI(ctx context.Context, exec interface {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if _, err := exec.ExecContext(ctx, `
|
||||
UPDATE mk_product_performance_kpi_daily k
|
||||
SET
|
||||
item_description = CASE WHEN btrim(COALESCE(k.item_description,'')) = '' THEN COALESCE(NULLIF(p.item_description,''), k.item_description) ELSE k.item_description END,
|
||||
kategori = CASE WHEN btrim(COALESCE(k.kategori,'')) = '' OR btrim(COALESCE(k.kategori,'')) = '-' THEN COALESCE(NULLIF(p.kategori,''), '') ELSE k.kategori END,
|
||||
askili_yan = CASE WHEN btrim(COALESCE(k.askili_yan,'')) = '' OR btrim(COALESCE(k.askili_yan,'')) = '-' THEN COALESCE(NULLIF(NULLIF(p.askili_yan,''), '-'), '') ELSE k.askili_yan END,
|
||||
urun_ilk_grubu = CASE WHEN btrim(COALESCE(k.urun_ilk_grubu,'')) = '' OR btrim(COALESCE(k.urun_ilk_grubu,'')) = '-' THEN COALESCE(NULLIF(p.urun_ilk_grubu,''), '') ELSE k.urun_ilk_grubu END,
|
||||
urun_ana_grubu = CASE WHEN btrim(COALESCE(k.urun_ana_grubu,'')) = '' THEN COALESCE(NULLIF(p.urun_ana_grubu,''), k.urun_ana_grubu) ELSE k.urun_ana_grubu END,
|
||||
urun_alt_grubu = CASE WHEN btrim(COALESCE(k.urun_alt_grubu,'')) = '' THEN COALESCE(NULLIF(p.urun_alt_grubu,''), k.urun_alt_grubu) ELSE k.urun_alt_grubu END,
|
||||
updated_at = now()
|
||||
FROM mk_product_performance_price_dim p
|
||||
WHERE k.kpi_date = $1
|
||||
AND p.product_code = k.product_code
|
||||
AND (
|
||||
btrim(COALESCE(k.item_description,'')) = ''
|
||||
OR btrim(COALESCE(k.kategori,'')) IN ('', '-')
|
||||
OR btrim(COALESCE(k.askili_yan,'')) IN ('', '-')
|
||||
OR btrim(COALESCE(k.urun_ilk_grubu,'')) IN ('', '-')
|
||||
OR btrim(COALESCE(k.urun_ana_grubu,'')) = ''
|
||||
OR btrim(COALESCE(k.urun_alt_grubu,'')) = ''
|
||||
)
|
||||
`, kpiDate); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
return int(n), nil
|
||||
}
|
||||
|
||||
@@ -382,6 +382,22 @@ ORDER BY StockDate, ProductCode, ColorCode, YakaKodu
|
||||
func productPerformancePriceSQL() string {
|
||||
return `
|
||||
;WITH ProductCodes AS (
|
||||
SELECT DISTINCT ItemCode
|
||||
FROM cdItem WITH(NOLOCK)
|
||||
WHERE ItemTypeCode = 1
|
||||
AND (
|
||||
LEN(LTRIM(RTRIM(ItemCode))) = 13
|
||||
AND (
|
||||
ItemCode LIKE 'S%'
|
||||
OR ItemCode LIKE 'O%'
|
||||
OR ItemCode LIKE 'N%'
|
||||
OR ItemCode LIKE 'X%'
|
||||
OR ItemCode LIKE 'I%'
|
||||
OR ItemCode LIKE 'A%'
|
||||
)
|
||||
)
|
||||
AND (@p1 = '' OR ItemCode LIKE @p1 + '%')
|
||||
UNION
|
||||
SELECT DISTINCT ItemCode
|
||||
FROM trPriceListLine WITH(NOLOCK)
|
||||
WHERE ItemTypeCode = 1
|
||||
@@ -460,6 +476,83 @@ Cost AS (
|
||||
)
|
||||
SELECT
|
||||
pc.ItemCode,
|
||||
ItemDescription = dbo.HG_Temizlik(ISNULL((
|
||||
SELECT ItemDescription
|
||||
FROM cdItemDesc WITH(NOLOCK)
|
||||
WHERE cdItemDesc.ItemTypeCode = 1
|
||||
AND cdItemDesc.ItemCode = pc.ItemCode
|
||||
AND cdItemDesc.LangCode = 'TR'
|
||||
), SPACE(0))),
|
||||
Kategori = dbo.HG_Temizlik(ISNULL((
|
||||
SELECT AttributeDescription
|
||||
FROM cdItemAttributeDesc WITH(NOLOCK)
|
||||
WHERE cdItemAttributeDesc.ItemTypeCode = 1
|
||||
AND cdItemAttributeDesc.AttributeTypeCode = 44
|
||||
AND cdItemAttributeDesc.AttributeCode = (
|
||||
SELECT TOP 1 AttributeCode
|
||||
FROM prItemAttribute WITH(NOLOCK)
|
||||
WHERE AttributeTypeCode = 44
|
||||
AND prItemAttribute.ItemTypeCode = 1
|
||||
AND prItemAttribute.ItemCode = pc.ItemCode
|
||||
)
|
||||
AND cdItemAttributeDesc.LangCode = 'TR'
|
||||
), SPACE(0))),
|
||||
AskiliYan = dbo.HG_Temizlik(ISNULL((
|
||||
SELECT AttributeDescription
|
||||
FROM cdItemAttributeDesc WITH(NOLOCK)
|
||||
WHERE cdItemAttributeDesc.ItemTypeCode = 1
|
||||
AND cdItemAttributeDesc.AttributeTypeCode = 45
|
||||
AND cdItemAttributeDesc.AttributeCode = (
|
||||
SELECT TOP 1 AttributeCode
|
||||
FROM prItemAttribute WITH(NOLOCK)
|
||||
WHERE AttributeTypeCode = 45
|
||||
AND prItemAttribute.ItemTypeCode = 1
|
||||
AND prItemAttribute.ItemCode = pc.ItemCode
|
||||
)
|
||||
AND cdItemAttributeDesc.LangCode = 'TR'
|
||||
), SPACE(0))),
|
||||
UrunIlkGrubu = dbo.HG_Temizlik(ISNULL((
|
||||
SELECT AttributeDescription
|
||||
FROM cdItemAttributeDesc WITH(NOLOCK)
|
||||
WHERE cdItemAttributeDesc.ItemTypeCode = 1
|
||||
AND cdItemAttributeDesc.AttributeTypeCode = 42
|
||||
AND cdItemAttributeDesc.AttributeCode = (
|
||||
SELECT TOP 1 AttributeCode
|
||||
FROM prItemAttribute WITH(NOLOCK)
|
||||
WHERE AttributeTypeCode = 42
|
||||
AND prItemAttribute.ItemTypeCode = 1
|
||||
AND prItemAttribute.ItemCode = pc.ItemCode
|
||||
)
|
||||
AND cdItemAttributeDesc.LangCode = 'TR'
|
||||
), SPACE(0))),
|
||||
UrunAnaGrubu = dbo.HG_Temizlik(ISNULL((
|
||||
SELECT AttributeDescription
|
||||
FROM cdItemAttributeDesc WITH(NOLOCK)
|
||||
WHERE cdItemAttributeDesc.ItemTypeCode = 1
|
||||
AND cdItemAttributeDesc.AttributeTypeCode = 1
|
||||
AND cdItemAttributeDesc.AttributeCode = (
|
||||
SELECT TOP 1 AttributeCode
|
||||
FROM prItemAttribute WITH(NOLOCK)
|
||||
WHERE AttributeTypeCode = 1
|
||||
AND prItemAttribute.ItemTypeCode = 1
|
||||
AND prItemAttribute.ItemCode = pc.ItemCode
|
||||
)
|
||||
AND cdItemAttributeDesc.LangCode = 'TR'
|
||||
), SPACE(0))),
|
||||
UrunAltGrubu = dbo.HG_Temizlik(ISNULL((
|
||||
SELECT AttributeDescription
|
||||
FROM cdItemAttributeDesc WITH(NOLOCK)
|
||||
WHERE cdItemAttributeDesc.ItemTypeCode = 1
|
||||
AND cdItemAttributeDesc.AttributeTypeCode = 2
|
||||
AND cdItemAttributeDesc.AttributeCode = (
|
||||
SELECT TOP 1 AttributeCode
|
||||
FROM prItemAttribute WITH(NOLOCK)
|
||||
WHERE AttributeTypeCode = 2
|
||||
AND prItemAttribute.ItemTypeCode = 1
|
||||
AND prItemAttribute.ItemCode = pc.ItemCode
|
||||
)
|
||||
AND cdItemAttributeDesc.LangCode = 'TR'
|
||||
), SPACE(0))),
|
||||
CostPriceUsd = ISNULL(c.CostPriceUsd, 0),
|
||||
BasePriceUsd = ISNULL(bp.BasePriceUsd, 0),
|
||||
BasePriceTry = ISNULL(bp.BasePriceTry, 0),
|
||||
|
||||
Reference in New Issue
Block a user