private void btnDeleteProduct_Click(object sender, EventArgs e)
        {
            //supName is equal to the selected items at index 0 to text
            string supName = (lvPkgDetails.Items[lvPkgDetails.SelectedItems[0].Index].Text);
            //suppID is equal to the result of the getProductId method taking supName as an argument
            int suppID = ProductsDB.getProductId(supName);

            //prodName is equal to the selected items at index 0, sub-item index 1 to text
            string prodName = lvPkgDetails.SelectedItems[0].SubItems[1].Text;
            //prodID is equal to the result of the getSupplierId method taking prodName as an argument
            int prodID = SuppliersDB.getSupplierId(prodName);

            //productSupplierID is equal to the result of the get_PpsID method taking suppID and prodID as arguments
            int psID = Products_SuppliersDB.Get_PpsID(suppID, prodID);

            // pkgID is equal to the result of the GetPackageIDBy_PsID method taking PsID as an argument
            int pkgID = PackagesDB.GetPackageIDBy_PsID(psID);

            bool exists  = false;
            bool attempt = false;

            if (!exists) // if it does not exist, do this
            {
                try
                {
                    // attempt = add product to package method, taking product supplier id and package id as arguments
                    attempt = ProductsDB.deleteProductFromPkg(pkgID, psID);
                    //MessageBox.Show("Succesfully deleted");
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Error modifying package\n" + ex);
                }

                if (attempt)
                {
                    MessageBox.Show("Succesfully deleted");
                    lvPkgDetails.Items.Clear();
                    int i = 0;
                    //int pkgID = lvPackages.Items.IndexOf(lvPackages.SelectedItems[i]); -- this is an old logic used

                    List <Package_Product_Suppliers> pps_o = PackagesDB.GetProductSuppliersByPackage(pkgID);
                    foreach (Package_Product_Suppliers pps in pps_o)
                    {
                        lvPkgDetails.Items.Add(pps.ProdName);
                        lvPkgDetails.Items[i].SubItems.Add(pps.SupName);
                        i += 1;
                    }
                }
            }
            else
            {
                MessageBox.Show("Unable to delete");
            }

            btnAddPackage.Visible    = true;
            btnDeleteProduct.Visible = false;
            lvPkgDetails.Items.Clear();
        }
        private void btnDeletePackage_Click(object sender, EventArgs e)
        {
            int pkgID = Convert.ToInt32(lvPackages.Items[lvPackages.SelectedItems[0].Index].Text);

            DialogResult result = MessageBox.Show("Are you sure you want to delete this Product?",
                                                  "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                bool exists  = false;
                bool attempt = false;

                if (!exists) // if it does not exist, do this
                {
                    try
                    {
                        // attempt = add product to package method, taking product supplier id and package id as arguments
                        attempt = PackagesDB.DeletePackage(pkgID);
                        //reload the packages list by getting new list and clearing the old one and adding new items to listview
                        List <Packages> packages = PackagesDB.GetPackages();
                        lvPackages.Items.Clear();
                        int i = 0;
                        foreach (Packages package in packages)
                        {
                            lvPackages.Items.Add(package.PackageID.ToString());
                            lvPackages.Items[i].SubItems.Add(package.PkgName.ToString());
                            lvPackages.Items[i].SubItems.Add(package.PkgStartDate.ToString());
                            lvPackages.Items[i].SubItems.Add(package.PkgEndDate.ToString());
                            lvPackages.Items[i].SubItems.Add(package.PkgDesc.ToString());
                            lvPackages.Items[i].SubItems.Add(package.PkgBasePrice.ToString());
                            lvPackages.Items[i].SubItems.Add(package.PkgAgencyCommission.ToString());
                            i += 1;
                        }
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show("Error deleting package\n" + ex);
                    }

                    if (attempt)
                    {
                        MessageBox.Show("Successfully deleted");
                        FrmPackages.ActiveForm.Refresh();

                        btnAddProd.Visible = false;
                        lvPkgDetails.Items.Clear();
                    }
                }
                else
                {
                    MessageBox.Show("Package is already deleted");
                }
            }
        }
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (cBSupplier.SelectedText != "")
            {
                string prodNameSelected = cBProduct.GetItemText(cBProduct.SelectedItem); //Get the product name for the selected product
                int    prodIdSelected   = ProductsDB.getProductId(prodNameSelected);     //find the productId of the selected item
                                                                                         // get thee supplier name from the combo box selection
                string supNameSelected = cBSupplier.GetItemText(cBSupplier.SelectedItem);
                // get the supplierID from the suppliername using the method in SuppliersDB
                int supplierIdSelected = SuppliersDB.getSupplierId(supNameSelected);
                // product supplier ID is equal to a method that retrieves it in a SQL Query
                int ppsID = Products_SuppliersDB.Get_PpsID(prodIdSelected, supplierIdSelected);
                // pkgID is equal to the selected ITEM at the index clicked to maintain consistency
                int pkgID = Convert.ToInt32(lvPackages.Items[lvPackages.SelectedItems[0].Index].Text);

                bool exists  = false;
                bool attempt = false;

                if (!exists) // if it does not exist, do this
                {
                    try
                    {
                        // attempt = add product to package method, taking product supplier id and package id as arguments
                        attempt = PackagesDB.AddProdToPackage(ppsID, pkgID);
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show("Error modifying package\n" + ex);
                    }

                    if (attempt)
                    {
                        MessageBox.Show("Succesfully modified");
                        int i = 0;
                        lvPkgDetails.Items.Clear();
                        List <Package_Product_Suppliers> pps_o = PackagesDB.GetProductSuppliersByPackage(pkgID);
                        foreach (Package_Product_Suppliers pps in pps_o)
                        {
                            lvPkgDetails.Items.Add(pps.ProdName);
                            lvPkgDetails.Items[i].SubItems.Add(pps.SupName);
                            i += 1;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Package is already in the database");
                }
            }
            else
            {
                MessageBox.Show("Please choose a valid supplier");
            }
        }
Ejemplo n.º 4
0
        private void btnModifyPack_Click_1(object sender, EventArgs e)
        {
            Packages modifiedPackage = new Packages();

            if (Validator.IsPkgPresent(txtPkgAgencyComm, oldPackage.PkgAgencyCommission.ToString("c")) &&
                Validator.isNonNegativeDecimal(txtPkgAgencyComm) &&
                Validator.IsPkgPresent(txtPkgBasePrice, oldPackage.PkgBasePrice.ToString("c")) &&
                Validator.isNonNegativeDecimal(txtPkgBasePrice) &&
                Validator.IsPkgPresent(txtPkgDesc, oldPackage.PkgDesc) &&
                Validator.IsPkgPresent(txtPkgName, oldPackage.PkgName) &&
                Validator.DateMin(dTEndDate, dTStartDate.Value))
            {
                modifiedPackage.PackageID           = oldPackage.PackageID;
                modifiedPackage.PkgAgencyCommission = decimal.Parse(txtPkgAgencyComm.Text, NumberStyles.AllowCurrencySymbol | NumberStyles.Number);
                modifiedPackage.PkgBasePrice        = decimal.Parse(txtPkgBasePrice.Text, NumberStyles.AllowCurrencySymbol | NumberStyles.Number);
                modifiedPackage.PkgDesc             = txtPkgDesc.Text;
                modifiedPackage.PkgName             = txtPkgName.Text;
                modifiedPackage.PkgStartDate        = dTStartDate.Value;
                modifiedPackage.PkgEndDate          = dTEndDate.Value;

                if (modifiedPackage.PkgBasePrice <= modifiedPackage.PkgAgencyCommission)
                {
                    MessageBox.Show("Commission cannot be greater than Base price!");
                }
                else
                {
                    bool attempt;

                    attempt = PackagesDB.UpdatePacakge(oldPackage, modifiedPackage);
                    if (attempt)
                    {
                        MessageBox.Show("Successfully updated package", "success");
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("Unable to modify package", "error");
                    }
                }
            }
            //else
            //{
            //    if (txtPkgName.Text == "" || txtPkgDesc.Text == "" || txtPkgBasePrice.Text == "" || txtPkgAgencyComm.Text == "")
            //    {
            //        txtPkgName.Text = oldPackage.PkgName;
            //        txtPkgDesc.Text = oldPackage.PkgDesc;
            //        txtPkgBasePrice.Text = oldPackage.PkgBasePrice.ToString("c");
            //        txtPkgAgencyComm.Text = oldPackage.PkgAgencyCommission.ToString("c");
            //    }
            //}
        }
Ejemplo n.º 5
0
        // add package button event
        private void btnAddPack_Click(object sender, EventArgs e)
        {
            bool exists = false;

            // validator checks input if its present, and a non negative decimal
            if (Validator.IsPresent(txtPkgName) && Validator.IsPresent(txtPkgDesc) &&
                Validator.IsPresent(txtPkgBasePrice) && Validator.IsPresent(txtPkgAgencyComm) &&
                Validator.NonNegativeDecimal(txtPkgBasePrice) && Validator.NonNegativeDecimal(txtPkgAgencyComm))
            {
                // new packages list retrieved with getpackages method
                List <Packages> packages = PackagesDB.GetPackages();
                foreach (Packages package in packages)
                {
                    if (package.PkgName == txtPkgName.Text)
                    {
                        exists = true;
                    }
                }
                if (!exists) // if it does not exist then execute this block to add package
                {
                    pkg = new Packages();
                    this.PutPkgData(pkg);
                    if (pkg.PkgBasePrice <= pkg.PkgAgencyCommission)
                    {
                        MessageBox.Show("Commission cannot be greater than Base price!");
                        return;
                    }
                    else
                    {
                        try
                        {
                            pkg.PackageID     = PackagesDB.AddPackage(pkg);
                            this.DialogResult = DialogResult.OK;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, ex.GetType().ToString());
                        }
                        finally
                        {
                            this.Close();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("A package of a similar name exists in database");
                }
            }
        }
        private void FrmPackages_Activated(object sender, EventArgs e) // when the form is activated (loaded or brought back to after adding) write data to list
        {
            List <Packages> packages = PackagesDB.GetPackages();
            int             i        = 0; // start count at 0

            lvPackages.Items.Clear();
            foreach (Packages package in packages)
            {
                lvPackages.Items.Add(package.PackageID.ToString());
                lvPackages.Items[i].SubItems.Add(package.PkgName.ToString());
                lvPackages.Items[i].SubItems.Add(package.PkgStartDate.ToString());
                lvPackages.Items[i].SubItems.Add(package.PkgEndDate.ToString());
                lvPackages.Items[i].SubItems.Add(package.PkgDesc.ToString());
                lvPackages.Items[i].SubItems.Add(package.PkgBasePrice.ToString("c"));
                lvPackages.Items[i].SubItems.Add(package.PkgAgencyCommission.ToString("c"));
                i += 1;
            }
        }
 private void lvPackages_SelectedIndexChanged(object sender, EventArgs e)
 {
     btnAddPackage.Visible    = true;
     btnModify.Visible        = true;
     btnDeleteProduct.Visible = false; // remove visibility for delete product button to avoid errors
     btnDeletePackage.Visible = true;  // make delete package button visible
     btnAddProd.Visible       = true;
     cBProduct.Visible        = false;
     cBSupplier.Visible       = false;
     lblProducts.Visible      = false;
     lblSupplier.Visible      = false;
     btnConfirm.Visible       = false;
     pbPic.Visible            = true;
     lblPkgName.Visible       = true;
     cBProduct.Text           = "";
     if (lvPackages.SelectedItems.Count > 0)
     {
         lblPkgName.Text = lvPackages.SelectedItems[0].SubItems[1].Text;
     }
     lvPkgDetails.Items.Clear();
     try
     {
         if (lvPackages.SelectedItems.Count > 0)
         {
             int i = 0;
             //int pkgID = lvPackages.Items.IndexOf(lvPackages.SelectedItems[i]); -- this is an old logic used
             int pkgID = Convert.ToInt32(lvPackages.Items[lvPackages.SelectedItems[0].Index].Text);
             selectedPackage = PackagesDB.GetPackageDetails(pkgID);
             List <Package_Product_Suppliers> pps_o = PackagesDB.GetProductSuppliersByPackage(pkgID);
             foreach (Package_Product_Suppliers pps in pps_o)
             {
                 lvPkgDetails.Items.Add(pps.ProdName);
                 lvPkgDetails.Items[i].SubItems.Add(pps.SupName);
                 i += 1;
             }
         }
     }
     catch (SqlException ex)
     {
         throw ex;
     }
 }