mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 08:58:48 +00:00
trabajando en la vista de pedidos
This commit is contained in:
@ -0,0 +1,128 @@
|
||||
package com.imprimelibros.erp.pedidos;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.security.Principal;
|
||||
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;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import com.imprimelibros.erp.common.Utils;
|
||||
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.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/pedidos")
|
||||
public class PedidosController {
|
||||
|
||||
private final PedidoRepository repoPedido;
|
||||
private final UserDao repoUser;
|
||||
private final MessageSource messageSource;
|
||||
|
||||
public PedidosController(PedidoRepository repoPedido, UserDao repoUser, MessageSource messageSource) {
|
||||
this.repoPedido = repoPedido;
|
||||
this.repoUser = repoUser;
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String listarPedidos() {
|
||||
if (Utils.isCurrentUserAdmin()) {
|
||||
return "imprimelibros/pedidos/pedidos-list";
|
||||
}
|
||||
return "imprimelibros/pedidos/pedidos-list-cliente";
|
||||
}
|
||||
|
||||
@GetMapping(value = "datatable", produces = "application/json")
|
||||
@ResponseBody
|
||||
public DataTablesResponse<Map<String, Object>> getDatatable(
|
||||
HttpServletRequest request,
|
||||
Principal principal,
|
||||
Locale locale) {
|
||||
|
||||
DataTablesRequest dt = DataTablesParser.from(request);
|
||||
|
||||
Boolean isAdmin = Utils.isCurrentUserAdmin();
|
||||
Long currentUserId = Utils.currentUserId(principal);
|
||||
|
||||
List<String> searchable = List.of(
|
||||
"id",
|
||||
"estado"
|
||||
// "client" no, porque lo calculas a posteriori
|
||||
);
|
||||
|
||||
// Campos ordenables
|
||||
List<String> orderable = List.of(
|
||||
"id",
|
||||
"client",
|
||||
"created_at",
|
||||
"total",
|
||||
"estado");
|
||||
|
||||
Specification<Pedido> base = (root, query, cb) -> cb.conjunction();
|
||||
if (!isAdmin) {
|
||||
base = base.and((root, query, cb) -> cb.equal(root.get("userId"), currentUserId));
|
||||
}
|
||||
String clientSearch = dt.getColumnSearch("cliente");
|
||||
|
||||
// 2) Si hay filtro, traducirlo a userIds y añadirlo al Specification
|
||||
if (clientSearch != null) {
|
||||
List<Long> userIds = repoUser.findIdsByFullNameLike(clientSearch.trim());
|
||||
|
||||
if (userIds.isEmpty()) {
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
Long total = repoPedido.count(base);
|
||||
|
||||
return DataTable
|
||||
.of(repoPedido, Pedido.class, dt, searchable)
|
||||
.orderable(orderable)
|
||||
.add("id", Pedido::getId)
|
||||
.add("created_at", pedido -> Utils.formatDateTime(pedido.getCreatedAt(), locale))
|
||||
.add("client", pedido -> {
|
||||
if (pedido.getCreatedBy() != null) {
|
||||
Optional<User> user = repoUser.findById(pedido.getCreatedBy());
|
||||
return user.map(User::getFullName).orElse("");
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.add("total", pedido -> {
|
||||
if (pedido.getTotal() != null) {
|
||||
return Utils.formatCurrency(pedido.getTotal(), locale);
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
})
|
||||
.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>";
|
||||
})
|
||||
.where(base)
|
||||
.toJson(total);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user