コード例 #1
0
ファイル: DiscountInfo.ascx.cs プロジェクト: terry2012/DSV
        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);
        }