Files
SupplierSystem/scripts/apply-branch-tax-rates.cjs
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

136 lines
4.2 KiB
JavaScript

/**
* 按分公司纳税等级回填商品税率(生产运维)
* node scripts/apply-branch-tax-rates.cjs [--branch BR001] [--execute]
*/
const path = require('path');
const pg = require(path.join(__dirname, '../server/node_modules/pg'));
const BRANCH_TAX_LEVELS = new Set([
'exempt_agri',
'standard_agri',
'flat_0',
'flat_9',
'flat_13',
]);
function normalizeLevel(value) {
return BRANCH_TAX_LEVELS.has(value) ? value : 'exempt_agri';
}
function classifyBand({ isFresh, taxCode, categoryName }) {
const code = String(taxCode || '').replace(/\D/g, '');
const cat = String(categoryName || '');
if (code.startsWith('1010117') || code.startsWith('1030105') || /香料原料|食用植物油|植物油脂/.test(cat)) {
return 0.09;
}
if (
code.startsWith('10302') ||
code.startsWith('104') ||
code.startsWith('105') ||
code.startsWith('106') ||
code.startsWith('107') ||
/调味|面食|干杂|日杂|包材|饮料|冷冻|方便食品/.test(cat)
) {
return 0.13;
}
if (isFresh === true) return 0;
if (code.startsWith('1010') || code.startsWith('1030107')) return 0;
if (/蔬菜|鲜肉|蛋|水果|水产|菌菇|叶菜|根茎|瓜类|茄果|葱蒜|豆类|玉米/.test(cat)) return 0;
return 0.13;
}
function resolveRate(level, product) {
const lv = normalizeLevel(level);
if (lv === 'flat_0') return 0;
if (lv === 'flat_9') return 0.09;
if (lv === 'flat_13') return 0.13;
const band = classifyBand(product);
if (lv === 'standard_agri') return band === 0 ? 0.09 : band;
return band;
}
async function main() {
const execute = process.argv.includes('--execute');
const branchArgIdx = process.argv.indexOf('--branch');
const onlyBranch = branchArgIdx >= 0 ? String(process.argv[branchArgIdx + 1] || '').trim() : '';
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
const client = await pool.connect();
try {
const branches = await client.query(
`SELECT branch_code, tax_level FROM branches
WHERE ($1 = '' OR branch_code = $1)
ORDER BY branch_code`,
[onlyBranch]
);
const summary = [];
for (const branch of branches.rows) {
const taxLevel = normalizeLevel(branch.tax_level);
const products = await client.query(
`
SELECT
p.code,
p.tax_code,
p.tax_rate,
COALESCE(c.is_fresh, c2.is_fresh, false) AS is_fresh,
COALESCE(c.name, c2.name, '') AS category_name
FROM product_catalog p
LEFT JOIN product_catalog c
ON c.code = p.parent_code AND c.item_type = 'category'
LEFT JOIN product_catalog c2
ON c2.code = c.parent_code AND c2.item_type = 'category'
WHERE p.item_type = 'product'
AND (
p.branch_code = $1
OR p.branch_code IS NULL
OR EXISTS (
SELECT 1 FROM skus s
WHERE s.sku_code = p.code
AND (s.branch_code = $1 OR s.branch_code IS NULL)
)
)
`,
[branch.branch_code]
);
let updated = 0;
for (const row of products.rows) {
const next = resolveRate(taxLevel, {
isFresh: row.is_fresh === true,
taxCode: row.tax_code,
categoryName: row.category_name,
});
const current = row.tax_rate == null ? null : Number(row.tax_rate);
if (current !== null && Math.abs(current - next) < 0.0001) continue;
if (execute) {
await client.query(
`UPDATE product_catalog SET tax_rate = $2, updated_at = NOW()
WHERE item_type = 'product' AND code = $1`,
[row.code, next]
);
await client.query(`UPDATE skus SET tax_rate = $2 WHERE sku_code = $1`, [row.code, next]);
}
updated += 1;
}
summary.push({
branch: branch.branch_code,
taxLevel,
products: products.rows.length,
wouldUpdate: updated,
});
}
console.log(JSON.stringify({ dryRun: !execute, summary }, null, 2));
if (!execute) console.log('\n[dry-run] 加 --execute 写入');
} finally {
client.release();
await pool.end();
}
}
main().catch((error) => {
console.error(error);
process.exit(1);
});