Exemplo n.º 1
0
        public void ProcessInvoice(string currentUserId, int currentTeamId, List <VmTeamSelection> teamSelectionList)
        {
            var invoiceDetailRepository = UnitOfWork.GetRepository <InvoiceDetailRepository>();

            var blPaymentRule     = new BLPaymentRule();
            var blparticipantRule = new BLParticipantRule();
            var blTeamMember      = new BLTeamMember();

            var paymentRule = blPaymentRule.GetPaymentRuleBySuitableDueDate(DateTime.Now.Date);

            var participantRule = blparticipantRule.GetParticipantRule();

            if (paymentRule != null)
            {
                var vmInvoice = GetInvoiceByUserId(currentUserId, false);

                if (vmInvoice == null)
                {
                    vmInvoice = new VmInvoice
                    {
                        UserId          = currentUserId,
                        DateOfIssue     = DateTime.Now.Date,
                        InvoiceTotal    = 0,
                        Subtotal        = 0,
                        Tax             = 0,
                        Total           = 0,
                        AmountDue       = 0,
                        ConventionalFee = 2.5M,
                        Finished        = false,
                    };

                    vmInvoice.Id = CreateInvoice(vmInvoice);
                }

                vmInvoice.Subtotal = 0;

                invoiceDetailRepository.DeleteInvoiceDetailsByInvoiceId(vmInvoice.Id);

                var finishedInvoice = GetInvoiceByUserId(currentUserId, true);

                var allowCheckFisrtTeam = false;

                if (finishedInvoice == null)
                {
                    allowCheckFisrtTeam = true;
                }

                foreach (var item in teamSelectionList)
                {
                    var teamMemberCount = blTeamMember.GetTeamMembersCount(item.TeamId);

                    var invoiceDetail = new InvoiceDetail();

                    if (item.Checked == true)
                    {
                        invoiceDetail.TeamId          = item.TeamId;
                        invoiceDetail.InvoiceId       = vmInvoice.Id;
                        invoiceDetail.PaymentRuleId   = paymentRule.Id;
                        invoiceDetail.IsFirstTeam     = item.IsFirstTeam;
                        invoiceDetail.ConventionalFee = vmInvoice.ConventionalFee;

                        if (item.IsFirstTeam && allowCheckFisrtTeam == true)
                        {
                            invoiceDetail.ExtraTeamDiscount     = 0;
                            invoiceDetail.ExtraParticipantCount = (teamMemberCount - participantRule.FirstTeamMaxMember) < 0 ? 0 : teamMemberCount - participantRule.FirstTeamMaxMember;
                        }
                        else
                        {
                            invoiceDetail.ExtraTeamDiscount     = paymentRule.ExtraTeamDiscount;
                            invoiceDetail.ExtraParticipantCount = (teamMemberCount - participantRule.EachExtraTeamMaxMember) < 0 ? 0 : teamMemberCount - participantRule.EachExtraTeamMaxMember;
                            invoiceDetail.TeamUnitCost          = paymentRule.FirstTeamFee - paymentRule.ExtraTeamDiscount;//Team Unit Cost
                        }

                        invoiceDetail.TeamUnitCost             = paymentRule.FirstTeamFee;//Team Unit Cost
                        invoiceDetail.ExtraParticipantUnitCost = participantRule.ExtraParticipantFee;
                        invoiceDetail.ExtraParticipantAmount   = invoiceDetail.ExtraParticipantCount * invoiceDetail.ExtraParticipantUnitCost;
                        invoiceDetail.Amount = invoiceDetail.ExtraParticipantAmount + invoiceDetail.TeamUnitCost - invoiceDetail.ExtraTeamDiscount;

                        invoiceDetailRepository.CreateInvoiceDetail(invoiceDetail);

                        vmInvoice.Subtotal             += invoiceDetail.Amount;
                        vmInvoice.TotalConventionalFee += vmInvoice.ConventionalFee * invoiceDetail.Amount / 100;
                    }
                }

                UnitOfWork.Commit();

                vmInvoice.Total        = vmInvoice.Subtotal + vmInvoice.TotalConventionalFee;
                vmInvoice.AmountDue    = vmInvoice.Total;
                vmInvoice.InvoiceTotal = vmInvoice.AmountDue * vmInvoice.ConventionalFee / 100;

                UpdateInvoice(vmInvoice);
            }
        }
