@7365admin1/layer-common 4.17.1-staging.330 → 4.17.1-staging.331

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,288 @@
1
+ <template>
2
+ <v-dialog v-model="show" max-width="760" persistent scrollable>
3
+ <v-card class="screen-modal">
4
+ <v-card-title class="d-flex align-center">
5
+ {{ copy.title }}
6
+ <v-spacer />
7
+ <v-btn icon="mdi-close" variant="text" class="screen-modal__close" aria-label="Close" @click="show = false" />
8
+ </v-card-title>
9
+
10
+ <v-card-text class="screen-modal__body">
11
+ <p class="text-body-2 text-medium-emphasis mb-4">{{ copy.help }}</p>
12
+ <v-form ref="form">
13
+ <template v-if="mode === 'issue' || mode === 'return'">
14
+ <InputLabel :title="mode === 'issue' ? 'Give to' : 'Returned by'" required class="mb-1" />
15
+ <v-autocomplete
16
+ v-if="mode === 'issue'"
17
+ v-model="holder"
18
+ :items="people"
19
+ item-title="name"
20
+ item-value="_id"
21
+ :loading="peopleLoading"
22
+ placeholder="Search a name"
23
+ density="comfortable"
24
+ :rules="[requiredRule]"
25
+ no-filter
26
+ @update:search="searchPeople"
27
+ >
28
+ <template #item="{ props: p, item }">
29
+ <v-list-item v-bind="p" :subtitle="item.raw.orgName" />
30
+ </template>
31
+ </v-autocomplete>
32
+ <v-text-field v-else :model-value="holderName" density="comfortable" readonly />
33
+ </template>
34
+
35
+ <div class="text-subtitle-2 mb-2">Items</div>
36
+ <div v-for="(line, i) in rows" :key="i" class="d-flex flex-wrap ga-2 align-start mb-1">
37
+ <v-autocomplete
38
+ v-model="line.item"
39
+ :items="choices"
40
+ item-title="name"
41
+ item-value="_id"
42
+ placeholder="Choose item"
43
+ density="comfortable"
44
+ :rules="[requiredRule]"
45
+ class="flex-grow-1"
46
+ style="min-width: 240px"
47
+ @update:search="(q: string) => mode !== 'return' && searchItems(q)"
48
+ >
49
+ <template #item="{ props: p, item }">
50
+ <v-list-item v-bind="p" :subtitle="availableText(item.raw)" />
51
+ </template>
52
+ </v-autocomplete>
53
+ <v-text-field
54
+ v-model.number="line.qty"
55
+ type="number"
56
+ min="1"
57
+ label="Qty"
58
+ density="comfortable"
59
+ style="max-width: 110px"
60
+ :rules="[qtyRule(line)]"
61
+ :suffix="unitOf(line.item)"
62
+ />
63
+ <v-text-field
64
+ v-if="mode === 'receive'"
65
+ v-model.number="line.unitCost"
66
+ type="number"
67
+ min="0"
68
+ step="0.01"
69
+ label="Unit price"
70
+ prefix="$"
71
+ density="comfortable"
72
+ style="max-width: 140px"
73
+ hint="Leave blank to keep the last price"
74
+ persistent-hint
75
+ />
76
+ <v-btn v-if="rows.length > 1" icon="mdi-delete-outline" variant="text" aria-label="Remove line" @click="rows.splice(i, 1)" />
77
+ </div>
78
+ <v-btn variant="text" class="text-none mb-4" prepend-icon="mdi-plus" @click="rows.push(blank())">Add another item</v-btn>
79
+
80
+ <v-row v-if="mode === 'receive'" dense>
81
+ <v-col cols="12" md="6">
82
+ <InputLabel title="Supplier" class="mb-1" />
83
+ <v-text-field v-model="supplier" density="comfortable" placeholder="e.g. GSK Hardware Pte Ltd" />
84
+ </v-col>
85
+ <v-col cols="12" md="3">
86
+ <InputLabel title="PO number" class="mb-1" />
87
+ <v-text-field v-model="reference" density="comfortable" />
88
+ </v-col>
89
+ <v-col cols="12" md="3">
90
+ <InputLabel title="Date received" class="mb-1" />
91
+ <v-text-field v-model="date" type="date" :max="today" density="comfortable" />
92
+ </v-col>
93
+ <v-col cols="12">
94
+ <InputLabel title="Delivery photo (optional)" class="mb-1" />
95
+ <InventoryPhotoInput v-model="photos" />
96
+ </v-col>
97
+ </v-row>
98
+
99
+ <template v-if="mode === 'request' || mode === 'issue'">
100
+ <v-select
101
+ v-if="mode === 'issue' && siteChoices.length > 1"
102
+ v-model="site"
103
+ :items="siteChoices"
104
+ item-title="name"
105
+ item-value="_id"
106
+ label="For which property"
107
+ density="comfortable"
108
+ />
109
+ <InputLabel title="Work order / reference no. (optional)" class="mb-1" />
110
+ <v-text-field v-model="reference" density="comfortable" placeholder="e.g. EE202507-0002" />
111
+ </template>
112
+
113
+ <InputLabel title="Remarks (optional)" class="mb-1 mt-2" />
114
+ <v-textarea v-model="remarks" rows="2" auto-grow density="comfortable" />
115
+ </v-form>
116
+ </v-card-text>
117
+
118
+ <div class="screen-modal__footer">
119
+ <v-btn class="screen-btn-ghost" variant="outlined" :disabled="saving" @click="show = false">Cancel</v-btn>
120
+ <v-btn class="screen-btn-primary" variant="flat" :loading="saving" @click="submit">{{ copy.button }}</v-btn>
121
+ </div>
122
+ </v-card>
123
+ </v-dialog>
124
+ </template>
125
+
126
+ <script setup lang="ts">
127
+ import useInventory, { pickedDate } from "../composables/useInventory";
128
+ import useUtils from "../composables/useUtils";
129
+
130
+ type TMode = "receive" | "issue" | "request" | "return";
131
+ type TRow = { item: string; qty: number | null; unitCost?: number | null };
132
+
133
+ const show = defineModel<boolean>({ default: false });
134
+ const props = defineProps({
135
+ mode: { type: String as PropType<TMode>, required: true },
136
+ store: { type: Object as PropType<TInventoryStore>, required: true },
137
+ siteId: { type: String, default: "" },
138
+ holderId: { type: String, default: "" },
139
+ holderName: { type: String, default: "" },
140
+ holdings: { type: Array as PropType<TInventoryHolding[]>, default: () => [] },
141
+ sites: { type: Array as PropType<{ _id: string; name: string }[]>, default: () => [] },
142
+ });
143
+ const emit = defineEmits(["done"]);
144
+
145
+ const api = useInventory();
146
+ const { requiredRule, debounce } = useUtils();
147
+ const notify = inject<(text: string, color?: string) => void>("inventoryNotify", () => {});
148
+
149
+ const COPY: Record<TMode, { title: string; help: string; button: string; success: string }> = {
150
+ receive: {
151
+ title: "Receive Stock",
152
+ help: "Record a delivery from a supplier. The quantities are added to the store straight away.",
153
+ button: "Add to store",
154
+ success: "Stock received.",
155
+ },
156
+ issue: {
157
+ title: "Issue Stock",
158
+ help: "Hand stock to someone at the store counter. It leaves the store and is counted as in their hands.",
159
+ button: "Issue stock",
160
+ success: "Stock issued.",
161
+ },
162
+ request: {
163
+ title: "Request Stock",
164
+ help: "Ask the store for the items you need. You will see them under My Stock once the store keeper approves.",
165
+ button: "Send request",
166
+ success: "Request sent to the store.",
167
+ },
168
+ return: {
169
+ title: "Receive Return",
170
+ help: "Take unused stock back into the store.",
171
+ button: "Return to store",
172
+ success: "Stock returned to the store.",
173
+ },
174
+ };
175
+ const copy = computed(() => COPY[props.mode]);
176
+
177
+ const form = ref();
178
+ const saving = ref(false);
179
+ const blank = (): TRow => ({ item: "", qty: null, unitCost: null });
180
+ const rows = ref<TRow[]>([blank()]);
181
+ const supplier = ref("");
182
+ const reference = ref("");
183
+ const remarks = ref("");
184
+ const photos = ref<string[]>([]);
185
+ const today = new Date().toLocaleDateString("en-CA");
186
+ const date = ref(today);
187
+ const holder = ref("");
188
+ const site = ref(props.siteId);
189
+
190
+ const itemChoices = ref<TInventoryItem[]>([]);
191
+ const choices = computed<any[]>(() =>
192
+ props.mode === "return"
193
+ ? props.holdings.map((h) => ({ _id: h.item, name: h.itemName, unit: h.unit, onHand: h.qty }))
194
+ : itemChoices.value,
195
+ );
196
+ const siteChoices = computed(() => (props.sites.length ? props.sites : props.store.sites.map((_id) => ({ _id, name: _id === props.siteId ? "This property" : "Other property" }))));
197
+
198
+ const unitOf = (id: string) => choices.value.find((c) => c._id === id)?.unit ?? "";
199
+ const availableOf = (id: string) => choices.value.find((c) => c._id === id)?.onHand;
200
+
201
+ function availableText(item: any) {
202
+ if (props.mode === "return") return `In hand: ${item.onHand} ${item.unit}`;
203
+ if (props.mode === "receive") return `In store: ${item.onHand} ${item.unit}`;
204
+ return `Available: ${item.onHand} ${item.unit}`;
205
+ }
206
+
207
+ function qtyRule(line: TRow) {
208
+ return (v: number | null) => {
209
+ if (!v || v < 1 || !Number.isInteger(Number(v))) return "Enter a whole number";
210
+ if (props.mode === "receive") return true;
211
+ const available = availableOf(line.item);
212
+ const total = rows.value.filter((r) => r.item === line.item).reduce((s, r) => s + Number(r.qty || 0), 0);
213
+ if (props.mode !== "request" && available !== undefined && total > available) return `Only ${available} available`;
214
+ return true;
215
+ };
216
+ }
217
+
218
+ async function loadItems(search = "") {
219
+ try {
220
+ const res = await api.getItems(props.store._id, { search, limit: 50 });
221
+ const selected = itemChoices.value.filter((c) => rows.value.some((r) => r.item === c._id));
222
+ const merged = [...selected, ...res.items.filter((i) => !selected.some((s) => s._id === i._id))];
223
+ itemChoices.value = merged;
224
+ } catch {
225
+ /* keep current list */
226
+ }
227
+ }
228
+ const searchItems = debounce((q: string) => loadItems(q || ""), 300);
229
+
230
+ const people = ref<{ _id: string; name: string; orgName: string }[]>([]);
231
+ const peopleLoading = ref(false);
232
+ const searchPeople = debounce(async (q: string) => {
233
+ peopleLoading.value = true;
234
+ try {
235
+ people.value = await api.getPeople(props.store._id, q || "");
236
+ } catch {
237
+ people.value = [];
238
+ } finally {
239
+ peopleLoading.value = false;
240
+ }
241
+ }, 300);
242
+
243
+ async function submit() {
244
+ const { valid } = await form.value.validate();
245
+ if (!valid) return;
246
+ const lines = rows.value.filter((r) => r.item && r.qty).map((r) => ({ item: r.item, qty: Number(r.qty) }));
247
+ saving.value = true;
248
+ try {
249
+ if (props.mode === "receive") {
250
+ await api.receive(props.store._id, {
251
+ lines: rows.value.map((r) => ({ item: r.item, qty: Number(r.qty), ...(r.unitCost !== null && r.unitCost !== undefined && r.unitCost !== ("" as any) ? { unitCost: Number(r.unitCost) } : {}) })),
252
+ supplier: supplier.value,
253
+ reference: reference.value,
254
+ date: pickedDate(date.value),
255
+ photos: photos.value,
256
+ remarks: remarks.value,
257
+ });
258
+ } else if (props.mode === "request") {
259
+ await api.createRequest(props.store._id, { site: props.siteId, lines, reference: reference.value, remarks: remarks.value });
260
+ } else if (props.mode === "issue") {
261
+ await api.issue(props.store._id, { holder: holder.value, site: site.value || props.siteId, lines, reference: reference.value, remarks: remarks.value });
262
+ } else {
263
+ await api.receiveReturn(props.store._id, { holder: props.holderId, lines, remarks: remarks.value });
264
+ }
265
+ notify(copy.value.success);
266
+ show.value = false;
267
+ emit("done");
268
+ } catch (error: any) {
269
+ notify(error?.data?.message || "Something went wrong. Please try again.", "error");
270
+ } finally {
271
+ saving.value = false;
272
+ }
273
+ }
274
+
275
+ watch(show, (open) => {
276
+ if (!open) return;
277
+ rows.value = [blank()];
278
+ supplier.value = "";
279
+ reference.value = "";
280
+ remarks.value = "";
281
+ photos.value = [];
282
+ date.value = today;
283
+ holder.value = "";
284
+ site.value = props.siteId;
285
+ if (props.mode !== "return") loadItems();
286
+ if (props.mode === "issue") searchPeople("");
287
+ });
288
+ </script>
@@ -0,0 +1,226 @@
1
+ <template>
2
+ <div class="inventory">
3
+ <PageHeader :title="store ? store.name : 'Inventory'" :subtitle="subtitle">
4
+ <template #actions>
5
+ <v-btn-toggle
6
+ v-if="stores.length > 1"
7
+ v-model="storeId"
8
+ mandatory
9
+ density="comfortable"
10
+ variant="outlined"
11
+ divided
12
+ >
13
+ <v-btn v-for="s in stores" :key="s._id" :value="s._id" class="text-none">
14
+ {{ s.ownerType === "property" ? "Property store" : "Our store" }}
15
+ </v-btn>
16
+ </v-btn-toggle>
17
+ </template>
18
+ </PageHeader>
19
+
20
+ <v-progress-linear v-if="loading" indeterminate color="primary" class="mb-4" />
21
+
22
+ <AppCard v-else-if="loadError" class="pa-6">
23
+ <div class="text-h6 mb-2">Inventory could not be loaded</div>
24
+ <p class="text-body-2 mb-4">{{ loadError }}</p>
25
+ <v-btn class="text-none screen-btn-primary" variant="flat" @click="loadStores">Try again</v-btn>
26
+ </AppCard>
27
+
28
+ <!-- No store yet: one clear next step, or one clear explanation. -->
29
+ <AppCard v-else-if="!store" class="pa-6">
30
+ <template v-if="!showSetup">
31
+ <div class="text-h6 mb-2">Inventory is not set up for this property yet</div>
32
+ <p class="text-body-2 mb-4">
33
+ Set up a store to keep track of stock: what comes in, who takes it, and where it is used.
34
+ </p>
35
+ <v-btn class="text-none screen-btn-primary" variant="flat" @click="showSetup = true">Set up inventory</v-btn>
36
+ <p class="text-caption mt-3">
37
+ Only people allowed to manage inventory can finish the setup. Others will see the store once it is ready.
38
+ </p>
39
+ </template>
40
+ <v-form v-else ref="setupForm" @submit.prevent="createStore">
41
+ <div class="text-h6 mb-4">Set up inventory</div>
42
+ <InputLabel title="Store name" required class="mb-1" />
43
+ <v-text-field v-model="setup.name" :rules="[requiredRule]" density="comfortable" placeholder="e.g. Main Store" class="mb-2" />
44
+ <template v-if="orgSites.length > 1">
45
+ <InputLabel title="Properties this store supplies" required class="mb-1" />
46
+ <v-select
47
+ v-model="setup.sites"
48
+ :items="orgSites"
49
+ item-title="name"
50
+ item-value="_id"
51
+ multiple
52
+ chips
53
+ density="comfortable"
54
+ :rules="[(v: string[]) => v.length > 0 || 'Choose at least one property']"
55
+ hint="Most properties keep their own store. Choose more than one only if they share one store room."
56
+ persistent-hint
57
+ class="mb-4"
58
+ />
59
+ </template>
60
+ <div class="d-flex ga-2">
61
+ <v-btn class="text-none screen-btn-ghost" variant="outlined" @click="showSetup = false">Cancel</v-btn>
62
+ <v-btn class="text-none screen-btn-primary" variant="flat" type="submit" :loading="saving">Create store</v-btn>
63
+ </div>
64
+ </v-form>
65
+ </AppCard>
66
+
67
+ <template v-else>
68
+ <v-tabs v-model="tab" color="primary" class="mb-4" show-arrows>
69
+ <v-tab v-for="t in tabs" :key="t.value" :value="t.value" class="text-none">
70
+ {{ t.label }}
71
+ <v-badge v-if="t.badge" :content="t.badge" color="error" inline class="ml-1" />
72
+ </v-tab>
73
+ </v-tabs>
74
+
75
+ <v-window v-model="tab">
76
+ <v-window-item value="stock">
77
+ <InventoryStockTab :store="store" :site-id="siteId" :sites="storeSites" @changed="refreshCounts" />
78
+ </v-window-item>
79
+ <v-window-item v-if="store.access.canRequest && !store.access.canManage" value="mine">
80
+ <InventoryMyStockTab :store="store" :site-id="siteId" :sites="storeSites" @changed="refreshCounts" />
81
+ </v-window-item>
82
+ <v-window-item value="requests">
83
+ <InventoryRequestsTab :store="store" :site-id="siteId" @changed="refreshCounts" />
84
+ </v-window-item>
85
+ <v-window-item v-if="store.access.canApprove" value="technicians">
86
+ <InventoryTechniciansTab :store="store" @changed="refreshCounts" />
87
+ </v-window-item>
88
+ <v-window-item v-if="isStaff" value="reports">
89
+ <InventoryReportsTab :store="store" :sites="storeSites" />
90
+ </v-window-item>
91
+ <v-window-item v-if="store.access.canManage" value="settings">
92
+ <InventorySettingsTab :store="store" :org-sites="orgSites" @saved="loadStores" />
93
+ </v-window-item>
94
+ </v-window>
95
+ </template>
96
+
97
+ <Snackbar v-model="snackbar.show" :text="snackbar.text" :color="snackbar.color" />
98
+ </div>
99
+ </template>
100
+
101
+ <script setup lang="ts">
102
+ import useInventory from "../composables/useInventory";
103
+ import useSite from "../composables/useSite";
104
+ import useUtils from "../composables/useUtils";
105
+
106
+ const props = defineProps({
107
+ orgId: { type: String, required: true },
108
+ siteId: { type: String, required: true },
109
+ });
110
+
111
+ const { getStores, createStore: apiCreateStore, getRequests } = useInventory();
112
+ const { getSites } = useSite();
113
+ const { requiredRule } = useUtils();
114
+
115
+ const stores = ref<TInventoryStore[]>([]);
116
+ const storeId = ref("");
117
+ const store = computed(() => stores.value.find((s) => s._id === storeId.value) ?? null);
118
+ const loading = ref(true);
119
+ const loadError = ref("");
120
+ const saving = ref(false);
121
+ const tab = ref("stock");
122
+ const pendingCount = ref(0);
123
+ const orgSites = ref<{ _id: string; name: string }[]>([]);
124
+
125
+ const showSetup = ref(false);
126
+ const setupForm = ref();
127
+ const setup = reactive({ name: "Main Store", sites: [props.siteId] as string[] });
128
+
129
+ const snackbar = reactive({ show: false, text: "", color: "success" });
130
+ function notify(text: string, color = "success") {
131
+ Object.assign(snackbar, { show: true, text, color });
132
+ }
133
+ provide("inventoryNotify", notify);
134
+
135
+ const isStaff = computed(() => {
136
+ const a = store.value?.access;
137
+ return !!a && (a.canManage || a.canApprove || (a.canView && !a.canRequest));
138
+ });
139
+
140
+ const storeSites = computed(() =>
141
+ (store.value?.sites ?? []).map((id) => ({ _id: id, name: orgSites.value.find((s) => s._id === id)?.name ?? "This property" })),
142
+ );
143
+
144
+ const subtitle = computed(() => {
145
+ if (!store.value) return "";
146
+ if (storeSites.value.length > 1) return `Supplies ${storeSites.value.map((s) => s.name).join(", ")}`;
147
+ return store.value.ownerType === "property" ? "Property store" : "Our own store";
148
+ });
149
+
150
+ const tabs = computed(() => {
151
+ const a = store.value?.access;
152
+ if (!a) return [];
153
+ return [
154
+ { value: "stock", label: "Stock" },
155
+ ...(a.canRequest && !a.canManage ? [{ value: "mine", label: "My Stock" }] : []),
156
+ { value: "requests", label: a.canApprove ? "Requests" : "My Requests", badge: a.canApprove ? pendingCount.value : 0 },
157
+ ...(a.canApprove ? [{ value: "technicians", label: "Technicians" }] : []),
158
+ ...(isStaff.value ? [{ value: "reports", label: "Reports" }] : []),
159
+ ...(a.canManage ? [{ value: "settings", label: "Settings" }] : []),
160
+ ];
161
+ });
162
+
163
+ async function loadStores() {
164
+ loading.value = true;
165
+ loadError.value = "";
166
+ try {
167
+ stores.value = (await getStores(props.siteId)) ?? [];
168
+ if (!stores.value.some((s) => s._id === storeId.value)) {
169
+ // A service provider working at a property store sees it first.
170
+ storeId.value = stores.value[0]?._id ?? "";
171
+ }
172
+ } catch (error: any) {
173
+ // Never fall through to "not set up": that would invite a second store.
174
+ loadError.value = error?.data?.message || "Please check your connection and try again.";
175
+ } finally {
176
+ loading.value = false;
177
+ }
178
+ refreshCounts();
179
+ }
180
+
181
+ async function refreshCounts() {
182
+ if (!store.value?.access.canApprove) return (pendingCount.value = 0);
183
+ try {
184
+ pendingCount.value = (await getRequests(store.value._id, { status: "pending", limit: 1 }))?.pendingCount ?? 0;
185
+ } catch {
186
+ pendingCount.value = 0;
187
+ }
188
+ }
189
+
190
+ async function loadOrgSites() {
191
+ try {
192
+ const res = await getSites({ orgId: props.orgId, limit: 100 });
193
+ orgSites.value = (res?.items ?? []).map((s: any) => ({ _id: s._id, name: s.name }));
194
+ } catch {
195
+ orgSites.value = [];
196
+ }
197
+ }
198
+
199
+ async function createStore() {
200
+ const { valid } = await setupForm.value.validate();
201
+ if (!valid) return;
202
+ saving.value = true;
203
+ try {
204
+ const created = await apiCreateStore({ org: props.orgId, sites: setup.sites, name: setup.name });
205
+ notify("Store created. Add your items next.");
206
+ showSetup.value = false;
207
+ storeId.value = (created as any)?._id ?? "";
208
+ await loadStores();
209
+ } catch (error: any) {
210
+ notify(error?.data?.message || "Could not create the store.", "error");
211
+ } finally {
212
+ saving.value = false;
213
+ }
214
+ }
215
+
216
+ watch(storeId, () => {
217
+ tab.value = "stock";
218
+ refreshCounts();
219
+ });
220
+ watch(() => props.siteId, loadStores);
221
+
222
+ onMounted(() => {
223
+ loadStores();
224
+ loadOrgSites();
225
+ });
226
+ </script>
@@ -0,0 +1,114 @@
1
+ <template>
2
+ <div>
3
+ <div class="d-flex flex-wrap ga-2 align-center mb-3">
4
+ <div class="text-body-2 text-medium-emphasis">Stock you have taken from the store and not used or returned yet.</div>
5
+ <v-spacer />
6
+ <v-btn class="text-none screen-btn-primary" variant="flat" prepend-icon="mdi-cart-arrow-down" @click="showRequest = true">Request Stock</v-btn>
7
+ </div>
8
+
9
+ <v-progress-linear v-if="loading" indeterminate color="primary" />
10
+ <AppCard v-else-if="!holdings.length" class="pa-6 text-center">
11
+ <div class="text-body-1 mb-1">You are not holding any stock.</div>
12
+ <div class="text-body-2 text-medium-emphasis">Request stock from the store. Once approved, it shows here and you can record where you used it.</div>
13
+ </AppCard>
14
+
15
+ <v-row v-else dense>
16
+ <v-col v-for="h in holdings" :key="h._id" cols="12" sm="6" md="4">
17
+ <AppCard class="pa-4 d-flex align-center ga-3">
18
+ <v-avatar rounded size="44" color="grey-lighten-3">
19
+ <v-img v-if="h.photo" :src="getFileUrl(h.photo)" cover />
20
+ <v-icon v-else icon="mdi-package-variant" />
21
+ </v-avatar>
22
+ <div class="flex-grow-1">
23
+ <div class="font-weight-medium">{{ h.itemName }}</div>
24
+ <div class="text-body-2">{{ h.qty }} {{ h.unit }} in hand</div>
25
+ </div>
26
+ <v-btn class="text-none screen-btn-primary" variant="flat" size="small" @click="use(h)">Record usage</v-btn>
27
+ </AppCard>
28
+ </v-col>
29
+ </v-row>
30
+
31
+ <div class="text-subtitle-2 mt-6 mb-2">My recent activity</div>
32
+ <v-table density="compact">
33
+ <thead>
34
+ <tr>
35
+ <th>Date</th>
36
+ <th>Item</th>
37
+ <th>What happened</th>
38
+ <th class="text-right">Qty</th>
39
+ <th>Where / reference</th>
40
+ <th class="text-right">In hand after</th>
41
+ </tr>
42
+ </thead>
43
+ <tbody>
44
+ <tr v-for="t in activity" :key="t._id">
45
+ <td class="text-no-wrap">{{ standardFormatDate(t.date) }}</td>
46
+ <td>{{ t.itemName }}</td>
47
+ <td>{{ typeLabels[t.type] }}</td>
48
+ <td class="text-right">{{ t.qty }}</td>
49
+ <td class="text-caption">
50
+ {{ [t.location?.type === "tenant_unit" ? `${t.location.unitName} ${t.location.tenantName || ""}` : t.location?.area, t.reference].filter(Boolean).join(" · ") }}
51
+ </td>
52
+ <td class="text-right">{{ t.holderBalance }}</td>
53
+ </tr>
54
+ <tr v-if="!activity.length">
55
+ <td colspan="6" class="text-center text-medium-emphasis py-4">Nothing yet.</td>
56
+ </tr>
57
+ </tbody>
58
+ </v-table>
59
+
60
+ <InventoryUsageDialog v-model="usage.show" :store="store" :site-id="siteId" :holding="usage.holding" :sites="sites" @done="changed" />
61
+ <InventoryLinesDialog v-model="showRequest" mode="request" :store="store" :site-id="siteId" @done="changed" />
62
+ </div>
63
+ </template>
64
+
65
+ <script setup lang="ts">
66
+ import useInventory from "../composables/useInventory";
67
+ import useFile from "../composables/useFile";
68
+ import useUtils from "../composables/useUtils";
69
+ import useLocalAuth from "../composables/useLocalAuth";
70
+
71
+ const props = defineProps({
72
+ store: { type: Object as PropType<TInventoryStore>, required: true },
73
+ siteId: { type: String, required: true },
74
+ sites: { type: Array as PropType<{ _id: string; name: string }[]>, default: () => [] },
75
+ });
76
+ const emit = defineEmits(["changed"]);
77
+
78
+ const api = useInventory();
79
+ const { typeLabels } = api;
80
+ const { getFileUrl } = useFile();
81
+ const { standardFormatDate } = useUtils();
82
+ const { currentUser } = useLocalAuth();
83
+
84
+ const holdings = ref<TInventoryHolding[]>([]);
85
+ const activity = ref<TInventoryTransaction[]>([]);
86
+ const loading = ref(false);
87
+ const showRequest = ref(false);
88
+ const usage = reactive({ show: false, holding: null as TInventoryHolding | null });
89
+
90
+ async function load() {
91
+ loading.value = true;
92
+ try {
93
+ const [h, t] = await Promise.all([api.getHoldings(props.store._id, "me"), api.getTransactions(props.store._id, { page: 1, limit: 15, holder: currentUser.value?._id })]);
94
+ holdings.value = h;
95
+ activity.value = t.items.filter((x) => x.holderChange !== undefined);
96
+ } catch {
97
+ holdings.value = [];
98
+ } finally {
99
+ loading.value = false;
100
+ }
101
+ }
102
+
103
+ function use(h: TInventoryHolding) {
104
+ Object.assign(usage, { show: true, holding: h });
105
+ }
106
+
107
+ function changed() {
108
+ load();
109
+ emit("changed");
110
+ }
111
+
112
+ watch(() => props.store._id, load);
113
+ onMounted(load);
114
+ </script>