Import ticket sales #693 to claim actions

This commit is contained in:
Joan Sanchez 2018-10-02 09:52:38 +02:00
parent 3de2bb85a2
commit b8d09bddb4
10 changed files with 194 additions and 10 deletions

View File

@ -81,8 +81,7 @@
}, },
"menu": { "menu": {
"icon": "icon-actions" "icon": "icon-actions"
}, }
"acl": ["salesAssistant"]
} }
] ]
} }

View File

@ -18,11 +18,16 @@
<vn-horizontal> <vn-horizontal>
<vn-tool-bar margin-medium-bottom> <vn-tool-bar margin-medium-bottom>
<vn-button <vn-button
vn-acl="salesAssistant"
label="Import claim" label="Import claim"
ng-click="$ctrl.importToNewRefundTicket()" ng-click="$ctrl.importToNewRefundTicket()"
vn-tooltip="Imports claim details"> vn-tooltip="Imports claim details">
</vn-button> </vn-button>
<vn-button
label="Import ticket"
ng-click="$ctrl.showLastTickets($event)"
vn-tooltip="Imports ticket lines">
</vn-button>
</vn-tool-bar> </vn-tool-bar>
</vn-horizontal> </vn-horizontal>
<vn-table model="model"> <vn-table model="model">
@ -124,3 +129,37 @@
</vn-table> </vn-table>
</tpl-body> </tpl-body>
</vn-dialog> </vn-dialog>
<vn-crud-model
vn-id="lastTicketsModel"
url="/claim/api/Tickets"
filter="{}"
data="lastTickets" auto-load="false">
</vn-crud-model>
<!-- Transfer Popover -->
<vn-popover class="lastTicketsPopover" vn-id="lastTicketsPopover">
<div class="ticketList" pad-medium>
<vn-table model="lastTicketsModel" class="vn-grid">
<vn-thead>
<vn-tr>
<vn-th field="id" number>ID</vn-th>
<vn-th field="shipped" default-order="DESC">F. envio</vn-th>
<vn-th>Agencia</vn-th>
<vn-th>Almacen</vn-th>
</vn-tr>
</vn-thead>
<vn-tbody>
<vn-tr
class="clickable"
ng-repeat="ticket in lastTickets"
ng-click="$ctrl.importTicketLines(ticket.id)">
<vn-td number>{{::ticket.id}}</vn-td>
<vn-td>{{::ticket.shipped | date: 'dd/MM/yyyy'}}</vn-td>
<vn-td>{{::ticket.agencyMode.name}}</vn-td>
<vn-td>{{::ticket.warehouse.name}}</vn-td>
</vn-tr>
</vn-tbody>
</vn-table>
</div>
</vn-popover>

View File

