Refactor tax calculation in calcRegionalSellPrice function of FalukantService to convert exemptTypes Set to PostgreSQL array string for improved query performance and clarity.
This commit is contained in:
@@ -194,6 +194,12 @@ async function calcRegionalSellPrice(product, knowledgeFactor, regionId, worthPe
|
|||||||
if (hasChancellor) return 0;
|
if (hasChancellor) return 0;
|
||||||
|
|
||||||
// Now compute cumulative tax but exclude regions whose regionType.labelTr is in exemptTypes
|
// Now compute cumulative tax but exclude regions whose regionType.labelTr is in exemptTypes
|
||||||
|
// Konvertiere exemptTypes Set zu einem PostgreSQL-Array-String
|
||||||
|
const exemptTypesArray = Array.from(exemptTypes);
|
||||||
|
const exemptTypesString = exemptTypesArray.length > 0
|
||||||
|
? `ARRAY[${exemptTypesArray.map(t => `'${t.replace(/'/g, "''")}'`).join(',')}]`
|
||||||
|
: `ARRAY[]::text[]`;
|
||||||
|
|
||||||
const rows = await sequelize.query(
|
const rows = await sequelize.query(
|
||||||
`WITH RECURSIVE ancestors AS (
|
`WITH RECURSIVE ancestors AS (
|
||||||
SELECT r.id, r.parent_id, r.tax_percent, rt.label_tr as region_type
|
SELECT r.id, r.parent_id, r.tax_percent, rt.label_tr as region_type
|
||||||
@@ -206,9 +212,9 @@ async function calcRegionalSellPrice(product, knowledgeFactor, regionId, worthPe
|
|||||||
JOIN falukant_type.region_type rt2 ON rt2.id = reg.region_type_id
|
JOIN falukant_type.region_type rt2 ON rt2.id = reg.region_type_id
|
||||||
JOIN ancestors a ON reg.id = a.parent_id
|
JOIN ancestors a ON reg.id = a.parent_id
|
||||||
)
|
)
|
||||||
SELECT COALESCE(SUM(CASE WHEN :exempt_types::text[] && ARRAY[region_type] THEN 0 ELSE tax_percent END),0) AS total FROM ancestors;`,
|
SELECT COALESCE(SUM(CASE WHEN ${exemptTypesString} && ARRAY[region_type] THEN 0 ELSE tax_percent END),0) AS total FROM ancestors;`,
|
||||||
{
|
{
|
||||||
replacements: { id: regionId, exempt_types: Array.from(exemptTypes) },
|
replacements: { id: regionId },
|
||||||
type: sequelize.QueryTypes.SELECT
|
type: sequelize.QueryTypes.SELECT
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user