Exemplo n.º 1
0
        /// <summary>
        /// Allocate amount to earnings bucket
        /// </summary>
        /// <param name="familyMemberId">Family member identifier</param>
        /// <param name="totalAmount">Total amount</param>
        public void AllocateEarnings(int familyMemberId, decimal totalAmount)
        {
            var allocationSettings = _allocationSettingsService.GetByMemberId(familyMemberId);

            // Calculate chore payments, then split
            decimal saveAmount  = Math.Round(totalAmount * (allocationSettings.Save / 100), 3);
            decimal shareAmount = Math.Round(totalAmount * (allocationSettings.Share / 100), 3);

            string[] amountArray             = saveAmount.ToString().Split(new char[] { '.' });
            int      afterDecimalRoundAmount = Convert.ToInt32(Convert.ToDecimal(amountArray[1]) % 1000);
            int      counter = 0;

            if (afterDecimalRoundAmount >= 5)
            {
                saveAmount = Math.Round(totalAmount * (allocationSettings.Save / 100), 2);
                counter++;
            }
            else
            {
                saveAmount = Math.Round(totalAmount * (allocationSettings.Save / 100), 2);
            }

            amountArray             = shareAmount.ToString().Split(new char[] { '.' });
            afterDecimalRoundAmount = Convert.ToInt32(Convert.ToDecimal(amountArray[1]) % 1000);

            if (afterDecimalRoundAmount >= 5 && counter == 0)
            {
                shareAmount = Math.Round(totalAmount * (allocationSettings.Share / 100), 2);
            }
            else
            {
                shareAmount = Math.Truncate(100 * (totalAmount * (allocationSettings.Share / 100))) / 100;
            }

            //Client Suggested
            var spendAmount = Math.Round(totalAmount, 2) - saveAmount - shareAmount;
            // Get child earnings
            var childEarnings    = Repository.Table <ChildEarnings>().SingleOrDefault(m => m.FamilyMemberID == familyMemberId);
            var hasChildEarnings = (childEarnings != null);

            childEarnings = childEarnings ?? new ChildEarnings {
                FamilyMemberID = familyMemberId
            };

            // Update child earnings
            childEarnings.Save  += saveAmount;
            childEarnings.Share += shareAmount;
            childEarnings.Spend += spendAmount;
            if (hasChildEarnings)
            {
                Repository.Update(childEarnings);
            }
            else
            {
                Repository.Insert(childEarnings);
            }
        }
        public HttpResponseMessage GetAllocationSettings(int familyMemberId)
        {
            if (!_accountService.BelongsToThisFamily(familyMemberId))
            {
                throw new UnauthorizedAccessException();
            }

            return(Request.CreateResponse(HttpStatusCode.OK, _allocationSettingsService.GetByMemberId(familyMemberId)));
        }