Esempio n. 1
0
        /// <summary>
        /// Send pay day notification.
        /// </summary>

        public void SendPayDayMessage()
        {
            var      dtTodayUtc      = DateTime.UtcNow;
            var      timeZoneCST     = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
            var      utcOffset       = new DateTimeOffset(dtTodayUtc, TimeSpan.Zero);
            DateTime cstTimeZoneTime = utcOffset.ToOffset(timeZoneCST.GetUtcOffset(utcOffset)).DateTime;

            var     nextPayDate      = cstTimeZoneTime.GetNextPayDay().Date.AddHours(15);
            decimal amountdeduction  = decimal.Zero;
            var     startDate        = nextPayDate.AddDays(-7);
            decimal memberPrevAmount = decimal.Zero;

            var completedChoresByFamily = _choreService.GetCompletedChoresByFamily(startDate, nextPayDate);

            foreach (var familyChores in completedChoresByFamily)
            {
                //Update Include flag for perticular family
                foreach (var choreDetail in familyChores)
                {
                    var includedChore = Repository.Table <Chore>().SingleOrDefault(p => p.Id.Equals(choreDetail.Id));
                    includedChore.IncludedFlag = true;
                    Repository.Update(includedChore);
                }

                var amountToBePaid = familyChores.Sum(m => m.Value);

                var childMembers = familyChores.Select(p => p.FamilyMember).Distinct().ToList();

                //Check for previous Week childs amount
                foreach (var child in childMembers)
                {
                    //memberPrevAmount = _earningServices.CalculateChildChoreStatusAmount(child.Id);
                    if (memberPrevAmount != decimal.Zero)
                    {
                        amountToBePaid = amountToBePaid - memberPrevAmount;
                    }
                }



                var childCount = Repository.Table <FamilyMember>().Count(m => m.User.FamilyID == familyChores.Key && m.MemberType == MemberType.Child && !m.IsDeleted);
                if (childCount == 0)
                {
                    continue;
                }
                var admin1 = Repository.Table <FamilyMember>().FirstOrDefault(m => m.User.FamilyID == familyChores.Key);
                var admin  = Repository.Table <FamilyMember>().FirstOrDefault(m => m.User.FamilyID == familyChores.Key &&
                                                                              m.MemberType == MemberType.Admin &&
                                                                              m.User.Family.FamilySubscription.Status == SubscriptionStatus.Active);

                if (admin == null)
                {
                    continue;
                }

                ////Check Financial Account Verification
                var hasFinancialAccount = Repository.Table <FinancialAccount>().Where(p => p.FamilyMemberID == admin.Id).FirstOrDefault().Status;
                if (hasFinancialAccount != FinancialAccountStatus.Verified)
                {
                    continue;
                }

                //Check The amount less than $1
                if (amountToBePaid < 1)
                {
                    var lessAmountMessage = "Your family total for the week is less than $1.00. BusyKid cannot process transactions less than $1.00.";
                    _smsApprovalHistory.AddLessAmount(admin.Id, ApprovalType.ChorePayment, lessAmountMessage);

                    if (!string.IsNullOrEmpty(admin.PhoneNumber))
                    {
                        _textMessageService.Send(admin.PhoneNumber, lessAmountMessage);
                    }

                    continue;
                }

                if (admin.PayDayAutoApproval)
                {
                    foreach (var chore in familyChores)
                    {
                        chore.ChoreStatus = ChoreStatus.CompletedAndApproved;
                        Repository.Update(chore);
                    }
                    continue;
                }

                decimal totalAmountToBePaid = 0;
                var     meesagesum          = "Total";

                var childPaymentDetails = string.Empty;
                foreach (var child in childMembers)
                {
                    var amountToBePaidToChild = familyChores.Where(m => m.FamilyMemberID == child.Id).Sum(m => m.Value);
                    if (amountToBePaidToChild == 0)
                    {
                        continue;
                    }
                    var separator = "\n";

                    //amountdeduction = _earningServices.CalculateChildChoreStatusAmount(child.Id);
                    amountToBePaidToChild = amountToBePaidToChild - amountdeduction;
                    if (amountToBePaidToChild <= 0)
                    {
                        continue;
                    }

                    totalAmountToBePaid  = totalAmountToBePaid + amountToBePaidToChild;
                    childPaymentDetails += $"{ child.Firstname }: ${amountToBePaidToChild:N2}{separator}";
                }
                childPaymentDetails += $"{ meesagesum }: ${totalAmountToBePaid:N2}";

                var message = $"Tomorrow is payday! Here is a summary of earnings: \n{childPaymentDetails}"
                              + " \n\nReply YES or NO.\nRespond within 2 hours to ensure Friday payday.";

                _smsApprovalHistory.Add(admin.Id, ApprovalType.ChorePayment, message);

                if (!string.IsNullOrEmpty(admin.PhoneNumber))
                {
                    _textMessageService.Send(admin.PhoneNumber, message);
                }

                //SendMessagePaydayNotProceedService(startDate, nextPayDate, admin.Id);
            }
        }