c0d05264df
| Numéro du ticket | Titre du ticket | |------------------|-----------------| | 334 | Correctifs | ## Description de la PR ## Modification du .env ## Check list - [x] Pas de régression - [ ] TU/TI/TF rédigée - [x] TU/TI/TF OK - [x] CHANGELOG modifié Reviewed-on: https://gitea.malio.fr/MALIO-DEV/Ferme/pulls/32 Co-authored-by: Matteo <matteo@yuno.malio.fr> Co-committed-by: Matteo <matteo@yuno.malio.fr>
181 lines
6.0 KiB
TypeScript
181 lines
6.0 KiB
TypeScript
import type {Ref} from 'vue'
|
|
import {computed, ref} from 'vue'
|
|
import type {ReceptionData, ReceptionPayload, WeightEntryData} from '~/services/dto/reception-data'
|
|
import type {WeightData} from '~/services/dto/weight-data'
|
|
import {getWeight} from '~/services/reception'
|
|
import {getWeightShipment} from '~/services/shipment'
|
|
import {createWeight, updateWeight} from '~/services/weight'
|
|
import type {UseWeighingShipmentOptions, UseWeighingOptions} from '~/services/weight'
|
|
import type {WeightShipmentEntryData} from "~/services/dto/shipment-data";
|
|
|
|
export type WeighingMode = 'gross' | 'tare'
|
|
|
|
|
|
export const useWeighing = ({
|
|
mode,
|
|
reception,
|
|
updateReception,
|
|
loadReception
|
|
}: UseWeighingOptions) => {
|
|
const weightData = ref<WeightData | null>(null)
|
|
const isFetching = ref(false)
|
|
|
|
const currentWeightEntry = computed<WeightEntryData | null>(() => {
|
|
const weights = reception.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(() => (mode === 'gross' ? 'Pesée à plein' : 'Pesée à vide'))
|
|
const showLoadingBox = computed(
|
|
() => isFetching.value || (displayWeight.value === null && currentWeightEntry.value === null)
|
|
)
|
|
|
|
const fetchWeight = async () => {
|
|
isFetching.value = true
|
|
weightData.value = await getWeight().finally(() => {
|
|
isFetching.value = false
|
|
})
|
|
}
|
|
|
|
const saveWeight = async () => {
|
|
if (!reception.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
|
|
}
|
|
|
|
if (existingEntry?.id) {
|
|
await updateWeight(existingEntry.id, {
|
|
type: mode,
|
|
dsd: baseDsd,
|
|
weight: baseWeight,
|
|
weighedAt: baseWeighedAt
|
|
})
|
|
} else {
|
|
await createWeight({
|
|
reception: `api/receptions/${reception.value.id}`,
|
|
type: mode,
|
|
dsd: baseDsd,
|
|
weight: baseWeight,
|
|
weighedAt: baseWeighedAt
|
|
})
|
|
}
|
|
|
|
const nextStep = mode === 'tare'
|
|
? reception.value.currentStep
|
|
: reception.value.currentStep + 1
|
|
await updateReception(reception.value.id, {
|
|
currentStep: nextStep,
|
|
isValid: reception.value.isValid
|
|
})
|
|
|
|
if (loadReception) {
|
|
await loadReception(reception.value.id)
|
|
}
|
|
}
|
|
|
|
return {
|
|
weightData,
|
|
currentWeightEntry,
|
|
displayWeight,
|
|
displayDsd,
|
|
title,
|
|
showLoadingBox,
|
|
fetchWeight,
|
|
saveWeight
|
|
}
|
|
}
|
|
|
|
export const useWeighingShipment = ({
|
|
modeShipment,
|
|
shipment,
|
|
updateShipment,
|
|
loadShipment
|
|
}: UseWeighingShipmentOptions) => {
|
|
const weightData = ref<WeightData | null>(null)
|
|
const isFetching = ref(false)
|
|
|
|
const currentWeightEntry = computed<WeightShipmentEntryData | null>(() => {
|
|
const weights = shipment.value?.weights ?? []
|
|
return weights.find((entry) => entry.type === modeShipment) ?? null
|
|
})
|
|
|
|
const displayWeight = computed(() => weightData.value?.weight ?? currentWeightEntry.value?.weight ?? null)
|
|
const displayDsd = computed(() => weightData.value?.dsd ?? currentWeightEntry.value?.dsd ?? '-')
|
|
const title = computed(() => (modeShipment === 'gross' ? 'Pesée à vide' : 'Pesée à plein'))
|
|
const showLoadingBox = computed(
|
|
() => isFetching.value || (displayWeight.value === null && currentWeightEntry.value === null)
|
|
)
|
|
|
|
const fetchWeight = async () => {
|
|
isFetching.value = true
|
|
weightData.value = await getWeightShipment().finally(() => {
|
|
isFetching.value = false
|
|
})
|
|
}
|
|
|
|
const saveWeight = async () => {
|
|
if (!shipment.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
|
|
}
|
|
|
|
if (existingEntry?.id) {
|
|
await updateWeight(existingEntry.id, {
|
|
type: modeShipment,
|
|
dsd: baseDsd,
|
|
weight: baseWeight,
|
|
weighedAt: baseWeighedAt
|
|
})
|
|
} else {
|
|
await createWeight({
|
|
shipment: `api/shipments/${shipment.value.id}`,
|
|
type: modeShipment,
|
|
dsd: baseDsd,
|
|
weight: baseWeight,
|
|
weighedAt: baseWeighedAt
|
|
})
|
|
}
|
|
|
|
const nextStep = modeShipment === 'tare'
|
|
? shipment.value.currentStep
|
|
: shipment.value.currentStep + 1
|
|
await updateShipment(shipment.value.id, {
|
|
currentStep: nextStep,
|
|
isValid: shipment.value.isValid
|
|
})
|
|
|
|
if (loadShipment) {
|
|
await loadShipment(shipment.value.id)
|
|
}
|
|
}
|
|
|
|
return {
|
|
weightData,
|
|
currentWeightEntry,
|
|
displayWeight,
|
|
displayDsd,
|
|
title,
|
|
showLoadingBox,
|
|
fetchWeight,
|
|
saveWeight
|
|
}
|
|
}
|