diff --git a/tienda_ilusion/don_confiao/frontend/don-confiao/src/components/Purchase.vue b/tienda_ilusion/don_confiao/frontend/don-confiao/src/components/Purchase.vue index 65bca7b..dcca9d2 100644 --- a/tienda_ilusion/don_confiao/frontend/don-confiao/src/components/Purchase.vue +++ b/tienda_ilusion/don_confiao/frontend/don-confiao/src/components/Purchase.vue @@ -1,6 +1,6 @@ @@ -148,6 +157,9 @@ data() { return { valid: false, + form_changed: false, + show_alert_lines: false, + show_alert_purchase: false, client_search: '', product_search: '', payment_methods: null, @@ -179,6 +191,12 @@ this.drawer = false }, }, + beforeMount() { + window.addEventListener('beforeunload', this.confirmLeave); + }, + beforeDestroy() { + window.removeEventListener('beforeunload', this.confirmLeave); + }, computed: { calculateTotal() { return this.purchase.saleline_set.reduce((total, saleline) => { @@ -208,9 +226,20 @@ openModal() { this.$refs.customerModal.openModal(); }, + onFormChange() { + this.form_changed = true; + }, openCasherModal() { this.$refs.casherModal.dialog = true }, + confirmLeave(event) { + if (this.form_changed) { + const message = '¿seguro que quieres salir? Perderas la información diligenciada'; + event.preventDefault(); + event.returnValue = message; + return message; + } + }, getCurrentDate() { const today = new Date(); const yyyy = today.getFullYear(); @@ -223,7 +252,6 @@ const selectedProductId = this.purchase.saleline_set[index].product; const selectedProduct = this.products.find(p => p.id == selectedProductId); this.purchase.saleline_set[index].unit_price = selectedProduct.price; - console.log(selectedProduct.measuring_unit); this.purchase.saleline_set[index].measuring_unit = selectedProduct.measuring_unit; }, fetchClients() { @@ -264,29 +292,21 @@ this.purchase.saleline_set.push({ product: '', unit_price: 0, quantity:0, measuring_unit: ''}); }, removeLine(index) { - // Solo elimina si hay más de una línea if (this.purchase.saleline_set.length > 1) { this.purchase.saleline_set.splice(index, 1); } else { - // Opcional: puedes mostrar un mensaje o alerta si lo deseas - console.log("No se puede eliminar la única línea."); + this.show_alert_lines = true; + setTimeout(() => { + this.show_alert_lines = false; + }, 2000); } }, calculateSubtotal(line) { return line.unit_price * line.quantity; }, async submit() { - if (this.$refs.form.validate()) { - const hasInvalidQuantity = this.purchase.saleline_set.some(line => line.quantity <= 0); - if (hasInvalidQuantity) { - this.errorMessage = 'La cantidad de cada línea de compra debe ser mayor que cero.'; - - console.log(this.errorMessage); - return; // Detener el submit si hay cantidades inválidas - } - - this.errorMessage = ''; // Limpiar el mensaje de error - + this.$refs.purchase.validate(); + if (this.valid) { try { const response = await fetch('/don_confiao/api/sales/', { method: 'POST', @@ -308,6 +328,11 @@ } catch (error) { console.error('Error de red:', error); } + } else { + this.show_alert_purchase = true; + setTimeout(() => { + this.show_alert_purchase = false; + }, 4000); } }, navigate(route) {