2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 14:50:56 +00:00

refactor: remove core js schemas

- switch to doc and json schemas instead
This commit is contained in:
18alantom 2022-04-07 14:57:01 +05:30
parent e0e74817be
commit 0efd2b4c33
11 changed files with 64 additions and 251 deletions

View File

@ -29,7 +29,6 @@ export class Frappe {
errors = errors;
isElectron = false;
isServer = false;
pesa: MoneyMaker;

View File

@ -0,0 +1,38 @@
import { getPaddedName } from '@/utils';
import frappe from 'frappe';
import Doc from 'frappe/model/doc';
export default class NumberSeries extends Doc {
validate() {
const current = this.get('current') as number | null;
if (current) {
this.current = this.get('start');
}
}
async next(schemaName: string) {
this.validate();
const exists = await this.checkIfCurrentExists(schemaName);
if (!exists) {
return this.getPaddedName(this.current as number);
}
this.current = (this.current as number) + 1;
await this.update();
return this.getPaddedName(this.current as number);
}
async checkIfCurrentExists(schemaName: string) {
if (!schemaName) {
return true;
}
const name = this.getPaddedName(this.current as number);
return await frappe.db.exists(schemaName, name);
}
getPaddedName(next: number): string {
return getPaddedName(this.name, next, this.padZeros);
}
}

View File

@ -0,0 +1,19 @@
import { DocValue } from 'frappe/core/types';
import Doc from 'frappe/model/doc';
import { ValidationMap } from 'frappe/model/types';
import { ValidationError } from 'frappe/utils/errors';
import { t } from 'frappe/utils/translation';
export default class SystemSettings extends Doc {
validations: ValidationMap = {
async displayPrecision(value: DocValue) {
if ((value as number) >= 0 && (value as number) <= 9) {
return;
}
throw new ValidationError(
t`Display Precision should have a value between 0 and 9.`
);
},
};
}

View File

@ -1,66 +0,0 @@
import { t } from 'frappe';
import NumberSeries from './NumberSeriesDocument.js';
const referenceTypeMap = {
SalesInvoice: t`Invoice`,
PurchaseInvoice: t`Bill`,
Payment: t`Payment`,
JournalEntry: t`Journal Entry`,
Quotation: t`Quotation`,
SalesOrder: t`SalesOrder`,
Fulfillment: t`Fulfillment`,
PurchaseOrder: t`PurchaseOrder`,
PurchaseReceipt: t`PurchaseReceipt`,
'-': t`None`,
};
export default {
name: 'NumberSeries',
label: t`Number Series`,
documentClass: NumberSeries,
doctype: 'DocType',
isSingle: 0,
isChild: 0,
keywordFields: [],
fields: [
{
fieldname: 'name',
label: t`Prefix`,
fieldtype: 'Data',
required: 1,
},
{
fieldname: 'start',
label: t`Start`,
fieldtype: 'Int',
default: 1001,
required: 1,
minvalue: 0,
},
{
fieldname: 'padZeros',
label: t`Pad Zeros`,
fieldtype: 'Int',
default: 4,
required: 1,
},
{
fieldname: 'referenceType',
label: t`Reference Type`,
fieldtype: 'Select',
options: Object.keys(referenceTypeMap),
map: referenceTypeMap,
default: '-',
required: 1,
readOnly: 1,
},
{
fieldname: 'current',
label: t`Current`,
fieldtype: 'Int',
required: 1,
readOnly: 1,
},
],
quickEditFields: ['start', 'padZeros', 'referenceType'],
};

View File

@ -1,37 +0,0 @@
import { getPaddedName } from '@/utils';
import frappe from 'frappe';
import Document from 'frappe/model/document';
export default class NumberSeries extends Document {
validate() {
if (!this.current) {
this.current = this.start;
}
}
async next(doctype) {
this.validate();
const exists = await this.checkIfCurrentExists(doctype);
if (!exists) {
return this.getPaddedName(this.current);
}
this.current++;
await this.update();
return this.getPaddedName(this.current);
}
async checkIfCurrentExists(doctype) {
if (!doctype) {
return true;
}
const name = this.getPaddedName(this.current);
return await frappe.db.exists(doctype, name);
}
getPaddedName(next) {
return getPaddedName(this.name, next, this.padZeros);
}
}

View File

@ -1,12 +0,0 @@
const { t } = require('frappe');
module.exports = {
name: 'PatchRun',
fields: [
{
fieldname: 'name',
fieldtype: 'Data',
label: t`Name`,
},
],
};

View File

@ -1,29 +0,0 @@
const { t } = require('frappe');
module.exports = {
name: 'SingleValue',
doctype: 'DocType',
isSingle: 0,
isChild: 0,
keywordFields: [],
fields: [
{
fieldname: 'parent',
label: t`Parent`,
fieldtype: 'Data',
required: 1,
},
{
fieldname: 'fieldname',
label: t`Fieldname`,
fieldtype: 'Data',
required: 1,
},
{
fieldname: 'value',
label: t`Value`,
fieldtype: 'Data',
required: 1,
},
],
};

View File

@ -1,94 +0,0 @@
const { DateTime } = require('luxon');
const { t } = require('frappe');
const {
DEFAULT_DISPLAY_PRECISION,
DEFAULT_INTERNAL_PRECISION,
DEFAULT_LOCALE,
} = require('../../../utils/consts');
let dateFormatOptions = (() => {
let formats = [
'dd/MM/yyyy',
'MM/dd/yyyy',
'dd-MM-yyyy',
'MM-dd-yyyy',
'yyyy-MM-dd',
'd MMM, y',
'MMM d, y',
];
let today = DateTime.local();
return formats.map((format) => {
return {
label: today.toFormat(format),
value: format,
};
});
})();
module.exports = {
name: 'SystemSettings',
label: t`System Settings`,
doctype: 'DocType',
isSingle: 1,
isChild: 0,
keywordFields: [],
fields: [
{
fieldname: 'dateFormat',
label: t`Date Format`,
fieldtype: 'Select',
options: dateFormatOptions,
default: 'MMM d, y',
required: 1,
description: t`Sets the app-wide date display format.`,
},
{
fieldname: 'locale',
label: t`Locale`,
fieldtype: 'Data',
default: DEFAULT_LOCALE,
description: t`Set the local code. This is used for number formatting.`,
},
{
fieldname: 'displayPrecision',
label: t`Display Precision`,
fieldtype: 'Int',
default: DEFAULT_DISPLAY_PRECISION,
required: 1,
minValue: 0,
maxValue: 9,
validate(value, doc) {
if (value >= 0 && value <= 9) {
return;
}
throw new frappe.errors.ValidationError(
t`Display Precision should have a value between 0 and 9.`
);
},
description: t`Sets how many digits are shown after the decimal point.`,
},
{
fieldname: 'internalPrecision',
label: t`Internal Precision`,
fieldtype: 'Int',
minValue: 0,
default: DEFAULT_INTERNAL_PRECISION,
description: t`Sets the internal precision used for monetary calculations. Above 6 should be sufficient for most currencies.`,
},
{
fieldname: 'hideGetStarted',
label: t`Hide Get Started`,
fieldtype: 'Check',
default: 0,
description: t`Hides the Get Started section from the sidebar. Change will be visible on restart or refreshing the app.`,
},
],
quickEditFields: [
'locale',
'dateFormat',
'displayPrecision',
'hideGetStarted',
],
};

View File

@ -1,11 +0,0 @@
import NumberSeries from './doctype/NumberSeries/NumberSeries.js';
import PatchRun from './doctype/PatchRun/PatchRun.js';
import SingleValue from './doctype/SingleValue/SingleValue.js';
import SystemSettings from './doctype/SystemSettings/SystemSettings.js';
export default {
NumberSeries,
SingleValue,
SystemSettings,
PatchRun,
};

7
frappe/models/index.ts Normal file
View File

@ -0,0 +1,7 @@
import NumberSeries from './NumberSeries.js';
import SystemSettings from './SystemSettings';
export default {
NumberSeries,
SystemSettings,
};

View File

@ -22,7 +22,6 @@ import { setLanguageMap, stringifyCircular } from './utils';
window.config = config;
}
frappe.isServer = true;
frappe.isElectron = true;
const models = (await import('../models')).default;