salix/modules/ticket/front/sale/index.js

503 lines
14 KiB
JavaScript
Raw Normal View History

2018-03-22 17:02:48 +00:00
import ngModule from '../module';
import './style.scss';
2018-03-22 17:02:48 +00:00
class Controller {
2018-07-17 06:44:31 +00:00
constructor($scope, $state, $http, vnApp, $translate) {
this.$scope = $scope;
this.vnApp = vnApp;
this.$translate = $translate;
2018-07-17 06:44:31 +00:00
this.$state = $state;
this.$stateParams = $state.params;
this.$http = $http;
this.edit = {};
this.moreOptions = [
2018-07-02 11:18:22 +00:00
{callback: this.markAsReserved, name: 'Mark as reserved'},
{callback: this.unmarkAsReserved, name: 'Unmark as reserved'},
2019-05-06 05:40:59 +00:00
{callback: this.showEditDialog, name: 'Update discount', show: () => !this.hasInvoice()},
{callback: this.createClaim, name: 'Add claim'},
{callback: this.showSMSDialog, name: 'Send SMS'}
];
this._sales = [];
2018-10-22 06:23:10 +00:00
this.imagesPath = '//verdnatura.es/vn-image-data/catalog';
}
get sales() {
return this._sales;
}
2018-10-22 06:23:10 +00:00
set sales(value) {
this._sales = value;
2018-10-23 12:33:07 +00:00
this.refreshTotal();
2018-10-22 06:23:10 +00:00
}
get editedPrice() {
return this._editedPrice;
2018-07-02 11:18:22 +00:00
}
2018-07-17 06:44:31 +00:00
set editedPrice(value) {
this._editedPrice = value;
this.updateNewPrice();
}
2018-10-23 12:33:07 +00:00
refreshTotal() {
this.loadSubTotal();
this.loadVAT();
}
2018-10-22 06:23:10 +00:00
loadSubTotal() {
if (!this.$stateParams.id || !this.sales) return;
this.$http.get(`/api/Tickets/${this.$stateParams.id}/subtotal`).then(res => {
this.subtotal = res.data || 0.0;
});
2018-06-19 10:10:38 +00:00
}
2018-07-17 06:44:31 +00:00
2018-10-22 06:23:10 +00:00
getSaleTotal(sale) {
if (!sale.quantity || !sale.price)
return;
2018-10-22 06:23:10 +00:00
return sale.quantity * sale.price * ((100 - sale.discount) / 100);
}
2018-06-19 10:10:38 +00:00
2018-10-22 06:23:10 +00:00
loadVAT() {
this.VAT = 0.0;
if (!this.$stateParams.id || !this.sales) return;
this.$http.get(`/api/Tickets/${this.$stateParams.id}/getVAT`).then(res => {
2018-10-22 06:23:10 +00:00
this.VAT = res.data || 0.0;
2018-06-19 10:10:38 +00:00
});
}
2018-07-17 06:44:31 +00:00
2018-10-22 06:23:10 +00:00
get total() {
return this.subtotal + this.VAT;
2018-10-22 06:23:10 +00:00
}
onMoreOpen() {
2019-05-06 05:40:59 +00:00
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));
});
2018-10-22 06:23:10 +00:00
this.$scope.moreButton.data = options;
}
onMoreChange(callback) {
callback.call(this);
2018-06-19 10:10:38 +00:00
}
2018-07-17 06:44:31 +00:00
get isEditable() {
try {
return !this.ticket.state.state.alertLevel;
} catch (e) {}
return true;
}
2018-07-17 06:44:31 +00:00
get isChecked() {
2018-11-06 12:59:16 +00:00
if (this.sales) {
2018-10-22 06:23:10 +00:00
for (let instance of this.sales)
if (instance.checked) return true;
2018-11-06 12:59:16 +00:00
}
return false;
}
2018-07-17 06:44:31 +00:00
/**
* Returns checked instances
*
* @return {Array} Checked instances
*/
getCheckedLines() {
if (!this.sales) return;
return this.sales.filter(sale => {
return sale.checked;
});
}
/**
* Returns an array of indexes
* from checked instances
*
* @return {Array} Indexes of checked instances
*/
getCheckedLinesIndex() {
if (!this.sales) return;
let indexes = [];
this.sales.forEach((sale, index) => {
if (sale.checked) indexes.push(index);
});
return indexes;
}
removeCheckedLines() {
const sales = this.getCheckedLines();
sales.forEach(sale => {
const index = this.sales.indexOf(sale);
this.sales.splice(index, 1);
});
this.refreshTotal();
}
2018-07-17 06:44:31 +00:00
onStateOkClick() {
2018-11-06 12:59:16 +00:00
let filter = {where: {code: 'OK'}, fields: ['id']};
let json = encodeURIComponent(JSON.stringify(filter));
2019-06-20 11:50:58 +00:00
return this.$http.get(`/api/States?filter=${json}`).then(res => {
this.onStateChange(res.data[0].id);
});
}
2018-07-17 06:44:31 +00:00
onStateChange(value) {
let params = {ticketFk: this.$state.params.id, stateFk: value};
this.$http.post(`/api/TicketTrackings/changeState`, params).then(() => {
this.card.reload();
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
});
}
2018-07-17 06:44:31 +00:00
onRemoveLinesClick(response) {
if (response === 'ACCEPT') {
let sales = this.getCheckedLines();
// Remove unsaved instances
sales.forEach((sale, index) => {
if (!sale.id) sales.splice(index);
});
let params = {sales: sales, actualTicketFk: this.ticket.id};
let query = `/api/Sales/removes`;
this.$http.post(query, params).then(() => {
this.removeCheckedLines();
2018-09-05 14:14:13 +00:00
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
});
}
}
2018-07-17 06:44:31 +00:00
showRemoveLinesDialog() {
2018-07-17 06:44:31 +00:00
this.$scope.deleteLines.show();
}
2018-07-17 06:44:31 +00:00
showTransferPopover(event) {
let filter = {clientFk: this.ticket.clientFk, ticketFk: this.ticket.id};
let json = encodeURIComponent(JSON.stringify(filter));
let query = `/api/Tickets/threeLastActive?filter=${json}`;
2018-07-02 11:18:22 +00:00
this.$http.get(query).then(res => {
this.lastThreeTickets = res.data;
});
2018-07-17 06:44:31 +00:00
this.$scope.transfer.parent = event.target;
this.$scope.transfer.show();
}
2018-07-17 06:44:31 +00:00
checkEmptiness(receiverTicketId) {
let sales = this.getCheckedLines();
let areAllSalesSelected = sales.length === this.$scope.model.data.length;
this.receiverTicketId = receiverTicketId;
if (areAllSalesSelected) {
let query = `/api/Tickets/${this.ticket.id}/checkEmptiness`;
this.$http.get(query).then(res => {
if (res.data)
this.$scope.deleteTicket.show();
if (!res.data)
this.moveLines(false);
});
}
if (!areAllSalesSelected)
this.moveLines(false);
}
2018-07-17 06:44:31 +00:00
moveLines(removeEmptyTicket) {
let sales = this.getCheckedLines();
let currentTicketData = {
currentTicketId: this.ticket.id,
clientFk: this.ticket.clientFk,
addressFk: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk,
warehouseFk: this.ticket.warehouseFk
};
let params = {
currentTicket: currentTicketData,
receiverTicket: this.receiverTicketId ? {id: this.receiverTicketId} : currentTicketData,
sales: sales,
removeEmptyTicket: removeEmptyTicket
};
this.$http.post(`/api/Sales/moveToTicket`, params).then(res => {
if (res.data) {
this.receiverTicketId = null;
this.goToTicket(res.data.id);
}
});
}
2018-07-17 06:44:31 +00:00
2018-09-11 10:49:31 +00:00
createClaim() {
const claim = {
2018-09-11 10:49:31 +00:00
ticketFk: this.ticket.id,
clientFk: this.ticket.clientFk,
2019-03-21 13:56:09 +00:00
ticketCreated: this.ticket.shipped
2018-09-11 10:49:31 +00:00
};
const sales = this.getCheckedLines();
2019-05-27 06:46:11 +00:00
this.$http.post(`/api/Claims/createFromSales`, {claim: claim, sales: sales}).then(res => {
2018-11-06 12:59:16 +00:00
this.$state.go('claim.card.basicData', {id: res.data.id});
2018-09-11 10:49:31 +00:00
});
}
goToTicket(ticketID) {
2018-11-06 12:59:16 +00:00
this.$state.go('ticket.card.sale', {id: ticketID});
}
2018-07-17 06:44:31 +00:00
// Slesperson Mana
getManaSalespersonMana() {
this.$http.get(`/api/Tickets/${this.$state.params.id}/getSalesPersonMana`).then(res => {
this.mana = res.data;
});
}
2018-07-17 06:44:31 +00:00
// Item Descriptor
showDescriptor(event, itemFk) {
2018-09-04 09:49:00 +00:00
this.quicklinks = {
btnThree: {
icon: 'icon-transaction',
state: `item.card.diary({
id: ${itemFk},
warehouseFk: ${this.ticket.warehouseFk},
ticketFk: ${this.ticket.id}
2018-09-04 09:49:00 +00:00
})`,
tooltip: 'Item diary'
}
};
2018-07-17 06:44:31 +00:00
this.$scope.descriptor.itemFk = itemFk;
this.$scope.descriptor.parent = event.target;
this.$scope.descriptor.show();
}
2018-07-17 06:44:31 +00:00
onDescriptorLoad() {
2018-07-17 06:44:31 +00:00
this.$scope.popover.relocate();
2018-03-26 12:55:10 +00:00
}
2018-07-17 06:44:31 +00:00
showEditPricePopover(event, sale) {
if (!this.isEditable) return;
this.sale = sale;
this.editedPrice = this.sale.price;
this.edit = {
ticketFk: this.ticket.id,
id: sale.id,
quantity: sale.quantity
};
2018-07-17 06:44:31 +00:00
this.$scope.editPricePopover.parent = event.target;
this.$scope.editPricePopover.show();
}
2018-07-17 06:44:31 +00:00
updatePrice() {
if (this.editedPrice != this.sale.price) {
this.$http.post(`/api/Sales/${this.edit.id}/updatePrice`, {newPrice: this.editedPrice}).then(res => {
if (res.data)
this.sale.price = res.data.price;
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
});
}
2018-11-06 12:59:16 +00:00
2018-07-17 06:44:31 +00:00
this.$scope.editPricePopover.hide();
}
2018-07-17 06:44:31 +00:00
updateNewPrice() {
this.newPrice = this.sale.quantity * this.editedPrice - ((this.sale.discount * (this.sale.quantity * this.editedPrice)) / 100);
}
showEditDiscountPopover(event, sale) {
if (!this.isEditable) return;
this.sale = sale;
this.edit = [{
ticketFk: this.ticket.id,
id: sale.id,
quantity: sale.quantity,
price: sale.price,
discount: sale.discount
}];
2018-07-17 06:44:31 +00:00
this.$scope.editPopover.parent = event.target;
this.$scope.editPopover.show();
}
2018-07-17 06:44:31 +00:00
showEditDialog() {
this.edit = this.getCheckedLines();
2018-07-17 06:44:31 +00:00
this.$scope.editDialog.show();
}
2018-07-17 06:44:31 +00:00
hideEditDialog() {
2018-07-17 06:44:31 +00:00
this.$scope.model.refresh();
this.$scope.editDialog.hide();
}
2018-07-17 06:44:31 +00:00
hideEditPopover() {
2018-07-17 06:44:31 +00:00
this.$scope.model.refresh();
this.$scope.editPopover.hide();
}
2018-07-17 06:44:31 +00:00
/*
2018-10-23 12:33:07 +00:00
* Unmark sale as reserved
*/
2018-06-19 07:09:49 +00:00
unmarkAsReserved() {
this.setReserved(false);
}
/*
2018-10-23 12:33:07 +00:00
* Mark sale as reserved
*/
markAsReserved() {
this.setReserved(true);
}
2018-06-19 07:09:49 +00:00
setReserved(reserved) {
let selectedSales = this.getCheckedLines();
let params = {sales: selectedSales, ticketFk: this.ticket.id, reserved: reserved};
2018-06-19 07:09:49 +00:00
let reservedSales = new Map();
this.$http.post(`/api/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;
});
2018-06-19 07:09:49 +00:00
});
}
newOrderFromTicket() {
this.$http.post(`/api/Orders/newFromTicket`, {ticketFk: this.ticket.id}).then(res => {
this.$state.go('order.card.catalog', {id: res.data});
this.vnApp.showSuccess(this.$translate.instant('Order created'));
});
}
showSMSDialog() {
const address = this.ticket.address;
const sales = this.getCheckedLines();
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 = {
2019-04-16 05:59:12 +00:00
destinationFk: this.ticket.clientFk,
destination: address.mobile || null,
message: this.$translate.instant('SMSAvailability', params)
};
this.$scope.sms.open();
}
2019-05-06 05:40:59 +00:00
/**
* Returns if the current ticket
* is already invoiced
* @return {Boolean} - True if invoiced
*/
hasInvoice() {
return this.ticket.refFk !== null;
}
/**
* Inserts a new instance
*/
add() {
this.$scope.model.insert({});
}
/*
* Creates a new sale if it's a new instance
* Updates the sale quantity for existing instance
*/
onChangeQuantity(sale) {
if (!sale.id)
this.addSale(sale);
else
this.updateQuantity(sale);
}
/*
* Updates a sale quantity
*/
updateQuantity(sale) {
const data = {quantity: parseInt(sale.quantity)};
const query = `/api/Sales/${sale.id}/updateQuantity`;
this.$http.post(query, data).then(() => {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}).catch(e => {
this.$scope.model.refresh();
throw e;
});
}
/*
* Updates a sale concept
*/
updateConcept(sale) {
const data = {concept: sale.concept};
const query = `/api/Sales/${sale.id}`;
this.$http.patch(query, data).then(() => {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}).catch(e => {
this.$scope.model.refresh();
throw e;
});
}
/*
* Adds a new sale
*/
addSale(sale) {
const data = {
itemId: sale.itemFk,
quantity: sale.quantity
};
const query = `/api/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.$translate.instant('Data saved!'));
});
}
2018-03-26 12:55:10 +00:00
}
2018-07-17 06:44:31 +00:00
Controller.$inject = ['$scope', '$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: {
2018-10-22 06:23:10 +00:00
card: '?^vnTicketCard'
}
2018-03-22 17:02:48 +00:00
});