Code as it is currently
This is all of the code that performs the functionality. The only piece not included here is the new template "AddQuoteItemsToCart.cshtml".
namespace Boxx.Web.CustomModules.SalesReps
{
/// <summary>
/// Custom module to deal with Guru specific tasks.
/// </summary>
[AddInName("SalesReps")]
public class Frontend : ContentModule
{
private User _user;
internal const string ModuleName = "SalesReps";
/// <summary>
/// Returns the HTML for this module.
/// </summary>
public override string GetContent()
{
switch (RenderMode)
{
case RenderMode.SaveConfiguration:
return SaveConfiguration();
case RenderMode.ManageConfigurations:
return ManageConfigurations();
case RenderMode.ProductList:
return HandleProductList();
default:
throw new Exception(string.Format("Unknown RenderMode: {0}.", RenderMode));
}
}
private string CheckSalesforceParameters()
{
var test = Request.QueryString;
var salesforceProvider = new SalesforceProvider();
if (!string.IsNullOrEmpty(Request.QueryString["ClearSession"]))
{
SalesforceSettings.ClearSession();
Response.Redirect(string.Format("/Default.aspx?ID={0}&QuoteId={1}&OpportunityId={2}&ContactId={3}", Pageview.ID, Request.QueryString["QuoteId"], Request.QueryString["OpportunityId"], Request.QueryString["ContactId"]));
return string.Empty;
}
var requestedSettings = SalesforceSettings.GetFromQueryString();
var currentSettings = SalesforceSettings.GetCurrent();
var existingQuoteId = Request.QueryString["QuoteId"];
var refreshQuoteId = Request.QueryString["RefreshQuoteId"];
var template = new Template("null");
if (!(string.IsNullOrEmpty(refreshQuoteId)))
{
template = new Template(string.Format("{0}/{1}", "SalesReps", "AddQuoteItemsToCart.cshtml"));
}
else
{
template = new Template(string.Format("{0}/{1}", "SalesReps", "SalesforceInfo.cshtml"));
}
if(requestedSettings == null && !string.IsNullOrEmpty(existingQuoteId) && string.IsNullOrEmpty(refreshQuoteId))
{
var result = RedirectToExistingQuote(existingQuoteId, salesforceProvider);
if (!string.IsNullOrEmpty(result))
{
template.SetTag("Salesforce:CantFindQuote", true);
}
}
var cartMismatch = requestedSettings != null && requestedSettings.DifferentCartExists() && Dynamicweb.eCommerce.Common.Context.Cart != null;
var hasValidSettings = currentSettings != null || requestedSettings != null;
var activeSettings = requestedSettings ?? currentSettings;
template.SetTag("Salesforce:HasCartMismatch", cartMismatch);
template.SetTag("Salesforce:HasValidSettings", hasValidSettings);
template.SetTag("Salesforce:SettingsAreValid", requestedSettings != null && requestedSettings.IsValid);
template.SetTag("Salesforce:HasOpportunityId", !string.IsNullOrEmpty(Request.QueryString["OpportunityId"]));
template.SetTag("Salesforce:HasContactId", !string.IsNullOrEmpty(Request.QueryString["ContactId"]));
template.SetTag("Salesforce:HasAccountId", !string.IsNullOrEmpty(Request.QueryString["AccountId"]));
template.SetTag("Salesforce:HasBillingAddressId", activeSettings?.HasBillingAddressId.ToString());
template.SetTag("Salesforce:HasShippingAddressId", activeSettings?.HasShippingAddressId.ToString());
var quoteId = requestedSettings != null ? requestedSettings.QuoteId : currentSettings?.QuoteId;
var existingId = Request.QueryString["ExistingConfigurationId"];
if (!string.IsNullOrEmpty(quoteId) && string.IsNullOrEmpty(existingId) && string.IsNullOrEmpty(refreshQuoteId))
{
AddLinesFromSalesforceQuote(salesforceProvider, template, quoteId);
}else if(!string.IsNullOrEmpty(refreshQuoteId) && string.IsNullOrEmpty(existingId))
{
if(!Convert.ToBoolean(Request.QueryString["pass"]))
{
var settings = salesforceProvider.GetSettingsByQuoteId(refreshQuoteId);
var url = string.Format("/Default.aspx?ID={0}&OpportunityId={1}&AccountId={2}&ContactId={3}&BillingAddressAccountId={4}&ShippingAddressAccountId={5}&QuoteId={6}&BillingAddressContactId={7}&ShippingAddressContactId={8}&RefreshQuoteId={9}&RefreshAddresses=true&addtocart=true&pass=true", Request.QueryString["ID"], settings.OpportunityId, settings.AccountId, settings.ContactId, settings.BillingAddressAccountId, settings.ShippingAddressAccountId, settings.QuoteId, settings.BillingAddressContactId, settings.ShippingAddressContactId, settings.QuoteId);
Response.Redirect(url, true);
}
else
{
AddItemsFromSalesforceQuoteToShoppingCart(template, salesforceProvider, refreshQuoteId);
}
}
if (!cartMismatch && requestedSettings != null && requestedSettings.IsValid)
{
SalesforceSettings.StoreInSession(requestedSettings);
}
if (!cartMismatch)
{
// There's an existing configuration ID which means we can send the user to the configuration page directly.
if (!string.IsNullOrEmpty(existingId))
{
var repo = new GuruConfigurationRepository();
CloneAndRedirect(repo, existingId);
return string.Empty;
}
}
salesforceProvider.Dispose();
return template.Output();
}
private static void AddItemsFromSalesforceQuoteToShoppingCart(Template template, SalesforceProvider salesforceProvider, string quoteId)
{
var quote = salesforceProvider.GetQuoteById(quoteId);
var quoteLines = salesforceProvider.GetQuoteLines(quoteId);
var settings = salesforceProvider.GetSettingsByQuoteId(quoteId);
var targetAddress = salesforceProvider.GetContactWithAddress(quote.ContactId);
var billingAddress = salesforceProvider.GetContactWithAddress(settings.BillingAddressContactId);
var account = salesforceProvider.GetAccountById(targetAddress.AccountId);
var address = targetAddress.MailingAddress;
template.SetTag("Salesforce:QuoteHasLines", quoteLines.Any());
template.SetTag("Salesforce:QuoteIsInternational", quote.IsInternational__cSpecified ? quote.IsInternational__c.Value.ToString() : "");
if (Context.Cart != null)
{
// redirect this to confirm cart deletion
// but for now
Context.Cart.Delete();
}
var Cart = new Order();
Cart.IsCart = true;
template.SetTag("Ecom:Order.Customer.Address", billingAddress.MailingAddress.street);
template.SetTag("Ecom:Order.Customer.City", billingAddress.MailingAddress.city);
template.SetTag("Ecom:Order.Customer.Region", billingAddress.MailingAddress.state);
template.SetTag("Ecom:Order.Customer.Zip", billingAddress.MailingAddress.postalCode);
Cart.CustomerFirstName = billingAddress.FirstName;
Cart.CustomerSurname = billingAddress.LastName;
Cart.CustomerName = billingAddress.FirstName + " " + billingAddress.LastName;
Cart.CustomerAddress = billingAddress.MailingAddress.street;
Cart.CustomerCity = billingAddress.MailingAddress.city;
Cart.CustomerZip = billingAddress.MailingAddress.postalCode;
Cart.CustomerRegion = billingAddress.MailingAddress.state;
Cart.CustomerCountry = billingAddress.MailingAddress.country;
Cart.DeliveryFirstName = targetAddress.FirstName;
Cart.DeliverySurname = targetAddress.LastName;
Cart.DeliveryAddress = address.street;
Cart.DeliveryCity = address.city;
Cart.DeliveryRegion = address.state;
Cart.DeliveryZip = address.postalCode;
Cart.DeliveryCountry = address.country;
Cart.DeliveryCountryCode = address.countryCode;
Cart.DeliveryName = targetAddress.FirstName + " " + targetAddress.LastName;
//Cart.OrderFields["QuoteId"] = Convert.ToInt32(quoteId);
var quoteIdField = Cart.OrderFieldValues.FirstOrDefault(x => x.OrderField.SystemName == "QuoteId");
quoteIdField.Value = quoteId;
var opportunityIdField = Cart.OrderFieldValues.FirstOrDefault(x => x.OrderField.SystemName == "OpportunityId");
opportunityIdField.Value = settings.OpportunityId;
var shippingAddressIdField = Cart.OrderFieldValues.FirstOrDefault(x => x.OrderField.SystemName == "ShippingAddressId");
shippingAddressIdField.Value = settings.ShippingAddressAccountId;
var billingAddressIdField = Cart.OrderFieldValues.FirstOrDefault(x => x.OrderField.SystemName == "BillingAddressId");
billingAddressIdField.Value = settings.BillingAddressAccountId;
var shippingAddressIdContactField = Cart.OrderFieldValues.FirstOrDefault(x => x.OrderField.SystemName == "ShippingAddressIdContact");
shippingAddressIdContactField.Value = settings.ShippingAddressContactId;
var billingAddressIdContactField = Cart.OrderFieldValues.FirstOrDefault(x => x.OrderField.SystemName == "BillingAddressIdContact");
billingAddressIdContactField.Value = settings.BillingAddressContactId;
var contactIdField = Cart.OrderFieldValues.FirstOrDefault(x => x.OrderField.SystemName == "ContactId");
contactIdField.Value = settings.ContactId;
var accountIdField = Cart.OrderFieldValues.FirstOrDefault(x => x.OrderField.SystemName == "AccountId");
accountIdField.Value = settings.AccountId;
var quoteNameField = Cart.OrderFieldValues.FirstOrDefault(x => x.OrderField.SystemName == "QuoteName");
quoteNameField.Value = quote.Name;
var shippingIdField = Cart.OrderFieldValues.FirstOrDefault(x => x.OrderField.SystemName == "SelectedShippingProvider");
//shippingIdField.Value = quote.;
Cart.Save();
foreach (var quoteLineItem in quoteLines)
{
if (string.IsNullOrWhiteSpace(quoteLineItem.ConfigID__c))
{
continue;
}
GuruConfiguration gcon = new GuruConfiguration();
GuruConfigurationRepository gcrepo = new GuruConfigurationRepository();
gcon = gcrepo.GetById(quoteLineItem.ConfigID__c);
var guru = GuruHelpers.ConvertCudToFlatObjects(quoteLineItem.CUD_XML__c);
var data = Boxx.Web.Shared.Guru.GuruExtensions.GetOrderDataFromCud(quoteLineItem.CUD_XML__c);
Product thisProduct = Product.GetProductByID(gcon.DynamicwebProductId);
var guruSessionIdField = thisProduct.OrderLineFields.FirstOrDefault(g => g.SystemName == "GuruSessionId");
var ol = Cart.CreateOrderLine(thisProduct, (double)quoteLineItem.Quantity);
OrderLineFieldValueCollection olfv = ol.OrderLineFieldValues;
OrderLineFieldValue confgname = new OrderLineFieldValue("ConfigurationName", quote.Name);
olfv.Add(confgname);
OrderLineFieldValue gurudiscval = new OrderLineFieldValue("GuruDiscountValue", null);
olfv.Add(gurudiscval);
OrderLineFieldValue gsi = new OrderLineFieldValue("GuruSessionId", quoteLineItem.ConfigID__c);
olfv.Add(gsi);
OrderLineFieldValue mngrappr = new OrderLineFieldValue("HasManagerApprovedQuote", null);
olfv.Add(mngrappr);
OrderLineFieldValue specinst = new OrderLineFieldValue("SpecialInstructions", null);
olfv.Add(specinst);
Cart.ClearCachedPrices();
if (data.SystemDiscountPrice == 0)
{
ol.SetUnitPrice(data.SystemTotalPrice);
}
else
{
ol.SetUnitPrice(data.SystemTotalPrice);
}
ol.SetOrderLineType(OrderLine.OrderLineType.Fixed);
ol.Order = Cart;
Cart.ForcePriceRecalculation();
Cart.Save();
}
Context.SetCart(Cart);
Dynamicweb.Notifications.eCommerce.Cart.LoadedArgs args = new Dynamicweb.Notifications.eCommerce.Cart.LoadedArgs(Cart);
NotificationManager.Notify(Dynamicweb.Notifications.eCommerce.Cart.Created, args);
}