2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 19:09:01 +00:00

fix: add patch to set/unset roundoff

This commit is contained in:
18alantom 2022-08-16 17:02:35 +05:30
parent 8dc27249b4
commit 5185820a32
2 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,55 @@
import { ModelNameEnum } from '../../models/types';
import { DatabaseManager } from '../database/manager';
const FIELDNAME = 'roundOffAccount';
async function execute(dm: DatabaseManager) {
const accounts = await dm.db!.getSingleValues(FIELDNAME);
if (!accounts.length) {
await testAndSetRoundOffAccount(dm);
}
await dm.db!.delete(ModelNameEnum.AccountingSettings, FIELDNAME);
let isSet = false;
for (const { parent, value } of accounts) {
if (parent !== ModelNameEnum.AccountingSettings) {
continue;
}
isSet = await setRoundOffAccountIfExists(value as string, dm);
if (isSet) {
break;
}
}
if (!isSet) {
await testAndSetRoundOffAccount(dm);
}
}
async function testAndSetRoundOffAccount(dm: DatabaseManager) {
const isSet = await setRoundOffAccountIfExists('Round Off', dm);
if (!isSet) {
await setRoundOffAccountIfExists('Rounded Off', dm);
}
return;
}
async function setRoundOffAccountIfExists(
roundOffAccount: string,
dm: DatabaseManager
) {
const exists = await dm.db!.exists(ModelNameEnum.Account, roundOffAccount);
if (!exists) {
return false;
}
await dm.db!.insert(ModelNameEnum.AccountingSettings, {
roundOffAccount,
});
return true;
}
export default { execute, beforeMigrate: true };

View File

@ -1,5 +1,6 @@
import { Patch } from '../database/types';
import addUOMs from './addUOMs';
import fixRoundOffAccount from './fixRoundOffAccount';
import testPatch from './testPatch';
import updateSchemas from './updateSchemas';
@ -16,4 +17,9 @@ export default [
version: '0.6.0-beta.0',
patch: addUOMs,
},
{
name: 'fixRoundOffAccount',
version: '0.6.3-beta.0',
patch: fixRoundOffAccount
},
] as Patch[];