来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
142 lines
5.0 KiB
TypeScript
142 lines
5.0 KiB
TypeScript
export interface OrderItemRow {
|
|
id?: string;
|
|
order_code: string;
|
|
sku_code: string | null;
|
|
supplier_code: string | null;
|
|
item_name: string | null;
|
|
quantity?: number | null;
|
|
unit?: string | null;
|
|
processing_method?: string | null;
|
|
processing_owner?: string | null;
|
|
/** 录单明细备注 */
|
|
remark?: string | null;
|
|
}
|
|
|
|
export interface PreparationDisplayFields {
|
|
order_code?: string | null;
|
|
supplier_code?: string | null;
|
|
sku_code?: string | null;
|
|
sku_name?: string | null;
|
|
item_name?: string | null;
|
|
prepare_qty?: number | null;
|
|
}
|
|
|
|
/** 从订单明细反查备货记录对应的原始商品行 */
|
|
export function findOrderItemForPreparation(
|
|
prep: PreparationDisplayFields,
|
|
orderItems: OrderItemRow[]
|
|
): OrderItemRow | null {
|
|
if (!prep.order_code) return null;
|
|
const candidates = orderItems.filter((oi) => oi.order_code === prep.order_code);
|
|
if (!candidates.length) return null;
|
|
|
|
const qty = Number(prep.prepare_qty) || 0;
|
|
const supplier = prep.supplier_code || null;
|
|
const sku = prep.sku_code || null;
|
|
|
|
const score = (oi: OrderItemRow) => {
|
|
let points = 0;
|
|
if (sku && oi.sku_code === sku) points += 4;
|
|
if (supplier && oi.supplier_code === supplier) points += 2;
|
|
if (qty > 0 && Number(oi.quantity) === qty) points += 1;
|
|
return points;
|
|
};
|
|
|
|
const ranked = [...candidates].sort((a, b) => score(b) - score(a));
|
|
if (ranked[0] && score(ranked[0]) > 0) return ranked[0];
|
|
if (candidates.length === 1) return candidates[0];
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 展示名称:**优先商品库 SKU 名称**(精准 SKU),无商品库记录时才回退录单名 → SKU 编码。
|
|
*
|
|
* 2026-09-15 变更:原为「订单原始名称(录单名)优先」。录单名是客户写法(如「草鱼」),
|
|
* 供应商按它备货容易对不上货;备货表列、发单文本、缺货弹层统一改为商品库名(如「草鱼杀好」)。
|
|
* 连带:`getPreparationMergeKey` 也按该名合并,同一 SKU 的多个录单名会合并成一行。
|
|
*/
|
|
export function getPreparationDisplayName(prep: PreparationDisplayFields): string {
|
|
const name = prep.sku_name?.trim() || prep.item_name?.trim() || prep.sku_code?.trim();
|
|
return name || '未命名商品';
|
|
}
|
|
|
|
/** 合并键:同名称 + 同单位 + 同规格 + 同加工方式才合并数量 */
|
|
export function getPreparationMergeKey(
|
|
prep: PreparationDisplayFields & {
|
|
unit?: string | null;
|
|
specification?: string | null;
|
|
processing_method?: string | null;
|
|
is_stockout?: boolean;
|
|
transferred_to_purchase?: boolean;
|
|
}
|
|
): string {
|
|
const name = getPreparationDisplayName(prep).trim().toLowerCase();
|
|
const unit = (prep.unit || '').trim().toLowerCase();
|
|
const spec = (prep.specification || '').trim().toLowerCase();
|
|
const processingMethod = (prep.processing_method || '').trim().toLowerCase();
|
|
const flow =
|
|
prep.transferred_to_purchase ? 'transferred' : prep.is_stockout ? 'stockout' : 'normal';
|
|
return `${name}||${unit}||${spec}||${processingMethod}||${flow}`;
|
|
}
|
|
|
|
function aggregatePrepareStatus(group: Array<{ prepare_status: number }>) {
|
|
if (group.every((p) => p.prepare_status === 40)) return 40;
|
|
if (group.every((p) => p.prepare_status === 30)) return 30;
|
|
if (group.every((p) => p.prepare_status >= 20 && p.prepare_status < 40)) return 20;
|
|
return 10;
|
|
}
|
|
|
|
export type AggregatedPreparation<T> = T & {
|
|
mergedIds: string[];
|
|
mergedSources: T[];
|
|
};
|
|
|
|
/** 同一供货商下按商品名称合并,数量相加 */
|
|
export function aggregatePreparationsByName<
|
|
T extends PreparationDisplayFields & {
|
|
id: string;
|
|
prepare_qty: number;
|
|
purchase_price: number;
|
|
prepare_status: number;
|
|
is_stockout?: boolean;
|
|
transferred_to_purchase?: boolean;
|
|
created_at?: string;
|
|
unit?: string | null;
|
|
specification?: string | null;
|
|
processing_method?: string | null;
|
|
},
|
|
>(items: T[]): AggregatedPreparation<T>[] {
|
|
const map = new Map<string, T[]>();
|
|
for (const item of items) {
|
|
const key = getPreparationMergeKey(item);
|
|
if (!map.has(key)) map.set(key, []);
|
|
map.get(key)!.push(item);
|
|
}
|
|
|
|
return [...map.values()]
|
|
.map((group) => {
|
|
const sorted = [...group].sort((a, b) =>
|
|
(a.created_at || '').localeCompare(b.created_at || '')
|
|
);
|
|
const totalQty = group.reduce((sum, p) => sum + (Number(p.prepare_qty) || 0), 0);
|
|
const amount = group.reduce(
|
|
(sum, p) => sum + (Number(p.prepare_qty) || 0) * (Number(p.purchase_price) || 0),
|
|
0
|
|
);
|
|
const base = { ...sorted[0] };
|
|
return {
|
|
...base,
|
|
prepare_qty: totalQty,
|
|
purchase_price: totalQty > 0 ? amount / totalQty : Number(base.purchase_price) || 0,
|
|
prepare_status: aggregatePrepareStatus(group),
|
|
is_stockout: group.every((p) => p.is_stockout),
|
|
transferred_to_purchase: group.every((p) => p.transferred_to_purchase),
|
|
mergedIds: group.map((p) => p.id),
|
|
mergedSources: group,
|
|
};
|
|
})
|
|
.sort((a, b) =>
|
|
getPreparationDisplayName(a).localeCompare(getPreparationDisplayName(b), 'zh-CN')
|
|
);
|
|
}
|