来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
127 lines
4.6 KiB
TypeScript
127 lines
4.6 KiB
TypeScript
import type { PoolClient } from 'pg';
|
|
import { pool } from '../db';
|
|
import { getTenantDbStore } from './tenantDbContext';
|
|
import { runWithTenantDataAccess } from './tenantSchemaService';
|
|
import { resolveActiveQuoteSheetForCustomer } from './quoteSheetService';
|
|
|
|
type Db = Pick<PoolClient, 'query'>;
|
|
|
|
function resolveQuoteSyncBranchCode(explicit?: string | null): string | null {
|
|
const fromArg = String(explicit || '').trim();
|
|
if (fromArg) return fromArg;
|
|
const fromAls = String(getTenantDbStore()?.branchCode || '').trim();
|
|
return fromAls || null;
|
|
}
|
|
|
|
/**
|
|
* 备货/自采价 → 生效报价单成本(补空)。
|
|
* 商品档案价列已删除,不再写 catalog/skus。
|
|
* 见 docs/design/20260731-quote-cost-fill-from-supply.md
|
|
*/
|
|
export async function syncSkuCostFromPreparation(
|
|
skuCode: string,
|
|
purchasePrice: number,
|
|
db?: Db,
|
|
branchCode?: string | null,
|
|
options?: {
|
|
syncQuoteSheet?: boolean;
|
|
onlyIfQuoteCostZero?: boolean;
|
|
salePrice?: number | null;
|
|
onlyIfQuoteSalePriceZero?: boolean;
|
|
customerCode?: string | null;
|
|
}
|
|
): Promise<void> {
|
|
if (!skuCode || !Number.isFinite(purchasePrice) || purchasePrice <= 0) return;
|
|
const resolvedBranch = resolveQuoteSyncBranchCode(branchCode);
|
|
const syncQuoteSheet = options?.syncQuoteSheet !== false;
|
|
const onlyIfQuoteCostZero = options?.onlyIfQuoteCostZero !== false;
|
|
const salePrice = Number(options?.salePrice);
|
|
const syncSalePrice = Number.isFinite(salePrice) && salePrice > 0;
|
|
const onlyIfQuoteSalePriceZero = options?.onlyIfQuoteSalePriceZero !== false;
|
|
const customerCode = String(options?.customerCode || '').trim();
|
|
if (!syncQuoteSheet) return;
|
|
|
|
if (db) {
|
|
await syncQuoteCostFromPreparation(db, skuCode, purchasePrice, resolvedBranch, onlyIfQuoteCostZero, syncSalePrice ? salePrice : null, onlyIfQuoteSalePriceZero, customerCode);
|
|
} else if (resolvedBranch) {
|
|
try {
|
|
await runWithTenantDataAccess(resolvedBranch, async (tclient) => {
|
|
await syncQuoteCostFromPreparation(tclient, skuCode, purchasePrice, resolvedBranch, onlyIfQuoteCostZero, syncSalePrice ? salePrice : null, onlyIfQuoteSalePriceZero, customerCode);
|
|
});
|
|
} catch {
|
|
// 报价回填失败不阻断主流程
|
|
}
|
|
} else {
|
|
try {
|
|
await syncQuoteCostFromPreparation(pool, skuCode, purchasePrice, null, onlyIfQuoteCostZero, syncSalePrice ? salePrice : null, onlyIfQuoteSalePriceZero, customerCode);
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
}
|
|
|
|
async function syncQuoteCostFromPreparation(
|
|
db: Db,
|
|
skuCode: string,
|
|
purchasePrice: number,
|
|
branchCode?: string | null,
|
|
onlyIfQuoteCostZero = true,
|
|
salePrice: number | null = null,
|
|
onlyIfQuoteSalePriceZero = true,
|
|
customerCode = ''
|
|
): Promise<void> {
|
|
const branch = String(branchCode || '').trim() || null;
|
|
const quoteSheetCode = customerCode
|
|
? await resolveActiveQuoteSheetForCustomer(db, customerCode, branch)
|
|
: null;
|
|
if (customerCode && !quoteSheetCode) return;
|
|
const run = () =>
|
|
db.query(
|
|
`UPDATE customer_quote_sheet_items i
|
|
SET cost_price = CASE
|
|
WHEN $4::boolean = false OR COALESCE(i.cost_price, 0) = 0 THEN $1
|
|
ELSE i.cost_price
|
|
END,
|
|
sale_price = CASE
|
|
WHEN $5::numeric IS NULL THEN sale_price
|
|
WHEN $6::boolean AND COALESCE(i.sale_price, 0) <> 0 THEN sale_price
|
|
ELSE $5::numeric
|
|
END,
|
|
price = CASE
|
|
WHEN $5::numeric IS NULL THEN price
|
|
WHEN $6::boolean AND COALESCE(i.sale_price, 0) <> 0 THEN price
|
|
ELSE $5::numeric
|
|
END
|
|
FROM customer_quote_sheets qs
|
|
WHERE qs.quote_sheet_code = i.quote_sheet_code
|
|
AND i.sku_code = $2
|
|
AND qs.approval_status = 30
|
|
AND qs.status = 1
|
|
AND (qs.expires_at IS NULL OR qs.expires_at > NOW())
|
|
AND ($7::text IS NOT NULL OR $3::text IS NULL OR qs.branch_code IS NOT DISTINCT FROM $3)
|
|
AND ($7::text IS NULL OR i.quote_sheet_code = $7)
|
|
AND (
|
|
($4::boolean = false OR COALESCE(i.cost_price, 0) = 0)
|
|
OR ($5::numeric IS NOT NULL AND ($6::boolean = false OR COALESCE(i.sale_price, 0) = 0))
|
|
)`,
|
|
[purchasePrice, skuCode, branch, onlyIfQuoteCostZero, salePrice, onlyIfQuoteSalePriceZero, quoteSheetCode]
|
|
);
|
|
|
|
const inTxn = await db.query(`SELECT txid_current_if_assigned() AS txid`);
|
|
if (inTxn.rows[0]?.txid) {
|
|
await db.query('SAVEPOINT sync_quote_cost');
|
|
try {
|
|
await run();
|
|
await db.query('RELEASE SAVEPOINT sync_quote_cost');
|
|
} catch {
|
|
await db.query('ROLLBACK TO SAVEPOINT sync_quote_cost');
|
|
}
|
|
} else {
|
|
try {
|
|
await run();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
}
|