private void UpdateItems()
        {
            //Pull books from the database, if it fails show a status message
            try
            {
                books = DBBook.GetBooks();
            }
            catch (Exception ex)
            {
                (this.Parent.Parent as MasterForm).SetStatus("Failed to load books: " + ex.Message);
            }

            //Pull maps from the database, if it fails show a status message
            try
            {
                maps = DBMap.GetMaps();
            }
            catch (Exception ex)
            {
                (this.Parent.Parent as MasterForm).SetStatus("Failed to load maps: " + ex.Message);
            }

            //Pull periodicals from the database, if it fails show a status message
            try
            {
                periodicals = DBPeriodical.GetPeriodicals();
            }
            catch (Exception ex)
            {
                (this.Parent.Parent as MasterForm).SetStatus("Failed to load periodicals: " + ex.Message);
            }
        }
        /// <summary>
        /// Gets a map with the passed id in the database
        /// </summary>
        /// <param name="id">The id of the map to pull</param>
        /// <returns>The map with the passed id</returns>
        public static DBMap GetMapOfId(int id)
        {
            using (var conn = new SqlConnection(Properties.Settings.Default.Britannicus_DBConnectionString))
                using (var command = new SqlCommand("GetMapOfId", conn)
                {
                    CommandType = CommandType.StoredProcedure
                })
                {
                    DBMap map;
                    conn.Open();
                    command.Parameters.Add("@id", SqlDbType.Int).Value = id;

                    var reader = command.ExecuteReader();
                    if (reader.Read())
                    {
                        map = new DBMap((int)reader["mapID"], (string)reader["publisher"], (string)reader["itemDescription"], Convert.ToInt32(reader["year"]), (byte)reader["edition"], (int)reader["quantity"], (int)reader["conditionID"], (decimal)reader["price"]);
                    }
                    else
                    {
                        map = new DBMap(0, "", "", 0, 0, 0, 1, 0);
                    }
                    return(map);
                }
        }
