2026-10-31 14:59:30 +08:00
|
|
|
|
'use strict';
|
2026-12-04 13:01:19 -08:00
|
|
|
|
const { workbook } = require('../../core/xlsx');
|
|
|
|
|
|
const TeamMembers = require('../team/team-members');
|
2026-10-31 14:59:30 +08:00
|
|
|
|
const MIME = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
2026-11-08 15:47:07 +08:00
|
|
|
|
const types = {
|
|
|
|
|
|
increase: '增加',
|
|
|
|
|
|
decrease: '减少',
|
|
|
|
|
|
transfer: '平移',
|
|
|
|
|
|
opening: '开账',
|
|
|
|
|
|
count_opening: '对数盘点基准',
|
|
|
|
|
|
purchase: '入库',
|
|
|
|
|
|
adjustment: '调整',
|
|
|
|
|
|
manual: '手动变动',
|
|
|
|
|
|
sale: '销售出库',
|
|
|
|
|
|
return: '退货入库',
|
|
|
|
|
|
manual_reversal: '更正冲回',
|
|
|
|
|
|
lock_purchase: '锁价黄金入库',
|
|
|
|
|
|
lock_finished: '锁价成品入库',
|
|
|
|
|
|
order_edit_reverse: '改单出库冲回',
|
|
|
|
|
|
order_edit_sale: '改单出库',
|
|
|
|
|
|
order_cancel_reverse: '订单作废冲回',
|
|
|
|
|
|
recovery: '回收旧料入库',
|
2026-10-31 14:59:30 +08:00
|
|
|
|
};
|
|
|
|
|
|
const filters = {
|
|
|
|
|
|
all: '全部流水',
|
|
|
|
|
|
increase: '手动增加',
|
|
|
|
|
|
decrease: '手动减少',
|
|
|
|
|
|
transfer: '手动平移',
|
|
|
|
|
|
manual: '全部手动变动',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
};
|
2026-10-31 14:59:30 +08:00
|
|
|
|
function status(row) {
|
|
|
|
|
|
if (row.reversal || row.kind === 'manual_reversal') return '更正记录 · 撤回旧记录的库存影响';
|
|
|
|
|
|
if (row.current === false)
|
|
|
|
|
|
return row.status === 'deleted'
|
|
|
|
|
|
? '原记录 · 已删除'
|
|
|
|
|
|
: row.status === 'modified'
|
|
|
|
|
|
? '原记录 · 已修改'
|
|
|
|
|
|
: '冲正/历史记录';
|
|
|
|
|
|
const value = row.status === 'modified' ? '修改后有效' : row.manual ? '当前有效' : '随原业务记录保留';
|
|
|
|
|
|
return value + (row.manual && row.pendingApproval ? ';待麦麦审批' : '');
|
|
|
|
|
|
}
|
|
|
|
|
|
function recordedAt(value) {
|
|
|
|
|
|
const date = new Date(value);
|
|
|
|
|
|
return Number.isNaN(date.getTime())
|
|
|
|
|
|
? '时间未提供'
|
|
|
|
|
|
: new Intl.DateTimeFormat('sv-SE', {
|
|
|
|
|
|
timeZone: 'Asia/Shanghai',
|
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
|
month: '2-digit',
|
|
|
|
|
|
day: '2-digit',
|
|
|
|
|
|
hour: '2-digit',
|
|
|
|
|
|
minute: '2-digit',
|
|
|
|
|
|
second: '2-digit',
|
|
|
|
|
|
hourCycle: 'h23',
|
|
|
|
|
|
}).format(date);
|
|
|
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
|
function actorDisplayName(value, label) {
|
|
|
|
|
|
const member =
|
|
|
|
|
|
value === '麦麦'
|
2026-12-04 08:58:53 -08:00
|
|
|
|
? 'owner'
|
2026-11-14 01:16:35 +08:00
|
|
|
|
: value === '助理2'
|
|
|
|
|
|
? 'assistant2'
|
|
|
|
|
|
: value === '助理' || value === '助理1'
|
|
|
|
|
|
? 'assistant'
|
2026-12-04 08:58:53 -08:00
|
|
|
|
: null;
|
2026-11-14 01:16:35 +08:00
|
|
|
|
return member ? label(member) : value;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
|
function inventoryMovementsFile(
|
2026-12-04 08:58:53 -08:00
|
|
|
|
result,
|
|
|
|
|
|
date,
|
2026-11-14 01:16:35 +08:00
|
|
|
|
pocketLabel = key => key,
|
|
|
|
|
|
memberLabel = key => TeamMembers.MEMBERS.find(row => row.member === key)?.label ?? key,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) {
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const rows = [
|
|
|
|
|
|
['序号', '日期', '货盘', '增减克重(g)', '类型', '备注/原单', '经办人', '记录时间', '状态', '增减对数(对)'],
|
|
|
|
|
|
...result.movements.map((row, index) => [
|
2026-12-04 08:58:53 -08:00
|
|
|
|
index + 1,
|
|
|
|
|
|
row.date,
|
2026-11-14 01:16:35 +08:00
|
|
|
|
pocketLabel(row.pocket),
|
|
|
|
|
|
row.mg / 1000,
|
|
|
|
|
|
types[row.kind] || '其他库存变动(' + row.kind + ')',
|
|
|
|
|
|
row.reversal || row.kind === 'manual_reversal' ? '撤回旧记录的库存影响' : row.note || '',
|
|
|
|
|
|
actorDisplayName(row.actor, memberLabel),
|
|
|
|
|
|
recordedAt(row.created),
|
|
|
|
|
|
status(row),
|
|
|
|
|
|
row.stockCount ?? (row.countUnit === 'pair' ? '未登记' : ''),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
]),
|
|
|
|
|
|
];
|
2026-11-08 03:30:58 +08:00
|
|
|
|
const name = (result.pocket ? pocketLabel(result.pocket) : '全部货盘').replace(/[\\/:*?"<>|\u0000-\u001f]/g, '_');
|
2026-12-01 14:27:27 +08:00
|
|
|
|
const period = result.from || result.to ? `${result.from || '最早'}至${result.to || '今'}` : date;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
return {
|
2026-12-01 14:27:27 +08:00
|
|
|
|
buffer: workbook(rows, { widths: [8, 14, 16, 18, 20, 48, 18, 24, 30, 18], optionalNumericColumns: [9] }),
|
2026-10-31 14:59:30 +08:00
|
|
|
|
filename: `库存流水-${name}-${filters[result.kind]}-${period}.xlsx`,
|
2026-12-01 14:27:27 +08:00
|
|
|
|
contentType: MIME,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
2026-10-31 14:59:30 +08:00
|
|
|
|
module.exports = { inventoryMovementsFile, MIME };
|