2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 03:19:01 +00:00

add number series

This commit is contained in:
Rushabh Mehta 2018-02-07 22:14:59 +05:30
parent 2a2f1fb6f3
commit 66b3dc6dc7
6 changed files with 81 additions and 2 deletions

View File

@ -296,7 +296,7 @@ class sqliteDatabase {
this.conn.run(query, params, (err) => {
if (err) {
if (debug) {
console.error(err);
console.log(err);
}
reject(err);
} else {

View File

@ -91,10 +91,12 @@ class TableControl extends BaseControl {
primary_label: frappe._('Submit'),
primary_action: (modal) => {
this.datatable.cellmanager.submitEditing();
this.datatable.cellmanager.deactivateEditing();
modal.hide();
}
});
this.text_modal.$modal.on('hidden.bs.modal', () => {
this.datatable.cellmanager.deactivateEditing();
})
return this.get_control(field, this.text_modal.get_body());
}

View File

@ -1,4 +1,20 @@
const frappe = require('frappejs');
module.exports = {
async get_series_next(prefix) {
let series;
try {
series = await frappe.get_doc('Number Series', prefix);
} catch (e) {
if (!e.status_code || e.status_code !== 404) {
throw e;
}
series = frappe.new_doc({doctype: 'Number Series', name: prefix, current: 0});
await series.insert();
}
let next = await series.next()
return prefix + next;
},
common_fields: [
{
fieldname: 'name', fieldtype: 'Data', reqd: 1

View File

@ -0,0 +1,24 @@
const BaseMeta = require('frappejs/model/meta');
const BaseDocument = require('frappejs/model/document');
class NumberSeriesMeta extends BaseMeta {
setup_meta() {
Object.assign(this, require('./number_series.json'));
}
}
class NumberSeries extends BaseDocument {
async next() {
if (this.current===null || this.current===undefined) {
this.current = 0;
}
this.current++;
await this.update();
return this.current;
}
}
module.exports = {
Document: NumberSeries,
Meta: NumberSeriesMeta
};

View File

@ -0,0 +1,21 @@
{
"name": "Number Series",
"doctype": "DocType",
"is_single": 0,
"is_child": 0,
"keyword_fields": [],
"fields": [
{
"fieldname": "name",
"label": "Name",
"fieldtype": "Data",
"reqd": 1
},
{
"fieldname": "current",
"label": "Current",
"fieldtype": "Int",
"reqd": 1
}
]
}

View File

@ -0,0 +1,16 @@
const assert = require('assert');
const frappe = require('frappejs');
const helpers = require('./helpers');
describe('Document', () => {
before(async function() {
await helpers.init_sqlite();
});
it('should start a series and get next value', async () => {
frappe.db.delete('Number Series', 'test-series-')
assert.equal(await frappe.model.get_series_next('test-series-'), 'test-series-1');
assert.equal(await frappe.model.get_series_next('test-series-'), 'test-series-2');
assert.equal(await frappe.model.get_series_next('test-series-'), 'test-series-3');
});
});