2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/frappe/models/NumberSeries.ts
18alantom 0efd2b4c33 refactor: remove core js schemas
- switch to doc and json schemas instead
2022-05-23 16:18:21 +05:30

39 lines
959 B
TypeScript

import { getPaddedName } from '@/utils';
import frappe from 'frappe';
import Doc from 'frappe/model/doc';
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.update();
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 frappe.db.exists(schemaName, name);
}
getPaddedName(next: number): string {
return getPaddedName(this.name, next, this.padZeros);
}
}