mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
terminando pdf de facturas
This commit is contained in:
@ -6,13 +6,18 @@ 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.dto.FacturaGuardarDto;
|
||||
import com.imprimelibros.erp.facturacion.dto.FacturaLineaUpsertDto;
|
||||
import com.imprimelibros.erp.facturacion.dto.FacturaPagoUpsertDto;
|
||||
import com.imprimelibros.erp.facturacion.repo.FacturaRepository;
|
||||
import com.imprimelibros.erp.facturacion.service.FacturacionService;
|
||||
import com.imprimelibros.erp.i18n.TranslationService;
|
||||
import com.imprimelibros.erp.pedidos.PedidoDireccion;
|
||||
import com.imprimelibros.erp.pedidos.PedidoService;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
@ -22,20 +27,20 @@ import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/facturas")
|
||||
@PreAuthorize("hasRole('SUPERADMIN') || hasRole('ADMIN')")
|
||||
public class FacturasController {
|
||||
|
||||
private final FacturacionService facturacionService;
|
||||
|
||||
private final FacturaRepository repo;
|
||||
private final TranslationService translationService;
|
||||
private final MessageSource messageSource;
|
||||
@ -45,11 +50,12 @@ public class FacturasController {
|
||||
FacturaRepository repo,
|
||||
TranslationService translationService,
|
||||
MessageSource messageSource,
|
||||
PedidoService pedidoService) {
|
||||
PedidoService pedidoService, FacturacionService facturacionService) {
|
||||
this.repo = repo;
|
||||
this.translationService = translationService;
|
||||
this.messageSource = messageSource;
|
||||
this.pedidoService = pedidoService;
|
||||
this.facturacionService = facturacionService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@ -75,12 +81,36 @@ public class FacturasController {
|
||||
PedidoDireccion direccionFacturacion = pedidoService
|
||||
.getPedidoDireccionFacturacionByPedidoId(factura.getPedidoId());
|
||||
|
||||
List<String> keys = List.of(
|
||||
"facturas.lineas.error.base",
|
||||
"facturas.lineas.delete.title",
|
||||
"facturas.lineas.delete.text",
|
||||
|
||||
"facturas.pagos.delete.title",
|
||||
"facturas.pagos.delete.text",
|
||||
"facturas.pagos.error.cantidad",
|
||||
"facturas.pagos.error.fecha",
|
||||
|
||||
"app.eliminar",
|
||||
"app.cancelar");
|
||||
|
||||
Map<String, String> translations = translationService.getTranslations(locale, keys);
|
||||
model.addAttribute("languageBundle", translations);
|
||||
|
||||
model.addAttribute("direccionFacturacion", direccionFacturacion);
|
||||
model.addAttribute("factura", factura);
|
||||
|
||||
return "imprimelibros/facturas/facturas-form";
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/guardar")
|
||||
public ResponseEntity<?> guardarFacturaCabeceraYDireccion(
|
||||
@PathVariable Long id,
|
||||
@RequestBody @Valid FacturaGuardarDto payload) {
|
||||
facturacionService.guardarCabeceraYDireccionFacturacion(id, payload);
|
||||
return ResponseEntity.ok(Map.of("ok", true));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/container")
|
||||
public String facturaContainer(@PathVariable Long id, Model model, Locale locale) {
|
||||
Factura factura = repo.findById(id)
|
||||
@ -116,7 +146,8 @@ public class FacturasController {
|
||||
.orElseThrow(() -> new EntityNotFoundException("Factura no encontrada con ID: " + id));
|
||||
|
||||
if (factura.getEstado() != EstadoFactura.validada) {
|
||||
return ResponseEntity.badRequest().body("Solo se pueden marcar como borrador facturas en estado 'validada'.");
|
||||
return ResponseEntity.badRequest()
|
||||
.body("Solo se pueden marcar como borrador facturas en estado 'validada'.");
|
||||
}
|
||||
|
||||
factura.setEstado(EstadoFactura.borrador);
|
||||
@ -125,22 +156,75 @@ public class FacturasController {
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@PostMapping("/{facturaId}/lineas")
|
||||
public ResponseEntity<?> createLinea(@PathVariable Long facturaId,
|
||||
@Valid @RequestBody FacturaLineaUpsertDto req) {
|
||||
facturacionService.createLinea(facturaId, req);
|
||||
return ResponseEntity.ok(Map.of("ok", true));
|
||||
}
|
||||
|
||||
@PostMapping("/{facturaId}/lineas/{lineaId}")
|
||||
public ResponseEntity<?> updateLinea(@PathVariable Long facturaId,
|
||||
@PathVariable Long lineaId,
|
||||
@Valid @RequestBody FacturaLineaUpsertDto req) {
|
||||
facturacionService.upsertLinea(facturaId, req);
|
||||
return ResponseEntity.ok(Map.of("ok", true));
|
||||
}
|
||||
|
||||
@PostMapping("/{facturaId}/lineas/{lineaId}/delete")
|
||||
public ResponseEntity<?> deleteLinea(@PathVariable Long facturaId,
|
||||
@PathVariable Long lineaId) {
|
||||
facturacionService.borrarLinea(facturaId, lineaId);
|
||||
return ResponseEntity.ok(Map.of("ok", true));
|
||||
}
|
||||
|
||||
/*
|
||||
* -----------------------------
|
||||
* Pagos
|
||||
* --------------------------------
|
||||
*/
|
||||
@PostMapping("/{facturaId}/pagos")
|
||||
public ResponseEntity<?> createPago(
|
||||
@PathVariable Long facturaId,
|
||||
@Valid @RequestBody FacturaPagoUpsertDto req, Principal principal) {
|
||||
facturacionService.upsertPago(facturaId, req, principal);
|
||||
return ResponseEntity.ok(Map.of("ok", true));
|
||||
}
|
||||
|
||||
@PostMapping("/{facturaId}/pagos/{pagoId}")
|
||||
public ResponseEntity<?> updatePago(
|
||||
@PathVariable Long facturaId,
|
||||
@PathVariable Long pagoId,
|
||||
@Valid @RequestBody FacturaPagoUpsertDto req,
|
||||
Principal principal) {
|
||||
// opcional: fuerza consistencia
|
||||
req.setId(pagoId);
|
||||
facturacionService.upsertPago(facturaId, req, principal);
|
||||
return ResponseEntity.ok(Map.of("ok", true));
|
||||
}
|
||||
|
||||
@PostMapping("/{facturaId}/pagos/{pagoId}/delete")
|
||||
public ResponseEntity<?> deletePago(
|
||||
@PathVariable Long facturaId,
|
||||
@PathVariable Long pagoId, Principal principal) {
|
||||
facturacionService.borrarPago(facturaId, pagoId, principal);
|
||||
return ResponseEntity.ok(Map.of("ok", true));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/notas")
|
||||
public ResponseEntity<?> setNotas(
|
||||
@PathVariable Long id,
|
||||
@RequestBody Map<String, String> payload,
|
||||
Model model,
|
||||
Locale locale
|
||||
) {
|
||||
Locale locale) {
|
||||
Factura factura = repo.findById(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Factura no encontrada con ID: " + id));
|
||||
String notas = payload.get("notas");
|
||||
factura.setNotas(notas);
|
||||
repo.save(factura);
|
||||
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------
|
||||
// API: DataTables (server-side)
|
||||
|
||||
Reference in New Issue
Block a user