158 lines
4.9 KiB
Vue
158 lines
4.9 KiB
Vue
<template>
|
|
<div
|
|
v-if="receptionStore.current?.receptionType?.code === RECEPTION_TYPE_CODES.BOVINS"
|
|
class="flex flex-col items-center gap-16">
|
|
<h1 class="text-4xl uppercase font-bold">Sélection des marchandises réceptionnnées</h1>
|
|
<div
|
|
class="flex flex-row gap-8 items-center">
|
|
<div
|
|
v-for="type in bovineType"
|
|
:key="type.id"
|
|
class="mt-8 flex flex-row mb-2 gap-6">
|
|
<UiNumberInput
|
|
:label="type.label"
|
|
:code="type.code"
|
|
v-model="bovineQuantities[String(type.id)]"
|
|
:placeholder="0"
|
|
:min="0"
|
|
:max="10"
|
|
/>
|
|
</div>
|
|
<div
|
|
class="mt-8 flex flex-row mb-2 gap-6">
|
|
<UiNumberInput
|
|
label="Autres"
|
|
v-model="otherQuantity"
|
|
/>
|
|
</div>
|
|
</div>
|
|
<button
|
|
class="text-xl uppercase bg-primary-500 text-white h-[50px] w-[272px]"
|
|
@click="goNext"
|
|
>Peser
|
|
</button>
|
|
</div>
|
|
</template>
|
|
<script setup lang="ts">
|
|
import type {BovineTypeData} from "~/services/dto/bovine-type-data";
|
|
import {getBovineTypeList} from "~/services/bovine-type";
|
|
import {RECEPTION_TYPE_CODES} from "~/utils/constants";
|
|
import {useReceptionStore} from '~/stores/reception'
|
|
import {
|
|
createReceptionBovine,
|
|
deleteReceptionBovine,
|
|
getReceptionBovineList,
|
|
updateReceptionBovine
|
|
} from "~/services/reception-bovine";
|
|
import {computed, onMounted, reactive, ref, watch} from "vue";
|
|
|
|
const isLoadingBovineType = ref(false)
|
|
const bovineType = ref<BovineTypeData[]>([])
|
|
const receptionStore = useReceptionStore()
|
|
const bovineQuantities = reactive<Record<string, number | null>>({})
|
|
const otherQuantity = ref<number | null>(0)
|
|
const receptionId = computed(() => receptionStore.current?.id ?? null)
|
|
const receptionIri = computed(() =>
|
|
receptionId.value ? `/api/receptions/${receptionId.value}` : null
|
|
)
|
|
const loadBovineType = async () => {
|
|
isLoadingBovineType.value = true
|
|
try {
|
|
bovineType.value = await getBovineTypeList()
|
|
} finally {
|
|
isLoadingBovineType.value = false
|
|
}
|
|
}
|
|
|
|
onMounted(async () => {
|
|
await loadBovineType()
|
|
})
|
|
|
|
watch(
|
|
() => receptionId.value,
|
|
async (id) => {
|
|
if (!id || !receptionIri.value) {
|
|
return
|
|
}
|
|
|
|
const selectionMap: Record<string, number | null> = {}
|
|
for (const type of bovineType.value) {
|
|
selectionMap[String(type.id)] = 0
|
|
}
|
|
|
|
const existing = await getReceptionBovineList(receptionIri.value)
|
|
for (const selection of existing) {
|
|
const bovineTypeId = String(selection.bovineType.id)
|
|
selectionMap[bovineTypeId] = selection.quantity ?? 0
|
|
}
|
|
|
|
for (const key of Object.keys(bovineQuantities)) {
|
|
delete bovineQuantities[key]
|
|
}
|
|
Object.assign(bovineQuantities, selectionMap)
|
|
},
|
|
{immediate: true}
|
|
)
|
|
|
|
async function syncBovineSelections(receptionIri: string) {
|
|
const existing = await getReceptionBovineList(receptionIri)
|
|
const existingMap = new Map<string, { id: number; quantity: number | null }>()
|
|
for (const selection of existing) {
|
|
const bovineTypeId = String(selection.bovineType.id)
|
|
existingMap.set(bovineTypeId, {
|
|
id: selection.id,
|
|
quantity: selection.quantity ?? 0
|
|
})
|
|
}
|
|
|
|
// Supprime les entrées supprimées ou modifiées
|
|
for (const [bovineTypeId, entry] of existingMap.entries()) {
|
|
const selectedQuantity = bovineQuantities[bovineTypeId] ?? 0
|
|
if (!selectedQuantity) {
|
|
await deleteReceptionBovine(entry.id)
|
|
existingMap.delete(bovineTypeId)
|
|
continue
|
|
}
|
|
|
|
if (selectedQuantity !== entry.quantity) {
|
|
await updateReceptionBovine(entry.id, { quantity: selectedQuantity })
|
|
existingMap.set(bovineTypeId, {
|
|
id: entry.id,
|
|
quantity: selectedQuantity
|
|
})
|
|
}
|
|
}
|
|
|
|
// Crée les entrées manquantes
|
|
for (const [bovineTypeId, quantity] of Object.entries(bovineQuantities)) {
|
|
if (!quantity) {
|
|
continue
|
|
}
|
|
if (existingMap.has(bovineTypeId)) {
|
|
// Déjà à jour
|
|
continue
|
|
}
|
|
await createReceptionBovine({
|
|
reception: receptionIri,
|
|
bovineType: `/api/bovine_types/${bovineTypeId}`,
|
|
quantity
|
|
})
|
|
}
|
|
}
|
|
|
|
async function goNext() {
|
|
if (!receptionStore.current || !receptionIri.value) {
|
|
return
|
|
}
|
|
const nextStep = receptionStore.current.currentStep + 1
|
|
|
|
await syncBovineSelections(receptionIri.value)
|
|
|
|
await receptionStore.updateReception(receptionStore.current.id, {
|
|
merchandiseType: null,
|
|
merchandiseDetail: null,
|
|
currentStep: nextStep
|
|
})
|
|
}
|
|
</script>
|