2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 02:49:03 +00:00

refactor: move ledgerPosting to models

- move getExchangeRate to helpers
- add git blame ignore
This commit is contained in:
18alantom 2022-04-19 11:35:39 +05:30
parent de7e5ba807
commit ae6a5e52f2
12 changed files with 63 additions and 82 deletions

2
.git-blame-ignore-revs Normal file
View File

@ -0,0 +1,2 @@
# Rename 'frappe' to 'fyo' outside src
32d282dc9c6f129807a1cf53eae47fc3602aa976

View File

@ -1,52 +0,0 @@
import { NotFoundError } from 'fyo/utils/errors';
import { DateTime } from 'luxon';
export async function getExchangeRate({
fromCurrency,
toCurrency,
date,
}: {
fromCurrency: string;
toCurrency: string;
date?: string;
}) {
if (!date) {
date = DateTime.local().toISODate();
}
if (!fromCurrency || !toCurrency) {
throw new NotFoundError(
'Please provide `fromCurrency` and `toCurrency` to get exchange rate.'
);
}
const cacheKey = `currencyExchangeRate:${date}:${fromCurrency}:${toCurrency}`;
let exchangeRate = 0;
if (localStorage) {
exchangeRate = parseFloat(
localStorage.getItem(cacheKey as string) as string
);
}
if (!exchangeRate) {
try {
const res = await fetch(
` https://api.vatcomply.com/rates?date=${date}&base=${fromCurrency}&symbols=${toCurrency}`
);
const data = await res.json();
exchangeRate = data.rates[toCurrency];
if (localStorage) {
localStorage.setItem(cacheKey, String(exchangeRate));
}
} catch (error) {
console.error(error);
throw new Error(
`Could not fetch exchange rate for ${fromCurrency} -> ${toCurrency}`
);
}
}
return exchangeRate;
}

View File

@ -1,20 +0,0 @@
import { t } from 'fyo';
export const ledgerLink = {
label: t`Ledger Entries`,
condition: (doc) => doc.submitted,
action: (doc, router) => {
router.push({
name: 'Report',
params: {
reportName: 'general-ledger',
defaultFilters: {
referenceType: doc.doctype,
referenceName: doc.name,
},
},
});
},
};
export default { ledgerLink };

View File

@ -1,9 +1,9 @@
import { LedgerPosting } from 'accounting/ledgerPosting';
import { DocValue } from 'fyo/core/types';
import Doc from 'fyo/model/doc';
import { DefaultMap, FiltersMap, FormulaMap } from 'fyo/model/types';
import { getExchangeRate } from 'models/helpers';
import { LedgerPosting } from 'models/ledgerPosting/ledgerPosting';
import Money from 'pesa/dist/types/src/money';
import { getExchangeRate } from '../../../accounting/exchangeRate';
import { Party } from '../Party/Party';
import { Payment } from '../Payment/Payment';
import { Tax } from '../Tax/Tax';

View File

@ -9,7 +9,7 @@ import {
import { DateTime } from 'luxon';
import { getLedgerLinkAction } from 'models/helpers';
import Money from 'pesa/dist/types/src/money';
import { LedgerPosting } from '../../../accounting/ledgerPosting';
import { LedgerPosting } from '../../ledgerPosting/ledgerPosting';
export class JournalEntry extends Doc {
accounts: Doc[] = [];

View File

@ -1,4 +1,4 @@
import { LedgerPosting } from 'accounting/ledgerPosting';
import { LedgerPosting } from 'models/ledgerPosting/ledgerPosting';
import { Fyo } from 'fyo';
import { DocValue } from 'fyo/core/types';
import Doc from 'fyo/model/doc';

View File

@ -1,4 +1,4 @@
import { LedgerPosting } from 'accounting/ledgerPosting';
import { LedgerPosting } from 'models/ledgerPosting/ledgerPosting';
import { Fyo } from 'fyo';
import { Action, ListViewSettings } from 'fyo/model/types';
import {

View File

@ -1,4 +1,4 @@
import { LedgerPosting } from 'accounting/ledgerPosting';
import { LedgerPosting } from 'models/ledgerPosting/ledgerPosting';
import { Fyo } from 'fyo';
import { Action, ListViewSettings } from 'fyo/model/types';
import {

0
models/exchangeRate.ts Normal file
View File

View File

@ -1,6 +1,8 @@
import { Fyo } from 'fyo';
import Doc from 'fyo/model/doc';
import { Action, ColumnConfig } from 'fyo/model/types';
import { NotFoundError } from 'fyo/utils/errors';
import { DateTime } from 'luxon';
import Money from 'pesa/dist/types/src/money';
import { Router } from 'vue-router';
import { InvoiceStatus } from './types';
@ -25,10 +27,7 @@ export function getLedgerLinkAction(fyo: Fyo): Action {
};
}
export function getTransactionActions(
schemaName: string,
fyo: Fyo
): Action[] {
export function getTransactionActions(schemaName: string, fyo: Fyo): Action[] {
return [
{
label: fyo.t`Make Payment`,
@ -121,3 +120,55 @@ export function getInvoiceStatus(doc: Doc) {
}
return status;
}
export async function getExchangeRate({
fromCurrency,
toCurrency,
date,
}: {
fromCurrency: string;
toCurrency: string;
date?: string;
}) {
if (!date) {
date = DateTime.local().toISODate();
}
if (!fromCurrency || !toCurrency) {
throw new NotFoundError(
'Please provide `fromCurrency` and `toCurrency` to get exchange rate.'
);
}
const cacheKey = `currencyExchangeRate:${date}:${fromCurrency}:${toCurrency}`;
let exchangeRate = 0;
if (localStorage) {
exchangeRate = parseFloat(
localStorage.getItem(cacheKey as string) as string
);
}
if (!exchangeRate && fetch) {
try {
const res = await fetch(
` https://api.vatcomply.com/rates?date=${date}&base=${fromCurrency}&symbols=${toCurrency}`
);
const data = await res.json();
exchangeRate = data.rates[toCurrency];
if (localStorage) {
localStorage.setItem(cacheKey, String(exchangeRate));
}
} catch (error) {
console.error(error);
throw new Error(
`Could not fetch exchange rate for ${fromCurrency} -> ${toCurrency}`
);
}
} else {
exchangeRate = 1;
}
return exchangeRate;
}