Files
FEDEO/spaces/pages/inventory/spaces/[mode]/[[id]].vue
2023-12-21 16:05:13 +01:00

242 lines
5.2 KiB
Vue

<script setup>
import axios from "axios";
definePageMeta({
middleware: "auth"
})
//
const supabase = useSupabaseClient()
const route = useRoute()
const router = useRouter()
const toast = useToast()
const id = ref(route.params.id ? route.params.id : null )
//Store
const {spaces,movements,products,units,ownTenant} = storeToRefs(useDataStore())
const {fetchSpaces, getSpaceById, movementsBySpace, getProductById} = useDataStore()
let currentItem = null
//Working
const mode = ref(route.params.mode || "show")
const itemInfo = ref({
spaceNumber: 0
})
const spaceTypes = ["Regalplatz", "Kiste", "Palettenplatz"]
const spaceProducts = ref([])
const spaceMovements = ref([])
//Functions
const setupPage = async () => {
if(mode.value === "show" || mode.value === "edit"){
currentItem = await getSpaceById(Number(useRoute().params.id))
console.log(currentItem)
spaceMovements.value = movementsBySpace(currentItem.id)
spaceProducts.value = []
spaceMovements.value.forEach(movement => {
if(spaceProducts.value.filter(product => product.id === movement.productId).length === 0) spaceProducts.value.push(getProductById(movement.productId))
})
}
if(mode.value === "edit") itemInfo.value = currentItem
if(mode.value === "create") {
let lastSpaceNumber = 0
spaces.value.forEach(space => {
if(space.spaceNumber > lastSpaceNumber) lastSpaceNumber = space.spaceNumber
})
itemInfo.value.spaceNumber = lastSpaceNumber + 1
}
}
const createItem = async () => {
const {data,error} = await supabase
.from("spaces")
.insert([itemInfo.value])
.select()
if(error) {
console.log(error)
} else {
mode.value = "show"
itemInfo.value = {}
toast.add({title: "Lagerplatz erfolgreich erstellt"})
await fetchSpaces()
router.push(`/inventory/spaces/show/${data[0].id}`)
setupPage()
}
}
const editItem = async () => {
router.push(`/inventory/spaces/edit/${currentItem.id}`)
setupPage()
}
const cancelEditorCreate = () => {
router.push(`/inventory/spaces/`)
}
const updateItem = async () => {
const {error} = await supabase
.from("spaces")
.update(itemInfo.value)
.eq('id',itemInfo.value.id)
if(error) {
console.log(error)
} else {
mode.value = "show"
itemInfo.value = {}
toast.add({title: "Lagerplatz erfolgreich gespeichert"})
fetchSpaces()
}
}
function getSpaceProductCount(productId) {
let productMovements = spaceMovements.value.filter(movement => movement.productId === productId)
let count = 0;
productMovements.forEach(movement => count += movement.quantity)
return count
}
const printSpaceLabel = async () => {
axios
.post(`http://${ownTenant.value.labelPrinterIp}/pstprnt`, `^XA^FO10,20^BCN,100^FD${currentItem.spaceNumber}^XZ` )
.then(console.log)
.catch(console.log)
}
setupPage()
</script>
<template>
<div>
<UCard v-if="currentItem && mode == 'show'" >
<template #header>
<UBadge>{{currentItem.spaceNumber}}</UBadge> {{currentItem.type}}
</template>
{{currentItem.description}}
<UDivider
class="my-2"
/>
<div v-if="spaceProducts.length > 0">
<p class="mt-5">Artikel in diesem Lagerplatz</p>
<table>
<tr>
<th>Artikel</th>
<th>Anzahl</th>
<th>Einheit</th>
</tr>
<tr v-for="product in spaceProducts">
<td>{{product.name}}</td>
<td>{{getSpaceProductCount(product.id)}}</td>
<td>{{units.find(unit => unit.id === product.unit).name}}</td>
</tr>
</table>
</div>
<p v-else>Es befinden sich keine Artikel in diesem Lagerplatz</p>
<template #footer>
<UButton
v-if="mode == 'show' && currentItem.id"
@click="editItem"
>
Bearbeiten
</UButton>
<UButton
v-if="mode == 'show' && currentItem.id"
@click="printSpaceLabel"
class="ml-2"
>
Label Drucken
</UButton>
<UButton
color="red"
class="ml-2"
disabled
>
Archivieren
</UButton>
</template>
</UCard>
<UCard v-else-if="mode == 'edit' || mode == 'create'" >
<template #header>
<UBadge>{{itemInfo.spaceNumber}}</UBadge>{{itemInfo.type}}
</template>
<UFormGroup
label="Typ:"
>
<USelectMenu
:options="spaceTypes"
v-model="itemInfo.type"
>
</USelectMenu>
</UFormGroup>
<UFormGroup
label="Beschreibung.:"
>
<UTextarea
v-model="itemInfo.description"
/>
</UFormGroup>
<template #footer>
<UButton
v-if="mode == 'edit'"
@click="updateItem"
>
Speichern
</UButton>
<UButton
v-else-if="mode == 'create'"
@click="createItem"
>
Erstellen
</UButton>
<UButton
@click="cancelEditorCreate"
color="red"
class="ml-2"
>
Abbrechen
</UButton>
</template>
</UCard>
</div>
</template>
<style scoped>
</style>