132 lines
5.2 KiB
Plaintext
132 lines
5.2 KiB
Plaintext
storageQuantityMap,
|
||
}));
|
||
}
|
||
|
||
/**
|
||
* å°†å<E280A0>•æ<E280A2>¡ç›˜ç‚¹æ˜Žç»†æ ¼å¼<C3A5>化为è<C2BA>šå<C5A1>ˆè¾“出行ã€?
|
||
* 商å“<C3A5>行:è<C5A1>šå<C5A1>ˆä½œä¸šè®°å½•输出已盘数é‡<C3A9>/差异/库ä½<C3A4>列表ç‰ï¼›
|
||
* 库ä½<C3A4>行:按商å“<C3A5>è<EFBFBD>šå<C5A1>ˆä½œä¸šè®°å½•输å‡?stocks 列表(å<CB86>« wms_storage è´¦é<C2A6>¢ä¸Žå·®å¼‚)ã€?
|
||
*
|
||
* @param {Object} params æ ¼å¼<C3A5>化å<E28093>‚æ•°ã€?
|
||
* @param {Object} params.detailRow 明细原始行�
|
||
* @param {Map<number, Array<Object>>} params.recordsByDetail 按明细ID分组的作业记录�
|
||
* @param {Map<number, Object>} params.stockMap 商å“<C3A5>å¿«ç…§æ˜ å°„ã€?
|
||
* @param {Map<string, number>} params.storageQuantityMap 库ä½<C3A4>商å“<C3A5>è´¦é<C2A6>¢æ•°é‡<C3A9>æ˜ å°„ï¼ˆé”® `${space_id}_${stock_id}`)ã€?
|
||
* @return {Object} æ ¼å¼<C3A5>化å<E28093>Žçš„æ˜Žç»†è¡Œã€?
|
||
*/
|
||
_formatDetailAggregationRow(params = {}) {
|
||
const { detailRow, recordsByDetail, stockMap, storageQuantityMap } = params;
|
||
const detailRowId = Number(detailRow.id);
|
||
const scopeType = (detailRow.scope_type || 'goods') === 'location' ? 'location' : 'goods';
|
||
const detailRecords = recordsByDetail.get(detailRowId) || [];
|
||
|
||
const locationCodes = [ ...new Set(detailRecords
|
||
.map(record => String(record.location_code || '').trim())
|
||
.filter(Boolean)) ];
|
||
const checkers = [ ...new Set(detailRecords
|
||
.map(record => String(record.checker_name || '').trim())
|
||
.filter(Boolean)) ];
|
||
const lastCheckedAt = detailRecords.length > 0
|
||
? detailRecords[detailRecords.length - 1].checked_at
|
||
: null;
|
||
|
||
// 按库ä½<C3A4>è<EFBFBD>šå<C5A1>ˆä½œä¸šè®°å½•(库ä½<C3A4>为空归入"æ— åº“ä½?分组,location_code ä¸?null),按首æ<E28093>¡è®°å½?id å<>‡åº<C3A5>
|
||
const locationGroups = new Map();
|
||
for (const record of detailRecords) {
|
||
const recordLocationCode = record.location_code !== null && record.location_code !== undefined
|
||
? String(record.location_code).trim()
|
||
: '';
|
||
if (!locationGroups.has(recordLocationCode)) {
|
||
locationGroups.set(recordLocationCode, {
|
||
location_id: recordLocationCode ? (Number(record.location_id) || null) : null,
|
||
location_code: recordLocationCode || null,
|
||
quantity: 0,
|
||
first_record_id: Number(record.id) || 0,
|
||
});
|
||
}
|
||
const locationGroup = locationGroups.get(recordLocationCode);
|
||
locationGroup.quantity += Number(record.quantity) || 0;
|
||
}
|
||
const locations = [ ...locationGroups.values() ]
|
||
.sort((left, right) => left.first_record_id - right.first_record_id)
|
||
.map(locationGroup => ({
|
||
location_id: locationGroup.location_id,
|
||
location_code: locationGroup.location_code,
|
||
quantity: locationGroup.quantity,
|
||
}));
|
||
|
||
const base = {
|
||
...detailRow,
|
||
scope_type: scopeType,
|
||
location_codes: locationCodes,
|
||
checkers,
|
||
last_checked_at: lastCheckedAt,
|
||
records: detailRecords,
|
||
locations,
|
||
};
|
||
|
||
if (scopeType === 'goods') {
|
||
const checkedQuantity = detailRecords.reduce((sum, record) => sum + (Number(record.quantity) || 0), 0);
|
||
const diffQuantity = checkedQuantity - Number(detailRow.system_quantity || 0);
|
||
return {
|
||
...base,
|
||
checked_quantity: detailRecords.length > 0 ? checkedQuantity : null,
|
||
diff_quantity: detailRecords.length > 0 ? diffQuantity : null,
|
||
is_diff: detailRecords.length > 0 && diffQuantity !== 0 ? 1 : 0,
|
||
status: detailRecords.length > 0 ? 'checked' : 'unchecked',
|
||
};
|
||
}
|
||
|
||
// 库ä½<C3A4>行:按商å“<C3A5>è<EFBFBD>šå<C5A1>ˆè®°å½•,账é<C2A6>¢ä»?wms_storage å<>–(location_id 为空时账é<C2A6>¢æŒ‰ 0ï¼?
|
||
const quantityByStock = new Map();
|
||
for (const record of detailRecords) {
|
||
const stockId = Number(record.stock_id);
|
||
if (!Number.isInteger(stockId) || stockId <= 0) {
|
||
continue;
|
||
}
|
||
const agg = quantityByStock.get(stockId) || { checked: 0 };
|
||
agg.checked += Number(record.quantity) || 0;
|
||
quantityByStock.set(stockId, agg);
|
||
}
|
||
const locationId = detailRow.location_id !== null && detailRow.location_id !== undefined
|
||
? Number(detailRow.location_id)
|
||
: 0;
|
||
let checkedQuantity = 0;
|
||
let diffQuantitySum = 0;
|
||
let hasDiff = false;
|
||
const stocks = [ ...quantityByStock.keys() ]
|
||
.sort((left, right) => left - right)
|
||
.map(stockId => {
|
||
const agg = quantityByStock.get(stockId);
|
||
const stock = stockMap.get(stockId) || {};
|
||
const systemQuantity = locationId > 0
|
||
? (storageQuantityMap.get(`${locationId}_${stockId}`) || 0)
|
||
: 0;
|
||
const diffQuantity = agg.checked - systemQuantity;
|
||
checkedQuantity += agg.checked;
|
||
diffQuantitySum += diffQuantity;
|
||
if (diffQuantity !== 0) {
|
||
hasDiff = true;
|
||
}
|
||
return {
|
||
stock_id: stockId,
|
||
item_code: stock.item_code || null,
|
||
item_name: stock.item_name || null,
|
||
system_quantity: systemQuantity,
|
||
checked_quantity: agg.checked,
|
||
diff_quantity: diffQuantity,
|
||
};
|
||
});
|
||
|
||
return {
|
||
...base,
|
||
stocks,
|
||
checked_quantity: detailRecords.length > 0 ? checkedQuantity : null,
|
||
diff_quantity: detailRecords.length > 0 ? diffQuantitySum : null,
|
||
is_diff: hasDiff ? 1 : 0,
|
||
status: detailRecords.length > 0 ? 'checked' : 'unchecked',
|
||
};
|
||
}
|
||
|
||
/**
|