Example #1
0
        private static void UpdateExistingCode(ObjectContext context, DiscountCode existingCode, DiscountCodeDtoV2 updatedCode)
        {
            int discountTypeId = LookupDiscountType(context, updatedCode.DiscountType);

            existingCode.Description = string.IsNullOrWhiteSpace(updatedCode.Description) ? existingCode.Description : updatedCode.Description;
            existingCode.DiscountTypeId = discountTypeId;
            existingCode.DiscountValue = Convert.ToDecimal(updatedCode.DiscountValue, CultureInfo.InvariantCulture);
            existingCode.StartDate = string.IsNullOrEmpty(updatedCode.StartDate) ? existingCode.StartDate : DateTime.Parse(updatedCode.StartDate, CultureInfo.InvariantCulture);
            existingCode.EndDate = string.IsNullOrEmpty(updatedCode.StartDate) ? existingCode.EndDate : DateTime.Parse(updatedCode.EndDate, CultureInfo.InvariantCulture);
            existingCode.IsEnabled = string.IsNullOrWhiteSpace(updatedCode.IsEnabled) ? existingCode.IsEnabled : bool.Parse(updatedCode.IsEnabled);
            existingCode.IsGroupCode = string.IsNullOrWhiteSpace(updatedCode.IsGroupCode) ? existingCode.IsGroupCode : bool.Parse(updatedCode.IsGroupCode);
            existingCode.RequiredGroupSize = string.IsNullOrWhiteSpace(updatedCode.RequiredGroupSize) ? existingCode.RequiredGroupSize : int.Parse(updatedCode.RequiredGroupSize, CultureInfo.InvariantCulture);
        }
Example #2
0
        public static void SetDiscountCodes(ObjectContext context, EventV2 source, Event theEvent)
        {
            foreach (var eventCode in theEvent.EventDiscountCodes.ToList())
            {
                context.DeleteObject(eventCode);
            }

            if (source.DiscountCodes == null || !source.DiscountCodes.Any())
                return;

            var discountCodes = context.CreateObjectSet<DiscountCode>();

            foreach (var discount in source.DiscountCodes)
            {
                if (string.IsNullOrEmpty(discount.Code))
                    throw new BusinessException("Code for discount code is required");

                DiscountCode code = new DiscountCode();

                //Check if org unit discount code exists
                if (theEvent.OrgUnitId.HasValue)
                    code = FindOrgUnitDiscountCode(context, theEvent, discount);

                if (code == null)
                {
                    //Check if event discount code exists
                    code = FindEventDiscountCode(context, discount);
                    if (code == null)
                    {
                        //If no discount with the given code is found, create a new event discount code
                        if (string.IsNullOrEmpty(discount.DiscountType))
                            throw new BusinessException("Unable to add discount code.  DiscountType is required");
                        if (string.IsNullOrEmpty(discount.DiscountValue))
                            throw new BusinessException("Unable to add discount code.  DiscountValue is required");

                        code = CreateEventDiscountCode(context, discount);

                        if (code.StartDate.HasValue && code.EndDate.HasValue && code.EndDate < code.StartDate)
                            throw new BusinessException("Unable to add discount code.  EndDate must be after StartDate");

                        if (theEvent.EventDiscountCodes.Any(c => c.DiscountCode.Code.ToUpper(CultureInfo.InvariantCulture) == code.Code.ToUpper(CultureInfo.InvariantCulture)))
                            throw new BusinessException("Event discount codes must be unique");

                        discountCodes.AddObject(code);

                        var codeWithId = discountCodes.FirstOrDefault(d => d.Code == code.Code);
                        code.Id = codeWithId == null ? 0 : codeWithId.Id;
                    }
                    else
                    {
                        //If event discount code exists, update existing discount code
                        UpdateExistingCode(context, code, discount);
                    }
                }
                else
                {
                    //If org unit discount code exists, just attach existing org unit code to event.
                    //Org unit codes are not editable from the Event API.
                    var codeWithId = discountCodes.FirstOrDefault(d => d.Code == code.Code);
                    code.Id = codeWithId == null ? 0 : codeWithId.Id;
                }
                theEvent.EventDiscountCodes.Add(new EventDiscountCode
                {
                    Event = theEvent,
                    DiscountCode = code,
                    DiscountCodeId = code.Id,
                });
            }
        }
