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');
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 groupingMode = lastBuy && lastBuy.groupingMode ? lastBuy.groupingMode : 1;
const weight = lastBuy && lastBuy.weight ? lastBuy.weight : 1;
const quantity = (stickers * buy.packing);
buys.push({
entryFk: entry.id,
@ -105,8 +103,8 @@ module.exports = Self => {
grouping: buy.grouping,
buyingValue: buy.buyingValue,
packageFk: buy.packageFk,
groupingMode: groupingMode,
weight: weight
groupingMode: lastBuy.groupingMode,
weight: lastBuy.weight
});
await models.ItemMatchProperties.upsert({
@ -117,6 +115,8 @@ module.exports = Self => {
}, myOptions);
}
if (buys.length) return;
const createdBuys = await models.Buy.create(buys, myOptions);
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()', () => {
const buyerId = 35;
const companyId = 442;
const travelId = 1;
const supplierId = 1;
const entryId = 3;
const activeCtx = {
accessToken: {userId: buyerId},
};
@ -51,20 +49,12 @@ describe('entry import()', () => {
const tx = await models.Entry.beginTransaction({});
try {
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({
where: {entryFk: newEntry.id}
where: {entryFk: entryId}
}, options);
expect(updatedEntry.observation).toEqual(expectedObservation);

View File

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

View File

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

View File

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

View File

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

View File

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