diff --git a/public/js/pages/WarehouseShippingNote/WarehouseShippingNoteModal.js b/public/js/pages/WarehouseShippingNote/WarehouseShippingNoteModal.js index b1efb5bb8..a2057bdd8 100644 --- a/public/js/pages/WarehouseShippingNote/WarehouseShippingNoteModal.js +++ b/public/js/pages/WarehouseShippingNote/WarehouseShippingNoteModal.js @@ -277,20 +277,83 @@ Vue.component('warehouse-shipping-note-modal', { this.loading = false; }, async updateCarId(userId) { - if (!userId) return this.$refs.hoursManager.updateField('carId', null); + if (!userId) { + this.$refs.hoursManager.updateField('carId', null); + this.$refs.hoursManager.updateField('kilometerCount', null); + return; + } this.hoursLoading = true; - const {data} = await axios.get(window.TT_CONFIG["BASE_PATH"] + '/WarehouseShippingNote/timerecordingCarForUser?userId=' + userId); - if (data.status === 'USER_NO_CAR') this.$refs.hoursManager.updateField('carId', null); - else this.$refs.hoursManager.updateField('carId', data.id); - this.hoursLoading = false; + try { + const {data} = await axios.get(window.TT_CONFIG["BASE_PATH"] + '/WarehouseShippingNote/timerecordingCarForUser?userId=' + userId); + if (data.status === 'USER_NO_CAR') { + this.$refs.hoursManager.updateField('carId', null); + this.$refs.hoursManager.updateField('kilometerCount', null); + this.hoursLoading = false; + } else { + this.$refs.hoursManager.updateField('carId', data.id); + // Trigger kilometer calculation after car is set + // Note: updateKilometer will set hoursLoading = false when done + await this.$nextTick(); + await this.updateKilometer(data.id); + } + } catch (error) { + console.error('Error fetching car for user:', error); + window.notify('error', 'Fehler beim Laden des Fahrzeugs'); + this.hoursLoading = false; + } }, - async updateKilometer(carId) { - if (!carId || carId === '0' && this.$refs.hoursManager) return this.$refs.hoursManager?.updateField('kilometerCount', null); + async updateKilometer(carIdParam) { + if (!this.$refs.hoursManager) { + this.hoursLoading = false; + return; + } + + // Get current form data from the hours manager + const currentFormData = this.$refs.hoursManager.formData || {}; + + // Use passed carId, or fall back to current form state + const carId = carIdParam !== undefined ? carIdParam : currentFormData.carId; + const externalCar = currentFormData.externalCar; + + // Check if we have a car selected OR external car is checked + const hasVehicle = (carId && carId !== '0' && carId !== 0) || externalCar; + + if (!hasVehicle) { + this.$refs.hoursManager.updateField('kilometerCount', null); + this.hoursLoading = false; + return; + } + + // Check if delivery address is complete enough for distance calculation + const deliveryAddr = this.shippingNote.deliveryAddressLine?.trim(); + const deliveryPLZ = this.shippingNote.deliveryAddressPLZ?.trim(); + const deliveryCity = this.shippingNote.deliveryAddressCity?.trim(); + + if (!deliveryAddr || !deliveryPLZ || !deliveryCity) { + this.hoursLoading = false; + return; // Don't calculate without complete address + } + this.hoursLoading = true; - const delAddr = this.shippingNote.deliveryAddressLine + ' ' + this.shippingNote.deliveryAddressPLZ + ' ' + this.shippingNote.deliveryAddressCity; - const {data} = await axios.get(window.TT_CONFIG["BASE_PATH"] + '/WarehouseShippingNote/getDistance?from=Xinon%20GmbH&to=' + delAddr); - this.$refs.hoursManager.updateField('kilometerCount', data.distance); - this.hoursLoading = false; + try { + const delAddr = `${deliveryAddr} ${deliveryPLZ} ${deliveryCity}`; + const {data} = await axios.get(window.TT_CONFIG["BASE_PATH"] + '/WarehouseShippingNote/getDistance', { + params: { + from: 'Xinon GmbH', + to: delAddr + } + }); + if (data.success === false) { + window.notify('error', data.message || 'Fehler bei der Kilometerberechnung'); + } else if (data.distance !== undefined) { + this.$refs.hoursManager.updateField('kilometerCount', data.distance); + } + } catch (error) { + console.error('Error calculating distance:', error); + window.notify('error', 'Fehler bei der Kilometerberechnung'); + } finally { + this.hoursLoading = false; + } } } })