import ngModule from '../module'; import FilterTicketList from '../filter-ticket-list'; import './style.scss'; class Controller extends FilterTicketList { constructor($scope, $timeout, $stateParams, $http, $state, vnApp) { super($scope, $timeout, $stateParams); this.$ = $scope; this.vnApp = vnApp; this.$timeout = $timeout; this.onOrder('itemFk', 'ASC'); this.$state = $stateParams; this.$http = $http; this.deletable = false; this.moreOptions = [ {callback: this.showAddTurnDialog, name: "Add turn"}, {callback: this.showDeleteTicketDialog, name: "Delete ticket"} ]; } 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.showMessage(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(); }); } // 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() { 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); }); } // Move Lines showTransferPopover(event) { let filter = {clientFk: this.ticket.clientFk, ticketFk: this.ticket.id}; let json = encodeURIComponent(JSON.stringify(filter)); this.$http.get(`/ticket/api/Tickets/threeLastActive?filter=${json}`).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); }); } /* newTicket() { let params = [this.ticket.clientFk, this.ticket.warehouseFk, this.ticket.companyFk, this.ticket.addressFk, this.ticket.agencyModeFk, null]; this.$http.post(`/ticket/api/Tickets/create`, params).then(res => { console.log(res); }); }*/ goToTicket(ticketID) { this.$state.go("ticket.card.sale", {id: ticketID}); } removeInstances(instances) { for (let i = instances.length - 1; i >= 0; i--) { this.$.index.model.instances.splice(instances[i].instance, 1); } } // Item Descriptor showDescriptor(event, itemFk) { this.$.descriptor.itemFk = itemFk; this.$.descriptor.parent = event.target; this.$.descriptor.show(); } onDescriptorLoad() { this.$.popover.relocate(); } // Ticket Create showticketCreate() { console.log(this); this.$.newTicket.show(); } onResponse(response) { if (response === 'ACCEPT') { let newTicketID = this.$.newTicket.dialog.createTicket(); console.log(newTicketID); } } // Edit Line _getworkerMana() { this.$http.get(`/api/WorkerManas/getCurrentWorkerMana`).then(res => { this.workerMana = res.data[0].mana; }); } showEditPopover(event, sale) { this.sale = sale; this.edit = { id: sale.id, quantity: sale.quantity, price: sale.price, discount: sale.discount }; this.$.edit.parent = event.target; this._getworkerMana(); this.$.edit.show(); } 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(); } onMoreClick() { this.removeOptionByName('Mark as reserved'); this.removeOptionByName('Unmark as reserved'); if (!this.isChecked) return; this.moreOptions.push({ callback: this.markAsReserved, name: 'Mark as reserved'} ); this.moreOptions.push({ callback: this.unmarkAsReserved, name: 'Unmark as reserved'} ); } /** * 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); } } /** * Mark sale as reserved */ markAsReserved() { this.setReserved(true); } /** * Unmark sale as reserved */ unmarkAsReserved() { this.setReserved(false); } /** * Mark/Unmark sale as reserved from selected lines * @param {Boolean} reserved reserved */ setReserved(reserved) { let data = { delete: [], create: [], update: this.getCheckedLines() }; data.update.forEach(line => { line.reserved = reserved; }); this.$http.post(`/ticket/api/Sales/crudSale`, data).then(() => { this.$.index.accept(); }); } } Controller.$inject = ['$scope', '$timeout', '$state', '$http', 'vnApp']; ngModule.component('vnTicketSale', { template: require('./index.html'), controller: Controller, bindings: { ticket: '<' }, require: { card: '^vnTicketCard' } });