Add vehicle management features in Falukant
- Introduced vehicle types and transport management in the backend, including new models and associations for vehicles and transports. - Implemented service methods to retrieve vehicle types and handle vehicle purchases, ensuring user validation and transaction management. - Updated the FalukantController and router to expose new endpoints for fetching vehicle types and buying vehicles. - Enhanced the frontend with a new transport tab in BranchView, allowing users to buy vehicles, and added localization for vehicle-related terms. - Included initialization logic for vehicle types in the database setup.
This commit is contained in:
191
frontend/src/dialogues/falukant/BuyVehicleDialog.vue
Normal file
191
frontend/src/dialogues/falukant/BuyVehicleDialog.vue
Normal file
@@ -0,0 +1,191 @@
|
||||
<template>
|
||||
<DialogWidget
|
||||
ref="dialog"
|
||||
name="buy-vehicle"
|
||||
:title="$t('falukant.branch.transport.title')"
|
||||
icon="carriage.png"
|
||||
showClose
|
||||
:buttons="dialogButtons"
|
||||
@close="onClose"
|
||||
>
|
||||
<div class="buy-vehicle-form" v-if="loaded">
|
||||
<p class="hint">
|
||||
{{ $t('falukant.branch.transport.balance') }}:
|
||||
<strong>{{ formattedMoney }}</strong>
|
||||
</p>
|
||||
|
||||
<label class="form-label">
|
||||
{{ $t('falukant.branch.transport.vehicleType') }}
|
||||
<select v-model.number="selectedTypeId" class="form-control">
|
||||
<option v-for="t in vehicleTypes" :key="t.id" :value="t.id">
|
||||
{{ $t(`falukant.branch.vehicles.${t.tr}`) }}
|
||||
({{ formatCost(t.cost) }}, C={{ t.capacity }})
|
||||
</option>
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<label class="form-label">
|
||||
{{ $t('falukant.branch.transport.quantity') }}
|
||||
<input
|
||||
type="number"
|
||||
class="form-control"
|
||||
v-model.number="quantity"
|
||||
min="1"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<p class="total">
|
||||
{{ $t('falukant.branch.transport.totalCost') }}:
|
||||
<strong>{{ formatCost(totalCost) }}</strong>
|
||||
</p>
|
||||
|
||||
<p v-if="totalCost > money" class="warning">
|
||||
{{ $t('falukant.branch.transport.notEnoughMoney') }}
|
||||
</p>
|
||||
</div>
|
||||
<div v-else class="loading">
|
||||
{{ $t('loading') }}
|
||||
</div>
|
||||
</DialogWidget>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import DialogWidget from '@/components/DialogWidget.vue';
|
||||
import apiClient from '@/utils/axios.js';
|
||||
|
||||
export default {
|
||||
name: 'BuyVehicleDialog',
|
||||
components: { DialogWidget },
|
||||
props: {
|
||||
regionId: { type: Number, required: true },
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
vehicleTypes: [],
|
||||
selectedTypeId: null,
|
||||
quantity: 1,
|
||||
money: 0,
|
||||
loaded: false,
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
dialogButtons() {
|
||||
return [
|
||||
{ text: this.$t('Cancel'), action: this.close },
|
||||
{
|
||||
text: this.$t('falukant.branch.transport.buy'),
|
||||
action: this.onConfirm,
|
||||
disabled: !this.canBuy,
|
||||
},
|
||||
];
|
||||
},
|
||||
selectedType() {
|
||||
return this.vehicleTypes.find((t) => t.id === this.selectedTypeId) || null;
|
||||
},
|
||||
totalCost() {
|
||||
if (!this.selectedType) return 0;
|
||||
const q = Math.max(1, this.quantity || 0);
|
||||
return this.selectedType.cost * q;
|
||||
},
|
||||
canBuy() {
|
||||
return (
|
||||
this.loaded &&
|
||||
this.selectedType &&
|
||||
this.quantity >= 1 &&
|
||||
this.totalCost > 0 &&
|
||||
this.totalCost <= this.money
|
||||
);
|
||||
},
|
||||
formattedMoney() {
|
||||
return this.formatCost(this.money);
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
async open() {
|
||||
this.loaded = false;
|
||||
this.quantity = 1;
|
||||
await Promise.all([this.loadVehicleTypes(), this.loadMoney()]);
|
||||
if (this.vehicleTypes.length && !this.selectedTypeId) {
|
||||
this.selectedTypeId = this.vehicleTypes[0].id;
|
||||
}
|
||||
this.loaded = true;
|
||||
this.$refs.dialog.open();
|
||||
},
|
||||
close() {
|
||||
this.$refs.dialog.close();
|
||||
},
|
||||
onClose() {
|
||||
this.close();
|
||||
this.$emit('close');
|
||||
},
|
||||
async loadVehicleTypes() {
|
||||
const { data } = await apiClient.get('/api/falukant/vehicles/types');
|
||||
this.vehicleTypes = data;
|
||||
},
|
||||
async loadMoney() {
|
||||
const { data } = await apiClient.get('/api/falukant/info');
|
||||
this.money = Number(data.money) || 0;
|
||||
},
|
||||
formatCost(value) {
|
||||
return new Intl.NumberFormat(navigator.language, {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
}).format(value);
|
||||
},
|
||||
async onConfirm() {
|
||||
if (!this.canBuy) return;
|
||||
try {
|
||||
await apiClient.post('/api/falukant/vehicles', {
|
||||
vehicleTypeId: this.selectedTypeId,
|
||||
quantity: this.quantity,
|
||||
regionId: this.regionId,
|
||||
});
|
||||
this.$emit('bought');
|
||||
this.close();
|
||||
} catch (err) {
|
||||
console.error('Error buying vehicles', err);
|
||||
this.$emit('error', err);
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.buy-vehicle-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.form-label {
|
||||
display: block;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.form-control {
|
||||
width: 100%;
|
||||
padding: 0.3rem;
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
.hint {
|
||||
margin: 0 0 0.5rem 0;
|
||||
}
|
||||
|
||||
.total {
|
||||
margin-top: 0.5rem;
|
||||
}
|
||||
|
||||
.warning {
|
||||
color: #c62828;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.loading {
|
||||
text-align: center;
|
||||
padding: 1rem;
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user