salix/client/ticket/src/sale/index.js

341 lines
9.7 KiB
JavaScript
Raw Normal View History

2018-03-22 17:02:48 +00:00
import ngModule from '../module';
import FilterTicketList from '../filter-ticket-list';
import './style.scss';
2018-03-22 17:02:48 +00:00
class Controller extends FilterTicketList {
constructor($scope, $timeout, $stateParams, $http, vnApp, $translate) {
super($scope, $timeout, $stateParams);
this.$ = $scope;
this.vnApp = vnApp;
this.translate = $translate;
this.$timeout = $timeout;
this.onOrder('itemFk', 'ASC');
this.$state = $stateParams;
this.$http = $http;
this.deletable = false;
this.edit = {};
this.moreOptions = [
2018-07-02 11:18:22 +00:00
{callback: this.showAddTurnDialog, name: "Add turn", always: true},
{callback: this.showDeleteTicketDialog, name: "Delete ticket", always: true},
{callback: this.markAsReserved, name: 'Mark as reserved'},
{callback: this.unmarkAsReserved, name: 'Unmark as reserved'},
{callback: this.showEditDialog, name: 'Update discount'}
];
}
2018-07-02 11:18:22 +00:00
onMoreOpen() {
let options = this.moreOptions.filter(o => o.always || this.isChecked);
this.$.moreButton.data = options;
}
2018-06-19 10:10:38 +00:00
getTaxes() {
this.getSubTotal();
this.getVAT();
}
getSubTotal() {
let sales = this.$.index.model.instances;
this.subTotal = 0.00;
sales.forEach(sale => {
this.subTotal += (sale.quantity * sale.price) - ((sale.discount * (sale.quantity * sale.price)) / 100);
2018-06-19 10:10:38 +00:00
});
}
getVAT() {
this.$http.get(`/ticket/api/Tickets/${this.ticket.id}/getVAT`).then(res => {
if (res.data) {
this.VAT = res.data;
this.total = this.subTotal + this.VAT;
}
});
}
get isEditable() {
try {
return !this.ticket.tracking.state.alertLevel;
} catch (e) {}
return true;
}
get isChecked() {
let data = this.$.index.model.instances;
if (data)
for (let instance of data)
if (instance.checked)
return true;
return false;
}
getCheckedLines() {
let lines = [];
let data = this.$.index.model.instances;
if (data)
for (let i = 0; i < data.length; i++)
if (data[i].checked)
lines.push({id: data[i].id, instance: i});
return lines;
}
onMoreChange(callback) {
callback.call(this);
}
// Change State
onStateOkClick() {
let filter = {where: {code: "OK"}, fields: ["id"]};
let json = encodeURIComponent(JSON.stringify(filter));
this.$http.get(`/ticket/api/States?filter=${json}`).then(res => {
this.onStateChange(res.data[0].id);
});
}
onStateChange(value) {
let params = {ticketFk: this.$state.params.id, stateFk: value};
this.$http.post(`/ticket/api/TicketTrackings/changeState`, params).then(() => {
this.card.reload();
this.vnApp.showSuccess(this.translate.instant('Data saved!'));
});
}
// Add Turn
showAddTurnDialog() {
this.$.addTurn.show();
}
addTurn(day) {
let params = {ticketFk: this.$state.params.id, weekDay: day};
this.$http.patch(`/ticket/api/TicketWeeklies`, params).then(() => {
this.$.addTurn.hide();
});
}
2018-03-26 12:55:10 +00:00
// Delete Ticket
showDeleteTicketDialog() {
this.$.deleteConfirmation.show();
}
returnDeleteTicketDialog(response) {
if (response === 'ACCEPT')
this.deleteTicket();
}
deleteTicket() {
let params = {id: this.$state.params.id};
this.$http.post(`/ticket/api/Tickets/deleted`, params).then(() => {
this.$state.go('ticket.list');
});
}
// Remove Lines
onRemoveLinesClick(response) {
if (response === 'ACCEPT') {
let sales = this.getCheckedLines();
let params = {sales: sales, actualTicketFk: this.ticket.id};
let query = `/ticket/api/Sales/removes`;
this.$http.post(query, params).then(() => {
this.removeInstances(sales);
});
}
}
2018-07-02 11:18:22 +00:00
removeInstances(instances) {
for (let i = instances.length - 1; i >= 0; i--) {
this.$.index.model.instances.splice(instances[i].instance, 1);
}
}
showRemoveLinesDialog() {
this.$.deleteLines.show();
}
// Move Lines
showTransferPopover(event) {
let filter = {clientFk: this.ticket.clientFk, ticketFk: this.ticket.id};
let json = encodeURIComponent(JSON.stringify(filter));
2018-07-02 11:18:22 +00:00
let query = `/ticket/api/Tickets/threeLastActive?filter=${json}`;
this.$http.get(query).then(res => {
this.lastThreeTickets = res.data;
});
this.$.transfer.parent = event.target;
this.$.transfer.show();
}
moveLines(ticketID) {
let sales = this.getCheckedLines();
let params = {sales: sales, newTicketFk: ticketID, actualTicketFk: this.ticket.id};
this.$http.post(`/ticket/api/Sales/moveToTicket`, params).then(() => {
this.goToTicket(ticketID);
});
}
// In Progress
linesToNewTicket() {
let ticket = {
oldTicketFk: this.ticket.id,
clientFk: this.ticket.clientFk,
addressFk: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk,
warehouseFk: this.ticket.warehouseFk
};
let sales = this.getCheckedLines();
this.$http.post(`/api/Sales/MoveToNewTicket`, {ticket: ticket, sales: sales}).then(res => {
this.goToTicket(res.data.id);
});
}
goToTicket(ticketID) {
this.$state.go("ticket.card.sale", {id: ticketID});
}
// Slesperson Mana
getManaSalespersonMana() {
this.$http.get(`/api/Tickets/${this.$state.params.id}/getSalesPersonMana`).then(res => {
this.mana = res.data;
});
}
// Item Descriptor
showDescriptor(event, itemFk) {
this.$.descriptor.itemFk = itemFk;
this.$.descriptor.parent = event.target;
this.$.descriptor.show();
}
onDescriptorLoad() {
this.$.popover.relocate();
2018-03-26 12:55:10 +00:00
}
// Edit Line
showEditPricePopover(event, sale) {
this.sale = sale;
this.editedPrice = this.sale.price;
this.edit = {
ticketFk: this.ticket.id,
id: sale.id,
quantity: sale.quantity
};
this.$.editPricePopover.parent = event.target;
this.$.editPricePopover.show();
}
updatePrice() {
if (this.editedPrice != this.sale.price) {
this.$http.post(`/ticket/api/Sales/updatePrice`, {id: this.edit.id, price: this.editedPrice, ticketFk: this.ticket.id}).then(() => {
this.sale.price = this.edit.price;
this.$.index.accept();
});
}
this.$.editPricePopover.hide();
}
showEditPopover(event, sale) {
this.sale = sale;
this.edit = [{
ticketFk: this.ticket.id,
id: sale.id,
quantity: sale.quantity,
price: sale.price,
discount: sale.discount
}];
this.$.editPopover.parent = event.target;
this.$.editPopover.show();
}
async showEditDialog() {
this.edit = this.getCheckedLines();
this.$.editDialog.show();
}
hideEditDialog() {
this.$.index.accept();
this.$.editDialog.hide();
}
hideEditPopover() {
this.$.index.accept();
this.$.editPopover.hide();
}
updateQuantity(id, quantity) {
this.$http.post(`/ticket/api/Sales/${id}/updateQuantity`, {quantity: parseInt(quantity)}).then(() => {
this.vnApp.showSuccess(this.translate.instant('Data saved!'));
this.$.index.accept();
});
}
/* updateLine() {
if (this.edit.quantity != this.sale.quantity) {
this.$http.post(`/ticket/api/Sales/updateQuantity`, {id: this.edit.id, quantity: this.edit.quantity}).then(() => {
this.sale.quantity = this.edit.quantity;
});
}
if (this.edit.price != this.sale.price) {
this.$http.post(`/ticket/api/Sales/updatePrice`, {id: this.edit.id, price: this.edit.price}).then(() => {
this.sale.price = this.edit.price;
});
}
if (this.edit.discount != this.sale.discount) {
this.$http.post(`/ticket/api/Sales/updateDiscount`, {id: this.edit.id, discount: this.edit.discount}).then(() => {
this.sale.discount = this.edit.discount;
});
}
this.$.edit.hide();
}*/
2018-06-19 07:09:49 +00:00
/**
* Remove options from 'More' menu
* @param {String} name - Option name
*/
removeOptionByName(name) {
let options = this.moreOptions;
for (let i = 0; i < this.moreOptions.length; i++) {
if (options[i].name === name)
this.moreOptions.splice(i, 1);
}
}
/**
* Unmark sale as reserved
*/
unmarkAsReserved() {
this.setReserved(false);
}
/**
* Mark sale as reserved
2018-06-19 07:09:49 +00:00
*/
markAsReserved() {
this.setReserved(true);
}
2018-06-19 07:09:49 +00:00
setReserved(reserved) {
let sales = this.getCheckedLines();
let params = {sales: sales, ticketFk: this.ticket.id, reserved: reserved};
2018-06-19 07:09:49 +00:00
this.$http.post(`/ticket/api/Sales/reserve`, params).then(() => {
2018-06-19 07:09:49 +00:00
this.$.index.accept();
});
}
2018-03-26 12:55:10 +00:00
}
Controller.$inject = ['$scope', '$timeout', '$state', '$http', 'vnApp', '$translate'];
2018-03-26 12:55:10 +00:00
2018-03-22 17:02:48 +00:00
ngModule.component('vnTicketSale', {
template: require('./index.html'),
controller: Controller,
bindings: {
ticket: '<'
},
require: {
card: '^vnTicketCard'
}
2018-03-22 17:02:48 +00:00
});