Skip to content

Commit

Permalink
Funcionalidad: Editar Factura
Browse files Browse the repository at this point in the history
  • Loading branch information
FazeElian committed Feb 5, 2024
1 parent b9754a8 commit 8b2a7cc
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 50 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/FactureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ public function edit($id)
$facture = Facture::find($id);
$customers = Customer::pluck('name', 'id');
$products = $facture->products;
$allProducts = Product::all();
$selectedProducts = $facture->products;

return view('Admin.facture.edit', compact('facture', 'products', 'customers'));
return view('Admin.facture.edit', compact('facture', 'products', 'customers', 'allProducts', 'selectedProducts'));
}

public function update(Request $request, $id)
Expand Down
7 changes: 0 additions & 7 deletions resources/sass/_variables.scss

This file was deleted.

8 changes: 0 additions & 8 deletions resources/sass/app.scss

This file was deleted.

11 changes: 11 additions & 0 deletions resources/views/Admin/facture/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
<form method="POST" action="{{ route('factures.store') }}" role="form" enctype="multipart/form-data">
@csrf
@include('Admin.facture.form')

<!-- Opciones Factura -->
<div class="opciones-factura">
<!-- Botón: Cancelar -->
<button class="bott-cancelar" type="button">
<a href="{{ route("factures.index") }}"><h2>Cancelar</h2></a>
</button>
<button class="bott-guardar-cambios" type="submit">
<a href="#"><h2>Crear factura</h2></a>
</button>
</div>
</form>
</section>
</body>
Expand Down
191 changes: 170 additions & 21 deletions resources/views/Admin/facture/edit.blade.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,179 @@
@extends('layouts.app')
@include("layouts.headerAdmin")

@section('template_title')
{{ __('Update') }} Facture
@endsection
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Facturas | Editar</title>

@section('content')
<section class="content container-fluid">
<div class="">
<div class="col-md-12">
{{-- Estilos para este archivo --}}
<link rel="stylesheet" href="{{ asset("assets/css/Admin/modules/Factures/form-styles.css") }}">
</head>
<body>
<section class="contenido-pag">
<!-- Titulo de ventana -->
<div class="titulo-ventana">
<h1 class="titulo">Editar Factura</h1>
</div>

<form method="POST" action="{{ route('factures.update', $facture->id) }}" role="form" enctype="multipart/form-data">
{{ method_field('PATCH') }}

@csrf

@includeif('partials.errors')
<!-- Contenedor de nueva factura -->
<div class="cont-factura">
<!-- Contenedor información de factura -->
<div class="cont-info-fact">
<!-- Fecha -->
<label for="date">Fecha:</label>
{{ Form::date('date', $facture->date, ['class' => 'form-control' . ($errors->has('date') ? ' is-invalid' : ''), 'placeholder' => 'Seleccionar una fecha', "id" => "fechaFactura", "name" => "date"]) }}
<br>

<div class="card card-default">
<div class="card-header">
<span class="card-title">{{ __('Update') }} Facture</span>
<!-- Cliente -->
<label for="customer_id">Cliente:</label>
{{ Form::select('customer_id', $customers, $facture->customer_id, ['class' => 'form-control' . ($errors->has('customer_id') ? ' is-invalid' : ''), "id" => "seleccionarCliente", "placeholder" => "Seleccionar cliente", "name" => "customer_id" ]) }}
</div>
<div class="card-body">
<form method="POST" action="{{ route('factures.update', $facture->id) }}" role="form" enctype="multipart/form-data">
{{ method_field('PATCH') }}
@csrf

@include('Admin.facture.form')
</form>
<!-- Tabla lista de productos de factura -->
<table class="tab-productos-fact" id="listaProductos">
<!-- Columnas características de lista de productos -->
<tr class="columnas-caract">
<td class="item-columna nombre-prod">Nombre producto</td>
<td class="item-columna cantidad-prod">Cantidad</td>
<td class="item-columna precio-prod">Precio /u</td>
<td class="item-columna elim-fila">
<img src="{{ asset("assets/img/Admin/modules/icono-eliminar-rojo.png") }}" alt="">
</td>
</tr>

