mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 15:37:33 +08:00
transaction overview supports multi currencies
This commit is contained in:
+54
-3
@@ -1,12 +1,15 @@
|
||||
import services from '../lib/services.js';
|
||||
import logger from '../lib/logger.js';
|
||||
import utils from '../lib/utils.js';
|
||||
|
||||
import { getExchangedAmount } from "./exchangeRates.js";
|
||||
|
||||
import {
|
||||
LOAD_TRANSACTION_OVERVIEW,
|
||||
UPDATE_TRANSACTION_OVERVIEW_INVALID_STATE,
|
||||
} from './mutations.js';
|
||||
|
||||
export function loadTransactionOverview(context, { dateRange, force }) {
|
||||
export function loadTransactionOverview(context, { defaultCurrency, dateRange, force }) {
|
||||
if (!force && !context.state.transactionOverviewStateInvalid) {
|
||||
return new Promise((resolve) => {
|
||||
resolve(context.state.transactionOverview);
|
||||
@@ -27,13 +30,61 @@ export function loadTransactionOverview(context, { dateRange, force }) {
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(LOAD_TRANSACTION_OVERVIEW, data.result);
|
||||
const overview = data.result;
|
||||
|
||||
for (let field in overview) {
|
||||
if (!Object.prototype.hasOwnProperty.call(overview, field)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const item = overview[field];
|
||||
|
||||
if (!item.amounts || !item.amounts.length) {
|
||||
item.amounts = [];
|
||||
}
|
||||
|
||||
let totalIncomeAmount = 0;
|
||||
let totalExpenseAmount = 0;
|
||||
let hasUnCalculatedTotalIncome = false;
|
||||
let hasUnCalculatedTotalExpense = false;
|
||||
|
||||
for (let i = 0; i < item.amounts.length; i++) {
|
||||
const amount = item.amounts[i];
|
||||
|
||||
if (amount.currency !== defaultCurrency) {
|
||||
const incomeAmount = getExchangedAmount(context.state)(amount.incomeAmount, amount.currency, defaultCurrency);
|
||||
const expenseAmount = getExchangedAmount(context.state)(amount.expenseAmount, amount.currency, defaultCurrency);
|
||||
|
||||
if (utils.isNumber(incomeAmount)) {
|
||||
totalIncomeAmount += Math.floor(incomeAmount);
|
||||
} else {
|
||||
hasUnCalculatedTotalIncome = true;
|
||||
}
|
||||
|
||||
if (utils.isNumber(expenseAmount)) {
|
||||
totalExpenseAmount += Math.floor(expenseAmount);
|
||||
} else {
|
||||
hasUnCalculatedTotalExpense = true;
|
||||
}
|
||||
} else {
|
||||
totalIncomeAmount += amount.incomeAmount;
|
||||
totalExpenseAmount += amount.expenseAmount;
|
||||
}
|
||||
}
|
||||
|
||||
item.incomeAmount = totalIncomeAmount;
|
||||
item.expenseAmount = totalExpenseAmount;
|
||||
item.incompleteIncomeAmount = hasUnCalculatedTotalIncome;
|
||||
item.incompleteExpenseAmount = hasUnCalculatedTotalExpense;
|
||||
}
|
||||
|
||||
context.commit(LOAD_TRANSACTION_OVERVIEW, overview);
|
||||
|
||||
if (context.state.transactionOverviewStateInvalid) {
|
||||
context.commit(UPDATE_TRANSACTION_OVERVIEW_INVALID_STATE, false);
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
resolve(overview);
|
||||
}).catch(error => {
|
||||
if (force) {
|
||||
logger.error('failed to force load transaction overview', error);
|
||||
|
||||
Reference in New Issue
Block a user