95 lines
3.3 KiB
JavaScript
95 lines
3.3 KiB
JavaScript
import { createBoxes } from '@/data/demo-data.js'
|
|
import { createItemStates } from '@/utils/picking-state.js'
|
|
|
|
function clone(value) {
|
|
return JSON.parse(JSON.stringify(value))
|
|
}
|
|
|
|
function createInitialState() {
|
|
const boxes = createBoxes()
|
|
return {
|
|
boxes,
|
|
currentBoxIndex: 0,
|
|
itemStates: createItemStates(boxes[0].items),
|
|
partialQuantities: {}, scannedBoxCodes: {}, boxDrafts: {}, completedBoxes: {}, scanValue: '', scanHistory: []
|
|
}
|
|
}
|
|
|
|
let state = createInitialState()
|
|
|
|
const STORAGE_PREFIX = 'pda-picking-draft'
|
|
|
|
function storageAvailable() {
|
|
return typeof uni !== 'undefined' && typeof uni.getStorageSync === 'function' && typeof uni.setStorageSync === 'function'
|
|
}
|
|
|
|
function identityValue(identity = {}) {
|
|
const operatorId = identity.operatorId ?? identity.accountId ?? identity.userId
|
|
const taskId = identity.taskId ?? identity.inboundId
|
|
const containerNo = identity.containerNo ?? identity.boxNo
|
|
return { operatorId: operatorId == null ? '' : String(operatorId), taskId: taskId == null ? '' : String(taskId), containerNo: containerNo == null ? '' : String(containerNo) }
|
|
}
|
|
|
|
export function getPickingDraftKey(identity = {}) {
|
|
const value = identityValue(identity)
|
|
if (!value.operatorId || !value.taskId || !value.containerNo) return ''
|
|
return `${STORAGE_PREFIX}:${encodeURIComponent(value.operatorId)}:${encodeURIComponent(value.taskId)}:${encodeURIComponent(value.containerNo)}`
|
|
}
|
|
|
|
function draftPayload(nextState, identity) {
|
|
const value = identityValue(identity)
|
|
return {
|
|
operatorId: value.operatorId,
|
|
taskId: value.taskId,
|
|
containerNo: value.containerNo,
|
|
warehouseId: identity.warehouseId ?? null,
|
|
currentBoxIndex: nextState.currentBoxIndex,
|
|
itemStates: nextState.itemStates,
|
|
partialQuantities: nextState.partialQuantities,
|
|
scannedBoxCodes: nextState.scannedBoxCodes,
|
|
boxDrafts: nextState.boxDrafts,
|
|
completedBoxes: nextState.completedBoxes,
|
|
scanHistory: nextState.scanHistory,
|
|
updatedAt: new Date().toISOString()
|
|
}
|
|
}
|
|
|
|
export function getPickingSession() {
|
|
return clone(state)
|
|
}
|
|
|
|
export function savePickingSession(nextState, identity = {}) {
|
|
state = clone(nextState)
|
|
const key = getPickingDraftKey(identity)
|
|
if (key && storageAvailable()) uni.setStorageSync(key, draftPayload(state, identity))
|
|
}
|
|
|
|
export function restorePickingSession(identity = {}) {
|
|
const key = getPickingDraftKey(identity)
|
|
if (!key || !storageAvailable()) return null
|
|
const saved = uni.getStorageSync(key)
|
|
if (!saved || saved.operatorId !== identityValue(identity).operatorId || saved.taskId !== identityValue(identity).taskId || saved.containerNo !== identityValue(identity).containerNo) return null
|
|
const restored = { ...state, ...clone(saved) }
|
|
delete restored.operatorId
|
|
delete restored.taskId
|
|
delete restored.containerNo
|
|
delete restored.warehouseId
|
|
delete restored.updatedAt
|
|
state = restored
|
|
return clone(state)
|
|
}
|
|
|
|
export function clearPickingDraft(identity = {}) {
|
|
const key = getPickingDraftKey(identity)
|
|
if (key && typeof uni !== 'undefined' && typeof uni.removeStorageSync === 'function') uni.removeStorageSync(key)
|
|
}
|
|
|
|
export function resetPickingSession() {
|
|
state = createInitialState()
|
|
}
|
|
|
|
export function getPickingOverview() {
|
|
const completedCount = Object.values(state.completedBoxes).filter(Boolean).length
|
|
return { boxes: clone(state.boxes), completedBoxes: { ...state.completedBoxes }, completedCount, pendingBoxCount: state.boxes.length - completedCount }
|
|
}
|