salix/client/claim/src/action/index.js

94 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-09-05 09:34:23 +00:00
import ngModule from '../module';
import './style.scss';
class Controller {
constructor($state, $scope, $http, $translate, vnApp) {
this.$state = $state;
this.$ = $scope;
this.$http = $http;
this.$translate = $translate;
this.vnApp = vnApp;
this.filter = {
where: {claimFk: $state.params.id},
include: [
{relation: 'sale',
scope: {
fields: ['concept', 'ticketFk', 'price', 'quantity', 'discount'],
include: {
relation: 'ticket'
}
}
},
{relation: 'claimBeggining'}
]
};
}
openAddSalesDialog() {
this.getClaimedSales();
this.$.addSales.show();
}
getClaimedSales() {
let json = encodeURIComponent(JSON.stringify(this.claim.id));
let query = `/claim/api/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 = `claim/api/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 = `claim/api/ClaimEnds/${json}`;
this.$http.delete(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();
this.calculateTotals();
}
setClaimDestination(id, claimDestinationFk) {
let params = {id: id, claimDestinationFk: claimDestinationFk};
let query = `claim/api/ClaimEnds/`;
this.$http.patch(query, params).then(() => {
this.$.model.refresh();
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
});
}
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);
});
}
}
Controller.$inject = ['$state', '$scope', '$http', '$translate', 'vnApp'];
ngModule.component('vnClaimAction', {
template: require('./index.html'),
controller: Controller,
bindings: {
claim: '<'
}
});