Exemple #1
0
 /// <summary>
 /// Creates a basket item for a product
 /// </summary>
 /// <param name="productId">The product for which to create the basket item</param>
 /// <param name="quantity">The quantity of the basket item</param>
 /// <returns>The BasketItem created</returns>
 public static BasketItem CreateForProduct(int productId, short quantity)
 {
     return(BasketItemDataSource.CreateForProduct(productId, quantity, string.Empty, String.Empty, Token.Instance.UserId));
 }
Exemple #2
0
 /// <summary>
 /// Creates a basket item for a product
 /// </summary>
 /// <param name="productId">Id of the product for which to create the basket item</param>
 /// <param name="quantity">The quantity of basket item</param>
 /// <param name="optionList">List of option ids for the variant</param>
 /// <param name="kitList">List of product Ids of the kit items</param>
 /// <returns>The BasketItem created</returns>
 public static BasketItem CreateForProduct(int productId, short quantity, string optionList, string kitList)
 {
     return(BasketItemDataSource.CreateForProduct(productId, quantity, optionList, kitList, Token.Instance.UserId));
 }
        /// <summary>
        /// Saves this BasketItem object to the database.
        /// </summary>
        /// <returns><b>SaveResult</b> enumeration that represents the result of the save operation.</returns>
        protected SaveResult BaseSave()
        {
            if (this.IsDirty)
            {
                Database database     = Token.Instance.Database;
                bool     recordExists = true;

                if (this.BasketItemId == 0)
                {
                    recordExists = false;
                }

                if (this.OrderBy < 0)
                {
                    this.OrderBy = BasketItemDataSource.GetNextOrderBy(this.BasketId);
                }

                //SET DEFAULT FOR DATE FIELD
                if (this.LastModifiedDate == System.DateTime.MinValue)
                {
                    this.LastModifiedDate = LocaleHelper.LocalNow;
                }

                if (recordExists)
                {
                    //verify whether record is already present
                    StringBuilder selectQuery = new StringBuilder();
                    selectQuery.Append("SELECT COUNT(*) As RecordCount FROM ac_BasketItems");
                    selectQuery.Append(" WHERE BasketItemId = @BasketItemId");
                    using (DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString()))
                    {
                        database.AddInParameter(selectCommand, "@BasketItemId", System.Data.DbType.Int32, this.BasketItemId);
                        if ((int)database.ExecuteScalar(selectCommand) == 0)
                        {
                            recordExists = false;
                        }
                    }
                }

                int result = 0;
                if (recordExists)
                {
                    //UPDATE
                    StringBuilder updateQuery = new StringBuilder();
                    updateQuery.Append("UPDATE ac_BasketItems SET ");
                    updateQuery.Append("ParentItemId = @ParentItemId");
                    updateQuery.Append(", BasketId = @BasketId");
                    updateQuery.Append(", BasketShipmentId = @BasketShipmentId");
                    updateQuery.Append(", ProductId = @ProductId");
                    updateQuery.Append(", OptionList = @OptionList");
                    updateQuery.Append(", TaxCodeId = @TaxCodeId");
                    updateQuery.Append(", Name = @Name");
                    updateQuery.Append(", Sku = @Sku");
                    updateQuery.Append(", Price = @Price");
                    updateQuery.Append(", Weight = @Weight");
                    updateQuery.Append(", Quantity = @Quantity");
                    updateQuery.Append(", LineMessage = @LineMessage");
                    updateQuery.Append(", OrderItemTypeId = @OrderItemTypeId");
                    updateQuery.Append(", OrderBy = @OrderBy");
                    updateQuery.Append(", WrapStyleId = @WrapStyleId");
                    updateQuery.Append(", GiftMessage = @GiftMessage");
                    updateQuery.Append(", WishlistItemId = @WishlistItemId");
                    updateQuery.Append(", ShippableId = @ShippableId");
                    updateQuery.Append(", LastModifiedDate = @LastModifiedDate");
                    updateQuery.Append(", TaxRate = @TaxRate");
                    updateQuery.Append(", TaxAmount = @TaxAmount");
                    updateQuery.Append(", KitList = @KitList");
                    updateQuery.Append(", CustomFields = @CustomFields");
                    updateQuery.Append(" WHERE BasketItemId = @BasketItemId");
                    using (DbCommand updateCommand = database.GetSqlStringCommand(updateQuery.ToString()))
                    {
                        database.AddInParameter(updateCommand, "@BasketItemId", System.Data.DbType.Int32, this.BasketItemId);
                        database.AddInParameter(updateCommand, "@ParentItemId", System.Data.DbType.Int32, this.ParentItemId);
                        database.AddInParameter(updateCommand, "@BasketId", System.Data.DbType.Int32, this.BasketId);
                        database.AddInParameter(updateCommand, "@BasketShipmentId", System.Data.DbType.Int32, NullableData.DbNullify(this.BasketShipmentId));
                        database.AddInParameter(updateCommand, "@ProductId", System.Data.DbType.Int32, this.ProductId);
                        database.AddInParameter(updateCommand, "@OptionList", System.Data.DbType.String, NullableData.DbNullify(this.OptionList));
                        database.AddInParameter(updateCommand, "@TaxCodeId", System.Data.DbType.Int32, NullableData.DbNullify(this.TaxCodeId));
                        database.AddInParameter(updateCommand, "@Name", System.Data.DbType.String, this.Name);
                        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, "@Quantity", System.Data.DbType.Int16, this.Quantity);
                        database.AddInParameter(updateCommand, "@LineMessage", System.Data.DbType.String, NullableData.DbNullify(this.LineMessage));
                        database.AddInParameter(updateCommand, "@OrderItemTypeId", System.Data.DbType.Int16, this.OrderItemTypeId);
                        database.AddInParameter(updateCommand, "@OrderBy", System.Data.DbType.Int16, this.OrderBy);
                        database.AddInParameter(updateCommand, "@WrapStyleId", System.Data.DbType.Int32, NullableData.DbNullify(this.WrapStyleId));
                        database.AddInParameter(updateCommand, "@GiftMessage", System.Data.DbType.String, NullableData.DbNullify(this.GiftMessage));
                        database.AddInParameter(updateCommand, "@WishlistItemId", System.Data.DbType.Int32, NullableData.DbNullify(this.WishlistItemId));
                        database.AddInParameter(updateCommand, "@ShippableId", System.Data.DbType.Byte, this.ShippableId);
                        database.AddInParameter(updateCommand, "@LastModifiedDate", System.Data.DbType.DateTime, LocaleHelper.FromLocalTime(this.LastModifiedDate));
                        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_BasketItems (ParentItemId, BasketId, BasketShipmentId, ProductId, OptionList, TaxCodeId, Name, Sku, Price, Weight, Quantity, LineMessage, OrderItemTypeId, OrderBy, WrapStyleId, GiftMessage, WishlistItemId, ShippableId, LastModifiedDate, TaxRate, TaxAmount, KitList, CustomFields)");
                    insertQuery.Append(" VALUES (@ParentItemId, @BasketId, @BasketShipmentId, @ProductId, @OptionList, @TaxCodeId, @Name, @Sku, @Price, @Weight, @Quantity, @LineMessage, @OrderItemTypeId, @OrderBy, @WrapStyleId, @GiftMessage, @WishlistItemId, @ShippableId, @LastModifiedDate, @TaxRate, @TaxAmount, @KitList, @CustomFields)");
                    insertQuery.Append("; SELECT Scope_Identity()");
                    using (DbCommand insertCommand = database.GetSqlStringCommand(insertQuery.ToString()))
                    {
                        database.AddInParameter(insertCommand, "@BasketItemId", System.Data.DbType.Int32, this.BasketItemId);
                        database.AddInParameter(insertCommand, "@ParentItemId", System.Data.DbType.Int32, this.ParentItemId);
                        database.AddInParameter(insertCommand, "@BasketId", System.Data.DbType.Int32, this.BasketId);
                        database.AddInParameter(insertCommand, "@BasketShipmentId", System.Data.DbType.Int32, NullableData.DbNullify(this.BasketShipmentId));
                        database.AddInParameter(insertCommand, "@ProductId", System.Data.DbType.Int32, this.ProductId);
                        database.AddInParameter(insertCommand, "@OptionList", System.Data.DbType.String, NullableData.DbNullify(this.OptionList));
                        database.AddInParameter(insertCommand, "@TaxCodeId", System.Data.DbType.Int32, NullableData.DbNullify(this.TaxCodeId));
                        database.AddInParameter(insertCommand, "@Name", System.Data.DbType.String, this.Name);
                        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, "@Quantity", System.Data.DbType.Int16, this.Quantity);
                        database.AddInParameter(insertCommand, "@LineMessage", System.Data.DbType.String, NullableData.DbNullify(this.LineMessage));
                        database.AddInParameter(insertCommand, "@OrderItemTypeId", System.Data.DbType.Int16, this.OrderItemTypeId);
                        database.AddInParameter(insertCommand, "@OrderBy", System.Data.DbType.Int16, this.OrderBy);
                        database.AddInParameter(insertCommand, "@WrapStyleId", System.Data.DbType.Int32, NullableData.DbNullify(this.WrapStyleId));
                        database.AddInParameter(insertCommand, "@GiftMessage", System.Data.DbType.String, NullableData.DbNullify(this.GiftMessage));
                        database.AddInParameter(insertCommand, "@WishlistItemId", System.Data.DbType.Int32, NullableData.DbNullify(this.WishlistItemId));
                        database.AddInParameter(insertCommand, "@ShippableId", System.Data.DbType.Byte, this.ShippableId);
                        database.AddInParameter(insertCommand, "@LastModifiedDate", System.Data.DbType.DateTime, LocaleHelper.FromLocalTime(this.LastModifiedDate));
                        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._BasketItemId = 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);
        }