import ngModule from '../module'; import Section from 'salix/components/section'; import './style.scss'; class Controller extends Section { constructor($element, $) { super($element, $); this.edit = {}; this.filter = { where: {claimFk: this.$params.id}, include: [ { relation: 'sale', scope: { fields: ['concept', 'ticketFk', 'price', 'quantity', 'discount', 'itemFk'], include: { relation: 'ticket' } } } ] }; } get claim() { return this._claim; } set claim(value) { this._claim = value; if (value) { this.isClaimEditable(); this.isTicketEditable(); } } set salesClaimed(value) { this._salesClaimed = value; if (value) this.calculateTotals(); } get salesClaimed() { return this._salesClaimed; } get newDiscount() { return this._newDiscount; } set newDiscount(value) { this._newDiscount = value; this.updateNewPrice(); } openAddSalesDialog() { this.getClaimableFromTicket(); this.$.addSales.show(); } getClaimableFromTicket() { let config = {params: {ticketFk: this.claim.ticketFk}}; let query = `Sales/getClaimableFromTicket`; this.$http.get(query, config).then(res => { if (res.data) this.salesToClaim = res.data; }); } addClaimedSale(index) { let sale = this.salesToClaim[index]; let saleToAdd = {saleFk: sale.saleFk, claimFk: this.claim.id, quantity: sale.quantity}; let query = `ClaimBeginnings/`; this.$http.post(query, saleToAdd).then(() => { this.$.addSales.hide(); this.$.model.refresh(); this.vnApp.showSuccess(this.$translate.instant('Data saved!')); if (this.aclService.hasAny(['salesAssistant'])) this.$state.go('claim.card.development'); }); } showDeleteConfirm(index) { this.sale = this.salesClaimed[index]; this.$.confirm.show(); } deleteClaimedSale() { let query = `ClaimBeginnings/${this.sale.id}`; this.$http.delete(query).then(() => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.$.model.remove(this.sale); this.calculateTotals(); }); } setClaimedQuantity(id, claimedQuantity) { let params = {id: id, quantity: claimedQuantity}; let query = `ClaimBeginnings/`; this.$http.patch(query, params).then(() => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.calculateTotals(); }); } calculateTotals() { this.paidTotal = 0.0; this.claimedTotal = 0.0; if (!this._salesClaimed) return; this._salesClaimed.forEach(sale => { let orgSale = sale.sale; this.paidTotal += this.getSaleTotal(orgSale); const price = sale.quantity * orgSale.price; const discount = ((orgSale.discount * price) / 100); this.claimedTotal += price - discount; }); } getSaleTotal(sale) { let total = 0.0; const price = sale.quantity * sale.price; const discount = ((sale.discount * price) / 100); total += price - discount; return total; } showEditPopover(event, saleClaimed) { if (this.isEditable) { if (!this.aclService.hasAny(['salesAssistant'])) return this.vnApp.showError(this.$translate.instant('Insuficient permisos')); this.saleClaimed = saleClaimed; this.$.editPopover.parent = event.target; this.$.editPopover.show(); } } getSalespersonMana() { this.$http.get(`Tickets/${this.claim.ticketFk}/getSalesPersonMana`).then(res => { this.mana = res.data; }); } isTicketEditable() { if (!this.claim) return; this.$http.get(`Tickets/${this.claim.ticketFk}/isEditable`).then(res => { this.isEditable = res.data; }); } isClaimEditable() { if (!this.claim) return; this.$http.get(`Claims/${this.claim.id}/isEditable`).then(res => { this.isRewritable = res.data; }); } updateDiscount() { const claimedSale = this.saleClaimed.sale; if (this.newDiscount != claimedSale.discount) { const params = {salesIds: [claimedSale.id], newDiscount: this.newDiscount}; const query = `Tickets/${claimedSale.ticketFk}/updateDiscount`; this.$http.post(query, params).then(() => { claimedSale.discount = this.newDiscount; this.calculateTotals(); this.clearDiscount(); this.vnApp.showSuccess(this.$translate.instant('Data saved!')); }).catch(err => { this.vnApp.showError(err.message); }); } this.$.editPopover.hide(); } updateNewPrice() { this.newPrice = (this.saleClaimed.quantity * this.saleClaimed.sale.price) - ((this.newDiscount * (this.saleClaimed.quantity * this.saleClaimed.sale.price)) / 100); } clearDiscount() { this.newDiscount = null; } } Controller.$inject = ['$element', '$scope']; ngModule.component('vnClaimDetail', { template: require('./index.html'), controller: Controller, bindings: { claim: '<' } });