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