salix/modules/claim/front/detail/index.js

174 lines
5.1 KiB
JavaScript
Raw Normal View History

2018-08-30 07:19:09 +00:00
import ngModule from '../module';
2020-03-16 10:58:14 +00:00
import Section from 'salix/components/section';
2018-08-30 07:19:09 +00:00
import './style.scss';
2020-03-16 10:58:14 +00:00
class Controller extends Section {
constructor($element, $) {
super($element, $);
this.edit = {};
2018-08-30 07:19:09 +00:00
this.filter = {
2020-03-16 10:58:14 +00:00
where: {claimFk: this.$params.id},
2018-08-30 07:19:09 +00:00
include: [
{
relation: 'sale',
2018-08-30 07:19:09 +00:00
scope: {
2018-11-21 12:29:46 +00:00
fields: ['concept', 'ticketFk', 'price', 'quantity', 'discount', 'itemFk'],
2018-08-30 07:19:09 +00:00
include: {
relation: 'ticket'
}
}
}
]
};
}
set salesClaimed(value) {
this._salesClaimed = value;
2020-01-07 12:03:45 +00:00
if (value) {
this.calculateTotals();
2020-01-07 12:03:45 +00:00
this.isClaimEditable();
}
}
get salesClaimed() {
return this._salesClaimed;
}
get newDiscount() {
return this._newDiscount;
}
set newDiscount(value) {
this._newDiscount = value;
this.updateNewPrice();
}
2018-08-30 07:19:09 +00:00
openAddSalesDialog() {
this.getClaimableFromTicket();
this.$.addSales.show();
2018-08-30 07:19:09 +00:00
}
getClaimableFromTicket() {
let config = {params: {ticketFk: this.claim.ticketFk}};
let query = `Sales/getClaimableFromTicket`;
this.$http.get(query, config).then(res => {
2018-11-21 12:29:46 +00:00
if (res.data)
2018-08-30 07:19:09 +00:00
this.salesToClaim = res.data;
});
}
2018-11-23 13:30:01 +00:00
addClaimedSale(index) {
let sale = this.salesToClaim[index];
let saleToAdd = {saleFk: sale.saleFk, claimFk: this.claim.id, quantity: sale.quantity};
let query = `ClaimBeginnings/`;
2018-08-30 07:19:09 +00:00
this.$http.post(query, saleToAdd).then(() => {
this.$.addSales.hide();
this.$.model.refresh();
2018-08-30 07:19:09 +00:00
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
if (this.aclService.hasAny(['salesAssistant']))
this.$state.go('claim.card.development');
2018-08-30 07:19:09 +00:00
});
}
2018-11-23 13:30:01 +00:00
deleteClaimedSale(index) {
let sale = this.salesClaimed[index];
let query = `ClaimBeginnings/${sale.id}`;
2018-08-30 07:19:09 +00:00
this.$http.delete(query).then(() => {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.$.model.remove(index);
2018-11-23 13:30:01 +00:00
this.calculateTotals();
2018-08-30 07:19:09 +00:00
});
}
setClaimedQuantity(id, claimedQuantity) {
let params = {id: id, quantity: claimedQuantity};
let query = `ClaimBeginnings/`;
2018-08-30 07:19:09 +00:00
this.$http.patch(query, params).then(() => {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
2018-11-23 13:30:01 +00:00
this.calculateTotals();
2018-08-30 07:19:09 +00:00
});
}
2018-09-05 09:45:33 +00:00
calculateTotals() {
this.paidTotal = 0.0;
this.claimedTotal = 0.0;
if (!this._salesClaimed) return;
2018-11-23 13:30:01 +00:00
this._salesClaimed.forEach(sale => {
let orgSale = sale.sale;
this.paidTotal += this.getSaleTotal(orgSale);
this.claimedTotal += sale.quantity * orgSale.price - ((orgSale.discount * (sale.quantity * orgSale.price)) / 100);
2018-09-05 09:45:33 +00:00
});
}
2018-11-21 12:29:46 +00:00
getSaleTotal(sale) {
let total = 0.0;
total += sale.quantity * sale.price - ((sale.discount * (sale.quantity * sale.price)) / 100);
return total;
}
showEditPopover(event, saleClaimed) {
2020-01-08 09:08:27 +00:00
if (this.isEditable) {
if (!this.aclService.hasAny(['salesAssistant']))
return this.vnApp.showError(this.$translate.instant('Insuficient permisos'));
2020-01-08 09:08:27 +00:00
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;
});
}
2020-01-07 12:03:45 +00:00
isClaimEditable() {
this.$http.get(`Tickets/${this.claim.ticketFk}/isEditable`).then(res => {
this.isEditable = 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;
2018-11-21 12:29:46 +00:00
}
2018-08-30 07:19:09 +00:00
}
2020-03-16 10:58:14 +00:00
Controller.$inject = ['$element', '$scope'];
2018-08-30 07:19:09 +00:00
ngModule.component('vnClaimDetail', {
template: require('./index.html'),
controller: Controller,
bindings: {
claim: '<'
}
});