164 lines
5.1 KiB
TypeScript
164 lines
5.1 KiB
TypeScript
import { computed, ref } from 'vue'
|
|
import type { Ref } from 'vue'
|
|
import type { WeightEntryData } from '~/services/dto/reception-data'
|
|
import type { WeightData } from '~/services/dto/weight-data'
|
|
import { createWeight, updateWeight } from '~/services/weight'
|
|
|
|
export type WeighingMode = 'gross' | 'tare'
|
|
|
|
export interface UseWeighingOptions {
|
|
mode: WeighingMode
|
|
entity: Ref<{ id: number; currentStep: number; isValid: boolean; weights?: WeightEntryData[] | null } | null>
|
|
entityName: 'reception' | 'shipment'
|
|
apiResource: string
|
|
titleLabel: string
|
|
getWeightFromScale: () => Promise<WeightData>
|
|
updateEntity: (id: number, payload: any) => Promise<any>
|
|
loadEntity?: (id: number) => Promise<any>
|
|
}
|
|
|
|
export const useWeighing = ({
|
|
mode,
|
|
entity,
|
|
entityName,
|
|
apiResource,
|
|
titleLabel,
|
|
getWeightFromScale,
|
|
updateEntity,
|
|
loadEntity
|
|
}: UseWeighingOptions) => {
|
|
const weightData = ref<WeightData | null>(null)
|
|
const isFetching = ref(false)
|
|
|
|
const currentWeightEntry = computed<WeightEntryData | null>(() => {
|
|
const weights = entity.value?.weights ?? []
|
|
return weights.find((entry) => entry.type === mode) ?? null
|
|
})
|
|
|
|
const displayWeight = computed(() => weightData.value?.weight ?? currentWeightEntry.value?.weight ?? null)
|
|
const displayDsd = computed(() => weightData.value?.dsd ?? currentWeightEntry.value?.dsd ?? '-')
|
|
const title = computed(() => titleLabel)
|
|
const showLoadingBox = computed(() => isFetching.value)
|
|
|
|
const fetchWeight = async () => {
|
|
isFetching.value = true
|
|
weightData.value = await getWeightFromScale().finally(() => {
|
|
isFetching.value = false
|
|
})
|
|
}
|
|
|
|
const saveWeight = async () => {
|
|
if (!entity.value) return
|
|
|
|
const existingEntry = currentWeightEntry.value
|
|
const baseDsd = weightData.value?.dsd ?? existingEntry?.dsd ?? null
|
|
const baseWeight = weightData.value?.weight ?? existingEntry?.weight ?? null
|
|
const baseWeighedAt = weightData.value?.weighedAt ?? existingEntry?.weighedAt ?? null
|
|
|
|
if (baseWeight === null) return
|
|
|
|
const relationPayload: Record<string, string> = {}
|
|
relationPayload[entityName] = `/api/${apiResource}/${entity.value.id}`
|
|
|
|
if (existingEntry?.id) {
|
|
await updateWeight(existingEntry.id, {
|
|
type: mode,
|
|
dsd: baseDsd,
|
|
weight: baseWeight,
|
|
weighedAt: baseWeighedAt
|
|
})
|
|
} else {
|
|
await createWeight({
|
|
...relationPayload,
|
|
type: mode,
|
|
dsd: baseDsd,
|
|
weight: baseWeight,
|
|
weighedAt: baseWeighedAt
|
|
})
|
|
}
|
|
|
|
const nextStep = mode === 'tare'
|
|
? entity.value.currentStep
|
|
: entity.value.currentStep + 1
|
|
await updateEntity(entity.value.id, {
|
|
currentStep: nextStep,
|
|
isValid: entity.value.isValid
|
|
})
|
|
|
|
if (loadEntity) {
|
|
await loadEntity(entity.value.id)
|
|
}
|
|
}
|
|
|
|
const saveWeightDraft = async () => {
|
|
if (!entity.value) return
|
|
if (!weightData.value && !currentWeightEntry.value) return
|
|
|
|
const existingEntry = currentWeightEntry.value
|
|
const baseDsd = weightData.value?.dsd ?? existingEntry?.dsd ?? null
|
|
const baseWeight = weightData.value?.weight ?? existingEntry?.weight ?? null
|
|
const baseWeighedAt = weightData.value?.weighedAt ?? existingEntry?.weighedAt ?? null
|
|
|
|
if (baseWeight === null) return
|
|
|
|
const relationPayload: Record<string, string> = {}
|
|
relationPayload[entityName] = `/api/${apiResource}/${entity.value.id}`
|
|
|
|
if (existingEntry?.id) {
|
|
await updateWeight(existingEntry.id, {
|
|
type: mode,
|
|
dsd: baseDsd,
|
|
weight: baseWeight,
|
|
weighedAt: baseWeighedAt
|
|
})
|
|
} else {
|
|
await createWeight({
|
|
...relationPayload,
|
|
type: mode,
|
|
dsd: baseDsd,
|
|
weight: baseWeight,
|
|
weighedAt: baseWeighedAt
|
|
})
|
|
}
|
|
}
|
|
|
|
return {
|
|
weightData,
|
|
currentWeightEntry,
|
|
displayWeight,
|
|
displayDsd,
|
|
title,
|
|
showLoadingBox,
|
|
fetchWeight,
|
|
saveWeight,
|
|
saveWeightDraft
|
|
}
|
|
}
|
|
|
|
// Backward-compatible aliases
|
|
export const useWeighingShipment = ({
|
|
modeShipment,
|
|
shipment,
|
|
updateShipment,
|
|
loadShipment
|
|
}: {
|
|
modeShipment: WeighingMode
|
|
shipment: Ref<any>
|
|
updateShipment: (id: number, payload: any) => Promise<any>
|
|
loadShipment?: (id: number) => Promise<any>
|
|
}) => {
|
|
return useWeighing({
|
|
mode: modeShipment,
|
|
entity: shipment,
|
|
entityName: 'shipment',
|
|
apiResource: 'shipments',
|
|
titleLabel: modeShipment === 'gross' ? 'Pesée à vide' : 'Pesée à plein',
|
|
getWeightFromScale: async () => {
|
|
const { getWeightShipment } = await import('~/services/shipment')
|
|
return getWeightShipment()
|
|
},
|
|
updateEntity: updateShipment,
|
|
loadEntity: loadShipment
|
|
})
|
|
}
|