fix : trie du code et reorganisation composant parent enfant
This commit is contained in:
@@ -173,17 +173,15 @@
|
||||
|
||||
<update-merchandise
|
||||
v-show="activeTab === 'merchandise' && isMerchandise"
|
||||
:idReception="idReception"
|
||||
:disabled="!auth.isAdmin"
|
||||
:isValidate="isValidMerchandise"
|
||||
v-model="merchandiseForm"
|
||||
:isAdmin="auth.isAdmin"
|
||||
/>
|
||||
|
||||
<update-bovin
|
||||
v-show="activeTab === 'merchandise' && !isMerchandise"
|
||||
:idReception="idReception"
|
||||
:disabled="!auth.isAdmin"
|
||||
:isValidate="isValidBovin"
|
||||
|
||||
v-model="bovineEntries"
|
||||
v-model:otherQuantity="bovineOtherQuantity"
|
||||
:isAdmin="auth.isAdmin"
|
||||
/>
|
||||
</div>
|
||||
<div class="flex justify-center">
|
||||
@@ -201,62 +199,67 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import {usePdfPrinter} from "#imports"
|
||||
import {useReceptionStore} from '~/stores/reception'
|
||||
import type {UserData} from '~/services/dto/user-data'
|
||||
import {getUsers} from '~/services/auth'
|
||||
import {useAuthStore} from '~/stores/auth'
|
||||
import type {SupplierData} from '~/services/dto/supplier-data'
|
||||
import {getSupplierList} from '~/services/supplier'
|
||||
import type {TruckData} from '~/services/dto/truck-data'
|
||||
import {getTruckList} from '~/services/truck'
|
||||
import type {CarrierData} from '~/services/dto/carrier-data'
|
||||
import {getCarrierList} from '~/services/carrier'
|
||||
import type {DriverData} from '~/services/dto/driver-data'
|
||||
import {getDriverList} from '~/services/driver'
|
||||
import type {VehicleData} from '~/services/dto/vehicle-data'
|
||||
import {getVehicleList} from '~/services/vehicle'
|
||||
import {RECEPTION_TYPE_CODES, SUPPLIER_CODE} from "~/utils/constants";
|
||||
import {deleteReceptionBovine, getReceptionBovineList,} from "~/services/reception-bovine";
|
||||
import type {ReceptionData, ReceptionFormData, WeightEntryData} from "~/services/dto/reception-data";
|
||||
import {getReception, updateReception} from "~/services/reception";
|
||||
import UpdateWeight from "~/components/reception/update-weight.vue";
|
||||
import UpdateMerchandise from "~/components/reception/update-merchandise.vue";
|
||||
import UpdateBovin from "~/components/reception/update-bovin.vue";
|
||||
import {getReceptionTypeList} from "~/services/reception-type";
|
||||
import type {ReceptionTypeData} from "~/services/dto/reception-type-data";
|
||||
import {computed} from "vue";
|
||||
import {createWeight, updateWeight} from "~/services/weight";
|
||||
import {deleteReceptionPelletBuilding, getReceptionPelletBuildingList} from "~/services/reception-pellet-building";
|
||||
import { usePdfPrinter } from '#imports'
|
||||
import { computed } from 'vue'
|
||||
import UpdateBovin from '~/components/reception/update-bovin.vue'
|
||||
import UpdateMerchandise from '~/components/reception/update-merchandise.vue'
|
||||
import UpdateWeight from '~/components/reception/update-weight.vue'
|
||||
import { getUsers } from '~/services/auth'
|
||||
import { getCarrierList } from '~/services/carrier'
|
||||
import type { CarrierData } from '~/services/dto/carrier-data'
|
||||
import type { DriverData } from '~/services/dto/driver-data'
|
||||
import type { ReceptionBovineTypeData } from '~/services/dto/reception-bovine-data'
|
||||
import type {
|
||||
MerchandiseEntryData,
|
||||
ReceptionData,
|
||||
ReceptionFormData,
|
||||
WeightEntryData
|
||||
} from '~/services/dto/reception-data'
|
||||
import type { ReceptionTypeData } from '~/services/dto/reception-type-data'
|
||||
import type { SupplierData } from '~/services/dto/supplier-data'
|
||||
import type { TruckData } from '~/services/dto/truck-data'
|
||||
import type { UserData } from '~/services/dto/user-data'
|
||||
import type { VehicleData } from '~/services/dto/vehicle-data'
|
||||
import { getDriverList } from '~/services/driver'
|
||||
import {
|
||||
createReceptionBovine,
|
||||
deleteReceptionBovine,
|
||||
getReceptionBovineList,
|
||||
updateReceptionBovine
|
||||
} from '~/services/reception-bovine'
|
||||
import {
|
||||
createReceptionPelletBuilding,
|
||||
deleteReceptionPelletBuilding,
|
||||
getReceptionPelletBuildingList
|
||||
} from '~/services/reception-pellet-building'
|
||||
import { getReception, updateReception } from '~/services/reception'
|
||||
import { getReceptionTypeList } from '~/services/reception-type'
|
||||
import { getSupplierList } from '~/services/supplier'
|
||||
import { getTruckList } from '~/services/truck'
|
||||
import { getVehicleList } from '~/services/vehicle'
|
||||
import { createWeight, updateWeight } from '~/services/weight'
|
||||
import { useAuthStore } from '~/stores/auth'
|
||||
import { useReceptionStore } from '~/stores/reception'
|
||||
import { RECEPTION_TYPE_CODES, SUPPLIER_CODE } from '~/utils/constants'
|
||||
|
||||
const form = reactive<ReceptionFormData>({
|
||||
identificationNumber: null,
|
||||
licensePlate: '',
|
||||
receptionDate: new Date().toISOString().slice(0, 10),
|
||||
receptionTypeId: '',
|
||||
userId: '',
|
||||
supplierId: '',
|
||||
addressId: '',
|
||||
truckId: '',
|
||||
carrierId: '',
|
||||
driverId: '',
|
||||
vehicleId: ''
|
||||
})
|
||||
|
||||
const createEmptyWeightEntry = (type: 'gross' | 'tare'): WeightEntryData => ({
|
||||
type,
|
||||
dsd: null,
|
||||
weight: null,
|
||||
weighedAt: null
|
||||
})
|
||||
const router = useRouter()
|
||||
const route = useRoute()
|
||||
const auth = useAuthStore()
|
||||
const authStore = useAuthStore()
|
||||
const receptionStore = useReceptionStore()
|
||||
const { printPdf } = usePdfPrinter()
|
||||
|
||||
const activeTab = ref<'weightsEmpty' | 'weights' | 'merchandise'>('weights')
|
||||
const grossWeight = ref<WeightEntryData>(createEmptyWeightEntry('gross'))
|
||||
const tareWeight = ref<WeightEntryData>(createEmptyWeightEntry('tare'))
|
||||
|
||||
const { printPdf } = usePdfPrinter()
|
||||
const activeTab = ref<'weightsEmpty' | 'weights' | 'merchandise'>('weights')
|
||||
const router = useRouter()
|
||||
const receptionStore = useReceptionStore()
|
||||
const bovineEntries = ref<ReceptionBovineTypeData[]>([])
|
||||
const bovineOtherQuantity = ref<number | null>(0)
|
||||
const merchandiseForm = ref<MerchandiseEntryData>({
|
||||
merchandiseTypeId: '',
|
||||
merchandiseDetail: '',
|
||||
selectedBuildingIds: [],
|
||||
selectedPelletBuildingIds: {}
|
||||
})
|
||||
const allowAnyLicensePlate = ref(false)
|
||||
const isLoading = ref(false)
|
||||
const users = ref<UserData[]>([])
|
||||
@@ -273,28 +276,30 @@ const drivers = ref<DriverData[]>([])
|
||||
const isLoadingDrivers = ref(false)
|
||||
const vehicles = ref<VehicleData[]>([])
|
||||
const isLoadingVehicles = ref(false)
|
||||
const authStore = useAuthStore()
|
||||
const isValid = ref(false)
|
||||
const isValidBovin = ref(false)
|
||||
const isValidMerchandise = ref(false)
|
||||
const formIsLoading = ref(false)
|
||||
const route = useRoute()
|
||||
const idReception = Number(route.params.id)
|
||||
const auth = useAuthStore()
|
||||
const isMerchandise = ref(false)
|
||||
|
||||
// Empêche les watchers de reset des champs pendant le remplissage initial
|
||||
const isHydrating = ref(false)
|
||||
|
||||
// Transporteur sélectionné dans le formulaire
|
||||
const idReception = Number(route.params.id)
|
||||
|
||||
const form = reactive<ReceptionFormData>({
|
||||
identificationNumber: null,
|
||||
licensePlate: '',
|
||||
receptionDate: new Date().toISOString().slice(0, 10),
|
||||
receptionTypeId: '',
|
||||
userId: '',
|
||||
supplierId: '',
|
||||
addressId: '',
|
||||
truckId: '',
|
||||
carrierId: '',
|
||||
driverId: '',
|
||||
vehicleId: ''
|
||||
})
|
||||
|
||||
const selectedCarrier = computed(() =>
|
||||
carriers.value.find((carrier) => String(carrier.id) === form.carrierId) ?? null
|
||||
)
|
||||
|
||||
// Indique si le transporteur est LIOT
|
||||
const isLiotCarrier = computed(() => selectedCarrier.value?.code === SUPPLIER_CODE.LIOT)
|
||||
|
||||
// Adresses disponibles pour le fournisseur sélectionné
|
||||
const supplierAddresses = computed(() => {
|
||||
const supplierId = Number(form.supplierId)
|
||||
if (!Number.isFinite(supplierId)) {
|
||||
@@ -302,16 +307,12 @@ const supplierAddresses = computed(() => {
|
||||
}
|
||||
return suppliers.value.find((supplier) => supplier.id === supplierId)?.addresses ?? []
|
||||
})
|
||||
|
||||
// Chauffeurs filtrés par transporteur (LIOT)
|
||||
const filteredDrivers = computed<DriverData[]>(() => {
|
||||
if (!form.carrierId) {
|
||||
return []
|
||||
}
|
||||
return drivers.value.filter((driver) => String(driver.carrier?.id) === form.carrierId)
|
||||
})
|
||||
|
||||
// Véhicules filtrés par transporteur + type de camion
|
||||
const filteredVehicles = computed<VehicleData[]>(() => {
|
||||
if (!form.carrierId) {
|
||||
return []
|
||||
@@ -323,8 +324,42 @@ const filteredVehicles = computed<VehicleData[]>(() => {
|
||||
)
|
||||
})
|
||||
|
||||
// Supprime les données bovines si on change de type de réception
|
||||
const clearReceptionBovines = async (receptionId: number) => {
|
||||
watch(
|
||||
() => idReception,
|
||||
async (id) => {
|
||||
if (id === null) {
|
||||
return
|
||||
}
|
||||
isLoading.value = true
|
||||
try {
|
||||
const reception = await getReception(id)
|
||||
hydrateFromReception(reception)
|
||||
await loadBovineEntries(id)
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
},
|
||||
{immediate: true}
|
||||
)
|
||||
|
||||
watch(
|
||||
() => form.receptionTypeId,
|
||||
() => {
|
||||
const receptionType = receptionTypes.value.find((type) => String(type.id) === form.receptionTypeId) ?? null
|
||||
isMerchandise.value = receptionType?.code === RECEPTION_TYPE_CODES.MERCHANDISES
|
||||
}
|
||||
)
|
||||
|
||||
function createEmptyWeightEntry(type: 'gross' | 'tare'): WeightEntryData {
|
||||
return {
|
||||
type,
|
||||
dsd: null,
|
||||
weight: null,
|
||||
weighedAt: null
|
||||
}
|
||||
}
|
||||
|
||||
async function clearReceptionBovines(receptionId: number) {
|
||||
const receptionIri = `/api/receptions/${receptionId}`
|
||||
const existing = await getReceptionBovineList(receptionIri)
|
||||
for (const selection of existing) {
|
||||
@@ -332,22 +367,46 @@ const clearReceptionBovines = async (receptionId: number) => {
|
||||
}
|
||||
}
|
||||
|
||||
const syncMerchandiseFlag = () => {
|
||||
async function loadBovineEntries(receptionId: number) {
|
||||
const receptionIri = `/api/receptions/${receptionId}`
|
||||
bovineEntries.value = await getReceptionBovineList(receptionIri)
|
||||
}
|
||||
|
||||
function syncMerchandiseFlag() {
|
||||
const receptionType =
|
||||
receptionTypes.value.find((type) => String(type.id) === form.receptionTypeId) ?? null
|
||||
isMerchandise.value = receptionType?.code === RECEPTION_TYPE_CODES.MERCHANDISES
|
||||
}
|
||||
|
||||
const clearReceptionMerchandise = async (receptionId: number) => {
|
||||
const receptionIri = `/api/receptions/${receptionId}`
|
||||
function getRelationId(value: unknown): string | null {
|
||||
if (!value) {
|
||||
return null
|
||||
}
|
||||
|
||||
// supprime toutes les associations granulés/bâtiments
|
||||
if (typeof value === 'string') {
|
||||
const match = value.match(/\/(\d+)$/)
|
||||
return match ? match[1] : null
|
||||
}
|
||||
|
||||
if (typeof value === 'object' && 'id' in value) {
|
||||
const relation = value as { id?: number | string }
|
||||
if (typeof relation.id === 'number') {
|
||||
return String(relation.id)
|
||||
}
|
||||
if (typeof relation.id === 'string') {
|
||||
return relation.id
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
async function clearReceptionMerchandise(receptionId: number) {
|
||||
const receptionIri = `/api/receptions/${receptionId}`
|
||||
const existing = await getReceptionPelletBuildingList(receptionIri)
|
||||
for (const selection of existing) {
|
||||
await deleteReceptionPelletBuilding(selection.id)
|
||||
}
|
||||
|
||||
// reset des champs marchandise
|
||||
await updateReception(receptionId, {
|
||||
merchandiseType: null,
|
||||
merchandiseDetail: null,
|
||||
@@ -355,7 +414,7 @@ const clearReceptionMerchandise = async (receptionId: number) => {
|
||||
})
|
||||
}
|
||||
|
||||
const hydrateFromReception = (reception: ReceptionData | null) => {
|
||||
function hydrateFromReception(reception: ReceptionData | null) {
|
||||
if (!reception) {
|
||||
return
|
||||
}
|
||||
@@ -384,41 +443,41 @@ const hydrateFromReception = (reception: ReceptionData | null) => {
|
||||
form.receptionTypeId = reception?.receptionType?.id
|
||||
? String(reception.receptionType.id)
|
||||
: ''
|
||||
|
||||
const selectionMap: Record<string, string[]> = {}
|
||||
for (const selection of reception?.pelletBuildings ?? []) {
|
||||
const pelletTypeId = getRelationId(selection.pelletType)
|
||||
const buildingId = getRelationId(selection.building)
|
||||
if (!pelletTypeId || !buildingId) {
|
||||
continue
|
||||
}
|
||||
if (!selectionMap[pelletTypeId]) {
|
||||
selectionMap[pelletTypeId] = []
|
||||
}
|
||||
selectionMap[pelletTypeId].push(buildingId)
|
||||
}
|
||||
|
||||
merchandiseForm.value = {
|
||||
merchandiseTypeId: reception?.merchandiseType?.id ? String(reception.merchandiseType.id) : '',
|
||||
merchandiseDetail: reception?.merchandiseDetail ?? '',
|
||||
selectedBuildingIds: reception?.buildings?.map((building) => String(building.id)) ?? [],
|
||||
selectedPelletBuildingIds: selectionMap
|
||||
}
|
||||
|
||||
const parsedOther =
|
||||
typeof reception?.bovineDetail === 'string' && reception.bovineDetail.trim() !== ''
|
||||
? Number(reception.bovineDetail)
|
||||
: 0
|
||||
bovineOtherQuantity.value = Number.isFinite(parsedOther) ? parsedOther : 0
|
||||
const gross = reception.weights?.find((weight) => weight.type === 'gross') ?? null
|
||||
const tare = reception.weights?.find((weight) => weight.type === 'tare') ?? null
|
||||
grossWeight.value = gross ? {...gross} : createEmptyWeightEntry('gross')
|
||||
tareWeight.value = tare ? {...tare} : createEmptyWeightEntry('tare')
|
||||
grossWeight.value = gross ? { ...gross } : createEmptyWeightEntry('gross')
|
||||
tareWeight.value = tare ? { ...tare } : createEmptyWeightEntry('tare')
|
||||
isHydrating.value = false
|
||||
syncMerchandiseFlag()
|
||||
}
|
||||
|
||||
watch(
|
||||
() => idReception,
|
||||
async (id) => {
|
||||
if (id === null) {
|
||||
return
|
||||
}
|
||||
isLoading.value = true
|
||||
try {
|
||||
const reception = await getReception(id)
|
||||
hydrateFromReception(reception)
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
},
|
||||
{immediate: true}
|
||||
)
|
||||
|
||||
watch(
|
||||
() => form.receptionTypeId,
|
||||
() => {
|
||||
const receptionType = receptionTypes.value.find((type) => String(type.id) === form.receptionTypeId) ?? null
|
||||
isMerchandise.value = receptionType?.code === RECEPTION_TYPE_CODES.MERCHANDISES
|
||||
}
|
||||
)
|
||||
|
||||
// Charge la liste des users pour le select
|
||||
const loadUsers = async () => {
|
||||
async function loadUsers() {
|
||||
isLoadingUsers.value = true
|
||||
try {
|
||||
users.value = await getUsers()
|
||||
@@ -427,8 +486,7 @@ const loadUsers = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// Charge la liste des fournisseurs pour le select
|
||||
const loadSuppliers = async () => {
|
||||
async function loadSuppliers() {
|
||||
isLoadingSuppliers.value = true
|
||||
try {
|
||||
suppliers.value = await getSupplierList()
|
||||
@@ -437,8 +495,7 @@ const loadSuppliers = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
//charge la liste des types pour le select
|
||||
const loadTypes = async () => {
|
||||
async function loadTypes() {
|
||||
isLoadingTypes.value = true
|
||||
try {
|
||||
receptionTypes.value = await getReceptionTypeList()
|
||||
@@ -447,8 +504,7 @@ const loadTypes = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// Charge la liste des camions pour le select
|
||||
const loadTrucks = async () => {
|
||||
async function loadTrucks() {
|
||||
isLoadingTrucks.value = true
|
||||
try {
|
||||
trucks.value = await getTruckList()
|
||||
@@ -457,8 +513,7 @@ const loadTrucks = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// Charge la liste des transporteurs pour le select
|
||||
const loadCarriers = async () => {
|
||||
async function loadCarriers() {
|
||||
isLoadingCarriers.value = true
|
||||
try {
|
||||
carriers.value = await getCarrierList()
|
||||
@@ -467,8 +522,7 @@ const loadCarriers = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// Charge la liste des chauffeurs pour le select
|
||||
const loadDrivers = async () => {
|
||||
async function loadDrivers() {
|
||||
isLoadingDrivers.value = true
|
||||
try {
|
||||
drivers.value = await getDriverList()
|
||||
@@ -477,8 +531,7 @@ const loadDrivers = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// Charge la liste des véhicules pour le select
|
||||
const loadVehicles = async () => {
|
||||
async function loadVehicles() {
|
||||
isLoadingVehicles.value = true
|
||||
try {
|
||||
vehicles.value = await getVehicleList()
|
||||
@@ -487,8 +540,7 @@ const loadVehicles = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// On met l'user connecté par défaut dans le select
|
||||
const setDefaultUser = () => {
|
||||
function setDefaultUser() {
|
||||
if (form.userId) {
|
||||
return
|
||||
}
|
||||
@@ -497,136 +549,7 @@ const setDefaultUser = () => {
|
||||
}
|
||||
}
|
||||
|
||||
// On récupère toutes les données des selects au chargement du composant
|
||||
onMounted(async () => {
|
||||
await loadTypes()
|
||||
syncMerchandiseFlag()
|
||||
formIsLoading.value =true
|
||||
await loadUsers()
|
||||
await loadSuppliers()
|
||||
await loadTrucks()
|
||||
await loadCarriers()
|
||||
await loadDrivers()
|
||||
await loadVehicles()
|
||||
await authStore.ensureSession()
|
||||
setDefaultUser()
|
||||
})
|
||||
|
||||
// Ajuste driver/vehicle quand le transporteur change (logique LIOT)
|
||||
watch(
|
||||
() => [form.supplierId, form.addressId, suppliers.value],
|
||||
() => {
|
||||
if (!form.supplierId) {
|
||||
form.addressId = ''
|
||||
return
|
||||
}
|
||||
if (!form.addressId && supplierAddresses.value.length === 1) {
|
||||
form.addressId = String(supplierAddresses.value[0].id)
|
||||
return
|
||||
}
|
||||
if (!form.addressId) {
|
||||
return
|
||||
}
|
||||
const matches = supplierAddresses.value.some(
|
||||
(address) => String(address.id) === form.addressId
|
||||
)
|
||||
if (!matches) {
|
||||
if (supplierAddresses.value.length === 1) {
|
||||
form.addressId = String(supplierAddresses.value[0].id)
|
||||
} else {
|
||||
form.addressId = ''
|
||||
}
|
||||
}
|
||||
},
|
||||
{immediate: true}
|
||||
)
|
||||
|
||||
// Valide/auto-sélectionne le véhicule selon camion + transporteur (LIOT)
|
||||
watch(
|
||||
() => form.carrierId,
|
||||
() => {
|
||||
if (isHydrating.value) {
|
||||
return
|
||||
}
|
||||
if (!form.carrierId && idReception == null) {
|
||||
form.driverId = ''
|
||||
form.vehicleId = ''
|
||||
return
|
||||
}
|
||||
if (!isLiotCarrier.value && idReception == null) {
|
||||
form.driverId = ''
|
||||
form.vehicleId = ''
|
||||
return
|
||||
}
|
||||
if (filteredDrivers.value.length === 1) {
|
||||
form.driverId = String(filteredDrivers.value[0].id)
|
||||
}
|
||||
if (filteredVehicles.value.length === 1) {
|
||||
form.vehicleId = String(filteredVehicles.value[0].id)
|
||||
}
|
||||
},
|
||||
{immediate: true}
|
||||
)
|
||||
|
||||
// Récupère la plaque depuis le véhicule choisi (LIOT)
|
||||
watch(
|
||||
() => [form.truckId, form.carrierId, vehicles.value],
|
||||
() => {
|
||||
if (!isLiotCarrier.value) {
|
||||
return
|
||||
}
|
||||
if (filteredVehicles.value.length === 1) {
|
||||
form.vehicleId = String(filteredVehicles.value[0].id)
|
||||
return
|
||||
}
|
||||
if (!form.vehicleId) {
|
||||
return
|
||||
}
|
||||
const matches = filteredVehicles.value.some(
|
||||
(vehicle) => String(vehicle.id) === form.vehicleId
|
||||
)
|
||||
if (!matches) {
|
||||
form.vehicleId = ''
|
||||
}
|
||||
},
|
||||
{immediate: true}
|
||||
)
|
||||
|
||||
// Auto-renseigne le véhicule si la plaque correspond (LIOT)
|
||||
watch(
|
||||
() => [form.vehicleId, form.carrierId, vehicles.value],
|
||||
() => {
|
||||
if (!isLiotCarrier.value) {
|
||||
return
|
||||
}
|
||||
if (isHydrating.value) {
|
||||
return
|
||||
}
|
||||
const selected = filteredVehicles.value.find(
|
||||
(vehicle) => String(vehicle.id) === form.vehicleId
|
||||
)
|
||||
if (selected) {
|
||||
form.licensePlate = selected.plate
|
||||
allowAnyLicensePlate.value = false
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
watch(
|
||||
() => [form.licensePlate, form.carrierId, vehicles.value],
|
||||
() => {
|
||||
if (!isLiotCarrier.value || form.vehicleId) {
|
||||
return
|
||||
}
|
||||
const match = filteredVehicles.value.find(
|
||||
(vehicle) => vehicle.plate === form.licensePlate
|
||||
)
|
||||
if (match) {
|
||||
form.vehicleId = String(match.id)
|
||||
}
|
||||
}
|
||||
)
|
||||
const printReceipt = async () => {
|
||||
async function printReceipt() {
|
||||
if (!import.meta.client || !Number.isFinite(idReception) || idReception <= 0) {
|
||||
return
|
||||
}
|
||||
@@ -641,7 +564,7 @@ const printReceipt = async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 600))
|
||||
}
|
||||
|
||||
const saveWeightEntry = async (entry: WeightEntryData) => {
|
||||
async function saveWeightEntry(entry: WeightEntryData) {
|
||||
if (!idReception || entry.weight === null) {
|
||||
return
|
||||
}
|
||||
@@ -663,6 +586,114 @@ const saveWeightEntry = async (entry: WeightEntryData) => {
|
||||
...payload
|
||||
})
|
||||
}
|
||||
async function saveBovineEntry(entry: ReceptionBovineTypeData) {
|
||||
if (!idReception || !entry.bovineType || entry.quantity === null || entry.quantity <= 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const payload = {
|
||||
quantity: entry.quantity
|
||||
}
|
||||
|
||||
if (entry.id) {
|
||||
await updateReceptionBovine(entry.id, payload)
|
||||
return
|
||||
}
|
||||
|
||||
await createReceptionBovine({
|
||||
reception: `/api/receptions/${idReception}`,
|
||||
bovineType: `/api/bovine_types/${entry.bovineType.id}`,
|
||||
...payload
|
||||
})
|
||||
}
|
||||
|
||||
async function syncBovineEntries() {
|
||||
if (!idReception) {
|
||||
return
|
||||
}
|
||||
|
||||
const receptionIri = `/api/receptions/${idReception}`
|
||||
const existing = await getReceptionBovineList(receptionIri)
|
||||
const currentPositive = bovineEntries.value.filter(
|
||||
(entry) => (entry.quantity ?? 0) > 0
|
||||
)
|
||||
|
||||
for (const existingEntry of existing) {
|
||||
const stillPresent = currentPositive.some(
|
||||
(entry) => entry.bovineType.id === existingEntry.bovineType.id
|
||||
)
|
||||
if (!stillPresent) {
|
||||
await deleteReceptionBovine(existingEntry.id)
|
||||
}
|
||||
}
|
||||
|
||||
for (const entry of currentPositive) {
|
||||
await saveBovineEntry(entry)
|
||||
}
|
||||
}
|
||||
|
||||
async function saveMerchandiseEntry(
|
||||
receptionIri: string,
|
||||
pelletTypeId: string,
|
||||
buildingId: string,
|
||||
existingKeys: Set<string>
|
||||
) {
|
||||
const key = `${pelletTypeId}:${buildingId}`
|
||||
if (existingKeys.has(key)) {
|
||||
return
|
||||
}
|
||||
await createReceptionPelletBuilding({
|
||||
reception: receptionIri,
|
||||
pelletType: `/api/pellet_types/${pelletTypeId}`,
|
||||
building: `/api/buildings/${buildingId}`
|
||||
})
|
||||
}
|
||||
|
||||
async function syncMerchandiseEntries() {
|
||||
if (!idReception) {
|
||||
return
|
||||
}
|
||||
|
||||
const receptionIri = `/api/receptions/${idReception}`
|
||||
const existing = await getReceptionPelletBuildingList(receptionIri)
|
||||
const existingMap = new Map<string, number>()
|
||||
for (const selection of existing) {
|
||||
const pelletTypeId = getRelationId(selection.pelletType)
|
||||
const buildingId = getRelationId(selection.building)
|
||||
if (!pelletTypeId || !buildingId) {
|
||||
continue
|
||||
}
|
||||
existingMap.set(`${pelletTypeId}:${buildingId}`, selection.id)
|
||||
}
|
||||
|
||||
const desiredEntries: Array<{ pelletTypeId: string; buildingId: string }> = []
|
||||
for (const [pelletTypeId, buildingIds] of Object.entries(
|
||||
merchandiseForm.value.selectedPelletBuildingIds
|
||||
)) {
|
||||
for (const buildingId of buildingIds) {
|
||||
desiredEntries.push({ pelletTypeId, buildingId })
|
||||
}
|
||||
}
|
||||
const desiredKeys = new Set(
|
||||
desiredEntries.map((entry) => `${entry.pelletTypeId}:${entry.buildingId}`)
|
||||
)
|
||||
|
||||
for (const [key, selectionId] of existingMap.entries()) {
|
||||
if (!desiredKeys.has(key)) {
|
||||
await deleteReceptionPelletBuilding(selectionId)
|
||||
}
|
||||
}
|
||||
|
||||
const existingKeys = new Set(existingMap.keys())
|
||||
for (const entry of desiredEntries) {
|
||||
await saveMerchandiseEntry(
|
||||
receptionIri,
|
||||
entry.pelletTypeId,
|
||||
entry.buildingId,
|
||||
existingKeys
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
async function validate() {
|
||||
const normalizedLicensePlate = form.licensePlate.trim()
|
||||
@@ -709,32 +740,168 @@ async function validate() {
|
||||
|
||||
const payload = {
|
||||
...basePayload,
|
||||
...(isLiotCarrier.value && driverIri ? {driver: driverIri} : {}),
|
||||
...(isLiotCarrier.value && driverIri ? { driver: driverIri } : {}),
|
||||
}
|
||||
|
||||
if (idReception) {
|
||||
isValid.value = true
|
||||
isMerchandise.value ? (isValidMerchandise.value = true) : (isValidBovin.value = true)
|
||||
await receptionStore.updateReception(idReception, {
|
||||
...payload
|
||||
})
|
||||
await saveWeightEntry(grossWeight.value)
|
||||
await saveWeightEntry(tareWeight.value)
|
||||
const refreshedReception = await getReception(idReception)
|
||||
hydrateFromReception(refreshedReception)
|
||||
|
||||
if (isMerchandise.value) {
|
||||
await clearReceptionBovines(idReception)
|
||||
await updateReception(idReception, {
|
||||
merchandiseType: merchandiseForm.value.merchandiseTypeId
|
||||
? `/api/merchandise_types/${merchandiseForm.value.merchandiseTypeId}`
|
||||
: null,
|
||||
merchandiseDetail: merchandiseForm.value.merchandiseDetail.trim() || null,
|
||||
buildings: merchandiseForm.value.selectedBuildingIds.map(
|
||||
(buildingId) => `/api/buildings/${buildingId}`
|
||||
),
|
||||
bovineDetail: null,
|
||||
bovinesTypes: null
|
||||
})
|
||||
await syncMerchandiseEntries()
|
||||
} else {
|
||||
await clearReceptionMerchandise(idReception)
|
||||
await syncBovineEntries()
|
||||
await updateReception(idReception, {
|
||||
bovineDetail: bovineOtherQuantity.value ? String(bovineOtherQuantity.value) : null
|
||||
})
|
||||
}
|
||||
|
||||
isValid.value = false
|
||||
isValidBovin.value = false
|
||||
isValidMerchandise.value = false
|
||||
const refreshedReception = await getReception(idReception)
|
||||
hydrateFromReception(refreshedReception)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
await loadTypes()
|
||||
syncMerchandiseFlag()
|
||||
formIsLoading.value = true
|
||||
await loadUsers()
|
||||
await loadSuppliers()
|
||||
await loadTrucks()
|
||||
await loadCarriers()
|
||||
await loadDrivers()
|
||||
await loadVehicles()
|
||||
await authStore.ensureSession()
|
||||
setDefaultUser()
|
||||
})
|
||||
|
||||
watch(
|
||||
() => [form.supplierId, form.addressId, suppliers.value],
|
||||
() => {
|
||||
if (!form.supplierId) {
|
||||
form.addressId = ''
|
||||
return
|
||||
}
|
||||
if (!form.addressId && supplierAddresses.value.length === 1) {
|
||||
form.addressId = String(supplierAddresses.value[0].id)
|
||||
return
|
||||
}
|
||||
if (!form.addressId) {
|
||||
return
|
||||
}
|
||||
const matches = supplierAddresses.value.some(
|
||||
(address) => String(address.id) === form.addressId
|
||||
)
|
||||
if (!matches) {
|
||||
if (supplierAddresses.value.length === 1) {
|
||||
form.addressId = String(supplierAddresses.value[0].id)
|
||||
} else {
|
||||
form.addressId = ''
|
||||
}
|
||||
}
|
||||
},
|
||||
{ immediate: true }
|
||||
)
|
||||
|
||||
watch(
|
||||
() => form.carrierId,
|
||||
() => {
|
||||
if (isHydrating.value) {
|
||||
return
|
||||
}
|
||||
if (!form.carrierId && idReception == null) {
|
||||
form.driverId = ''
|
||||
form.vehicleId = ''
|
||||
return
|
||||
}
|
||||
if (!isLiotCarrier.value && idReception == null) {
|
||||
form.driverId = ''
|
||||
form.vehicleId = ''
|
||||
return
|
||||
}
|
||||
if (filteredDrivers.value.length === 1) {
|
||||
form.driverId = String(filteredDrivers.value[0].id)
|
||||
}
|
||||
if (filteredVehicles.value.length === 1) {
|
||||
form.vehicleId = String(filteredVehicles.value[0].id)
|
||||
}
|
||||
},
|
||||
{ immediate: true }
|
||||
)
|
||||
|
||||
watch(
|
||||
() => [form.truckId, form.carrierId, vehicles.value],
|
||||
() => {
|
||||
if (!isLiotCarrier.value) {
|
||||
return
|
||||
}
|
||||
if (filteredVehicles.value.length === 1) {
|
||||
form.vehicleId = String(filteredVehicles.value[0].id)
|
||||
return
|
||||
}
|
||||
if (!form.vehicleId) {
|
||||
return
|
||||
}
|
||||
const matches = filteredVehicles.value.some(
|
||||
(vehicle) => String(vehicle.id) === form.vehicleId
|
||||
)
|
||||
if (!matches) {
|
||||
form.vehicleId = ''
|
||||
}
|
||||
},
|
||||
{ immediate: true }
|
||||
)
|
||||
|
||||
watch(
|
||||
() => [form.vehicleId, form.carrierId, vehicles.value],
|
||||
() => {
|
||||
if (!isLiotCarrier.value) {
|
||||
return
|
||||
}
|
||||
if (isHydrating.value) {
|
||||
return
|
||||
}
|
||||
const selected = filteredVehicles.value.find(
|
||||
(vehicle) => String(vehicle.id) === form.vehicleId
|
||||
)
|
||||
if (selected) {
|
||||
form.licensePlate = selected.plate
|
||||
allowAnyLicensePlate.value = false
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
watch(
|
||||
() => [form.licensePlate, form.carrierId, vehicles.value],
|
||||
() => {
|
||||
if (!isLiotCarrier.value || form.vehicleId) {
|
||||
return
|
||||
}
|
||||
const match = filteredVehicles.value.find(
|
||||
(vehicle) => vehicle.plate === form.licensePlate
|
||||
)
|
||||
if (match) {
|
||||
form.vehicleId = String(match.id)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user