/// <summary> /// Generate subscriptions for order items /// </summary> /// <param name="order">The order being created</param> /// <param name="orderItemSubscriptions">Generated subscriptions are returned in this parameter</param> internal static void GenerateOrderSubscriptions(Order order, Dictionary <int, Subscription[]> orderItemSubscriptions) { //GENERATE (BUT DO NOT ACTIVATE) SUBSCRIPTIONS //BUILD A LIST OF RECURRING SUBSCRIPTIONS GROUPED BY ORDER ITEM OrderItemCollection subscriptionItems = OrderItemDataSource.LoadSubscriptionItems(order.OrderId); if (subscriptionItems.Count > 0) { foreach (OrderItem orderItem in subscriptionItems) { Subscription[] allSubs = orderItem.GenerateSubscriptions(false); if ((allSubs != null) && (allSubs.Length > 0)) { List <Subscription> recurringSubs = new List <Subscription>(); foreach (Subscription sub in allSubs) { if (sub.SubscriptionPlan.IsRecurring) { recurringSubs.Add(sub); } } if (recurringSubs.Count > 0) { orderItemSubscriptions.Add(orderItem.OrderItemId, recurringSubs.ToArray()); } } } } }
/// <summary> /// Saves this OrderItem object to the database. /// </summary> /// <returns><b>SaveResult</b> enumeration that represents the result of the save operation.</returns> public virtual SaveResult Save() { if (this.IsDirty) { Database database = Token.Instance.Database; bool recordExists = true; if (this.OrderItemId == 0) { recordExists = false; } if (this.OrderBy < 0) { this.OrderBy = OrderItemDataSource.GetNextOrderBy(this.OrderId); } if (recordExists) { //verify whether record is already present StringBuilder selectQuery = new StringBuilder(); selectQuery.Append("SELECT COUNT(*) As RecordCount FROM ac_OrderItems"); selectQuery.Append(" WHERE OrderItemId = @OrderItemId"); using (DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString())) { database.AddInParameter(selectCommand, "@OrderItemId", System.Data.DbType.Int32, this.OrderItemId); if ((int)database.ExecuteScalar(selectCommand) == 0) { recordExists = false; } } } int result = 0; if (recordExists) { //UPDATE StringBuilder updateQuery = new StringBuilder(); updateQuery.Append("UPDATE ac_OrderItems SET "); updateQuery.Append("OrderId = @OrderId"); updateQuery.Append(", ParentItemId = @ParentItemId"); updateQuery.Append(", OrderItemTypeId = @OrderItemTypeId"); updateQuery.Append(", ShippableId = @ShippableId"); updateQuery.Append(", OrderShipmentId = @OrderShipmentId"); updateQuery.Append(", ProductId = @ProductId"); updateQuery.Append(", Name = @Name"); updateQuery.Append(", OptionList = @OptionList"); updateQuery.Append(", VariantName = @VariantName"); updateQuery.Append(", Sku = @Sku"); updateQuery.Append(", Price = @Price"); updateQuery.Append(", Weight = @Weight"); updateQuery.Append(", CostOfGoods = @CostOfGoods"); updateQuery.Append(", Quantity = @Quantity"); updateQuery.Append(", LineMessage = @LineMessage"); updateQuery.Append(", OrderBy = @OrderBy"); updateQuery.Append(", GiftMessage = @GiftMessage"); updateQuery.Append(", TaxCodeId = @TaxCodeId"); updateQuery.Append(", WrapStyleId = @WrapStyleId"); updateQuery.Append(", WishlistItemId = @WishlistItemId"); updateQuery.Append(", InventoryStatusId = @InventoryStatusId"); updateQuery.Append(", TaxRate = @TaxRate"); updateQuery.Append(", TaxAmount = @TaxAmount"); updateQuery.Append(", KitList = @KitList"); updateQuery.Append(", CustomFields = @CustomFields"); updateQuery.Append(" WHERE OrderItemId = @OrderItemId"); using (DbCommand updateCommand = database.GetSqlStringCommand(updateQuery.ToString())) { database.AddInParameter(updateCommand, "@OrderItemId", System.Data.DbType.Int32, this.OrderItemId); database.AddInParameter(updateCommand, "@OrderId", System.Data.DbType.Int32, this.OrderId); database.AddInParameter(updateCommand, "@ParentItemId", System.Data.DbType.Int32, NullableData.DbNullify(this.ParentItemId)); database.AddInParameter(updateCommand, "@OrderItemTypeId", System.Data.DbType.Int16, this.OrderItemTypeId); database.AddInParameter(updateCommand, "@ShippableId", System.Data.DbType.Byte, this.ShippableId); database.AddInParameter(updateCommand, "@OrderShipmentId", System.Data.DbType.Int32, NullableData.DbNullify(this.OrderShipmentId)); database.AddInParameter(updateCommand, "@ProductId", System.Data.DbType.Int32, NullableData.DbNullify(this.ProductId)); database.AddInParameter(updateCommand, "@Name", System.Data.DbType.String, this.Name); database.AddInParameter(updateCommand, "@OptionList", System.Data.DbType.String, NullableData.DbNullify(this.OptionList)); database.AddInParameter(updateCommand, "@VariantName", System.Data.DbType.String, NullableData.DbNullify(this.VariantName)); database.AddInParameter(updateCommand, "@Sku", System.Data.DbType.String, NullableData.DbNullify(this.Sku)); database.AddInParameter(updateCommand, "@Price", System.Data.DbType.Decimal, this.Price); database.AddInParameter(updateCommand, "@Weight", System.Data.DbType.Decimal, this.Weight); database.AddInParameter(updateCommand, "@CostOfGoods", System.Data.DbType.Decimal, this.CostOfGoods); database.AddInParameter(updateCommand, "@Quantity", System.Data.DbType.Int16, this.Quantity); database.AddInParameter(updateCommand, "@LineMessage", System.Data.DbType.String, NullableData.DbNullify(this.LineMessage)); database.AddInParameter(updateCommand, "@OrderBy", System.Data.DbType.Int16, this.OrderBy); database.AddInParameter(updateCommand, "@GiftMessage", System.Data.DbType.String, NullableData.DbNullify(this.GiftMessage)); database.AddInParameter(updateCommand, "@TaxCodeId", System.Data.DbType.Int32, NullableData.DbNullify(this.TaxCodeId)); database.AddInParameter(updateCommand, "@WrapStyleId", System.Data.DbType.Int32, NullableData.DbNullify(this.WrapStyleId)); database.AddInParameter(updateCommand, "@WishlistItemId", System.Data.DbType.Int32, NullableData.DbNullify(this.WishlistItemId)); database.AddInParameter(updateCommand, "@InventoryStatusId", System.Data.DbType.Int16, this.InventoryStatusId); database.AddInParameter(updateCommand, "@TaxRate", System.Data.DbType.Decimal, this.TaxRate); database.AddInParameter(updateCommand, "@TaxAmount", System.Data.DbType.Decimal, this.TaxAmount); database.AddInParameter(updateCommand, "@KitList", System.Data.DbType.String, NullableData.DbNullify(this.KitList)); database.AddInParameter(updateCommand, "@CustomFields", System.Data.DbType.String, NullableData.DbNullify(this.CustomFields.ToString())); //RESULT IS NUMBER OF RECORDS AFFECTED result = database.ExecuteNonQuery(updateCommand); } } else { //INSERT StringBuilder insertQuery = new StringBuilder(); insertQuery.Append("INSERT INTO ac_OrderItems (OrderId, ParentItemId, OrderItemTypeId, ShippableId, OrderShipmentId, ProductId, Name, OptionList, VariantName, Sku, Price, Weight, CostOfGoods, Quantity, LineMessage, OrderBy, GiftMessage, TaxCodeId, WrapStyleId, WishlistItemId, InventoryStatusId, TaxRate, TaxAmount, KitList, CustomFields)"); insertQuery.Append(" VALUES (@OrderId, @ParentItemId, @OrderItemTypeId, @ShippableId, @OrderShipmentId, @ProductId, @Name, @OptionList, @VariantName, @Sku, @Price, @Weight, @CostOfGoods, @Quantity, @LineMessage, @OrderBy, @GiftMessage, @TaxCodeId, @WrapStyleId, @WishlistItemId, @InventoryStatusId, @TaxRate, @TaxAmount, @KitList, @CustomFields)"); insertQuery.Append("; SELECT Scope_Identity()"); using (DbCommand insertCommand = database.GetSqlStringCommand(insertQuery.ToString())) { database.AddInParameter(insertCommand, "@OrderItemId", System.Data.DbType.Int32, this.OrderItemId); database.AddInParameter(insertCommand, "@OrderId", System.Data.DbType.Int32, this.OrderId); database.AddInParameter(insertCommand, "@ParentItemId", System.Data.DbType.Int32, NullableData.DbNullify(this.ParentItemId)); database.AddInParameter(insertCommand, "@OrderItemTypeId", System.Data.DbType.Int16, this.OrderItemTypeId); database.AddInParameter(insertCommand, "@ShippableId", System.Data.DbType.Byte, this.ShippableId); database.AddInParameter(insertCommand, "@OrderShipmentId", System.Data.DbType.Int32, NullableData.DbNullify(this.OrderShipmentId)); database.AddInParameter(insertCommand, "@ProductId", System.Data.DbType.Int32, NullableData.DbNullify(this.ProductId)); database.AddInParameter(insertCommand, "@Name", System.Data.DbType.String, this.Name); database.AddInParameter(insertCommand, "@OptionList", System.Data.DbType.String, NullableData.DbNullify(this.OptionList)); database.AddInParameter(insertCommand, "@VariantName", System.Data.DbType.String, NullableData.DbNullify(this.VariantName)); database.AddInParameter(insertCommand, "@Sku", System.Data.DbType.String, NullableData.DbNullify(this.Sku)); database.AddInParameter(insertCommand, "@Price", System.Data.DbType.Decimal, this.Price); database.AddInParameter(insertCommand, "@Weight", System.Data.DbType.Decimal, this.Weight); database.AddInParameter(insertCommand, "@CostOfGoods", System.Data.DbType.Decimal, this.CostOfGoods); database.AddInParameter(insertCommand, "@Quantity", System.Data.DbType.Int16, this.Quantity); database.AddInParameter(insertCommand, "@LineMessage", System.Data.DbType.String, NullableData.DbNullify(this.LineMessage)); database.AddInParameter(insertCommand, "@OrderBy", System.Data.DbType.Int16, this.OrderBy); database.AddInParameter(insertCommand, "@GiftMessage", System.Data.DbType.String, NullableData.DbNullify(this.GiftMessage)); database.AddInParameter(insertCommand, "@TaxCodeId", System.Data.DbType.Int32, NullableData.DbNullify(this.TaxCodeId)); database.AddInParameter(insertCommand, "@WrapStyleId", System.Data.DbType.Int32, NullableData.DbNullify(this.WrapStyleId)); database.AddInParameter(insertCommand, "@WishlistItemId", System.Data.DbType.Int32, NullableData.DbNullify(this.WishlistItemId)); database.AddInParameter(insertCommand, "@InventoryStatusId", System.Data.DbType.Int16, this.InventoryStatusId); database.AddInParameter(insertCommand, "@TaxRate", System.Data.DbType.Decimal, this.TaxRate); database.AddInParameter(insertCommand, "@TaxAmount", System.Data.DbType.Decimal, this.TaxAmount); database.AddInParameter(insertCommand, "@KitList", System.Data.DbType.String, NullableData.DbNullify(this.KitList)); database.AddInParameter(insertCommand, "@CustomFields", System.Data.DbType.String, NullableData.DbNullify(this.CustomFields.ToString())); //RESULT IS NEW IDENTITY; result = AlwaysConvert.ToInt(database.ExecuteScalar(insertCommand)); this._OrderItemId = result; } } this.SaveChildren(); //OBJECT IS DIRTY IF NO RECORDS WERE UPDATED OR INSERTED this.IsDirty = (result == 0); if (this.IsDirty) { return(SaveResult.Failed); } else { return(recordExists ? SaveResult.RecordUpdated : SaveResult.RecordInserted); } } //SAVE IS SUCCESSFUL IF OBJECT IS NOT DIRTY return(SaveResult.NotDirty); }
public static OrderItem Load(Int32 orderItemId) { return(OrderItemDataSource.Load(orderItemId, true)); }
/// <summary> /// Gets the order items that would result from the addition of the given product to an order. /// </summary> /// <param name="productId">The id of the product to add.</param> /// <param name="quantity">The quantity of the product to add.</param> /// <returns>The order items that would result from the addition of the given product to an order</returns> public static List <OrderItem> CreateForProduct(int productId, short quantity) { return(OrderItemDataSource.CreateForProduct(productId, quantity, string.Empty, null)); }