/// <summary> /// Checks stock for given product /// </summary> /// <param name="productId">Id of the product to check stock for</param> /// <param name="optionList">The option list of the product to check stock for</param> /// <param name="kitProductIds">If its a kit product the Ids of the kit products to check stock for</param> /// <returns>InventoryManagerData object containing stock details for the given product</returns> public static InventoryManagerData CheckStock(int productId, string optionList, List <int> kitProductIds) { Product product = ProductDataSource.Load(productId); if (product == null) { throw new InvalidProductException(); } if (kitProductIds == null) { kitProductIds = new List <int>(); } ProductVariant variant = null; if (!string.IsNullOrEmpty(optionList)) { variant = ProductVariantDataSource.LoadForOptionList(productId, optionList); } List <KitProduct> kitProducts = new List <KitProduct>(); foreach (int kpid in kitProductIds) { KitProduct kp = KitProductDataSource.Load(kpid); if (kp != null) { kitProducts.Add(kp); } } return(CheckStock(product, variant, kitProducts)); }
/// <summary> /// Destock a given quantity of the given product from inventory /// </summary> /// <param name="quantity">Quantity of the product to destock</param> /// <param name="productId">Id of the product to destock</param> /// <param name="optionList">The option list of the product to check stock for</param> /// <param name="lowStock">value returned in this variable indicates whether after destocking the product has reached low stock warning level or not</param> public static void Destock(short quantity, int productId, string optionList, out bool lowStock) { int stockIndicator; Product product = ProductDataSource.Load(productId); if (product == null) { throw new InvalidProductException(); } if (product.InventoryMode == InventoryMode.None) { throw new InvalidProductException(); } if (product.InventoryMode == InventoryMode.Product) { //DECREMENT PRODUCT STOCK Database database = Token.Instance.Database; using (DbCommand updateCommand = database.GetSqlStringCommand("UPDATE ac_Products SET InStock = InStock - @quantity WHERE ProductId = @productId;SELECT InStock - InStockWarningLevel As LowStock FROM ac_Products WHERE ProductId = @productId")) { database.AddInParameter(updateCommand, "@productId", System.Data.DbType.Int32, productId); database.AddInParameter(updateCommand, "@quantity", System.Data.DbType.Int16, quantity); stockIndicator = AlwaysConvert.ToInt(database.ExecuteScalar(updateCommand)); } } else { //GET THE VARIANT ProductVariant variant = ProductVariantDataSource.LoadForOptionList(productId, optionList); if (variant != null) { //DECREMENT VARIANT STOCK Database database = Token.Instance.Database; using (DbCommand updateCommand = database.GetSqlStringCommand("UPDATE ac_ProductVariants SET InStock = InStock - @quantity WHERE ProductVariantId = @productVariantId;SELECT InStock - InStockWarningLevel As LowStock FROM ac_ProductVariants WHERE ProductVariantId = @productVariantId")) { database.AddInParameter(updateCommand, "@productVariantId", System.Data.DbType.Int32, variant.ProductVariantId); database.AddInParameter(updateCommand, "@quantity", System.Data.DbType.Int16, quantity); stockIndicator = AlwaysConvert.ToInt(database.ExecuteScalar(updateCommand)); } } else { Logger.Error("Could not destock " + quantity.ToString() + " of variant " + optionList + " for Product " + productId.ToString()); //DO NOT WARN ON LOW STOCK FOR ERROR CONDITION stockIndicator = 1; } } lowStock = (stockIndicator < 1); }
/// <summary> /// Restock a given quantity of the given product to the inventory /// </summary> /// <param name="quantity">Quantity of the product to restock</param> /// <param name="productId">Id of the product to restock</param> /// <param name="optionList">The option list of the product to check stock for</param> public static void Restock(short quantity, int productId, string optionList) { Product product = ProductDataSource.Load(productId); if (product == null) { throw new InvalidProductException("The specified product does not exist."); } if (product.InventoryMode == InventoryMode.None) { throw new InvalidProductException("The specified product does not have inventory enabled."); } if (product.InventoryMode == InventoryMode.Product) { //INCREMENT PRODUCT STOCK Database database = Token.Instance.Database; using (DbCommand updateCommand = database.GetSqlStringCommand("UPDATE ac_Products SET InStock = InStock + @quantity WHERE ProductId = @productId")) { database.AddInParameter(updateCommand, "@productId", System.Data.DbType.Int32, productId); database.AddInParameter(updateCommand, "@quantity", System.Data.DbType.Int32, quantity); database.ExecuteNonQuery(updateCommand); } } else { //GET THE VARIANT ProductVariant variant = ProductVariantDataSource.LoadForOptionList(productId, optionList); if (variant != null) { //INCREMENT VARIANT STOCK Database database = Token.Instance.Database; using (DbCommand updateCommand = database.GetSqlStringCommand("UPDATE ac_ProductVariants SET InStock = InStock + @quantity WHERE ProductVariantId = @productVariantId")) { database.AddInParameter(updateCommand, "@productVariantId", System.Data.DbType.Int32, variant.ProductVariantId); database.AddInParameter(updateCommand, "@quantity", System.Data.DbType.Int32, quantity); database.ExecuteNonQuery(updateCommand); } } else { Logger.Error("Could not restock " + quantity.ToString() + " of variant " + optionList + " for Product " + productId.ToString()); } } }
private void Calculate() { Product product = ProductDataSource.Load(_ProductId); if (product != null) { if (string.IsNullOrEmpty(_OptionList)) { //NO VARIANT, SET VALUES FROM BASE PRODCUT _Sku = product.Sku; _Price = GetPriceFromRules(product); _Weight = product.Weight; } else { //VARIANT PRESENT, CHECK VARIANT FOR CORRECT VALUES ProductVariant variant = ProductVariantDataSource.LoadForOptionList(_ProductId, _OptionList); if (variant == null) { throw new InvalidOptionsException("The options specified for " + product.Name + " are invalid."); } _Sku = variant.Sku; if (variant.Price != 0) { if (variant.PriceMode == ModifierMode.Modify) { _Price = GetPriceFromRules(product) + variant.Price; } else { _Price = variant.Price; } } else { _Price = GetPriceFromRules(product); } if (variant.Weight != 0) { if (variant.WeightMode == ModifierMode.Modify) { _Weight = product.Weight + variant.Weight; } else { _Weight = variant.Weight; } } else { _Weight = product.Weight; } } // CALCULATE PRICE WITH TAX IF SPECIFIED if (_CalculateTax) { _PriceWithTax = TaxHelper.GetShopPrice(_Price, product.TaxCodeId); } //CHECK FOR KIT PRODUCTS if (_KitProductIds != null) { foreach (int kitProductId in _KitProductIds) { KitProduct kp = KitProductDataSource.Load(kitProductId); if (kp != null) { _Price += kp.CalculatedPrice; _Weight += kp.CalculatedWeight; // CALCULATE PRICE WITH TAX IF SPECIFIED if (_CalculateTax) { int taxCodeId = 0; KitComponent component = kp.KitComponent; if (component.InputType == KitInputType.IncludedHidden) { taxCodeId = product.TaxCodeId; } else if (kp.Product != null) { taxCodeId = kp.Product.TaxCodeId; } _PriceWithTax += TaxHelper.GetShopPrice(kp.CalculatedPrice, taxCodeId); } } } } _Calculated = true; } }
/// <summary> /// Recalculates the kit /// </summary> /// <param name="optionList">Option list specifying the variant</param> public void Recalculate(string optionList) { //DETERMINE THE BASE PRICE OF THE PRODUCT LSDecimal basePrice = 0; LSDecimal baseWeight = 0; //CHECK FOR A SPECIFIED VARIANT ProductVariant variant = ProductVariantDataSource.LoadForOptionList(_Product.ProductId, optionList); if (variant == null) { //NO VARIANT, SET VALUES FROM BASE PRODCUT _OptionList = string.Empty; basePrice = _Product.Price; baseWeight = _Product.Weight; } else { //VARIANT PRESENT, CHECK VARIANT FOR CORRECT VALUES _OptionList = optionList; basePrice = (variant.Price == 0) ? _Product.Price : variant.Price; baseWeight = (variant.Weight == 0) ? _Product.Weight : variant.Weight; } _MinPrice = basePrice; _MaxPrice = basePrice; _DefaultPrice = basePrice; _MinWeight = baseWeight; _MaxWeight = baseWeight; _DefaultWeight = baseWeight; foreach (ProductKitComponent pkc in _Product.ProductKitComponents) { KitComponent component = pkc.KitComponent; if (component.KitProducts.Count > 0) { switch (component.InputType) { case KitInputType.IncludedHidden: case KitInputType.IncludedShown: //ALL OF THESE GO INTO MINIMUM AND DEFAULT PRICES foreach (KitProduct kp in component.KitProducts) { LSDecimal kpPrice = kp.CalculatedPrice; LSDecimal kpWeight = kp.CalculatedWeight; _MinPrice += kpPrice; _MaxPrice += kpPrice; _DefaultPrice += kpPrice; _MinWeight += kpWeight; _MaxWeight += kpWeight; _DefaultWeight += kpWeight; } break; case KitInputType.CheckBox: //NONE GO INTO MINIMUM //ALL GO INTO MAXIMUM //DEFAULT SELECTED foreach (KitProduct kp in component.KitProducts) { LSDecimal kpPrice = kp.CalculatedPrice; LSDecimal kpWeight = kp.CalculatedWeight; _MaxPrice += kpPrice; _MaxWeight += kpWeight; if (kp.IsSelected) { _DefaultPrice += kpPrice; _DefaultWeight += kpWeight; } } break; case KitInputType.DropDown: case KitInputType.RadioButton: //LOWEST PRICE / WEIGHT GOES INTO MINIMUM //HIGHEST PRICE / WEIGHT GOES INTO MAXIMUM //DEFAULT SELECTED LSDecimal tempDefaultPrice; LSDecimal tempDefaultWeight; LSDecimal tempHighPrice; LSDecimal tempLowPrice; LSDecimal tempHighWeight; LSDecimal tempLowWeight; //GET FIRST KIT PRODUCT KitProduct firstKp = component.KitProducts[0]; LSDecimal firstKpPrice = firstKp.CalculatedPrice; LSDecimal firstKpWeight = firstKp.CalculatedWeight; //CHECK FOR HEADER OPTION if (string.IsNullOrEmpty(component.HeaderOption)) { //NO HEADER OPTION, VALUES MUST START WITH FIRST KIT PRODUCT tempHighPrice = firstKpPrice; tempHighWeight = firstKpWeight; tempLowPrice = firstKpPrice; tempLowWeight = firstKpWeight; tempDefaultPrice = firstKpPrice; tempDefaultWeight = firstKpWeight; } else { //HEADEROPTION IS PRESENT tempHighPrice = firstKpPrice > 0 ? firstKpPrice : 0; tempHighWeight = firstKpWeight > 0 ? firstKpWeight : 0; tempLowPrice = firstKpPrice < 0 ? firstKpPrice : 0; tempLowWeight = firstKpWeight < 0 ? firstKpWeight : 0; if (firstKp.IsSelected) { tempDefaultPrice = firstKpPrice; tempDefaultWeight = firstKpWeight; } else { tempDefaultPrice = 0; tempDefaultWeight = 0; } } bool foundDefault = firstKp.IsSelected; //LOOP THE REMAINING PRODUCTS IN COLLECTION TO CALCULATE VALUES for (int index = 1; index < component.KitProducts.Count; index++) { KitProduct kp = component.KitProducts[index]; LSDecimal kpPrice = kp.CalculatedPrice; LSDecimal kpWeight = kp.CalculatedWeight; if (kpPrice > tempHighPrice) { tempHighPrice = kpPrice; } if (kpPrice < tempLowPrice) { tempLowPrice = kpPrice; } if (kpWeight > tempHighWeight) { tempHighWeight = kpWeight; } if (kpWeight < tempLowWeight) { tempLowWeight = kpWeight; } if ((!foundDefault) && (kp.IsSelected)) { tempDefaultPrice = kpPrice; tempDefaultWeight = kpWeight; foundDefault = true; } } _MinPrice += tempLowPrice; _MinWeight += tempLowWeight; _MaxPrice += tempHighPrice; _MaxWeight += tempHighWeight; _DefaultPrice += tempDefaultPrice; _DefaultWeight += tempDefaultWeight; break; } } } _Calculated = true; }
/// <summary> /// Save this ProductVariant object to the database /// </summary> /// <returns><b>SaveResult</b> enumeration that represents the result of the save operation.</returns> public virtual SaveResult Save() { int productVariantId = ProductVariantDataSource.GetProductVariantId(this.ProductId, this.GetOptionChoices(OptionCountBehavior.Fixed)); //ONLY RESET THE ID IF AN EXISTING ONE IS FOUND //WE DO NOT WANT TO RESET THE VALUE TO GUID.EMPTY, IN THE EVENT THAT THE VALUE HAS //BEEN EXPLICITLY SET FOR A NEW RECORD PRIOR TO THIS CALL if (productVariantId != 0) { this.ProductVariantId = productVariantId; } if (!_Calculated) { CalculateVariant(); } SaveResult result; if (this.IsCustomized) { //DO NOT WRITE VALUES TO DATABASE IF THEY MATCH THE CALCULATED VALUES if (this.VariantName == _CalculatedName) { this.VariantName = string.Empty; } if (this.Price == _CalculatedPrice) { this.Price = 0; } if (this.Weight == _CalculatedWeight) { this.Weight = 0; } if (this.Sku == _CalculatedSku) { this.Sku = string.Empty; } //THE RECORD IS REQUIRED result = this.BaseSave(); } else if (this.ProductVariantId != 0) { //WE SHOULD NOT STORE THIS RECORD this.Delete(); result = SaveResult.RecordDeleted; } else { //RECORD IS NOT REQUIRED AND WE DID NOT SAVE OR DELETE result = SaveResult.NotDirty; } if (this.VariantName == string.Empty) { this.VariantName = _CalculatedName; } if (this.Price == 0) { this.Price = _CalculatedPrice; } if (this.Weight == 0) { this.Weight = _CalculatedWeight; } if (this.Sku == string.Empty) { this.Sku = _CalculatedSku; } if (result != SaveResult.Failed) { this.IsDirty = false; } return(result); }
public static ProductVariant Load(Int32 productVariantId) { return(ProductVariantDataSource.Load(productVariantId, true)); }