diff --git a/frappe/index.ts b/frappe/index.ts index e490927d..19d40d4b 100644 --- a/frappe/index.ts +++ b/frappe/index.ts @@ -29,7 +29,6 @@ export class Frappe { errors = errors; isElectron = false; - isServer = false; pesa: MoneyMaker; diff --git a/frappe/models/NumberSeries.ts b/frappe/models/NumberSeries.ts new file mode 100644 index 00000000..ec13f80d --- /dev/null +++ b/frappe/models/NumberSeries.ts @@ -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); + } +} diff --git a/frappe/models/SystemSettings.ts b/frappe/models/SystemSettings.ts new file mode 100644 index 00000000..dd164bdc --- /dev/null +++ b/frappe/models/SystemSettings.ts @@ -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.` + ); + }, + }; +} diff --git a/frappe/models/doctype/NumberSeries/NumberSeries.js b/frappe/models/doctype/NumberSeries/NumberSeries.js deleted file mode 100644 index 51b1282d..00000000 --- a/frappe/models/doctype/NumberSeries/NumberSeries.js +++ /dev/null @@ -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'], -}; diff --git a/frappe/models/doctype/NumberSeries/NumberSeriesDocument.js b/frappe/models/doctype/NumberSeries/NumberSeriesDocument.js deleted file mode 100644 index 09c6f73c..00000000 --- a/frappe/models/doctype/NumberSeries/NumberSeriesDocument.js +++ /dev/null @@ -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); - } -} diff --git a/frappe/models/doctype/PatchRun/PatchRun.js b/frappe/models/doctype/PatchRun/PatchRun.js deleted file mode 100644 index 8ec73bce..00000000 --- a/frappe/models/doctype/PatchRun/PatchRun.js +++ /dev/null @@ -1,12 +0,0 @@ -const { t } = require('frappe'); - -module.exports = { - name: 'PatchRun', - fields: [ - { - fieldname: 'name', - fieldtype: 'Data', - label: t`Name`, - }, - ], -}; diff --git a/frappe/models/doctype/SingleValue/SingleValue.js b/frappe/models/doctype/SingleValue/SingleValue.js deleted file mode 100644 index 721a7b30..00000000 --- a/frappe/models/doctype/SingleValue/SingleValue.js +++ /dev/null @@ -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, - }, - ], -}; diff --git a/frappe/models/doctype/SystemSettings/SystemSettings.js b/frappe/models/doctype/SystemSettings/SystemSettings.js deleted file mode 100644 index 45603b80..00000000 --- a/frappe/models/doctype/SystemSettings/SystemSettings.js +++ /dev/null @@ -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', - ], -}; diff --git a/frappe/models/index.js b/frappe/models/index.js deleted file mode 100644 index b24843d0..00000000 --- a/frappe/models/index.js +++ /dev/null @@ -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, -}; diff --git a/frappe/models/index.ts b/frappe/models/index.ts new file mode 100644 index 00000000..ce29ca97 --- /dev/null +++ b/frappe/models/index.ts @@ -0,0 +1,7 @@ +import NumberSeries from './NumberSeries.js'; +import SystemSettings from './SystemSettings'; + +export default { + NumberSeries, + SystemSettings, +}; diff --git a/src/main.js b/src/main.js index 6e564444..ed9b3688 100644 --- a/src/main.js +++ b/src/main.js @@ -22,7 +22,6 @@ import { setLanguageMap, stringifyCircular } from './utils'; window.config = config; } - frappe.isServer = true; frappe.isElectron = true; const models = (await import('../models')).default;