feat: importar datos de BALANCE.numbers (facturas, IVA, retencion, cobros, pagos, consolidado)
This commit is contained in:
@@ -117,6 +117,7 @@ func main() {
|
||||
migrations.SeedShield()
|
||||
migrations.SeedPartnerRecursos()
|
||||
models.SeedContabilidad()
|
||||
models.SeedBalanceData()
|
||||
migrations.SeedContabilidadMenu()
|
||||
migrations.SeedWebSms()
|
||||
// Iniciar cron de vencimientos
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
@@ -276,6 +278,196 @@ func GetAllCuentasCobro(limit, offset int, search string, estado string) ([]Cuen
|
||||
return items, total, nil
|
||||
}
|
||||
|
||||
func SeedBalanceData() {
|
||||
db := app.Http.Database.DB
|
||||
|
||||
parseDate := func(s string) time.Time {
|
||||
t, _ := time.Parse("2006-01-02 15:04:05", s)
|
||||
return t
|
||||
}
|
||||
|
||||
getEntidad := func(nombre string) *uint {
|
||||
var e Entidad
|
||||
if err := db.Where("nombre = ?", nombre).First(&e).Error; err == nil {
|
||||
return &e.ID
|
||||
}
|
||||
e = Entidad{Nombre: nombre, Tipo: "cliente", Activo: true}
|
||||
db.Create(&e)
|
||||
return &e.ID
|
||||
}
|
||||
|
||||
getCuenta := func(codigo string) *uint {
|
||||
var c Cuenta
|
||||
if err := db.Where("codigo = ?", codigo).First(&c).Error; err == nil {
|
||||
return &c.ID
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ─── FACTURAS → Transacciones (ingresos) ────────────────────────────────
|
||||
facturas := []struct{ factura float64; valor float64; fecha string }{
|
||||
{54, 450000, "2026-01-01 00:00:00"},
|
||||
{55, 9000000, "2026-01-01 00:00:00"},
|
||||
{56, 119000, "2026-01-01 00:00:00"},
|
||||
{57, 330000, "2026-01-01 00:00:00"},
|
||||
{58, 1000000, "2026-01-01 00:00:00"},
|
||||
{59, 1000000, "2026-01-01 00:00:00"},
|
||||
{60, 900000, "2026-01-01 00:00:00"},
|
||||
{0, 0, "2026-01-01 00:00:00"}, // ajuste -130000
|
||||
{61, 580000, "2026-02-01 00:00:00"},
|
||||
{62, 330000, "2026-02-01 00:00:00"},
|
||||
{63, 1785000, "2026-02-01 00:00:00"},
|
||||
{64, 120000, "2026-02-01 00:00:00"},
|
||||
{65, 350000, "2026-02-01 00:00:00"},
|
||||
{66, 550000, "2026-02-01 00:00:00"},
|
||||
{67, 1300000, "2026-02-01 00:00:00"},
|
||||
{68, 270000, "2026-02-01 00:00:00"},
|
||||
{69, 32000, "2026-02-01 00:00:00"},
|
||||
{70, 3153500, "2026-02-01 00:00:00"},
|
||||
{71, 349000, "2026-03-01 00:00:00"},
|
||||
{72, 0, "2026-03-01 00:00:00"},
|
||||
{73, 1380000, "2026-03-01 00:00:00"},
|
||||
{74, 380000, "2026-03-01 00:00:00"},
|
||||
{75, 2650000, "2026-04-01 00:00:00"},
|
||||
{76, 390000, "2026-04-01 00:00:00"},
|
||||
{77, 2200000, "2026-04-01 00:00:00"},
|
||||
{78, 4400000, "2026-04-01 00:00:00"},
|
||||
{79, 142000, "2026-04-01 00:00:00"},
|
||||
}
|
||||
cuentaIng := getCuenta("ING-FAC")
|
||||
docuxer := getEntidad("DOCUXER")
|
||||
giaf := getEntidad("GIAF SAS")
|
||||
|
||||
for _, f := range facturas {
|
||||
if f.factura == 0 && f.valor == 0 {
|
||||
// ajuste negativo: nota crédito
|
||||
var existing Transaccion
|
||||
if db.Where("descripcion = ?", "Ajuste/NC ene 2026").First(&existing).Error != nil {
|
||||
db.Create(&Transaccion{
|
||||
Fecha: parseDate("2026-01-01 00:00:00"),
|
||||
Tipo: "egreso", Descripcion: "Ajuste/NC ene 2026",
|
||||
Valor: 130000, CuentaID: cuentaIng, Estado: "registrada",
|
||||
})
|
||||
}
|
||||
continue
|
||||
}
|
||||
desc := fmt.Sprintf("Factura #%.0f", f.factura)
|
||||
var existing Transaccion
|
||||
if db.Where("descripcion = ? AND fecha = ?", desc, parseDate(f.fecha)).First(&existing).Error != nil {
|
||||
eid := docuxer
|
||||
if f.factura == 56 {
|
||||
eid = giaf
|
||||
}
|
||||
db.Create(&Transaccion{
|
||||
Fecha: parseDate(f.fecha), Tipo: "ingreso",
|
||||
Descripcion: desc, Valor: f.valor,
|
||||
CuentaID: cuentaIng, EntidadID: eid, Estado: "registrada",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ─── IVA → Transacciones (egresos) ──────────────────────────────────────
|
||||
ivas := []struct{ valor float64; entidad string; desc string }{
|
||||
{285000, "DOCUXER", "IVA Fact #63"},
|
||||
{503500, "DOCUXER", "IVA Fact #70"},
|
||||
{19000, "GIAF SAS", "IVA Fact #56"},
|
||||
{503500, "DOCUXER", "IVA Fact #75"},
|
||||
{418000, "DOCUXER", "IVA Fact #77"},
|
||||
}
|
||||
cuentaImp := getCuenta("EGR-IMPU")
|
||||
for _, iv := range ivas {
|
||||
var existing Transaccion
|
||||
if db.Where("descripcion = ?", iv.desc).First(&existing).Error != nil {
|
||||
eid := getEntidad(iv.entidad)
|
||||
db.Create(&Transaccion{
|
||||
Fecha: parseDate("2026-01-01 00:00:00"),
|
||||
Tipo: "egreso", Descripcion: iv.desc,
|
||||
Valor: iv.valor, CuentaID: cuentaImp,
|
||||
EntidadID: eid, Estado: "registrada",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ─── RETENCION → Transacciones (egresos) ────────────────────────────────
|
||||
rets := []struct{ valor float64; fecha string; estado string }{
|
||||
{195000, "2026-03-01 00:00:00", "pagado"},
|
||||
{257000, "2026-02-01 00:00:00", "pagado"},
|
||||
{431000, "2026-01-01 00:00:00", "pendiente"},
|
||||
}
|
||||
for _, r := range rets {
|
||||
desc := fmt.Sprintf("Retención %s", r.fecha[:7])
|
||||
var existing Transaccion
|
||||
if db.Where("descripcion = ?", desc).First(&existing).Error != nil {
|
||||
db.Create(&Transaccion{
|
||||
Fecha: parseDate(r.fecha),
|
||||
Tipo: "egreso", Descripcion: desc,
|
||||
Valor: r.valor, CuentaID: cuentaImp,
|
||||
Estado: "registrada",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ─── CUENTAS DE COBRO ───────────────────────────────────────────────────
|
||||
cobros := []struct{ entidad string; valor float64; fecha string }{
|
||||
{"NATALIA", 2000000, "2026-01-01 00:00:00"},
|
||||
{"FELIPE", 2000000, "2026-01-01 00:00:00"},
|
||||
{"CONTADORA", 1780000, "2026-01-01 00:00:00"},
|
||||
{"FELIPE", 2300000, "2026-02-01 00:00:00"},
|
||||
{"NATALIA", 2000000, "2026-02-01 00:00:00"},
|
||||
{"FELIPE", 2300000, "2026-03-01 00:00:00"},
|
||||
{"NATALIA", 2000000, "2026-03-01 00:00:00"},
|
||||
{"FELIPE", 520000, "2026-04-01 00:00:00"},
|
||||
{"ANDREMER", 80000, "2026-04-01 00:00:00"},
|
||||
{"NATALIA", 2000000, "2026-04-01 00:00:00"},
|
||||
}
|
||||
for _, cb := range cobros {
|
||||
eid := getEntidad(cb.entidad)
|
||||
desc := fmt.Sprintf("Cobro %s %s", cb.entidad, cb.fecha[:7])
|
||||
var existing CuentaCobro
|
||||
if db.Where("descripcion = ?", desc).First(&existing).Error != nil {
|
||||
df := parseDate(cb.fecha)
|
||||
db.Create(&CuentaCobro{
|
||||
EntidadID: *eid, Fecha: df,
|
||||
Descripcion: desc, Valor: cb.valor,
|
||||
Estado: "pendiente",
|
||||
FechaVencimiento: &df,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ─── FAC POR PAGAR ──────────────────────────────────────────────────────
|
||||
var existingCp CuentaPagar
|
||||
if db.Where("descripcion = ?", "TECZONE ene 2026").First(&existingCp).Error != nil {
|
||||
eid := getEntidad("TECZONE")
|
||||
f := parseDate("2026-01-01 00:00:00")
|
||||
db.Create(&CuentaPagar{
|
||||
EntidadID: *eid, Fecha: f,
|
||||
Descripcion: "TECZONE ene 2026",
|
||||
Valor: 2687849, Estado: "pendiente",
|
||||
})
|
||||
}
|
||||
|
||||
// ─── CONSOLIDADO POR MES ───────────────────────────────────────────────
|
||||
consols := []struct{ mes int; ing, egre, resul float64 }{
|
||||
{1, 12669000, 8467849, 4201151},
|
||||
{2, 8470500, 4300000, 4170500},
|
||||
{3, 2109000, 4300000, -2191000},
|
||||
{4, 9782000, 2600000, 7182000},
|
||||
}
|
||||
for _, cs := range consols {
|
||||
var existing ConsolidadoMensual
|
||||
if db.Where("anio = 2026 AND mes = ?", cs.mes).First(&existing).Error != nil {
|
||||
db.Create(&ConsolidadoMensual{
|
||||
Anio: 2026, Mes: cs.mes,
|
||||
TotalIngresos: cs.ing, TotalEgresos: cs.egre,
|
||||
Resultado: cs.resul,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("[SEED] Balance data imported from BALANCE.numbers")
|
||||
}
|
||||
|
||||
func CreateCuentaCobro(cc *CuentaCobro) error {
|
||||
return app.Http.Database.DB.Create(cc).Error
|
||||
}
|
||||
@@ -494,6 +686,8 @@ func SeedContabilidad() {
|
||||
{Nombre: "DOCUXER", Tipo: "cliente"},
|
||||
{Nombre: "FELIPE", Tipo: "cliente"},
|
||||
{Nombre: "NATALIA", Tipo: "cliente"},
|
||||
{Nombre: "CONTADORA", Tipo: "cliente"},
|
||||
{Nombre: "ANDREMER", Tipo: "cliente"},
|
||||
}
|
||||
for _, e := range entidades {
|
||||
var existing Entidad
|
||||
|
||||
Reference in New Issue
Block a user