#1104 order.index mostar mas campos
gitea/salix/dev This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2019-02-27 12:03:54 +01:00
parent c13253dcc2
commit 048c5c2ad8
12 changed files with 358 additions and 66 deletions

View File

@ -0,0 +1,152 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const buildFilter = require('vn-loopback/util/filter').buildFilter;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => {
Self.remoteMethod('filter', {
description: 'Find all instances of the model matched by filter from the data source.',
accepts: [
{
arg: 'ctx',
type: 'Object',
http: {source: 'context'}
}, {
arg: 'filter',
type: 'Object',
description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string`
}, {
arg: 'search',
type: 'String',
description: `If it's and integer searchs by id, otherwise it searchs by nickname`
}, {
arg: 'from',
type: 'Date',
description: `The from date`
}, {
arg: 'to',
type: 'Date',
description: `The to date`
}, {
arg: 'id',
type: 'Integer',
description: `The ticket id`
}, {
arg: 'clientFk',
type: 'Integer',
description: `The client id`
}, {
arg: 'agencyModeFk',
type: 'Integer',
description: `The agency mode id`
}, {
arg: 'workerFk',
type: 'Integer',
description: `The salesperson id`
}, {
arg: 'isConfirmed',
type: 'Boolean',
description: `Order is confirmed`
}
],
returns: {
type: ['Object'],
root: true
},
http: {
path: '/filter',
verb: 'GET'
}
});
Self.filter = async(ctx, filter) => {
let conn = Self.dataSource.connector;
let where = buildFilter(ctx.args, (param, value) => {
switch (param) {
case 'search':
return {'o.id': value};
case 'from':
return {'o.date_send': {gte: value}};
case 'to':
return {'o.date_send': {lte: value}};
case 'workerFk':
return {'c.salesPersonFk': value};
case 'clientFk':
return {'o.customer_id': value};
case 'agencyModeFk':
return {'o.agency_id': value};
case 'sourceApp':
return {'o.source_app': value};
case 'isConfirmed':
return {'o.confirmed': value ? 1 : 0};
case 'id':
param = `o.${param}`;
return {[param]: value};
}
});
filter = mergeFilters(filter, {where});
let stmts = [];
let stmt;
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.filter');
stmt = new ParameterizedSQL(
`CREATE TEMPORARY TABLE tmp.filter
(INDEX (id))
ENGINE = MEMORY
SELECT
o.id,
o.date_send landed,
o.date_make created,
o.customer_id clientFk,
o.agency_id agencyModeFk,
o.address_id addressFk,
o.company_id companyFk,
o.source_app sourceApp,
o.confirmed isConfirmed,
c.name clientName,
u.nickname workerNickname,
u.id userId,
co.code companyCode
FROM hedera.order o
LEFT JOIN address a ON a.id = o.address_id
LEFT JOIN agencyMode am ON am.id = o.agency_id
LEFT JOIN client c ON c.id = o.customer_id
LEFT JOIN worker wk ON wk.id = c.salesPersonFk
LEFT JOIN account.user u ON u.id = wk.userFk
LEFT JOIN company co ON co.id = o.company_id`);
stmt.merge(conn.makeSuffix(filter));
stmts.push(stmt);
stmts.push(`
CREATE TEMPORARY TABLE tmp.order
(INDEX (orderFk))
ENGINE = MEMORY
SELECT id AS orderFk
FROM tmp.filter`);
stmts.push('CALL hedera.orderGetTotal()');
let orderIndex = stmts.push(`
SELECT f.*, ot.*
FROM tmp.filter f
LEFT JOIN tmp.orderTotal ot ON ot.orderFk = f.id`) - 1;
stmts.push(`
DROP TEMPORARY TABLE
tmp.order,
tmp.orderTotal`);
stmts.push(
`DROP TEMPORARY TABLE
tmp.filter`);
let sql = ParameterizedSQL.join(stmts, ';');
let result = await conn.executeStmt(sql);
return result[orderIndex];
};
};

