2022-04-19 05:59:36 +00:00
|
|
|
import { Fyo } from 'fyo';
|
2023-07-24 09:23:57 +00:00
|
|
|
import { NumberSeries } from 'fyo/models/NumberSeries';
|
2022-04-19 05:59:36 +00:00
|
|
|
import { DEFAULT_SERIES_START } from 'fyo/utils/consts';
|
|
|
|
import { BaseError } from 'fyo/utils/errors';
|
2022-04-21 13:08:36 +00:00
|
|
|
import { getRandomString } from 'utils';
|
2022-04-24 06:48:44 +00:00
|
|
|
import { Doc } from './doc';
|
2022-04-08 06:10:04 +00:00
|
|
|
|
2022-04-19 05:59:36 +00:00
|
|
|
export function isNameAutoSet(schemaName: string, fyo: Fyo): boolean {
|
|
|
|
const schema = fyo.schemaMap[schemaName]!;
|
2022-05-05 10:44:26 +00:00
|
|
|
if (schema.naming === 'manual') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-08 06:10:04 +00:00
|
|
|
if (schema.naming === 'autoincrement') {
|
2022-03-17 11:46:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-02-24 06:09:35 +00:00
|
|
|
|
2022-05-05 10:44:26 +00:00
|
|
|
if (schema.naming === 'random') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const numberSeries = fyo.getField(schema.name, 'numberSeries');
|
2022-03-17 11:46:49 +00:00
|
|
|
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-04-19 05:59:36 +00:00
|
|
|
export async function setName(doc: Doc, fyo: Fyo) {
|
2022-04-29 13:00:24 +00:00
|
|
|
if (doc.schema.naming === 'manual') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-08 06:10:04 +00:00
|
|
|
if (doc.schema.naming === 'autoincrement') {
|
2022-05-11 10:44:31 +00:00
|
|
|
return (doc.name = await getNextId(doc.schemaName, fyo));
|
2022-04-08 06:10:04 +00:00
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2022-05-11 10:44:31 +00:00
|
|
|
if (doc.numberSeries !== undefined) {
|
|
|
|
return (doc.name = await getSeriesNext(
|
|
|
|
doc.numberSeries as string,
|
|
|
|
doc.schemaName,
|
|
|
|
fyo
|
|
|
|
));
|
2022-03-17 11:46:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 04:45:47 +00:00
|
|
|
// name === schemaName for Single
|
2022-04-08 06:10:04 +00:00
|
|
|
if (doc.schema.isSingle) {
|
2022-05-11 10:44:31 +00:00
|
|
|
return (doc.name = doc.schemaName);
|
2022-03-17 11:46:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 10:44:31 +00:00
|
|
|
// Assign a random name by default
|
2022-03-17 11:46:49 +00:00
|
|
|
if (!doc.name) {
|
|
|
|
doc.name = getRandomString();
|
|
|
|
}
|
|
|
|
|
2022-05-11 10:44:31 +00:00
|
|
|
return doc.name;
|
2022-03-17 11:46:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 10:44:31 +00:00
|
|
|
export async function getNextId(schemaName: string, fyo: Fyo): Promise<string> {
|
|
|
|
const lastInserted = await fyo.db.getLastInserted(schemaName);
|
|
|
|
return String(lastInserted + 1).padStart(9, '0');
|
2022-03-17 11:46:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 11:29:20 +00:00
|
|
|
export async function getSeriesNext(
|
|
|
|
prefix: string,
|
|
|
|
schemaName: string,
|
2022-04-19 05:59:36 +00:00
|
|
|
fyo: Fyo
|
2022-04-18 11:29:20 +00:00
|
|
|
) {
|
2022-04-08 06:10:04 +00:00
|
|
|
let series: NumberSeries;
|
2022-03-17 11:46:49 +00:00
|
|
|
|
|
|
|
try {
|
2023-07-25 06:10:31 +00:00
|
|
|
series = (await fyo.doc.get('NumberSeries', prefix)) as NumberSeries;
|
2022-03-17 11:46:49 +00:00
|
|
|
} catch (e) {
|
2022-04-08 06:10:04 +00:00
|
|
|
const { statusCode } = e as BaseError;
|
|
|
|
if (!statusCode || statusCode !== 404) {
|
2022-03-17 11:46:49 +00:00
|
|
|
throw e;
|
2022-01-20 20:57:29 +00:00
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2022-04-19 05:59:36 +00:00
|
|
|
await createNumberSeries(prefix, schemaName, DEFAULT_SERIES_START, fyo);
|
2023-07-25 06:10:31 +00:00
|
|
|
series = (await fyo.doc.get('NumberSeries', prefix)) as NumberSeries;
|
2022-03-17 11:46:49 +00:00
|
|
|
}
|
2018-03-27 13:55:26 +00:00
|
|
|
|
2022-04-08 06:10:04 +00:00
|
|
|
return await series.next(schemaName);
|
2022-03-17 11:46:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 06:10:04 +00:00
|
|
|
export async function createNumberSeries(
|
|
|
|
prefix: string,
|
|
|
|
referenceType: string,
|
2022-04-18 11:29:20 +00:00
|
|
|
start: number,
|
2022-04-19 05:59:36 +00:00
|
|
|
fyo: Fyo
|
2022-04-08 06:10:04 +00:00
|
|
|
) {
|
2022-04-19 05:59:36 +00:00
|
|
|
const exists = await fyo.db.exists('NumberSeries', prefix);
|
2022-03-17 11:46:49 +00:00
|
|
|
if (exists) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2023-07-25 06:10:31 +00:00
|
|
|
const series = fyo.doc.new('NumberSeries', {
|
2022-03-17 11:46:49 +00:00
|
|
|
name: prefix,
|
|
|
|
start,
|
|
|
|
referenceType,
|
|
|
|
});
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2022-04-24 06:48:44 +00:00
|
|
|
await series.sync();
|
2022-03-17 11:46:49 +00:00
|
|
|
}
|