2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/src/main-electron.js

169 lines
4.1 KiB
JavaScript
Raw Normal View History

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';
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();
2018-10-23 12:42:36 +00:00
frappe.events.on('connect-database', async (filepath) => {
await connectToLocalDatabase(filepath);
const accountingSettings = await frappe.getSingle('AccountingSettings');
const country = accountingSettings.country;
if (country === "India") {
frappe.models.Party = require('../models/doctype/Party/RegionalChanges.js')
} else {
frappe.models.Party = require('../models/doctype/Party/Party.js')
}
2018-10-23 12:42:36 +00:00
frappe.events.trigger('show-desk');
2018-10-23 12:42:36 +00:00
});
frappe.events.on('DatabaseSelector:file-selected', async (filepath) => {
await connectToLocalDatabase(filepath);
2018-10-22 18:02:47 +00:00
2018-10-23 12:42:36 +00:00
localStorage.dbPath = filepath;
const accountingSettings = await frappe.getSingle('AccountingSettings');
if (!accountingSettings.companyName) {
frappe.events.trigger('show-setup-wizard');
} else {
frappe.events.trigger('show-desk');
}
});
frappe.events.on('SetupWizard:setup-complete', async (setupWizardValues) => {
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,
fiscalYearEnd
2018-10-21 12:49:33 +00:00
});
2018-10-22 18:02:47 +00:00
await doc.update();
await frappe.call({
method: 'import-coa'
});
2018-10-22 18:02:47 +00:00
if (country === "India") {
frappe.models.Party = require('../models/doctype/Party/RegionalChanges.js')
await frappe.db.migrate()
await generateGstTaxes();
}
2018-10-22 18:02:47 +00:00
frappe.events.trigger('show-desk');
});
async function generateGstTaxes() {
const gstPercents = [5, 12, 18, 28];
const gstTypes = ['Out of State', 'In State'];
let newTax = await frappe.getNewDoc('Tax');
for (const type of gstTypes) {
for (const percent of gstPercents) {
switch (type) {
case 'Out of State':
await newTax.set({
name: `${type}-${percent}`,
details: [{
account: "IGST",
rate: percent
}]
})
break;
case 'In State':
await newTax.set({
name: `${type}-${percent}`,
details: [{
account: "CGST",
rate: percent / 2
},
{
account: "SGST",
rate: percent / 2
}
]
})
break;
}
await newTax.insert();
}
}
await newTax.set({
name: `Exempt-0`,
details: [{
account: "Exempt",
rate: 0
}]
})
await newTax.insert();
}
2018-10-22 18:02:47 +00:00
2018-10-23 12:42:36 +00:00
async function connectToLocalDatabase(filepath) {
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',
duration: 3000
});
2018-10-21 12:49:33 +00:00
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: {
App
},
2018-10-21 12:49:33 +00:00
template: '<App/>'
2018-10-20 12:32:01 +00:00
});
})()