View File

@ -1,3 +1,5 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
module.exports = Self => {
Self.remoteMethod('getTaxes', {
description: 'Gets the taxes of a given order',
@ -20,11 +22,32 @@ module.exports = Self => {
});
Self.getTaxes = async orderFk => {
let query = `CALL hedera.orderGetTax(?);
SELECT * FROM tmp.orderTax;`;
let res = await Self.rawSql(query, [orderFk]);
let taxes = res[1];
let conn = Self.dataSource.connector;
let stmts = [];
let stmt;
return taxes;
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.order');
stmt = new ParameterizedSQL(`
CREATE TEMPORARY TABLE tmp.order
(INDEX (orderFk))
ENGINE = MEMORY
SELECT ? AS orderFk`, [orderFk]);
stmts.push(stmt);
stmts.push('CALL hedera.orderGetTax()');
let orderTaxIndex = stmts.push('SELECT * FROM tmp.orderTax') - 1;
stmts.push(`
DROP TEMPORARY TABLE
tmp.order,
tmp.orderTax`);
let sql = ParameterizedSQL.join(stmts, ';');
let result = await conn.executeStmt(sql);
return result[orderTaxIndex];
};
};

View File

@ -0,0 +1,32 @@
const app = require('vn-loopback/server/server');
describe('order filter()', () => {
it('should call the filter method with a basic search', async() => {
let ctx = {params: {}};
let filter = {where: {'o.id': 2}};
let result = await app.models.Order.filter(ctx, filter);
let orderId = result[0].id;
expect(orderId).toEqual(2);
});
it('should call the filter method with a single advanced search', async() => {
let ctx = {params: {}};
let filter = {where: {'o.confirmed': false}};
let result = await app.models.Order.filter(ctx, filter);
expect(result.length).toEqual(6);
});
it('should call the filter method with a complex advanced search', async() => {
let ctx = {params: {}};
let filter = {where: {'o.confirmed': false, 'c.salesPersonFk': 18}};
let result = await app.models.Order.filter(ctx, filter);
expect(result.length).toEqual(1);
expect(result[0].id).toEqual(16);
});
});

View File

@ -2,7 +2,7 @@ const app = require('vn-loopback/server/server');
describe('order getTaxes()', () => {
it('should call the getTaxes method and return undefined if its called with a string', async() => {
let result = await app.models.Order.getTaxes('pepinillos');
let result = await app.models.Order.getTaxes('string');
expect(result.length).toEqual(0);
});

View File

@ -12,4 +12,5 @@ module.exports = Self => {
require('../methods/order/newFromTicket')(Self);
require('../methods/order/updateBasicData')(Self);
require('../methods/order/confirm')(Self);
require('../methods/order/filter')(Self);
};

View File

@ -1,24 +1,24 @@
<vn-crud-model
vn-id="model"
url="/order/api/Orders"
filter="::$ctrl.filter"
url="/api/Orders/filter"
limit="20"
data="orders" auto-load="false">
data="orders"
auto-load="false">
</vn-crud-model>
<div class="content-block">
<div class="vn-list">
<vn-card pad-medium-h>
<vn-searchbar auto-load="false"
<vn-searchbar
panel="vn-order-search-panel"
model="model"
expr-builder="$ctrl.exprBuilder(param, value)"
auto-load="true"
on-search="$ctrl.onSearch($params)"
vn-focus>
</vn-searchbar>
</vn-card>
</div>
<vn-card margin-medium-v>
<vn-table model="model">
<vn-table
model="model"
auto-load="false">
<vn-thead>
<vn-tr>
<vn-th field="id" default-order="DESC" number>Id</vn-th>
@ -29,6 +29,7 @@
<vn-th field="created" center>Created</vn-th>
<vn-th field="created" center>Landed</vn-th>
<vn-th field="companyFk">Company</vn-th>
<vn-th field="total" center>Total</vn-th>
</vn-tr>
</vn-thead>
<vn-tbody>
@ -37,14 +38,14 @@
<vn-td number>{{::order.id}}</vn-td>
<vn-td expand>
<span class="link" ng-click="$ctrl.showClientDescriptor($event, order.clientFk)">
{{::order.client.name}}
{{::order.clientName}}
</span>
</vn-td>
<vn-td expand>
<span
class="link"
ng-click="$ctrl.showWorkerDescriptor($event, order.client.salesPerson.user.id)">
{{::order.client.salesPerson.user.nickname | dashIfEmpty}}
ng-click="$ctrl.showWorkerDescriptor($event, order.userId)">
{{::order.workerNickname | dashIfEmpty}}
</span>
</vn-td>
<vn-td center>
@ -56,7 +57,8 @@
<vn-td>{{::order.sourceApp}}</vn-td>
<vn-td center>{{::order.created | date:'dd/MM/yyyy HH:mm'}}</vn-td>
<vn-td center>{{::order.landed | date:'dd/MM/yyyy'}}</vn-td>
<vn-td>{{::order.company.code}}</vn-td>
<vn-td>{{::order.companyCode}}</vn-td>
<vn-td number>{{::order.total | currency: 'EUR': 2 | dashIfEmpty}}</vn-td>
<vn-td shrink>
<vn-icon-button
ng-click="$ctrl.preview($event, order)"

View File

@ -1,54 +1,18 @@
import ngModule from '../module';
export default class Controller {
constructor($scope) {
constructor($scope, $state, $stateParams) {
this.$stateParams = $stateParams;
this.$state = $state;
this.$ = $scope;
this.ticketSelected = null;
this.filter = {
include: [
{
relation: 'client',
scope: {
fields: ['name', 'salesPersonFk'],
include: {
relation: 'salesPerson',
scope: {
fields: ['userFk'],
include: {
relation: 'user',
scope: {
fields: ['nickname']
}
}
}
}
}
},
{
relation: 'company',
fields: ['code']
}
]
};
}
exprBuilder(param, value) {
switch (param) {
case 'search':
return {id: value};
case 'from':
return {landed: {gte: value}};
case 'to':
return {landed: {lte: value}};
case 'id':
case 'clientFk':
case 'workerFk':
case 'sourceApp':
case 'agencyModeFk':
case 'isConfirmed':
return {[param]: value};
}
onSearch(params) {
if (params)
this.$.model.applyFilter(null, params);
else
this.$.model.clear();
}
showClientDescriptor(event, clientFk) {
@ -79,7 +43,7 @@ export default class Controller {
}
}
Controller.$inject = ['$scope'];
Controller.$inject = ['$scope', '$state', '$stateParams'];
ngModule.component('vnOrderIndex', {
template: require('./index.html'),

View File

@ -51,14 +51,16 @@
</vn-date-picker>
</vn-horizontal>
<vn-horizontal>
<vn-autocomplete vn-one
<vn-autocomplete
vn-one
label="Application"
field="filter.sourceApp"
url="/order/api/Orders/getSourceValues"
show-field="value"
value-field="value">
</vn-autocomplete>
<vn-check vn-one style="padding-top: 1em"
<vn-check
vn-one
label="Order confirmed"
triple-state="true"
field="filter.isConfirmed">

View File

@ -10,7 +10,7 @@ echo "[INFO] -> Imported ./dump/mysqlPlugins.sql"
mysql -u root -f < ./dump/mysqlPlugins.sql
# Import changes
for file in changes/*/*.sql; do
for file in changes/*.sql; do
echo "[INFO] -> Imported ./$file"
mysql -u root -fc < $file
done

View File

@ -0,0 +1,29 @@
USE `hedera`;
DROP procedure IF EXISTS `orderGetTotal`;
DELIMITER $$
USE `hedera`$$
CREATE DEFINER=`root`@`%` PROCEDURE `orderGetTotal`()
BEGIN
/**
* Calcula el total con IVA para un conjunto de orders.
*
* @table tmp.order(orderFk) Identificadores de las ordenes a calcular
* @return tmp.orderTotal Total para cada order
*/
CALL orderGetTax;
DROP TEMPORARY TABLE IF EXISTS tmp.orderTotal;
CREATE TEMPORARY TABLE tmp.orderTotal
(INDEX (orderFk))
ENGINE = MEMORY
SELECT o.orderFk, IFNULL(SUM(ot.taxBase + ot.tax + ot.equalizationTax), 0.0) AS total
FROM tmp.order o
LEFT JOIN tmp.orderTax ot ON o.orderFk = ot.orderFk
GROUP BY orderFk;
DROP TEMPORARY TABLE IF EXISTS tmp.orderTax;
END$$
DELIMITER ;

View File

@ -0,0 +1,36 @@
USE `hedera`;
DROP FUNCTION IF EXISTS `orderGetTotal`;
DELIMITER $$
USE `hedera`$$
CREATE DEFINER=`root`@`%` FUNCTION `orderGetTotal`(vOrder INT) RETURNS decimal(10,2)
READS SQL DATA
DETERMINISTIC
BEGIN
/**
* Obtiene el total de un pedido con el IVA y el recargo de
* equivalencia incluidos.
*
* @param vOrder El identificador del pedido
* @return El total del pedido
*/
DECLARE vTotal DECIMAL(10,2);
DROP TEMPORARY TABLE IF EXISTS tmp.order;
CREATE TEMPORARY TABLE tmp.order
ENGINE = MEMORY
SELECT vOrder orderFk;
CALL orderGetTotal;
SELECT total INTO vTotal FROM tmp.orderTotal;
DROP TEMPORARY TABLE
tmp.order,
tmp.orderTotal;
RETURN vTotal;
END$$
DELIMITER ;

View File

@ -0,0 +1,51 @@
USE `hedera`;
DROP procedure IF EXISTS `orderGetTax`;
DELIMITER $$
USE `hedera`$$
CREATE DEFINER=`root`@`%` PROCEDURE `orderGetTax`()
READS SQL DATA
BEGIN
/**
* Calcula el IVA, y el recargo de equivalencia de un pedido
* desglosados por tipos.
*
* @tabla tmp.order Contiene los identificadores de los pedidos
* @treturn tmp.orderTax Bases imponibles, IVA y recargo de equivalencia
*/
CALL vn.taxGetRates (NULL);
-- Calcula el IVA y el recargo desglosado.
DROP TEMPORARY TABLE IF EXISTS tmp.orderTax;
CREATE TEMPORARY TABLE tmp.orderTax
(INDEX (orderFk))
ENGINE = MEMORY
SELECT id orderFk, t.type, t.taxBase,
CAST(IF(t.hasTax, t.taxBase * x.rate, 0) AS DECIMAL(10,2)) tax,
CAST(IF(t.hasEqualizationTax, t.taxBase * x.equalizationTax, 0) AS DECIMAL(10,2)) equalizationTax
FROM (
SELECT o.id, g.countryFk, g.type
,SUM(CAST(m.amount * m.price AS DECIMAL(10,2))) taxBase
,NOT(c.isVies AND p.countryFk <> c.countryFk) hasTax
,c.isEqualizated != FALSE AS hasEqualizationTax
FROM `order` o
JOIN tmp.order tmpo ON tmpo.orderFk = o.id
JOIN orderRow m ON m.orderFk = o.id
JOIN vn.item a ON a.id = m.itemFk
JOIN vn.client c ON c.id = o.customer_id
JOIN vn.supplier p ON p.id = o.company_id
JOIN tmp.taxClass g
ON g.countryFk = p.countryFk AND g.taxClassFk = a.taxClassFk
GROUP BY o.id, g.type
) t
JOIN tmp.taxType x
ON x.countryFk = t.countryFk AND x.type = t.type;
DROP TEMPORARY TABLE
tmp.taxClass,
tmp.taxType;
END$$
DELIMITER;