@7365admin1/layer-common 4.189.1 → 4.190.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # @iservice365/layer-common
2
2
 
3
+ ## 4.190.0
4
+
5
+ ### Minor Changes
6
+
7
+ - bd00ecd: Inventory: a "Checked out" tab (stock issued for a work order / with a return date, overdue flagged, badge on the tab) and "Checked out to me" in My Stock; recent activity shows the work order and return date (DV-1787).
8
+ - ef2b86b: Issue Stock: pick the work order the stock is for (a table with technician, subject and status filters, separate from Reference no.) and set an optional return date (DV-1787).
9
+ - d3d3d57: Work Order Settings: "Items must be returned before completing". New WorkOrderIssuedItems (issued items of a work order / feedback with status, return date and returned date-time); the Checked out list shows a Returned column (DV-1787).
10
+ - 044a080: Inventory serial numbers: a Serial numbers button on stock items and in Add Item (one per unit, each with its own history), choose serials in Issue Stock, and return all or chosen serials (DV-1787).
11
+
12
+ ### Patch Changes
13
+
14
+ - f4b5b68: DV-1787: "Already asked to return" uses the unit wording (1 pc, 2 pcs).
15
+ - cc228d3: Inventory: serial history shows received by / returned from and accepted by; item History shows date and time (DV-1787).
16
+ - b9a2b68: Inventory: store keepers can set, change or clear a checked-out item's return date (DV-1787).
17
+ - 6ce6dbf: Inventory: Return to store chooses serial numbers; Issue Stock Qty message refreshes; Checked out reloads after a return is accepted (DV-1787).
18
+ - 7d131d2: Inventory: scan serial numbers (1D barcodes and 2D codes) with the camera, one after another (DV-1787).
19
+ - 4594dd5: Inventory: choose which serial numbers are written off or used up (DV-1787).
20
+ - 06fba05: DV-1787: Issued items on the feedback and work order show one short line per item (with whom, returned); tap it for the details and Ask to return.
21
+ - 06fba05: DV-1787: Ask to return / Return on a checked-out item can bring back a number of units without a serial number, alone or with chosen serials.
22
+
23
+ ## 4.189.2
24
+
25
+ ### Patch Changes
26
+
27
+ - aa58650: Manage Account passes the current page, and the header shares it (only iService365 addresses) as the `account-return` state, so the Account app can show a Back button to it.
28
+
3
29
  ## 4.189.1
4
30
 
5
31
  ### Patch Changes
@@ -4148,11 +4174,11 @@ Monthly report: a month that had patrols opens the list of that month's patrol r
4148
4174
  `utils/console-tier.ts` + `composables/useConsoleTier.ts` mirror the server's
4149
4175
  own rule from two endpoints the console already calls, unprojected:
4150
4176
 
4151
- GET /api/members/user/:user/app/admin the Seven365 staff membership
4152
- GET /api/roles/id/:role that membership's role document
4177
+ GET /api/members/user/:user/app/admin the Seven365 staff membership
4178
+ GET /api/roles/id/:role that membership's role document
4153
4179
 
4154
- owner = member.type === "admin" && role.type === "admin" && role.default === true
4155
- staff = member.type === "admin" && role.type === "admin"
4180
+ owner = member.type === "admin" && role.type === "admin" && role.default === true
4181
+ staff = member.type === "admin" && role.type === "admin"
4156
4182
 
4157
4183
  `role.default` is the marker because it is the only property of a platform
4158
4184
  staff role no API caller can set - `role.controller.ts` validates create and
