Merged PR 1917: feat(remission-data-access): enhance stock calculation to handle zero predefi...

feat(remission-data-access): enhance stock calculation to handle zero predefined quantities

Improve calculateStockToRemit and getStockToRemit functions to properly distinguish
between undefined and zero predefined return quantities. When predefinedReturnQuantity
is undefined, the system now falls back to approximation calculation (availableStock
minus remainingQuantityInStock). When predefinedReturnQuantity is explicitly set to 0,
the system respects this backend-calculated value.

Add comprehensive test coverage for edge cases including:
- Zero predefined return quantities for both Pflicht and Abteilung types
- Negative approximation calculations (clamped to 0)
- Null/undefined remainingQuantityInStock handling
- Missing returnItem scenarios for Abteilung type

Ref: #5280
This commit is contained in:
Nino Righi
2025-08-13 13:39:18 +00:00
committed by Andreas Schickinger
parent bbb9c5d39c
commit 0740273dbc
2 changed files with 123 additions and 4 deletions

View File

@@ -5,7 +5,17 @@ import {
import { RemissionListType } from '@isa/remission/data-access';
describe('calculateStockToRemit', () => {
it('should return predefinedReturnQuantity when provided', () => {
it('should return predefinedReturnQuantity when provided (even if 0) - #5280 Fix', () => {
const result = calculateStockToRemit({
availableStock: 10,
predefinedReturnQuantity: 0,
remainingQuantityInStock: 2,
});
expect(result).toBe(0);
});
it('should return predefinedReturnQuantity when provided with positive value', () => {
const result = calculateStockToRemit({
availableStock: 10,
predefinedReturnQuantity: 5,
@@ -15,7 +25,7 @@ describe('calculateStockToRemit', () => {
expect(result).toBe(5);
});
it('should calculate availableStock minus remainingQuantityInStock when no predefinedReturnQuantity', () => {
it('should calculate availableStock minus remainingQuantityInStock when no predefinedReturnQuantity - #5269 Fix', () => {
const result = calculateStockToRemit({
availableStock: 10,
remainingQuantityInStock: 3,
@@ -23,6 +33,34 @@ describe('calculateStockToRemit', () => {
expect(result).toBe(7);
});
it('should return 0 when approximation calculation would be negative - #5269 Fix', () => {
const result = calculateStockToRemit({
availableStock: 5,
remainingQuantityInStock: 8,
});
expect(result).toBe(0);
});
it('should handle undefined remainingQuantityInStock when no predefinedReturnQuantity - #5269 Fix', () => {
const result = calculateStockToRemit({
availableStock: 10,
remainingQuantityInStock: undefined,
});
expect(result).toBe(10);
});
it('should handle null remainingQuantityInStock when no predefinedReturnQuantity - #5269 Fix', () => {
const result = calculateStockToRemit({
availableStock: 10,
// @ts-ignore - Testing runtime behavior with null
remainingQuantityInStock: null,
});
expect(result).toBe(10);
});
});
describe('getStockToRemit', () => {
@@ -41,6 +79,35 @@ describe('getStockToRemit', () => {
expect(result).toBe(5);
});
it('should handle Pflicht remission list type with zero predefined return quantity - #5280 Fix', () => {
const remissionItem = {
remainingQuantityInStock: 2,
predefinedReturnQuantity: 0,
} as any;
const result = getStockToRemit({
remissionItem,
remissionListType: RemissionListType.Pflicht,
availableStock: 10,
});
expect(result).toBe(0);
});
it('should handle Pflicht remission list type without predefined return quantity - #5269 Fix', () => {
const remissionItem = {
remainingQuantityInStock: 3,
} as any;
const result = getStockToRemit({
remissionItem,
remissionListType: RemissionListType.Pflicht,
availableStock: 10,
});
expect(result).toBe(7);
});
it('should handle Abteilung remission list type with return suggestion', () => {
const remissionItem = {
remainingQuantityInStock: 1,
@@ -59,4 +126,54 @@ describe('getStockToRemit', () => {
expect(result).toBe(8);
});
it('should handle Abteilung remission list type with zero return suggestion - #5280 Fix', () => {
const remissionItem = {
remainingQuantityInStock: 1,
returnItem: {
data: {
predefinedReturnQuantity: 0,
},
},
} as any;
const result = getStockToRemit({
remissionItem,
remissionListType: RemissionListType.Abteilung,
availableStock: 10,
});
expect(result).toBe(0);
});
it('should handle Abteilung remission list type without return suggestion - #5269 Fix', () => {
const remissionItem = {
remainingQuantityInStock: 2,
returnItem: {
data: {},
},
} as any;
const result = getStockToRemit({
remissionItem,
remissionListType: RemissionListType.Abteilung,
availableStock: 10,
});
expect(result).toBe(8);
});
it('should handle Abteilung remission list type with missing returnItem - #5269 Fix', () => {
const remissionItem = {
remainingQuantityInStock: 1,
} as any;
const result = getStockToRemit({
remissionItem,
remissionListType: RemissionListType.Abteilung,
availableStock: 10,
});
expect(result).toBe(9);
});
});

View File

@@ -24,7 +24,7 @@ export const getStockToRemit = ({
availableStock: number;
}): number => {
const remainingQuantityInStock = remissionItem?.remainingQuantityInStock;
let predefinedReturnQuantity: number | undefined = undefined; // #5269 Fix - predefinedReturnQuantity 0 soll genauso behandelt werden wie undefined bei Pflichtremission
let predefinedReturnQuantity: number | undefined = undefined;
if (remissionListType === RemissionListType.Pflicht) {
predefinedReturnQuantity = (remissionItem as ReturnItem)
@@ -62,10 +62,12 @@ export const calculateStockToRemit = ({
predefinedReturnQuantity?: number;
remainingQuantityInStock?: number;
}): number => {
if (!predefinedReturnQuantity) {
// #5269 Fix - Mache Näherungskalkulation, wenn kein predefinedReturnQuantity Wert vom Backend kommt
if (predefinedReturnQuantity === undefined) {
const stockToRemit = availableStock - (remainingQuantityInStock ?? 0);
return stockToRemit < 0 ? 0 : stockToRemit;
}
// #5280 Fix - Ansonsten nehme immer den kalkulierten Wert vom Backend her auch wenn dieser 0 ist
return predefinedReturnQuantity;
};