Exemplo n.º 2
0
        public VmInvoice GetExtraMemberInvoiceFullInfoByUserId(string userId)
        {
            var viewInvoiceRepository = UnitOfWork.GetRepository <ViewInvoiceExtraMemberRepository>();

            var viewInvoice = viewInvoiceRepository.GetViewInvoiceExtraMemberByUserId(userId, true, true);

            if (viewInvoice.Count() == 0)
            {
                return(null);//At first, Please pay for the team by the peyment menu
            }

            var teamIds = viewInvoice.Select(t => t.TeamId).Distinct(); // Grouped Teams

            var invoiceDetailRepository = UnitOfWork.GetRepository <InvoiceDetailRepository>();

            var blparticipantRule = new BLParticipantRule();

            var blTeamMember = new BLTeamMember();

            var participantRule = blparticipantRule.GetParticipantRule();

            var extraMemberCount = 0;

            #region Temprorary Calculation


            foreach (var teamId in teamIds)
            {
                var teamExtraParticipantSum = viewInvoice.Where(i => i.TeamId == teamId).Sum(t => t.ExtraParticipantCount);

                var team = viewInvoice.Where(i => i.TeamId == teamId).First();

                var teamMemberCount = blTeamMember.GetTeamMembersCount(team.TeamId);

                extraMemberCount = 0;

                if (team.IsFirstTeam.Value)
                {
                    extraMemberCount = (teamMemberCount - participantRule.FirstTeamMaxMember - teamExtraParticipantSum).Value;
                }
                else
                {
                    extraMemberCount = (teamMemberCount - participantRule.EachExtraTeamMaxMember - teamExtraParticipantSum).Value;
                }

                if (extraMemberCount > 0)
                {
                    break;
                }
            }

            if (extraMemberCount <= 0)
            {
                return(null);// There is no balance to pay for extra members.
            }

            #endregion Temprorary Calculation

            #region Create/Edit Invoice

            VmInvoice vmInvoice = null;

            if (extraMemberCount > 0) // there is one payable extra member at least
            {
                var viewInvoiceUnFinished = viewInvoiceRepository.GetViewInvoiceExtraMemberByUserId(userId, true, false);

                if (viewInvoiceUnFinished == null || viewInvoiceUnFinished.Count() == 0)
                {
                    vmInvoice = new VmInvoice
                    {
                        UserId          = userId,
                        DateOfIssue     = DateTime.Now.Date,
                        InvoiceTotal    = 0,
                        Subtotal        = 0,
                        Tax             = 0,
                        Total           = 0,
                        AmountDue       = 0,
                        ConventionalFee = 2.5M,
                        Finished        = false,
                    };

                    vmInvoice.Id = CreateInvoice(vmInvoice);
                }
                else
                {
                    var blInvoice = new BLInvoice();
                    vmInvoice = blInvoice.GetInvoiceById(viewInvoiceUnFinished.First().InvoiceId.Value);

                    invoiceDetailRepository.DeleteInvoiceDetailsByInvoiceId(viewInvoiceUnFinished.First().InvoiceId.Value);
                    UnitOfWork.Commit();
                }

                vmInvoice.Subtotal = 0;
            }

            #endregion Create/Edit Invoice

            #region Calculation

            foreach (var teamId in teamIds)
            {
                var teamExtraParticipantSum = viewInvoice.Where(i => i.TeamId == teamId).Sum(t => t.ExtraParticipantCount);

                var team = viewInvoice.Where(i => i.TeamId == teamId).First();

                var teamMemberCount = blTeamMember.GetTeamMembersCount(team.TeamId);

                extraMemberCount = 0;

                if (team.IsFirstTeam.Value)
                {
                    extraMemberCount = (teamMemberCount - participantRule.FirstTeamMaxMember - teamExtraParticipantSum).Value;
                }
                else
                {
                    extraMemberCount = (teamMemberCount - participantRule.EachExtraTeamMaxMember - teamExtraParticipantSum).Value;
                }

                if (extraMemberCount > 0) // has payable extra member
                {
                    var invoiceDetail = new InvoiceDetail
                    {
                        TeamId            = team.TeamId,
                        InvoiceId         = vmInvoice.Id,
                        PaymentRuleId     = team.PaymentRuleId.Value,
                        IsFirstTeam       = team.IsFirstTeam.Value,
                        ConventionalFee   = vmInvoice.ConventionalFee,
                        ExtraTeamDiscount = 0
                    };

                    invoiceDetail.ExtraParticipantCount = extraMemberCount;

                    invoiceDetail.TeamUnitCost = 0;

                    invoiceDetail.ExtraParticipantUnitCost = participantRule.ExtraParticipantFee;

                    invoiceDetail.ExtraParticipantAmount = invoiceDetail.ExtraParticipantCount * invoiceDetail.ExtraParticipantUnitCost;

                    invoiceDetail.Amount = invoiceDetail.ExtraParticipantAmount;

                    invoiceDetailRepository.CreateInvoiceDetail(invoiceDetail);

                    vmInvoice.Subtotal += invoiceDetail.Amount;

                    vmInvoice.TotalConventionalFee += vmInvoice.ConventionalFee * invoiceDetail.Amount / 100;
                }
            }

            #endregion Calculation

            UnitOfWork.Commit();

            vmInvoice.Total        = vmInvoice.Subtotal + vmInvoice.TotalConventionalFee;
            vmInvoice.AmountDue    = vmInvoice.Total;
            vmInvoice.InvoiceTotal = vmInvoice.AmountDue * vmInvoice.ConventionalFee / 100;

            UpdateInvoice(vmInvoice);

            viewInvoice = viewInvoiceRepository.GetViewInvoiceExtraMemberByUserId(userId, true, false);

            var viewDataInvoice = viewInvoiceRepository.GetViewInvoiceExtraMemberById(vmInvoice.Id);

            var invoice = viewDataInvoice.OrderByDescending(i => i.Total).First();

            var allowCheckFisrtTeam = false;

            if (GetPayedTeamCount(userId) == 0)
            {
                allowCheckFisrtTeam = true;
            }

            vmInvoice.Id                  = invoice.InvoiceId ?? -1;
            vmInvoice.University          = invoice.University;
            vmInvoice.Address             = invoice.StreetLine1 + " " + invoice.StreetLine2 + " " + invoice.City + invoice.State + " " + invoice.ZipCode;
            vmInvoice.AccountOwner        = invoice.FirstName + " " + invoice.LastName;
            vmInvoice.DateOfIssue         = invoice.DateOfIssue ?? DateTime.Now.Date;
            vmInvoice.InvoiceNumber       = invoice.InvoiceNumber ?? 0;
            vmInvoice.InvoiceTotal        = invoice.InvoiceTotal ?? 0;
            vmInvoice.Title               = invoice.Title ?? "";
            vmInvoice.UserId              = invoice.UserId;
            vmInvoice.Subtotal            = invoice.Subtotal ?? 0;
            vmInvoice.Tax                 = invoice.Tax ?? 0;
            vmInvoice.Total               = invoice.Total ?? 0;
            vmInvoice.AmountDue           = invoice.AmountDue ?? 0;
            vmInvoice.AllowCheckFisrtTeam = allowCheckFisrtTeam;

            vmInvoice.InvoiceDetails =

                (from i in viewInvoice
                 select new VmInvoiceDetail
            {
                Amount = i.Amount ?? 0,
                ExtraParticipantAmount = i.ExtraParticipantAmount ?? 0,
                ExtraParticipantCount = i.ExtraParticipantCount ?? 0,
                ExtraParticipantUnitCost = i.ExtraParticipantUnitCost ?? 0,
                ExtraTeamDiscount = i.ExtraTeamDiscount ?? 0,
                Id = i.InvoiceId ?? -1,
                InvoiceId = invoice.InvoiceId ?? -1,
                IsFirstTeam = i.IsFirstTeam ?? false,
                FirstTeamOrExtraTeam =
                    (allowCheckFisrtTeam == true) ?
                    (i.IsFirstTeam == null || i.IsFirstTeam == false) ? "Extra" : "First"
                                               : "Extra",

                PaymentRuleId = i.PaymentRuleId ?? -1,
                PayStatus = i.PayStatus,
                TeamId = i.TeamId,
                TeamName = i.TeamName,
                TeamUnitCost = (i.IsFirstTeam == null || i.IsFirstTeam == false) ?
                               (i.TeamUnitCost - i.ExtraTeamDiscount) ?? 0 : i.TeamUnitCost ?? 0,
                TypeOfRegistration = i.TypeOfRegistration,
                DueDate = i.DueDate ?? DateTime.Now.Date,
                IsChecked = i.Amount > 0 ? true : false,
                ConventionalFee = i.Amount * i.ConventionalFee / 100,
                Task = i.Task,
            }).ToList();

            vmInvoice.TotalConventionalFee = vmInvoice.InvoiceDetails.Sum(i => i.ConventionalFee).Value;

            return(vmInvoice);
        }