public ActionResult Camping(int id)
 {
     var show = new Shows(id);
     var showCampingList = ShowCampingList.getShowCampingList(id, "Name", 0);
     var cmp = new Camping(id);
     List<DateTime> cmpDays = new List<DateTime>();
     var start = cmp.Startdate;
     var end = cmp.Enddate;
     do
     {
         cmpDays.Add(start);
         start = start.AddDays(1);
     } while (start <= end);
     return View(new CampingModel
     {
         Show = show,
         ShowName = show.ShowName,
         ShowDetails = Business.ShowDetails.GetShowDetails(id),
         ShowCamping = cmp,
         CampingDays = cmpDays,
         CampingTotals = Business.Camping.getCampingTotals(id),
         PaidUsers = showCampingList.Count(x => x.UserShowStatus == 3 && x.HandlerType == 0),
         Members = showCampingList.Count(x => x.HandlerType == 1),
         Judges = showCampingList.Count(x => x.HandlerType == 2),
         NotPaidMembers = showCampingList.Count(x => x.HandlerType == 1 && x.UserShowStatus != 3),
         NotPaidJudges = showCampingList.Count(x => x.HandlerType == 2 && x.UserShowStatus != 3),
         WaitingList = Business.WaitingList.getWaitList(id),
         ShowCampingList = showCampingList,
         CampingSummary = UserCamping.GetCampingSummary(id, 0)
     });
 }
        public void ProcessRequest(HttpContext context)
        {
            int ShowID = Convert.ToInt32(context.Request["showid"]);
            Shows show = new Shows(ShowID);
            var sw = new StringWriter();
            sw.WriteLine(string.Format("Camping List for {0} ", show.ShowName));
            sw.WriteLine("Show Ref,Handler Type,Name,Group Name,Comments,Code,Pitch No,Days Camping,Unit Size");

            var showDetails = ShowDetails.GetShowDetails(ShowID);
            var showCamping = new Camping(ShowID);
            var campingTotals = Camping.getCampingTotals(ShowID);
            var unpaidUsers = Camping.getUnpaidCampingUsers(ShowID);
            var waitingList = WaitingList.getWaitList(ShowID);
            var showCampingList = ShowCampingList.getShowCampingList(ShowID, "Name", 0);

            foreach (var campingList in showCampingList)
            {
                var handlerType = "";
                var handlerTypeName = "";
                if (campingList.HandlerType != 0)
                {
                    handlerType = (campingList.HandlerType == 1 ? "M" : "J");
                    handlerTypeName = (campingList.HandlerType == 1 ? "Member" : "Judge");
                }

                sw.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8}",
                        campingList.UserShowid,
                        handlerTypeName,
                        campingList.Name,
                        campingList.PartyName.Replace(",", " "),
                        campingList.Comments.Replace(",", "").Replace("\r", string.Empty).Replace("\n", " "),
                        campingList.CampingCode,
                        campingList.PitchNo,
                        campingList.CampingDaysFormatted.Replace("<b>", "").Replace("</b>",". "),
                        campingList.PitchSize ));
            }

            context.Response.ClearContent();
            context.Response.ContentType = "text/csv";
            context.Response.AddHeader("content-disposition", String.Format("inline;filename=Camping-{0:ddMMMyyyy}.csv", show.ShowDate));
            context.Response.Write(sw);
            context.Response.End();
        }
        public ActionResult AdminEntry(int Id, int? UserId )
        {
            var show = new Shows(Id);
            var entries = UserShows.getAdminEntryUserShow(Id);
            var showCampingList = ShowCampingList.getShowCampingList(Id, "Name", 0);
            var cmp = new Camping(Id);
            //entries = entries.OrderBy(x => x.UserShowId).ToList();
            //ShowCamping.Limit - Model.PaidUsers
            var AdminEntry = new AdminEntryDto()
            {
                ShowId= Id,
                ShowName = show.ShowName,
                UserShowDetails = entries,
                OfflineCnt = entries.Count(y => y.EnteredBy.HasValue && y.EnteredBy == 1),
                OnlineCnt = entries.Count(y => y.EnteredBy.HasValue && y.EnteredBy == 0),
                PaidCamping = cmp.Limit - showCampingList.Count(x => x.UserShowStatus == 3 && x.HandlerType == 0)
            };

            ViewBag.NoBootstrap = true;
            ViewBag.SelectedUser = (UserId.HasValue ? UserId.Value : -1);
            return View(AdminEntry);
        }
        public static UserShows SaveEntry(ShowEntry showEntry)
        {
            DogClasses dc = new DogClasses();
            String[] dogs = new String[] { };
            if (showEntry.EnterData != null)
            {
                dogs = showEntry.EnterData.Split(',');
            }
            else
            {
                AppException.LogEvent(string.Format("SaveEntry: data length = 0, {0}{1}", showEntry.ShowId, showEntry.UserId));
            }
            var altHandlersList = new string[] { };
            if (showEntry.AltHandlersList != null)
            {
                altHandlersList = showEntry.AltHandlersList.Split(',');
            }

            //
            //  Multidog format is packed as
            //  <dogid>@<clsid>:<handlername>.<dogname>[;<handlername>.<dogname>][,<dogid>#<clsid>:<handlername>.<dogname>[;<handlername>.<dogname>]](captain$$teamname)
            //
            MultiDog.DeleteUserFromShow(showEntry.UserId, showEntry.ShowId);

            if (!string.IsNullOrEmpty(showEntry.PairsTeams))
            {
                var spt = Regex.Split(showEntry.PairsTeams, "[|]");
                var captain = "";
                var teamName = "";
                if (spt.Length > 1)
                {
                    var s = Regex.Split(spt[1], "[$]");
                    captain = s[0];
                    teamName = s[1];
                    if (captain.Length > 0 && teamName.Length > 0)
                    {
                        MultiDog.AddTeamPairsDetails(showEntry.ShowId, showEntry.UserId, captain, teamName,1,-1);
                    }
                }
                var pairTeams = spt[0].Split(',');
                foreach (String pairTeam in pairTeams)
                {
                    String[] pt = pairTeam.Split(';');
                    int DogID = -1;
                    int ClsID = -1; ;

                    foreach (String p in pt)
                    {
                        String[] dogcls;
                        String[] otherHandlerDog;
                        if (p.IndexOf(':') > 0)
                        {
                            dogcls = p.Split(':')[0].Split('@');
                            DogID = Convert.ToInt32(dogcls[0]);
                            ClsID = Convert.ToInt32(dogcls[1]);
                            otherHandlerDog = p.Split(':')[1].Split('.');

                            String[] handlerDetails = otherHandlerDog[0].Split('-');
                            if (handlerDetails.Length > 1)
                            {
                                String handlerName = handlerDetails[0];
                                int handlerID = -1;
                                int.TryParse(handlerDetails[1], out handlerID);

                                String[] dogDetails = otherHandlerDog[1].Split('-');
                                String dogName = dogDetails[0];
                                int dogID = -1;
                                int.TryParse(dogDetails[1], out  dogID);

                                MultiDog.Add(1, showEntry.UserId, DogID, ClsID, showEntry.ShowId, handlerName, dogName, handlerID, dogID,1);
                            }
                            else
                            {
                                MultiDog.Add(showEntry.UserId, DogID, ClsID, showEntry.ShowId, otherHandlerDog[0], otherHandlerDog[1]);
                            }
                        }
                        else
                        {
                            otherHandlerDog = p.Split('.');
                            String[] handlerDetails = otherHandlerDog[0].Split('-');
                            if (handlerDetails.Length > 1)
                            {
                                String handlerName = handlerDetails[0];
                                int handlerID = -1;
                                int.TryParse(handlerDetails[1], out handlerID);

                                String[] dogDetails = otherHandlerDog[1].Split('-');
                                String dogName = dogDetails[0];
                                int dogID = -1;
                                int.TryParse(dogDetails[1], out  dogID);

                                MultiDog.Add(1, showEntry.UserId, DogID, ClsID, showEntry.ShowId, handlerName, dogName, handlerID, dogID,1);
                            }
                        }
                    }
                }
            }

            foreach (String dog in dogs)
            {
                String[] details = dog.Split(':');
                int dogid = Convert.ToInt32(details[0]);

                String[] clslist = details[1].Split('.');
                List<int> classes = new List<int>();
                List<int> altHandlers = new List<int>();
                foreach (String clsid in clslist)
                {
                    if (clsid.Length > 0)
                    {
                        int altid = 0;
                        classes.Add(Convert.ToInt32(clsid));
                        for (int ii = 0; ii < altHandlersList.Length; ii++)
                        {
                            String[] kvPairs = altHandlersList[ii].Split(':');
                            if (kvPairs[0] == clsid && kvPairs[2] == details[0])
                            {
                                altid = Convert.ToInt32(kvPairs[1]);
                            }
                        }
                        altHandlers.Add(altid);
                    }
                }
                dc.Save(dogid, showEntry.ShowId, classes, altHandlers, -1);
            }

            UserShows us = new UserShows(showEntry.UserId, showEntry.ShowId);
            us.Status = us.Status = (int)UserShows.UserStatus.STATUS_SAVED;

            //
            // add check to see if this person is on the judging list.
            if (showEntry.HandlerType != (int)UserShows.HandlerTypes.MEMBER
                && Judge.isJudgeAtShow(showEntry.UserId, showEntry.ShowId))
            {
                us.HandlerType = (int)UserShows.HandlerTypes.JUDGE;
            }
            else
            {
                us.HandlerType = showEntry.HandlerType;
            }
            us.Optout = showEntry.OptOut;
            us.DogsMeasured = showEntry.DogsMeasured;
            us.Save();

            Camping camping = new Camping(showEntry.ShowId);
            if (camping.ID > -1)
            {
                if (!string.IsNullOrEmpty(showEntry.CampingDays))
                {
                    UserCamping.DeleteForUser(us.ID);
                    for (var i = 0; i < showEntry.Plots; i++)
                    {
                        UserCamping.Add(us.ID, showEntry.CampingParty, showEntry.CampComments, showEntry.CampingDays, showEntry.PitchSize);
                    }
                }
            }

            if (showEntry.WaitingList == 1)
            {
                WaitingList.Add(us.ShowID, us.Userid);
            }

            return us;
        }
        public static UserShows SaveEntryv2(UserShowEntry showEntry)
        {
            DogClasses dc = new DogClasses();
            var altHandlersList = new string[] { };
            UserShows us = new UserShows(showEntry.UserId, showEntry.ShowId);
            try
            {
                MultiDog.DeleteUserFromShow(showEntry.UserId, showEntry.ShowId);
                try
                {
                    if (showEntry.MultiDogEntries != null && showEntry.MultiDogEntries.Any())
                    {
                        var idx = 1;
                        var teamPairsId = 0;
                        foreach (var md in showEntry.MultiDogEntries)
                        {
                            teamPairsId = MultiDog.AddTeamPairsDetails(showEntry.ShowId, showEntry.UserId, md.TeamDetails.Captain, md.TeamDetails.TeamName, idx, md.ClassId);
                            foreach (var member in md.Members)
                            {
                                MultiDog.Add(teamPairsId, showEntry.UserId, member.DogId, md.ClassId, showEntry.ShowId, member.HandlerName ?? "", member.DogName ?? "", member.HandlerId, member.DogId, idx);
                            }
                            idx++;
                        }
                    }
                }
                catch (Exception e)
                {
                    AppException.Log(string.Format("SaveEntryv2 Error MultiDogs  (ShowId={0}, UserId={1} [{2}][{3}]", showEntry.ShowId, showEntry.UserId, e.Message, e.StackTrace), "");
                }

                foreach (DogsModel dog in showEntry.Dogs)
                {
                    dc.Save(showEntry.ShowId, dog);

                    if (dog.ClassNo != null)
                    {
                        var ykc = dog.ClassNo.Where(y => y.Juniors != null);
                        if (ykc.Any())
                        {
                            var y = ykc.First();
                            if (y.Juniors.Id == 0)
                            {
                                Juniors.AddJunior(dog.Id, y.Id, y.Juniors.DoB, y.Juniors.YKCNumber);
                            }
                            else
                            {
                                var jnr = new Juniors(y.Juniors.Id);
                                jnr.DogId = dog.Id;
                                jnr.ClassId = y.Id;
                                jnr.YKCNumber = y.Juniors.YKCNumber;
                                jnr.DoB = y.Juniors.DoB;
                                jnr.Save();
                            }
                        }
                        var g7dogs = dog.ClassNo.Where(x => x.ChampWins != null && x.ChampWins.Any());
                        foreach (var d in g7dogs)
                        {
                            foreach (var win in d.ChampWins)
                            {
                                if (!string.IsNullOrEmpty(win.ShowName) && !string.IsNullOrEmpty(win.ShowClass))
                                {
                                    DateTime parsedDate = DateTime.MinValue;
                                    if (!DateTime.TryParseExact(win.ShowDate, "dd-MM-yy", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate))
                                    {
                                        DateTime.TryParseExact(win.ShowDate, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate);
                                    }

                                    Fpp.Business.DogHistory.AddChampWins(new Core.Views.UpgradeDogView
                                    {
                                        Id = win.Id,
                                        ShowName = win.ShowName,
                                        ClassWon = win.ShowClass,
                                        WinDate = parsedDate,
                                        UserId = showEntry.UserId,
                                        DogId = dog.Id,
                                        NewGrade = 7,
                                        Comments = "Win towards grade 7",
                                        UpgradeType = win.UpgradeType,
                                        DateOfWin = win.ShowDate
                                    });

                                }
                            }
                        }
                    }
                }

                if (us.Status != (int)UserShows.UserStatus.STATUS_ENTERED_AND_PAID)
                {
                    us.Status = us.Status = (int)UserShows.UserStatus.STATUS_SAVED;
                }

                //
                // add check to see if this person is on the judging list.
                if (showEntry.HandlerType != (int)UserShows.HandlerTypes.MEMBER
                    && Judge.isJudgeAtShow(showEntry.UserId, showEntry.ShowId))
                {
                    us.HandlerType = (int)UserShows.HandlerTypes.JUDGE;
                }
                else
                {
                    us.HandlerType = showEntry.HandlerType;
                }
                us.Optout = showEntry.OptOut;
                us.DogsMeasured = showEntry.DogsMeasured;
                us.Save();

                Camping camping = new Camping(showEntry.ShowId);
                if (camping.ID > -1)
                {

                    var oldcamp = new UserCamping(us.ID);
                    UserCamping.DeleteForUser(us.ID);
                    if (showEntry.UserCamping != null)
                    {
                        for (var i = 0; i < showEntry.UserCamping.Plots; i++)
                        {
                            UserCamping.Add(us.ID, showEntry.UserCamping);
                        }
                        var uc = new UserCamping(us.ID);

                        if (oldcamp.ID > -1 && oldcamp.PitchDetails.Any() )
                        {
                            UserCamping.SetCode(uc.PitchDetails[0].ID, oldcamp.PitchDetails[0].CampingCode);
                            UserCamping.SetComments(uc.PitchDetails[0].ID, oldcamp.PitchDetails[0].Comments);
                            UserCamping.SetPitchNo(uc.PitchDetails[0].ID, oldcamp.PitchDetails[0].PitchNo);
                            if (uc.PitchDetails.Count > 1 && oldcamp.PitchDetails.Count > 1)
                            {
                                UserCamping.SetCode(uc.PitchDetails[1].ID, oldcamp.PitchDetails[1].CampingCode);
                                UserCamping.SetComments(uc.PitchDetails[1].ID, oldcamp.PitchDetails[1].Comments);
                                UserCamping.SetPitchNo(uc.PitchDetails[1].ID, oldcamp.PitchDetails[1].PitchNo);
                            }

                        }

                    }
                }

                if (showEntry.UserCamping != null && showEntry.UserCamping.WaitingList == 1)
                {
                    WaitingList.Add(showEntry.ShowId, showEntry.UserId);
                }

            }
            catch (Exception e)
            {
                AppException.Log( string.Format("SaveEntryv2 Error (ShowId={0}, UserId={1} [{2}][{3}]", showEntry.ShowId, showEntry.UserId, e.Message, e.StackTrace), "");
            }
            return us;
        }
