import ngModule from '../module'; import './style.scss'; class Controller { constructor($stateParams, $scope, $http, $translate, vnApp) { this.$stateParams = $stateParams; this.$ = $scope; this.$http = $http; this.$translate = $translate; this.vnApp = vnApp; this.filter = { where: {claimFk: $stateParams.id}, include: [ {relation: 'sale', scope: { fields: ['concept', 'ticketFk', 'price', 'quantity', 'discount', 'itemFk'], include: { relation: 'ticket' } } }, {relation: 'claimBeggining'}, {relation: 'claimDestination'} ] }; this.resolvedState = 3; this.maxResponsibility = 5; } openAddSalesDialog() { this.getClaimedSales(); this.$.addSales.show(); } getClaimedSales() { let json = encodeURIComponent(JSON.stringify(this.claim.id)); let query = `ClaimBeginnings/${json}`; this.$http.get(query).then(res => { if (res.data) this.claimedSales = res.data; }); } addClaimedSale(saleFk) { let saleToAdd = {saleFk: saleFk, claimFk: this.claim.id, workerFk: this.claim.workerFk, claimDestinationFk: 1}; let query = `ClaimEnds/`; this.$http.post(query, saleToAdd).then(() => { this.$.model.refresh(); this.$.addSales.hide(); this.vnApp.showSuccess(this.$translate.instant('Data saved!')); }); } deleteClaimedSale(id) { let json = encodeURIComponent(JSON.stringify(id)); let query = `ClaimEnds/${json}`; this.$http.delete(query).then(() => { this.$.model.refresh(); this.vnApp.showSuccess(this.$translate.instant('Data saved!')); }); } importToNewRefundTicket() { let query = `ClaimBeginnings/${this.$stateParams.id}/importToNewRefundTicket`; return this.$http.post(query).then(() => { this.$.model.refresh(); this.vnApp.showSuccess(this.$translate.instant('Data saved!')); }); } showTicketDescriptor(event, ticketFk) { this.$.ticketDescriptor.ticketFk = ticketFk; this.$.ticketDescriptor.parent = event.target; this.$.ticketDescriptor.show(); event.preventDefault(); } focusLastInput() { let inputs = document.querySelectorAll('#claimDestinationFk'); inputs[inputs.length - 1].querySelector('input').focus(); this.calculateTotals(); } calculateTotals() { this.claimedTotal = 0; this.salesClaimed.forEach(sale => { 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}, clientFk: this.claim.clientFk } }; this.$.lastTicketsModel.filter = filter; this.$.lastTicketsModel.refresh(); this.$.lastTicketsPopover.parent = event.target; this.$.lastTicketsPopover.show(); } importTicketLines(ticketFk) { let data = {claimFk: this.$stateParams.id, ticketFk: ticketFk}; let query = `ClaimEnds/importTicketSales`; this.$http.post(query, data).then(() => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.$.lastTicketsPopover.hide(); this.$.model.refresh(); }); } regularize() { let data = {claimFk: this.$stateParams.id}; let query = `Claims/regularizeClaim`; return this.$http.post(query, data).then(() => { this.card.reload(); this.vnApp.showSuccess(this.$translate.instant('Data saved!')); if (this.claim.responsibility >= Math.ceil(this.maxResponsibility) / 2) this.$.updateGreuge.show(); }); } onUpdateGreugeResponse(response) { if (response !== 'ACCEPT') return; let greugeTypeFreight = 7; let query = `Greuges/`; let data = { clientFk: this.claim.clientFk, description: `claim: ${this.claim.id}`, amount: 11, greugeTypeFk: greugeTypeFreight, ticketFk: this.claim.ticketFk }; this.$http.post(query, data).then(() => { this.card.reload(); this.vnApp.showSuccess(this.$translate.instant('Greuge inserted!')); }); } // Item Descriptor showDescriptor(event, itemFk) { this.$.descriptor.itemFk = itemFk; this.$.descriptor.parent = event.target; this.$.descriptor.show(); } saveResponsibility(value) { let query = `Claims/${this.$stateParams.id}/updateClaimAction`; this.$http.post(query, {responsibility: value}).then(() => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); }); } saveMana(value) { let query = `Claims/${this.$stateParams.id}/updateClaimAction`; this.$http.post(query, {isChargedToMana: value}).then(() => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); }); } } Controller.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp']; ngModule.component('vnClaimAction', { template: require('./index.html'), controller: Controller, bindings: { claim: '<' }, require: { card: '^vnClaimCard' } });