63 lines
2.2 KiB
TypeScript
63 lines
2.2 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import type { Category, Order, Product } from '@/main'
|
|
import { ref } from 'vue'
|
|
import { axiosInstance } from '@/main'
|
|
|
|
export const useCategoriesStore = defineStore('categories', () => {
|
|
const categories = ref<Array<Category>>([]);
|
|
|
|
async function fetchCategories() {
|
|
const response = await axiosInstance.get('/towary', {withCredentials: true});
|
|
categories.value = response.data;
|
|
|
|
if(categories.value != undefined) {
|
|
for (const category of categories.value) {
|
|
for (const product of category.Towary) {
|
|
product.Options = new Array(product.Twr_JM);
|
|
product.ChosenOption = product.Twr_JM;
|
|
product.BasePrice = product.Twr_Cena;
|
|
product.BasePriceZ = product.Twr_CenaZ;
|
|
product.SummedQuantity = 0;
|
|
product.SummedPrice = 0;
|
|
if (product.Twr_JMZ != null) {
|
|
product.Options.push(product.Twr_JMZ);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async function sumProductsFromOrders(orders : Array<Order>) {
|
|
const productsMap = new Map<string, Product>();
|
|
|
|
await fetchCategories();
|
|
|
|
if(categories.value != undefined) {
|
|
for (const category of categories.value) {
|
|
for (const product of category.Towary) {
|
|
productsMap.set(product.Twr_TwrId.toString(), product);
|
|
}
|
|
}
|
|
}
|
|
|
|
if(orders != undefined) {
|
|
for (const order of orders) {
|
|
for (const product of order.MZamElem) {
|
|
const mapProduct = productsMap.get(String(product.MZE_TwrId));
|
|
if (product.MZE_TwrJm == mapProduct?.Twr_JM) {
|
|
mapProduct.SummedQuantity += Number(product.MZE_TwrIlosc);
|
|
mapProduct.SummedPrice += (Number(product.MZE_TwrCena) * Number(product.MZE_TwrIlosc));
|
|
}
|
|
else if (product.MZE_TwrJm == mapProduct?.Twr_JMZ) {
|
|
mapProduct.SummedQuantity += (Number(product.MZE_TwrIlosc) * Number(mapProduct.Twr_JMPrzelicznikL)/Number(mapProduct.Twr_JMPrzelicznikM));
|
|
|
|
mapProduct.SummedPrice += (Number(product.MZE_TwrCena) * Number(product.MZE_TwrIlosc));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return productsMap;
|
|
}
|
|
|
|
return {categories, fetchCategories, sumProductsFromOrders}
|
|
}) |