hecha funcionalidad de reportes y exportacion de reportes

Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
fede committed 2026-11-28 00:14:15 -03:00
1 parent 61baaf14f8
commit 298f3c699c
7 files changed
+338 -7

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
package reporte
import (
"encoding/csv"
"fmt"
"net/http"
dto "repuestera_back/dto/Stock"
"repuestera_back/models"
)
// ExportadorCSV exporta el reporte en formato CSV.
type ExportadorCSV struct{}
func (e *ExportadorCSV) Exportar(w http.ResponseWriter, movimientos []models.MovimientoStock, resumen []dto.ResumenMovimientoResponse, nombreArchivo string) error {
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", nombreArchivo))
w.Header().Set("Cache-Control", "no-cache")
w.Write([]byte{0xEF, 0xBB, 0xBF})
writer := csv.NewWriter(w)
if err := writer.Write([]string{
"ID", "Fecha", "Tipo", "Repuesto", "RepuestoID", "AlmacenID",
"Cantidad", "SaldoAnterior", "SaldoPosterior", "ReferenciaTipo",
"ReferenciaID", "UsuarioID", "Observacion",
}); err != nil {
return err
}
for _, m := range movimientos {
nombreRepuesto := ""
if m.Repuesto.Nombre != "" {
nombreRepuesto = m.Repuesto.Nombre
}
if err := writer.Write([]string{
fmt.Sprintf("%d", m.ID),
m.Fecha.Format("2006-01-02 15:04:05"),
string(m.Tipo),
nombreRepuesto,
fmt.Sprintf("%d", m.RepuestoID),
fmt.Sprintf("%d", m.AlmacenID),
fmt.Sprintf("%g", m.Cantidad),
fmt.Sprintf("%g", m.SaldoAnterior),
fmt.Sprintf("%g", m.SaldoPosterior),
m.ReferenciaTipo,
fmt.Sprintf("%d", m.ReferenciaID),
fmt.Sprintf("%d", m.UsuarioID),
m.Observacion,
}); err != nil {
return err
}
}
writer.Flush()
return writer.Error()
}
+75
View File
@@ -0,0 +1,75 @@
package reporte
import (
"fmt"
"net/http"
"github.com/xuri/excelize/v2"
dto "repuestera_back/dto/Stock"
"repuestera_back/models"
)
// ExportadorXLSX exporta el reporte en formato XLSX.
type ExportadorXLSX struct{}
func (e *ExportadorXLSX) Exportar(w http.ResponseWriter, movimientos []models.MovimientoStock, resumen []dto.ResumenMovimientoResponse, nombreArchivo string) error {
archivo := excelize.NewFile()
hoja := "Movimientos"
archivo.SetSheetName("Sheet1", hoja)
encabezados := []string{
"ID", "Fecha", "Tipo", "Repuesto", "RepuestoID", "AlmacenID",
"Cantidad", "SaldoAnterior", "SaldoPosterior", "ReferenciaTipo",
"ReferenciaID", "UsuarioID", "Observacion",
}
for i, h := range encabezados {
celda, _ := excelize.CoordinatesToCellName(i+1, 1)
archivo.SetCellValue(hoja, celda, h)
}
for idx, m := range movimientos {
fila := idx + 2
valores := []interface{}{
m.ID,
m.Fecha.Format("2006-01-02 15:04:05"),
string(m.Tipo),
m.Repuesto.Nombre,
m.RepuestoID,
m.AlmacenID,
m.Cantidad,
m.SaldoAnterior,
m.SaldoPosterior,
m.ReferenciaTipo,
m.ReferenciaID,
m.UsuarioID,
m.Observacion,
}
for i, v := range valores {
celda, _ := excelize.CoordinatesToCellName(i+1, fila)
archivo.SetCellValue(hoja, celda, v)
}
}
hojaResumen := "Resumen"
archivo.NewSheet(hojaResumen)
archivo.SetCellValue(hojaResumen, "A1", "Tipo")
archivo.SetCellValue(hojaResumen, "B1", "Cantidad")
for idx, r := range resumen {
fila := idx + 2
archivo.SetCellValue(hojaResumen, fmt.Sprintf("A%d", fila), string(r.Tipo))
archivo.SetCellValue(hojaResumen, fmt.Sprintf("B%d", fila), r.Cantidad)
}
buffer, err := archivo.WriteToBuffer()
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", nombreArchivo))
w.Header().Set("Cache-Control", "no-cache")
w.WriteHeader(http.StatusOK)
_, err = w.Write(buffer.Bytes())
return err
}
@@ -0,0 +1,27 @@
package reporte
import (
"fmt"
"net/http"
"strings"
dto "repuestera_back/dto/Stock"
"repuestera_back/models"
)
// ExportadorReporte define la estrategia de exportación de un reporte.
type ExportadorReporte interface {
Exportar(w http.ResponseWriter, movimientos []models.MovimientoStock, resumen []dto.ResumenMovimientoResponse, nombreArchivo string) error
}
// NuevoExportador devuelve la estrategia correspondiente al formato indicado.
func NuevoExportador(formato string) (ExportadorReporte, error) {
switch strings.ToLower(strings.TrimSpace(formato)) {
case "csv":
return &ExportadorCSV{}, nil
case "xlsx":
return &ExportadorXLSX{}, nil
default:
return nil, fmt.Errorf("formato inválido: use csv o xlsx")
}
}