protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Purchasable == null)
            {
                PurchaseSummary.Visible     = false;
                GeneralErrorMessage.Visible = true;

                WebForm.EnableDisableNextButton(false);

                return;
            }

            PurchaseSummary.Visible     = true;
            GeneralErrorMessage.Visible = false;
            Shipping.Visible            = Purchasable.RequiresShipping;

            PurchaseDiscounts.DataSource = Purchasable.Discounts;
            PurchaseDiscounts.DataBind();

            PurchaseItems.DataSource = Purchasable.Items;
            PurchaseItems.DataBind();

            QuoteId.Value = Purchasable.Quote.Id.ToString();

            if (Purchasable.RequiresShipping && Purchasable.ShipToAddress != null)
            {
                ShippingCity.Text          = Purchasable.ShipToAddress.City;
                ShippingCountry.Text       = Purchasable.ShipToAddress.Country;
                ShippingName.Text          = Purchasable.ShipToAddress.Name;
                ShippingPostalCode.Text    = Purchasable.ShipToAddress.PostalCode;
                ShippingStateProvince.Text = Purchasable.ShipToAddress.StateOrProvince;
                ShippingAddressLine1.Text  = Purchasable.ShipToAddress.Line1;
                ShippingAddressLine2.Text  = Purchasable.ShipToAddress.Line2;
            }
        }
        protected void ApplyDiscount_OnClick(object sender, EventArgs e)
        {
            if (Purchasable == null)
            {
                return;
            }

            var discountCode = DiscountCode.Value;

            var discountCodeValidationResult = DiscountCodeValidationResult.ValidateDiscountCode(ServiceContext, Purchasable.Quote.Id, discountCode);

            if (!discountCodeValidationResult.IsValid)
            {
                DiscountErrorAlreadyApplied.Visible = discountCodeValidationResult.ErrorCode ==
                                                      DiscountCodeValidationResult.DiscountErrorCode.AlreadyApplied ||
                                                      discountCodeValidationResult.DiscountErrors.Any(
                    o => o.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.AlreadyApplied);
                DiscountErrorCodeNotSpecified.Visible = discountCodeValidationResult.ErrorCode ==
                                                        DiscountCodeValidationResult.DiscountErrorCode.CodeNotSpecified ||
                                                        discountCodeValidationResult.DiscountErrors.Any(
                    o => o.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.CodeNotSpecified);
                DiscountErrorDoesNotExist.Visible = discountCodeValidationResult.ErrorCode ==
                                                    DiscountCodeValidationResult.DiscountErrorCode.DoesNotExist ||
                                                    discountCodeValidationResult.DiscountErrors.Any(
                    o => o.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.DoesNotExist);
                DiscountErrorInvalidDiscount.Visible = discountCodeValidationResult.ErrorCode ==
                                                       DiscountCodeValidationResult.DiscountErrorCode.InvalidDiscountConfiguration ||
                                                       discountCodeValidationResult.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.QuoteNotFound ||
                                                       discountCodeValidationResult.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.UpdateFailed ||
                                                       discountCodeValidationResult.DiscountErrors.Any(
                    o => o.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.InvalidDiscountConfiguration);
                DiscountErrorMaximumRedemptions.Visible = discountCodeValidationResult.ErrorCode ==
                                                          DiscountCodeValidationResult.DiscountErrorCode.MaximumRedemptions ||
                                                          discountCodeValidationResult.DiscountErrors.Any(
                    o => o.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.MaximumRedemptions);
                DiscountErrorMinimumAmountNotMet.Visible = discountCodeValidationResult.ErrorCode ==
                                                           DiscountCodeValidationResult.DiscountErrorCode.MinimumAmountNotMet ||
                                                           discountCodeValidationResult.DiscountErrors.Any(
                    o => o.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.MinimumAmountNotMet);
                DiscountErrorUnknown.Visible    = discountCodeValidationResult.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.Unknown || (discountCodeValidationResult.ErrorCode == 0 && !discountCodeValidationResult.DiscountErrors.Any());
                DiscountErrorZeroAmount.Visible = discountCodeValidationResult.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.ZeroAmount ||
                                                  discountCodeValidationResult.DiscountErrors.Any(
                    o => o.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.ZeroAmount);
                DiscountErrorNotApplicable.Visible = discountCodeValidationResult.ErrorCode ==
                                                     DiscountCodeValidationResult.DiscountErrorCode.NotApplicable ||
                                                     discountCodeValidationResult.DiscountErrors.Any(
                    o =>
                    o.ErrorCode == DiscountCodeValidationResult.DiscountErrorCode.NotApplicable);

                return;
            }

            DiscountErrorAlreadyApplied.Visible      = false;
            DiscountErrorCodeNotSpecified.Visible    = false;
            DiscountErrorDoesNotExist.Visible        = false;
            DiscountErrorInvalidDiscount.Visible     = false;
            DiscountErrorMaximumRedemptions.Visible  = false;
            DiscountErrorMinimumAmountNotMet.Visible = false;
            DiscountErrorUnknown.Visible             = false;
            DiscountErrorZeroAmount.Visible          = false;

            try
            {
                // Add new discount code to existing discount codes and update quote, plugins will process the code.
                var updateContext = new OrganizationServiceContext(new OrganizationService("Xrm"));
                var quoteUpdate   = new Entity("quote")
                {
                    Id = Purchasable.Quote.Id
                };
                var updateDiscountCodes = string.IsNullOrWhiteSpace(discountCodeValidationResult.ExistingDiscountCodes) ? discountCode : string.Format("{0},{1}", discountCodeValidationResult.ExistingDiscountCodes, discountCode);
                quoteUpdate["adx_discountcodes"] = updateDiscountCodes;
                updateContext.Attach(quoteUpdate);
                updateContext.UpdateObject(quoteUpdate);
                updateContext.SaveChanges();
            }
            catch (Exception ex)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, ex.ToString());
            }

            Target = GetTargetEntityReference();

            if (Target == null)
            {
                return;
            }

            var options = IsPostBack
                                ? GetPostedOptions()
                                : Enumerable.Empty <IPurchasableItemOptions>();

            Guid quoteId;

            if (IsPostBack && Guid.TryParse(QuoteId.Value, out quoteId))
            {
                WebForm.CurrentSessionHistory.QuoteId = quoteId;
            }

            var dataAdapter = CreatePurchaseDataAdapter(Target, CurrentStepEntityPrimaryKeyLogicalName);

            var quoteProducts = ServiceContext.CreateQuery("quotedetail").Where(q => q.GetAttributeValue <EntityReference>("quoteid") == Purchasable.Quote).ToArray();

            foreach (var quoteProduct in quoteProducts)
            {
                ServiceContext.TryRemoveFromCache(quoteProduct);
            }

            Purchasable = dataAdapter.Select(options);

            PurchaseDiscounts.DataSource = Purchasable.Discounts;
            PurchaseDiscounts.DataBind();

            PurchaseItems.DataSource = Purchasable.Items;
            PurchaseItems.DataBind();

            DiscountCode.Value = string.Empty;
        }
