Files
SupplierSystem/miniprogram/utils/pickingAssembly.js
T
Ubuntu b789aea353
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
feat(加工): 小程序 BOM/分配与分拣标准名对齐
补人工追价入口;移动端 BOM/成品预览;分拣任务标准商品名优先。
2026-11-04 13:05:40 +08:00

306 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const { isWeightUnit, buildUnitPicker, getUnitDictionaryLabels } = require('./units');
const PICK_STATUS_LABELS = {
10: '待分拣',
20: '分拣中',
// 出库并入装车核货:30 即分拣完成,待司机装车核对(20260821-merge-outbound-with-loading)
30: '分拣完成',
40: '已出库',
};
function displayText(value, fallback = '') {
if (value === undefined || value === null) return fallback;
const text = String(value).trim();
if (!text || text.toLowerCase() === 'null') return fallback;
return text;
}
function settlementQty(item) {
if (item && item.pick_by_weight) {
const weight = Number(item.input_weight ?? item.picked_weight_num ?? item.picked_weight ?? 0) || 0;
if (weight > 0) return weight;
}
const weight = Number(item.input_weight ?? item.picked_weight_num ?? item.picked_weight ?? 0) || 0;
const qty = Number(item.input_qty ?? item.picked_qty_num ?? item.picked_qty ?? 0) || 0;
return weight > 0 ? weight : qty;
}
/** 服务端已落库的实拣结算量(不含未提交 input) */
function storedSettlementQty(item) {
const pickedQty = Number(item.picked_qty_num ?? item.picked_qty ?? 0) || 0;
const pickedWeight = Number(item.picked_weight_num ?? item.picked_weight ?? 0) || 0;
return pickedWeight > 0 ? pickedWeight : pickedQty;
}
/** 是否已「确认分拣」——须 review_status≥20(与完成接口一致);仅有实拣量不算确认 */
function isItemPicked(item) {
const stored = storedSettlementQty(item);
if (stored <= 0) return false;
return (Number(item.review_status) || 0) >= 20;
}
function isItemPending(item) {
return !isItemPicked(item);
}
/**
* 列表该展示哪些行:
* - 未完成单(`pick_status < 30`):**只展示待分拣行** —— 已确认的即时移出视野
* (2026-10-16 开发者:「某一个商品分拣完毕了…就不用出现了,避免已分拣完的还占视野」);
* - 已完成单(`≥ 30`):只看待补录,全确认则回退展示全部(供复核);
* - 已出库(`≥ 40`):展示全部。
*/
function visibleItemsForStatus(items, pickStatus) {
const status = Number(pickStatus) || 0;
const list = items || [];
if (status >= 40) return list;
if (status >= 30) {
const needs = list.filter(
(item) => isItemPending(item) || Number(item.settlement_qty || 0) <= 0
);
return needs.length ? needs : list;
}
return list.filter((item) => isItemPending(item));
}
/** 可否点「分拣完成」:须已确认落库;称重开启时仅斤类须有重量,计件重量选填 */
function isItemReadyForComplete(item) {
if (!(settlementQty(item) > 0)) return false;
if (item.scale_required !== true) return true;
const byWeight = item.pick_by_weight === true || isWeightUnit(item.unit);
if (!byWeight) return true;
const required = Number(item.required_qty) || 0;
if (required <= 0) return true;
const measures = resolvePickMeasures(item);
return (Number(measures.picked_weight) || 0) > 0;
}
function isScaleWeightMandatory(item, scaleRequired) {
if (!scaleRequired) return false;
const byWeight = item && (item.pick_by_weight === true || isWeightUnit(item.unit));
if (!byWeight) return false;
if ((Number(item.required_qty) || 0) <= 0) return false;
return !(Number(resolvePickMeasures(item).picked_weight) > 0);
}
/** 按单位模式解析提交用的 qty/weight */
function resolvePickMeasures(item) {
const required = Number(item.required_qty) || 0;
const byWeight = item.pick_by_weight === true || isWeightUnit(item.unit);
const scaleRequired = item.scale_required === true;
if (byWeight) {
const raw = Number(item.input_weight);
const weight = Number.isFinite(raw) ? raw : required;
return { picked_qty: weight, picked_weight: weight, byWeight: true };
}
const raw = Number(item.input_qty);
const qty = Number.isFinite(raw) ? raw : required;
let picked_weight = 0;
if (scaleRequired) {
const wRaw = Number(item.input_weight);
if (Number.isFinite(wRaw)) {
picked_weight = wRaw;
} else {
picked_weight = Number(item.picked_weight_num ?? item.picked_weight ?? 0) || 0;
}
}
return { picked_qty: qty, picked_weight, byWeight: false };
}
function normalizePickingItem(item, pickStatus, options = {}) {
const pickedQty = Number(item.picked_qty) || 0;
const pickedWeight = Number(item.picked_weight) || 0;
const requiredQty = Number(item.required_qty) || 0;
const skuCode = displayText(item.sku_code);
const barcode = displayText(item.barcode, skuCode);
const containerCode = displayText(item.container_code);
const status = Number(pickStatus) || 0;
const unit = displayText(item.unit, '个') || '个';
const pickByWeight = isWeightUnit(unit);
const scaleRequired =
options.scaleRequired === true || item.scale_required === true;
const { unitOptions, unitIndex } = buildUnitPicker(unit, getUnitDictionaryLabels());
const settlement = pickedWeight > 0 ? pickedWeight : pickedQty;
const needsPick = settlement <= 0;
const pickedDone = !needsPick && settlement > 0 && Number(item.review_status) >= 20;
const staged = Boolean(containerCode);
let stepLabel = '待分拣';
let stepClass = 'step-pick';
// 分拣完成但本行未拣:仍按待分拣展示,允许补录(后端会回退任务)
if (status >= 30 && !needsPick) {
stepLabel = status >= 40 ? '已出库' : '分拣完成';
stepClass = 'step-done';
} else if (status >= 40) {
stepLabel = '已出库';
stepClass = 'step-done';
} else if (pickedDone) {
stepLabel = settlement < requiredQty ? '已确认(少拣)' : '已分拣';
stepClass = 'step-done';
} else if (settlement > 0) {
stepLabel = '分拣中';
stepClass = 'step-stage';
} else if (status >= 30 && needsPick) {
stepLabel = '待补录';
stepClass = 'step-pick';
}
// 重量单位:默认实拣进重量框;计件:进数量框。避免双框都显示 0
let inputQty = item.input_qty;
let inputWeight = item.input_weight;
if (inputQty === undefined || inputQty === null) {
inputQty = pickByWeight ? pickedQty || 0 : pickedQty || requiredQty || 0;
}
if (inputWeight === undefined || inputWeight === null) {
inputWeight = pickByWeight
? pickedWeight || requiredQty || 0
: scaleRequired
? pickedWeight || 0
: pickedWeight || 0;
}
const canEditPick = status < 40 && (status < 30 || needsPick);
return {
...item,
unit,
unitOptions,
unitIndex,
pick_by_weight: pickByWeight,
scale_required: scaleRequired,
need_extra_weight: scaleRequired && !pickByWeight,
qty_chip_label: pickByWeight ? '重量' : '数量',
input_qty: inputQty,
input_weight: inputWeight,
input_measure: pickByWeight ? inputWeight : inputQty,
display_name: displayText(item.standard_name || item.item_name, skuCode || '未知商品'),
display_sku: skuCode || '待生成',
display_barcode: barcode || skuCode || '待生成',
picked_qty_num: pickedQty,
picked_weight_num: pickedWeight,
settlement_qty: settlement,
picked_done: pickedDone,
container_code: containerCode,
step_label: stepLabel,
step_class: stepClass,
can_pick: canEditPick,
can_stage: false,
show_stage_btn: false,
is_staged: staged,
needs_pick: needsPick,
};
}
function summarizeTask(items, pickStatus) {
const total = items.length;
const pickedCount = items.filter((item) => isItemPicked(item)).length;
const stagedCount = items.filter((item) => item.is_staged || item.container_code).length;
const status = Number(pickStatus) || 0;
return {
total,
pickedCount,
stagedCount,
unpickedCount: Math.max(total - pickedCount, 0),
unstagedCount: Math.max(total - stagedCount, 0),
canComplete:
total > 0 &&
items.every((item) => isItemPicked(item) && isItemReadyForComplete(item)) &&
status < 30,
canAudit:
total > 0 &&
items.every((item) => isItemPicked(item) && isItemReadyForComplete(item)) &&
status < 30,
statusLabel: PICK_STATUS_LABELS[status] || `状态${status}`,
};
}
function buildProgressRows(items, pickCode) {
return (items || []).map((item) => {
const measures = resolvePickMeasures(item);
return {
id: item.id,
required_qty: item.required_qty,
picked_qty: measures.picked_qty,
picked_weight: measures.picked_weight,
unit: item.unit || null,
diff_reason: item.diff_reason_input || item.diff_reason || '',
pick_code: pickCode,
outbound_batches: Array.isArray(item.outbound_batches) ? item.outbound_batches : undefined,
outbound_photos: Array.isArray(item.outbound_photos) ? item.outbound_photos : undefined,
};
});
}
/** 本行本次出库需扣的总量(重量优先) */
function resolveItemNeed(item) {
const measures = resolvePickMeasures(item);
return Number(measures.picked_weight) > 0
? Number(measures.picked_weight) || 0
: Number(measures.picked_qty) || 0;
}
/**
* 按先进先出把 need 量分配到候选批次(batches 已按 FIFO 排序)。
* 返回 { plan:[{batch_no,qty}], shortfall };shortfall=库存总量不足的部分。
*/
function allocateBatchesFifo(batches, need) {
const needQty = Math.max(0, Number(need) || 0);
const plan = [];
let remaining = needQty;
let availableSum = 0;
(batches || []).forEach((b) => {
const avail = Number(b.available_qty) || 0;
if (avail <= 0) return;
availableSum += avail;
if (remaining <= 0) return;
const take = Math.min(avail, remaining);
plan.push({ batch_no: String(b.batch_no), qty: Math.round(take * 1000) / 1000 });
remaining = Math.round((remaining - take) * 1000) / 1000;
});
return { plan, shortfall: Math.round(Math.max(0, needQty - availableSum) * 1000) / 1000 };
}
/** 行内批次摘要文案(开发者 2026-10-12 定稿):
* 有区 → `区 入库日 数量单位`,如「冷库B 10-12 5斤」;无区 → `入库日 数量单位`。
* 多个批次用 ` · ` 分隔。 */
function formatBatchPlanText(plan, batches, unit) {
const byNo = new Map();
(batches || []).forEach((b) => byNo.set(String(b.batch_no), b));
const unitText = displayText(unit);
return (plan || [])
.map((p) => {
const b = byNo.get(String(p.batch_no)) || {};
const date = displayText(b.inbound_date || b.created_at).slice(5, 10);
const qty = `${formatBatchQty(p.qty)}${unitText}`;
// 区域优先(开发者 2026-10-12 选 ②):批次行显示「区域 + 入库日 + 取多少」,让分拣员知道去哪个区拿多少
const zone = String(b.storage_zone || '').trim();
return [zone, date, qty].filter(Boolean).join(' ');
})
.filter(Boolean)
.join(' · ');
}
function formatBatchQty(value) {
const num = Number(value) || 0;
return String(Math.round(num * 1000) / 1000);
}
module.exports = {
PICK_STATUS_LABELS,
displayText,
settlementQty,
storedSettlementQty,
isItemPicked,
isItemPending,
visibleItemsForStatus,
isItemReadyForComplete,
isScaleWeightMandatory,
resolvePickMeasures,
normalizePickingItem,
summarizeTask,
buildProgressRows,
resolveItemNeed,
allocateBatchesFifo,
formatBatchPlanText,
formatBatchQty,
};