diff --git a/src/initialization.js b/src/initialization.js index e2eb4772..e61be3c2 100644 --- a/src/initialization.js +++ b/src/initialization.js @@ -7,6 +7,7 @@ import postStart from '../server/postStart'; import { DB_CONN_FAILURE } from './messages'; import migrate from './migrate'; import { getSavePath } from './utils'; +import { callInitializeMoneyMaker } from './utils'; export async function createNewDatabase() { const { canceled, filePath } = await getSavePath('books', 'db'); @@ -49,6 +50,8 @@ export async function connectToLocalDatabase(filePath) { return { connectionSuccess: false, reason: DB_CONN_FAILURE.CANT_CONNECT }; } + await callInitializeMoneyMaker(); + try { await runRegionalModelUpdates(); } catch (error) { diff --git a/src/pages/SetupWizard/setupCompany.js b/src/pages/SetupWizard/setupCompany.js index 42ce9332..80c8cfc7 100644 --- a/src/pages/SetupWizard/setupCompany.js +++ b/src/pages/SetupWizard/setupCompany.js @@ -3,6 +3,7 @@ import frappe from 'frappejs'; import countryList from '~/fixtures/countryInfo.json'; import generateTaxes from '../../../models/doctype/Tax/RegionalEntries'; import regionalModelUpdates from '../../../models/regionalModelUpdates'; +import { callInitializeMoneyMaker } from '../../utils'; export default async function setupCompany(setupWizardValues) { const { @@ -17,6 +18,9 @@ export default async function setupCompany(setupWizardValues) { } = setupWizardValues; const accountingSettings = frappe.AccountingSettings; + const currency = countryList[country]['currency']; + await callInitializeMoneyMaker(currency); + await accountingSettings.update({ companyName, country, @@ -25,7 +29,7 @@ export default async function setupCompany(setupWizardValues) { bankName, fiscalYearStart, fiscalYearEnd, - currency: countryList[country]['currency'], + currency, }); const printSettings = await frappe.getSingle('PrintSettings'); diff --git a/src/utils.js b/src/utils.js index baa28c2c..3219a23f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -351,3 +351,39 @@ export function titleCase(phrase) { }) .join(' '); } + +export async function getIsSetupComplete() { + try { + const { setupComplete } = await frappe.getSingle('AccountingSettings'); + return !!setupComplete; + } catch { + return false; + } +} + +export async function getCurrency() { + let currency = frappe?.AccoutingSettings?.currency ?? undefined; + + if (!currency) { + try { + currency = ( + await frappe.db.getSingleValues({ + fieldname: 'currency', + parent: 'AccountingSettings', + }) + )[0].value; + } catch (err) { + currency = undefined; + } + } + + return currency; +} + +export async function callInitializeMoneyMaker(currency) { + currency ??= await getCurrency(); + if (!currency && frappe.pesa) { + return; + } + await frappe.initializeMoneyMaker(currency); +}