Beispiel #1
0
        public Discount SaveInfo()
        {
            //discou tn type
            DiscountTypeEnum discountType = (DiscountTypeEnum)int.Parse(this.ddlDiscountType.SelectedItem.Value);

            //requirements
            DiscountRequirementEnum discountRequirement = (DiscountRequirementEnum)int.Parse(this.ddlDiscountRequirement.SelectedItem.Value);

            int[] restrictedProductVariantIds = new int[0];
            if (discountRequirement == DiscountRequirementEnum.HadPurchasedAllOfTheseProductVariants || discountRequirement == DiscountRequirementEnum.HadPurchasedOneOfTheseProductVariants)
            {
                restrictedProductVariantIds = ParseListOfRestrictedProductVariants(txtRestrictedProductVariants.Text);
            }
            decimal requirementSpentAmount = txtRequirementSpentAmount.Value;

            int requirementBillingCountryIs  = int.Parse(this.ddlRequirementBillingCountryIs.SelectedItem.Value);
            int requirementShippingCountryIs = int.Parse(this.ddlRequirementShippingCountryIs.SelectedItem.Value);

            //limitation
            DiscountLimitationEnum discountLimitation = (DiscountLimitationEnum)int.Parse(this.ddlDiscountLimitation.SelectedItem.Value);
            int limitationTimes = txtLimitationTimes.Value;

            string  name               = txtName.Text.Trim();
            bool    usePercentage      = cbUsePercentage.Checked;
            decimal discountPercentage = txtDiscountPercentage.Value;
            decimal discountAmount     = txtDiscountAmount.Value;
            bool    requiresCouponCode = cbRequiresCouponCode.Checked;
            string  couponCode         = txtCouponCode.Text.Trim();

            //dates
            if (!ctrlStartDatePicker.SelectedDate.HasValue)
            {
                throw new NopException("Start date is not set");
            }
            DateTime discountStartDate = ctrlStartDatePicker.SelectedDate.Value;

            if (!ctrlEndDatePicker.SelectedDate.HasValue)
            {
                throw new NopException("End date is not set");
            }
            DateTime discountEndDate = ctrlEndDatePicker.SelectedDate.Value;

            discountStartDate = DateTime.SpecifyKind(discountStartDate, DateTimeKind.Utc);
            discountEndDate   = DateTime.SpecifyKind(discountEndDate, DateTimeKind.Utc);

            Discount discount = DiscountManager.GetDiscountById(this.DiscountId);

            if (discount != null)
            {
                discount = DiscountManager.UpdateDiscount(discount.DiscountId,
                                                          discountType,
                                                          discountRequirement,
                                                          requirementSpentAmount,
                                                          requirementBillingCountryIs,
                                                          requirementShippingCountryIs,
                                                          discountLimitation,
                                                          limitationTimes,
                                                          name,
                                                          usePercentage,
                                                          discountPercentage,
                                                          discountAmount,
                                                          discountStartDate,
                                                          discountEndDate,
                                                          requiresCouponCode,
                                                          couponCode,
                                                          discount.Deleted);

                //discount requirements
                foreach (CustomerRole customerRole in discount.CustomerRoles)
                {
                    CustomerManager.RemoveDiscountFromCustomerRole(customerRole.CustomerRoleId, discount.DiscountId);
                }
                foreach (int customerRoleId in CustomerRoleMappingControl.SelectedCustomerRoleIds)
                {
                    CustomerManager.AddDiscountToCustomerRole(customerRoleId, discount.DiscountId);
                }

                foreach (ProductVariant pv in ProductManager.GetProductVariantsRestrictedByDiscountId(discount.DiscountId))
                {
                    DiscountManager.RemoveDiscountRestriction(pv.ProductVariantId, discount.DiscountId);
                }
                foreach (int productVariantId in restrictedProductVariantIds)
                {
                    DiscountManager.AddDiscountRestriction(productVariantId, discount.DiscountId);
                }
            }
            else
            {
                discount = DiscountManager.InsertDiscount(discountType,
                                                          discountRequirement,
                                                          requirementSpentAmount,
                                                          requirementBillingCountryIs,
                                                          requirementShippingCountryIs,
                                                          discountLimitation,
                                                          limitationTimes,
                                                          name,
                                                          usePercentage,
                                                          discountPercentage,
                                                          discountAmount,
                                                          discountStartDate,
                                                          discountEndDate,
                                                          requiresCouponCode,
                                                          couponCode,
                                                          false);

                //discount requirements
                foreach (int customerRoleId in CustomerRoleMappingControl.SelectedCustomerRoleIds)
                {
                    CustomerManager.AddDiscountToCustomerRole(customerRoleId, discount.DiscountId);
                }

                foreach (int productVariantId in restrictedProductVariantIds)
                {
                    DiscountManager.AddDiscountRestriction(productVariantId, discount.DiscountId);
                }
            }

            return(discount);
        }
        public Discount SaveInfo()
        {
            DateTime discountStartDate = DateTime.MinValue;
            DateTime discountEndDate   = DateTime.MinValue;

            if (!DateTime.TryParse(txtStartDate.Text, out discountStartDate))
            {
                throw new NopException("Start date is not set");
            }
            if (!DateTime.TryParse(txtEndDate.Text, out discountEndDate))
            {
                throw new NopException("End date is not set");
            }
            discountStartDate = DateTime.SpecifyKind(discountStartDate, DateTimeKind.Utc);
            discountEndDate   = DateTime.SpecifyKind(discountEndDate, DateTimeKind.Utc);

            Discount discount = DiscountManager.GetDiscountByID(this.DiscountID);

            if (discount != null)
            {
                discount = DiscountManager.UpdateDiscount(discount.DiscountID,
                                                          (DiscountTypeEnum)int.Parse(this.ddlDiscountType.SelectedItem.Value),
                                                          (DiscountRequirementEnum)int.Parse(this.ddlDiscountRequirement.SelectedItem.Value),
                                                          txtName.Text,
                                                          cbUsePercentage.Checked,
                                                          txtDiscountPercentage.Value,
                                                          txtDiscountAmount.Value,
                                                          discountStartDate,
                                                          discountEndDate,
                                                          cbRequiresCouponCode.Checked,
                                                          txtCouponCode.Text.Trim(),
                                                          discount.Deleted);

                foreach (CustomerRole customerRole in discount.CustomerRoles)
                {
                    CustomerManager.RemoveDiscountFromCustomerRole(customerRole.CustomerRoleID, discount.DiscountID);
                }
                foreach (int customerRoleID in CustomerRoleMappingControl.SelectedCustomerRoleIDs)
                {
                    CustomerManager.AddDiscountToCustomerRole(customerRoleID, discount.DiscountID);
                }
            }
            else
            {
                discount = DiscountManager.InsertDiscount((DiscountTypeEnum)int.Parse(this.ddlDiscountType.SelectedItem.Value),
                                                          (DiscountRequirementEnum)int.Parse(this.ddlDiscountRequirement.SelectedItem.Value),
                                                          txtName.Text,
                                                          cbUsePercentage.Checked,
                                                          txtDiscountPercentage.Value,
                                                          txtDiscountAmount.Value,
                                                          discountStartDate,
                                                          discountEndDate,
                                                          cbRequiresCouponCode.Checked,
                                                          txtCouponCode.Text.Trim(),
                                                          false);

                foreach (int customerRoleID in CustomerRoleMappingControl.SelectedCustomerRoleIDs)
                {
                    CustomerManager.AddDiscountToCustomerRole(customerRoleID, discount.DiscountID);
                }
            }

            return(discount);
        }