Ejemplo n.º 1
0
        /// <summary>
        /// Can transact from earnings bucket
        /// </summary>
        /// <param name="bucketType">Earnings bucket type</param>
        /// <param name="amount">Amount</param>
        /// <param name="familyMemberId">The family member identifier.</param>
        /// <returns>Result</returns>
        public bool CanTransact(EarningsBucketType bucketType, decimal amount, int?familyMemberId = null)
        {
            var memberId = familyMemberId ?? _currentUserService.MemberID;

            switch (bucketType)
            {
            case EarningsBucketType.Save:
                return(Repository.Table <ChildEarnings>().Any(m => m.FamilyMemberID == memberId && m.Save >= amount));

            case EarningsBucketType.Share:
                return(Repository.Table <ChildEarnings>().Any(m => m.FamilyMemberID == memberId && m.Share >= amount));

            case EarningsBucketType.Spend:
                return(Repository.Table <ChildEarnings>().Any(m => m.FamilyMemberID == memberId && m.Spend >= amount));

            default:
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Move money
        /// </summary>
        /// <param name="sourceBucket">Source bucket</param>
        /// <param name="destinationBucket">Destination bucket</param>
        /// <param name="amount">Amount</param>
        /// <returns>Child earnings</returns>
        public ChildEarnings MoveMoney(EarningsBucketType sourceBucket, EarningsBucketType destinationBucket, decimal amount)
        {
            if (sourceBucket == destinationBucket)
            {
                throw new InvalidParameterException("You can't move same bucket");
            }

            // Gets child earnings
            var earnings = Repository.Table <ChildEarnings>().SingleOrDefault(p => p.FamilyMemberID.Equals(_currentUserService.MemberID));

            if (earnings == null)
            {
                throw new ObjectNotFoundException("Child earnings is empty");
            }

            var insufBalanceMsg = "Insufficient balance to complete the transfer";

            // Reduce amount from source bucket
            switch (sourceBucket)
            {
            case EarningsBucketType.Save:
                if (earnings.Save < amount)
                {
                    throw new InvalidOperationException(insufBalanceMsg);
                }

                earnings.Save -= amount;
                break;

            case EarningsBucketType.Share:
                if (earnings.Share < amount)
                {
                    throw new InvalidOperationException(insufBalanceMsg);
                }

                earnings.Share -= amount;
                break;

            case EarningsBucketType.Spend:
                if (earnings.Spend < amount)
                {
                    throw new InvalidOperationException(insufBalanceMsg);
                }

                earnings.Spend -= amount;
                break;
            }

            // Add amount to desination
            switch (destinationBucket)
            {
            case EarningsBucketType.Save:
                earnings.Save += amount;
                break;

            case EarningsBucketType.Share:
                earnings.Share += amount;
                break;

            case EarningsBucketType.Spend:
                earnings.Spend += amount;
                break;
            }

            Repository.Update <ChildEarnings>(earnings);
            return(earnings);
        }