mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
feat: Re-compute the given items cost job.
feat: Optimize the architecture.
This commit is contained in:
135
server/src/services/Accounting/JournalCommands.ts
Normal file
135
server/src/services/Accounting/JournalCommands.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import { sumBy, chain } from 'lodash';
|
||||
import JournalPoster from "./JournalPoster";
|
||||
import JournalEntry from "./JournalEntry";
|
||||
import { AccountTransaction } from '@/models';
|
||||
import { IInventoryTransaction } from '@/interfaces';
|
||||
import AccountsService from '../Accounts/AccountsService';
|
||||
import { IInventoryTransaction, IInventoryTransaction } from '../../interfaces';
|
||||
|
||||
interface IInventoryCostEntity {
|
||||
date: Date,
|
||||
|
||||
referenceType: string,
|
||||
referenceId: number,
|
||||
|
||||
costAccount: number,
|
||||
incomeAccount: number,
|
||||
inventoryAccount: number,
|
||||
|
||||
inventory: number,
|
||||
cost: number,
|
||||
income: number,
|
||||
};
|
||||
|
||||
export default class JournalCommands{
|
||||
journal: JournalPoster;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {JournalPoster} journal -
|
||||
*/
|
||||
constructor(journal: JournalPoster) {
|
||||
this.journal = journal;
|
||||
Object.assign(this, arguments[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes and revert accounts balance journal entries that associated
|
||||
* to the given inventory transactions.
|
||||
* @param {IInventoryTransaction[]} inventoryTransactions
|
||||
* @param {Journal} journal
|
||||
*/
|
||||
revertEntriesFromInventoryTransactions(inventoryTransactions: IInventoryTransaction[]) {
|
||||
const groupedInvTransactions = chain(inventoryTransactions)
|
||||
.groupBy((invTransaction: IInventoryTransaction) => invTransaction.transactionType)
|
||||
.map((groupedTrans: IInventoryTransaction[], transType: string) => [groupedTrans, transType])
|
||||
.value();
|
||||
|
||||
console.log(groupedInvTransactions);
|
||||
|
||||
return Promise.all(
|
||||
groupedInvTransactions.map(async (grouped: [IInventoryTransaction[], string]) => {
|
||||
const [invTransGroup, referenceType] = grouped;
|
||||
const referencesIds = invTransGroup.map((trans: IInventoryTransaction) => trans.transactionId);
|
||||
|
||||
const _transactions = await AccountTransaction.tenant()
|
||||
.query()
|
||||
.where('reference_type', referenceType)
|
||||
.whereIn('reference_id', referencesIds)
|
||||
.withGraphFetched('account.type');
|
||||
|
||||
console.log(_transactions, referencesIds);
|
||||
|
||||
if (_transactions.length > 0) {
|
||||
this.journal.loadEntries(_transactions);
|
||||
this.journal.removeEntries(_transactions.map((t: any) => t.id));
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} referenceType -
|
||||
* @param {number} referenceId -
|
||||
* @param {ISaleInvoice[]} sales -
|
||||
*/
|
||||
public async inventoryEntries(
|
||||
transactions: IInventoryCostEntity[],
|
||||
) {
|
||||
const receivableAccount = { id: 10 };
|
||||
const payableAccount = { id: 11 };
|
||||
|
||||
transactions.forEach((sale: IInventoryCostEntity) => {
|
||||
const commonEntry = {
|
||||
date: sale.date,
|
||||
referenceId: sale.referenceId,
|
||||
referenceType: sale.referenceType,
|
||||
};
|
||||
switch(sale.referenceType) {
|
||||
case 'Bill':
|
||||
const inventoryDebit: JournalEntry = new JournalEntry({
|
||||
...commonEntry,
|
||||
debit: sale.inventory,
|
||||
account: sale.inventoryAccount,
|
||||
});
|
||||
const payableEntry: JournalEntry = new JournalEntry({
|
||||
...commonEntry,
|
||||
credit: sale.inventory,
|
||||
account: payableAccount.id,
|
||||
});
|
||||
this.journal.debit(inventoryDebit);
|
||||
this.journal.credit(payableEntry);
|
||||
break;
|
||||
case 'SaleInvoice':
|
||||
const receivableEntry: JournalEntry = new JournalEntry({
|
||||
...commonEntry,
|
||||
debit: sale.income,
|
||||
account: receivableAccount.id,
|
||||
});
|
||||
const incomeEntry: JournalEntry = new JournalEntry({
|
||||
...commonEntry,
|
||||
credit: sale.income,
|
||||
account: sale.incomeAccount,
|
||||
});
|
||||
// Cost journal transaction.
|
||||
const costEntry: JournalEntry = new JournalEntry({
|
||||
...commonEntry,
|
||||
debit: sale.cost,
|
||||
account: sale.costAccount,
|
||||
});
|
||||
const inventoryCredit: JournalEntry = new JournalEntry({
|
||||
...commonEntry,
|
||||
credit: sale.cost,
|
||||
account: sale.inventoryAccount,
|
||||
});
|
||||
this.journal.debit(receivableEntry);
|
||||
this.journal.debit(costEntry);
|
||||
|
||||
this.journal.credit(incomeEntry);
|
||||
this.journal.credit(inventoryCredit);
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user