From ae6a5e52f2cd22703ec59ab29364bbdf5c6d27a4 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Tue, 19 Apr 2022 11:35:39 +0530 Subject: [PATCH] refactor: move ledgerPosting to models - move getExchangeRate to helpers - add git blame ignore --- .git-blame-ignore-revs | 2 + accounting/exchangeRate.ts | 52 ---------------- accounting/utils.js | 20 ------- models/baseModels/Invoice/Invoice.ts | 4 +- .../baseModels/JournalEntry/JournalEntry.ts | 2 +- models/baseModels/Payment/Payment.ts | 2 +- .../PurchaseInvoice/PurchaseInvoice.ts | 2 +- .../baseModels/SalesInvoice/SalesInvoice.ts | 2 +- models/exchangeRate.ts | 0 models/helpers.ts | 59 +++++++++++++++++-- .../ledgerPosting}/ledgerPosting.ts | 0 {accounting => models/ledgerPosting}/types.ts | 0 12 files changed, 63 insertions(+), 82 deletions(-) create mode 100644 .git-blame-ignore-revs delete mode 100644 accounting/exchangeRate.ts delete mode 100644 accounting/utils.js create mode 100644 models/exchangeRate.ts rename {accounting => models/ledgerPosting}/ledgerPosting.ts (100%) rename {accounting => models/ledgerPosting}/types.ts (100%) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 00000000..21fb8066 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,2 @@ +# Rename 'frappe' to 'fyo' outside src +32d282dc9c6f129807a1cf53eae47fc3602aa976 \ No newline at end of file diff --git a/accounting/exchangeRate.ts b/accounting/exchangeRate.ts deleted file mode 100644 index 445af0a2..00000000 --- a/accounting/exchangeRate.ts +++ /dev/null @@ -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; -} diff --git a/accounting/utils.js b/accounting/utils.js deleted file mode 100644 index 091fd8bf..00000000 --- a/accounting/utils.js +++ /dev/null @@ -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 }; diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 8eed94b4..83c5b522 100644 --- a/models/baseModels/Invoice/Invoice.ts +++ b/models/baseModels/Invoice/Invoice.ts @@ -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'; diff --git a/models/baseModels/JournalEntry/JournalEntry.ts b/models/baseModels/JournalEntry/JournalEntry.ts index 04621747..2f11b019 100644 --- a/models/baseModels/JournalEntry/JournalEntry.ts +++ b/models/baseModels/JournalEntry/JournalEntry.ts @@ -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[] = []; diff --git a/models/baseModels/Payment/Payment.ts b/models/baseModels/Payment/Payment.ts index e8f89dfa..ef3788b6 100644 --- a/models/baseModels/Payment/Payment.ts +++ b/models/baseModels/Payment/Payment.ts @@ -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'; diff --git a/models/baseModels/PurchaseInvoice/PurchaseInvoice.ts b/models/baseModels/PurchaseInvoice/PurchaseInvoice.ts index 1cc09a10..f8489f99 100644 --- a/models/baseModels/PurchaseInvoice/PurchaseInvoice.ts +++ b/models/baseModels/PurchaseInvoice/PurchaseInvoice.ts @@ -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 { diff --git a/models/baseModels/SalesInvoice/SalesInvoice.ts b/models/baseModels/SalesInvoice/SalesInvoice.ts index 4c7d66b7..aba42e44 100644 --- a/models/baseModels/SalesInvoice/SalesInvoice.ts +++ b/models/baseModels/SalesInvoice/SalesInvoice.ts @@ -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 { diff --git a/models/exchangeRate.ts b/models/exchangeRate.ts new file mode 100644 index 00000000..e69de29b diff --git a/models/helpers.ts b/models/helpers.ts index d3315fed..7d7a7154 100644 --- a/models/helpers.ts +++ b/models/helpers.ts @@ -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; +} diff --git a/accounting/ledgerPosting.ts b/models/ledgerPosting/ledgerPosting.ts similarity index 100% rename from accounting/ledgerPosting.ts rename to models/ledgerPosting/ledgerPosting.ts diff --git a/accounting/types.ts b/models/ledgerPosting/types.ts similarity index 100% rename from accounting/types.ts rename to models/ledgerPosting/types.ts