Example #3
0
        private static DiscountCode CreateEventDiscountCode(ObjectContext context, DiscountCodeDtoV2 code)
        {
            int discountTypeId = LookupDiscountType(context, code.DiscountType);

            var discountCode = new DiscountCode
            {
                Code = code.Code,
                Description = code.Description,
                DiscountTypeId = discountTypeId,
                DiscountValue = Convert.ToDecimal(code.DiscountValue, CultureInfo.InvariantCulture),
                StartDate = string.IsNullOrEmpty(code.StartDate) ? new DateTime?() : DateTime.Parse(code.StartDate, CultureInfo.InvariantCulture),
                EndDate = string.IsNullOrEmpty(code.StartDate) ? new DateTime?() : DateTime.Parse(code.EndDate, CultureInfo.InvariantCulture),
                IsEnabled = bool.Parse(code.IsEnabled),
                IsGroupCode = bool.Parse(code.IsGroupCode),
                IsFromOrgUnit = false,
                RequiredGroupSize = int.Parse(code.RequiredGroupSize, CultureInfo.InvariantCulture),
            };

            context.AddObject("DiscountCodes", discountCode);

            return discountCode;
        }
Example #4
0
        public static void SetOrgUnitDiscountCodes(ObjectContext context, OrgUnitV2 source, OrgUnit orgUnit)
        {
            //ignore null values
            if (source.OrgUnitDiscountCodes == null)
                return;

            try
            {
                var existingOrgUnitDiscountCodes = orgUnit.OrgUnitDiscountCodes.ToArray();
                foreach (var item in existingOrgUnitDiscountCodes)
                    context.DeleteObject(item);

                var discountCodes = context.CreateObjectSet<DiscountCode>();
                var orgUnitDiscountCodes = new List<OrgUnitDiscountCode>();

                foreach (var item in source.OrgUnitDiscountCodes)
                {
                    //Check if discount code exists
                    var discountCode = discountCodes.FirstOrDefault(i => i.Code == item.Code);

                    //If the discount code doesn't exist, create a new one
                    if (discountCode == null)
                    {
                        discountCode = new DiscountCode()
                        {
                            Code = item.Code,
                            Description = item.Description,
                            DiscountTypeId = LookupDiscountType(context, item.DiscountType),
                            IsEnabled = item.IsEnabled.ToString().ToUpperInvariant() == "TRUE" ? true : false,
                            IsFromOrgUnit = true,
                            IsGroupCode = item.IsGroupCode.ToString().ToUpperInvariant() == "TRUE" ? true : false,
                        };
                        int requiredGroupSize;
                        if (int.TryParse(item.RequiredGroupSize.ToString(), out requiredGroupSize))
                            discountCode.RequiredGroupSize = requiredGroupSize;

                        decimal discountValue;
                        if (decimal.TryParse(item.DiscountValue, out discountValue))
                            discountCode.DiscountValue = discountValue;

                        DateTime startDate;
                        if (DateTime.TryParse(item.StartDate, out startDate))
                            discountCode.StartDate = startDate;

                        DateTime endDate;
                        if (DateTime.TryParse(item.EndDate, out endDate))
                            discountCode.EndDate = endDate;

                        discountCodes.AddObject(discountCode);
                    }
                    else
                    {
                        //Ensure existing discount code is not an Event discount code
                        if (!discountCode.IsFromOrgUnit)
                        {
                            throw new BusinessException("Discount code already in use by an Event.");
                        }
                        UpdateExistingCode(context, discountCode, item);
                    }

                    var newOrgUnitDiscountCode = new OrgUnitDiscountCode();
                    var codeWithId = discountCodes.FirstOrDefault(d => d.Code == discountCode.Code);

                    newOrgUnitDiscountCode.DiscountCodeId = codeWithId == null ? 0 : codeWithId.Id;
                    newOrgUnitDiscountCode.DiscountCode = discountCode;

                    int orgUnitId;
                    if (int.TryParse(source.CopiedInternalId, out orgUnitId))
                        newOrgUnitDiscountCode.OrgUnitId = orgUnitId;

                    orgUnitDiscountCodes.Add(newOrgUnitDiscountCode);
                }

                orgUnit.OrgUnitDiscountCodes = orgUnitDiscountCodes;
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing OrgUnitDiscountCodes for orgUnit '" + orgUnit.Name + "' - " + ex.Message, ex);
            }
        }