mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
series de facturación terminadas (vista en configuración)
This commit is contained in:
@ -0,0 +1,9 @@
|
||||
package com.imprimelibros.erp.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
|
||||
@Configuration
|
||||
@EnableMethodSecurity
|
||||
public class MethodSecurityConfig {
|
||||
}
|
||||
@ -18,7 +18,7 @@ public class SerieFactura extends AbstractAuditedEntitySoftTs {
|
||||
private TipoSerieFactura tipo = TipoSerieFactura.facturacion;
|
||||
|
||||
@Column(name = "numero_actual", nullable = false)
|
||||
private Integer numeroActual = 1;
|
||||
private Long numeroActual = 1L;
|
||||
|
||||
public String getNombreSerie() { return nombreSerie; }
|
||||
public void setNombreSerie(String nombreSerie) { this.nombreSerie = nombreSerie; }
|
||||
@ -29,6 +29,6 @@ public class SerieFactura extends AbstractAuditedEntitySoftTs {
|
||||
public TipoSerieFactura getTipo() { return tipo; }
|
||||
public void setTipo(TipoSerieFactura tipo) { this.tipo = tipo; }
|
||||
|
||||
public Integer getNumeroActual() { return numeroActual; }
|
||||
public void setNumeroActual(Integer numeroActual) { this.numeroActual = numeroActual; }
|
||||
public Long getNumeroActual() { return numeroActual; }
|
||||
public void setNumeroActual(Long numeroActual) { this.numeroActual = numeroActual; }
|
||||
}
|
||||
|
||||
@ -0,0 +1,201 @@
|
||||
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.SerieFactura;
|
||||
import com.imprimelibros.erp.facturacion.TipoSerieFactura;
|
||||
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.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/configuracion/series-facturacion")
|
||||
@PreAuthorize("hasRole('SUPERADMIN')")
|
||||
public class SeriesFacturacionController {
|
||||
|
||||
private final SerieFacturaRepository repo;
|
||||
private final TranslationService translationService;
|
||||
private final MessageSource messageSource;
|
||||
|
||||
public SeriesFacturacionController(SerieFacturaRepository repo, TranslationService translationService,
|
||||
MessageSource messageSource) {
|
||||
this.repo = repo;
|
||||
this.translationService = translationService;
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// VISTA
|
||||
// -----------------------------
|
||||
@GetMapping
|
||||
public String listView(Model model, Locale locale) {
|
||||
|
||||
List<String> keys = List.of(
|
||||
"series-facturacion.modal.title.add",
|
||||
"series-facturacion.modal.title.edit",
|
||||
"app.guardar",
|
||||
"app.cancelar",
|
||||
"app.eliminar",
|
||||
"series-facturacion.delete.title",
|
||||
"series-facturacion.delete.text",
|
||||
"series-facturacion.delete.ok.title",
|
||||
"series-facturacion.delete.ok.text");
|
||||
|
||||
Map<String, String> translations = translationService.getTranslations(locale, keys);
|
||||
model.addAttribute("languageBundle", translations);
|
||||
|
||||
return "imprimelibros/configuracion/series-facturas/series-facturas-list";
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// API: DataTables (server-side)
|
||||
// -----------------------------
|
||||
@GetMapping("/api/datatables")
|
||||
@ResponseBody
|
||||
public DataTablesResponse<Map<String, Object>> datatables(HttpServletRequest request, Locale locale) {
|
||||
|
||||
DataTablesRequest dt = DataTablesParser.from(request);
|
||||
|
||||
Specification<SerieFactura> notDeleted = (root, q, cb) -> cb.isNull(root.get("deletedAt"));
|
||||
long total = repo.count(notDeleted);
|
||||
|
||||
return DataTable
|
||||
.of(repo, SerieFactura.class, dt, List.of("nombreSerie", "prefijo"))
|
||||
.where(notDeleted)
|
||||
.orderable(List.of("id", "nombreSerie", "prefijo", "tipo", "numeroActual"))
|
||||
.onlyAddedColumns()
|
||||
.add("id", SerieFactura::getId)
|
||||
.add("nombre_serie", SerieFactura::getNombreSerie)
|
||||
.add("prefijo", SerieFactura::getPrefijo)
|
||||
.add("tipo", s -> s.getTipo() != null ? s.getTipo().name() : null)
|
||||
.add("tipo_label", s -> {
|
||||
if (s.getTipo() == null)
|
||||
return null;
|
||||
return messageSource.getMessage(
|
||||
"series-facturacion.tipo." + s.getTipo().name(),
|
||||
null,
|
||||
s.getTipo().name(),
|
||||
locale);
|
||||
})
|
||||
.add("numero_actual", SerieFactura::getNumeroActual)
|
||||
.add("actions", s -> """
|
||||
<div class="hstack gap-3 flex-wrap">
|
||||
<button type="button"
|
||||
class="btn btn-link p-0 link-success btn-edit-serie fs-15"
|
||||
data-id="%d">
|
||||
<i class="ri-edit-2-line"></i>
|
||||
</button>
|
||||
<button type="button"
|
||||
class="btn btn-link p-0 link-danger btn-delete-serie fs-15"
|
||||
data-id="%d">
|
||||
<i class="ri-delete-bin-5-line"></i>
|
||||
</button>
|
||||
</div>
|
||||
""".formatted(s.getId(), s.getId()))
|
||||
.toJson(total);
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// API: CREATE
|
||||
// -----------------------------
|
||||
@PostMapping(value = "/api", consumes = "application/json")
|
||||
@ResponseBody
|
||||
public Map<String, Object> create(@RequestBody SerieFacturaPayload payload) {
|
||||
validate(payload);
|
||||
|
||||
SerieFactura s = new SerieFactura();
|
||||
s.setNombreSerie(payload.nombre_serie.trim());
|
||||
s.setPrefijo(payload.prefijo.trim());
|
||||
s.setTipo(TipoSerieFactura.facturacion); // fijo
|
||||
s.setNumeroActual(payload.numero_actual);
|
||||
|
||||
repo.save(s);
|
||||
return Map.of("ok", true, "id", s.getId());
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// API: UPDATE
|
||||
// -----------------------------
|
||||
@PutMapping(value = "/api/{id}", consumes = "application/json")
|
||||
@ResponseBody
|
||||
public Map<String, Object> update(@PathVariable Long id, @RequestBody SerieFacturaPayload payload) {
|
||||
validate(payload);
|
||||
|
||||
SerieFactura s = repo.findById(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Serie no encontrada: " + id));
|
||||
|
||||
if (s.getDeletedAt() != null) {
|
||||
throw new IllegalStateException("No se puede editar una serie eliminada.");
|
||||
}
|
||||
|
||||
s.setNombreSerie(payload.nombre_serie.trim());
|
||||
s.setPrefijo(payload.prefijo.trim());
|
||||
s.setTipo(TipoSerieFactura.facturacion);
|
||||
s.setNumeroActual(payload.numero_actual);
|
||||
|
||||
repo.save(s);
|
||||
return Map.of("ok", true);
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// API: DELETE (soft)
|
||||
// -----------------------------
|
||||
@DeleteMapping("/api/{id}")
|
||||
@ResponseBody
|
||||
public ResponseEntity<?> delete(@PathVariable Long id) {
|
||||
SerieFactura s = repo.findById(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Serie no encontrada: " + id));
|
||||
|
||||
if (s.getDeletedAt() == null) {
|
||||
s.setDeletedAt(Instant.now());
|
||||
s.setDeletedBy(null); // luego lo conectamos al usuario actual
|
||||
repo.save(s);
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(Map.of("ok", true));
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Payload + validación
|
||||
// -----------------------------
|
||||
public static class SerieFacturaPayload {
|
||||
public String nombre_serie;
|
||||
public String prefijo;
|
||||
public String tipo; // lo manda UI, pero en backend lo fijamos
|
||||
public Long numero_actual;
|
||||
}
|
||||
|
||||
private void validate(SerieFacturaPayload p) {
|
||||
if (p == null)
|
||||
throw new IllegalArgumentException("Body requerido.");
|
||||
if (p.nombre_serie == null || p.nombre_serie.trim().isBlank()) {
|
||||
throw new IllegalArgumentException("nombre_serie es obligatorio.");
|
||||
}
|
||||
if (p.prefijo == null || p.prefijo.trim().isBlank()) {
|
||||
throw new IllegalArgumentException("prefijo es obligatorio.");
|
||||
}
|
||||
if (p.prefijo.trim().length() > 10) {
|
||||
throw new IllegalArgumentException("prefijo máximo 10 caracteres.");
|
||||
}
|
||||
if (p.numero_actual == null || p.numero_actual < 1) {
|
||||
throw new IllegalArgumentException("numero_actual debe ser >= 1.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package com.imprimelibros.erp.facturacion.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public class SerieFacturaForm {
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 100)
|
||||
private String nombreSerie;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 10)
|
||||
private String prefijo;
|
||||
|
||||
@NotNull
|
||||
private Long numeroActual;
|
||||
|
||||
public String getNombreSerie() { return nombreSerie; }
|
||||
public void setNombreSerie(String nombreSerie) { this.nombreSerie = nombreSerie; }
|
||||
|
||||
public String getPrefijo() { return prefijo; }
|
||||
public void setPrefijo(String prefijo) { this.prefijo = prefijo; }
|
||||
|
||||
public Long getNumeroActual() { return numeroActual; }
|
||||
public void setNumeroActual(Long numeroActual) { this.numeroActual = numeroActual; }
|
||||
}
|
||||
@ -4,15 +4,19 @@ import com.imprimelibros.erp.facturacion.SerieFactura;
|
||||
import com.imprimelibros.erp.facturacion.TipoSerieFactura;
|
||||
import org.springframework.data.jpa.repository.*;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
import jakarta.persistence.LockModeType;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface SerieFacturaRepository extends JpaRepository<SerieFactura, Long> {
|
||||
public interface SerieFacturaRepository extends JpaRepository<SerieFactura, Long>, JpaSpecificationExecutor<SerieFactura> {
|
||||
|
||||
Optional<SerieFactura> findByTipo(TipoSerieFactura tipo);
|
||||
|
||||
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||
@Query("select s from SerieFactura s where s.id = :id")
|
||||
Optional<SerieFactura> findByIdForUpdate(@Param("id") Long id);
|
||||
List<SerieFactura> findAllByDeletedAtIsNullOrderByNombreSerieAsc();
|
||||
}
|
||||
|
||||
@ -64,7 +64,7 @@ public class FacturacionService {
|
||||
factura.setNumeroFactura(numeroFactura);
|
||||
|
||||
// Incrementar contador para la siguiente
|
||||
serieLocked.setNumeroActual((int) (next + 1)); // si cambias numero_actual a BIGINT en entidad, quita el cast
|
||||
serieLocked.setNumeroActual(next + 1);
|
||||
serieRepo.save(serieLocked);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user