2022-03-17 11:46:49 +00:00
|
|
|
import frappe from 'frappe';
|
|
|
|
import { getRandomString } from 'frappe/utils';
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-03-17 11:46:49 +00:00
|
|
|
export async function isNameAutoSet(doctype) {
|
2022-03-22 06:00:33 +00:00
|
|
|
const doc = frappe.getEmptyDoc(doctype);
|
2022-03-17 11:46:49 +00:00
|
|
|
if (doc.meta.naming === 'autoincrement') {
|
|
|
|
return true;
|
|
|
|
}
|
2022-02-24 06:09:35 +00:00
|
|
|
|
2022-03-17 11:46:49 +00:00
|
|
|
if (!doc.meta.settings) {
|
2022-02-24 06:09:35 +00:00
|
|
|
return false;
|
2022-03-17 11:46:49 +00:00
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2022-03-17 11:46:49 +00:00
|
|
|
const { numberSeries } = await doc.getSettings();
|
|
|
|
if (numberSeries) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2022-03-17 11:46:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-03-17 11:46:49 +00:00
|
|
|
export async function setName(doc) {
|
|
|
|
if (frappe.isServer) {
|
|
|
|
// if is server, always name again if autoincrement or other
|
|
|
|
if (doc.meta.naming === 'autoincrement') {
|
|
|
|
doc.name = await getNextId(doc.doctype);
|
2022-01-20 20:57:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-03-17 11:46:49 +00:00
|
|
|
// Current, per doc number series
|
|
|
|
if (doc.numberSeries) {
|
|
|
|
doc.name = await getSeriesNext(doc.numberSeries, doc.doctype);
|
2022-01-20 20:57:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-03-17 11:46:49 +00:00
|
|
|
// Legacy, using doc settings for number series
|
|
|
|
if (doc.meta.settings) {
|
|
|
|
const numberSeries = (await doc.getSettings()).numberSeries;
|
|
|
|
if (!numberSeries) {
|
|
|
|
return;
|
2022-01-20 20:57:29 +00:00
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2022-03-17 11:46:49 +00:00
|
|
|
doc.name = await getSeriesNext(numberSeries, doc.doctype);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc.name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// name === doctype for Single
|
|
|
|
if (doc.meta.isSingle) {
|
|
|
|
doc.name = doc.meta.name;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign a random name by default
|
|
|
|
// override doc to set a name
|
|
|
|
if (!doc.name) {
|
|
|
|
doc.name = getRandomString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getNextId(doctype) {
|
|
|
|
// get the last inserted row
|
|
|
|
let lastInserted = await 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');
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSeriesNext(prefix, doctype) {
|
|
|
|
let series;
|
|
|
|
|
|
|
|
try {
|
|
|
|
series = await frappe.getDoc('NumberSeries', prefix);
|
|
|
|
} catch (e) {
|
|
|
|
if (!e.statusCode || e.statusCode !== 404) {
|
|
|
|
throw e;
|
2022-01-20 20:57:29 +00:00
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2022-03-17 11:46:49 +00:00
|
|
|
await createNumberSeries(prefix, doctype);
|
|
|
|
series = await frappe.getDoc('NumberSeries', prefix);
|
|
|
|
}
|
2018-03-27 13:55:26 +00:00
|
|
|
|
2022-03-17 11:46:49 +00:00
|
|
|
return await series.next(doctype);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createNumberSeries(prefix, referenceType, start = 1001) {
|
|
|
|
const exists = await frappe.db.exists('NumberSeries', prefix);
|
|
|
|
if (exists) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2022-03-22 06:00:33 +00:00
|
|
|
const series = frappe.getNewDoc({
|
2022-03-17 11:46:49 +00:00
|
|
|
doctype: 'NumberSeries',
|
|
|
|
name: prefix,
|
|
|
|
start,
|
|
|
|
referenceType,
|
|
|
|
});
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2022-03-17 11:46:49 +00:00
|
|
|
await series.insert();
|
|
|
|
}
|