mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-02-28 05:39:13 +00:00
hehco
This commit is contained in:
@ -513,6 +513,102 @@ public class PresupuestoController {
|
||||
return ResponseEntity.ok(resumen);
|
||||
}
|
||||
|
||||
@PostMapping("/public/prepare-claim")
|
||||
public ResponseEntity<?> prepareClaim(
|
||||
@RequestBody Map<String, Object> body,
|
||||
HttpServletRequest request) {
|
||||
|
||||
Long presupuestoId = objectMapper.convertValue(body.get("presupuestoId"), Long.class);
|
||||
if (presupuestoId == null) {
|
||||
return ResponseEntity.badRequest().body(Map.of("message", "missing presupuestoId"));
|
||||
}
|
||||
|
||||
Presupuesto p = presupuestoRepository.findById(presupuestoId).orElse(null);
|
||||
if (p == null) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||
.body(Map.of("message", "presupuesto not found"));
|
||||
}
|
||||
|
||||
if (p.getOrigen() != Presupuesto.Origen.publico) {
|
||||
return ResponseEntity.badRequest().body(Map.of("message", "presupuesto not public"));
|
||||
}
|
||||
|
||||
request.getSession(true).setAttribute("presupuesto_claim_id", presupuestoId);
|
||||
return ResponseEntity.ok(Map.of("success", true));
|
||||
}
|
||||
|
||||
@GetMapping("/claim")
|
||||
@Transactional
|
||||
public String claimPresupuesto(
|
||||
HttpServletRequest request,
|
||||
Authentication authentication,
|
||||
RedirectAttributes redirectAttributes,
|
||||
Locale locale) {
|
||||
|
||||
Object attr = request.getSession(false) != null
|
||||
? request.getSession(false).getAttribute("presupuesto_claim_id")
|
||||
: null;
|
||||
|
||||
Long presupuestoId = null;
|
||||
if (attr instanceof Long) {
|
||||
presupuestoId = (Long) attr;
|
||||
} else if (attr != null) {
|
||||
try {
|
||||
presupuestoId = Long.valueOf(attr.toString());
|
||||
} catch (NumberFormatException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
if (presupuestoId == null) {
|
||||
redirectAttributes.addFlashAttribute("errorMessage",
|
||||
messageSource.getMessage("presupuesto.errores.presupuesto-no-existe", new Object[] { 0 }, locale));
|
||||
return "redirect:/presupuesto";
|
||||
}
|
||||
|
||||
Presupuesto p = presupuestoRepository.findById(presupuestoId).orElse(null);
|
||||
if (p == null) {
|
||||
redirectAttributes.addFlashAttribute("errorMessage",
|
||||
messageSource.getMessage("presupuesto.errores.presupuesto-no-existe",
|
||||
new Object[] { presupuestoId }, locale));
|
||||
return "redirect:/presupuesto";
|
||||
}
|
||||
|
||||
if (p.getUser() != null && authentication != null) {
|
||||
Long currentUserId = null;
|
||||
if (authentication.getPrincipal() instanceof UserDetailsImpl udi) {
|
||||
currentUserId = udi.getId();
|
||||
} else {
|
||||
currentUserId = userRepo.findIdByUserNameIgnoreCase(authentication.getName()).orElse(null);
|
||||
}
|
||||
if (currentUserId != null && p.getUser().getId().equals(currentUserId)) {
|
||||
request.getSession().removeAttribute("presupuesto_claim_id");
|
||||
return "redirect:/presupuesto/edit/" + p.getId();
|
||||
}
|
||||
}
|
||||
|
||||
if (p.getOrigen() != Presupuesto.Origen.publico) {
|
||||
redirectAttributes.addFlashAttribute("errorMessage",
|
||||
messageSource.getMessage("presupuesto.errores.presupuesto-no-existe",
|
||||
new Object[] { presupuestoId }, locale));
|
||||
return "redirect:/presupuesto";
|
||||
}
|
||||
|
||||
if (authentication != null) {
|
||||
if (authentication.getPrincipal() instanceof UserDetailsImpl udi) {
|
||||
p.setUser(userRepo.getReferenceById(udi.getId()));
|
||||
} else {
|
||||
userRepo.findByUserNameIgnoreCase(authentication.getName()).ifPresent(p::setUser);
|
||||
}
|
||||
}
|
||||
|
||||
p.setOrigen(Presupuesto.Origen.privado);
|
||||
p.setEstado(Presupuesto.Estado.borrador);
|
||||
presupuestoRepository.saveAndFlush(p);
|
||||
|
||||
request.getSession().removeAttribute("presupuesto_claim_id");
|
||||
return "redirect:/presupuesto/edit/" + p.getId();
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// MÉTODOS PARA USUARIOS AUTENTICADOS
|
||||
// =============================================
|
||||
|
||||
@ -318,6 +318,50 @@ export default class PresupuestoWizard {
|
||||
document.body.removeChild(a);
|
||||
});
|
||||
|
||||
$(document)
|
||||
.off('click.login-required', '.btn-login-required')
|
||||
.on('click.login-required', '.btn-login-required', async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
const rawId = this.opts.presupuestoId || window.PRESUPUESTO_ID || $('#presupuesto_id').val();
|
||||
const presupuestoId = rawId ? parseInt(rawId, 10) : null;
|
||||
|
||||
if (!presupuestoId || Number.isNaN(presupuestoId)) {
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: 'No se encontró el presupuesto',
|
||||
text: 'Vuelve a generar el resumen e inténtalo de nuevo.',
|
||||
buttonsStyling: false,
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-secondary me-2',
|
||||
cancelButton: 'btn btn-light'
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await $.ajax({
|
||||
url: '/presupuesto/public/prepare-claim',
|
||||
type: 'POST',
|
||||
contentType: 'application/json',
|
||||
data: JSON.stringify({ presupuestoId })
|
||||
});
|
||||
window.location.assign('/presupuesto/claim');
|
||||
} catch (err) {
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: 'No se pudo continuar',
|
||||
text: 'Inténtalo de nuevo en unos segundos.',
|
||||
buttonsStyling: false,
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-secondary me-2',
|
||||
cancelButton: 'btn btn-light'
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user