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 async Task <IActionResult> AcceptOffer(RequestsViewModel model) { var transactionId = model.TransactionId; var offer = _transactionRepository.Transactions.FirstOrDefault(t => t.Id == transactionId); var adminId = await GetUserId(); var coBalance = _compAccount.GetCoopShare(); if (coBalance < offer.Price) //check if company has enough money { model.ErrorMessage = "Co-Partenership balance is not enought to buy the offer."; model.PendingOffers = _transactionRepository.Transactions .Where(t => t.IsProcessed == 0 && t.Type == 2).ToList(); model.PendingOrders = _transactionRepository.Transactions .Where(t => t.IsProcessed == 0 && t.Type == 1).ToList(); model.Addresses = new List <Address>(); foreach (var order in model.PendingOrders) { var address = _addressRepository.AddressRepo .FirstOrDefault(a => a.TransactionId == order.Id); if (address != null) { model.Addresses.Add(address); } } return(View("Requests", model)); } //take money from company coBalance -= (decimal)offer.Price; _compAccount.UpdateCoopBalance(coBalance); //give money to member var memberAccount = _personalAccountRepo.GetAccount((int)offer.OwnerId); if (memberAccount == null) { memberAccount = new PersonalFinancialAccount() { UserId = (int)offer.OwnerId, Amount = offer.Price }; _personalAccountRepo.AddAccount(memberAccount); } else { memberAccount.Amount += offer.Price; _personalAccountRepo.UpdateAccount(memberAccount); } //offer accepted by current user offer.IsProcessed = 1; offer.DateProcessed = DateTime.Now; offer.RecipientId = adminId; _transactionRepository.UpdateTransaction(offer); //get offered products & add them to db var itemId = offer.TransactionItem.First().ItemId; var quantity = offer.TransactionItem.First().Quantinty; var item = _itemRepository.GetItem((int)itemId); item.StockQuantity += quantity; _itemRepository.UpdateItem(item); //send notification to member var message = new Message() { SenderId = adminId, ReceiverId = offer.OwnerId, DateSent = DateTime.Now, Title = "Offer accepted", Message1 = $"Your offer for {quantity} {item.UnitType} {item.Name} has been accepted and {Math.Round((decimal)offer.Price, 2)}€ has been deposited to your member account.<br> Thank you and have a great day,<br> Co-Partenership" }; _messageInterface.SaveMessage(message); return(RedirectToAction(nameof(Requests))); }