diff --git a/client/claim/src/action/index.html b/client/claim/src/action/index.html index 94a27d946..761786d0a 100644 --- a/client/claim/src/action/index.html +++ b/client/claim/src/action/index.html @@ -15,6 +15,16 @@ + + + + + + diff --git a/client/claim/src/action/index.js b/client/claim/src/action/index.js index 295b9a955..f4b751227 100644 --- a/client/claim/src/action/index.js +++ b/client/claim/src/action/index.js @@ -59,6 +59,14 @@ class Controller { }); } + importToNewRefundTicket() { + let query = `claim/api/ClaimBeginnings/${this.$state.params.id}/importToNewRefundTicket`; + this.$http.post(query).then(() => { + this.$.model.refresh(); + this.vnApp.showSuccess(this.$translate.instant('Data saved!')); + }); + } + focusLastInput() { let inputs = document.querySelectorAll("#claimDestinationFk"); inputs[inputs.length - 1].querySelector("input").focus(); diff --git a/client/claim/src/action/index.spec.js b/client/claim/src/action/index.spec.js index 5241f1a0f..dcc4905b3 100644 --- a/client/claim/src/action/index.spec.js +++ b/client/claim/src/action/index.spec.js @@ -1,7 +1,7 @@ import './index.js'; describe('claim', () => { - describe('Component vnClaimDetail', () => { + describe('Component vnClaimAction', () => { let $componentController; let controller; let $httpBackend; @@ -11,11 +11,10 @@ describe('claim', () => { angular.mock.module('claim'); }); - beforeEach(angular.mock.inject((_$componentController_, _$state_, _$httpBackend_, $rootScope) => { + beforeEach(angular.mock.inject((_$componentController_, _$state_, _$httpBackend_) => { $componentController = _$componentController_; $httpBackend = _$httpBackend_; $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); - $httpBackend.when('GET', 'claim/api/Claims/ClaimBeginnings').respond({}); $state = _$state_; $state.params.id = 1; @@ -104,5 +103,18 @@ describe('claim', () => { expect(controller.claimedTotal).toEqual(50); }); }); + + describe('importToNewRefundTicket()', () => { + it('should perform a post query', () => { + spyOn(controller.$.model, 'refresh'); + spyOn(controller.vnApp, 'showSuccess'); + $httpBackend.expect('POST', `claim/api/ClaimBeginnings/1/importToNewRefundTicket`).respond({}); + controller.importToNewRefundTicket(); + $httpBackend.flush(); + + expect(controller.$.model.refresh).toHaveBeenCalledWith(); + expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); + }); + }); }); }); diff --git a/client/claim/src/action/style.scss b/client/claim/src/action/style.scss index a3947e63c..32db1645f 100644 --- a/client/claim/src/action/style.scss +++ b/client/claim/src/action/style.scss @@ -12,5 +12,4 @@ vn-claim-action { } } } -} - +} \ No newline at end of file diff --git a/services/claim/common/methods/claim-beginning/importToNewRefundTicket.js b/services/claim/common/methods/claim-beginning/importToNewRefundTicket.js new file mode 100644 index 000000000..dcdd0e914 --- /dev/null +++ b/services/claim/common/methods/claim-beginning/importToNewRefundTicket.js @@ -0,0 +1,129 @@ +module.exports = Self => { + Self.remoteMethodCtx('importToNewRefundTicket', { + description: 'Imports lines from claimBeginning to a new ticket with specific shipped, landed dates, agency and company', + accessType: 'WRITE', + accepts: [{ + arg: 'id', + type: 'number', + required: true, + description: 'the claim id', + http: {source: 'path'} + }], + returns: { + type: 'string', + root: true + }, + http: { + path: `/:id/importToNewRefundTicket`, + verb: 'POST' + } + }); + + async function addSalesToTicket(salesToRefund, newRefundTicket, transaction) { + let promises = []; + salesToRefund.forEach(sale => { + let formatedSale = { + itemFk: sale.sale().itemFk, + ticketFk: newRefundTicket, + concept: sale.sale().concept, + quantity: -Math.abs(sale.quantity), + price: sale.sale().price, + discount: sale.sale().discount, + reserved: sale.sale().reserved, + isPicked: sale.sale().isPicked, + created: sale.sale().created + }; + promises.push(Self.app.models.Sale.create(formatedSale, transaction)); + }); + return await Promise.all(promises); + } + + async function insertIntoClaimEnd(createdSales, claimId, workerId, transaction) { + let promises = []; + createdSales.forEach(sale => { + let formatedSale = { + saleFk: sale.id, + claimFk: claimId, + workerFk: workerId + }; + promises.push(Self.app.models.ClaimEnd.create(formatedSale, transaction)); + }); + return await Promise.all(promises); + } + + async function saveObservation(observation, transaction) { + let query = `INSERT INTO vn.ticketObservation(ticketFk, observationTypeFk, description) VALUES(?, ?, ?) + ON DUPLICATE KEY UPDATE description = CONCAT(vn.ticketObservation.description, VALUES(description),' ')`; + await Self.rawSql(query, [ + observation.ticketFk, + observation.observationTypeFk, + observation.description + ], transaction); + } + + Self.importToNewRefundTicket = async(ctx, id) => { + let models = Self.app.models; + let token = ctx.req.accessToken; + let userId = token.userId; + let worker = await models.Worker.findOne({where: {userFk: userId}}); + let obsevationType = await models.ObservationType.findOne({where: {description: 'comercial'}}); + let agency = await models.AgencyMode.findOne({where: {code: 'refund'}}); + let state = await models.State.findOne({where: {code: 'DELIVERED'}}); + + let filter = { + where: {id: id}, + include: [ + { + relation: 'ticket', + scope: { + fields: ['id', 'clientFk', 'warehouseFk', 'companyFk', 'addressFk'] + } + } + ] + }; + + let claim = await models.Claim.findOne(filter); + + let today = new Date(); + + let params = { + clientFk: claim.ticket().clientFk, + shipped: today, + landed: today, + warehouseFk: claim.ticket().warehouseFk, + companyFk: claim.ticket().companyFk, + addressFk: claim.ticket().addressFk, + agencyModeFk: agency.id, + userId: userId + }; + + let salesFilter = { + where: {claimFk: id}, + include: [ + { + relation: 'sale', + scope: { + fields: ['id', 'itemFk', 'concept', 'price', 'discount', 'reserved', 'isPicked', 'created', 'priceFixed', 'isPriceFixed'] + } + } + ] + }; + + let transaction = await Self.beginTransaction({}); + + try { + let newRefundTicket = await models.Ticket.new(params, {transaction: transaction}); + let observation = {description: `Reclama ticket: ${claim.ticketFk}`, ticketFk: newRefundTicket, observationTypeFk: obsevationType.id}; + await saveObservation(observation, {transaction: transaction}); + await models.TicketTracking.create({ticketFk: newRefundTicket, stateFk: state.id, workerFk: worker.id}, {transaction: transaction}); + let salesToRefund = await models.ClaimBeginning.find(salesFilter); + let createdSales = await addSalesToTicket(salesToRefund, newRefundTicket, {transaction: transaction}); + insertIntoClaimEnd(createdSales, id, worker.id, {transaction: transaction}); + await Self.rawSql('CALL vn.ticketCalculateClon(?)', [newRefundTicket], {transaction: transaction}); + await transaction.commit(); + } catch (e) { + await transaction.rollback(); + throw e; + } + }; +}; diff --git a/services/claim/common/models/claim-beginning.js b/services/claim/common/models/claim-beginning.js index bce69d531..4cf0e7b70 100644 --- a/services/claim/common/models/claim-beginning.js +++ b/services/claim/common/models/claim-beginning.js @@ -1,6 +1,5 @@ module.exports = Self => { - // Validations - + require('../methods/claim-beginning/importToNewRefundTicket')(Self); Self.validatesUniquenessOf('saleFk', { message: `A claim with that sale already exists` }); diff --git a/services/claim/common/models/claim.json b/services/claim/common/models/claim.json index ab6a4ab05..049dbe3ca 100644 --- a/services/claim/common/models/claim.json +++ b/services/claim/common/models/claim.json @@ -31,6 +31,15 @@ }, "ticketFk": { "type": "Number" + }, + "claimStateFk": { + "type": "Number" + }, + "clientFk": { + "type": "Number" + }, + "workerFk": { + "type": "Number" } }, "relations": { @@ -48,6 +57,11 @@ "type": "belongsTo", "model": "Worker", "foreignKey": "workerFk" + }, + "ticket": { + "type": "belongsTo", + "model": "Ticket", + "foreignKey": "ticketFk" } } } diff --git a/services/loopback/common/methods/ticket/new.js b/services/loopback/common/methods/ticket/new.js index 932f12b56..7c9ee611c 100644 --- a/services/loopback/common/methods/ticket/new.js +++ b/services/loopback/common/methods/ticket/new.js @@ -21,7 +21,7 @@ module.exports = Self => { } }); - Self.new = async params => { + Self.new = async(params, transaction) => { let existsAddress = await Self.app.models.Address.findOne({where: {id: params.addressFk, clientFk: params.clientFk}}); if (!existsAddress) throw new UserError(`This address doesn't exist`); @@ -38,7 +38,7 @@ module.exports = Self => { params.routeFk | null, params.landed, params.userId - ]); + ], transaction); return result[1][0].newTicketId; }; diff --git a/services/ticket/common/models/obvservation-type.json b/services/loopback/common/models/obvservation-type.json similarity index 100% rename from services/ticket/common/models/obvservation-type.json rename to services/loopback/common/models/obvservation-type.json diff --git a/services/ticket/common/models/ticket-observation.js b/services/loopback/common/models/ticket-observation.js similarity index 76% rename from services/ticket/common/models/ticket-observation.js rename to services/loopback/common/models/ticket-observation.js index 3d15e9b0e..34e7e35d4 100644 --- a/services/ticket/common/models/ticket-observation.js +++ b/services/loopback/common/models/ticket-observation.js @@ -1,4 +1,4 @@ -const UserError = require('vn-loopback/common/helpers').UserError; +const UserError = require(`../helpers`).UserError; module.exports = Self => { Self.rewriteDbError(function(err) { diff --git a/services/ticket/common/models/ticket-observation.json b/services/loopback/common/models/ticket-observation.json similarity index 100% rename from services/ticket/common/models/ticket-observation.json rename to services/loopback/common/models/ticket-observation.json diff --git a/services/ticket/common/models/ticket-tracking.js b/services/loopback/common/models/ticket-tracking.js similarity index 58% rename from services/ticket/common/models/ticket-tracking.js rename to services/loopback/common/models/ticket-tracking.js index 2ebef8a9a..0ecd23fba 100644 --- a/services/ticket/common/models/ticket-tracking.js +++ b/services/loopback/common/models/ticket-tracking.js @@ -1,5 +1,5 @@ module.exports = function(Self) { - require('../methods/ticket-tracking/changeState')(Self); + require('../../../ticket/common/methods/ticket-tracking/changeState')(Self); Self.validatesPresenceOf('stateFk', {message: 'State cannot be blank'}); }; diff --git a/services/ticket/common/models/ticket-tracking.json b/services/loopback/common/models/ticket-tracking.json similarity index 100% rename from services/ticket/common/models/ticket-tracking.json rename to services/loopback/common/models/ticket-tracking.json diff --git a/services/loopback/common/models/vn-model.js b/services/loopback/common/models/vn-model.js index deaf64387..2b42d0668 100644 --- a/services/loopback/common/models/vn-model.js +++ b/services/loopback/common/models/vn-model.js @@ -173,10 +173,10 @@ module.exports = function(Self) { }; }; - Self.rawSql = function(query, params, cb) { + Self.rawSql = function(query, params, options = {}, cb) { var connector = this.dataSource.connector; return new Promise(function(resolve, reject) { - connector.execute(query, params, function(error, response) { + connector.execute(query, params, options, function(error, response) { if (cb) cb(error, response); if (error) diff --git a/services/loopback/server/model-config.json b/services/loopback/server/model-config.json index d80927479..684e4aa4e 100644 --- a/services/loopback/server/model-config.json +++ b/services/loopback/server/model-config.json @@ -15,6 +15,15 @@ "ACL": { "dataSource": "salix" }, + "ObservationType": { + "dataSource": "vn" + }, + "TicketObservation": { + "dataSource": "vn" + }, + "TicketTracking": { + "dataSource": "vn" + }, "RoleMapping": { "dataSource": "salix" }, diff --git a/services/ticket/server/model-config.json b/services/ticket/server/model-config.json index dfdee74c1..8f4fa23b5 100644 --- a/services/ticket/server/model-config.json +++ b/services/ticket/server/model-config.json @@ -1,13 +1,4 @@ { - "TicketObservation": { - "dataSource": "vn" - }, - "ObservationType": { - "dataSource": "vn" - }, - "TicketTracking": { - "dataSource": "vn" - }, "TicketPackaging": { "dataSource": "vn" },