feat: refs #7644 Optimized entry labels report
gitea/salix/pipeline/pr-master There was a failure building this commit Details

This commit is contained in:
Guillermo Bonet 2024-08-05 11:29:35 +02:00
parent e1dc7a3cb9
commit ab446b54ed
9 changed files with 107 additions and 40 deletions

View File

@ -368,5 +368,6 @@
"Payment method is required": "El método de pago es obligatorio", "Payment method is required": "El método de pago es obligatorio",
"Cannot send mail": "Não é possível enviar o email", "Cannot send mail": "Não é possível enviar o email",
"CONSTRAINT `supplierAccountTooShort` failed for `vn`.`supplier`": "La cuenta debe tener exactamente 10 dígitos", "CONSTRAINT `supplierAccountTooShort` failed for `vn`.`supplier`": "La cuenta debe tener exactamente 10 dígitos",
"The sale not exists in the item shelving": "La venta no existe en la estantería del artículo" "The sale not exists in the item shelving": "La venta no existe en la estantería del artículo",
"The entry not have stickers": "La entrada no tiene etiquetas"
} }

View File

@ -1,6 +1,6 @@
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('buyLabel', { Self.remoteMethodCtx('buyLabel', {
description: 'Returns the entry buys labels', description: 'Returns the entry buy labels',
accessType: 'READ', accessType: 'READ',
accepts: [ accepts: [
{ {

View File

@ -0,0 +1,58 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('print', {
description: 'Print stickers of all entries',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'The entry id',
http: {source: 'path'}
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'String',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'String',
http: {target: 'header'}
}
],
http: {
path: '/:id/print',
verb: 'GET'
},
accessScopes: ['DEFAULT', 'read:multimedia']
});
Self.print = async function(ctx, id, options) {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
// Importación dinámica porque no admite commonjs
const PDFMerger = ((await import('pdf-merger-js')).default);
const merger = new PDFMerger();
const buys = await models.Buy.find({where: {entryFk: id}}, myOptions);
for (const buy of buys) {
if (buy.stickers < 1) continue;
ctx.args.id = buy.id;
const pdfBuffer = await models.Entry.buyLabel(ctx, myOptions);
await merger.add(new Uint8Array(pdfBuffer[0]));
}
if (!merger._doc) throw new UserError('The entry not have stickers');
return [await merger.saveAsBuffer(), 'application/pdf', `filename="entry-${id}.pdf"`];
};
};

View File

@ -10,6 +10,7 @@ module.exports = Self => {
require('../methods/entry/addFromPackaging')(Self); require('../methods/entry/addFromPackaging')(Self);
require('../methods/entry/addFromBuy')(Self); require('../methods/entry/addFromBuy')(Self);
require('../methods/entry/buyLabel')(Self); require('../methods/entry/buyLabel')(Self);
require('../methods/entry/print')(Self);
Self.observe('before save', async function(ctx, options) { Self.observe('before save', async function(ctx, options) {
if (ctx.isNewInstance) return; if (ctx.isNewInstance) return;

View File

@ -43,6 +43,7 @@
"mysql": "2.18.1", "mysql": "2.18.1",
"node-ssh": "^11.0.0", "node-ssh": "^11.0.0",
"object.pick": "^1.3.0", "object.pick": "^1.3.0",
"pdf-merger-js": "^5.1.2",
"puppeteer": "21.11.0", "puppeteer": "21.11.0",
"read-chunk": "^3.2.0", "read-chunk": "^3.2.0",
"require-yaml": "0.0.1", "require-yaml": "0.0.1",
@ -80,10 +81,10 @@
"gulp-merge-json": "^1.3.1", "gulp-merge-json": "^1.3.1",
"gulp-nodemon": "^2.5.0", "gulp-nodemon": "^2.5.0",
"gulp-print": "^2.0.1", "gulp-print": "^2.0.1",
"gulp-wrap": "^0.15.0",
"gulp-yaml": "^1.0.1",
"gulp-rename": "^2.0.0", "gulp-rename": "^2.0.0",
"gulp-replace": "^1.1.4", "gulp-replace": "^1.1.4",
"gulp-wrap": "^0.15.0",
"gulp-yaml": "^1.0.1",
"html-loader": "^0.4.5", "html-loader": "^0.4.5",
"html-loader-jest": "^0.2.1", "html-loader-jest": "^0.2.1",
"html-webpack-plugin": "^5.5.1", "html-webpack-plugin": "^5.5.1",

View File

@ -86,7 +86,7 @@
<td> <td>
<div class="cell"> <div class="cell">
<span class="lbl">{{$t('boxNum')}}</span> <span class="lbl">{{$t('boxNum')}}</span>
{{`${buy.labelNum} / ${maxLabelNum}`}} {{`${buy.labelNum} / ${buy.maxLabelNum}`}}
</div> </div>
</td> </td>
</tr> </tr>

View File

@ -7,8 +7,9 @@ module.exports = {
name: 'buy-label', name: 'buy-label',
mixins: [vnReport], mixins: [vnReport],
async serverPrefetch() { async serverPrefetch() {
this.buys = await this.rawSqlFromDef('buys', [this.id, this.id]); const models = require('vn-loopback/server/server').models;
this.maxLabelNum = Math.max(...this.buys.map(buy => buy.labelNum)); const buy = await models.Buy.findById(this.id, null);
this.buys = await this.rawSqlFromDef('buy', [buy.entryFk, buy.entryFk, buy.entryFk, this.id]);
const date = new Date(); const date = new Date();
this.weekNum = moment(date).isoWeek(); this.weekNum = moment(date).isoWeek();
this.dayNum = moment(date).day(); this.dayNum = moment(date).day();

View File

@ -0,0 +1,38 @@
WITH RECURSIVE numbers AS (
SELECT 1 n
UNION ALL
SELECT n + 1
FROM numbers
WHERE n < (
SELECT MAX(stickers)
FROM buy
WHERE entryFk = ?
)
),
labels AS (
SELECT ROW_NUMBER() OVER(ORDER BY b.id, num.n) labelNum,
i.name,
i.`size`,
i.category,
ink.id color,
o.code,
b.packing,
b.`grouping`,
i.stems,
b.id,
b.itemFk,
p.name producer,
IF(i2.id, i2.comment, i.comment) comment
FROM buy b
JOIN item i ON i.id = b.itemFk
LEFT JOIN producer p ON p.id = i.producerFk
LEFT JOIN ink ON ink.id = i.inkFk
LEFT JOIN origin o ON o.id = i.originFk
LEFT JOIN item i2 ON i2.id = b.itemOriginalFk
JOIN numbers num
WHERE b.entryFk = ?
AND num.n <= b.stickers
)
SELECT *, (SELECT SUM(stickers) FROM buy WHERE entryFk = ?) maxLabelNum
FROM labels
WHERE id = ?

View File

@ -1,33 +0,0 @@
WITH RECURSIVE numbers AS (
SELECT 1 n
UNION ALL
SELECT n + 1
FROM numbers
WHERE n < (
SELECT MAX(stickers)
FROM buy
WHERE entryFk = ?
)
)
SELECT ROW_NUMBER() OVER(ORDER BY b.id, num.n) labelNum,
i.name,
i.`size`,
i.category,
ink.id color,
o.code,
b.packing,
b.`grouping`,
i.stems,
b.id,
b.itemFk,
p.name producer,
IF(i2.id, i2.comment, i.comment) comment
FROM buy b
JOIN item i ON i.id = b.itemFk
LEFT JOIN producer p ON p.id = i.producerFk
LEFT JOIN ink ON ink.id = i.inkFk
LEFT JOIN origin o ON o.id = i.originFk
LEFT JOIN item i2 ON i2.id = b.itemOriginalFk
JOIN numbers num
WHERE b.entryFk = ?
AND num.n <= b.stickers