public async Task <IActionResult> AwardDividents() { // Lets take it slowly step by step // Checks // Find the total number of members IList <User> Members = userRepository.Users.Where(a => a.UserType == 2).ToList(); if (!Members.Any()) { // Bad request if no members return(StatusCode(406)); } // Number of members int NoMembers = Members.Count(); // Get the total member founds decimal TotalShare = (decimal)compRepository.Account.MemberShare; if (TotalShare == 0) { // Precondition Fail, no money! return(StatusCode(412)); } // The devident for each user decimal Divident = TotalShare / ((decimal)NoMembers); // Get the current user int CuId = await GetUserId(); // For each member foreach (User us in Members) { // Give money if he does not have an account create one PersonalFinancialAccount personal = personalAccount.Account.SingleOrDefault(a => a.UserId == us.Id); if (personal == null) { personal = new PersonalFinancialAccount(); personal.UserId = us.Id; personal.Amount = Divident; personalAccount.AddAccount(personal); } else { personal.Amount += Divident; personalAccount.UpdateAccount(personal); } // Register Transaction Transaction Current = new Transaction(); Current.Date = DateTime.Now; Current.OwnerId = CuId; Current.RecipientId = us.Id; Current.DateProcessed = DateTime.Now; Current.IsProcessed = 1; Current.Price = Divident; Current.Type = 3; financeRepository.SaveTransaction(Current); // Inform the user Message message = new Message(); message.SenderId = CuId; message.ReceiverId = us.Id; message.DateSent = DateTime.Now; message.Processed = false; message.Title = "Divident Award"; message.Message1 = $"Today {DateTime.Today.ToString()},<br> the CoPartnership awards an intermittent divident which amounts to {Divident.ToString("#.##")} euros. <br> Kind regards, <br> the Administrative Team "; messageInterface.SaveMessage(message); } // For the Company found // Empty the account CompanyFinancialAccount account = compRepository.Account; account.MemberShare = 0; compRepository.UpdateAccount(account); return(Ok()); }
public void UpdateAccount(CompanyFinancialAccount account) { db.Update(account); db.SaveChanges(); }