mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
add server to monorepo.
This commit is contained in:
56
packages/server/src/services/Ledger/LedgerRepository.ts
Normal file
56
packages/server/src/services/Ledger/LedgerRepository.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { Service } from 'typedi';
|
||||
import { omit } from 'lodash';
|
||||
import JournalPoster from '@/services/Accounting/JournalPoster';
|
||||
import JournalEntry from '@/services/Accounting/JournalEntry';
|
||||
import Knex from 'knex';
|
||||
import { ILedgerEntry } from '@/interfaces';
|
||||
|
||||
@Service()
|
||||
export default class LedgerRepository {
|
||||
/**
|
||||
*
|
||||
* @param {number} tenantId
|
||||
* @param {ILedgerEntry[]} ledgerEntries
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public saveLedgerEntries = async (
|
||||
tenantId: number,
|
||||
ledgerEntries: ILedgerEntry[],
|
||||
trx?: Knex.Transaction
|
||||
) => {
|
||||
const journal = new JournalPoster(tenantId, null, trx);
|
||||
|
||||
ledgerEntries.forEach((ledgerEntry) => {
|
||||
const entry = new JournalEntry({
|
||||
...omit(ledgerEntry, [
|
||||
'accountNormal',
|
||||
'referenceNo',
|
||||
'transactionId',
|
||||
'transactionType',
|
||||
]),
|
||||
contactId: ledgerEntry.contactId,
|
||||
account: ledgerEntry.accountId,
|
||||
referenceId: ledgerEntry.transactionId,
|
||||
referenceType: ledgerEntry.transactionType,
|
||||
referenceNumber: ledgerEntry.referenceNo,
|
||||
transactionNumber: ledgerEntry.transactionNumber,
|
||||
index: ledgerEntry.index,
|
||||
indexGroup: ledgerEntry.indexGroup,
|
||||
});
|
||||
|
||||
if (ledgerEntry.credit) {
|
||||
journal.credit(entry);
|
||||
}
|
||||
if (ledgerEntry.debit) {
|
||||
journal.debit(entry);
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.all([
|
||||
journal.deleteEntries(),
|
||||
journal.saveBalance(),
|
||||
journal.saveContactsBalance(),
|
||||
journal.saveEntries(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user