mirror of
https://github.com/frappe/books.git
synced 2024-11-14 01:14:03 +00:00
9877bf4fd3
- allow nulls in converter else not not null'll fail
41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
import { Doc } from 'fyo/model/doc';
|
|
|
|
function getPaddedName(prefix: string, next: number, padZeros: number): string {
|
|
return prefix + next.toString().padStart(padZeros ?? 4, '0');
|
|
}
|
|
|
|
export default class NumberSeries extends Doc {
|
|
validate() {
|
|
const current = this.get('current') as number | null;
|
|
if (current) {
|
|
this.current = this.get('start');
|
|
}
|
|
}
|
|
|
|
async next(schemaName: string) {
|
|
this.validate();
|
|
|
|
const exists = await this.checkIfCurrentExists(schemaName);
|
|
if (!exists) {
|
|
return this.getPaddedName(this.current as number);
|
|
}
|
|
|
|
this.current = (this.current as number) + 1;
|
|
await this.sync();
|
|
return this.getPaddedName(this.current as number);
|
|
}
|
|
|
|
async checkIfCurrentExists(schemaName: string) {
|
|
if (!schemaName) {
|
|
return true;
|
|
}
|
|
|
|
const name = this.getPaddedName(this.current as number);
|
|
return await this.fyo.db.exists(schemaName, name);
|
|
}
|
|
|
|
getPaddedName(next: number): string {
|
|
return getPaddedName(this.name as string, next, this.padZeros as number);
|
|
}
|
|
}
|