<!-- Mostrar productos asociados a la factura -->
@foreach ($selectedProducts as $product)
<tr class="filas-datos">
<td class="item-fila nombre-prod-dato">
<select name='product_id[]' id="seleccionarProducto">
@foreach ($allProducts as $availableProduct)
<option value='{{ $availableProduct->id }}' {{ $availableProduct->id == $product->id ? 'selected' : '' }}>
{{ $availableProduct->name }}
</option>
@endforeach
</select>
</td>
<td class="item-fila cantidad-prod">
<input type="number" name="quantity[]" id="cantidadProd" placeholder="#" value="{{ $product->pivot->quantity }}">
</td>
<td class="item-fila precio-prod">
<input type="number" name="price[]" id="precioProd" placeholder="$$$" value="{{ $product->pivot->price }}">
</td>
<td class="item-fila elim-fila-dato">
<img src="{{ asset("assets/img/Admin/modules/icono-eliminar-rojo.png") }}" alt="Eliminar Producto" onclick="removeRow(this)">
</td>
</tr>
@endforeach
</table>

<!-- Contenedor añadir fila -->
<div class="cont-anadir-fila">
<!-- Botón -->
<div class="btn-anadir-fila" id="btn-anadir-fila">
Añadir Producto
</div>
</div>
</div>
</div>
</div>

<!-- Opciones Factura -->
<div class="opciones-factura">
<!-- Botón: Cancelar -->
<button class="bott-cancelar" type="button">
<a href="{{ route("factures.index") }}"><h2>Cancelar</h2></a>
</button>
<button class="bott-guardar-cambios" type="submit">
<a href="#"><h2>Actualizar factura</h2></a>
</button>
</div>
</form>
</section>
@endsection
</body>
</html>
<script>
document.getElementById('btn-anadir-fila').addEventListener('click', function() {
const table = document.getElementById('listaProductos').getElementsByTagName('tbody')[0]; // Cambia el índice a 0
const newRow = table.insertRow(table.rows.length);
// Agregar la clase "filas-datos" al nuevo elemento <tr>
newRow.classList.add("filas-datos");
const productName = newRow.insertCell(0);
const productQuantity = newRow.insertCell(1);
const productPrice = newRow.insertCell(2);
const deleteRowBtn = newRow.insertCell(3);
// Obtener la lista de productos disponibles (puedes pasarla desde el controlador)
const productList = {!! json_encode($products) !!};
// Crear un elemento select y agregar opciones para cada producto
const select = document.createElement('select');
select.name = 'product_id[]';
select.id = "seleccionarProducto";
productName.classList.add('item-fila', 'nombre-prod-dato'); // Agregar clases CSS
// Agregar una opción por defecto "Seleccionar producto" al inicio
const defaultOption = document.createElement('option');
defaultOption.value = ''; // Valor vacío
defaultOption.text = 'Seleccionar producto';
select.appendChild(defaultOption);
for (const product of productList) {
const option = document.createElement('option');
option.value = product.id; // Asignar el valor del ID del producto
option.text = product.name; // Mostrar el nombre del producto en la opción
select.appendChild(option);
}
// Agregar el select a la celda
productName.appendChild(select);
// Cantidad de Producto
productQuantity.innerHTML = '<td><input type="number" name="quantity[]" id="cantidadProd" placeholder="#"></td>';
productQuantity.className = "item-fila cantidad-prod";
// Precio de Producto
productPrice.innerHTML = '<td><input type="number" name="price[]" id="precioProd" placeholder="$$$"></td>';
productPrice.className = "item-fila precio-prod";
// deleteRowBtn.innerHTML = '<td class="item-fila elim-fila-dato"><button type="button" onclick="removeRow(this)">Eliminar</button></td>';
deleteRowBtn.innerHTML = '<td><img src="{{ asset("assets/img/Admin/modules/icono-eliminar-rojo.png") }}" alt="Eliminar Producto" onclick="removeRow(this)"></td>';
deleteRowBtn.className = "item-fila elim-fila-dato";
});
// Función para eliminar filas
function removeRow(button){
// Obtén la fila actual a través del botón
const row = button.parentElement.parentElement;
// Elimina la fila
row.remove();
}
// Agregar un evento de cambio al select generado dinámicamente
document.addEventListener('change', function(event) {
const target = event.target;
// Verificar si el elemento cambiado es un select de producto
if (target && target.tagName === 'SELECT' && target.name === 'product_id[]') {
const selectedProductId = target.value;
const priceField = target.parentNode.nextElementSibling.nextElementSibling.querySelector('input[name="price[]"]');
// Buscar el producto seleccionado en la lista de productos disponibles
const selectedProduct = {!! json_encode($products) !!}.find(product => product.id == selectedProductId);
// Actualizar el campo de precio con el precio del producto seleccionado
if (selectedProduct) {
priceField.value = selectedProduct.price;
} else {
priceField.value = ''; // Si el producto no se encuentra, se limpia el campo de precio
}
}
});
</script>
11 changes: 0 additions & 11 deletions resources/views/Admin/facture/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,6 @@
</div>
</div>
</div>

