2021-12-01 13:08:00 +00:00
|
|
|
import config from '@/config';
|
2022-01-20 20:57:29 +00:00
|
|
|
import frappe from 'frappe';
|
|
|
|
import { DEFAULT_LOCALE } from 'frappe/utils/consts';
|
2020-01-01 08:11:57 +00:00
|
|
|
import countryList from '~/fixtures/countryInfo.json';
|
2021-12-01 13:08:00 +00:00
|
|
|
import generateTaxes from '../../../models/doctype/Tax/RegionalEntries';
|
|
|
|
import regionalModelUpdates from '../../../models/regionalModelUpdates';
|
2021-12-02 08:40:40 +00:00
|
|
|
import { callInitializeMoneyMaker } from '../../utils';
|
2020-01-01 08:11:57 +00:00
|
|
|
|
|
|
|
export default async function setupCompany(setupWizardValues) {
|
|
|
|
const {
|
|
|
|
companyLogo,
|
|
|
|
companyName,
|
|
|
|
country,
|
|
|
|
name,
|
|
|
|
email,
|
|
|
|
bankName,
|
|
|
|
fiscalYearStart,
|
2021-11-09 10:38:25 +00:00
|
|
|
fiscalYearEnd,
|
2020-01-01 08:11:57 +00:00
|
|
|
} = setupWizardValues;
|
|
|
|
|
|
|
|
const accountingSettings = frappe.AccountingSettings;
|
2021-12-02 08:40:40 +00:00
|
|
|
const currency = countryList[country]['currency'];
|
2021-12-23 12:37:50 +00:00
|
|
|
const locale = countryList[country]['locale'] ?? DEFAULT_LOCALE;
|
2021-12-02 08:40:40 +00:00
|
|
|
await callInitializeMoneyMaker(currency);
|
|
|
|
|
2020-01-01 08:11:57 +00:00
|
|
|
await accountingSettings.update({
|
|
|
|
companyName,
|
|
|
|
country,
|
|
|
|
fullname: name,
|
|
|
|
email,
|
|
|
|
bankName,
|
|
|
|
fiscalYearStart,
|
|
|
|
fiscalYearEnd,
|
2021-12-02 08:40:40 +00:00
|
|
|
currency,
|
2020-01-01 08:11:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const printSettings = await frappe.getSingle('PrintSettings');
|
|
|
|
printSettings.update({
|
|
|
|
logo: companyLogo,
|
|
|
|
companyName,
|
|
|
|
email,
|
2021-11-09 10:38:25 +00:00
|
|
|
displayLogo: companyLogo ? 1 : 0,
|
2020-01-01 08:11:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await setupGlobalCurrencies(countryList);
|
2021-11-10 08:59:25 +00:00
|
|
|
await setupChartOfAccounts(bankName, country);
|
2020-01-01 08:11:57 +00:00
|
|
|
await setupRegionalChanges(country);
|
2020-01-28 08:20:01 +00:00
|
|
|
updateCompanyNameInConfig();
|
2020-01-01 08:11:57 +00:00
|
|
|
|
|
|
|
await accountingSettings.update({ setupComplete: 1 });
|
|
|
|
frappe.AccountingSettings = accountingSettings;
|
2021-12-23 12:37:50 +00:00
|
|
|
|
|
|
|
(await frappe.getSingle('SystemSettings')).update({ locale });
|
2020-01-01 08:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function setupGlobalCurrencies(countries) {
|
|
|
|
const promises = [];
|
|
|
|
const queue = [];
|
|
|
|
for (let country of Object.values(countries)) {
|
|
|
|
const {
|
|
|
|
currency,
|
|
|
|
currency_fraction: fraction,
|
|
|
|
currency_fraction_units: fractionUnits,
|
|
|
|
smallest_currency_fraction_value: smallestValue,
|
|
|
|
currency_symbol: symbol,
|
|
|
|
} = country;
|
|
|
|
|
2021-11-09 10:38:25 +00:00
|
|
|
if (!currency || queue.includes(currency)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const docObject = {
|
|
|
|
doctype: 'Currency',
|
|
|
|
name: currency,
|
|
|
|
fraction,
|
|
|
|
fractionUnits,
|
|
|
|
smallestValue,
|
|
|
|
symbol,
|
|
|
|
};
|
|
|
|
|
2021-11-10 08:59:25 +00:00
|
|
|
const doc = checkAndCreateDoc(docObject);
|
|
|
|
if (doc) {
|
|
|
|
promises.push(doc);
|
2021-11-09 10:38:25 +00:00
|
|
|
queue.push(currency);
|
2020-01-01 08:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
2021-11-10 08:59:25 +00:00
|
|
|
async function setupChartOfAccounts(bankName, country) {
|
2020-01-01 08:11:57 +00:00
|
|
|
await frappe.call({
|
2021-11-09 10:38:25 +00:00
|
|
|
method: 'import-coa',
|
2020-01-01 08:11:57 +00:00
|
|
|
});
|
2021-11-10 08:59:25 +00:00
|
|
|
const parentAccount = await getBankAccountParentName(country);
|
2021-11-09 10:38:25 +00:00
|
|
|
const docObject = {
|
|
|
|
doctype: 'Account',
|
2020-01-01 08:11:57 +00:00
|
|
|
name: bankName,
|
|
|
|
rootType: 'Asset',
|
2021-11-10 08:59:25 +00:00
|
|
|
parentAccount,
|
2020-01-01 08:11:57 +00:00
|
|
|
accountType: 'Bank',
|
2021-11-09 10:38:25 +00:00
|
|
|
isGroup: 0,
|
|
|
|
};
|
2021-11-10 08:59:25 +00:00
|
|
|
await checkAndCreateDoc(docObject);
|
2020-01-01 08:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function setupRegionalChanges(country) {
|
2021-11-05 19:41:39 +00:00
|
|
|
await generateTaxes(country);
|
2021-12-01 13:08:00 +00:00
|
|
|
await regionalModelUpdates({ country });
|
|
|
|
await frappe.db.migrate();
|
2020-01-01 08:11:57 +00:00
|
|
|
}
|
2020-01-28 08:20:01 +00:00
|
|
|
|
|
|
|
function updateCompanyNameInConfig() {
|
|
|
|
let filePath = frappe.db.dbPath;
|
|
|
|
let files = config.get('files', []);
|
2021-11-09 10:38:25 +00:00
|
|
|
files.forEach((file) => {
|
2020-01-28 08:20:01 +00:00
|
|
|
if (file.filePath === filePath) {
|
|
|
|
file.companyName = frappe.AccountingSettings.companyName;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
config.set('files', files);
|
|
|
|
}
|
2021-11-09 10:38:25 +00:00
|
|
|
|
|
|
|
export async function checkIfExactRecordAbsent(docObject) {
|
|
|
|
const { doctype, name } = docObject;
|
|
|
|
const newDocObject = Object.assign({}, docObject);
|
|
|
|
delete newDocObject.doctype;
|
|
|
|
const rows = await frappe.db.knex(doctype).where({ name });
|
|
|
|
|
|
|
|
if (rows.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const storedDocObject = rows[0];
|
|
|
|
const matchList = Object.keys(newDocObject).map((key) => {
|
|
|
|
const newValue = newDocObject[key];
|
|
|
|
const storedValue = storedDocObject[key];
|
|
|
|
return newValue == storedValue; // Should not be type sensitive.
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!matchList.every(Boolean)) {
|
|
|
|
await frappe.db.knex(doctype).where({ name }).del();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-10 08:59:25 +00:00
|
|
|
|
|
|
|
async function checkAndCreateDoc(docObject) {
|
|
|
|
const canCreate = await checkIfExactRecordAbsent(docObject);
|
|
|
|
if (!canCreate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const doc = await frappe.newDoc(docObject);
|
|
|
|
return doc.insert();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getBankAccountParentName(country) {
|
|
|
|
const parentBankAccount = await frappe.db
|
|
|
|
.knex('Account')
|
|
|
|
.where({ isGroup: true, accountType: 'Bank' });
|
|
|
|
|
|
|
|
if (parentBankAccount.length === 0) {
|
|
|
|
// This should not happen if the fixtures are correct.
|
|
|
|
return 'Bank Accounts';
|
|
|
|
} else if (parentBankAccount.length > 1) {
|
|
|
|
switch (country) {
|
|
|
|
case 'Indonesia':
|
|
|
|
return 'Bank Rupiah - 1121.000';
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parentBankAccount[0].name;
|
|
|
|
}
|