2022-09-07 17:28:41 +02:00
|
|
|
import { ProductCategory } from '../models/product-category';
|
2022-09-12 15:55:41 +02:00
|
|
|
import { stockMovementInReasons, stockMovementOutReasons, StockMovementReason } from '../models/product';
|
2022-09-07 17:28:41 +02:00
|
|
|
|
|
|
|
export default class ProductLib {
|
|
|
|
/**
|
|
|
|
* Map product categories by position
|
|
|
|
* @param categories unsorted categories, as returned by the API
|
|
|
|
*/
|
2022-09-08 17:51:48 +02:00
|
|
|
static sortCategories = (categories: Array<ProductCategory>): Array<ProductCategory> => {
|
2022-09-07 17:28:41 +02:00
|
|
|
const sortedCategories = categories
|
|
|
|
.filter(c => !c.parent_id)
|
|
|
|
.sort((a, b) => a.position - b.position);
|
|
|
|
const childrenCategories = categories
|
|
|
|
.filter(c => typeof c.parent_id === 'number')
|
|
|
|
.sort((a, b) => b.position - a.position);
|
|
|
|
childrenCategories.forEach(c => {
|
|
|
|
const parentIndex = sortedCategories.findIndex(i => i.id === c.parent_id);
|
|
|
|
sortedCategories.splice(parentIndex + 1, 0, c);
|
|
|
|
});
|
|
|
|
return sortedCategories;
|
|
|
|
};
|
2022-09-08 17:51:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the translation key associated with the given reason
|
|
|
|
*/
|
|
|
|
static stockMovementReasonTrKey = (reason: StockMovementReason): string => {
|
|
|
|
return `app.admin.store.stock_movement_reason.${reason}`;
|
|
|
|
};
|
2022-09-12 15:55:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given stock movement is of type 'in' or 'out'
|
|
|
|
*/
|
|
|
|
static stockMovementType = (reason: StockMovementReason): 'in' | 'out' => {
|
|
|
|
if ((stockMovementInReasons as readonly StockMovementReason[]).includes(reason)) return 'in';
|
|
|
|
if ((stockMovementOutReasons as readonly StockMovementReason[]).includes(reason)) return 'out';
|
|
|
|
|
|
|
|
throw new Error(`Unexpected stock movement reason: ${reason}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the given quantity, prefixed by its addition operator (- or +)
|
|
|
|
*/
|
|
|
|
static absoluteStockMovement = (quantity: number, reason: StockMovementReason): string => {
|
|
|
|
if (ProductLib.stockMovementType(reason) === 'in') {
|
|
|
|
return `+${quantity}`;
|
|
|
|
} else {
|
|
|
|
return `-${quantity}`;
|
|
|
|
}
|
|
|
|
};
|
2022-09-07 17:28:41 +02:00
|
|
|
}
|