/// <summary> /// Get the Packages_Products_Suppliers details for each item in a list of Product_Supplier /// </summary> /// <param name="lstPS"></param> /// <returns>List of Package_Product_Supplier</returns> public List <Packages_Products_Supplier> GetPackages_Products_Suppliers(List <Products_Supplier> lstPS) { //setup DataAccess TravelExpertsDataContext dbContext = new TravelExpertsDataContext(); Packages_Products_Supplier ppsd; // temp object List <Packages_Products_Supplier> ppsdList = new List <Packages_Products_Supplier>(); // temp list //iterate through the list and the the details for each item foreach (Products_Supplier prodsup in lstPS) { ppsd = new Packages_Products_Supplier(); // new object for each iteration ppsd.ProductSupplierId = prodsup.ProductSupplierId; // set the ProductSupplierId if (txtPackageID.Text != "") { ppsd.PackageId = Convert.ToInt32(txtPackageID.Text); // set the PackageID } ppsdList.Add(ppsd); // add item to the list } return(ppsdList); // return the list }
/// <summary> /// Fires when Save is clicked /// </summary> private void btnSave_Click(object sender, EventArgs e) { // set booleans for various checks later on bool allGoodAdd = false; // True when adding Packages_Products_Suppliers records succesfully bool allGoodRmv = false; // True when deleting Packages_Products_Suppliers records succesfully bool noItems = false; // set to true if we have no items (no changes in Products of package) // temporary lists and objects List <Products_Supplier> prodsToAdd = new List <Products_Supplier>(); Packages_Products_Supplier ppsd = new Packages_Products_Supplier(); List <Packages_Products_Supplier> ppsdList = new List <Packages_Products_Supplier>(); Products_Supplier ps = new Products_Supplier(); // setup DataAccess TravelExpertsDataContext dbContext = new TravelExpertsDataContext(); // if we are not adding a new record if (!isAdd) { try { // Add new products to existing record if (addProd.Count > 0) { // create lists for Products_Suppliers, Packages_Products_Suppliers prodsToAdd = GetProducts_Suppliers(addProd); ppsdList = GetPackages_Products_Suppliers(prodsToAdd); // call the Save method, indicating true for save allGoodAdd = Save_Packages_Products_Suppliers(ppsdList, true); } else { noItems = true; // no items to save } //Remove products from existing package if (rmvProd.Count > 0) { // create lists for Products_Suppliers, Packages_Products_Suppliers prodsToAdd = GetProducts_Suppliers(rmvProd); ppsdList = GetPackages_Products_Suppliers(prodsToAdd); // call the Save method, indicating false for delete allGoodRmv = Save_Packages_Products_Suppliers(ppsdList, false); } else { noItems = true; // no items to remove } // Save main record detail // if there are noItems or Add/Remove methods were successful if (noItems || allGoodAdd || allGoodRmv) { // setup variables for later use decimal basePrice, agcyComm; // create a Package object with detail from DB based on PackageID Package pkg = dbContext.Packages.Single(p => p.PackageId == Convert.ToInt32(txtPackageID.Text)); // set the various attributes of the object from form controls pkg.PkgName = txtPkgName.Text; pkg.PkgDesc = txtPkgDesc.Text; pkg.PkgStartDate = dtpPkgStart.Value.Date; pkg.PkgEndDate = dtpPkgEnd.Value.Date; //if (pkg.PkgStartDate < pkg.PkgEndDate) //{ if (txtPkgBase.Text.StartsWith("$")) // remove the leading $ if it exists { basePrice = Convert.ToDecimal(txtPkgBase.Text.Remove(0, 1)); } else { basePrice = Convert.ToDecimal(txtPkgBase.Text); } if (txtPakComm.Text.StartsWith("$")) // remove the leading $ if it exists { agcyComm = Convert.ToDecimal(txtPakComm.Text.Remove(0, 1)); } else { agcyComm = Convert.ToDecimal(txtPakComm.Text); } //set the object attributes pkg.PkgBasePrice = basePrice; pkg.PkgAgencyCommission = agcyComm; if (basePrice > agcyComm) // check the Commision is not more than the base price { dbContext.SubmitChanges(); // save the changes } else { MessageBox.Show("Agency Commision is too high"); } //} } else { MessageBox.Show("An error occurred saving the data, tasks cancelled"); } } catch (ChangeConflictException) { // if we have concurency exceptions, resolve them and contine the save dbContext.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges); dbContext.SubmitChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message + " - " + ex.ToString()); } } else // this is a new Package { // create lists, objects and variables needed later prodsToAdd = GetProducts_Suppliers(addProd); ppsdList = GetPackages_Products_Suppliers(prodsToAdd); allGoodAdd = Save_Packages_Products_Suppliers(ppsdList, true); decimal basePrice, agcyComm; Package pkg = new Package(); // create a new Package object // set object attributes based on form controls pkg.PkgName = txtPkgName.Text; pkg.PkgDesc = txtPkgDesc.Text; pkg.PkgStartDate = dtpPkgStart.Value.Date; pkg.PkgEndDate = dtpPkgEnd.Value.Date; if (txtPkgBase.Text.StartsWith("$")) // remove the leading $ if it exists { basePrice = Convert.ToDecimal(txtPkgBase.Text.Remove(0, 1)); } else { basePrice = Convert.ToDecimal(txtPkgBase.Text); } if (txtPakComm.Text.StartsWith("$")) // remove the leading $ if it exists { agcyComm = Convert.ToDecimal(txtPakComm.Text.Remove(0, 1)); } else { agcyComm = Convert.ToDecimal(txtPakComm.Text); } // set object attributes pkg.PkgBasePrice = basePrice; pkg.PkgAgencyCommission = agcyComm; if (basePrice > agcyComm) // ensure commision is less than base price { dbContext.Packages.InsertOnSubmit(pkg); dbContext.SubmitChanges(); // submit the changes to the DB currPkg = (from pk in dbContext.Packages where pk.PkgName == pkg.PkgName select pk).Single(); } else { MessageBox.Show("Agency Commision is too high"); } // need to retrieve the newly created package ID prodsToAdd = GetProducts_Suppliers(addProd); ppsdList = GetPackages_Products_Suppliers(prodsToAdd); allGoodAdd = Save_Packages_Products_Suppliers(ppsdList, true); NewOrClear(); } LoadDGV(); lbAvail.Items.Clear(); gbDetails.Enabled = false; }
/// <summary> /// Method for saving the Package_Product_Supplier information /// </summary> /// <param name="lstPPS"></param> list of Package_Product_Supplier /// <param name="adding"></param> iadding items or removing items /// <returns>true if save was successful</returns> public bool Save_Packages_Products_Suppliers(List <Packages_Products_Supplier> lstPPS, bool adding) { //setup DataAccess TravelExpertsDataContext dbContext = new TravelExpertsDataContext(); bool status = false; // initalize the return value if (adding) // if this is a new item { foreach (Packages_Products_Supplier pw in lstPPS) // iterate through the list add a record for each { try { // search for any existing items, if not then save if ((from ppst in dbContext.Packages_Products_Suppliers where ppst.PackageId == pw.PackageId && ppst.ProductSupplierId == pw.ProductSupplierId select ppst.ProductSupplierId).Count() < 1) { Packages_Products_Supplier insItem = new Packages_Products_Supplier(); if (isAdd) { insItem.PackageId = currPkg.PackageId; } else { insItem.PackageId = Convert.ToInt32(txtPackageID.Text); } insItem.ProductSupplierId = pw.ProductSupplierId; dbContext.Packages_Products_Suppliers.InsertOnSubmit(insItem); dbContext.SubmitChanges(); status = true; } } catch (Exception) { status = false; //MessageBox.Show("Error encounctered saving data: \n" + ex.Message); break; } } } else // removing items form the table { foreach (Packages_Products_Supplier pw in lstPPS) { //List<Packages_Products_Supplier> c = new List<Packages_Products_Supplier>(); Packages_Products_Supplier delItem = new Packages_Products_Supplier();// temp object to hold search results // get record details based on PackageID & ProductSupplierID delItem = (from ppst in dbContext.Packages_Products_Suppliers where ppst.PackageId == pw.PackageId && ppst.ProductSupplierId == pw.ProductSupplierId select ppst).Single(); // delete the record dbContext.Packages_Products_Suppliers.DeleteOnSubmit(delItem); try { //Execute the delete dbContext.SubmitChanges(); status = true; // set the return value } catch { status = false; // error in saving } } } return(status); }
private void detach_Packages_Products_Suppliers(Packages_Products_Supplier entity) { this.SendPropertyChanging(); entity.Products_Supplier = null; }
partial void DeletePackages_Products_Supplier(Packages_Products_Supplier instance);
partial void InsertPackages_Products_Supplier(Packages_Products_Supplier instance);
private void attach_Packages_Products_Suppliers(Packages_Products_Supplier entity) { this.SendPropertyChanging(); entity.Package = this; }