Beispiel #3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            Target = GetTargetEntityReference();

            if (Target == null)
            {
                Payment.Visible             = false;
                GeneralErrorMessage.Visible = true;

                return;
            }

            Guid quoteId;

            if (IsPostBack && Guid.TryParse(QuoteId.Value, out quoteId))
            {
                WebForm.CurrentSessionHistory.QuoteId = quoteId;
            }

            var dataAdapterDependencies = new Adxstudio.Xrm.Commerce.PortalConfigurationDataAdapterDependencies(PortalName, Request.RequestContext);
            var dataAdapter             = CreatePurchaseDataAdapter(Target, CurrentStepEntityPrimaryKeyLogicalName);

            Purchasable = dataAdapter.Select();

            if (Purchasable == null)
            {
                Payment.Visible             = false;
                GeneralErrorMessage.Visible = true;

                return;
            }

            // If the session quote is not the purchase quote, update and persist the session, as
            // there won't necessarily be a postback to save the session later.
            if (WebForm.CurrentSessionHistory.QuoteId != Purchasable.Quote.Id)
            {
                WebForm.CurrentSessionHistory.QuoteId = Purchasable.Quote.Id;

                WebForm.SaveSessionHistory(dataAdapterDependencies.GetServiceContext());
            }

            QuoteId.Value = Purchasable.Quote.Id.ToString();

            if (Purchasable.TotalAmount == 0)
            {
                var portal  = PortalCrmConfigurationManager.CreatePortalContext(PortalName);
                var context = dataAdapterDependencies.GetServiceContext();
                var quote   = context.CreateQuery("quote").First(q => q.GetAttributeValue <Guid>("quoteid") == Purchasable.Quote.Id);
                var adapter = new CoreDataAdapter(portal, context);
                var status  = quote.GetAttributeValue <OptionSetValue>("statecode").Value;

                if (status != (int)QuoteState.Active)
                {
                    adapter.SetState(quote.ToEntityReference(), (int)QuoteState.Active, (int)QuoteStatusCode.InProgressActive);
                }

                if (status != (int)QuoteState.Won)
                {
                    adapter.WinQuote(quote.ToEntityReference());
                }

                adapter.CovertQuoteToSalesOrder(quote.ToEntityReference());

                dataAdapter.CompletePurchase();

                SetAttributeValuesAndSave();

                MoveToNextStep();

                return;
            }

            if (Paid)
            {
                dataAdapter.CompletePurchase();

                SetAttributeValuesAndSave();

                MoveToNextStep();

                return;
            }

            Payment.Visible             = true;
            GeneralErrorMessage.Visible = false;

            if (IsPaymentError)
            {
                SetErrorFields();
            }

            SetMerchantShippingFields(ServiceContext.CreateQuery("quote").FirstOrDefault(q => q.GetAttributeValue <Guid>("quoteid") == Purchasable.Quote.Id));

            if (IsPaymentAuthorizeNet || IsPaymentDemo)
            {
                CreditCardPaymentPanel.Visible = true;
                PayPalPaymentPanel.Visible     = false;

                SetMerchantFields();

                PopulateContactInfo(Contact);

                EnableTestMode(TestModeEnabled);

                PurchaseDiscounts.DataSource = Purchasable.Discounts;
                PurchaseDiscounts.DataBind();

                PurchaseItems.DataSource = Purchasable.Items.Where(item => item.IsSelected && item.Quantity > 0);
                PurchaseItems.DataBind();
            }
            else if (IsPaymentPaypal)
            {
                PayPalPaymentPanel.Visible     = true;
                CreditCardPaymentPanel.Visible = false;

                PayPalPurchaseDiscounts.DataSource = Purchasable.Discounts;
                PayPalPurchaseDiscounts.DataBind();

                PayPalPurchaseItems.DataSource = Purchasable.Items.Where(item => item.IsSelected && item.Quantity > 0);
                PayPalPurchaseItems.DataBind();
            }
        }