Skip to content

Commit c4278f3

Browse files
authored
Fix/nrbottlesdontdecrement (#1115)
* fixed delta for nr bottles for correct update * added last change
1 parent 4ab4a66 commit c4278f3

File tree

7 files changed

+79
-23
lines changed

7 files changed

+79
-23
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "drinkitembatch" ADD COLUMN "nr_bottles_delta" INTEGER;

src/database/prisma/schema.prisma

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,13 @@ model DrinkItem {
477477
}
478478

479479
model DrinkItemBatch {
480-
id String @id() @default(dbgenerated("gen_random_uuid()")) @db.Uuid()
481-
drinkItemId String @map("drink_item_id") @db.Uuid()
482-
item DrinkItem @relation(fields: [drinkItemId], references: [id])
483-
quantityDelta Int @map("quantity_delta")
484-
user String
485-
date DateTime
480+
id String @id() @default(dbgenerated("gen_random_uuid()")) @db.Uuid()
481+
drinkItemId String @map("drink_item_id") @db.Uuid()
482+
item DrinkItem @relation(fields: [drinkItemId], references: [id])
483+
quantityDelta Int @map("quantity_delta")
484+
user String
485+
date DateTime
486+
nrBottlesDelta Int? @map("nr_bottles_delta")
486487
487488
@@map("drinkitembatch")
488489
}

src/database/schema.zmodel

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,13 +565,13 @@ model DrinkItem {
565565
}
566566

567567
model DrinkItemBatch {
568-
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
569-
drinkItemId String @map("drink_item_id") @db.Uuid
570-
item DrinkItem @relation(fields: [drinkItemId], references: [id])
571-
quantityDelta Int @map("quantity_delta")
572-
user String
573-
date DateTime
574-
568+
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
569+
drinkItemId String @map("drink_item_id") @db.Uuid
570+
item DrinkItem @relation(fields: [drinkItemId], references: [id])
571+
quantityDelta Int @map("quantity_delta")
572+
user String
573+
date DateTime
574+
nrBottlesDelta Int? @map("nr_bottles_delta")
575575

576576
@@allow("read", true)
577577
@@allow("create", has(auth().policies, "drinkitembatch:create"))

src/database/seed/.snaplet/dataModel.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4168,6 +4168,20 @@
41684168
"isId": false,
41694169
"maxLength": null
41704170
},
4171+
{
4172+
"id": "public.drinkitembatch.nr_bottles_delta",
4173+
"name": "nr_bottles_delta",
4174+
"columnName": "nr_bottles_delta",
4175+
"type": "int4",
4176+
"isRequired": false,
4177+
"kind": "scalar",
4178+
"isList": false,
4179+
"isGenerated": false,
4180+
"sequence": false,
4181+
"hasDefaultValue": false,
4182+
"isId": false,
4183+
"maxLength": null
4184+
},
41714185
{
41724186
"name": "drinkitem",
41734187
"type": "drinkitem",

src/routes/(app)/admin/stocklist/stockchange/+page.server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const actions: Actions = {
5353
data: {
5454
drinkItemId: form.data.drinkItemId,
5555
quantityDelta: form.data.quantityDelta,
56+
nrBottlesDelta: form.data.nrBottles,
5657
date: dayjs(form.data.date).toDate(),
5758
user: user.studentId!,
5859
},
@@ -105,6 +106,7 @@ export const actions: Actions = {
105106
data: {
106107
drinkItemId: form.data.drinkItemId,
107108
quantityDelta: -form.data.quantityDelta,
109+
nrBottlesDelta: -form.data.nrBottles,
108110
date: dayjs(form.data.date).toDate(),
109111
user: user.studentId!,
110112
},

src/routes/(app)/admin/stocklist/treasury/+page.server.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const dateSchema = z.object({
1818
const updateSchema = z.object({
1919
id: z.string(),
2020
quantityDelta: z.number(),
21+
nrBottlesDelta: z.number(),
2122
});
2223

2324
export const load: PageServerLoad = async (event) => {
@@ -65,7 +66,8 @@ export const actions: Actions = {
6566
if (!form.valid) return fail(400, { form });
6667

6768
const batchId = form.data.id;
68-
const requestedDelta = form.data.quantityDelta;
69+
const requestedQuantityDelta = form.data.quantityDelta;
70+
const requestedNrBottlesDelta = form.data.nrBottlesDelta;
6971
try {
7072
await prisma.$transaction(async (tx) => {
7173
const batch = await tx.drinkItemBatch.findUnique({
@@ -75,26 +77,37 @@ export const actions: Actions = {
7577
},
7678
});
7779

78-
const newAmount =
80+
const newQuantity =
7981
batch!.item.quantityAvailable! -
8082
batch!.quantityDelta +
81-
requestedDelta;
82-
83-
if (newAmount < 0) {
83+
requestedQuantityDelta;
84+
85+
const newNrBottles =
86+
batch!.item.nrBottles! -
87+
batch!.nrBottlesDelta! +
88+
requestedNrBottlesDelta
89+
? batch!.item.nrBottles! -
90+
batch!.nrBottlesDelta! +
91+
requestedNrBottlesDelta
92+
: 0;
93+
94+
if (newQuantity < 0 || newNrBottles < 0) {
8495
throw new Error("Totalt antal blir negativt");
8596
}
8697

8798
await tx.drinkItemBatch.update({
8899
where: { id: batchId },
89100
data: {
90-
quantityDelta: requestedDelta,
101+
quantityDelta: requestedQuantityDelta,
102+
nrBottlesDelta: requestedNrBottlesDelta,
91103
},
92104
});
93105

94106
await tx.drinkItem.update({
95107
where: { id: batch?.item.id },
96108
data: {
97-
quantityAvailable: newAmount,
109+
quantityAvailable: newQuantity,
110+
nrBottles: newNrBottles,
98111
},
99112
});
100113
});
@@ -120,17 +133,23 @@ export const actions: Actions = {
120133
item: true,
121134
},
122135
});
123-
const newAmount =
136+
const newQuantity =
124137
batch!.item.quantityAvailable! - batch!.quantityDelta!;
125138

126-
if (newAmount < 0) {
139+
const newNrBottles =
140+
batch!.item.nrBottles! - batch!.nrBottlesDelta!
141+
? batch!.item.nrBottles! - batch!.nrBottlesDelta!
142+
: 0;
143+
144+
if (newQuantity < 0 || newNrBottles < 0) {
127145
throw new Error("Totalt antal blir negativt");
128146
}
129147

130148
await tx.drinkItem.update({
131149
where: { id: batch?.item.id },
132150
data: {
133-
quantityAvailable: newAmount,
151+
quantityAvailable: newQuantity,
152+
nrBottles: newNrBottles,
134153
},
135154
});
136155

src/routes/(app)/admin/stocklist/treasury/+page.svelte

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
function enableEdit(drinkBatch: DrinkItemBatch) {
2424
editId = drinkBatch.id;
2525
$updateForm.quantityDelta = drinkBatch.quantityDelta;
26+
$updateForm.nrBottlesDelta = drinkBatch.nrBottlesDelta
27+
? drinkBatch.nrBottlesDelta
28+
: 0;
2629
$updateForm.id = drinkBatch.id;
2730
}
2831
@@ -81,6 +84,7 @@
8184
<th>Datum</th>
8285
<th>Namn</th>
8386
<th>Antal/Vikt</th>
87+
<th>Antal flaskor</th>
8488
<th>Användare</th>
8589
<th>Ändra</th>
8690
<th>Ta bort</th>
@@ -107,6 +111,19 @@
107111
bind:value={$updateForm.id}
108112
/>
109113
</td>
114+
{#if entry.item.quantityType === "WEIGHT"}
115+
<td>
116+
<Input
117+
type="number"
118+
name="nrBottlesDelta"
119+
class="input w-20 bg-base-300"
120+
form="updateForm"
121+
bind:value={$updateForm.nrBottlesDelta}
122+
/>
123+
</td>
124+
{:else}
125+
<td>-</td>
126+
{/if}
110127
<td>{entry.user}</td>
111128
<td>
112129
<button
@@ -133,6 +150,7 @@
133150
<td>{new Date(entry.date).toLocaleDateString("sv-SE")}</td>
134151
<td>{entry.item.name}</td>
135152
<td>{entry.quantityDelta}</td>
153+
<td>{entry.nrBottlesDelta ? entry.nrBottlesDelta : "-"}</td>
136154
<td>{entry.user}</td>
137155
<td>
138156
<button

0 commit comments

Comments
 (0)