Esempio n. 3
0
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            MasterForm master = (this.Parent.Parent as MasterForm);

            try
            {
                //Input data gathered here to improve readability and centralize any processing
                //that needs to be done before insertion
                decimal  price       = this.nudPrice.Value;
                string   publisher   = this.txtPublisher.Text.Trim();
                string   title       = this.txtTitle.Text.Trim();
                string   authorFirst = this.txtAuthorFirst.Text.Trim();
                string   authorLast  = this.txtAuthorLast.Text.Trim();
                string   location    = this.txtLocation.Text.Trim();
                string   companyName = this.txtCompanyName.Text.Trim();
                DateTime publishDate = this.dtpPublishDate.Value;
                int      year        = (int)this.nudYear.Value;
                int      genreID     = (int)this.cbxGenre.SelectedValue;
                int      conditionID = this.tkbCondition.Value;
                int      quantity    = (int)this.nudQuantity.Value;
                int      edition     = (int)this.nudEdition.Value;

                object insertedID = null;
                string status     = "";

                switch (this.cbxItemType.SelectedValue)
                {
                case 1:
                    if (String.IsNullOrEmpty(status = DBBook.Validate(price, title, authorFirst, authorLast, publisher, publishDate)))
                    {
                        insertedID = DBBook.InsertBook(title, price, edition, genreID, authorFirst, authorLast, publisher,
                                                       publishDate, conditionID, quantity);

                        if (insertedID != null)
                        {
                            status = "Book " + title + " has been added successfully";
                        }
                    }

                    break;

                case 2:
                    if (String.IsNullOrEmpty(status = DBMap.Validate(price, location, year, publisher)))
                    {
                        insertedID = DBMap.InsertMap(location, price, edition, publisher, year, conditionID, quantity);

                        if (insertedID != null)
                        {
                            status = "Map " + location + " has been added successfully";
                        }
                    }

                    break;

                case 3:
                    if (String.IsNullOrEmpty(status = DBPeriodical.Validate(price, title, companyName, publishDate)))
                    {
                        insertedID = DBPeriodical.InsertPeriodical(title, price, edition, companyName, genreID, publishDate,
                                                                   conditionID, quantity);

                        if (insertedID != null)
                        {
                            status = "Periodical " + title + " has been added successfully";
                        }
                    }

                    break;
                }

                if (insertedID != null)
                {
                    List <int> tagValues = DBControlHelper.GetValuesFromCheckedControls(this.tlpTagSelection);
                    if (tagValues.Count > 0 && DBItem.InsertItemTags((int)insertedID, tagValues))
                    {
                        status += Environment.NewLine + "Item tags added.";
                    }
                }

                master.SetStatus(status);
            }
            catch (Exception ex)
            {
                master.SetStatus("Error! Failed to add item: " + ex.Message);
            }
        }
        public void ShowItem()
        {
            int itemTypeID = DBItem.GetDBItemTypeOfId((int)nudItemID.Value);

            cbxItemType.SelectedValue = itemTypeID;
            DBItem tempItem = null;

            switch (itemTypeID)
            {
            case 1:
                tempItem = DBBook.GetBookOfId((int)nudItemID.Value);
                DBBook tempBook = (tempItem as DBBook);
                this.txtTitle.Text          = tempBook.Title;
                this.txtAuthorFirst.Text    = tempBook.GetAuthorFirst();
                this.txtAuthorLast.Text     = tempBook.GetAuthorLast();
                this.cbxGenre.SelectedValue = tempBook.GetGenreID();
                this.txtPublisher.Text      = tempBook.Publisher;
                this.dtpPublishDate.Value   = tempBook.PublishDate;
                break;

            case 2:
                tempItem = DBMap.GetMapOfId((int)nudItemID.Value);
                DBMap tempMap = (tempItem as DBMap);
                this.txtPublisher.Text = tempMap.Publisher;
                this.txtLocation.Text  = tempMap.Location;
                this.nudYear.Value     = tempMap.Year;
                break;

            case 3:
                tempItem = DBPeriodical.GetPeriodicalOfId((int)nudItemID.Value);
                DBPeriodical tempPeriodical = (tempItem as DBPeriodical);
                this.txtTitle.Text          = tempPeriodical.Title;
                this.cbxGenre.SelectedValue = tempPeriodical.GetGenreID();
                this.txtCompanyName.Text    = tempPeriodical.CompanyName;
                this.dtpPublishDate.Value   = tempPeriodical.PublishDate;
                break;
            }

            if (tempItem != null)
            {
                this.tkbCondition.Value = tempItem.GetConditionID();
                this.nudQuantity.Value  = tempItem.GetQuantity();

                try
                {
                    DBControlHelper.PopulateWithControls <DBTag, CheckBox>(this.tlpTagSelection, DBTag.GetTags(), "Description", "ID", tempItem.GetTags());
                }
                catch (Exception ex)
                {
                    (this.Parent.Parent as MasterForm).SetStatus("Error! Failed to load tags: " + ex.Message);
                }

                this.nudPrice.Value = tempItem.GetRegularPrice();

                //Show the discount
                if (tempItem.HasDiscount())
                {
                    this.nudDiscountPrice.Value        = tempItem.GetDiscount().Amount;
                    this.dtpDiscountFrom.Value         = tempItem.GetDiscount().StartDate;
                    this.dtpDiscountTo.Value           = tempItem.GetDiscount().EndDate;
                    this.chkSetupDiscountTitle.Checked = true;
                }
                else
                {
                    this.nudDiscountPrice.Value = 0;
                    this.dtpDiscountFrom.ResetText();
                    this.dtpDiscountTo.ResetText();
                    this.chkSetupDiscountTitle.Checked = false;
                }
            }
        }
        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            MasterForm master = (this.Parent.Parent as MasterForm);

            try
            {
                //Input data gathered here to improve readability and centralize any processing
                //that needs to be done before insertion
                int      itemID        = (int)this.nudItemID.Value;
                decimal  price         = this.nudPrice.Value;
                string   publisher     = this.txtPublisher.Text.Trim();
                string   title         = this.txtTitle.Text.Trim();
                string   authorFirst   = this.txtAuthorFirst.Text.Trim();
                string   authorLast    = this.txtAuthorLast.Text.Trim();
                string   location      = this.txtLocation.Text.Trim();
                string   companyName   = this.txtCompanyName.Text.Trim();
                DateTime publishDate   = this.dtpPublishDate.Value;
                int      year          = (int)this.nudYear.Value;
                int      genreID       = (int)this.cbxGenre.SelectedValue;
                int      conditionID   = this.tkbCondition.Value;
                int      quantity      = (int)this.nudQuantity.Value;
                int      edition       = (int)this.nudEdition.Value;
                decimal  discountPrice = this.nudDiscountPrice.Value;
                DateTime discountFrom  = this.dtpDiscountFrom.Value;
                DateTime discountTo    = this.dtpDiscountTo.Value;

                string status = "";

                switch (cbxItemType.SelectedValue)
                {
                case 1:
                    if (String.IsNullOrEmpty(status = DBBook.Validate(price, title, authorFirst, authorLast, publisher, publishDate)))
                    {
                        if (DBBook.UpdateBook(itemID, title, price, edition, genreID, authorFirst, authorLast, publisher, publishDate,
                                              conditionID, quantity))
                        {
                            status = "Book " + title + " has been saved." + Environment.NewLine;
                        }
                    }
                    break;

                case 2:
                    if (String.IsNullOrEmpty(status = DBMap.Validate(price, location, year, publisher)))
                    {
                        if (DBMap.UpdateMap(itemID, location, price, edition, publisher, year, conditionID, quantity))
                        {
                            status = "Map " + location + " has been saved." + Environment.NewLine;
                        }
                    }
                    break;

                case 3:
                    if (String.IsNullOrEmpty(status = DBPeriodical.Validate(price, title, companyName, publishDate)))
                    {
                        if (DBPeriodical.UpdatePeriodical(itemID, title, price, edition, companyName, genreID, publishDate, conditionID,
                                                          quantity))
                        {
                            status = "Periodical " + title + " has been saved." + Environment.NewLine;
                        }
                    }
                    break;
                }

                if (quantity < Cart.Invoice.GetQuantityBeingSold(itemID))
                {
                    status += "Quantity cannot be less than the amount of the item currently being sold" + Environment.NewLine;
                }
                else
                {
                    for (var i = 0; i < Cart.Invoice.Transactions.Count; i++)
                    {
                        if (Cart.Invoice.Transactions[i].ItemID == itemID)
                        {
                            Cart.Invoice.Transactions[i].SetItemStock(quantity);
                        }
                    }
                }

                if (DBItem.UpdateItemTags(itemID, DBControlHelper.GetValuesFromCheckedControls(this.tlpTagSelection)))
                {
                    status += "Item tags have been saved." + Environment.NewLine;
                }

                if (this.chkSetupDiscountTitle.Checked)
                {
                    string discountError = DBItemDiscount.Validate(discountPrice, discountFrom, discountTo);
                    if (String.IsNullOrEmpty(discountError))
                    {
                        if (DBItem.SaveDiscount(itemID, discountPrice, discountFrom, discountTo))
                        {
                            status += "Discount has been saved." + Environment.NewLine;
                        }
                    }
                    else
                    {
                        status += discountError;
                    }
                }

                master.SetStatus(status);
            }
            catch (Exception ex)
            {
                master.SetStatus("Error! Update failed: " + ex.Message);
            }
        }