640 lines
17 KiB
JavaScript
640 lines
17 KiB
JavaScript
import ngModule from '../module';
|
|
import Section from 'salix/components/section';
|
|
import './style.scss';
|
|
|
|
class Controller extends Section {
|
|
constructor($element, $) {
|
|
super($element, $);
|
|
// this.edit = {};
|
|
/* this.moreOptions = [
|
|
{
|
|
name: 'Send shortage SMS',
|
|
callback: this.showSMSDialog
|
|
}, {
|
|
name: 'Mark as reserved',
|
|
callback: this.markAsReserved,
|
|
show: () => this.isEditable
|
|
}, {
|
|
name: 'Unmark as reserved',
|
|
callback: this.unmarkAsReserved,
|
|
show: () => this.isEditable
|
|
}, {
|
|
name: 'Update discount',
|
|
callback: this.showEditDialog,
|
|
show: () => this.isEditable
|
|
}, {
|
|
name: 'Add claim',
|
|
callback: this.createClaim
|
|
}, {
|
|
name: 'Recalculate price',
|
|
callback: this.calculateSalePrice,
|
|
show: () => this.hasOneSaleSelected()
|
|
},
|
|
]; */
|
|
this._sales = [];
|
|
}
|
|
|
|
get ticket() {
|
|
return this._ticket;
|
|
}
|
|
|
|
set ticket(value) {
|
|
this._ticket = value;
|
|
this.isTicketEditable();
|
|
this.isTicketLocked();
|
|
}
|
|
|
|
get sales() {
|
|
return this._sales;
|
|
}
|
|
|
|
set sales(value) {
|
|
this._sales = value;
|
|
this.refreshTotal();
|
|
}
|
|
|
|
/* get editedPrice() {
|
|
return this._editedPrice;
|
|
}
|
|
|
|
set editedPrice(value) {
|
|
this._editedPrice = value;
|
|
this.updateNewPrice();
|
|
} */
|
|
|
|
get ticketState() {
|
|
if (!this.ticket) return null;
|
|
|
|
return this.ticket.ticketState.state.code;
|
|
}
|
|
|
|
refreshTotal() {
|
|
this.getSubTotal();
|
|
this.getVat();
|
|
}
|
|
|
|
getSubTotal() {
|
|
if (!this.$params.id || !this.sales) return;
|
|
this.$http.get(`Tickets/${this.$params.id}/subtotal`).then(res => {
|
|
this.subtotal = res.data || 0.0;
|
|
});
|
|
}
|
|
|
|
getSaleTotal(sale) {
|
|
if (sale.quantity == null || sale.price == null)
|
|
return null;
|
|
|
|
const price = sale.quantity * sale.price;
|
|
const discount = (sale.discount * price) / 100;
|
|
|
|
return price - discount;
|
|
}
|
|
|
|
getVat() {
|
|
this.VAT = 0.0;
|
|
if (!this.$params.id || !this.sales) return;
|
|
this.$http.get(`Tickets/${this.$params.id}/getVAT`).then(res => {
|
|
this.VAT = res.data || 0.0;
|
|
});
|
|
}
|
|
|
|
get total() {
|
|
return this.subtotal + this.VAT;
|
|
}
|
|
|
|
/* onMoreOpen() {
|
|
let options = this.moreOptions.filter(option => {
|
|
const hasShowProperty = Object.hasOwnProperty.call(option, 'show');
|
|
const shouldShow = !hasShowProperty || option.show === true ||
|
|
typeof option.show === 'function' && option.show();
|
|
|
|
return (shouldShow && (option.always || this.isChecked));
|
|
});
|
|
this.$.moreButton.data = options;
|
|
}
|
|
|
|
onMoreChange(callback) {
|
|
callback.call(this);
|
|
} */
|
|
|
|
/* get isChecked() {
|
|
if (this.sales) {
|
|
for (let instance of this.sales)
|
|
if (instance.checked) return true;
|
|
}
|
|
|
|
return false;
|
|
} */
|
|
|
|
/**
|
|
* Returns checked instances
|
|
*
|
|
* @return {Array} Checked instances
|
|
*/
|
|
checkedLines() {
|
|
if (!this.sales) return;
|
|
|
|
return this.sales.filter(sale => {
|
|
return sale.checked;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns the total of checked instances
|
|
*
|
|
* @return {Number} Total checked instances
|
|
*/
|
|
checkedLinesCount() {
|
|
const checkedLines = this.checkedLines();
|
|
if (checkedLines)
|
|
return checkedLines.length;
|
|
|
|
return 0;
|
|
}
|
|
|
|
hasCheckedLines() {
|
|
const checkedLines = this.checkedLines();
|
|
|
|
if (checkedLines)
|
|
return checkedLines.length > 0;
|
|
|
|
return false;
|
|
}
|
|
|
|
hasOneSaleSelected() {
|
|
if (this.checkedLinesCount() === 1)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Returns new instances
|
|
*
|
|
* @return {Array} New instances
|
|
*/
|
|
newInstances() {
|
|
if (!this.sales) return;
|
|
|
|
return this.sales.filter(sale => {
|
|
return !sale.id;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns an array of indexes
|
|
* from checked instances
|
|
*
|
|
* @return {Array} Indexes of checked instances
|
|
*/
|
|
checkedLinesIndex() {
|
|
if (!this.sales) return;
|
|
|
|
let indexes = [];
|
|
this.sales.forEach((sale, index) => {
|
|
if (sale.checked) indexes.push(index);
|
|
});
|
|
|
|
return indexes;
|
|
}
|
|
|
|
/* firstCheckedLine() {
|
|
const checkedLines = this.checkedLines();
|
|
if (checkedLines)
|
|
return checkedLines[0];
|
|
} */
|
|
|
|
removeCheckedLines() {
|
|
const sales = this.checkedLines();
|
|
|
|
sales.forEach(sale => {
|
|
const index = this.sales.indexOf(sale);
|
|
this.sales.splice(index, 1);
|
|
});
|
|
|
|
if (this.newInstances().length === 0)
|
|
this.$.watcher.updateOriginalData();
|
|
|
|
this.refreshTotal();
|
|
}
|
|
|
|
/* onStateOkClick() {
|
|
let filter = {where: {code: 'OK'}, fields: ['id']};
|
|
let json = encodeURIComponent(JSON.stringify(filter));
|
|
return this.$http.get(`States?filter=${json}`).then(res => {
|
|
this.changeState(res.data[0].id);
|
|
});
|
|
}
|
|
*/
|
|
changeState(value) {
|
|
let params = {ticketFk: this.$params.id, code: value};
|
|
this.$http.post('TicketTrackings/changeState', params).then(() => {
|
|
this.vnApp.showSuccess(this.$t('Data saved!'));
|
|
this.card.reload();
|
|
}).finally(() => {
|
|
if (this.newInstances().length === 0)
|
|
this.$.watcher.updateOriginalData();
|
|
});
|
|
}
|
|
|
|
onRemoveLinesClick(response) {
|
|
if (response === 'accept') {
|
|
let sales = this.checkedLines();
|
|
|
|
// Remove unsaved instances
|
|
sales.forEach((sale, index) => {
|
|
if (!sale.id) sales.splice(index);
|
|
});
|
|
|
|
let params = {sales: sales, actualTicketFk: this.ticket.id};
|
|
let query = `Sales/removes`;
|
|
this.$http.post(query, params).then(() => {
|
|
this.removeCheckedLines();
|
|
this.vnApp.showSuccess(this.$t('Data saved!'));
|
|
});
|
|
}
|
|
}
|
|
|
|
showTransferPopover(event) {
|
|
this.setTransferParams();
|
|
this.$.transfer.parent = event.target;
|
|
this.$.transfer.show();
|
|
}
|
|
|
|
setTransferParams() {
|
|
const checkedSales = JSON.stringify(this.checkedLines());
|
|
const sales = JSON.parse(checkedSales);
|
|
this.transfer = {
|
|
lastActiveTickets: [],
|
|
sales: sales
|
|
};
|
|
|
|
const params = {ticketId: this.ticket.id};
|
|
const query = `clients/${this.ticket.clientFk}/lastActiveTickets`;
|
|
this.$http.get(query, {params}).then(res => {
|
|
this.transfer.lastActiveTickets = res.data;
|
|
});
|
|
}
|
|
|
|
transferSales(ticketId) {
|
|
const params = {
|
|
ticketId: ticketId,
|
|
sales: this.transfer.sales
|
|
};
|
|
|
|
this.$.watcher.updateOriginalData();
|
|
|
|
const query = `tickets/${this.ticket.id}/transferSales`;
|
|
this.$http.post(query, params)
|
|
.then(res => this.$state.go('ticket.card.sale', {id: res.data.id}));
|
|
}
|
|
|
|
createClaim() {
|
|
const sales = this.checkedLines();
|
|
const params = {
|
|
claim: {
|
|
ticketFk: this.ticket.id,
|
|
clientFk: this.ticket.clientFk,
|
|
ticketCreated: this.ticket.shipped
|
|
},
|
|
sales: sales
|
|
};
|
|
|
|
if (this.newInstances().length === 0)
|
|
this.$.watcher.updateOriginalData();
|
|
|
|
this.$http.post(`Claims/createFromSales`, params)
|
|
.then(res => this.$state.go('claim.card.basicData', {id: res.data.id}));
|
|
}
|
|
|
|
getMana() {
|
|
this.$http.get(`Tickets/${this.$params.id}/getSalesPersonMana`)
|
|
.then(res => this.edit.mana = res.data);
|
|
}
|
|
|
|
showEditPricePopover(event, sale) {
|
|
if (!this.isEditable) return;
|
|
|
|
this.edit = {
|
|
price: sale.price,
|
|
sale: sale
|
|
};
|
|
this.$.editPricePopover.show(event);
|
|
}
|
|
|
|
updatePrice() {
|
|
const sale = this.edit.sale;
|
|
const newPrice = this.edit.price;
|
|
if (newPrice != null && newPrice != sale.price) {
|
|
const query = `Sales/${sale.id}/updatePrice`;
|
|
this.$http.post(query, {newPrice}).then(res => {
|
|
sale.price = res.data.price;
|
|
|
|
this.vnApp.showSuccess(this.$t('Data saved!'));
|
|
}).finally(() => {
|
|
if (this.newInstances().length === 0)
|
|
this.$.watcher.updateOriginalData();
|
|
});
|
|
}
|
|
|
|
this.$.editPricePopover.hide();
|
|
}
|
|
|
|
/* updateNewPrice() {
|
|
this.newPrice = this.sale.quantity * this.newPrice - ((this.sale.discount * (this.sale.quantity * this.newPrice)) / 100);
|
|
}
|
|
*/
|
|
showEditDiscountPopover(event, sale) {
|
|
if (this.isLocked) return;
|
|
|
|
this.edit = {
|
|
discount: sale.discount,
|
|
sale: sale
|
|
};
|
|
|
|
this.$.editDiscount.show(event);
|
|
}
|
|
|
|
showEditDiscountDialog(event) {
|
|
if (this.isLocked) return;
|
|
|
|
this.edit = {
|
|
discount: null,
|
|
sales: this.checkedLines()
|
|
};
|
|
|
|
this.$.editDiscountDialog.show(event);
|
|
}
|
|
|
|
changeDiscount() {
|
|
const sale = this.edit.sale;
|
|
const newDiscount = this.edit.discount;
|
|
if (newDiscount != null && newDiscount != sale.discount)
|
|
this.updateDiscount([sale]);
|
|
|
|
this.$.editDiscount.hide();
|
|
}
|
|
|
|
changeMultipleDiscount() {
|
|
const sales = this.edit.sales;
|
|
const newDiscount = this.edit.discount;
|
|
const hasChanges = sales.some(sale => {
|
|
return sale.discount != newDiscount;
|
|
});
|
|
|
|
if (newDiscount != null && hasChanges)
|
|
this.updateDiscount(sales);
|
|
|
|
this.$.editDiscountDialog.hide();
|
|
}
|
|
|
|
updateDiscount(sales) {
|
|
const saleIds = sales.map(sale => {
|
|
return sale.id;
|
|
});
|
|
|
|
const params = {salesIds: saleIds, newDiscount: this.edit.discount};
|
|
const query = `Tickets/${this.$params.id}/updateDiscount`;
|
|
this.$http.post(query, params).then(() => {
|
|
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
|
|
|
|
for (let sale of sales)
|
|
sale.discount = this.edit.discount;
|
|
}).catch(e => {
|
|
this.vnApp.showError(e.message);
|
|
});
|
|
}
|
|
|
|
getNewPrice() {
|
|
if (this.edit) {
|
|
const sale = this.edit.sale;
|
|
let newDiscount = sale.discount;
|
|
let newPrice = this.edit.price || sale.price;
|
|
|
|
if (this.edit.discount != null)
|
|
newDiscount = this.edit.discount;
|
|
|
|
if (this.edit.price != null)
|
|
newPrice = this.edit.price;
|
|
|
|
const price = sale.quantity * newPrice;
|
|
const discount = (newDiscount * price) / 100;
|
|
|
|
return price - discount;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* showEditDialog() {
|
|
this.edit = this.checkedLines();
|
|
this.$.editDialog.show();
|
|
}
|
|
|
|
hideEditDialog() {
|
|
this.$.model.refresh();
|
|
this.$.editDialog.hide();
|
|
}
|
|
|
|
hideEditPopover() {
|
|
this.$.model.refresh();
|
|
this.$.editPopover.hide();
|
|
} */
|
|
|
|
hasReserves() {
|
|
return this.sales.some(sale => {
|
|
return sale.reserved == true;
|
|
});
|
|
}
|
|
|
|
/*
|
|
* Unmark sale as reserved
|
|
*/
|
|
unmarkAsReserved() {
|
|
this.setReserved(false);
|
|
}
|
|
|
|
/*
|
|
* Mark sale as reserved
|
|
*/
|
|
markAsReserved() {
|
|
this.setReserved(true);
|
|
}
|
|
|
|
setReserved(reserved) {
|
|
let selectedSales = this.checkedLines();
|
|
let params = {sales: selectedSales, ticketFk: this.ticket.id, reserved: reserved};
|
|
|
|
let reservedSales = new Map();
|
|
this.$http.post(`Sales/reserve`, params).then(res => {
|
|
let isReserved = res.config.data.reserved;
|
|
|
|
res.config.data.sales.forEach(sale => {
|
|
reservedSales.set(sale.id, {reserved: isReserved});
|
|
});
|
|
|
|
this.sales.forEach(sale => {
|
|
const reservedSale = reservedSales.get(sale.id);
|
|
if (reservedSale)
|
|
sale.reserved = reservedSale.reserved;
|
|
});
|
|
}).finally(() => {
|
|
if (this.newInstances().length === 0)
|
|
this.$.watcher.updateOriginalData();
|
|
});
|
|
}
|
|
|
|
newOrderFromTicket() {
|
|
this.$http.post(`Orders/newFromTicket`, {ticketFk: this.ticket.id}).then(res => {
|
|
const path = this.$state.href('order.card.catalog', {id: res.data});
|
|
window.open(path, '_blank');
|
|
|
|
this.vnApp.showSuccess(this.$t('Order created'));
|
|
});
|
|
}
|
|
|
|
showSMSDialog() {
|
|
const address = this.ticket.address;
|
|
const client = this.ticket.client;
|
|
const phone = address.mobile || address.phone ||
|
|
client.mobile || client.phone;
|
|
const sales = this.checkedLines();
|
|
const items = sales.map(sale => {
|
|
return `${sale.quantity} ${sale.concept}`;
|
|
});
|
|
const notAvailables = items.join(', ');
|
|
const params = {
|
|
ticketFk: this.ticket.id,
|
|
created: this.ticket.created,
|
|
notAvailables
|
|
};
|
|
this.newSMS = {
|
|
destinationFk: this.ticket.clientFk,
|
|
destination: phone,
|
|
message: this.$t('Product not available', params)
|
|
};
|
|
this.$.sms.open();
|
|
}
|
|
|
|
/**
|
|
* Inserts a new instance
|
|
*/
|
|
add() {
|
|
this.$.model.insert({});
|
|
}
|
|
|
|
/*
|
|
* Creates a new sale if it's a new instance
|
|
* Updates the sale quantity for existing instance
|
|
*/
|
|
changeQuantity(sale) {
|
|
if (!sale.quantity) return;
|
|
|
|
if (!sale.id)
|
|
return this.addSale(sale);
|
|
|
|
this.updateQuantity(sale);
|
|
}
|
|
|
|
/*
|
|
* Changes a sale quantity
|
|
*/
|
|
updateQuantity(sale) {
|
|
const data = {quantity: sale.quantity};
|
|
const query = `Sales/${sale.id}/updateQuantity`;
|
|
this.$http.post(query, data).then(() => {
|
|
this.vnApp.showSuccess(this.$t('Data saved!'));
|
|
}).catch(e => {
|
|
this.$.model.refresh();
|
|
throw e;
|
|
}).finally(() => {
|
|
if (this.newInstances().length === 0)
|
|
this.$.watcher.updateOriginalData();
|
|
});
|
|
}
|
|
|
|
/*
|
|
* Changes a sale concept
|
|
*/
|
|
changeConcept(sale) {
|
|
const data = {newConcept: sale.concept};
|
|
const query = `Sales/${sale.id}/updateConcept`;
|
|
this.$http.post(query, data).then(() => {
|
|
this.vnApp.showSuccess(this.$t('Data saved!'));
|
|
}).catch(e => {
|
|
this.$.model.refresh();
|
|
throw e;
|
|
}).finally(() => {
|
|
if (this.newInstances().length === 0)
|
|
this.$.watcher.updateOriginalData();
|
|
});
|
|
}
|
|
|
|
/*
|
|
* Adds a new sale
|
|
*/
|
|
addSale(sale) {
|
|
const data = {
|
|
itemId: sale.itemFk,
|
|
quantity: sale.quantity
|
|
};
|
|
const query = `tickets/${this.ticket.id}/addSale`;
|
|
this.$http.post(query, data).then(res => {
|
|
if (!res.data) return;
|
|
|
|
const newSale = res.data;
|
|
|
|
sale.id = newSale.id;
|
|
sale.image = newSale.item.image;
|
|
sale.subName = newSale.item.subName;
|
|
sale.concept = newSale.concept;
|
|
sale.quantity = newSale.quantity;
|
|
sale.discount = newSale.discount;
|
|
sale.price = newSale.price;
|
|
sale.item = newSale.item;
|
|
|
|
this.vnApp.showSuccess(this.$t('Data saved!'));
|
|
}).finally(() => {
|
|
if (this.newInstances().length === 0)
|
|
this.$.watcher.updateOriginalData();
|
|
});
|
|
}
|
|
|
|
isTicketEditable() {
|
|
this.$http.get(`Tickets/${this.$state.params.id}/isEditable`).then(res => {
|
|
this.isEditable = res.data;
|
|
});
|
|
}
|
|
|
|
isTicketLocked() {
|
|
this.$http.get(`Tickets/${this.$state.params.id}/isLocked`).then(res => {
|
|
this.isLocked = res.data;
|
|
});
|
|
}
|
|
|
|
calculateSalePrice() {
|
|
const sale = this.checkedLines()[0];
|
|
const query = `Sales/${sale.id}/recalculatePrice`;
|
|
this.$http.post(query).then(res => {
|
|
this.vnApp.showSuccess(this.$t('Data saved!'));
|
|
this.$.model.refresh();
|
|
});
|
|
}
|
|
|
|
itemSearchFunc($search) {
|
|
return /^\d+$/.test($search)
|
|
? {id: $search}
|
|
: {name: {like: '%' + $search + '%'}};
|
|
}
|
|
}
|
|
|
|
ngModule.component('vnTicketSale', {
|
|
template: require('./index.html'),
|
|
controller: Controller,
|
|
bindings: {
|
|
ticket: '<'
|
|
},
|
|
require: {
|
|
card: '?^vnTicketCard'
|
|
}
|
|
});
|