Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public Model.ShopCart.Checkout.Result HandelCheckout(List <VmTeamSelection> teamSelectionList, string advisorUserId)
        {
            var productCode = 0;

            var blInvoice = new BLInvoice();

            var invoiceList = blInvoice.GetInvoiceFullInfoByUserId(advisorUserId, false);

            var newShopOrder = CreateOrder();

            if (newShopOrder.Error == "0")
            {
                var blProduct   = new BLProduct();
                var productList = new List <VmProduct>();

                var blOrder = new BLOrder();

                var newOrder = new VmOrder()
                {
                    InvoiceId   = invoiceList.Id,
                    OrderDate   = DateTime.Now,
                    ShopOrderId = int.Parse(newShopOrder.Order.Id),
                    UserId      = advisorUserId,
                    Complete    = false
                };

                blOrder.CreateOrder(newOrder);

                foreach (var item in invoiceList.InvoiceDetails)
                {
                    if (item.IsChecked)
                    {
                        productCode = ProductDictionary[item.PaymentRuleId + item.FirstTeamOrExtraTeam].Id;
                        productList.Add(new VmProduct
                        {
                            Amount        = 1,
                            ShopOrderId   = int.Parse(newShopOrder.Order.Id),
                            ShopProductId = productCode
                        });

                        var orderInfo = AddProduct(newShopOrder.Order.Id, productCode.ToString());
                        if (orderInfo.Error != "0")
                        {
                            return(null);
                        }

                        if (item.ExtraParticipantCount > 0)
                        {
                            productCode = ProductDictionary["ExtraParticipant"].Id;

                            orderInfo = AddProduct(newShopOrder.Order.Id, productCode.ToString(), item.ExtraParticipantCount);

                            if (orderInfo.Error != "0")
                            {
                                return(null);
                            }
                            else
                            {
                                productList.Add(new VmProduct
                                {
                                    Amount        = item.ExtraParticipantCount,
                                    ShopOrderId   = int.Parse(newShopOrder.Order.Id),
                                    ShopProductId = productCode
                                });
                            }
                        }
                    }
                }

                blProduct.CreateBatchProduct(productList);

                return(PrepareCheckout(int.Parse(newShopOrder.Order.Id)));
            }

            return(null);
        }