@ -2,14 +2,14 @@ import ngModule from '../module';
import './style.scss'; import './style.scss';
class Controller { class Controller {
constructor($state, $scope, $http, $translate, vnApp) { constructor($stateParams, $scope, $http, $translate, vnApp) {
this.$state = $state; this.$stateParams = $stateParams;
this.$ = $scope; this.$ = $scope;
this.$http = $http; this.$http = $http;
this.$translate = $translate; this.$translate = $translate;
this.vnApp = vnApp; this.vnApp = vnApp;
this.filter = { this.filter = {
where: {claimFk: $state.params.id}, where: {claimFk: $stateParams.id},
include: [ include: [
{relation: 'sale', {relation: 'sale',
scope: { scope: {
@ -60,7 +60,7 @@ class Controller {
} }
importToNewRefundTicket() { importToNewRefundTicket() {
let query = `claim/api/ClaimBeginnings/${this.$state.params.id}/importToNewRefundTicket`; let query = `claim/api/ClaimBeginnings/${this.$stateParams.id}/importToNewRefundTicket`;
this.$http.post(query).then(() => { this.$http.post(query).then(() => {
this.$.model.refresh(); this.$.model.refresh();
this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
@ -88,9 +88,39 @@ class Controller {
this.claimedTotal += (sale.sale.quantity * sale.sale.price) - ((sale.sale.discount * (sale.sale.quantity * sale.sale.price)) / 100); this.claimedTotal += (sale.sale.quantity * sale.sale.price) - ((sale.sale.discount * (sale.sale.quantity * sale.sale.price)) / 100);
}); });
} }
showLastTickets(event) {
let pastWeek = new Date();
pastWeek.setDate(-7);
let filter = {
include: [
{relation: 'agencyMode', fields: ['name']},
{relation: 'warehouse', fields: ['name']}
],
where: {
created: {gt: pastWeek}
}
};
this.$.lastTicketsModel.filter = filter;
this.$.lastTicketsModel.refresh();
this.$.lastTicketsPopover.parent = event.target;
this.$.lastTicketsPopover.show();
} }
Controller.$inject = ['$state', '$scope', '$http', '$translate', 'vnApp']; importTicketLines(ticketFk) {
let data = {claimFk: this.$stateParams.id, ticketFk: ticketFk};
let query = `/claim/api/ClaimEnds/importTicketSales`;
this.$http.post(query, data).then(() => {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.$.lastTicketsPopover.hide();
this.$.model.refresh();
});
}
}
Controller.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp'];
ngModule.component('vnClaimAction', { ngModule.component('vnClaimAction', {
template: require('./index.html'), template: require('./index.html'),

View File

@ -1,4 +1,5 @@
import './index.js'; import './index.js';
import {crudModel} from '../../../helpers/crudModelHelper';
describe('claim', () => { describe('claim', () => {
describe('Component vnClaimAction', () => { describe('Component vnClaimAction', () => {
@ -25,6 +26,11 @@ describe('claim', () => {
hide: () => {}, hide: () => {},
show: () => {} show: () => {}
}; };
controller.$.lastTicketsModel = crudModel;
controller.$.lastTicketsPopover = {
hide: () => {},
show: () => {}
};
})); }));
describe('openAddSalesDialog()', () => { describe('openAddSalesDialog()', () => {
@ -116,5 +122,32 @@ describe('claim', () => {
expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
}); });
}); });
describe('showLastTickets()', () => {
it('should get a list of tickets and call lastTicketsPopover show() method', () => {
spyOn(controller.$.lastTicketsModel, 'refresh');
spyOn(controller.$.lastTicketsPopover, 'show');
controller.showLastTickets({});
expect(controller.$.lastTicketsModel.refresh).toHaveBeenCalledWith();
expect(controller.$.lastTicketsPopover.show).toHaveBeenCalledWith();
});
});
describe('importTicketLines()', () => {
it('should perform a post quer', () => {
spyOn(controller.$.model, 'refresh');
spyOn(controller.vnApp, 'showSuccess');
spyOn(controller.$.lastTicketsPopover, 'hide');
let data = {claimFk: 1, ticketFk: 1};
$httpBackend.expect('POST', `/claim/api/ClaimEnds/importTicketSales`, data).respond({});
controller.importTicketLines(1);
$httpBackend.flush();
expect(controller.$.model.refresh).toHaveBeenCalledWith();
expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
expect(controller.$.lastTicketsPopover.hide).toHaveBeenCalledWith();
});
});
}); });
}); });

View File

@ -3,3 +3,5 @@ Action: Actuaciones
Total claimed: Total Reclamado Total claimed: Total Reclamado
Import claim: Importar reclamacion Import claim: Importar reclamacion
Imports claim details: Importa detalles de la reclamacion Imports claim details: Importa detalles de la reclamacion
Import ticket: Importar ticket
Imports ticket lines: Importa las lineas de un ticket

View File

@ -12,4 +12,16 @@ vn-claim-action {
} }
} }
} }
vn-popover.lastTicketsPopover {
vn-table {
min-width: 650px;
overflow: auto
}
div.ticketList {
overflow: auto;
max-height: 350px
}
}
} }

View File

@ -0,0 +1,42 @@
const UserError = require('vn-loopback/common/helpers').UserError;
module.exports = Self => {
Self.remoteMethodCtx('importTicketSales', {
description: 'Imports lines from claimBeginning to a new ticket with specific shipped, landed dates, agency and company',
accessType: 'WRITE',
accepts: [{
arg: 'params',
type: 'object',
http: {source: 'body'}
}],
returns: {
type: ['Object'],
root: true
},
http: {
path: `/importTicketSales`,
verb: 'POST'
}
});
Self.importTicketSales = async(ctx, params) => {
let models = Self.app.models;
let userId = ctx.req.accessToken.userId;
let worker = await models.Worker.findOne({where: {userFk: userId}});
let ticketSales = await models.Sale.find({
where: {ticketFk: params.ticketFk}
});
let claimEnds = [];
ticketSales.forEach(sale => {
claimEnds.push({
saleFk: sale.id,
claimFk: params.claimFk,
workerFk: worker.id
});
});
return await Self.create(claimEnds);
};
};

View File

@ -0,0 +1,23 @@
const app = require(`${servicesDir}/claim/server/server`);
describe('Claim importTicketSales()', () => {
let claimEnds;
afterAll(async() => {
claimEnds.forEach(async line => {
await line.destroy();
});
});
it('should import sales to a claim actions from an specific ticket', async() => {
let ctx = {req: {accessToken: {userId: 5}}};
claimEnds = await app.models.ClaimEnd.importTicketSales(ctx, {
claimFk: 1,
ticketFk: 1
});
expect(claimEnds.length).toEqual(4);
expect(claimEnds[0].saleFk).toEqual(1);
expect(claimEnds[2].saleFk).toEqual(3);
});
});

View File

@ -1,5 +1,6 @@
module.exports = Self => { module.exports = Self => {
require('../methods/claim-beginning/importToNewRefundTicket')(Self); require('../methods/claim-beginning/importToNewRefundTicket')(Self);
Self.validatesUniquenessOf('saleFk', { Self.validatesUniquenessOf('saleFk', {
message: `A claim with that sale already exists` message: `A claim with that sale already exists`
}); });

View File

@ -0,0 +1,3 @@
module.exports = Self => {
require('../methods/claim-end/importTicketSales')(Self);
};