Skip to content

Commit e1a97b2

Browse files
[bug] Stock adjust (#10914) (#10915)
* Extra checks on backend * Bug fix for adjustment forms - Set default quantity of zero * Additional unit testing (to ensure no regression) (cherry picked from commit 5713cff) Co-authored-by: Oliver <[email protected]>
1 parent c9a1d9a commit e1a97b2

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/backend/InvenTree/stock/serializers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,10 @@ def save(self):
16781678
stock_item = item['pk']
16791679
quantity = item['quantity']
16801680

1681+
if quantity is None or quantity <= 0:
1682+
# Ignore in this case - no stock to add
1683+
continue
1684+
16811685
# Optional fields
16821686
extra = {}
16831687

@@ -1703,6 +1707,10 @@ def save(self):
17031707
stock_item = item['pk']
17041708
quantity = item['quantity']
17051709

1710+
# Ignore in this case - no stock to remove
1711+
if quantity is None or quantity <= 0:
1712+
continue
1713+
17061714
# Optional fields
17071715
extra = {}
17081716

src/frontend/src/forms/StockForms.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,10 +862,17 @@ function stockRemoveFields(items: any[]): ApiFormFieldSet {
862862

863863
const records = Object.fromEntries(items.map((item) => [item.pk, item]));
864864

865+
const initialValue = mapAdjustmentItems(items).map((elem) => {
866+
return {
867+
...elem,
868+
quantity: 0
869+
};
870+
});
871+
865872
const fields: ApiFormFieldSet = {
866873
items: {
867874
field_type: 'table',
868-
value: mapAdjustmentItems(items),
875+
value: initialValue,
869876
modelRenderer: (row: TableFieldRowProps) => {
870877
const record = records[row.item.pk];
871878

@@ -902,10 +909,17 @@ function stockAddFields(items: any[]): ApiFormFieldSet {
902909

903910
const records = Object.fromEntries(items.map((item) => [item.pk, item]));
904911

912+
const initialValue = mapAdjustmentItems(items).map((elem) => {
913+
return {
914+
...elem,
915+
quantity: 0
916+
};
917+
});
918+
905919
const fields: ApiFormFieldSet = {
906920
items: {
907921
field_type: 'table',
908-
value: mapAdjustmentItems(items),
922+
value: initialValue,
909923
modelRenderer: (row: TableFieldRowProps) => {
910924
const record = records[row.item.pk];
911925

@@ -941,10 +955,12 @@ function stockCountFields(items: any[]): ApiFormFieldSet {
941955

942956
const records = Object.fromEntries(items.map((item) => [item.pk, item]));
943957

958+
const initialValue = mapAdjustmentItems(items);
959+
944960
const fields: ApiFormFieldSet = {
945961
items: {
946962
field_type: 'table',
947-
value: mapAdjustmentItems(items),
963+
value: initialValue,
948964
modelRenderer: (row: TableFieldRowProps) => {
949965
return (
950966
<StockOperationsRow

src/frontend/tests/pages/pui_stock.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ test('Stock - Stock Actions', async ({ browser }) => {
332332
await page.getByRole('button', { name: 'Scan', exact: true }).click();
333333
await page.getByText('Scanned stock item into location').waitFor();
334334

335+
// Add "zero" stock - ensure the quantity stays the same
336+
await launchStockAction('add');
337+
await page.getByRole('button', { name: 'Submit' }).click();
338+
await page.getByText('Quantity: 123').first().waitFor();
339+
335340
// Add stock, and change status
336341
await launchStockAction('add');
337342
await page.getByLabel('number-field-quantity').fill('12');
@@ -342,6 +347,11 @@ test('Stock - Stock Actions', async ({ browser }) => {
342347
await page.getByText('Unavailable').first().waitFor();
343348
await page.getByText('135').first().waitFor();
344349

350+
// Remove "zero" stock - ensure the quantity stays the same
351+
await launchStockAction('remove');
352+
await page.getByRole('button', { name: 'Submit' }).click();
353+
await page.getByText('Quantity: 135').first().waitFor();
354+
345355
// Remove stock, and change status
346356
await launchStockAction('remove');
347357
await page.getByLabel('number-field-quantity').fill('99');

0 commit comments

Comments
 (0)