From a0aaa8c52b2c51eb01e2016c958a7a53465e9d2a Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Wed, 10 Aug 2022 12:16:59 +0530 Subject: [PATCH] fix: set Round Off if not set --- src/setup/setupInstance.ts | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/setup/setupInstance.ts b/src/setup/setupInstance.ts index bbac2bfb..9944df63 100644 --- a/src/setup/setupInstance.ts +++ b/src/setup/setupInstance.ts @@ -207,26 +207,28 @@ export async function createDiscountAccount(fyo: Fyo) { } async function setDefaultAccounts(fyo: Fyo) { - const accountMap: Record = { - writeOffAccount: fyo.t`Write Off`, - roundOffAccount: fyo.t`Rounded Off`, - }; + await setDefaultAccount('writeOffAccount', fyo.t`Write Off`, fyo); + const isSet = await setDefaultAccount( + 'roundOffAccount', + fyo.t`Rounded Off`, + fyo + ); - for (const key in accountMap) { - const accountName = accountMap[key]; - const accountExists = await fyo.db.exists( - ModelNameEnum.Account, - accountName - ); - - if (!accountExists) { - continue; - } - - await fyo.singles.AccountingSettings!.setAndSync(key, accountName); + if (!isSet) { + await setDefaultAccount('roundOffAccount', fyo.t`Round Off`, fyo); } } +async function setDefaultAccount(key: string, accountName: string, fyo: Fyo) { + const accountExists = await fyo.db.exists(ModelNameEnum.Account, accountName); + if (!accountExists) { + return false; + } + + await fyo.singles.AccountingSettings!.setAndSync(key, accountName); + return true; +} + async function completeSetup(companyName: string, fyo: Fyo) { await fyo.singles.AccountingSettings!.setAndSync('setupComplete', true); }