/// <summary> /// Make a donation approval request to the parent /// </summary> /// <param name="donation">The donation</param> /// <returns>The donation</returns> public Donation Donate(Donation donation) { var canAllowTransaction = _earningsService.CanTransact(EarningsBucketType.Share, donation.Amount); if (donation.Amount == 0 || !canAllowTransaction) { throw new InvalidOperationException($"Insufficient balance in share bucket!"); } donation.FamilyMemberID = _currentUserService.MemberID; donation.Date = DateTime.UtcNow; Repository.Insert(donation); // Updates child earnings var childEarnings = _earningsService.GetByMemberId(_currentUserService.MemberID); childEarnings.Share -= donation.Amount; Repository.Update(childEarnings); var child = _familyService.GetMemberById(donation.FamilyMemberID); var admin = _familyService.GetAdmin(); var charity = Repository.Table <Charity>().SingleOrDefault(p => p.Id == donation.CharityID); var message = $"{child.Firstname.FirstCharToUpper()} has decided to donate ${donation.Amount:N2} to {charity.Name}." + $" Are you OK with transfering ${donation.Amount:N2} back into your account so you can make the donation? Reply YES or NO."; _smsApprovalHistory.Add(admin.Id, ApprovalType.CharityDonation, message, donation.Id); if (admin != null && !string.IsNullOrEmpty(admin.PhoneNumber)) { _textMessageService.Send(admin.PhoneNumber, message); } return(donation); }
/// <summary> /// initiates the stock purchase. /// </summary> /// <param name="stockPurchaseRequest">The stock purchase request.</param> /// <returns>The stock purchase request.</returns> public StockPurchaseRequest InitiateStockPurchase(StockPurchaseRequest stockPurchaseRequest) { stockPurchaseRequest.Fee = StockFee; var canAllowTransaction = _earningsService.CanTransact(EarningsBucketType.Save, (stockPurchaseRequest.Amount + stockPurchaseRequest.Fee)); if (stockPurchaseRequest.Amount == 0 || !canAllowTransaction) { throw new InvalidOperationException("Insufficient balance in save bucket!"); } stockPurchaseRequest.LineItemID = Guid.NewGuid(); stockPurchaseRequest.TransactionID = Guid.NewGuid(); stockPurchaseRequest.DateCreated = DateTime.UtcNow; stockPurchaseRequest.ChildID = _currentUserService.MemberID; Repository.Insert(stockPurchaseRequest); // deduct save amount of corresponding child from child earnings var childEarnings = _earningsService.GetByMemberId(_currentUserService.MemberID); childEarnings.Save -= (stockPurchaseRequest.Amount + stockPurchaseRequest.Fee); // Deducting stock amount including Fee Repository.Update(childEarnings); var admin = _familyService.GetAdmin(); var child = _familyService.GetMember(); var stock = GetById(stockPurchaseRequest.StockItemID); var stockName = string.IsNullOrEmpty(stock.BrandName) ? stock.CompanyPopularName : stock.BrandName; var message = $"{child.Firstname.FirstCharToUpper()} would like to buy ${stockPurchaseRequest.Amount:N2} of {stockName} stock. Are you OK with this? Reply YES or NO."; _smsApprovalHistory.Add(admin.Id, ApprovalType.StockPurchase, message, stockPurchaseRequest.Id); if (admin != null && !string.IsNullOrEmpty(admin.PhoneNumber)) { _textMessageService.Send(admin.PhoneNumber, message); } return(stockPurchaseRequest); }