HomeGuidesAPI ReferenceChangelogDiscussions
GuidesiMIS ProfessionaliMIS EnterpriseClient SupportLog In
Guides

Cart manager

using Asi.Soa.ClientServices;
using Asi.Soa.Commerce.DataContracts;
using Asi.Soa.Core.DataContracts;
using Asi.Soa.Membership.DataContracts;

string partyId = "215";
string itemId = "G15";

EntityManager entityManager = new EntityManager();

var cartManager = new CartManager(entityManager, partyId);
// Clear the cart
cartManager.DeleteCart();

if (cartManager.Cart != null)
{
  // Check cart for existing item and if it does not exist add it
  if (cartManager.CheckItemQuantity(itemId) < 1)
    cartManager.AddItem(itemId, 1);

  // Set the delivery method
  var deliveryData =
    new DeliveryData {DeliveryMethod = new DeliveryMethodData {DeliveryMethodId = "UPS"}};
  cartManager.Cart.ComboOrder.Order.Delivery = new DeliveryDataCollection {deliveryData};
  cartManager.Cart.ComboOrder.Order.Delivery[0].AddressId = null;
  cartManager.Cart.ComboOrder.Order.Delivery[0].Address = new FullAddressData
  {
    Address = new AddressData {AddressLines = new AddressLineDataCollection {"1 Main Street"}}
  };

  // Create new payment object
  RemittanceData remittanceData = new RemittanceData
  {
    PaymentMethod = new PaymentMethodData {PaymentMethodId = "CASH"},
    Amount = cartManager.Cart.ComboOrder.Order.OrderTotal ??
      new MonetaryAmountData(0, cartManager.Cart.ComboOrder.Order.Currency)
  };
  cartManager.Cart.ComboOrder.Payments = new RemittanceDataCollection {remittanceData};

  // Make sure the cart is valid
  var results = cartManager.ValidateCart();
  if (!results.IsValid) return;

  // Set the payment amount
  cartManager.Cart.ComboOrder.Payments[0].Amount = new MonetaryAmountData(8.10m, new CurrencyData("USD"));

  // Submit the cart
  results = cartManager.SubmitCart();
  if (results.IsValid)
  {
    var comboOrderData = results.Entity.ComboOrder;
    Console.WriteLine("Order Number: {0}", comboOrderData.Order.OrderNumber);
  }
  else
  {
    Console.WriteLine(results.ValidationResults.Summary);
  }
}

Cart Add/Update/Submit

using Asi.Soa.ClientServices;
using Asi.Soa.Commerce.DataContracts;
using Asi.Soa.Core.DataContracts;
using Asi.Soa.Membership.DataContracts;

string partyId = "215";
string itemId = "G15";

EntityManager entityManager = new EntityManager();

CartData cartData = null;
bool newCartCreated = false;

// Check if cart exists
QueryData query = new QueryData("Cart")
{
  Criteria = new CriteriaDataCollection
  {
    CriteriaData.Equal("UserId", partyId),
    CriteriaData.Equal("UpdatedBy", partyId)
      // The updated by filter keeps us from retrieving any carts created for this contact 
      // by another user in "On Behalf Of" mode
  }
};
FindResultsData queryResults = entityManager.Find(query);
if (queryResults.Result != null && queryResults.Result.Count > 0)
  cartData = (CartData) queryResults.Result[0];

// No existing cart was found, create a new one
if (cartData == null)
{
  cartData = new CartData
  {
    UserId = partyId,
    UpdateInformation = new EntityUpdateInformationData
    {
      CreatedBy = partyId,
      CreatedOn = DateTime.UtcNow,
      UpdatedBy = partyId,
      UpdatedOn = DateTime.UtcNow
    }
  };
  newCartCreated = true;
}
else
{
  cartData.UpdateInformation.UpdatedBy = partyId;
  cartData.UpdateInformation.UpdatedOn = DateTime.UtcNow;
}

// iMIS only supports one currency for now, but it is still important to define the currency
// to ensure proper formatting in the user interface
var findResultsData =
                entityManager.Find(
                    new QueryData(PartyCommercePreferencesData.EntityTypeName)
                        .AddCriteria(new CriteriaData("PartyId",
                            OperationData.Equal, partyId))).Result;

PartyCommercePreferencesData partyCommercePreferencesData = findResultsData[0] as PartyCommercePreferencesData;

var currency = partyCommercePreferencesData.DefaultCurrency;
var comboOrderData = new ComboOrderData {Currency = currency};
OrderData orderData = comboOrderData.Order = new OrderData {Currency = currency};

var customerPartyData = new CustomerPartyData {PartyId = partyId};
orderData.SoldToCustomerParty = customerPartyData;

// Add the product
orderData.Lines = new OrderLineDataCollection();
var line = new OrderLineData
{
  Item = new ItemData {ItemId = itemId},
  LineNumber = 1,
  QuantityOrdered = new QuantityData(1)
};
orderData.Lines.Add(line);

// Set delivery
orderData.Delivery = new DeliveryDataCollection
{
  new DeliveryData
  {
    DeliveryMethod = new DeliveryMethodData {DeliveryMethodId = "UPS"},
    CustomerParty = customerPartyData,
    Address = new FullAddressData
    {
      Address = new AddressData {AddressLines = new AddressLineDataCollection {"1 Main Street"}}
    }
  }
};

// Validate will calculate pricing
var comboOrderValidation = entityManager.Validate(comboOrderData);
comboOrderData = comboOrderValidation.Entity;

// Create cash payment   
var remittanceData = new RemittanceData
{
  // Set the amount to the order total + invoice total
  Amount = new MonetaryAmountData(comboOrderData.Order.OrderTotal?.Amount ?? 0,
                                  comboOrderData.Order.Currency),
  PaymentMethod = new PaymentMethodData {PaymentMethodId = "CASH"},
  ReferenceNumber = "1"
};
comboOrderData.Payments = new RemittanceDataCollection {remittanceData};

// Add order to the cart and save it for later
cartData.ComboOrder = comboOrderData;
var results = newCartCreated ? entityManager.Add(cartData) : entityManager.Update(cartData);
if (results.IsValid)
{
  cartData = results.Entity; // Refresh the cart with the one returned on save
  Console.WriteLine("Order Total: {0}", comboOrderData.Order.OrderTotal);
}
else
{
  Console.WriteLine(results.ValidationResults.Summary);
}

// Or submit it
var cartSubmitResults =
  (ValidateResultsData) entityManager.Execute(new CartSubmissionRequest {Cart = cartData});
if (cartSubmitResults.IsValid)
{
  cartData = (CartData) cartSubmitResults.Entity;
  Console.WriteLine("Order Number: {0}", cartData.ComboOrder.Order.OrderNumber);
}
else
{
  Console.WriteLine(results.ValidationResults.Summary);
}