Private
Public Access
153 lines
4.1 KiB
Go
153 lines
4.1 KiB
Go
package services
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
|
|
"repuestera_back/models"
|
|
"repuestera_back/models/contabilidad"
|
|
"repuestera_back/repositorios"
|
|
)
|
|
|
|
// RegistrarAsientoVenta registra el asiento contable de una venta.
|
|
func RegistrarAsientoVenta(
|
|
tx *gorm.DB,
|
|
repo *repositorios.Repositorios,
|
|
factura *models.Factura,
|
|
subtotal, descuento, impuestos, total float64,
|
|
) error {
|
|
if factura == nil {
|
|
return gorm.ErrInvalidValue
|
|
}
|
|
|
|
base := redondearCantidad(subtotal - descuento)
|
|
if base <= 0 {
|
|
return nil
|
|
}
|
|
|
|
asiento := &contabilidad.AsientoContable{
|
|
ComprobanteTipo: "FACTURA",
|
|
ComprobanteID: factura.ID,
|
|
}
|
|
|
|
if err := agregarMovimiento(tx, repo, asiento, "Clientes", "activo", total, 0, "Venta a clientes"); err != nil {
|
|
return err
|
|
}
|
|
if err := agregarMovimiento(tx, repo, asiento, "Ventas", "resultado", 0, base, "Ingreso por ventas"); err != nil {
|
|
return err
|
|
}
|
|
if impuestos > 0 {
|
|
if err := agregarMovimiento(tx, repo, asiento, "IVA Débito Fiscal", "pasivo", 0, impuestos, "IVA sobre ventas"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return repo.Contabilidad.CrearAsientoConMovimientos(tx, asiento)
|
|
}
|
|
|
|
// RegistrarAsientoNotaCredito registra el reverso parcial o total de una venta.
|
|
// El importe de la nota incluye IVA; se separa la base imponible para que las
|
|
// cuentas de resultado y de IVA queden consistentes.
|
|
func RegistrarAsientoNotaCredito(
|
|
tx *gorm.DB,
|
|
repo *repositorios.Repositorios,
|
|
nota *models.NotaCredito,
|
|
alicuota float64,
|
|
) error {
|
|
if nota == nil {
|
|
return gorm.ErrInvalidValue
|
|
}
|
|
|
|
base, impuesto := desglosarImporteConIVA(nota.Total, alicuota)
|
|
asiento := &contabilidad.AsientoContable{
|
|
ComprobanteTipo: "NOTA_CREDITO",
|
|
ComprobanteID: nota.ID,
|
|
}
|
|
|
|
if err := agregarMovimiento(tx, repo, asiento, "Clientes", "activo", 0, nota.Total, "Nota de crédito a clientes"); err != nil {
|
|
return err
|
|
}
|
|
if base > 0 {
|
|
if err := agregarMovimiento(tx, repo, asiento, "Devoluciones sobre ventas", "resultado", base, 0, "Devolución sobre ventas"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if impuesto > 0 {
|
|
if err := agregarMovimiento(tx, repo, asiento, "IVA Débito Fiscal", "pasivo", impuesto, 0, "IVA de nota de crédito"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return repo.Contabilidad.CrearAsientoConMovimientos(tx, asiento)
|
|
}
|
|
|
|
// RegistrarAsientoNotaDebito registra el incremento de la venta por un ajuste
|
|
// posterior a la emisión de la factura.
|
|
func RegistrarAsientoNotaDebito(
|
|
tx *gorm.DB,
|
|
repo *repositorios.Repositorios,
|
|
nota *models.NotaDebito,
|
|
alicuota float64,
|
|
) error {
|
|
if nota == nil {
|
|
return gorm.ErrInvalidValue
|
|
}
|
|
|
|
base, impuesto := desglosarImporteConIVA(nota.Total, alicuota)
|
|
asiento := &contabilidad.AsientoContable{
|
|
ComprobanteTipo: "NOTA_DEBITO",
|
|
ComprobanteID: nota.ID,
|
|
}
|
|
|
|
if err := agregarMovimiento(tx, repo, asiento, "Clientes", "activo", nota.Total, 0, "Nota de débito a clientes"); err != nil {
|
|
return err
|
|
}
|
|
if base > 0 {
|
|
if err := agregarMovimiento(tx, repo, asiento, "Ventas", "resultado", 0, base, "Ingreso adicional por ventas"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if impuesto > 0 {
|
|
if err := agregarMovimiento(tx, repo, asiento, "IVA Débito Fiscal", "pasivo", 0, impuesto, "IVA de nota de débito"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return repo.Contabilidad.CrearAsientoConMovimientos(tx, asiento)
|
|
}
|
|
|
|
// desglosarImporteConIVA separa un importe final en base imponible e IVA.
|
|
func desglosarImporteConIVA(total, alicuota float64) (base, impuesto float64) {
|
|
if total <= 0 {
|
|
return 0, 0
|
|
}
|
|
if alicuota < 0 {
|
|
alicuota = 0
|
|
}
|
|
divisor := 1 + alicuota/100
|
|
base = redondearMonto(total / divisor)
|
|
impuesto = redondearMonto(total - base)
|
|
return base, impuesto
|
|
}
|
|
|
|
func agregarMovimiento(
|
|
tx *gorm.DB,
|
|
repo *repositorios.Repositorios,
|
|
asiento *contabilidad.AsientoContable,
|
|
nombre, tipo string,
|
|
debe, haber float64,
|
|
descripcion string,
|
|
) error {
|
|
cuenta := &contabilidad.CuentaContable{Nombre: nombre, Tipo: tipo}
|
|
if err := repo.Contabilidad.GuardarCuenta(tx, cuenta); err != nil {
|
|
return err
|
|
}
|
|
|
|
asiento.Movimientos = append(asiento.Movimientos, contabilidad.MovimientoContable{
|
|
CuentaID: cuenta.ID,
|
|
Debe: redondearCantidad(debe),
|
|
Haber: redondearCantidad(haber),
|
|
Descripcion: descripcion,
|
|
})
|
|
return nil
|
|
}
|