2018-10-20 12:32:01 +00:00
|
|
|
// frappejs imports
|
|
|
|
import frappe from 'frappejs';
|
2018-10-22 18:02:47 +00:00
|
|
|
import path from 'path';
|
2018-10-20 12:32:01 +00:00
|
|
|
import SQLite from 'frappejs/backends/sqlite';
|
|
|
|
import common from 'frappejs/common';
|
|
|
|
import coreModels from 'frappejs/models';
|
|
|
|
import models from '../models';
|
2018-10-22 18:02:47 +00:00
|
|
|
import postStart from '../server/postStart';
|
2019-07-16 07:04:51 +00:00
|
|
|
import { getSettings, saveSettings } from '../electron/settings';
|
2018-10-22 18:02:47 +00:00
|
|
|
|
|
|
|
// vue imports
|
|
|
|
import Vue from 'vue';
|
|
|
|
import App from './App';
|
|
|
|
import router from './router';
|
|
|
|
import frappeVue from 'frappejs/ui/plugins/frappeVue';
|
2018-10-22 21:29:07 +00:00
|
|
|
import Toasted from 'vue-toasted';
|
2018-10-21 12:49:33 +00:00
|
|
|
|
|
|
|
(async () => {
|
|
|
|
frappe.isServer = true;
|
2018-10-22 18:02:47 +00:00
|
|
|
frappe.isElectron = true;
|
2018-10-21 12:49:33 +00:00
|
|
|
frappe.init();
|
|
|
|
frappe.registerLibs(common);
|
|
|
|
frappe.registerModels(coreModels);
|
|
|
|
frappe.registerModels(models);
|
|
|
|
frappe.fetch = window.fetch.bind();
|
2019-07-16 07:04:51 +00:00
|
|
|
frappe.events.on('connect-database', async filepath => {
|
2018-10-23 12:42:36 +00:00
|
|
|
await connectToLocalDatabase(filepath);
|
2019-02-18 05:42:04 +00:00
|
|
|
|
2019-08-20 08:57:27 +00:00
|
|
|
const { completed } = await frappe.getSingle('SetupWizard');
|
|
|
|
|
|
|
|
if (!completed) {
|
|
|
|
frappe.events.trigger('show-setup-wizard');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-16 07:04:51 +00:00
|
|
|
const { country } = await frappe.getSingle('AccountingSettings');
|
2019-02-18 05:42:04 +00:00
|
|
|
|
2019-07-16 07:04:51 +00:00
|
|
|
if (country === 'India') {
|
|
|
|
frappe.models.Party = require('../models/doctype/Party/RegionalChanges.js');
|
2019-02-18 05:42:04 +00:00
|
|
|
} else {
|
2019-07-16 07:04:51 +00:00
|
|
|
frappe.models.Party = require('../models/doctype/Party/Party.js');
|
2019-02-18 05:42:04 +00:00
|
|
|
}
|
2019-09-03 09:43:16 +00:00
|
|
|
|
2018-10-23 12:42:36 +00:00
|
|
|
frappe.events.trigger('show-desk');
|
|
|
|
});
|
|
|
|
|
2019-07-16 07:04:51 +00:00
|
|
|
frappe.events.on('DatabaseSelector:file-selected', async filepath => {
|
2018-10-23 12:42:36 +00:00
|
|
|
await connectToLocalDatabase(filepath);
|
2018-10-22 18:02:47 +00:00
|
|
|
|
2018-10-23 12:42:36 +00:00
|
|
|
localStorage.dbPath = filepath;
|
|
|
|
|
2019-07-16 07:04:51 +00:00
|
|
|
const { companyName } = await frappe.getSingle('AccountingSettings');
|
|
|
|
if (!companyName) {
|
2018-10-23 12:42:36 +00:00
|
|
|
frappe.events.trigger('show-setup-wizard');
|
|
|
|
} else {
|
|
|
|
frappe.events.trigger('show-desk');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-07-16 07:04:51 +00:00
|
|
|
frappe.events.on('SetupWizard:setup-complete', async setupWizardValues => {
|
|
|
|
const countryList = require('../fixtures/countryInfo.json');
|
2018-10-22 18:02:47 +00:00
|
|
|
const {
|
|
|
|
companyName,
|
|
|
|
country,
|
|
|
|
name,
|
|
|
|
email,
|
|
|
|
bankName,
|
|
|
|
fiscalYearStart,
|
|
|
|
fiscalYearEnd
|
|
|
|
} = setupWizardValues;
|
|
|
|
|
|
|
|
const doc = await frappe.getSingle('AccountingSettings');
|
|
|
|
await doc.set({
|
|
|
|
companyName,
|
|
|
|
country,
|
|
|
|
fullname: name,
|
|
|
|
email,
|
|
|
|
bankName,
|
|
|
|
fiscalYearStart,
|
2019-07-16 07:04:51 +00:00
|
|
|
fiscalYearEnd,
|
2019-09-03 09:43:16 +00:00
|
|
|
numberFormat: countryList[country]['number_format'],
|
|
|
|
symbol: countryList[country]['currency_symbol'],
|
2019-07-16 07:04:51 +00:00
|
|
|
currency: countryList[country]['currency']
|
2018-10-21 12:49:33 +00:00
|
|
|
});
|
|
|
|
|
2018-10-22 18:02:47 +00:00
|
|
|
await doc.update();
|
2019-08-14 07:43:49 +00:00
|
|
|
|
|
|
|
const systemSettings = await frappe.getSingle('SystemSettings');
|
|
|
|
await systemSettings.set({
|
|
|
|
dateFormat: countryList[country]['date_format'] || 'yyyy-MM-dd'
|
|
|
|
});
|
|
|
|
await systemSettings.update();
|
|
|
|
|
2019-09-03 09:43:16 +00:00
|
|
|
await setupGlobalCurrencies(countryList);
|
2019-08-14 07:43:49 +00:00
|
|
|
await setupAccountsAndDashboard(bankName);
|
|
|
|
await setupRegionalChanges(country);
|
|
|
|
|
2019-08-20 08:57:27 +00:00
|
|
|
await setupWizardValues.set({ completed: 1 });
|
|
|
|
await setupWizardValues.update();
|
|
|
|
|
2019-08-14 07:43:49 +00:00
|
|
|
frappe.events.trigger('show-desk');
|
|
|
|
});
|
2019-08-20 08:57:27 +00:00
|
|
|
|
2019-09-03 09:43:16 +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,
|
|
|
|
number_format: numberFormat
|
|
|
|
} = country;
|
|
|
|
|
|
|
|
if (currency) {
|
|
|
|
const exists = queue.includes(currency);
|
|
|
|
if (!exists) {
|
|
|
|
const doc = await frappe.newDoc({
|
|
|
|
doctype: 'Currency',
|
|
|
|
name: currency,
|
|
|
|
fraction,
|
|
|
|
fractionUnits,
|
|
|
|
smallestValue,
|
|
|
|
symbol,
|
|
|
|
numberFormat: numberFormat || '#,###.##'
|
|
|
|
});
|
|
|
|
promises.push(doc.insert());
|
|
|
|
queue.push(currency);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
2019-08-14 07:43:49 +00:00
|
|
|
async function setupAccountsAndDashboard(bankName) {
|
2019-02-18 05:42:04 +00:00
|
|
|
await frappe.call({
|
|
|
|
method: 'import-coa'
|
|
|
|
});
|
2019-08-14 07:43:49 +00:00
|
|
|
|
|
|
|
const accountDoc = await frappe.newDoc({
|
|
|
|
doctype: 'Account'
|
|
|
|
});
|
|
|
|
Object.assign(accountDoc, {
|
|
|
|
name: bankName,
|
|
|
|
rootType: 'Asset',
|
|
|
|
parentAccount: 'Bank Accounts',
|
2019-08-20 08:57:27 +00:00
|
|
|
accountType: 'Bank',
|
|
|
|
isGroup: 0
|
2019-08-14 07:43:49 +00:00
|
|
|
});
|
|
|
|
accountDoc.insert();
|
|
|
|
|
|
|
|
const dashboardSettings = await frappe.getSingle('DashboardSettings');
|
|
|
|
const accounts = await frappe.db.getAll({
|
|
|
|
doctype: 'Account',
|
|
|
|
filters: { parentAccount: ['in', ['', undefined, null]] }
|
|
|
|
});
|
|
|
|
|
|
|
|
const colors = [
|
|
|
|
{ name: 'Red', hexvalue: '#d32f2f' },
|
|
|
|
{ name: 'Green', hexvalue: '#388e3c' },
|
|
|
|
{ name: 'Blue', hexvalue: '#0288d1' },
|
|
|
|
{ name: 'Yellow', hexvalue: '#cddc39' }
|
|
|
|
];
|
|
|
|
colors.forEach(async color => {
|
|
|
|
const c = await frappe.newDoc({ doctype: 'Color' });
|
|
|
|
c.set(color);
|
|
|
|
c.insert();
|
|
|
|
});
|
|
|
|
|
|
|
|
let charts = [];
|
|
|
|
accounts.forEach(account => {
|
|
|
|
charts.push({
|
|
|
|
account: account.name,
|
|
|
|
type: 'Bar',
|
|
|
|
color: colors[Math.floor(Math.random() * 4)].name
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await dashboardSettings.set({
|
|
|
|
charts
|
|
|
|
});
|
|
|
|
await dashboardSettings.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setupRegionalChanges(country) {
|
2019-07-19 13:24:31 +00:00
|
|
|
const generateRegionalTaxes = require('../models/doctype/Tax/RegionalChanges');
|
|
|
|
await generateRegionalTaxes(country);
|
2019-07-16 07:04:51 +00:00
|
|
|
if (country === 'India') {
|
2019-07-19 13:24:31 +00:00
|
|
|
frappe.models.Party = require('../models/doctype/Party/RegionalChanges');
|
2019-07-16 07:04:51 +00:00
|
|
|
await frappe.db.migrate();
|
2019-02-18 05:42:04 +00:00
|
|
|
}
|
2019-08-14 07:43:49 +00:00
|
|
|
}
|
2018-10-22 18:02:47 +00:00
|
|
|
|
2018-10-23 12:42:36 +00:00
|
|
|
async function connectToLocalDatabase(filepath) {
|
2019-02-18 05:42:04 +00:00
|
|
|
try {
|
|
|
|
frappe.login('Administrator');
|
|
|
|
frappe.db = new SQLite({
|
|
|
|
dbPath: filepath
|
|
|
|
});
|
|
|
|
await frappe.db.connect();
|
|
|
|
await frappe.db.migrate();
|
|
|
|
frappe.getSingle('SystemSettings');
|
|
|
|
await postStart();
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2018-10-22 18:02:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 12:49:33 +00:00
|
|
|
window.frappe = frappe;
|
|
|
|
|
|
|
|
Vue.config.productionTip = false;
|
|
|
|
Vue.use(frappeVue);
|
2018-10-22 21:29:07 +00:00
|
|
|
Vue.use(Toasted, {
|
|
|
|
position: 'bottom-right',
|
2019-02-18 05:42:04 +00:00
|
|
|
duration: 3000
|
|
|
|
});
|
2018-10-21 12:49:33 +00:00
|
|
|
|
2019-10-13 12:03:01 +00:00
|
|
|
Vue.config.errorHandler = (err, vm, info) => {
|
|
|
|
console.error(err, vm, info);
|
|
|
|
};
|
|
|
|
|
2018-10-21 12:49:33 +00:00
|
|
|
/* eslint-disable no-new */
|
|
|
|
new Vue({
|
|
|
|
el: '#app',
|
|
|
|
router,
|
2019-02-18 05:42:04 +00:00
|
|
|
components: {
|
|
|
|
App
|
|
|
|
},
|
2018-10-21 12:49:33 +00:00
|
|
|
template: '<App/>'
|
2018-10-20 12:32:01 +00:00
|
|
|
});
|
2019-07-16 07:04:51 +00:00
|
|
|
})();
|