@@ -0,0 +1,288 @@
1
+ <template>
2
+ <!--
3
+ DV-1787: the details of issued items (one item's issues on a work order / feedback, or one row of
4
+ Checked out): totals, then each issue with a table of its serial numbers and the units without one,
5
+ and for the holder Received / Not Received / Ask to Return.
6
+ -->
7
+ <v-dialog v-model="show" max-width="600" scrollable>
8
+ <v-card v-if="rows.length" class="screen-modal">
9
+ <v-card-title class="d-flex align-center">
10
+ {{ rows[0].itemName }}
11
+ <span v-if="subtitle" class="text-body-2 text-medium-emphasis ml-2">{{ subtitle }}</span>
12
+ <v-spacer />
13
+ <v-btn icon="mdi-close" variant="text" class="screen-modal__close" aria-label="Close" @click="show = false" />
14
+ </v-card-title>
15
+ <v-card-text class="screen-modal__body">
16
+ <div class="cd__stats mb-3">
17
+ <div v-for="st in totals" :key="st.label" class="cd__stat">
18
+ <div class="cd__stat-num" :class="st.cls">{{ st.n }}</div>
19
+ <div class="cd__stat-label">{{ st.label }}</div>
20
+ </div>
21
+ </div>
22
+ <div v-for="c in rows" :key="c._id" class="cd__issue">
23
+ <div class="cd__top">
24
+ <span class="cd__name">
25
+ {{ qtyUnit(c.qty, c.unit) }} issued to {{ c.holderName }}
26
+ <span v-if="c.workOrderNumber && mixedWo" class="text-medium-emphasis font-weight-regular">· {{ c.workOrderNumber }}</span>
27
+ </span>
28
+ <span class="d-flex flex-wrap ga-1">
29
+ <!-- only what needs attention: overdue / due today, or how it ended -->
30
+ <v-tooltip v-if="chipShown(c)" :text="checkoutChip(c).info" location="top" max-width="280">
31
+ <template #activator="{ props: tip }">
32
+ <v-chip v-bind="tip" size="small" variant="tonal" :color="checkoutChip(c).color" :prepend-icon="checkoutChip(c).icon">{{ checkoutChip(c).text }}</v-chip>
33
+ </template>
34
+ </v-tooltip>
35
+ <v-tooltip v-if="c.status === 'out' && c.returnRequest && !c.returnAsked" :text="RETURN_ASKED_INFO" location="top" max-width="280">
36
+ <template #activator="{ props: tip }">
37
+ <v-chip v-bind="tip" size="small" variant="tonal" color="primary" prepend-icon="mdi-keyboard-return">Return Asked</v-chip>
38
+ </template>
39
+ </v-tooltip>
40
+ </span>
41
+ </div>
42
+ <div class="cd__dates">
43
+ <span><span class="cd__label">Issued</span> {{ standardFormatDateTime(c.createdAt) }}</span>
44
+ <span><span class="cd__label">Return by</span> {{ c.returnDate ? returnDayText(c.returnDate) : "—" }}</span>
45
+ <span v-if="c.status !== 'out' && c.closedAt"><span class="cd__label">{{ c.status === "returned" ? "Returned" : "Closed" }}</span> {{ standardFormatDateTime(c.closedAt) }}</span>
46
+ </div>
47
+ <!-- this issue's own totals (when the dialog shows more than one) -->
48
+ <div v-if="rows.length > 1" class="cd__mini mt-2">
49
+ <span v-for="st in checkoutStats([c])" :key="st.label" class="cd__mini-pill">
50
+ <b :class="st.cls">{{ st.n }}</b> {{ st.label }}
51
+ </span>
52
+ </div>
53
+ <v-table v-if="serialRows(c).length" density="compact" class="cd__table mt-2">
54
+ <thead>
55
+ <tr>
56
+ <th>S/N / Qty</th>
57
+ <th>Status</th>
58
+ <th>When</th>
59
+ </tr>
60
+ </thead>
61
+ <tbody>
62
+ <tr v-for="(r, i) in serialRows(c)" :key="i">
63
+ <td>
64
+ <span class="font-weight-medium">{{ r.serial }}</span>
65
+ <span v-if="r.plain" class="text-body-2 text-medium-emphasis"> · no S/N</span>
66
+ </td>
67
+ <td><v-chip size="small" variant="tonal" :color="r.color">{{ r.label }}</v-chip></td>
68
+ <td class="text-body-2 text-medium-emphasis">{{ r.at ? standardFormatDateTime(r.at) : "—" }}</td>
69
+ </tr>
70
+ </tbody>
71
+ </v-table>
72
+ <!-- not received yet: the holder taps Received first (then Ask to Return shows) -->
73
+ <template v-if="c.status === 'out' && c.received === false">
74
+ <v-alert v-if="c.ackStatus === 'not_received'" type="error" variant="tonal" density="compact" class="mt-2 cd__note" icon="mdi-alert-circle-outline">
75
+ {{ isMine(c) ? "You said you did not get it." : `${c.holderName} said they did not get it.` }} The store keeper checks and takes it back.
76
+ <template v-if="isMine(c)"> If it turns up, tap Received.</template>
77
+ </v-alert>
78
+ <div v-if="isMine(c)" class="d-flex flex-wrap align-center ga-2 mt-2">
79
+ <v-btn variant="flat" color="primary" class="text-none cd__btn" prepend-icon="mdi-check" :loading="receiving === c._id" @click="markReceived(c)">Received</v-btn>
80
+ <v-btn v-if="c.ackStatus !== 'not_received'" variant="outlined" color="error" class="text-none cd__btn" prepend-icon="mdi-close" @click="openNotReceived(c)">
81
+ Not Received
82
+ </v-btn>
83
+ </div>
84
+ <div v-if="isMine(c) && c.ackStatus !== 'not_received'" class="cd__meta text-medium-emphasis mt-1">
85
+ Tap Received once it is in your hands (then you can ask to return it), or Not Received if you did not get it.
86
+ </div>
87
+ </template>
88
+ <v-btn v-if="isMine(c) && askLeft(c) > 0" variant="tonal" color="primary" class="text-none mt-2 cd__btn" prepend-icon="mdi-keyboard-return" @click="openAsk(c)">
89
+ {{ c.returnAsked?.qty ? `Ask to Return the Other ${askLeft(c)}` : "Ask to Return" }}
90
+ </v-btn>
91
+ </div>
92
+ </v-card-text>
93
+ </v-card>
94
+ </v-dialog>
95
+
96
+ <InventoryCheckoutReturnDialog v-model="ask.show" :row="ask.row" mode="ask" @done="emit('changed')" />
97
+
98
+ <!-- Not Received: why, so the store keeper can check -->
99
+ <v-dialog v-model="nr.show" max-width="440">
100
+ <v-card class="screen-modal">
101
+ <v-card-title class="d-flex align-center">
102
+ Not Received
103
+ <v-spacer />
104
+ <v-btn icon="mdi-close" variant="text" class="screen-modal__close" aria-label="Close" @click="nr.show = false" />
105
+ </v-card-title>
106
+ <v-card-text class="screen-modal__body">
107
+ <p class="text-body-1 mb-3">Tell the store keeper you did not get {{ nr.row ? `${qtyUnit(nr.row.qty, nr.row.unit)} of ${nr.row.itemName}` : "it" }}. It stays out until they check and take it back.</p>
108
+ <InputLabel title="Why?" required class="mb-1" />
109
+ <v-text-field v-model="nr.reason" density="comfortable" placeholder="e.g. nothing was handed to me" hide-details="auto" :rules="[(v) => !!String(v || '').trim() || 'Required']" />
110
+ </v-card-text>
111
+ <div class="screen-modal__footer">
112
+ <v-btn class="screen-btn-ghost" variant="outlined" :disabled="nr.saving" @click="nr.show = false">Cancel</v-btn>
113
+ <v-btn class="screen-btn-primary" variant="flat" color="error" :loading="nr.saving" :disabled="!nr.reason.trim()" @click="saveNotReceived">Not Received</v-btn>
114
+ </div>
115
+ </v-card>
116
+ </v-dialog>
117
+ </template>
118
+
119
+ <script setup lang="ts">
120
+ import useInventory from "../composables/useInventory";
121
+ import useUtils from "../composables/useUtils";
122
+ import { qtyUnit } from "../utils/inventory-ack";
123
+ import { askLeft, checkoutChip, checkoutStats, returnDayKey, RETURN_ASKED_INFO } from "../utils/inventory-checkout";
124
+
125
+ const show = defineModel<boolean>({ default: false });
126
+ const props = defineProps({
127
+ /** The issues to show (one item's issues, or one checked-out row). */
128
+ rows: { type: Array as PropType<TInventoryCheckout[]>, default: () => [] },
129
+ /** Shown next to the item name, e.g. the work order number. */
130
+ subtitle: { type: String, default: "" },
131
+ });
132
+ /** Something changed (Received, Not Received, Ask to Return): the list reloads; the dialog follows its rows. */
133
+ const emit = defineEmits<{ changed: [] }>();
134
+
135
+ const { standardFormatDate, standardFormatDateTime } = useUtils();
136
+ const returnDayText = (d: string) => standardFormatDate(returnDayKey(d));
137
+ const totals = computed(() => checkoutStats(props.rows));
138
+ // Each issue names its work order only when they differ (the title has it otherwise).
139
+ const mixedWo = computed(() => new Set(props.rows.map((c) => c.workOrderNumber ?? "")).size > 1);
140
+
141
+ const { currentUser } = useLocalAuth();
142
+ const me = computed(() => String(currentUser.value?._id ?? ""));
143
+ const isMine = (c: TInventoryCheckout) => c.status === "out" && !!me.value && String(c.holder) === me.value;
144
+
145
+ // The plain "Not returned yet" / "Out" chip says nothing the totals don't; Pending Receipt / Not Received are in the table.
146
+ const chipShown = (c: TInventoryCheckout) => !["Not returned yet", "Out", "Pending Receipt", "Not Received"].includes(checkoutChip(c).text);
147
+ const ENDED_LABEL: Record<string, [string, string]> = {
148
+ returned: ["Returned", "success"],
149
+ written_off: ["Written Off", "error"],
150
+ used: ["Used", "grey"],
151
+ handed_over: ["Handed Over", "secondary"],
152
+ closed: ["Closed", "grey"],
153
+ };
154
+ const waitLabel = (c: TInventoryCheckout) => (c.ackStatus === "not_received" ? "Not Received" : "Pending Receipt");
155
+ const waitColor = (c: TInventoryCheckout) => (c.ackStatus === "not_received" ? "error" : "grey");
156
+ const endedLabel = (e: { type: string; toName?: string }) => (e.type === "handed_over" && e.toName ? `Handed Over to ${e.toName}` : (ENDED_LABEL[e.type]?.[0] ?? e.type));
157
+ type TRow = { serial: string; label: string; color: string; at: string | null; plain: boolean };
158
+ function serialRows(c: TInventoryCheckout): TRow[] {
159
+ const asked = new Set(c.returnAsked?.serials ?? []);
160
+ const holding = (): Omit<TRow, "serial" | "plain"> =>
161
+ c.received === false ? { label: waitLabel(c), color: waitColor(c), at: null } : { label: `With ${c.holderName}`, color: "warning", at: null };
162
+ const withSerial: TRow[] = [
163
+ ...(c.serials ?? []).map((x) => ({ serial: x.serial, plain: false, ...(asked.has(String(x._id)) ? { label: "Return Asked", color: "primary", at: null } : holding()) })),
164
+ ...(c.endedSerials ?? []).map((e) => ({ serial: e.serial, plain: false, label: endedLabel(e), color: ENDED_LABEL[e.type]?.[1] ?? "grey", at: e.at })),
165
+ ].sort((x, y) => x.serial.localeCompare(y.serial, undefined, { numeric: true }));
166
+ // Units without a serial number: one line per state, with how many.
167
+ const unit = c.unit || "pcs";
168
+ const plainOut = c.status === "out" ? Math.max(0, c.outQty - (c.serials?.length ?? 0)) : 0;
169
+ const plainAsked = c.status === "out" && c.returnAsked ? Math.max(0, c.returnAsked.qty - c.returnAsked.serials.length) : 0;
170
+ const plain: TRow[] = [];
171
+ if (plainOut - plainAsked > 0) plain.push({ serial: qtyUnit(plainOut - plainAsked, unit), plain: true, ...holding() });
172
+ if (plainAsked) plain.push({ serial: qtyUnit(plainAsked, unit), plain: true, label: "Return Asked", color: "primary", at: null });
173
+ for (const e of c.endedPlain ?? []) plain.push({ serial: qtyUnit(e.qty, unit), plain: true, label: endedLabel(e), color: ENDED_LABEL[e.type]?.[1] ?? "grey", at: e.at });
174
+ return [...withSerial, ...plain];
175
+ }
176
+
177
+ // Ask to Return / Received / Not Received for the holder.
178
+ const ask = reactive({ show: false, row: null as TInventoryCheckout | null });
179
+ const openAsk = (row: TInventoryCheckout) => Object.assign(ask, { show: true, row });
180
+ const receiving = ref("");
181
+ async function markReceived(c: TInventoryCheckout) {
182
+ if (!c.request) return;
183
+ receiving.value = c._id;
184
+ try {
185
+ await useInventory().acknowledgeRequest(String(c.request));
186
+ emit("changed");
187
+ } finally {
188
+ receiving.value = "";
189
+ }
190
+ }
191
+ const nr = reactive({ show: false, row: null as TInventoryCheckout | null, reason: "", saving: false });
192
+ const openNotReceived = (c: TInventoryCheckout) => Object.assign(nr, { show: true, row: c, reason: "" });
193
+ async function saveNotReceived() {
194
+ if (!nr.row?.request || !nr.reason.trim()) return;
195
+ nr.saving = true;
196
+ try {
197
+ await useInventory().notReceived(String(nr.row.request), nr.reason.trim());
198
+ nr.show = false;
199
+ emit("changed");
200
+ } finally {
201
+ nr.saving = false;
202
+ }
203
+ }
204
+ </script>
205
+
206
+ <style scoped>
207
+ .cd__stats {
208
+ display: flex;
209
+ flex-wrap: wrap;
210
+ gap: 8px;
211
+ }
212
+ .cd__stat {
213
+ flex: 1 1 90px;
214
+ border: 1px solid rgba(var(--v-border-color), var(--v-border-opacity));
215
+ border-radius: 8px;
216
+ padding: 8px 10px;
217
+ }
218
+ .cd__stat-num {
219
+ font-size: 1.5rem;
220
+ font-weight: 600;
221
+ line-height: 1.2;
222
+ }
223
+ .cd__stat-label {
224
+ font-size: 0.875rem;
225
+ color: rgba(var(--v-theme-on-surface), var(--v-medium-emphasis-opacity));
226
+ }
227
+ .cd__issue {
228
+ padding: 10px 0;
229
+ border-top: 1px solid rgba(var(--v-border-color), var(--v-border-opacity));
230
+ }
231
+ .cd__top {
232
+ display: flex;
233
+ flex-wrap: wrap;
234
+ align-items: center;
235
+ justify-content: space-between;
236
+ gap: 4px 8px;
237
+ }
238
+ .cd__name {
239
+ font-weight: 500;
240
+ min-width: 0;
241
+ overflow-wrap: anywhere;
242
+ }
243
+ .cd__dates {
244
+ display: flex;
245
+ flex-wrap: wrap;
246
+ gap: 2px 16px;
247
+ font-size: 0.875rem;
248
+ margin-top: 2px;
249
+ color: rgba(var(--v-theme-on-surface), var(--v-medium-emphasis-opacity));
250
+ }
251
+ .cd__label {
252
+ font-weight: 500;
253
+ }
254
+ .cd__meta {
255
+ font-size: 0.875rem;
256
+ }
257
+ .cd__mini {
258
+ display: flex;
259
+ flex-wrap: wrap;
260
+ gap: 6px;
261
+ }
262
+ .cd__mini-pill {
263
+ font-size: 0.875rem;
264
+ border: 1px solid rgba(var(--v-border-color), var(--v-border-opacity));
265
+ border-radius: 14px;
266
+ padding: 2px 10px;
267
+ color: rgba(var(--v-theme-on-surface), var(--v-medium-emphasis-opacity));
268
+ }
269
+ .cd__mini-pill b {
270
+ font-size: 0.9375rem;
271
+ }
272
+ .cd__table {
273
+ font-size: 0.9375rem;
274
+ border: 1px solid rgba(var(--v-border-color), var(--v-border-opacity));
275
+ border-radius: 6px;
276
+ }
277
+ .cd__table :deep(.v-chip) {
278
+ font-size: 0.875rem;
279
+ font-weight: 500;
280
+ }
281
+ .cd__btn {
282
+ font-size: 1rem;
283
+ font-weight: 500;
284
+ }
285
+ .cd__note {
286
+ font-size: 0.9375rem;
287
+ }
288
+ </style>
@@ -0,0 +1,189 @@
1
+ <template>
2
+ <!--
3
+ DV-1787: return ONE checked-out item (its work order). `ask`: the holder asks to return it (a store
4
+ keeper accepts it when they have it). `return`: a store keeper takes it back now, or accepts the
5
+ holder's request.
6
+ -->
7
+ <v-dialog v-model="show" max-width="500">
8
+ <v-card v-if="row" class="screen-modal">
9
+ <v-card-title class="d-flex align-center">
10
+ {{ title }}
11
+ <v-spacer />
12
+ <v-btn icon="mdi-close" variant="text" class="screen-modal__close" aria-label="Close" @click="show = false" />
13
+ </v-card-title>
14
+ <v-card-text class="screen-modal__body">
15
+ <p class="text-body-2 mb-3">
16
+ <strong>{{ qtyUnit(row.outQty, row.unit) }} of {{ row.itemName }}</strong>
17
+ <template v-if="row.workOrderNumber"> for work order <strong>{{ row.workOrderNumber }}</strong></template>
18
+ <template v-if="mode === 'return'"> from {{ row.holderName }}</template>
19
+ <template v-if="row.returnDate"> · return by {{ returnDayText(row.returnDate) }}</template>
20
+ </p>
21
+ <!-- DV-1787: part of it already asked to return - say what, and that only the rest is offered. -->
22
+ <v-alert v-if="row.returnAsked?.qty" type="success" variant="tonal" density="compact" class="mb-3" icon="mdi-keyboard-return">
23
+ Already asked to return: {{ askedText(row) }}.<template v-if="mode === 'ask'"> You can ask for the other {{ qtyUnit(available, row.unit) }}.</template>
24
+ </v-alert>
25
+
26
+ <v-alert type="info" variant="tonal" density="compact" class="mb-3" icon="mdi-information-outline">
27
+ <template v-if="mode === 'ask'">
28
+ <div class="font-weight-medium mb-1">How Ask to return works</div>
29
+ <ol class="return-steps">
30
+ <li>The store keeper is told you are bringing this back.</li>
31
+ <li>Hand it over at the store. Until the store keeper accepts it, it stays with you: it still shows under Checked out to me and on the work order, with "Return asked".</li>
32
+ <li>When they accept, it goes back into the store and shows Returned with the date and time.</li>
33
+ <li>Only then can the work order (or its feedback) be completed, if Work Order Settings ask for items to be returned first.</li>
34
+ </ol>
35
+ </template>
36
+ <template v-else-if="row.returnRequest">
37
+ {{ row.holderName }} asked to return it. Accept once you have it in hand: it goes back into the store now and the work order is freed.
38
+ </template>
39
+ <template v-else>
40
+ Take it back now: it goes back into the store straight away and shows Returned for this work order. Use this when it is in your hands.
41
+ </template>
42
+ </v-alert>
43
+
44
+ <!-- DV-1787: more than one unit out on it - return all of it, or only some: chosen serial
45
+ numbers and/or a number of the units without one. -->
46
+ <template v-if="available > 1 && !(mode === 'return' && row.returnRequest)">
47
+ <InputLabel title="What comes back" class="mb-1" />
48
+ <v-radio-group v-model="which" inline hide-details density="compact" class="mb-1">
49
+ <v-radio :label="`All ${partial ? 'the other ' : ''}${qtyUnit(available, row.unit)}`" value="all" />
50
+ <v-radio label="Choose which" value="some" />
51
+ </v-radio-group>
52
+ <div v-if="which === 'all'" class="text-caption text-medium-emphasis mb-3">{{ allText }}</div>
53
+ <template v-else>
54
+ <div v-if="serials.length" class="d-flex flex-wrap ga-2 mb-2">
55
+ <v-checkbox
56
+ v-for="s in serials"
57
+ :key="s._id"
58
+ v-model="picked"
59
+ :value="s._id"
60
+ :label="s.serial"
61
+ density="compact"
62
+ hide-details
63
+ />
64
+ </div>
65
+ <div v-if="plain > 0" class="d-flex align-center ga-3 mb-3">
66
+ <v-text-field
67
+ v-model.number="plainQty"
68
+ type="number"
69
+ :min="0"
70
+ :max="plain"
71
+ density="comfortable"
72
+ hide-details="auto"
73
+ :rules="[plainRule]"
74
+ style="max-width: 140px"
75
+ :suffix="row.unit || 'pcs'"
76
+ />
77
+ <span class="text-body-2 text-medium-emphasis">without serial number (of {{ plain }})</span>
78
+ </div>
79
+ </template>
80
+ </template>
81
+ <div v-else-if="allText && (serials.length || partial)" class="text-caption text-medium-emphasis mb-3">{{ allText }}</div>
82
+ <template v-if="!(mode === 'return' && row.returnRequest)">
83
+ <InputLabel title="Note (optional)" class="mb-1" />
84
+ <v-text-field v-model="note" density="comfortable" placeholder="e.g. job done, left at the store counter" hide-details />
85
+ </template>
86
+ </v-card-text>
87
+ <div class="screen-modal__footer">
88
+ <v-btn class="screen-btn-ghost" variant="outlined" :disabled="saving" @click="show = false">Cancel</v-btn>
89
+ <v-btn class="screen-btn-primary" variant="flat" :loading="saving" :disabled="which === 'some' && !someOk" @click="confirm">
90
+ {{ title }}{{ which === "some" && someCount ? ` (${someCount})` : "" }}
91
+ </v-btn>
92
+ </div>
93
+ </v-card>
94
+ </v-dialog>
95
+ <Snackbar v-model="toast.show" :text="toast.message" :color="toast.color" />
96
+ </template>
97
+
98
+ <script setup lang="ts">
99
+ import useInventory from "../composables/useInventory";
100
+ import useUtils from "../composables/useUtils";
101
+ import { qtyUnit } from "../utils/inventory-ack";
102
+ import { askedText, returnDayKey } from "../utils/inventory-checkout";
103
+
104
+ const show = defineModel<boolean>({ default: false });
105
+ const props = defineProps({
106
+ row: { type: Object as PropType<TInventoryCheckout | null>, default: null },
107
+ /** `ask`: the holder asks to return it. `return`: a store keeper takes it back (or accepts the request). */
108
+ mode: { type: String as PropType<"ask" | "return">, default: "ask" },
109
+ });
110
+ const emit = defineEmits<{ done: [] }>();
111
+
112
+ const api = useInventory();
113
+ const { standardFormatDate } = useUtils();
114
+ // Inventory pages give their own message bar; elsewhere (work order / feedback details) this one is used.
115
+ const toast = reactive({ show: false, message: "", color: "success" });
116
+ const injected = inject<((text: string, color?: string) => void) | null>("inventoryNotify", null);
117
+ const notify = (text: string, color = "success") => (injected ? injected(text, color) : Object.assign(toast, { show: true, message: text, color }));
118
+ const note = ref("");
119
+ const saving = ref(false);
120
+ // DV-1787: serial numbers still out on it; return all, or the chosen ones.
121
+ // Asking again on a row asked for in parts: only what is not asked yet is offered.
122
+ const asked = computed(() => (props.mode === "ask" ? props.row?.returnAsked ?? null : null));
123
+ const partial = computed(() => Boolean(asked.value?.qty));
124
+ const allSerials = computed(() => [...(props.row?.serials ?? [])].sort((a, b) => a.serial.localeCompare(b.serial, undefined, { numeric: true })));
125
+ const serials = computed(() => allSerials.value.filter((s) => !asked.value?.serials.includes(String(s._id))));
126
+ const which = ref<"all" | "some">("all");
127
+ const picked = ref<string[]>([]);
128
+ // ...and units without a serial number: how many are out on it, and how many come back.
129
+ const plain = computed(() => {
130
+ const all = Math.max(0, (props.row?.outQty ?? 0) - allSerials.value.length);
131
+ const askedPlain = asked.value ? Math.max(0, asked.value.qty - asked.value.serials.length) : 0;
132
+ return Math.max(0, all - askedPlain);
133
+ });
134
+ const available = computed(() => serials.value.length + plain.value);
135
+ const plainQty = ref(0);
136
+ const plainRule = (v: number) => (Number.isInteger(Number(v)) && v >= 0 && v <= plain.value) || `0 to ${plain.value}`;
137
+ const someCount = computed(() => picked.value.length + (plain.value ? Number(plainQty.value) || 0 : 0));
138
+ const someOk = computed(() => someCount.value > 0 && plainRule(Number(plainQty.value) || 0) === true);
139
+ const allText = computed(() => {
140
+ const parts = [];
141
+ if (serials.value.length) parts.push(`S/N ${serials.value.map((s) => s.serial).join(", ")}`);
142
+ if (plain.value) parts.push(`${qtyUnit(plain.value, props.row?.unit)} without serial number`);
143
+ return parts.join(" + ");
144
+ });
145
+ // "All" on a row asked for in parts is the rest, spelled out (the serials not asked yet + the units without one).
146
+ const chosenSerials = () => (which.value === "some" ? picked.value : partial.value ? serials.value.map((s) => s._id) : undefined);
147
+ const chosenPlain = () => (which.value === "some" ? (plain.value ? Number(plainQty.value) || 0 : 0) : partial.value ? plain.value : undefined);
148
+
149
+ const title = computed(() => (props.mode === "ask" ? "Ask to return" : props.row?.returnRequest ? "Accept return" : "Return to store"));
150
+ const returnDayText = (d: string) => standardFormatDate(returnDayKey(d));
151
+
152
+ async function confirm() {
153
+ if (!props.row) return;
154
+ saving.value = true;
155
+ try {
156
+ if (props.mode === "ask") {
157
+ await api.requestCheckoutReturn(props.row._id, note.value, chosenSerials(), chosenPlain());
158
+ notify("Return asked. The store keeper accepts it when they have it.");
159
+ } else {
160
+ await api.returnCheckout(props.row._id, note.value, chosenSerials(), chosenPlain());
161
+ notify("Returned to the store.");
162
+ }
163
+ show.value = false;
164
+ emit("done");
165
+ } catch (error: any) {
166
+ notify(error?.data?.message || "Something went wrong. Please try again.", "error");
167
+ } finally {
168
+ saving.value = false;
169
+ }
170
+ }
171
+
172
+ watch(show, (open) => {
173
+ if (!open) return;
174
+ note.value = "";
175
+ which.value = "all";
176
+ picked.value = [];
177
+ plainQty.value = 0;
178
+ });
179
+ </script>
180
+
181
+ <style scoped>
182
+ .return-steps {
183
+ padding-left: 18px;
184
+ margin: 0;
185
+ }
186
+ .return-steps li {
187
+ margin-bottom: 2px;
188
+ }
189
+ </style>