来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
225 lines
7.8 KiB
JavaScript
225 lines
7.8 KiB
JavaScript
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);
|
||
}
|
||
|
||
/** 可否点「分拣完成」:须已确认落库;称重开启时仅斤类须有重量,计件重量选填 */
|
||
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.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,
|
||
};
|
||
});
|
||
}
|
||
|
||
module.exports = {
|
||
PICK_STATUS_LABELS,
|
||
displayText,
|
||
settlementQty,
|
||
storedSettlementQty,
|
||
isItemPicked,
|
||
isItemPending,
|
||
isItemReadyForComplete,
|
||
isScaleWeightMandatory,
|
||
resolvePickMeasures,
|
||
normalizePickingItem,
|
||
summarizeTask,
|
||
buildProgressRows,
|
||
};
|