From b4bed9521f28649028ac166dcb6b76b049c26941 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Tue, 8 Mar 2022 14:17:37 +0530 Subject: [PATCH] fix: minor bugs, fix patch too --- frappe/model/naming.js | 11 +++-------- frappe/models/doctype/NumberSeries/NumberSeries.js | 1 + .../doctype/NumberSeries/NumberSeriesDocument.js | 12 ++++++++++-- patches/0.3.2/moveNumberSeriesFromSettings.js | 4 ++++ src/utils.js | 9 +++++++++ 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/frappe/model/naming.js b/frappe/model/naming.js index 7c3ab8f1..86fd62d0 100644 --- a/frappe/model/naming.js +++ b/frappe/model/naming.js @@ -1,3 +1,4 @@ +const { getPaddedName } = require('@/utils'); const frappe = require('frappe'); const { getRandomString } = require('frappe/utils'); @@ -90,6 +91,7 @@ module.exports = { async getSeriesNext(prefix, doctype) { let series; + try { series = await frappe.getDoc('NumberSeries', prefix); } catch (e) { @@ -101,14 +103,7 @@ module.exports = { series = await frappe.getDoc('NumberSeries', prefix); } - let next = await series.next(doctype); - console.log(next, series.pad, series) - const pad = series.padZeros ?? 4; - - const l = next.toString().length; - const z = '0'.repeat(Math.max(0, pad - l)); - - return prefix + z + next; + return await series.next(doctype); }, async createNumberSeries(prefix, referenceType, start = 1001) { diff --git a/frappe/models/doctype/NumberSeries/NumberSeries.js b/frappe/models/doctype/NumberSeries/NumberSeries.js index ba1a42e7..9ae22a21 100644 --- a/frappe/models/doctype/NumberSeries/NumberSeries.js +++ b/frappe/models/doctype/NumberSeries/NumberSeries.js @@ -33,6 +33,7 @@ module.exports = { fieldtype: 'Int', default: 1001, required: 1, + minvalue: 0, }, { fieldname: 'padZeros', diff --git a/frappe/models/doctype/NumberSeries/NumberSeriesDocument.js b/frappe/models/doctype/NumberSeries/NumberSeriesDocument.js index 95a71866..4effd328 100644 --- a/frappe/models/doctype/NumberSeries/NumberSeriesDocument.js +++ b/frappe/models/doctype/NumberSeries/NumberSeriesDocument.js @@ -1,3 +1,4 @@ +const { getPaddedName } = require('@/utils'); const frappe = require('frappe'); const BaseDocument = require('frappe/model/document'); @@ -7,6 +8,7 @@ module.exports = class NumberSeries extends BaseDocument { this.current = this.start; } } + async next(doctype) { this.validate(); @@ -17,13 +19,19 @@ module.exports = class NumberSeries extends BaseDocument { this.current++; await this.update(); - return this.current; + return this.getPaddedName(this.current); } async checkIfCurrentExists(doctype) { if (!doctype) { return true; } - return await frappe.db.exists(doctype, this.name + this.current); + + 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/patches/0.3.2/moveNumberSeriesFromSettings.js b/patches/0.3.2/moveNumberSeriesFromSettings.js index fc3f3aeb..def267df 100644 --- a/patches/0.3.2/moveNumberSeriesFromSettings.js +++ b/patches/0.3.2/moveNumberSeriesFromSettings.js @@ -6,6 +6,10 @@ async function setReferencesOnNumberSeries() { const map = invertMap(DEFAULT_NUMBER_SERIES); const rows = await frappe.db.knex('NumberSeries'); for (const row of rows) { + if (row.referenceType === map[row.name]) { + return; + } + row.referenceType = map[row.name]; } await frappe.db.prestigeTheTable('NumberSeries', rows); diff --git a/src/utils.js b/src/utils.js index a4c1c84c..dd88f129 100644 --- a/src/utils.js +++ b/src/utils.js @@ -547,3 +547,12 @@ export function invertMap(map) { return inverted; } + +export function getPaddedName(prefix, next, padZeros) { + const padding = padZeros ?? 4; + + const l = next.toString().length; + const z = '0'.repeat(Math.max(0, padding - l)); + + return prefix + z + next; +}