Skip to content

Commit

Permalink
refactor: shift pesa init with currency to books
Browse files Browse the repository at this point in the history
  • Loading branch information
18alantom committed Dec 2, 2021
1 parent 14ce1e8 commit fb84a33
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import regionalModelUpdates from '../models/regionalModelUpdates';
import postStart from '../server/postStart';
import { IPC_ACTIONS } from './messages';
import migrate from './migrate';
import { callInitializeMoneyMaker } from './utils';

export async function createNewDatabase() {
const options = {
Expand Down Expand Up @@ -66,6 +67,8 @@ export async function connectToLocalDatabase(filePath) {
return false;
}

await callInitializeMoneyMaker();

try {
await runRegionalModelUpdates();
} catch (error) {
Expand Down
6 changes: 5 additions & 1 deletion src/pages/SetupWizard/setupCompany.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -25,7 +29,7 @@ export default async function setupCompany(setupWizardValues) {
bankName,
fiscalYearStart,
fiscalYearEnd,
currency: countryList[country]['currency'],
currency,
});

const printSettings = await frappe.getSingle('PrintSettings');
Expand Down
36 changes: 36 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,39 @@ export function fuzzyMatch(keyword, candidate) {
export function openSettings(tab) {
routeTo({ path: '/settings', query: { tab } });
}

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);
}

0 comments on commit fb84a33

Please sign in to comment.