mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
Merge branch 'master' of https://github.com/abouolia/Bigcapital
This commit is contained in:
@@ -62,12 +62,7 @@ export default class ContactsService {
|
||||
* Converts contact DTO object to model object attributes to insert or update.
|
||||
* @param {IContactNewDTO | IContactEditDTO} contactDTO
|
||||
*/
|
||||
private transformContactObj(contactDTO: IContactNewDTO | IContactEditDTO) {
|
||||
const baseCurrency = 'USD';
|
||||
const currencyCode = typeof contactDTO.currencyCode !== 'undefined'
|
||||
? contactDTO.currencyCode
|
||||
: baseCurrency;
|
||||
|
||||
private commonTransformContactObj(contactDTO: IContactNewDTO | IContactEditDTO) {
|
||||
return {
|
||||
...omit(contactDTO, [
|
||||
'billingAddress1',
|
||||
@@ -79,10 +74,35 @@ export default class ContactsService {
|
||||
billing_address_2: contactDTO?.billingAddress2,
|
||||
shipping_address_1: contactDTO?.shippingAddress1,
|
||||
shipping_address_2: contactDTO?.shippingAddress2,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms contact new DTO object to model object to insert to the storage.
|
||||
* @param {IContactNewDTO} contactDTO
|
||||
*/
|
||||
private transformNewContactDTO(contactDTO: IContactNewDTO) {
|
||||
const baseCurrency = 'USD';
|
||||
const currencyCode = typeof contactDTO.currencyCode !== 'undefined'
|
||||
? contactDTO.currencyCode
|
||||
: baseCurrency;
|
||||
|
||||
return {
|
||||
...this.commonTransformContactObj(contactDTO),
|
||||
...(currencyCode ? ({ currencyCode }) : {}),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms contact edit DTO object to model object to update to the storage.
|
||||
* @param {IContactEditDTO} contactDTO
|
||||
*/
|
||||
private transformEditContactDTO(contactDTO: IContactEditDTO) {
|
||||
return {
|
||||
...this.commonTransformContactObj(contactDTO),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new contact on the storage.
|
||||
* @param {number} tenantId
|
||||
@@ -95,7 +115,7 @@ export default class ContactsService {
|
||||
contactService: TContactService
|
||||
) {
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
const contactObj = this.transformContactObj(contactDTO);
|
||||
const contactObj = this.transformNewContactDTO(contactDTO);
|
||||
|
||||
this.logger.info('[contacts] trying to insert contact to the storage.', {
|
||||
tenantId,
|
||||
@@ -126,7 +146,7 @@ export default class ContactsService {
|
||||
contactService: TContactService
|
||||
) {
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
const contactObj = this.transformContactObj(contactDTO);
|
||||
const contactObj = this.transformEditContactDTO(contactDTO);
|
||||
|
||||
// Retrieve the given contact by id or throw not found service error.
|
||||
const contact = await this.getContactByIdOrThrowError(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { isEmpty, get, last } from 'lodash';
|
||||
import { isEmpty, get, last, sumBy } from 'lodash';
|
||||
import {
|
||||
IGeneralLedgerSheetQuery,
|
||||
IGeneralLedgerSheetAccount,
|
||||
@@ -19,7 +19,6 @@ export default class GeneralLedgerSheet extends FinancialSheet {
|
||||
accounts: IAccount[];
|
||||
query: IGeneralLedgerSheetQuery;
|
||||
openingBalancesJournal: IJournalPoster;
|
||||
closingBalancesJournal: IJournalPoster;
|
||||
transactions: IJournalPoster;
|
||||
contactsMap: Map<number, IContact>;
|
||||
baseCurrency: string;
|
||||
@@ -39,7 +38,7 @@ export default class GeneralLedgerSheet extends FinancialSheet {
|
||||
contactsByIdMap: Map<number, IContact>,
|
||||
transactions: IJournalPoster,
|
||||
openingBalancesJournal: IJournalPoster,
|
||||
closingBalancesJournal: IJournalPoster,
|
||||
|
||||
baseCurrency: string
|
||||
) {
|
||||
super();
|
||||
@@ -51,7 +50,6 @@ export default class GeneralLedgerSheet extends FinancialSheet {
|
||||
this.contactsMap = contactsByIdMap;
|
||||
this.transactions = transactions;
|
||||
this.openingBalancesJournal = openingBalancesJournal;
|
||||
this.closingBalancesJournal = closingBalancesJournal;
|
||||
this.baseCurrency = baseCurrency;
|
||||
}
|
||||
|
||||
@@ -162,9 +160,10 @@ export default class GeneralLedgerSheet extends FinancialSheet {
|
||||
* @return {IGeneralLedgerSheetAccountBalance}
|
||||
*/
|
||||
private accountClosingBalance(
|
||||
account: IAccount
|
||||
openingBalance: number,
|
||||
transactions: IGeneralLedgerSheetAccountTransaction[]
|
||||
): IGeneralLedgerSheetAccountBalance {
|
||||
const amount = this.closingBalancesJournal.getAccountBalance(account.id);
|
||||
const amount = this.calcClosingBalance(openingBalance, transactions);
|
||||
const formattedAmount = this.formatTotalNumber(amount);
|
||||
const currencyCode = this.baseCurrency;
|
||||
const date = this.query.toDate;
|
||||
@@ -172,6 +171,13 @@ export default class GeneralLedgerSheet extends FinancialSheet {
|
||||
return { amount, formattedAmount, currencyCode, date };
|
||||
}
|
||||
|
||||
private calcClosingBalance(
|
||||
openingBalance: number,
|
||||
transactions: IGeneralLedgerSheetAccountTransaction[]
|
||||
) {
|
||||
return openingBalance + sumBy(transactions, (trans) => trans.amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retreive general ledger accounts sections.
|
||||
* @param {IAccount} account
|
||||
@@ -179,7 +185,15 @@ export default class GeneralLedgerSheet extends FinancialSheet {
|
||||
*/
|
||||
private accountMapper(account: IAccount): IGeneralLedgerSheetAccount {
|
||||
const openingBalance = this.accountOpeningBalance(account);
|
||||
const closingBalance = this.accountClosingBalance(account);
|
||||
|
||||
const transactions = this.accountTransactionsMapper(
|
||||
account,
|
||||
openingBalance.amount
|
||||
);
|
||||
const closingBalance = this.accountClosingBalance(
|
||||
openingBalance.amount,
|
||||
transactions
|
||||
);
|
||||
|
||||
return {
|
||||
id: account.id,
|
||||
@@ -188,10 +202,7 @@ export default class GeneralLedgerSheet extends FinancialSheet {
|
||||
index: account.index,
|
||||
parentAccountId: account.parentAccountId,
|
||||
openingBalance,
|
||||
transactions: this.accountTransactionsMapper(
|
||||
account,
|
||||
openingBalance.amount
|
||||
),
|
||||
transactions,
|
||||
closingBalance,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -134,12 +134,7 @@ export default class GeneralLedgerService {
|
||||
});
|
||||
// Retreive opening balance credit/debit sumation.
|
||||
const openingBalanceTrans = await transactionsRepository.journal({
|
||||
toDate: filter.fromDate,
|
||||
sumationCreditDebit: true,
|
||||
});
|
||||
// Retreive closing balance credit/debit sumation.
|
||||
const closingBalanceTrans = await transactionsRepository.journal({
|
||||
toDate: filter.toDate,
|
||||
toDate: moment(filter.fromDate).subtract(1, 'day'),
|
||||
sumationCreditDebit: true,
|
||||
});
|
||||
// Transform array transactions to journal collection.
|
||||
@@ -154,12 +149,6 @@ export default class GeneralLedgerService {
|
||||
tenantId,
|
||||
accountsGraph
|
||||
);
|
||||
// Accounts closing transactions.
|
||||
const closingTransJournal = Journal.fromTransactions(
|
||||
closingBalanceTrans,
|
||||
tenantId,
|
||||
accountsGraph
|
||||
);
|
||||
// General ledger report instance.
|
||||
const generalLedgerInstance = new GeneralLedgerSheet(
|
||||
tenantId,
|
||||
@@ -168,7 +157,6 @@ export default class GeneralLedgerService {
|
||||
contactsByIdMap,
|
||||
transactionsJournal,
|
||||
openingTransJournal,
|
||||
closingTransJournal,
|
||||
baseCurrency
|
||||
);
|
||||
// Retrieve general ledger report data.
|
||||
|
||||
Reference in New Issue
Block a user