salix/print/templates/reports/item-label-barcode/item-label-barcode.js

60 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-10-22 11:43:54 +00:00
const UserError = require('vn-loopback/util/user-error');
const {DOMImplementation, XMLSerializer} = require('xmldom');
const moment = require('moment');
const jsbarcode = require('jsbarcode');
module.exports = {
2024-10-24 10:18:09 +00:00
name: 'item-label-barcode',
2024-10-22 11:43:54 +00:00
async serverPrefetch() {
this.date = Date.vnNew();
if (this.typeId === 'item') {
this.company = await this.findOneFromDef('company', [this.warehouseId]);
if (!this.company)
throw new UserError(`There is no company associated with that warehouse`);
this.lastBuy = await this.findOneFromDef('lastBuy', [
this.id,
this.warehouseId,
this.date
]);
}
this.items = await this.rawSqlFromDef('item', [this.copies || 1, this.lastBuy?.id || this.id]);
2024-10-22 11:43:54 +00:00
if (!this.items.length) throw new UserError(`Empty data source`);
this.date = moment(this.date).format('WW/E');
},
methods: {
getBarcode(data) {
const document = new DOMImplementation().createDocument('http://www.w3.org/1999/xhtml', 'html', null);
const svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
jsbarcode(svgNode, data, {
xmlDocument: document,
format: 'code128',
displayValue: false,
2024-10-23 07:46:06 +00:00
width: 3.8,
2024-10-22 11:43:54 +00:00
height: 85,
margin: 0
});
return new XMLSerializer().serializeToString(svgNode);
}
},
props: {
id: {
type: Number,
required: true,
description: 'The item id'
},
warehouseId: {
type: Number
},
packing: {
type: Number
},
copies: {
type: Number
},
typeId: {
type: String
2024-10-22 11:43:54 +00:00
}
}
};