Exemplo n.º 1
0
        public void GenerarMovimiento(ListadoAcumuladorPeriodos model, TipoOperacionMaes tipo)// short multiplo)
        {
            var movs = _db.Movs.Include(i => i.MovsLin)
                       .Where(w => w.empresa == model.Empresa && w.fechaalta <= Convert.ToDateTime(model.SeccionDesde) && w.fechaalta >= Convert.ToDateTime(model.SeccionHasta) && w.fkejercicio == Convert.ToInt32(model.fkEjercicio) && w.empresa == model.Empresa);

            foreach (var m in movs.ToList())
            {
                foreach (var lineas in m.MovsLin.GroupBy(g => g.fkcuentas))
                {
                    string keyGroup = lineas.Key;
                    var    itemmaes = _db.Maes.SingleOrDefault(f => f.empresa == model.Empresa && f.fkcuentas == keyGroup && f.fkejercicio == Convert.ToInt32(model.fkEjercicio))
                                      ?? _db.Maes.Create();

                    if (string.IsNullOrWhiteSpace(itemmaes.empresa))
                    {
                        itemmaes.empresa     = model.Empresa;
                        itemmaes.fkcuentas   = keyGroup;
                        itemmaes.fkejercicio = Convert.ToInt32(model.fkEjercicio);
                    }
                    int multiplo = (tipo == TipoOperacionMaes.Alta ? 1 : -1);

                    itemmaes.debe  = (itemmaes.debe ?? 0) + (lineas.Where(l => l.esdebe == 1).Sum(l => l.importe) * (multiplo));
                    itemmaes.haber = (itemmaes.haber ?? 0) + (lineas.Where(l => l.esdebe == -1).Sum(l => l.importe) * (multiplo));
                    itemmaes.saldo = (itemmaes.debe ?? 0) - (itemmaes.haber ?? 0);

                    _db.Maes.AddOrUpdate(itemmaes);
                }
            }
            //if (model.Ejercicio)//R2
            //{
            //    var r2 = movs.Where(w => w.tipoasiento.ToUpper() == "R2");
            //}

            //if (model.Existencia) //R3
            //{
            //    var r3 = movs.Where(w => w.tipoasiento.ToUpper() == "R3");
            //}
            //if (model.Grupos) //R4
            //{
            //    var r4 = movs.Where(w => w.tipoasiento.ToUpper() == "R4");
            //}

            //if (model.CierreEjercicio)//R5
            //{
            //    var r5 = movs.Where(w => w.tipoasiento.ToUpper() == "R5");
            //}

            //if (model.IncluirAsientosSimulacion)//F2
            //{
            //    var f2 = movs.Where(w => w.tipoasiento.ToUpper() == "F2");
            //}

            //if (model.ExcluirAsientosSimulacion)//F3
            //{
            //    var f3 = movs.Where(w => w.tipoasiento.ToUpper() == "F3");
            //}
        }
Exemplo n.º 2
0
        public void GenerarMovimiento(MovsModel model, TipoOperacionMaes tipo)// short multiplo)
        // multiplo 1 para alta  -1 para baja
        {
            // nivel 0
            foreach (var item in model.Lineas.GroupBy(l => l.Fkcuentas))
            {
                string fkcuentas = item.Key;

                //Creamos la cuenta en caso de que no exista en el proceso de asignar a cartera
                if(!_db.Cuentas.Any(f => f.empresa == model.Empresa && f.id == fkcuentas) && model.Generar == GenerarMovimientoAPartirDe.AsignarCartera)
                {
                    var nuevacuenta = new CuentasModel(_context);
                    nuevacuenta.Empresa = model.Empresa;
                    nuevacuenta.Id = fkcuentas;
                    nuevacuenta.Descripcion = "**ALTA CUENTA AUTOMÁTICA**";
                    nuevacuenta.Descripcion2 = "**ALTA CUENTA AUTOMÁTICA**";
                    nuevacuenta.UsuarioId = _db.Usuarios.Where(f => f.usuario == _context.Usuario).Select(f => f.id.ToString()).SingleOrDefault() ?? "";
                    nuevacuenta.Nivel = 0;
                    var id = model.Lineas[1].Fkcuentas.ToString();

                    var newNif = new NifCifModel
                    {
                        Nif = _db.Cuentas.Where(f => f.empresa == Empresa && f.id == id).Select(f => f.nif).SingleOrDefault() ?? ""
                    };

                    nuevacuenta.Nif = newNif;
                    nuevacuenta.FkPais = _db.Cuentas.Where(f => f.empresa == Empresa && f.id == id).Select(f => f.fkPais).SingleOrDefault() ?? "";
                    nuevacuenta.Fechaalta = DateTime.Now;

                    var cuentaService = new CuentasService(_context);
                    cuentaService.create(nuevacuenta);
                }
                
                var itemmaes = _db.Maes.SingleOrDefault(f => f.empresa == model.Empresa && f.fkcuentas == fkcuentas && f.fkejercicio == model.Fkejercicio)
                              ?? _db.Maes.Create();

                if (string.IsNullOrWhiteSpace(itemmaes.empresa))
                {
                    itemmaes.empresa = model.Empresa;
                    itemmaes.fkcuentas = fkcuentas;
                    itemmaes.fkejercicio = model.Fkejercicio;
                }
                int multiplo = (tipo == TipoOperacionMaes.Alta ? 1 : -1);

                itemmaes.debe = (itemmaes.debe ?? 0) + (item.Where(l=>l.Esdebe == 1).Sum(l=> l.Importe) * (multiplo));
                itemmaes.haber = (itemmaes.haber ?? 0) + (item.Where(l => l.Esdebe == -1).Sum(l => l.Importe) * (multiplo));
                itemmaes.saldo = (itemmaes.debe ?? 0) - (itemmaes.haber ?? 0);

                _db.Maes.AddOrUpdate(itemmaes);
            }


            // nivel 0 - 4
            for (int nivel = 4; nivel > 0; nivel--)
            {
                foreach (var item in model.Lineas.GroupBy(l=> l.Fkcuentas.Substring(0, nivel)))
                {
                    string fkcuentas = item.Key;//.Substring(0, nivel);

                    var itemmaes = _db.Maes.SingleOrDefault(f => f.empresa == model.Empresa && f.fkcuentas == fkcuentas && f.fkejercicio == model.Fkejercicio)
                                   ?? _db.Maes.Create();

                    if(string.IsNullOrWhiteSpace(itemmaes.empresa))
                    {
                        itemmaes.empresa = model.Empresa;
                        itemmaes.fkcuentas = fkcuentas;
                        itemmaes.fkejercicio = model.Fkejercicio;
                    }
                    int multiplo = (tipo == TipoOperacionMaes.Alta ? 1 : -1);

                    itemmaes.debe = (itemmaes.debe ?? 0) + (item.Where(l => l.Esdebe == 1).Sum(l => l.Importe) * (multiplo));
                    itemmaes.haber = (itemmaes.haber ?? 0) + (item.Where(l => l.Esdebe == -1).Sum(l => l.Importe) * (multiplo));
                    itemmaes.saldo = (itemmaes.debe ?? 0) - (itemmaes.haber ?? 0);

                    _db.Maes.AddOrUpdate(itemmaes);
                }
            }
        }