2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

feat: update name setting

This commit is contained in:
18alantom 2022-03-08 13:42:08 +05:30
parent 2de72153e4
commit 78955b9d52
3 changed files with 48 additions and 29 deletions

View File

@ -28,11 +28,23 @@ module.exports = {
return; return;
} }
// Current, per doc number series
if (doc.numberSeries) {
doc.name = await this.getSeriesNext(doc.numberSeries, doc.doctype);
return;
}
// if(doc.meta)
// Legacy, using doc settings for number series
if (doc.meta.settings) { if (doc.meta.settings) {
const numberSeries = (await doc.getSettings()).numberSeries; const numberSeries = (await doc.getSettings()).numberSeries;
if (numberSeries) { if (!numberSeries) {
doc.name = await this.getSeriesNext(numberSeries, doc.doctype); return;
} }
doc.name = await this.getSeriesNext(numberSeries, doc.doctype);
return;
} }
} }
@ -84,27 +96,34 @@ module.exports = {
if (!e.statusCode || e.statusCode !== 404) { if (!e.statusCode || e.statusCode !== 404) {
throw e; throw e;
} }
await this.createNumberSeries(prefix);
await this.createNumberSeries(prefix, doctype);
series = await frappe.getDoc('NumberSeries', prefix); series = await frappe.getDoc('NumberSeries', prefix);
} }
let next = await series.next(doctype); let next = await series.next(doctype);
return prefix + next; console.log(next, series.pad, series)
const pad = series.padZeros ?? 4;
const l = next.toString().length;
const z = '0'.repeat(Math.max(0, pad - l));
return prefix + z + next;
}, },
async createNumberSeries(prefix, setting, start = 1001) { async createNumberSeries(prefix, referenceType, start = 1001) {
if (!(await frappe.db.exists('NumberSeries', prefix))) { const exists = await frappe.db.exists('NumberSeries', prefix);
if (exists) {
return;
}
const series = frappe.newDoc({ const series = frappe.newDoc({
doctype: 'NumberSeries', doctype: 'NumberSeries',
name: prefix, name: prefix,
current: start, start,
referenceType,
}); });
await series.insert();
if (setting) { await series.insert();
const settingDoc = await frappe.getSingle(setting);
settingDoc.numberSeries = series.name;
await settingDoc.update();
}
}
}, },
}; };

View File

@ -3,8 +3,8 @@ const BaseDocument = require('frappe/model/document');
module.exports = class NumberSeries extends BaseDocument { module.exports = class NumberSeries extends BaseDocument {
validate() { validate() {
if (this.current === null || this.current === undefined) { if (!this.current) {
this.current = 0; this.current = this.start;
} }
} }
async next(doctype) { async next(doctype) {

View File

@ -20,15 +20,15 @@ export default async function postStart() {
frappe.metaCache = {}; frappe.metaCache = {};
// init naming series if missing // init naming series if missing
await naming.createNumberSeries('SINV-', 'SalesInvoiceSettings'); await naming.createNumberSeries('SINV-', 'SalesInvoice');
await naming.createNumberSeries('PINV-', 'PurchaseInvoiceSettings'); await naming.createNumberSeries('PINV-', 'PurchaseInvoice');
await naming.createNumberSeries('PAY-', 'PaymentSettings'); await naming.createNumberSeries('PAY-', 'Payment');
await naming.createNumberSeries('JV-', 'JournalEntrySettings'); await naming.createNumberSeries('JV-', 'JournalEntry');
await naming.createNumberSeries('QTN-', 'QuotationSettings'); // await naming.createNumberSeries('QTN-', 'QuotationSettings');
await naming.createNumberSeries('SO-', 'SalesOrderSettings'); // await naming.createNumberSeries('SO-', 'SalesOrderSettings');
await naming.createNumberSeries('OF-', 'FulfillmentSettings'); // await naming.createNumberSeries('OF-', 'FulfillmentSettings');
await naming.createNumberSeries('PO-', 'PurchaseOrderSettings'); // await naming.createNumberSeries('PO-', 'PurchaseOrderSettings');
await naming.createNumberSeries('PREC-', 'PurchaseReceiptSettings'); // await naming.createNumberSeries('PREC-', 'PurchaseReceiptSettings');
// fetch singles // fetch singles
// so that they are available synchronously // so that they are available synchronously