protected void btnSave_Click(object sender, EventArgs e) { if (!Page.IsValid) { return; } Product product = null; if (IsNewMode) { product = new Product(); product.SendSupplier = cbxIsSendSupplier.Checked; product.IsDeleted = false; } else { product = Product.FetchByID(ProductId); } Product p = Product.FetchByCode(txtProductCode.Text); if (p != null && p.ProductId != ProductId) { Master.MessageCenter.DisplayErrorMessage(ProductsStrings.GetText(@"ProductCodeAlreadyExists")); return; } if (txtProductNum.Text != "") { Product p1 = Product.FetchByProductNum(Convert.ToInt64(txtProductNum.Text)); if (p1 != null && p1.ProductId != ProductId) { Master.MessageCenter.DisplayErrorMessage(ProductsStrings.GetText(@"ProductNumAlreadyExists")); return; } else { product.ProductNum = Convert.ToInt64(txtProductNum.Text); } } else { product.ProductNum = null; } product.ProductName = txtProductName.Text; product.ProductCode = txtProductCode.Text; product.Amount = txtProductAmount.Text; product.Description = txtProductDescription.Text; product.CategoryId = Convert.ToInt64(ddlCategory.SelectedValue); product.SubCategoryId = Convert.ToInt64(ddlSubCategory.SelectedValue); product.RecomendedPrice = txtRecomendedPrice.Text.Trim() != "" ? Convert.ToDecimal(txtRecomendedPrice.Text.Trim()) : 0; if (fuImage.HasFile) { if (!IsNewMode) { MediaUtility.DeleteImageFilePath("Product", product.ProductImage, 64, 64, 0); } string fn = MediaUtility.SaveFile(fuImage.PostedFile, "Product", 0); product.ProductImage = fn; imgImage.ImageUrl = Snoopi.core.MediaUtility.GetImagePath("Product", product.ProductImage, 0, 64, 64); ImageFileHandler(fuImage, imgImage, btnDeleteImage, imgImage.ImageUrl); } else if (product.ProductImage != "" && fuImage.Visible) { MediaUtility.DeleteImageFilePath("Product", product.ProductImage, 64, 64, 0); product.ProductImage = ""; } product.Save(); ProductId = product.ProductId; int count = 0; foreach (ListItem item in ddlAnimalType.Items) { if (item.Selected) { count++; ProductAnimal productAnimal = ProductAnimal.FetchByID(ProductId, Convert.ToInt64(item.Value)); if (productAnimal == null) { productAnimal = new ProductAnimal(); productAnimal.ProductId = ProductId; productAnimal.AnimalId = Convert.ToInt64(item.Value); productAnimal.Save(); } } else { ProductAnimal.Delete(ProductId, Convert.ToInt64(item.Value)); } } int index = 0; //save filters foreach (GridViewRow row in gvFilters.Rows) { Int64 FilterId = Int64.Parse(gvFilters.DataKeys[index].Value.ToString()); CheckBoxList lsbx = (CheckBoxList)row.FindControl("ddlSubFilter"); foreach (ListItem item in lsbx.Items) { ProductFilter productFilter = ProductFilter.FetchByID(ProductId, FilterId, Convert.ToInt64(item.Value)); if (productFilter == null && item.Selected) { productFilter = new ProductFilter(); productFilter.ProductId = ProductId; productFilter.FilterId = FilterId; productFilter.SubFilterId = Convert.ToInt64(item.Value); productFilter.Save(); } else if (productFilter != null && !item.Selected) { (new Query(ProductFilter.TableSchema).Where(ProductFilter.Columns.ProductId, productFilter.ProductId) .AddWhere(ProductFilter.Columns.FilterId, productFilter.FilterId) .AddWhere(ProductFilter.Columns.SubFilterId, productFilter.SubFilterId).Delete()).Execute(); } } index++; } if (IsNewMode) { if (cbxIsSendSupplier.Checked) { EmailMessagingService.SendEmailNewProductToSupplier(product); } string successMessage = ProductsStrings.GetText(@"MessageProductCreated"); string url = @"EditProduct.aspx?ProductId=" + ProductId; url += @"&message-success=" + Server.UrlEncode(successMessage); Response.Redirect(url, true); } else { string successMessage = ProductsStrings.GetText(@"MessageProductSaved"); Master.MessageCenter.DisplaySuccessMessage(successMessage); } }
protected void btnImport_Click(object sender, EventArgs e) { if (CsvDataTable != null) { int count = 0; try { foreach (DataRow productRow in CsvDataTable.Rows) { if (productRow["Comments"].ToString() == "") { Product product = null; if (productRow["ProductCode"].ToString() != "") { product = Product.FetchByCode(productRow["ProductCode"].ToString()); } if (product == null && productRow["ProductNum"].ToString() != "") { product = Product.FetchByProductNum(Convert.ToInt64(productRow["ProductNum"])); } if (product == null) { product = new Product(); } product.ProductCode = productRow["ProductCode"].ToString(); product.ProductNum = productRow["ProductNum"].ToString() == "" ? (Int64?)null : Convert.ToInt64(productRow["ProductNum"]); product.ProductImage = ""; product.ProductName = productRow["ProductName"].ToString(); product.Amount = productRow["Amount"] != null ? productRow["Amount"].ToString() : ""; product.Description = productRow["Description"] != null ? productRow["Description"].ToString() : ""; product.CategoryId = Category.FetchByID(productRow["CategoryId"].ToString()).CategoryId; product.SubCategoryId = SubCategory.FetchByID(Int64.Parse(productRow["SubCategoryId"].ToString()), product.CategoryId).SubCategoryId; product.IsDeleted = false; product.RecomendedPrice = productRow["RecomendedPrice"] == null ? 0 : Convert.ToDecimal(productRow["RecomendedPrice"].ToString()); product.Save(); string animals = productRow["AnimalType"].ToString(); List <string> result = animals.Split(',').ToList(); foreach (string animal in result) { ProductAnimal productAnimal = new ProductAnimal(); productAnimal.ProductId = product.ProductId; productAnimal.AnimalId = Animal.FetchByName(animal.Trim()).AnimalId; productAnimal.Save(); } count++; } } lblImportResult.Text = ProductsStrings.GetText(@"MessageImportSuccess"); } catch { lblImportResult.Text = ProductsStrings.GetText(@"MessageImportFailedUnknown"); } phImportResult.Visible = true; lblTotalImported.Text = count.ToString(); btnImport.Enabled = false; phErrors.Visible = false; phProductsList.Visible = false; } }