draft acquisition

This commit is contained in:
2024-05-14 21:21:17 +02:00
parent 9fb78329cc
commit 7332e145f4
15 changed files with 146 additions and 135 deletions

View File

@@ -0,0 +1,8 @@
import { MarketType } from "..";
import { AcquiredMarketItem } from "./acquisition";
export type AcquiredItem = Omit<AcquiredMarketItem, 'type'> & {
type: MarketType,
buy: number,
sell: number
}

View File

@@ -5,8 +5,8 @@ import { MarketType, MarketTypeLabel, TaxInput, useMarketTaxStore } from "@/mark
import { MinusIcon, PlusIcon } from '@heroicons/vue/24/outline'; import { MinusIcon, PlusIcon } from '@heroicons/vue/24/outline';
import { useStorage } from '@vueuse/core'; import { useStorage } from '@vueuse/core';
import { computed, ref } from 'vue'; import { computed, ref } from 'vue';
import { TrackedItem } from '.'; import { AcquiredItem } from './AcquiredItem';
import TrackQuantilsTooltip from './TrackQuantilsTooltip.vue'; import AcquisitionQuantilsTooltip from './AcquisitionQuantilsTooltip.vue';
type Result = { type Result = {
type: MarketType; type: MarketType;
@@ -21,7 +21,7 @@ type Result = {
} }
interface Props { interface Props {
items?: TrackedItem[]; items?: AcquiredItem[];
} }
interface Emits { interface Emits {
@@ -36,12 +36,12 @@ defineEmits<Emits>();
const marketTaxStore = useMarketTaxStore(); const marketTaxStore = useMarketTaxStore();
const threshold = useStorage('market-track-threshold', 10); const threshold = useStorage('market-axquisition-threshold', 10);
const filter = ref(""); const filter = ref("");
const { sortedArray, headerProps } = useSort<Result>(computed(() => props.items const { sortedArray, headerProps } = useSort<Result>(computed(() => props.items
.filter(r => r.type.name.toLowerCase().includes(filter.value.toLowerCase())) .filter(r => r.type.name.toLowerCase().includes(filter.value.toLowerCase()))
.map(r => { .map(r => {
const precentProfit = marketTaxStore.calculateProfit(r.averagePrice, r.sell); const precentProfit = marketTaxStore.calculateProfit(r.price, r.sell);
return { return {
type: r.type, type: r.type,
@@ -49,10 +49,10 @@ const { sortedArray, headerProps } = useSort<Result>(computed(() => props.items
name: r.type.name, name: r.type.name,
buy: r.buy, buy: r.buy,
sell: r.sell, sell: r.sell,
price: r.averagePrice, price: r.price,
count: r.count, count: r.remaining,
precentProfit, precentProfit,
iskProfit: r.averagePrice * precentProfit * r.count iskProfit: r.price * precentProfit * r.remaining
}; };
})), { })), {
defaultSortKey: 'precentProfit', defaultSortKey: 'precentProfit',
@@ -100,7 +100,7 @@ const getLineColor = (result: Result) => {
<td> <td>
<div class="flex"> <div class="flex">
<MarketTypeLabel :id="r.typeID" :name="r.name" /> <MarketTypeLabel :id="r.typeID" :name="r.name" />
<TrackQuantilsTooltip :id="r.typeID" :buy="r.buy" :sell="r.sell" /> <AcquisitionQuantilsTooltip :id="r.typeID" :buy="r.buy" :sell="r.sell" />
</div> </div>
</td> </td>
<td class="text-right">{{ formatIsk(r.buy) }}</td> <td class="text-right">{{ formatIsk(r.buy) }}</td>

View File

@@ -3,10 +3,10 @@ import { Modal } from '@/components';
import { formatIsk } from '@/formaters'; import { formatIsk } from '@/formaters';
import { MarketType, MarketTypeLabel } from '@/market'; import { MarketType, MarketTypeLabel } from '@/market';
import { ref } from 'vue'; import { ref } from 'vue';
import { useTrackedItemStore } from './track'; import { useAcquiredItemStore } from './acquisition';
const trackedItemStore = useTrackedItemStore(); const acquiredItemStore = useAcquiredItemStore();
const modalOpen = ref<boolean>(false); const modalOpen = ref<boolean>(false);
const type = ref<MarketType>(); const type = ref<MarketType>();
@@ -38,7 +38,7 @@ const add = () => {
return; return;
} }
trackedItemStore.addTrackedItem(id, count.value, price.value); acquiredItemStore.addAcquiredItem(id, count.value, price.value);
modalOpen.value = false; modalOpen.value = false;
} }
@@ -66,4 +66,4 @@ defineExpose({ open });
</div> </div>
</div> </div>
</Modal> </Modal>
</template> </template>./acquisitions

View File

@@ -2,10 +2,10 @@
import { Modal } from '@/components'; import { Modal } from '@/components';
import { MarketType, MarketTypeLabel } from '@/market'; import { MarketType, MarketTypeLabel } from '@/market';
import { ref } from 'vue'; import { ref } from 'vue';
import { useTrackedItemStore } from './track'; import { useAcquiredItemStore } from './acquisition';
const trackedItemStore = useTrackedItemStore(); const acquiredItemStore = useAcquiredItemStore();
const modalOpen = ref<boolean>(false); const modalOpen = ref<boolean>(false);
const type = ref<MarketType>(); const type = ref<MarketType>();
@@ -24,7 +24,7 @@ const remove = () => {
return; return;
} }
trackedItemStore.removeTrackedItem(id, count.value); acquiredItemStore.removeAcquiredItem(id, count.value);
modalOpen.value = false; modalOpen.value = false;
} }
@@ -45,4 +45,4 @@ defineExpose({ open });
</div> </div>
</div> </div>
</Modal> </Modal>
</template> </template>./acquisitions

View File

@@ -0,0 +1,65 @@
import { marbasAxiosInstance } from "@/service";
import { defineStore } from "pinia";
import { computed, onMounted, ref } from "vue";
export type AcquiredMarketItem = {
id: number;
type: number;
quantity: number;
remaining: number;
price: number;
date: Date;
source: 'bo' | 'so' | 'prod';
user: number;
}
const endpoint = '/api/acquisitions';
export const useAcquiredItemStore = defineStore('market-acquisition', () => {
const _acquiredItems = ref<AcquiredMarketItem[]>([]);
const items = computed(() => _acquiredItems.value);
const addAcquiredItem = async (type: number, quantity: number, price: number) => {
_acquiredItems.value = [..._acquiredItems.value, (await marbasAxiosInstance.post<AcquiredMarketItem>(endpoint, {
type: type,
quantity: quantity,
remaining: quantity,
price: price,
date: new Date(),
source: 'bo',
user: 0 // TODO: get user id
})).data];
};
const removeAcquiredItem = async (type: number, quantity: number) => {
const found = _acquiredItems.value.find(item => item.type === type);
if (!found) {
return;
}
if (found.remaining <= 0) {
_acquiredItems.value = _acquiredItems.value.filter(i => i.type !== type);
await marbasAxiosInstance.delete(`${endpoint}/${found.id}`);
} else {
const item = {
...found,
remaining: found.remaining - quantity
};
_acquiredItems.value = _acquiredItems.value.map(i => {
if (i.type === item.type) {
return item;
} else {
return i;
}
});
await marbasAxiosInstance.put(`${endpoint}/${item.id}`, item);
}
};
onMounted(async () => {
_acquiredItems.value = (await marbasAxiosInstance.get<AcquiredMarketItem[]>(endpoint)).data;
});
return { items, addAcquiredItem, removeAcquiredItem };
});

View File

@@ -0,0 +1,7 @@
export * from './AcquiredItem';
export * from './acquisition';
export { default as AcquisitionResultTable } from './AcquisitionResultTable.vue';
export { default as BuyModal } from './BuyModal.vue';
export { default as SellModal } from './SellModal.vue';

View File

@@ -1,9 +0,0 @@
import { MarketType } from "@/market";
export type TrackedItem = {
type: MarketType;
count: number;
averagePrice: number;
buy: number,
sell: number
}

View File

@@ -1,7 +0,0 @@
export * from './TrackedItem';
export * from './track';
export { default as BuyModal } from './BuyModal.vue';
export { default as SellModal } from './SellModal.vue';
export { default as TrackResultTable } from './TrackResultTable.vue';

View File

@@ -1,58 +0,0 @@
import { useCollection, usePocketBase } from "@/pocketbase";
import { createSharedComposable, useLocalStorage } from '@vueuse/core';
import { defineStore } from "pinia";
import { RecordModel } from "pocketbase";
import { computed } from "vue";
export type TrackedMarketItemStorage = {
typeID: number;
count: number;
averagePrice: number;
}
interface TrackedMarketItem extends RecordModel {
owner: string;
typeID: number;
count: number;
averagePrice: number;
}
const marketTrackings = 'marketTrackings';
export const useTrackedItemsStorage = createSharedComposable(() => useLocalStorage<TrackedMarketItemStorage[]>('market-track-items', []));
export const useTrackedItemStore = defineStore(marketTrackings, () => {
const pb = usePocketBase();
const trackedItems = useCollection<TrackedMarketItem>(marketTrackings);
const items = computed(() => trackedItems);
const addTrackedItem = async (typeID: number, count: number, averagePrice: number) => {
const oldItem = trackedItems.value.find(i => i.typeID === typeID);
if (oldItem?.id) {
pb.collection(marketTrackings).update(oldItem.id, {
...oldItem,
count: count + oldItem.count,
averagePrice: ((averagePrice * count) + (oldItem.averagePrice * oldItem.count)) / (count + oldItem.count)
});
} else {
pb.collection(marketTrackings).create({ owner: pb.authStore.model!.id, typeID, count, averagePrice});
}
};
const removeTrackedItem = async (typeID: number, count: number) => {
const oldItem = trackedItems.value.find(i => i.typeID === typeID);
if (!oldItem?.id) {
return;
} else if (oldItem.count > count) {
pb.collection(marketTrackings).update(oldItem.id, {
...oldItem,
count: oldItem.count - count
});
} else {
pb.collection(marketTrackings).delete(oldItem.id);
}
};
return { items, addTrackedItem, removeTrackedItem };
});

View File

@@ -12,8 +12,8 @@ import { RouterLink, RouterView } from 'vue-router';
<RouterLink to="/market/scan" class="tab"> <RouterLink to="/market/scan" class="tab">
<span>Scan</span> <span>Scan</span>
</RouterLink> </RouterLink>
<RouterLink to="/market/track" class="tab"> <RouterLink to="/market/acquisition" class="tab">
<span>Tracking</span> <span>Acquisitions</span>
</RouterLink> </RouterLink>
</div> </div>
<RouterView /> <RouterView />

View File

@@ -0,0 +1,43 @@
<script setup lang="ts">
import { MarketTypePrice, getMarketTypes, useApraisalStore } from "@/market";
import { AcquiredItem, AcquisitionResultTable, BuyModal, SellModal, useAcquiredItemStore } from '@/market/acquisition';
import { ref, watch } from 'vue';
const buyModal = ref<typeof BuyModal>();
const sellModal = ref<typeof SellModal>();
const apraisalStore = useApraisalStore();
const acquiredItemStore = useAcquiredItemStore();
const items = ref<AcquiredItem[]>([]);
watch(() => acquiredItemStore.items, async itms => {
if (itms.length === 0) {
return;
}
const prices = await apraisalStore.getPrices(await getMarketTypes(itms.map(i => i.type)));
items.value = itms.map(i => {
const price = prices.find(p => p.type.id === i.type) as MarketTypePrice;
return {
...i,
type: price.type,
buy: price.buy,
sell: price.sell
};
});
}, { immediate: true })
</script>
<template>
<div class="mt-4">
<template v-if="items.length > 0">
<AcquisitionResultTable :items="items" @buy="(type, price, buy, sell) => buyModal?.open(type, { 'Price': price, 'Buy': buy, 'Sell': sell })" @sell="type => sellModal?.open(type)" />
<BuyModal ref="buyModal" />
<SellModal ref="sellModal" />
</template>
</div>
</template>@/market/acquisition

View File

@@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { MarketType, MarketTypeInput, MarketTypePrice, getHistory, getMarketTypes, jitaId, useApraisalStore } from "@/market"; import { MarketType, MarketTypeInput, MarketTypePrice, getHistory, getMarketTypes, jitaId, useApraisalStore } from "@/market";
import { ScanResult, ScanResultTable, createResult, useMarketScanStore } from '@/market/scan'; import { BuyModal } from '@/market/acquisition';
import { BuyModal } from '@/market/track'; import { ScanResult, ScanResultTable, useMarketScanStore } from '@/market/scan';
import { ref, watch } from 'vue'; import { ref, watch } from 'vue';
@@ -73,4 +73,4 @@ watch(() => markeyScanStore.types, async t => {
<ScanResultTable :items="items" @buy="(type, buy, sell) => buyModal?.open(type, { 'Buy': buy, 'Sell': sell })" @remove="removeItem" /> <ScanResultTable :items="items" @buy="(type, buy, sell) => buyModal?.open(type, { 'Buy': buy, 'Sell': sell })" @remove="removeItem" />
<BuyModal ref="buyModal" /> <BuyModal ref="buyModal" />
</template> </template>
</template> </template>@/market/acquisition

View File

@@ -1,38 +0,0 @@
<script setup lang="ts">
import { MarketTypePrice, getMarketTypes, useApraisalStore } from "@/market";
import { BuyModal, SellModal, TrackResultTable, TrackedItem, useTrackedItemStore } from '@/market/track';
import { ref, watch } from 'vue';
const buyModal = ref<typeof BuyModal>();
const sellModal = ref<typeof SellModal>();
const apraisalStore = useApraisalStore();
const trackedItemStore = useTrackedItemStore();
const items = ref<TrackedItem[]>([]);
watch(() => trackedItemStore.items.value, async itms => {
if (itms.length === 0) {
return;
}
const prices = await apraisalStore.getPrices(await getMarketTypes(itms.map(i => i.typeID)));
items.value = itms.map(i => {
const price = prices.find(p => p.type.id === i.typeID) as MarketTypePrice;
return { ...i, ...price };
});
}, { immediate: true })
</script>
<template>
<div class="mt-4">
<template v-if="items.length > 0">
<TrackResultTable :items="items" @buy="(type, price, buy, sell) => buyModal?.open(type, { 'Price': price, 'Buy': buy, 'Sell': sell })" @sell="type => sellModal?.open(type)" />
<BuyModal ref="buyModal" />
<SellModal ref="sellModal" />
</template>
</div>
</template>

View File

@@ -8,7 +8,7 @@ export const routes: RouteRecordRaw[] = [
{ path: '', redirect: '/market/type' }, { path: '', redirect: '/market/type' },
{ path: 'type/:type?', name: 'market-type', component: () => import('@/pages/market/TypeInfo.vue') }, { path: 'type/:type?', name: 'market-type', component: () => import('@/pages/market/TypeInfo.vue') },
{ path: 'scan', component: () => import('@/pages/market/Scan.vue') }, { path: 'scan', component: () => import('@/pages/market/Scan.vue') },
{ path: 'track', component: () => import('@/pages/market/Track.vue') }, { path: 'acquisitions', component: () => import('@/pages/market/Acquisitions.vue') },
] }, ] },
{ path: '/tools', component: () => import('@/pages/Tools.vue') }, { path: '/tools', component: () => import('@/pages/Tools.vue') },
{ path: '/about', component: () => import('@/pages/About.vue') }, { path: '/about', component: () => import('@/pages/About.vue') },