mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
trabajando en el formulario de la factura
This commit is contained in:
@ -0,0 +1,146 @@
|
||||
package com.imprimelibros.erp.facturacion.controller;
|
||||
|
||||
import com.imprimelibros.erp.datatables.DataTable;
|
||||
import com.imprimelibros.erp.datatables.DataTablesParser;
|
||||
import com.imprimelibros.erp.datatables.DataTablesRequest;
|
||||
import com.imprimelibros.erp.datatables.DataTablesResponse;
|
||||
import com.imprimelibros.erp.facturacion.EstadoFactura;
|
||||
import com.imprimelibros.erp.facturacion.Factura;
|
||||
import com.imprimelibros.erp.facturacion.SerieFactura;
|
||||
import com.imprimelibros.erp.facturacion.TipoSerieFactura;
|
||||
import com.imprimelibros.erp.facturacion.repo.FacturaRepository;
|
||||
import com.imprimelibros.erp.facturacion.repo.SerieFacturaRepository;
|
||||
import com.imprimelibros.erp.i18n.TranslationService;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/facturas")
|
||||
@PreAuthorize("hasRole('SUPERADMIN') || hasRole('ADMIN')")
|
||||
public class FacturasController {
|
||||
|
||||
private final FacturaRepository repo;
|
||||
private final TranslationService translationService;
|
||||
private final MessageSource messageSource;
|
||||
|
||||
public FacturasController(
|
||||
FacturaRepository repo,
|
||||
TranslationService translationService,
|
||||
MessageSource messageSource) {
|
||||
this.repo = repo;
|
||||
this.translationService = translationService;
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String facturasList(Model model, Locale locale) {
|
||||
|
||||
List<String> keys = List.of(
|
||||
"app.eliminar",
|
||||
"app.cancelar",
|
||||
"facturas.delete.title",
|
||||
"facturas.delete.text",
|
||||
"facturas.delete.ok.title",
|
||||
"facturas.delete.ok.text");
|
||||
Map<String, String> translations = translationService.getTranslations(locale, keys);
|
||||
model.addAttribute("languageBundle", translations);
|
||||
return "imprimelibros/facturas/facturas-list";
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public String facturaDetail(@PathVariable Long id, Model model, Locale locale) {
|
||||
Factura factura = repo.findById(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Factura no encontrada con ID: " + id));
|
||||
|
||||
model.addAttribute("factura", factura);
|
||||
|
||||
return "imprimelibros/facturas/facturas-form";
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------
|
||||
// API: DataTables (server-side)
|
||||
// -----------------------------
|
||||
@GetMapping("/api/datatables")
|
||||
@ResponseBody
|
||||
public DataTablesResponse<Map<String, Object>> datatables(HttpServletRequest request, Locale locale) {
|
||||
|
||||
DataTablesRequest dt = DataTablesParser.from(request);
|
||||
|
||||
Specification<Factura> notDeleted = (root, q, cb) -> cb.isNull(root.get("deletedAt"));
|
||||
long total = repo.count(notDeleted);
|
||||
|
||||
return DataTable
|
||||
.of(repo, Factura.class, dt, List.of("clienteNombre", "numeroFactura", "estado", "estadoPago"))
|
||||
.where(notDeleted)
|
||||
.orderable(List.of("id", "clienteNombre", "numeroFactura", "estado", "estadoPago"))
|
||||
.onlyAddedColumns()
|
||||
.add("id", Factura::getId)
|
||||
.add("cliente", f -> {
|
||||
var c = f.getCliente();
|
||||
return c == null ? null : c.getFullName(); // o getNombre(), etc.
|
||||
})
|
||||
|
||||
.add("numero_factura", Factura::getNumeroFactura)
|
||||
.add("estado", Factura::getEstado)
|
||||
.add("estado_label", f -> {
|
||||
String key = "facturas.estado." + f.getEstado().name().toLowerCase();
|
||||
return messageSource.getMessage(key, null, f.getEstado().name(), locale);
|
||||
})
|
||||
.add("estado_pago", Factura::getEstadoPago)
|
||||
.add("estado_pago_label", f -> {
|
||||
String key = "facturas.estado-pago." + f.getEstadoPago().name().toLowerCase();
|
||||
return messageSource.getMessage(key, null, f.getEstadoPago().name(), locale);
|
||||
})
|
||||
.add("total", Factura::getTotalFactura)
|
||||
.add("fecha_emision", f -> {
|
||||
LocalDateTime fecha = f.getFechaEmision();
|
||||
return fecha == null ? null : fecha.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
|
||||
})
|
||||
.add("actions", f -> {
|
||||
if (f.getEstado() == EstadoFactura.borrador) {
|
||||
return """
|
||||
<div class="hstack gap-3 flex-wrap">
|
||||
<button type="button"
|
||||
class="btn p-0 link-success btn-view-factura fs-15"
|
||||
data-id="%d">
|
||||
<i class="ri-eye-line"></i>
|
||||
</button>
|
||||
<button type="button"
|
||||
class="btn p-0 link-danger btn-delete-factura fs-15"
|
||||
data-id="%d">
|
||||
<i class="ri-delete-bin-5-line"></i>
|
||||
</button>
|
||||
</div>
|
||||
""".formatted(f.getId(), f.getId());
|
||||
} else {
|
||||
return """
|
||||
<div class="hstack gap-3 flex-wrap">
|
||||
<button type="button"
|
||||
class="btn p-0 link-success btn-view-factura fs-15"
|
||||
data-id="%d">
|
||||
<i class="ri-eye-line"></i>
|
||||
</button>
|
||||
</div>
|
||||
""".formatted(f.getId());
|
||||
}
|
||||
})
|
||||
.toJson(total);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user