This commit is contained in:
Ahmed Bouhuolia
2025-01-15 15:28:39 +02:00
parent 936800600b
commit 7bcd578c11
11 changed files with 894 additions and 1000 deletions

View File

@@ -1,4 +1,7 @@
import { chain } from 'lodash';
import { pick } from 'lodash';
import { IItemEntryTransactionType } from '../TransactionItemEntry/ItemEntry.types';
import { TInventoryTransactionDirection } from './types/InventoryCost.types';
/**
* Grpups by transaction type and id the inventory transactions.
@@ -6,10 +9,48 @@ import { chain } from 'lodash';
* @returns
*/
export function groupInventoryTransactionsByTypeId(
transactions: { transactionType: string; transactionId: number }[]
transactions: { transactionType: string; transactionId: number }[],
): { transactionType: string; transactionId: number }[][] {
return chain(transactions)
.groupBy((t) => `${t.transactionType}-${t.transactionId}`)
.values()
.value();
}
/**
* Transforms the items entries to inventory transactions.
*/
export function transformItemEntriesToInventory(transaction: {
transactionId: number;
transactionType: IItemEntryTransactionType;
transactionNumber?: string;
exchangeRate?: number;
warehouseId: number | null;
date: Date | string;
direction: TInventoryTransactionDirection;
entries: IItemEntry[];
createdAt: Date;
}): IInventoryTransaction[] {
const exchangeRate = transaction.exchangeRate || 1;
return transaction.entries.map((entry: IItemEntry) => ({
...pick(entry, ['itemId', 'quantity']),
rate: entry.rate * exchangeRate,
transactionType: transaction.transactionType,
transactionId: transaction.transactionId,
direction: transaction.direction,
date: transaction.date,
entryId: entry.id,
createdAt: transaction.createdAt,
costAccountId: entry.costAccountId,
warehouseId: entry.warehouseId || transaction.warehouseId,
meta: {
transactionNumber: transaction.transactionNumber,
description: entry.description,
},
}));
}