mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
157 lines
6.6 KiB
Java
157 lines
6.6 KiB
Java
package com.imprimelibros.erp.direcciones;
|
|
|
|
import java.text.Collator;
|
|
import java.util.Comparator;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.stream.Collectors;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
@Service
|
|
public class DireccionService {
|
|
|
|
protected DireccionRepository repo;
|
|
|
|
public DireccionService(DireccionRepository repo) {
|
|
this.repo = repo;
|
|
}
|
|
|
|
public Map<String, Object> getForSelect(String q1, String q2, Long userId) {
|
|
try {
|
|
|
|
// Termino de búsqueda (Select2 usa 'q' o 'term' según versión/config)
|
|
String search = Optional.ofNullable(q1).orElse(q2);
|
|
if (search != null) {
|
|
search = search.trim();
|
|
}
|
|
final String q = (search == null || search.isEmpty())
|
|
? null
|
|
: search.toLowerCase();
|
|
|
|
List<Direccion> all = userId != null ? repo.findByUserId(userId) : repo.findAll();
|
|
|
|
// Mapear a opciones id/text con i18n y filtrar por búsqueda si llega
|
|
List<Map<String, String>> options = all.stream()
|
|
.map(cc -> {
|
|
String id = cc.getId().toString();
|
|
String alias = cc.getAlias();
|
|
String direccion = cc.getDireccion();
|
|
String cp = String.valueOf(cc.getCp());
|
|
String ciudad = cc.getCiudad();
|
|
String att = cc.getAtt();
|
|
Map<String, String> m = new HashMap<>();
|
|
m.put("id", id); // lo normal en Select2: id = valor que guardarás (code3)
|
|
m.put("text", alias); // texto mostrado, i18n con fallback a keyword
|
|
m.put("cp", cp);
|
|
m.put("ciudad", ciudad);
|
|
m.put("att", att);
|
|
m.put("alias", alias);
|
|
m.put("direccion", direccion);
|
|
return m;
|
|
})
|
|
.filter(opt -> {
|
|
if (q == null || q.isEmpty())
|
|
return true;
|
|
String cp = opt.get("cp");
|
|
String ciudad = opt.get("ciudad").toLowerCase();
|
|
String att = opt.get("att").toLowerCase();
|
|
String alias = opt.get("alias").toLowerCase();
|
|
String text = opt.get("text").toLowerCase();
|
|
String direccion = opt.get("direccion").toLowerCase();
|
|
return text.contains(q) || cp.contains(q) || ciudad.contains(q) || att.contains(q)
|
|
|| alias.contains(q) || direccion.contains(q);
|
|
})
|
|
.sorted(Comparator.comparing(m -> m.get("text"), Collator.getInstance()))
|
|
.collect(Collectors.toList());
|
|
|
|
// Estructura Select2
|
|
Map<String, Object> resp = new HashMap<>();
|
|
resp.put("results", options);
|
|
return resp;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return Map.of("results", List.of());
|
|
}
|
|
}
|
|
|
|
|
|
public Map<String, Object> getForSelectFacturacion(String q1, String q2, Long userId) {
|
|
try {
|
|
|
|
// Termino de búsqueda (Select2 usa 'q' o 'term' según versión/config)
|
|
String search = Optional.ofNullable(q1).orElse(q2);
|
|
if (search != null) {
|
|
search = search.trim();
|
|
}
|
|
final String q = (search == null || search.isEmpty())
|
|
? null
|
|
: search.toLowerCase();
|
|
|
|
List<Direccion> all = repo.findByUserIdAndDireccionFacturacion(userId);
|
|
|
|
// Mapear a opciones id/text con i18n y filtrar por búsqueda si llega
|
|
List<Map<String, String>> options = all.stream()
|
|
.map(cc -> {
|
|
String id = cc.getId().toString();
|
|
String alias = cc.getAlias();
|
|
String direccion = cc.getDireccion();
|
|
String cp = String.valueOf(cc.getCp());
|
|
String ciudad = cc.getCiudad();
|
|
String att = cc.getAtt();
|
|
Map<String, String> m = new HashMap<>();
|
|
m.put("id", id); // lo normal en Select2: id = valor que guardarás (code3)
|
|
m.put("text", alias); // texto mostrado, i18n con fallback a keyword
|
|
m.put("cp", cp);
|
|
m.put("ciudad", ciudad);
|
|
m.put("att", att);
|
|
m.put("alias", alias);
|
|
m.put("direccion", direccion);
|
|
return m;
|
|
})
|
|
.filter(opt -> {
|
|
if (q == null || q.isEmpty())
|
|
return true;
|
|
String cp = opt.get("cp");
|
|
String ciudad = opt.get("ciudad").toLowerCase();
|
|
String att = opt.get("att").toLowerCase();
|
|
String alias = opt.get("alias").toLowerCase();
|
|
String text = opt.get("text").toLowerCase();
|
|
String direccion = opt.get("direccion").toLowerCase();
|
|
return text.contains(q) || cp.contains(q) || ciudad.contains(q) || att.contains(q)
|
|
|| alias.contains(q) || direccion.contains(q);
|
|
})
|
|
.sorted(Comparator.comparing(m -> m.get("text"), Collator.getInstance()))
|
|
.collect(Collectors.toList());
|
|
|
|
// Estructura Select2
|
|
Map<String, Object> resp = new HashMap<>();
|
|
resp.put("results", options);
|
|
return resp;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return Map.of("results", List.of());
|
|
}
|
|
}
|
|
|
|
public Optional<Direccion> findById(Long id) {
|
|
return repo.findById(id);
|
|
}
|
|
|
|
public Boolean checkFreeShipment(Integer cp, String paisCode3) {
|
|
if (paisCode3 != null && paisCode3.toLowerCase().equals("esp") && cp != null) {
|
|
// Excluir Canarias (35xxx y 38xxx), Baleares (07xxx), Ceuta (51xxx), Melilla
|
|
// (52xxx)
|
|
int provincia = cp / 1000;
|
|
|
|
if (provincia != 7 && provincia != 35 && provincia != 38 && provincia != 51 && provincia != 52) {
|
|
return true; // España peninsular
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|