Private
Public Access
1 parent
bc0db6dbee
commit
fcf16df5d3
21 files changed
+728
-3
No files matched your search
@@ -32,6 +32,7 @@ var permisosSemilla = []permisoSemilla{
|
||||
{14, "Generar Reporte de Stock", "stock/reportes", "Generar y consultar reportes de stock."},
|
||||
{15, "Gestión de Ventas y Facturación", "facturas", "Consultar y operar la facturación y ventas de la empresa."},
|
||||
{16, "Gestión de Descuentos", "descuentos", "Crear, editar y eliminar descuentos."},
|
||||
{17, "Gestión de Formas de Pago", "formas-pago", "Crear, editar y eliminar formas de pago."},
|
||||
}
|
||||
|
||||
// InitPermisos crea el catálogo global de permisos y garantiza que cada
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package formapago
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"repuestera_back/models"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ActualizarFormaDePago actualiza una forma de pago existente.
|
||||
func ActualizarFormaDePago(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalido"})
|
||||
return
|
||||
}
|
||||
if id < 1 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID no puede ser menor que 1"})
|
||||
return
|
||||
}
|
||||
|
||||
// Obtener la forma de pago actual
|
||||
fp, err := r.ObtenerRepositorios(c).FormaDePago.ObtenerPorID(uint(id))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Forma de pago no encontrada"})
|
||||
return
|
||||
}
|
||||
|
||||
var input models.FormaDePago
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if input.Descripcion == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "La descripción no puede estar vacía"})
|
||||
return
|
||||
}
|
||||
|
||||
// Verificar que no exista otra forma de pago con la misma descripción
|
||||
if existing, _ := r.ObtenerRepositorios(c).FormaDePago.ObtenerPorDescripcion(input.Descripcion); existing != nil && existing.ID != fp.ID {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "ya existe una forma de pago con esa descripción"})
|
||||
return
|
||||
}
|
||||
|
||||
uid := c.GetUint("userID")
|
||||
if err := r.ObtenerRepositorios(c).FormaDePago.Actualizar(fp, input, uid); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, fp)
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package formapago
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func AltaFormaDePago(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalido"})
|
||||
return
|
||||
}
|
||||
if id < 1 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID no puede ser menor que 1"})
|
||||
return
|
||||
}
|
||||
|
||||
uid := c.GetUint("userID")
|
||||
fp, err := r.ObtenerRepositorios(c).FormaDePago.ObtenerPorID(uint(id))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Forma de pago no encontrada"})
|
||||
return
|
||||
}
|
||||
|
||||
if !fp.DeletedAt.Valid {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Forma de pago no está eliminada"})
|
||||
return
|
||||
}
|
||||
|
||||
fp.DeletedAt = gorm.DeletedAt{}
|
||||
if err := r.ObtenerRepositorios(c).FormaDePago.Alta(fp, *fp, uid); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package formapago
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"repuestera_back/models"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func CrearFormaDePago(c *gin.Context) {
|
||||
var fp models.FormaDePago
|
||||
if err := c.ShouldBindJSON(&fp); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if fp.Descripcion == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "La descripción es obligatoria"})
|
||||
return
|
||||
}
|
||||
|
||||
repo := r.ObtenerRepositorios(c).FormaDePago
|
||||
if _, err := repo.ObtenerPorDescripcion(fp.Descripcion); err == nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "ya existe una forma de pago con esa descripción"})
|
||||
return
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := repo.Crear(fp, c.GetUint("userID")); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusCreated)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package formapago
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func EliminarFormaDePago(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalido"})
|
||||
return
|
||||
}
|
||||
if id < 1 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID no puede ser menor que 1"})
|
||||
return
|
||||
}
|
||||
|
||||
uid := c.GetUint("userID")
|
||||
err = r.ObtenerRepositorios(c).FormaDePago.Baja(uint(id), uid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package formapago
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ObtenerFormaDePago(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalido"})
|
||||
return
|
||||
}
|
||||
if id < 1 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID no puede ser menor que 1"})
|
||||
return
|
||||
}
|
||||
|
||||
fp, err := r.ObtenerRepositorios(c).FormaDePago.ObtenerPorID(uint(id))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Forma de pago no encontrada"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, fp)
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
type RepositorioFormaDePago interface {
|
||||
IRepositorio[models.FormaDePago]
|
||||
ObtenerPorDescripcion(descripcion string) (*models.FormaDePago, error)
|
||||
}
|
||||
|
||||
type repositorioFormaDePago struct {
|
||||
@@ -23,3 +24,13 @@ func NuevoRepositorioFormaDePago(db *gorm.DB, empresaID uint) RepositorioFormaDe
|
||||
empresaID: empresaID,
|
||||
}
|
||||
}
|
||||
|
||||
// ObtenerPorDescripcion busca una forma de pago por descripción dentro de la
|
||||
// empresa, incluyendo las dadas de baja para validar duplicados al reactivar.
|
||||
func (r *repositorioFormaDePago) ObtenerPorDescripcion(descripcion string) (*models.FormaDePago, error) {
|
||||
var fp models.FormaDePago
|
||||
if err := r.db.Unscoped().Where("descripcion = ?", descripcion).First(&fp).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &fp, nil
|
||||
}
|
||||
@@ -8,9 +8,21 @@ import (
|
||||
)
|
||||
|
||||
func FormaDePagoRoute(r *gin.Engine) {
|
||||
grupo := r.Group("/formas-pago")
|
||||
grupo.Use(middleware.AuthMiddleware(15))
|
||||
// Lectura: facturación (15) necesita las formas de pago activas, y gestión
|
||||
// de formas de pago (17) necesita el listado completo.
|
||||
lectura := r.Group("/formas-pago")
|
||||
lectura.Use(middleware.AuthMiddleware(15, 17))
|
||||
{
|
||||
grupo.GET("/", formapago.ObtenerFormasPago)
|
||||
lectura.GET("/", formapago.ObtenerFormasPago)
|
||||
lectura.GET("/:id", formapago.ObtenerFormaDePago)
|
||||
}
|
||||
|
||||
gestion := r.Group("/formas-pago")
|
||||
gestion.Use(middleware.AuthMiddleware(17))
|
||||
{
|
||||
gestion.POST("/", formapago.CrearFormaDePago)
|
||||
gestion.PUT("/:id", formapago.ActualizarFormaDePago)
|
||||
gestion.DELETE("/:id", formapago.EliminarFormaDePago)
|
||||
gestion.PATCH("/:id/alta", formapago.AltaFormaDePago)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user