2022-04-19 05:59:36 +00:00
|
|
|
import { Fyo } from 'fyo';
|
|
|
|
import NumberSeries from 'fyo/models/NumberSeries';
|
|
|
|
import { DEFAULT_SERIES_START } from 'fyo/utils/consts';
|
|
|
|
import { BaseError } from 'fyo/utils/errors';
|
2022-04-08 06:10:04 +00:00
|
|
|
import { Field, Schema } from 'schemas/types';
|
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
|
|
|
|
|
|
|
export function getNumberSeries(schema: Schema): Field | undefined {
|
|
|
|
const numberSeries = schema.fields.find(
|
|
|
|
(f) => f.fieldname === 'numberSeries'
|
|
|
|
);
|
|
|
|
return numberSeries;
|
|
|
|
}
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2022-04-19 05:59:36 +00:00
|
|
|
export function isNameAutoSet(schemaName: string, fyo: Fyo): boolean {
|
|
|
|
const schema = fyo.schemaMap[schemaName]!;
|
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-04-08 06:10:04 +00:00
|
|
|
const numberSeries = getNumberSeries(schema);
|
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-04-19 05:59:36 +00:00
|
|
|
doc.name = await getNextId(doc.schemaName, fyo);
|
2022-04-08 06:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-03-08 08:12:08 +00:00
|
|
|
|
2022-04-08 06:10:04 +00:00
|
|
|
// Current, per doc number series
|
|
|
|
const numberSeries = doc.numberSeries as string | undefined;
|
|
|
|
if (numberSeries !== undefined) {
|
2022-04-19 05:59:36 +00:00
|
|
|
doc.name = await getSeriesNext(numberSeries, doc.schemaName, fyo);
|
2022-04-08 06:10:04 +00:00
|
|
|
return;
|
2022-03-17 11:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (doc.name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-01 04:45:47 +00:00
|
|
|
doc.name = doc.schemaName;
|
2022-03-17 11:46:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign a random name by default
|
|
|
|
// override doc to set a name
|
|
|
|
if (!doc.name) {
|
|
|
|
doc.name = getRandomString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 05:59:36 +00:00
|
|
|
export async function getNextId(schemaName: string, fyo: Fyo) {
|
2022-03-17 11:46:49 +00:00
|
|
|
// get the last inserted row
|
2022-04-19 05:59:36 +00:00
|
|
|
const lastInserted = await getLastInserted(schemaName, fyo);
|
2022-03-17 11:46:49 +00:00
|
|
|
let name = 1;
|
|
|
|
if (lastInserted) {
|
2022-04-08 06:10:04 +00:00
|
|
|
let lastNumber = parseInt(lastInserted.name as string);
|
2022-03-17 11:46:49 +00:00
|
|
|
if (isNaN(lastNumber)) lastNumber = 0;
|
|
|
|
name = lastNumber + 1;
|
|
|
|
}
|
|
|
|
return (name + '').padStart(9, '0');
|
|
|
|
}
|
|
|
|
|
2022-04-19 05:59:36 +00:00
|
|
|
export async function getLastInserted(schemaName: string, fyo: Fyo) {
|
|
|
|
const lastInserted = await fyo.db.getAll(schemaName, {
|
2022-03-17 11:46:49 +00:00
|
|
|
fields: ['name'],
|
|
|
|
limit: 1,
|
2022-04-27 12:02:43 +00:00
|
|
|
orderBy: 'created',
|
2022-03-17 11:46:49 +00:00
|
|
|
order: 'desc',
|
|
|
|
});
|
|
|
|
return lastInserted && lastInserted.length ? lastInserted[0] : null;
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-04-19 05:59:36 +00:00
|
|
|
series = (await fyo.doc.getDoc('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);
|
|
|
|
series = (await fyo.doc.getDoc('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
|
|
|
|
2022-04-19 05:59:36 +00:00
|
|
|
const series = fyo.doc.getNewDoc('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
|
|
|
}
|