public async Task <ActionResult> AddNumbers(string recharge_id, string agent_id, string data)
        {
            Guid agentId    = Guid.Parse(agent_id);
            Guid rechargeId = Guid.Parse(recharge_id);

            RechargeAgent rechargeAgent = await applicationDbContext.RechargeAgents.FirstOrDefaultAsync(x => x.Agent_Id == agentId && x.Recharge_Id == rechargeId);

            string[] numbers = data.Trim().Split('\n');

            string sreturn = "";

            Number number_empty = await applicationDbContext.Numbers.FirstOrDefaultAsync(x => x.RechargeAgent_Id == rechargeAgent.Id && !x.Confirmation && string.IsNullOrEmpty(x.Value));

            foreach (var temp in numbers)
            {
                string number = temp.Replace("+53", "").Trim();
                if (number.Length == 8 && IsDigitsOnly(number))
                {
                    Number add = new Number()
                    {
                        Id               = Guid.NewGuid(),
                        CreatedDate      = DateTime.Now,
                        UpdatedDate      = DateTime.Now,
                        User_Id          = User.Identity.GetUserId(),
                        RechargeAgent_Id = rechargeAgent.Id,
                        Value            = number,
                        Consecutive      = number_empty.Consecutive++
                    };
                    applicationDbContext.Numbers.Add(add);
                }
                else
                {
                    sreturn += $"{number}\n";
                }
            }

            number_empty.CreatedDate = DateTime.Now;
            number_empty.UpdatedDate = DateTime.Now;

            applicationDbContext.Entry(number_empty).State = System.Data.Entity.EntityState.Modified;
            await applicationDbContext.SaveChangesAsync();

            return(Json(sreturn.Length > 2 ? sreturn.Substring(0, sreturn.Length - 1) : ""));
        }
        public async Task <ActionResult> ImportAgents(string DT_RowId)
        {
            Guid recharge_id = Guid.Parse(DT_RowId);

            Recharge old_recharge = await applicationDbContext.Recharges.Include("RechargeAgents").OrderByDescending(x => x.DateEnd).FirstOrDefaultAsync(x => x.Id != recharge_id);

            foreach (var agent in old_recharge.RechargeAgents)
            {
                RechargeAgent rechargeAgent = new RechargeAgent()
                {
                    Id          = Guid.NewGuid(),
                    Agent_Id    = agent.Agent_Id,
                    Cost        = agent.Cost,
                    Recharge_Id = recharge_id
                };
                applicationDbContext.RechargeAgents.Add(rechargeAgent);
            }

            await applicationDbContext.SaveChangesAsync();

            return(RedirectToAction("View", new { id = DT_RowId }));
        }
        public async Task <ActionResult> AddAgent(RechargeViewModel model)
        {
            Guid agent_id    = Guid.Parse(model.Agent_Id);
            Guid recharge_id = Guid.Parse(model.DT_RowId);

            RechargeAgent rechargeAgent = await applicationDbContext.RechargeAgents.FirstOrDefaultAsync(x => x.Agent_Id == agent_id && x.Recharge_Id == recharge_id);

            if (rechargeAgent == null)
            {
                rechargeAgent = new RechargeAgent()
                {
                    Id          = Guid.NewGuid(),
                    Recharge_Id = recharge_id,
                    Agent_Id    = agent_id,
                    Cost        = decimal.Parse(model.Agent_Cost.Replace(".", ","))
                };
                applicationDbContext.RechargeAgents.Add(rechargeAgent);
                await applicationDbContext.SaveChangesAsync();
            }

            return(RedirectToAction("View", new { id = model.DT_RowId }));
        }
        public async Task <ActionResult> Search(TableFilterViewModel filter)
        {
            Guid agent_id          = Guid.Parse(filter.type);
            Guid business_working  = Guid.Parse(Session["BusinessWorking"].ToString());
            long totalRowsFiltered = 0;
            long totalRows         = await applicationDbContext.Numbers.Include("RechargeAgent").Include("RechargeAgent.Recharge").CountAsync(x => x.RechargeAgent.Agent_Id == agent_id && x.RechargeAgent.Recharge.Activated);

            List <Number> model;

            var entity = applicationDbContext.Numbers.Include("RechargeAgent").Include("RechargeAgent.Agent").Include("RechargeAgent.Recharge").Include("User").Where(x => x.RechargeAgent.Agent_Id == agent_id && x.RechargeAgent.Recharge.Activated);

            IOrderedQueryable <Number> sort = null;

            if (filter.order[0].column == 0)
            {
                if (filter.order[0].dir.Equals("asc"))
                {
                    sort = entity.OrderBy(x => x.Consecutive);
                }
                else
                {
                    sort = entity.OrderByDescending(x => x.Consecutive);
                }
            }
            else if (filter.order[0].column == 1)
            {
                if (filter.order[0].dir.Equals("asc"))
                {
                    sort = entity.OrderBy(x => x.Value);
                }
                else
                {
                    sort = entity.OrderByDescending(x => x.Value);
                }
            }
            else if (filter.order[0].column == 2)
            {
                if (filter.order[0].dir.Equals("asc"))
                {
                    sort = entity.OrderBy(x => x.Confirmation);
                }
                else
                {
                    sort = entity.OrderByDescending(x => x.Confirmation);
                }
            }
            else if (filter.order[0].column == 4)
            {
                if (filter.order[0].dir.Equals("asc"))
                {
                    sort = entity.OrderBy(x => x.CreatedDate);
                }
                else
                {
                    sort = entity.OrderByDescending(x => x.CreatedDate);
                }
            }
            else if (filter.order[0].column == 5)
            {
                if (filter.order[0].dir.Equals("asc"))
                {
                    sort = entity.OrderBy(x => x.UpdatedDate);
                }
                else
                {
                    sort = entity.OrderByDescending(x => x.UpdatedDate);
                }
            }

            if (string.IsNullOrEmpty(filter.search.value))
            {
                totalRowsFiltered = totalRows;
                model             = await sort.Skip(filter.start)
                                    .Take(filter.length)
                                    .ToListAsync();
            }
            else
            {
                totalRowsFiltered = await
                                    applicationDbContext.Numbers.Include("RechargeAgent").Include("RechargeAgent.Agent").Include("RechargeAgent.Recharge").Include("User").CountAsync(x => x.RechargeAgent.Agent_Id == agent_id && x.RechargeAgent.Recharge.Activated &&
                                                                                                                                                                                      (x.Value.ToString().Contains(filter.search.value) ||
                                                                                                                                                                                       x.Consecutive.ToString().Contains(filter.search.value) ||
                                                                                                                                                                                       x.Confirmation.ToString().Contains(filter.search.value) ||
                                                                                                                                                                                       x.User.UserName.ToString().Contains(filter.search.value) ||
                                                                                                                                                                                       x.CreatedDate.ToString().Contains(filter.search.value) ||
                                                                                                                                                                                       x.UpdatedDate.ToString().Contains(filter.search.value)));

                model = await
                        sort.Where(x => x.RechargeAgent.Agent_Id == agent_id &&
                                   (x.Value.ToString().Contains(filter.search.value) ||
                                    x.Consecutive.ToString().Contains(filter.search.value) ||
                                    x.Confirmation.ToString().Contains(filter.search.value) ||
                                    x.User.UserName.ToString().Contains(filter.search.value) ||
                                    x.CreatedDate.ToString().Contains(filter.search.value) ||
                                    x.UpdatedDate.ToString().Contains(filter.search.value)))
                        .Skip(filter.start)
                        .Take(filter.length)
                        .ToListAsync();
            }

            List <NumberViewModel> result = new List <NumberViewModel>();

            foreach (var number in model)
            {
                result.Add(new NumberViewModel()
                {
                    DT_RowId     = number.Id.ToString(),
                    consecutive  = number.Consecutive.ToString("###000"),
                    number       = number.Value,
                    created_date = number.CreatedDate.ToString("yyyy-MM-dd hh:mm tt"),
                    updated_date = number.UpdatedDate.ToString("yyyy-MM-dd hh:mm tt"),
                    confirmation = number.Confirmation.ToString(),
                    user         = number.User?.UserName
                });
            }

            if (model.Count == 0)
            {
                RechargeAgent rechargeAgent = await applicationDbContext.RechargeAgents.FirstOrDefaultAsync(x => x.Agent_Id == agent_id && x.Recharge.Activated);

                Number number = new Number()
                {
                    Id            = Guid.NewGuid(),
                    RechargeAgent = rechargeAgent,
                    CreatedDate   = DateTime.Now,
                    UpdatedDate   = DateTime.Now,
                    Consecutive   = 1
                };
                applicationDbContext.Numbers.Add(number);
                await applicationDbContext.SaveChangesAsync();

                result.Add(new NumberViewModel()
                {
                    DT_RowId     = number.Id.ToString(),
                    consecutive  = number.Consecutive.ToString("###000"),
                    number       = number.Value,
                    created_date = number.CreatedDate.ToString("yyyy-MM-dd hh:mm tt"),
                    updated_date = number.UpdatedDate.ToString("yyyy-MM-dd hh:mm tt"),
                    confirmation = number.Confirmation.ToString(),
                    user         = number.User?.UserName
                });
            }

            return(Json(new
            {
                draw = filter.draw,
                recordsTotal = totalRows,
                recordsFiltered = totalRowsFiltered,
                data = result
            }));
        }