2022-03-08 08:47:37 +00:00
|
|
|
const { getPaddedName } = require('@/utils');
|
2022-01-20 20:57:29 +00:00
|
|
|
const frappe = require('frappe');
|
|
|
|
const { getRandomString } = require('frappe/utils');
|
2018-03-05 16:45:21 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2022-02-24 06:09:35 +00:00
|
|
|
async isNameAutoSet(doctype) {
|
|
|
|
const doc = frappe.getNewDoc(doctype);
|
|
|
|
if (doc.meta.naming === 'autoincrement') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!doc.meta.settings) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { numberSeries } = await doc.getSettings();
|
|
|
|
if (numberSeries) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2022-01-20 20:57:29 +00:00
|
|
|
async setName(doc) {
|
|
|
|
if (frappe.isServer) {
|
|
|
|
// if is server, always name again if autoincrement or other
|
|
|
|
if (doc.meta.naming === 'autoincrement') {
|
|
|
|
doc.name = await this.getNextId(doc.doctype);
|
|
|
|
return;
|
|
|
|
}
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-03-08 08:12:08 +00:00
|
|
|
// Current, per doc number series
|
|
|
|
if (doc.numberSeries) {
|
|
|
|
doc.name = await this.getSeriesNext(doc.numberSeries, doc.doctype);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy, using doc settings for number series
|
2022-01-20 20:57:29 +00:00
|
|
|
if (doc.meta.settings) {
|
|
|
|
const numberSeries = (await doc.getSettings()).numberSeries;
|
2022-03-08 08:12:08 +00:00
|
|
|
if (!numberSeries) {
|
|
|
|
return;
|
2018-03-05 16:45:21 +00:00
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
|
|
|
doc.name = await this.getSeriesNext(numberSeries, doc.doctype);
|
|
|
|
return;
|
2022-01-20 20:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-01-20 20:57:29 +00:00
|
|
|
if (doc.name) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-01-20 20:57:29 +00:00
|
|
|
// name === doctype for Single
|
|
|
|
if (doc.meta.isSingle) {
|
|
|
|
doc.name = doc.meta.name;
|
|
|
|
return;
|
|
|
|
}
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-01-20 20:57:29 +00:00
|
|
|
// assign a random name by default
|
|
|
|
// override doc to set a name
|
|
|
|
if (!doc.name) {
|
|
|
|
doc.name = getRandomString();
|
|
|
|
}
|
|
|
|
},
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-01-20 20:57:29 +00:00
|
|
|
async getNextId(doctype) {
|
|
|
|
// get the last inserted row
|
|
|
|
let lastInserted = await this.getLastInserted(doctype);
|
|
|
|
let name = 1;
|
|
|
|
if (lastInserted) {
|
|
|
|
let lastNumber = parseInt(lastInserted.name);
|
|
|
|
if (isNaN(lastNumber)) lastNumber = 0;
|
|
|
|
name = lastNumber + 1;
|
|
|
|
}
|
|
|
|
return (name + '').padStart(9, '0');
|
|
|
|
},
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-01-20 20:57:29 +00:00
|
|
|
async getLastInserted(doctype) {
|
|
|
|
const lastInserted = await frappe.db.getAll({
|
|
|
|
doctype: doctype,
|
|
|
|
fields: ['name'],
|
|
|
|
limit: 1,
|
|
|
|
order_by: 'creation',
|
|
|
|
order: 'desc',
|
|
|
|
});
|
|
|
|
return lastInserted && lastInserted.length ? lastInserted[0] : null;
|
|
|
|
},
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-02-07 10:18:37 +00:00
|
|
|
async getSeriesNext(prefix, doctype) {
|
2022-01-20 20:57:29 +00:00
|
|
|
let series;
|
2022-03-08 08:47:37 +00:00
|
|
|
|
2022-01-20 20:57:29 +00:00
|
|
|
try {
|
|
|
|
series = await frappe.getDoc('NumberSeries', prefix);
|
|
|
|
} catch (e) {
|
|
|
|
if (!e.statusCode || e.statusCode !== 404) {
|
|
|
|
throw e;
|
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
|
|
|
await this.createNumberSeries(prefix, doctype);
|
2022-01-20 20:57:29 +00:00
|
|
|
series = await frappe.getDoc('NumberSeries', prefix);
|
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2022-03-08 08:47:37 +00:00
|
|
|
return await series.next(doctype);
|
2022-01-20 20:57:29 +00:00
|
|
|
},
|
2018-03-27 13:55:26 +00:00
|
|
|
|
2022-03-08 08:12:08 +00:00
|
|
|
async createNumberSeries(prefix, referenceType, start = 1001) {
|
|
|
|
const exists = await frappe.db.exists('NumberSeries', prefix);
|
|
|
|
if (exists) {
|
|
|
|
return;
|
2018-03-05 16:45:21 +00:00
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
|
|
|
const series = frappe.newDoc({
|
|
|
|
doctype: 'NumberSeries',
|
|
|
|
name: prefix,
|
|
|
|
start,
|
|
|
|
referenceType,
|
|
|
|
});
|
|
|
|
|
|
|
|
await series.insert();
|
2022-01-20 20:57:29 +00:00
|
|
|
},
|
|
|
|
};
|