mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-28 22:42:11 +01:00
Merged PR 1900: feat(remission): add getStockToRemit helper and improve stock calculation logic
feat(remission): add getStockToRemit helper and improve stock calculation logic Add new getStockToRemit helper function that handles different remission list types (Pflicht and Abteilung) for calculating stock to remit. Refactor existing logic to use the centralized helper instead of duplicated calculation code. Changes: - Add getStockToRemit function to handle RemissionListType-specific logic - Update calculateStockToRemit to use strict undefined check for predefinedReturnQuantity - Refactor RemissionListItemComponent to use getStockToRemit helper - Update RemissionListComponent to use getStockToRemit for consistent calculations - Add comprehensive test coverage for both helper functions This centralizes stock calculation logic and ensures consistent behavior across all remission components. Ref: #5252
This commit is contained in:
committed by
Andreas Schickinger
parent
b39abe630d
commit
244984b6cf
@@ -8,9 +8,8 @@ import {
|
||||
import { FormsModule, Validators } from '@angular/forms';
|
||||
import {
|
||||
calculateAvailableStock,
|
||||
calculateStockToRemit,
|
||||
calculateTargetStock,
|
||||
RemissionListType,
|
||||
getStockToRemit,
|
||||
RemissionStore,
|
||||
ReturnItem,
|
||||
ReturnSuggestion,
|
||||
@@ -122,44 +121,11 @@ export class RemissionListItemComponent {
|
||||
);
|
||||
|
||||
/**
|
||||
* Computes the predefined return quantity for the current item.
|
||||
* - For Abteilung (suggestion), uses the item's return item data or calculates based on stock.
|
||||
* - For Pflicht (item), uses the item's predefined quantity.
|
||||
* - Returns 0 if not available.
|
||||
*/
|
||||
predefinedReturnQuantity = computed(() => {
|
||||
const item = this.item();
|
||||
|
||||
// ReturnSuggestion
|
||||
if (this.remissionListType() === RemissionListType.Abteilung) {
|
||||
const predefinedReturnQuantity = (item as ReturnSuggestion)?.returnItem
|
||||
?.data?.predefinedReturnQuantity;
|
||||
return (
|
||||
predefinedReturnQuantity ??
|
||||
calculateStockToRemit({
|
||||
availableStock: this.availableStock(),
|
||||
remainingQuantityInStock: this.remainingQuantityInStock(),
|
||||
}) ??
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
// ReturnItem
|
||||
if (this.remissionListType() === RemissionListType.Pflicht) {
|
||||
return (item as ReturnItem)?.predefinedReturnQuantity ?? 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
|
||||
/**
|
||||
* Computes whether the item has a predefined return quantity.
|
||||
* Returns true if the predefined quantity is greater than 0.
|
||||
* Computes whether to display action buttons based on stock to remit and remission status.
|
||||
* @returns true if stock to remit is greater than 0 and remission has started
|
||||
*/
|
||||
displayActions = computed<boolean>(() => {
|
||||
return (
|
||||
this.predefinedReturnQuantity() > 0 && this.#store.remissionStarted()
|
||||
);
|
||||
return this.stockToRemit() > 0 && this.#store.remissionStarted();
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -182,16 +148,14 @@ export class RemissionListItemComponent {
|
||||
);
|
||||
|
||||
/**
|
||||
* Computes the stock to remit based on available stock, predefined return quantity,
|
||||
* and remaining quantity in stock.
|
||||
*
|
||||
* @returns The calculated stock to remit.
|
||||
* Computes the stock to remit based on the remission item and available stock.
|
||||
* Uses the getStockToRemit helper function.
|
||||
*/
|
||||
stockToRemit = computed(() =>
|
||||
calculateStockToRemit({
|
||||
getStockToRemit({
|
||||
remissionItem: this.item(),
|
||||
remissionListType: this.remissionListType(),
|
||||
availableStock: this.availableStock(),
|
||||
predefinedReturnQuantity: this.predefinedReturnQuantity(),
|
||||
remainingQuantityInStock: this.remainingQuantityInStock(),
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ import {
|
||||
RemissionItem,
|
||||
calculateAvailableStock,
|
||||
RemissionReturnReceiptService,
|
||||
getStockToRemit,
|
||||
} from '@isa/remission/data-access';
|
||||
import { injectDialog } from '@isa/ui/dialog';
|
||||
import { SearchItemToRemitDialogComponent } from '@isa/remission/shared/search-item-to-remit-dialog';
|
||||
@@ -366,6 +367,7 @@ export class RemissionListComponent {
|
||||
});
|
||||
});
|
||||
|
||||
// TODO: Improvement - In Separate Komponente zusammen mit Remi-Button Auslagern
|
||||
async remitItems() {
|
||||
if (this.remitItemsInProgress()) {
|
||||
return;
|
||||
@@ -373,6 +375,7 @@ export class RemissionListComponent {
|
||||
this.remitItemsInProgress.set(true);
|
||||
|
||||
try {
|
||||
const remissionListType = this.selectedRemissionListType();
|
||||
const selected = this.#store.selectedItems();
|
||||
const quantities = this.#store.selectedQuantity();
|
||||
|
||||
@@ -382,6 +385,13 @@ export class RemissionListComponent {
|
||||
const remissionItemIdNumber = Number(remissionItemId);
|
||||
const quantity = quantities[remissionItemIdNumber];
|
||||
const inStock = this.getAvailableStockForItem(item);
|
||||
const stockToRemit =
|
||||
quantity ??
|
||||
getStockToRemit({
|
||||
remissionItem: item,
|
||||
remissionListType,
|
||||
availableStock: inStock,
|
||||
});
|
||||
|
||||
if (returnId && receiptId) {
|
||||
await this.#remissionReturnReceiptService.remitItem({
|
||||
@@ -389,7 +399,7 @@ export class RemissionListComponent {
|
||||
addItem: {
|
||||
returnId,
|
||||
receiptId,
|
||||
quantity,
|
||||
quantity: stockToRemit,
|
||||
inStock,
|
||||
},
|
||||
type: this.selectedRemissionListType(),
|
||||
|
||||
Reference in New Issue
Block a user