Ejemplo n.º 6
0
        public static decimal costOfEntry(int ShowId, int UserId)
        {
            var us = new  UserShows(UserId, ShowId);
            var userCamping = new UserCamping(us.ID);
            if (userCamping.ID == -1 ) return 0M;

            var cost = 0M;
            var showCamping = new Camping(ShowId);
            var maxShowNights = (showCamping.Enddate - showCamping.Startdate).Days + 1;
            var userShowNight = userCamping.PitchDetails[0].CampingDays.Split(',').Count();
            switch (showCamping.ChargeType)
            {
                case 0:
                case 1:
                    if (maxShowNights == userShowNight)
                    {
                        cost = showCamping.Costpershow ;
                    }
                    else
                    {
                        cost = showCamping.Costpernight  * userShowNight ;
                    }
                    break;

                case 2:
                    cost = showCamping.Costpershow;
                    break;
            }
            return cost * userCamping.PitchDetails.Count;
        }
        public JsonResult ReceivePayment(ReceivedPayments receivedPayments)
        {
            decimal discountTotal = 0;
            int addNewTransaction = 1;

            var us = new UserShows(receivedPayments.UserId, receivedPayments.ShowId);
            String UserRefNo = us.ID.ToString().PadLeft(6, '0');

            int transID; ;

            if (receivedPayments.PaymentType.ToUpper().IndexOf("CHEQUE") > -1)
            {
                transID = Transaction.Add(UserRefNo, TransactionTypes.ShowEntryPayment, receivedPayments.Amount, receivedPayments.PaymentType, receivedPayments.ShowId, receivedPayments.UserId, DateTime.Now, receivedPayments.Amount);
            }
            else
            {
                transID = Transaction.Add(UserRefNo, TransactionTypes.ShowEntryPayment, receivedPayments.Amount, receivedPayments.PaymentType, receivedPayments.ShowId, receivedPayments.UserId, DateTime.Now);
            }
            Transaction.SetEnteredBy(transID, Transaction.ENTERED_BY.SHOW_ADMIN_ENTRY);

            var showTransList = ShowTransaction.getTransactionForUser(receivedPayments.UserId);

            var adminCharge = 0M;
            var tmp = showTransList.Sum(x => x.Amount);

            var trans = new Transaction(receivedPayments.UserId, receivedPayments.ShowId);
            List<ShowDiscounts> list;
            if (trans.EnteredBy == (int)Transaction.ENTERED_BY.SHOW_ADMIN_ENTRY)
            {
                list = ShowDiscounts.getDiscountsByType(receivedPayments.ShowId, us.HandlerType, (int)DiscountTypes.Fixed);
                foreach (ShowDiscounts sd in list)
                {
                    if (sd.DiscountType == (int)DiscountTypes.Fixed)
                    {
                        adminCharge = sd.Amount;
                    }
                }
            }
            var singleEntryId = ProcessEntry.ProcessEntry.getSingleEntry(receivedPayments.ShowId);
            var showCamping = new Camping(us.ShowID);
            var yy = ProcessEntry.ProcessEntry.calcNewTotalForEntry(us, showCamping, singleEntryId, ref discountTotal, ref addNewTransaction);
            if (yy + adminCharge == 0)
            {
                us.Status = 3;
                us.Save();
            }

            return Json(new
            {
                UserId = receivedPayments.UserId,
                Status = 0,
                ShowTransList = showTransList,
                UserShowStatus = us.Status
            });
        }
        public PaymentSummary GetDiscounts(int ShowId, int UserId, int HandlerType, bool fromAdmin = false, bool campingFull = false)
        {
            var paymentSummary = new PaymentSummary();
            var us = new UserShows(UserId, ShowId) {HandlerType = HandlerType};
            UserCamping.GetCampingSummary(ShowId, UserId);

            var charges = ShowCharges.getClassChargeDetails(ShowId);
            var discountList = ShowDiscounts.getDiscountsByType(ShowId, HandlerType, (int)DiscountTypes.TransactionCharge);

            var allDiscounts = ShowDiscounts.getClassChargeDetails(ShowId);
            var adminCharge = allDiscounts.FirstOrDefault(x => x.DiscountName == "Admin Charge");
            var onlineCharge = allDiscounts.FirstOrDefault(x => x.DiscountName == "Transaction Charge");
            //
            // Added display what has already been paid and add/subtract difference.
            var showTransactions = new List<Transaction>();
            if (us.ID > -1)
            {
                showTransactions = Transaction.getTransactionForShowUser(us.RefCode);
                if (showTransactions.Any(x => x.EnteredBy == (int)Transaction.ENTERED_BY.SHOW_ADMIN_ENTRY))
                {
                    if (adminCharge != null)
                    {
                        paymentSummary.add("Admin Charge", adminCharge.Amount, 0, ChargableTypes.OFFLINE_CHARGE);
                    }
                }
                if (showTransactions.Any(x => x.EnteredBy == (int)Transaction.ENTERED_BY.ONLINE_ENTRY)
                    || us.Status  != 3 )
                {
                    if (onlineCharge!= null)
                    {
                        if (fromAdmin)
                        {
                            var onlineEntryCnt = showTransactions.Count(x => x.EnteredBy == 0 && x.TransactionType == TransactionTypes.ShowEntryPayment && x.Amount > 0);
                            if (onlineEntryCnt > 0)
                            {
                                paymentSummary.add("'n' Online Charges ", onlineCharge.Amount, onlineEntryCnt, ChargableTypes.TRANSACTION_CHARGE_ADMIN);
                            }
                        }
                        else
                        {
                            paymentSummary.add("Transaction Charge", onlineCharge.Amount, 0, ChargableTypes.TRANSACTION_CHARGE);
                        }
                    }
                }
            }
            else
            {
                if (fromAdmin)
                {
                    if (adminCharge != null)
                    {
                        paymentSummary.add("Admin Charge", adminCharge.Amount, 0, ChargableTypes.OFFLINE_CHARGE);
                    }
                }
                else
                {
                    if (onlineCharge != null)
                    {
                        paymentSummary.add("Transaction Charge", onlineCharge.Amount, 0, ChargableTypes.TRANSACTION_CHARGE);
                    }
                }
            }
            var showCamping = new Camping(ShowId);
            foreach (var sd in discountList)
            {
                var tmp = sd.DiscountName + " discount " + sd.SubTypeName;
                tmp = tmp.Replace("'n'", sd.Amount.ToString());
                if (sd.Subtype == (int)CHARGES_SUBTYPES.n_NightsCamping)
                {
                    if (!campingFull)
                    {
                        paymentSummary.add(tmp, showCamping.Costpernight, Convert.ToInt32(sd.Amount), ChargableTypes.DISCOUNT_CAMPING);
                    }
                }
                else if (sd.Subtype == (int)CHARGES_SUBTYPES.percentCamping)
                {
                    if (!campingFull)
                    {
                        paymentSummary.addDiscount(tmp, sd.Amount, sd.Subtype, ChargableTypes.DISCOUNT_CAMPING, sd.ChargeType);
                    }
                }
                else if (sd.Subtype == (int)CHARGES_SUBTYPES.n_RunsFree)
                {
                    paymentSummary.addDiscount(tmp, sd.Amount, sd.Subtype, ChargableTypes.DISCOUNT, sd.ChargeType);
                }
                else if (sd.Subtype == (int)CHARGES_SUBTYPES.percentOff)
                {
                    paymentSummary.addDiscount(tmp, sd.Amount, sd.Subtype, ChargableTypes.DISCOUNT, sd.ChargeType);
                }
            }

            int lastChargeType = -1;
            var usedAtShow = ShowClasses.GetChargeTypesForShow(ShowId);
            foreach (ShowChargeTypes item in usedAtShow)
            {
                var charge = charges.FirstOrDefault(x => x.ChargeType == item.ChargeTypeId);
                if (charge != null)
                {
                    lastChargeType = charge.ChargeType;
                    paymentSummary.add(charge, ChargableTypes.STANDARD);
                }
            }
            if (showCamping.ID > -1 && !campingFull)
            {
                UserCamping userCamping = new UserCamping(us.ID);
                var maxNights = (showCamping.Enddate - showCamping.Startdate).Days + 1;
                int Nights = 0;
                if (userCamping.ID > -1)
                {
                    var list = userCamping.PitchDetails[0].CampingDays.Split(',').Where(x => x.Length > 0);
                    Nights = list.Count() * userCamping.PitchDetails.Count;
                }
                if (showCamping.Costpershow > 0)
                {
                    paymentSummary.add("Camping", showCamping.Costpershow, maxNights, ChargableTypes.MAXCAMPING);
                }
                if (showCamping.ChargeType == 1)
                {
                    if (showCamping.Costpershow == 0)
                    {
                        paymentSummary.add("Camping", showCamping.Costpershow, maxNights, ChargableTypes.MAXCAMPING);
                    }
                    paymentSummary.add("Camping", showCamping.Costpernight, Nights, ChargableTypes.CAMPING);
                }
                if (showCamping.ChargeType == 2)
                {
                    paymentSummary.add("Camping", showCamping.Costpershow, Nights, ChargableTypes.CAMPING_FIXFEE);
                }
            }

            Decimal val = 0M;
            showTransactions.ForEach(x => val += x.Amount);

            if (showTransactions.Any())
            {
                decimal lastPayment = 0;
                decimal showEntries = 0;
                //                if (showTransactions.LastOrDefault().TransactionType != TransactionTypes.RefundPaid)
                if (showTransactions.Any(x => x.TransactionType == TransactionTypes.ShowEntryPayment))
                {
                    lastPayment = showTransactions.Where(x => x.TransactionType == TransactionTypes.ShowEntryPayment).Sum(x => x.Amount);
                    if (lastPayment != 0)
                    {
                        paymentSummary.add("Payments Received", lastPayment, 0, ChargableTypes.TRANSACTION);
                    }
                }
                if (showTransactions.Any(x => x.TransactionType == TransactionTypes.ShowEntry))
                {
                    showEntries = showTransactions.Where(x => x.TransactionType == TransactionTypes.ShowEntry).Sum(x => x.Amount);
                    paymentSummary.add("Fees", showEntries, 0, ChargableTypes.TRANSACTION);
                }
                if (showTransactions.Any(x => x.TransactionType == TransactionTypes.TransactionCharge))
                {
                    showEntries = showTransactions.Where(x => x.TransactionType == TransactionTypes.TransactionCharge).Sum(x => x.Amount);
                    paymentSummary.add("Transaction Charges", showEntries, 0, ChargableTypes.TRANSACTION);
                }
                if (showTransactions.Any(x => x.TransactionType == TransactionTypes.RefundRequest))
                {
                    showEntries = showTransactions.Where(x => x.TransactionType == TransactionTypes.RefundRequest).Sum(x => x.Amount);
                    paymentSummary.add("RefundRequest", showEntries, 0, ChargableTypes.TRANSACTION);
                }
                if (showTransactions.Any(x => x.TransactionType == TransactionTypes.RefundPaid))
                {
                    var refunds = showTransactions.Where(x => x.TransactionType == TransactionTypes.RefundPaid);
                    showEntries = refunds.Sum(x => x.Amount);
                    paymentSummary.add("RefundPaid", showEntries, 0, ChargableTypes.TRANSACTION);

                    var RefundChargeAmount = DiscountManager.GetRefundCharge(ShowId);
                    paymentSummary.add("RefundCharge", RefundChargeAmount, refunds.Count(), ChargableTypes.REFUND_CHARGE);

                }
                if (showTransactions.Any(x => x.TransactionType == TransactionTypes.RefundCancelled))
                {
                    var refunds = showTransactions.Where(x => x.TransactionType == TransactionTypes.RefundCancelled);
                    showEntries = refunds.Sum(x => x.Amount);
                    paymentSummary.add("RefundCancelled", showEntries, 0, ChargableTypes.TRANSACTION);
                }

            }

            return paymentSummary;
        }
        private decimal calcCampingCost(int Id)
        {
            var total = 0M;
            var campingList = UserCamping.GetCampingForPaidUsers(Id);
            var camping = new Camping(Id);
            foreach (var userCamping in campingList)
            {
                switch (camping.ChargeType )
                {
                    case 2: // flat fee
                        total += camping.Costpershow;
                        break;

                    default:
                        total += (userCamping.Nights == camping.Nights ? camping.Costpershow : camping.Costpernight * userCamping.Nights);
                        break;
                }
            }

            return total;
        }
        public PaymentSummary CreateTotalsSummary(int ShowID, int UserID, ref Decimal totals)
        {
            ShowClasses classes = new ShowClasses();
            List<ShowClasses.Totals> totalsList = classes.getShowTotalsForUser(ShowID, UserID);

            PaymentSummary paymentSummary = new PaymentSummary();
            decimal standardClassAmount = 0;
            foreach (ShowClasses.Totals total in totalsList)
            {
                if (total.ChargeType == 1)
                {
                    standardClassAmount = total.ChargeRate;
                }
                paymentSummary.add(new ChargableItem(total.ChargeName, total.ChargeRate, total.Count, ChargableTypes.STANDARD, total.ChargeType));
            }

            UserShows us = new UserShows(UserID, ShowID);
            //
            // check if alt handlers are judging
            //
            if (us.HandlerType == 0)
            {
                List<AltHandler> altHList = AltHandler.GetAllAltHandlersList(us.Userid);
                foreach (AltHandler alt in altHList)
                {
                    if (Judge.isJudgeAtShow(alt.AltHandlerID, ShowID))
                    {
                        us.HandlerType = 2;
                    }
                }
            }

            Decimal val = 0;
            List<ShowDiscounts> list = ShowDiscounts.getDiscountsByType(ShowID, us.HandlerType);
            foreach (ShowDiscounts sd in list)
            {
                String tmp = "";
                if (sd.DiscountType == (int)DiscountTypes.Fixed)
                {
                    val = sd.Amount;
                    tmp = sd.DiscountName + " " + sd.SubTypeName;
                }
                else if (sd.DiscountType == (int)DiscountTypes.Member)
                {
                    tmp = sd.DiscountName + " discount " + sd.SubTypeName;
                    tmp = tmp.Replace("'n'", sd.Amount.ToString());
                    val = -(sd.Amount * standardClassAmount);
                }

                paymentSummary.add(tmp, val, 1, ChargableTypes.DISCOUNT);
                totals += val;
            }

            if (us.ID > -1)
            {
                Camping showCamping = new Camping(us.ShowID);
                UserCamping userCamping = new UserCamping(us.ID);
                int Nights = 0;
                if (userCamping.ID > -1)
                {
                    //ShowCharges chg = new ShowCharges(us.ShowID, 6); // Get Camping Charge for This Show

                    Nights = userCamping.PitchDetails[0].CampingDays.Split(',').Length;
                }
                paymentSummary.add("Camping", showCamping.Costpernight, Nights, ChargableTypes.CAMPING);
            }

            if (totals > 0)
            {
                paymentSummary.add("Outstanding Payments", totals, 0, ChargableTypes.TRANSACTION);
            }
            else if (totals < 0)
            {
                paymentSummary.add("Over Payments", totals, 0, ChargableTypes.TRANSACTION);
            }

            return paymentSummary;
        }
        public StatusCls CalcShow(Fpp.Core.Models.UserShowEntry showEntry)
        {
            DogClasses dc = new DogClasses();
            Shows show = new Shows(showEntry.ShowId);
            User currentUser = new User(showEntry.UserId);
            UserShows us = new UserShows(showEntry.UserId, showEntry.ShowId);

            decimal discountTotal = 0;
            var singleEntryId = getSingleEntry(showEntry.ShowId);

            //            Decimal amount = calcNewTotalForEntry(showEntry.ShowId, showEntry.UserId, singleEntryId, ref discountTotal, ref addNewTransaction);
            var showTransactions = Transaction.getTransactionForShowUser(us.ID.ToString("000000"));
            decimal fixedFees = 0M;

            var showEntries = DogClasses.calcCostOfEntry(showEntry.ShowId, showEntry.UserId);

            var entryCost = showEntries.Sum(x => x.Total);
            var noOfSingleEntries = showEntries.FirstOrDefault(x => x.ChargeTypeId == singleEntryId);

            var entriesDiscount = Math.Abs(ShowDiscounts.getDiscountFor(showEntry.ShowId, showEntry.UserId, "entry", (noOfSingleEntries != null ? noOfSingleEntries.Count : 0), showEntries).Amount);
            discountTotal = entriesDiscount;
            var currentEntryCost = (entryCost - entriesDiscount);
            if (!ShowDiscounts.discount100Percent(showEntry.ShowId, showEntry.UserId, "entry") && currentEntryCost > 0)
            {
                fixedFees = ShowDiscounts.getDiscountsByType(showEntry.ShowId, -1, (int)DiscountTypes.TransactionCharge).Sum(x => x.Amount);
                currentEntryCost += fixedFees;
            }

            var uc = new UserCamping(us.ID);
            if (uc.ID > -1)
            {
                var showCamping = new Camping(showEntry.ShowId);
                var campingCost = Camping.costOfEntry(showEntry.ShowId, showEntry.UserId);
                var campingDiscount = ShowDiscounts.getDiscountFor(showEntry.ShowId, showEntry.UserId, "camping", uc.Nights, null, campingCost);
                var discount = 0M;
                switch (campingDiscount.Type)
                {
                    case (int)CHARGES_SUBTYPES.percentCamping:
                        currentEntryCost = currentEntryCost - (campingCost - campingDiscount.Amount);
                        discountTotal += campingDiscount.Amount;
                        break;
                    case (int)CHARGES_SUBTYPES.n_NightsCamping:
                        if (campingDiscount.Amount == showCamping.Nights)
                        {
                            currentEntryCost = currentEntryCost + (campingCost - (showCamping.Costpershow));
                            discount = showCamping.Costpershow;
                        }
                        else
                        {
                            currentEntryCost = currentEntryCost + (campingCost - (campingDiscount.Amount * showCamping.Costpernight));
                            discount = campingDiscount.Amount * showCamping.Costpernight;
                        }
                        discountTotal += discount;
                        break;
                    default:
                        break;

                }

            }

            Decimal previousEntryCost = showTransactions
                            .Where(x => x.TransactionType == TransactionTypes.ShowEntry)
                            .Sum(x => x.Amount);
            Decimal whatHasBeenPaid = showTransactions
                            .Where(x => x.TransactionType == TransactionTypes.ShowEntryPayment)
                            .Sum(x => x.Amount);
            Decimal whatHasBeenRequested = showTransactions
                            .Where(x => x.TransactionType == TransactionTypes.RefundRequest)
                            .Sum(x => x.Amount);
            Decimal whatHasBeenRefund = showTransactions
                            .Where(x => x.TransactionType == TransactionTypes.RefundPaid || x.TransactionType == TransactionTypes.RefundCancelled)
                            .Sum(x => x.Amount);

            if (whatHasBeenPaid != 0)
            {
                if (Math.Abs(previousEntryCost) != currentEntryCost)
                {
                    currentEntryCost += fixedFees;
                }
            }
            StatusCls status = new StatusCls();
            status.PaymentSummary = CreatePaymentSummary(us);

            return status;
        }
        public static decimal calcNewTotalForEntry(UserShows userShow, Camping showCamping, int singleEntryId, ref decimal discountTotal, ref int addNewTransation)
        {
            var showTransactions = Transaction.getTransactionForShowUser(userShow.RefCode);
            decimal fixedFees = 0M;

            var showEntries = DogClasses.calcCostOfEntry(userShow.ShowID, userShow.Userid);

            var entryCost = showEntries.Sum(x => x.Total);
            var noOfSingleEntries = showEntries.FirstOrDefault(x => x.ChargeTypeId == singleEntryId);
            if (noOfSingleEntries == null)
            {
                AppException.LogEvent(string.Format("calcNewTotalForEntry - null no of single entries:ShowId={0}, UserId={1}, showRef={2}", userShow.ShowID, userShow.Userid, userShow.ID));
            }

            var entriesDiscount = Math.Abs(ShowDiscounts.getDiscountFor(userShow.ShowID, userShow.Userid, "entry", (noOfSingleEntries != null ? noOfSingleEntries.Count : 0), showEntries).Amount);
            discountTotal = entriesDiscount;
            var currentEntryCost = (entryCost - entriesDiscount);
            if (!(ShowDiscounts.discount100Percent(userShow.ShowID, userShow.Userid, "entry") && currentEntryCost == 0))
            {
                fixedFees = ShowDiscounts.getDiscountsByType(userShow.ShowID, -1, (int)DiscountTypes.TransactionCharge).Sum(x => x.Amount);
                currentEntryCost += fixedFees;
            }

            var uc = new UserCamping(userShow.ID);
            if (uc.ID > -1)
            {
                var campingCost = Camping.costOfEntry(userShow.ShowID, userShow.Userid);
                var campingDiscount = ShowDiscounts.getDiscountFor(userShow.ShowID, userShow.Userid, "camping", uc.Nights, null, campingCost);
                var discount = 0M;
                if (showCamping.ChargeType < 2)
                {
                    switch (campingDiscount.Type)
                    {
                        case (int)CHARGES_SUBTYPES.percentCamping:
                            currentEntryCost = currentEntryCost - (campingCost - campingDiscount.Amount);
                            discountTotal += campingDiscount.Amount;
                            break;
                        case (int)CHARGES_SUBTYPES.n_NightsCamping:
                            if (campingDiscount.Amount == showCamping.Nights)
                            {
                                currentEntryCost = currentEntryCost + (campingCost - (showCamping.Costpershow));
                                discount = showCamping.Costpershow;
                            }
                            else
                            {
                                currentEntryCost = currentEntryCost + (campingCost - (campingDiscount.Amount * showCamping.Costpernight));
                                discount = campingDiscount.Amount * showCamping.Costpernight;
                            }
                            discountTotal += discount;
                            break;
                        default:
                            currentEntryCost += campingCost;
                            break;
                    }
                }
                else if (showCamping.ChargeType == 2)
                {
                    currentEntryCost += campingCost;
                    if (campingDiscount.Type != 0)
                    {
                        discountTotal += campingCost - campingDiscount.Amount;
                        currentEntryCost += -campingDiscount.Amount;
                    }
                }
            }

            Decimal previousEntryCost = showTransactions
                            .Where(x => x.TransactionType == TransactionTypes.ShowEntry)
                            .Sum(x => x.Amount);
            Decimal whatHasBeenPaid = showTransactions
                            .Where(x => x.TransactionType == TransactionTypes.ShowEntryPayment)
                            .Sum(x => x.Amount);
            Decimal whatHasBeenRequested = showTransactions
                            .Where(x => x.TransactionType == TransactionTypes.RefundRequest)
                            .Sum(x => x.Amount);
            Decimal whatHasBeenRefund = showTransactions
                            .Where(x => x.TransactionType == TransactionTypes.RefundPaid || x.TransactionType == TransactionTypes.RefundPaid)
                            .Sum(x => x.Amount);

            if (previousEntryCost == 0)
            {
                addNewTransation = 1;
                return currentEntryCost;
            }

            if (whatHasBeenPaid == 0)
            {

                if (Math.Abs(previousEntryCost) == currentEntryCost)
                {
                    addNewTransation = 0;
                    return Math.Abs(previousEntryCost);
                }
                addNewTransation = 2;
                return currentEntryCost;
            }
            else
            {
                if (Math.Abs(previousEntryCost) == currentEntryCost)
                {
                    addNewTransation = 0;
                }
                else
                {
                    currentEntryCost += fixedFees;
                }
            }
            return currentEntryCost - (whatHasBeenPaid - (whatHasBeenRequested - whatHasBeenRefund));
        }
        public static Decimal GetOutstandingBalance(UserShows userShow)
        {
            decimal discountTotal = 0;
            int addNewTransaction = 1;
            var singleEntryId = getSingleEntry(userShow.ShowID);
            var showCamping = new Camping(userShow.ShowID);
            var amount = calcNewTotalForEntry(userShow, showCamping, singleEntryId, ref discountTotal, ref addNewTransaction);
            var isOffLineEntry = Transaction.hasShowAdminUpdateEntry(userShow.ShowID, userShow.Userid) == Transaction.ENTERED_BY.SHOW_ADMIN_ENTRY;

            if (isOffLineEntry)
            {
                var adminAmount = GetAdminCharge(userShow.ShowID);
                amount += adminAmount;
            }

            return amount;
        }
        private static decimal existingShowEntryCost(int ShowId, int UserId)
        {
            var userShow = new UserShows(UserId, ShowId);
            var showTransactions = Transaction.getTransactionForShowUser(userShow.RefCode);

            var showEntries = DogClasses.calcCostOfEntry(ShowId, UserId);
            var singleEntryId = getSingleEntry(ShowId);
            var entryCost = showEntries.Sum(x => x.Total);
            var noOfSingleEntries = showEntries.First(x => x.ChargeTypeId == singleEntryId);

            var entriesDiscount = Math.Abs(ShowDiscounts.getDiscountFor(ShowId, UserId, "entry", noOfSingleEntries.Count, showEntries).Amount);
            var discountTotal = entriesDiscount;
            var currentEntryCost = (entryCost - entriesDiscount);

            var uc = new UserCamping(userShow.ID);
            if (uc.ID > -1)
            {
                var showCamping = new Camping(ShowId);
                var campingCost = Camping.costOfEntry(ShowId, UserId);
                var campingDiscount = ShowDiscounts.getDiscountFor(ShowId, UserId, "camping", uc.Nights, null, campingCost);

                var discount = 0M;
                switch (campingDiscount.Type)
                {
                    case (int)CHARGES_SUBTYPES.percentCamping:
                        currentEntryCost = currentEntryCost - (campingCost - campingDiscount.Amount);
                        discountTotal += campingDiscount.Amount;
                        break;
                    case (int)CHARGES_SUBTYPES.n_NightsCamping:
                        if (campingDiscount.Amount == showCamping.Nights)
                        {
                            currentEntryCost = currentEntryCost + (campingCost - (showCamping.Costpershow));
                            discount = showCamping.Costpershow;
                        }
                        else
                        {
                            currentEntryCost = currentEntryCost + (campingCost - (campingDiscount.Amount * showCamping.Costpernight));
                            discount = campingDiscount.Amount * showCamping.Costpernight;
                        }
                        discountTotal += discount;
                        break;
                    default:
                        break;

                }
            }

            Decimal previousEntry = showTransactions
                            .Where(x => x.TransactionType == TransactionTypes.ShowEntry)
                            .Sum(x => x.Amount);
            Decimal whatHasBeenPaid = showTransactions
                            .Where(x => x.TransactionType == TransactionTypes.ShowEntryPayment)
                            .Sum(x => x.Amount);
            //Decimal whatHasBeenRequested = showTransactions
            //                .Where(x => x.TransactionType == TransactionTypes.RefundRequest)
            //                .Sum(x => x.Amount);
            //Decimal whatHasBeenRefund = showTransactions
            //                .Where(x => x.TransactionType == TransactionTypes.RefundPaid)
            //                .Sum(x => x.Amount);
            return Math.Abs(previousEntry) - whatHasBeenPaid - discountTotal;
        }