<!-- Opciones Factura -->
<div class="opciones-factura">
<!-- Botón: Cancelar -->
<button class="bott-cancelar" type="button">
<a href="{{ route("factures.index") }}"><h2>Cancelar</h2></a>
</button>
<button class="bott-guardar-cambios" type="submit">
<a href="#"><h2>Crear factura</h2></a>
</button>
</div>
</body>
</html>
<script>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/Admin/facture/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<td class="opciones-factura-tabla-dato">
<a href=".pdf"><button class="btn-factura btn-pdf"><a href="{{ route("factures.pdf", ['id' => $facture->id]) }}" target="_blank" class="link-op-fact">PDF</a><img src="{{ asset("assets/img/Admin/modules/pdf-factura-icono.png") }}" alt="PDF Icono"></button></a>
<a href="{{ route('factures.show', $facture->id) }}"><button class="btn-factura btn-ver"><a href="{{ route('factures.show', $facture->id) }}" class="link-op-fact">Ver</a><a href="{{ route('factures.show', $facture->id) }}"><img src="{{ asset("assets/img/Admin/modules/ver-factura-icono.png") }}" alt="Ver Icono" class="icono-op-fact"></button></a>
<a href="#"><button class="btn-factura btn-editar"><a href="#" class="link-op-fact">Editar</a><a href="#"><img src="{{ asset("assets/img/Admin/modules/editar-icono.png") }}" alt="Editar Icono"></a></button></a>
<a href="{{ route("factures.edit", $facture->id) }}"><button class="btn-factura btn-editar"><a href="{{ route("factures.edit", $facture->id) }}" class="link-op-fact">Editar</a><a href="{{ route("factures.edit", $facture->id) }}"><img src="{{ asset("assets/img/Admin/modules/editar-icono.png") }}" alt="Editar Icono"></a></button></a>

<form action="{{ route('factures.destroy', $facture->id) }}" method="POST" class="btn-eliminar">
@csrf
Expand Down
2 changes: 1 addition & 1 deletion resources/views/Admin/facture/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<div class="cont-volver">
<a href="{{ route("factures.index") }}" class="link-btn-volver">
<button class="btn-volver">
<img src="{{ asset("assets/img/Admin/modules/volver_flecha-icono.png") }}" alt="Volver icono">
<img src="{{ asset("assets/img/Admin/modules/volver-flecha-icono-negro.png") }}" alt="Volver icono">
<h2>Volver</h2>
</button>
</a>
Expand Down

0 comments on commit 8b2a7cc

Please sign in to comment.