I see myself stuck in defining a table for the Chart of Accounts, these can be control or auxiliary , that is, if it is control , it is a parent account, which means that it will have sub-accounts that would be child accounts and the auxiliary type accounts will not have daughter accounts.
For example:
1 – Activos.//control
1.0 – Activos Corrientes.//control
1.0.1 – Efectivos en Caja y Bancos. //control
1.0.1.1 – Caja General.//auxiliar
That's an example of the chart of accounts hierarchy, the point is that I would like to have access to the only parent account of an account and the list of child accounts of an account. But I don't know how to structure the model.
I have tried this way:
Model:
public class CatalogoCuenta
{
[Key]
public int NumeroCuenta { get; set; }
[Required]
public string NombreCuenta { get; set; }
[Required]
public FuenteCuenta Fuente { get; set; }
[Required]
public GrupoCuenta Grupo { get; set; }
[Required]
public ISR Retencion { get; set; }
[Required]
public TipoCuenta TipoCuenta { get; set; }
[Required]
public TipoIngreso TipoIngreso { get; set; }
public int? CuentaPadreId { get; set; }
public List<RelacionCuentaHijo> CuentasHijas { get; set; }
public RelacionCuentaPadre CuentaPadre { get; set; }
}
public class RelacionCuentaHijo
{
[Key]
public int RelacionId { get; set; }
public int CuentaHijoId { get; set; }
public CatalogoCuenta CuentaHijo { get; set; }
}
public class RelacionCuentaPadre
{
[Key]
public int RelacionId { get; set; }
public int CuentaPadreId { get; set; }
public CatalogoCuenta CuentaPadre { get; set; }
}
But I have realized that this model does not do anything, rather it would need a model with a double relationship, that is, with the Id of the account and the Id of the account with which it is related. For example:
public class RelacionCuentaHijo
{
[Key]
public int RelacionId { get; set; }
public int CuentaHijoId { get; set; }
public CatalogoCuenta CuentaHijo { get; set; }
public int CuentaId { get; set; }
public CatalogoCuenta Cuenta { get; set; }
}
But I don't know how to make this work and CuentaHijoId
save the Id of the child account and CuentaId
the Id of the account that has it as parent.
I also have a doubt if I should create those two tables to make that relationship or with one is enough. I use api fluent to configure the relations presented in the model.
I have managed to obtain a result that satisfies my need.
Model
This way I can have an external table that stores all the child accounts and the parent account.
To achieve this I use the following code with fluent api:
Then it is possible to get the accounts with their parent account just by doing:
And this way I get the accounts with a list of the child accounts: