Filters items with last item buy

This commit is contained in:
Joan Sanchez 2022-01-28 09:35:30 +01:00
parent bacc0101db
commit c5ffc16486
8 changed files with 122 additions and 29 deletions

View File

@ -88,13 +88,11 @@ module.exports = Self => {
throw new Error('The item is required'); throw new Error('The item is required');
const lastItemBuy = lastItemBuys.get(buy.itemFk); const lastItemBuy = lastItemBuys.get(buy.itemFk);
let lastBuy;
if (lastItemBuy)
lastBuy = await models.Buy.findById(lastItemBuy.buyFk, null, myOptions);
if (!lastItemBuy) continue;
const lastBuy = await models.Buy.findById(lastItemBuy.buyFk, null, myOptions);
const stickers = 1; const stickers = 1;
const groupingMode = lastBuy && lastBuy.groupingMode ? lastBuy.groupingMode : 1;
const weight = lastBuy && lastBuy.weight ? lastBuy.weight : 1;
const quantity = (stickers * buy.packing); const quantity = (stickers * buy.packing);
buys.push({ buys.push({
entryFk: entry.id, entryFk: entry.id,
@ -105,8 +103,8 @@ module.exports = Self => {
grouping: buy.grouping, grouping: buy.grouping,
buyingValue: buy.buyingValue, buyingValue: buy.buyingValue,
packageFk: buy.packageFk, packageFk: buy.packageFk,
groupingMode: groupingMode, groupingMode: lastBuy.groupingMode,
weight: weight weight: lastBuy.weight
}); });
await models.ItemMatchProperties.upsert({ await models.ItemMatchProperties.upsert({
@ -117,6 +115,8 @@ module.exports = Self => {
}, myOptions); }, myOptions);
} }
if (buys.length) return;
const createdBuys = await models.Buy.create(buys, myOptions); const createdBuys = await models.Buy.create(buys, myOptions);
const buyIds = createdBuys.map(buy => buy.id); const buyIds = createdBuys.map(buy => buy.id);

View File

@ -0,0 +1,91 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => {
Self.remoteMethod('lastItemBuys', {
description: 'Returns a list of buys',
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'origin itemId',
http: {source: 'path'}
},
{
arg: 'filter',
type: 'object',
description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string`
}
],
returns: {
type: ['object'],
root: true
},
http: {
path: `/:id/lastItemBuys`,
verb: 'GET'
}
});
Self.lastItemBuys = async(id, filter, options) => {
const conn = Self.dataSource.connector;
const models = Self.app.models;
const where = {isActive: true};
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
filter = mergeFilters(filter, {where});
const entry = await models.Entry.findById(id, {
include: [{
relation: 'travel',
scope: {
fields: ['warehouseInFk', 'landed'],
}
}]
});
const travel = entry.travel();
const stmts = [];
let stmt;
stmt = new ParameterizedSQL(`CALL buyUltimate(?, ?)`, [
travel.warehouseInFk,
travel.landed
]);
stmts.push(stmt);
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.item');
stmt = new ParameterizedSQL(
`CREATE TEMPORARY TABLE tmp.item
(PRIMARY KEY (id))
ENGINE = MEMORY
SELECT
i.*,
p.name AS producerName,
nk.name AS inkName
FROM item i
JOIN producer p ON p.id = i.producerFk
JOIN ink nk ON nk.id = i.inkFk
JOIN tmp.buyUltimate bu ON i.id = bu.itemFk
AND bu.warehouseFk = ?
`, [travel.warehouseInFk]);
stmts.push(stmt);
stmt = new ParameterizedSQL('SELECT * FROM tmp.item');
stmt.merge(conn.makeSuffix(filter));
const itemsIndex = stmts.push(stmt) - 1;
stmts.push('DROP TEMPORARY TABLE tmp.item');
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions);
return result[itemsIndex];
};
};

View File

@ -3,9 +3,7 @@ const LoopBackContext = require('loopback-context');
describe('entry import()', () => { describe('entry import()', () => {
const buyerId = 35; const buyerId = 35;
const companyId = 442; const entryId = 3;
const travelId = 1;
const supplierId = 1;
const activeCtx = { const activeCtx = {
accessToken: {userId: buyerId}, accessToken: {userId: buyerId},
}; };
@ -51,20 +49,12 @@ describe('entry import()', () => {
const tx = await models.Entry.beginTransaction({}); const tx = await models.Entry.beginTransaction({});
try { try {
const options = {transaction: tx}; const options = {transaction: tx};
const newEntry = await models.Entry.create({
dated: new Date(),
supplierFk: supplierId,
travelFk: travelId,
companyFk: companyId,
observation: 'The entry',
ref: 'Entry ref'
}, options);
await models.Entry.importBuys(ctx, newEntry.id, options); await models.Entry.importBuys(ctx, entryId, options);
const updatedEntry = await models.Entry.findById(newEntry.id, null, options); const updatedEntry = await models.Entry.findById(entryId, null, options);
const entryBuys = await models.Buy.find({ const entryBuys = await models.Buy.find({
where: {entryFk: newEntry.id} where: {entryFk: entryId}
}, options); }, options);
expect(updatedEntry.observation).toEqual(expectedObservation); expect(updatedEntry.observation).toEqual(expectedObservation);

View File

@ -5,4 +5,5 @@ module.exports = Self => {
require('../methods/entry/addBuy')(Self); require('../methods/entry/addBuy')(Self);
require('../methods/entry/importBuys')(Self); require('../methods/entry/importBuys')(Self);
require('../methods/entry/importBuysPreview')(Self); require('../methods/entry/importBuysPreview')(Self);
require('../methods/entry/lastItemBuys')(Self);
}; };

View File

@ -59,7 +59,7 @@
<vn-autocomplete <vn-autocomplete
class="dense" class="dense"
vn-focus vn-focus
url="Items/withName" url="Entries/{{$ctrl.$params.id}}/lastItemBuys"
ng-model="buy.itemFk" ng-model="buy.itemFk"
show-field="name" show-field="name"
value-field="id" value-field="id"
@ -119,15 +119,18 @@
<vn-textfield <vn-textfield
label="Name" label="Name"
ng-model="$ctrl.itemFilterParams.name" ng-model="$ctrl.itemFilterParams.name"
ng-keydown="$ctrl.onKeyPress($event)"
vn-focus> vn-focus>
</vn-textfield> </vn-textfield>
<vn-textfield <vn-textfield
label="Size" label="Size"
ng-model="$ctrl.itemFilterParams.size"> ng-model="$ctrl.itemFilterParams.size"
ng-keydown="$ctrl.onKeyPress($event)">
</vn-textfield> </vn-textfield>
<vn-autocomplete <vn-autocomplete
label="Producer" label="Producer"
ng-model="$ctrl.itemFilterParams.producerFk" ng-model="$ctrl.itemFilterParams.producerFk"
ng-keydown="$ctrl.onKeyPress($event)"
url="Producers" url="Producers"
show-field="name" show-field="name"
value-field="id"> value-field="id">
@ -135,6 +138,7 @@
<vn-autocomplete <vn-autocomplete
label="Type" label="Type"
ng-model="$ctrl.itemFilterParams.typeFk" ng-model="$ctrl.itemFilterParams.typeFk"
ng-keydown="$ctrl.onKeyPress($event)"
url="ItemTypes" url="ItemTypes"
show-field="name" show-field="name"
value-field="id"> value-field="id">
@ -142,6 +146,7 @@
<vn-autocomplete <vn-autocomplete
label="Color" label="Color"
ng-model="$ctrl.itemFilterParams.inkFk" ng-model="$ctrl.itemFilterParams.inkFk"
ng-keydown="$ctrl.onKeyPress($event)"
url="Inks" url="Inks"
show-field="name" show-field="name"
value-field="id"> value-field="id">
@ -155,7 +160,7 @@
</vn-horizontal> </vn-horizontal>
<vn-crud-model <vn-crud-model
vn-id="itemsModel" vn-id="itemsModel"
url="Items/withName" url="Entries/{{$ctrl.$params.id}}/lastItemBuys"
filter="$ctrl.itemFilter" filter="$ctrl.itemFilter"
data="items" data="items"
limit="10"> limit="10">
@ -186,8 +191,8 @@
</vn-td> </vn-td>
<vn-td expand>{{::item.name}}</vn-td> <vn-td expand>{{::item.name}}</vn-td>
<vn-td number>{{::item.size}}</vn-td> <vn-td number>{{::item.size}}</vn-td>
<vn-td expand>{{::item.producer.name}}</vn-td> <vn-td expand>{{::item.producerName}}</vn-td>
<vn-td>{{::item.ink.name}}</vn-td> <vn-td>{{::item.inkName}}</vn-td>
</a> </a>
</vn-tbody> </vn-tbody>
</vn-table> </vn-table>

View File

@ -128,7 +128,7 @@ class Controller extends Section {
switch (key) { switch (key) {
case 'name': case 'name':
where[key] = {like: `%${value}%`}; where['i.name'] = {like: `%${value}%`};
break; break;
case 'producerFk': case 'producerFk':
case 'typeFk': case 'typeFk':
@ -141,6 +141,11 @@ class Controller extends Section {
filter.where = where; filter.where = where;
this.$.itemsModel.applyFilter(filter); this.$.itemsModel.applyFilter(filter);
} }
onKeyPress($event) {
if ($event.key === 'Enter')
this.filter();
}
} }
Controller.$inject = ['$element', '$scope']; Controller.$inject = ['$element', '$scope'];

View File

@ -28,3 +28,4 @@ Real hour: Hora real
T. Hour: Hora T. T. Hour: Hora T.
Theoretical hour: Hora Teórica Theoretical hour: Hora Teórica
Go to the order: Ir al pedido Go to the order: Ir al pedido
Basket: Cesta

View File

@ -7,7 +7,7 @@
<vn-icon-button icon="launch"></vn-icon-button> <vn-icon-button icon="launch"></vn-icon-button>
</a> </a>
<span> <span>
Ticket #{{$ctrl.summary.id}} - {{$ctrl.summary.client.name}} <span translate>Basket</span> #{{$ctrl.summary.id}} - {{$ctrl.summary.client.name}}
({{$ctrl.summary.client.salesPersonFk}}) ({{$ctrl.summary.client.salesPersonFk}})
</span> </span>
<vn-button <vn-button