mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 08:58:48 +00:00
listado de pedidos admin hecho
This commit is contained in:
@ -4,10 +4,10 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
@ -18,16 +18,12 @@ 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.payments.model.Payment;
|
||||
import com.imprimelibros.erp.payments.model.PaymentTransaction;
|
||||
import com.imprimelibros.erp.payments.model.PaymentTransactionStatus;
|
||||
import com.imprimelibros.erp.payments.model.PaymentTransactionType;
|
||||
import com.imprimelibros.erp.users.User;
|
||||
import com.imprimelibros.erp.users.UserDao;
|
||||
|
||||
import jakarta.persistence.criteria.Join;
|
||||
import jakarta.persistence.criteria.JoinType;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
@ -37,11 +33,14 @@ public class PedidosController {
|
||||
private final PedidoRepository repoPedido;
|
||||
private final UserDao repoUser;
|
||||
private final MessageSource messageSource;
|
||||
private final PedidoLineaRepository repoPedidoLinea;
|
||||
|
||||
public PedidosController(PedidoRepository repoPedido, UserDao repoUser, MessageSource messageSource) {
|
||||
public PedidosController(PedidoRepository repoPedido, UserDao repoUser, MessageSource messageSource,
|
||||
PedidoLineaRepository repoPedidoLinea) {
|
||||
this.repoPedido = repoPedido;
|
||||
this.repoUser = repoUser;
|
||||
this.messageSource = messageSource;
|
||||
this.repoPedidoLinea = repoPedidoLinea;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@ -65,16 +64,15 @@ public class PedidosController {
|
||||
Long currentUserId = Utils.currentUserId(principal);
|
||||
|
||||
List<String> searchable = List.of(
|
||||
"id",
|
||||
"estado"
|
||||
"id"
|
||||
// "client" no, porque lo calculas a posteriori
|
||||
);
|
||||
|
||||
// Campos ordenables
|
||||
List<String> orderable = List.of(
|
||||
"id",
|
||||
"client",
|
||||
"created_at",
|
||||
"createdBy.fullName",
|
||||
"createdAt",
|
||||
"total",
|
||||
"estado");
|
||||
|
||||
@ -83,6 +81,7 @@ public class PedidosController {
|
||||
base = base.and((root, query, cb) -> cb.equal(root.get("userId"), currentUserId));
|
||||
}
|
||||
String clientSearch = dt.getColumnSearch("cliente");
|
||||
String estadoSearch = dt.getColumnSearch("estado");
|
||||
|
||||
// 2) Si hay filtro, traducirlo a userIds y añadirlo al Specification
|
||||
if (clientSearch != null) {
|
||||
@ -92,7 +91,26 @@ public class PedidosController {
|
||||
// Ningún usuario coincide → forzamos 0 resultados
|
||||
base = base.and((root, query, cb) -> cb.disjunction());
|
||||
} else {
|
||||
base = base.and((root, query, cb) -> root.get("created_by").in(userIds));
|
||||
base = base.and((root, query, cb) -> root.get("createdBy").in(userIds));
|
||||
}
|
||||
}
|
||||
if (estadoSearch != null && !estadoSearch.isBlank()) {
|
||||
try {
|
||||
PedidoLinea.Estado estadoEnum = PedidoLinea.Estado.valueOf(estadoSearch.trim());
|
||||
|
||||
base = base.and((root, query, cb) -> {
|
||||
// Evitar duplicados de pedidos si el provider usa joins
|
||||
if (Pedido.class.equals(query.getResultType())) {
|
||||
query.distinct(true);
|
||||
}
|
||||
|
||||
Join<Pedido, PedidoLinea> lineas = root.join("lineas", JoinType.INNER);
|
||||
return cb.equal(lineas.get("estado"), estadoEnum);
|
||||
});
|
||||
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Valor de estado no válido → forzamos 0 resultados
|
||||
base = base.and((root, query, cb) -> cb.disjunction());
|
||||
}
|
||||
}
|
||||
Long total = repoPedido.count(base);
|
||||
@ -101,11 +119,10 @@ public class PedidosController {
|
||||
.of(repoPedido, Pedido.class, dt, searchable)
|
||||
.orderable(orderable)
|
||||
.add("id", Pedido::getId)
|
||||
.add("created_at", pedido -> Utils.formatDateTime(pedido.getCreatedAt(), locale))
|
||||
.add("client", pedido -> {
|
||||
.add("created_at", pedido -> Utils.formatInstant(pedido.getCreatedAt(), locale))
|
||||
.add("cliente", pedido -> {
|
||||
if (pedido.getCreatedBy() != null) {
|
||||
Optional<User> user = repoUser.findById(pedido.getCreatedBy());
|
||||
return user.map(User::getFullName).orElse("");
|
||||
return pedido.getCreatedBy().getFullName();
|
||||
}
|
||||
return "";
|
||||
})
|
||||
@ -116,9 +133,30 @@ public class PedidosController {
|
||||
return "";
|
||||
}
|
||||
})
|
||||
.add("estado", pedido -> {
|
||||
List<PedidoLinea> lineas = repoPedidoLinea.findByPedidoId(pedido.getId());
|
||||
if (lineas.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
// concatenar los estados de las líneas, ordenados por prioridad
|
||||
StringBuilder sb = new StringBuilder();
|
||||
lineas.stream()
|
||||
.map(PedidoLinea::getEstado)
|
||||
.distinct()
|
||||
.sorted(Comparator.comparingInt(PedidoLinea.Estado::getPriority))
|
||||
.forEach(estado -> {
|
||||
if (sb.length() > 0) {
|
||||
sb.append(", ");
|
||||
}
|
||||
sb.append(messageSource.getMessage(estado.getMessageKey(), null, locale));
|
||||
});
|
||||
String text = sb.toString();
|
||||
return text;
|
||||
})
|
||||
.add("actions", pedido -> {
|
||||
return "<span class=\'badge bg-success btn-view \' data-id=\'" + pedido.getId() + "\' style=\'cursor: pointer;\'>"
|
||||
+ messageSource.getMessage("app.view", null, locale) + "</span>";
|
||||
return "<span class=\'badge bg-success btn-view \' data-id=\'" + pedido.getId()
|
||||
+ "\' style=\'cursor: pointer;\'>"
|
||||
+ messageSource.getMessage("app.view", null, locale) + "</span>";
|
||||
})
|
||||
.where(base)
|
||||
.toJson(total);
|
||||
|
||||
Reference in New Issue
Block a user