diff --git a/src/main/java/com/imprimelibros/erp/pedidos/PedidosController.java b/src/main/java/com/imprimelibros/erp/pedidos/PedidosController.java new file mode 100644 index 0000000..c907fca --- /dev/null +++ b/src/main/java/com/imprimelibros/erp/pedidos/PedidosController.java @@ -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> getDatatable( + HttpServletRequest request, + Principal principal, + Locale locale) { + + DataTablesRequest dt = DataTablesParser.from(request); + + Boolean isAdmin = Utils.isCurrentUserAdmin(); + Long currentUserId = Utils.currentUserId(principal); + + List searchable = List.of( + "id", + "estado" + // "client" no, porque lo calculas a posteriori + ); + + // Campos ordenables + List orderable = List.of( + "id", + "client", + "created_at", + "total", + "estado"); + + Specification 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 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 = 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 "" + + messageSource.getMessage("app.view", null, locale) + ""; + }) + .where(base) + .toJson(total); + + } + +} diff --git a/src/main/resources/static/assets/js/pages/pedidos/pedidos.js b/src/main/resources/static/assets/js/pages/pedidos/pedidos.js new file mode 100644 index 0000000..731ec59 --- /dev/null +++ b/src/main/resources/static/assets/js/pages/pedidos/pedidos.js @@ -0,0 +1,54 @@ +$(() => { + + const csrfToken = document.querySelector('meta[name="_csrf"]')?.getAttribute('content'); + const csrfHeader = document.querySelector('meta[name="_csrf_header"]')?.getAttribute('content'); + if (window.$ && csrfToken && csrfHeader) { + $.ajaxSetup({ + beforeSend: function (xhr) { + xhr.setRequestHeader(csrfHeader, csrfToken); + } + }); + } + + const language = document.documentElement.lang || 'es-ES'; + + const tablePedidos = $('#table-pedidos').DataTable({ + processing: true, + serverSide: true, + orderCellsTop: true, + pageLength: 50, + lengthMenu: [10, 25, 50, 100, 500], + order: [[5, 'desc']], // Ordena por fecha por defecto + language: { url: '/assets/libs/datatables/i18n/' + language + '.json' }, + responsive: true, + dom: 'lBrtip', + buttons: { + dom: { + button: { + className: 'btn btn-sm btn-outline-primary me-1' + }, + buttons: [ + { extend: 'copy' }, + { extend: 'csv' }, + { extend: 'excel' }, + { extend: 'pdf' }, + { extend: 'print' }, + { extend: 'colvis' } + ], + } + }, + ajax: { + url: '/pedidos/datatable', + method: 'GET', + }, + order: [[0, 'desc']], + columns: [ + { data: 'id', name: 'id', orderable: true }, + { data: 'cliente', name: 'cliente', orderable: true }, + { data: 'created_at', name: 'created_at', orderable: true }, + { data: 'total', name: 'total', orderable: true }, + { data: 'actions', name: 'actions', orderable: false, searchable: false } + + ], + }); +}) \ No newline at end of file diff --git a/src/main/resources/templates/imprimelibros/pedidos/pedidos-list-cliente.html b/src/main/resources/templates/imprimelibros/pedidos/pedidos-list-cliente.html new file mode 100644 index 0000000..b1dfc1c --- /dev/null +++ b/src/main/resources/templates/imprimelibros/pedidos/pedidos-list-cliente.html @@ -0,0 +1,95 @@ + + + + + + + + + + + + + +
+
+ + +
+ +
+ +
+ + +
+ + + + + + + + + + + + + + + + + + + + + + +
Num. PedidoClienteFechaImporteEstadoAcciones
+ +
+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/imprimelibros/pedidos/pedidos-list.html b/src/main/resources/templates/imprimelibros/pedidos/pedidos-list.html new file mode 100644 index 0000000..3a2e279 --- /dev/null +++ b/src/main/resources/templates/imprimelibros/pedidos/pedidos-list.html @@ -0,0 +1,83 @@ + + + + + + + + + + + + + +
+
+ + +
+ +
+ +
+ + +
+ + + + + + + + + + + + + + + + + + + + +
Num. PedidoClienteFechaImporteAcciones
+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file