protected override void RemoveItem(int index) { TPersistable entity = Items[index]; using (ITranScope trans = mSession.CreateTranScope()) { using (mSession.Activate()) { Delete(entity); } trans.Complete(); } entity.IsDeleted = true; base.RemoveItem(index); }
private void btnSetCategories_Click(object sender, EventArgs e) { PurOrder order = mHelper.CurrentEntity; int notRemoved = 0; if (order.IsPersisted) { using (ITranScope tran = Ambient.DbSession.CreateTranScope()) { using (Ambient.DbSession.Activate()) { for (int index = 0; index < lstCategories.Items.Count; index++) { ProductCategory category = (ProductCategory)lstCategories.Items[index]; if (lstCategories.GetSelected(index)) { OrderingRepositories.PurLine.AddCategory(order.Id, category.Id, chkIncludeInactive.Checked); } else { notRemoved += OrderingRepositories.PurLine.RemoveCategory(order.Id, category.Id); } } tran.Complete(); } } ShowOrderCategories(order.Id); PurLineForm purLineForm = PurLineForm.FindOrder(order.Id); if (purLineForm != null) { purLineForm.ShowLines(); } if (notRemoved > 0) { MessageBox.Show(string.Format( "Unable to remove {0} lines from order because you have used those items.", notRemoved), "Order Updated"); } MessageBox.Show("Product categories updated for order.", "Order Updated"); } else { MessageBox.Show("You cannot set categories on an order which has not been saved. " + "Click on another line in the order list to save the current order.", "Order Not Updated"); } }
// Called by BindingSourceHelper when it receives a CurrentChanged event // from the BindingSource and the old current entity is dirty, to commit // the old current entity to the repository. public void Save(TPersistable entity) { if (!entity.IsDeleted) { using (ITranScope trans = mSession.CreateTranScope()) { using (mSession.Activate()) { if (entity.IsPersisted) { Update(entity); } else { Insert(entity); } } trans.Complete(); } } }
private void btnCreateProducts_Click(object sender, EventArgs e) { JoinPlToVpToProdBindingList data = (JoinPlToVpToProdBindingList)mHelper.DataSource; List <JoinPlToVpToProd> skippedDupes = new List <JoinPlToVpToProd>(); int numberToCreate = data.Count(p => p.PurLine_VendorProductId.IsNull); int numberCreated = 0; bool confirmIndividually = (numberToCreate < 5); if (!confirmIndividually) { string prompt = "Found " + numberToCreate.ToString() + " products to create. Okay to proceed?"; if (MessageBox.Show(prompt, "Confirm", MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } } foreach (JoinPlToVpToProd purLine in data) { if (purLine.PurLine_VendorProductId.IsNull) { if (confirmIndividually) { string prompt = "Create product \"" + (purLine.PurLine_ProductName + " " + purLine.PurLine_Size).TrimEnd() + "\"?"; if (MessageBox.Show(prompt, "Confirm", MessageBoxButtons.OKCancel) != DialogResult.OK) { continue; } } using (ITranScope trans = Ambient.DbSession.CreateTranScope()) { using (Ambient.DbSession.Activate()) { List <VendorProduct> dupeVendorProducts = OrderingRepositories.VendorProduct.Get(mVendorId, purLine.PurLine_VendorPartNum); if (dupeVendorProducts.Count > 0) { skippedDupes.Add(purLine); } else { Product newProduct = new Product(); newProduct.IsActive = true; newProduct.ManufacturerBarcode = purLine.PurLine_ManufacturerBarcode; newProduct.ManufacturerPartNum = purLine.PurLine_ManufacturerPartNum; newProduct.ProductBrandId = purLine.PurLine_ProductBrandId; newProduct.ProductSubCategoryId = purLine.PurLine_ProductSubCategoryId; newProduct.ProductName = purLine.PurLine_ProductName; newProduct.Size = purLine.PurLine_Size; newProduct.RetailPrice = purLine.PurLine_RetailPrice; OrderingRepositories.Product.Insert(newProduct); VendorProduct newVendorProduct = new VendorProduct(); newVendorProduct.VendorId = mVendorId; newVendorProduct.ProductId = newProduct.Id; newVendorProduct.CaseCost = purLine.PurLine_CaseCost; newVendorProduct.CountInCase = purLine.PurLine_CountInCase; newVendorProduct.EachCost = purLine.PurLine_EachCost; newVendorProduct.PreferredSource = purLine.PurLine_PreferredSource; newVendorProduct.RetailPriceOverride = purLine.PurLine_RetailPriceOverride; newVendorProduct.ShelfOrder = purLine.PurLine_ShelfOrder; newVendorProduct.VendorPartNum = purLine.PurLine_VendorPartNum; newVendorProduct.WholeCasesOnly = purLine.PurLine_WholeCasesOnly; OrderingRepositories.VendorProduct.Insert(newVendorProduct); purLine.PurLine_VendorProductId = newVendorProduct.Id; OrderingRepositories.PurLine.Update(purLine.InnerPurLine); numberCreated++; } } trans.Complete(); } } } ShowLines(); ShowTotalCost(); if (numberCreated > 0) { MessageBox.Show("Created " + numberCreated.ToString() + " products."); } else { MessageBox.Show("No products created."); } if (skippedDupes.Count > 0) { MessageBox.Show("Skipped the following products because they would have created duplicate vendor codes."); foreach (JoinPlToVpToProd purLine in skippedDupes) { MessageBox.Show("Skipped \"" + purLine.InnerPurLine.ProductName + "\""); } } }
private void btnCreate_Click(object sender, EventArgs e) { if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.OK) { return; } foreach (NewProductData rec in mProductDataToSave) { using (Ambient.DbSession.Activate()) { if (!rec.Product.ProductBrandId.IsNull) { List <VendorProduct> partNumMatches = OrderingRepositories.VendorProduct.Get( mVendor.Id, rec.VendorProduct.VendorPartNum); if (partNumMatches.Count == 0) { List <Product> brandNameSizeMatches = OrderingRepositories.Product.Get( rec.Product.ProductBrandId, rec.Product.ProductName, rec.Product.Size); if (brandNameSizeMatches.Count > 0) { MessageBox.Show(string.Format( "Product name \"{0}\", brand \"{1}\" and size \"{2}\" already exists in database.", rec.Product.ProductName, rec.BrandName, rec.Product.Size)); return; } } } } foreach (NewProductData otherRecord in mProductDataToSave) { if (otherRecord != rec) { if (otherRecord.VendorProduct.VendorPartNum == rec.VendorProduct.VendorPartNum) { MessageBox.Show(string.Format("Vendor code {0} exists multiple times in input.", rec.VendorProduct.VendorPartNum)); return; } if (otherRecord.Product.ProductName == rec.Product.ProductName) { if (otherRecord.BrandName == rec.BrandName) { if (otherRecord.Product.Size == rec.Product.Size) { MessageBox.Show(string.Format( "Product name \"{0}\", brand \"{1}\" and size \"{2}\" exists multiple times in input.", rec.Product.ProductName, rec.BrandName, rec.Product.Size)); return; } } } } } } foreach (ProductBrand brand in mBrands) { if (!brand.IsPersisted) { using (Ambient.DbSession.Activate()) { OrderingRepositories.ProductBrand.Insert(brand); } } } int recordsCreated = 0; int recordsUpdated = 0; foreach (NewProductData objToSave in mProductDataToSave) { using (ITranScope tranScope = Ambient.DbSession.CreateTranScope()) { using (IDbSession session = Ambient.DbSession.Activate()) { List <VendorProduct> partNumMatches = OrderingRepositories.VendorProduct.Get( mVendor.Id, objToSave.VendorProduct.VendorPartNum); if (partNumMatches.Count == 1) { VendorProduct venProdToUpdate = partNumMatches[0]; venProdToUpdate.EachCost = objToSave.VendorProduct.EachCost; venProdToUpdate.CaseCost = objToSave.VendorProduct.CaseCost; venProdToUpdate.CountInCase = objToSave.VendorProduct.CountInCase; OrderingRepositories.VendorProduct.Update(venProdToUpdate); Product prodToUpdate = OrderingRepositories.Product.Get(venProdToUpdate.ProductId); prodToUpdate.ProductName = objToSave.Product.ProductName; prodToUpdate.Size = objToSave.Product.Size; prodToUpdate.RetailPrice = objToSave.Product.RetailPrice; prodToUpdate.ProductBrandId = objToSave.Product.ProductBrandId; prodToUpdate.ProductSubCategoryId = objToSave.Product.ProductSubCategoryId; prodToUpdate.ManufacturerBarcode = objToSave.Product.ManufacturerBarcode; prodToUpdate.ManufacturerPartNum = objToSave.Product.ManufacturerPartNum; OrderingRepositories.Product.Update(prodToUpdate); recordsUpdated++; } else if (partNumMatches.Count == 0) { OrderingRepositories.Product.Insert(objToSave.Product); objToSave.VendorProduct.ProductId = objToSave.Product.Id; OrderingRepositories.VendorProduct.Insert(objToSave.VendorProduct); recordsCreated++; } else { MessageBox.Show("Multiple matches to vendor part number " + objToSave.VendorProduct.VendorPartNum); } } tranScope.Complete(); } } MessageBox.Show(string.Format("{0} products created, {1} updated.", recordsCreated, recordsUpdated)); btnCreate.Enabled = false; btnEnter.Enabled = false; btnReadClipboard.Enabled = false; }