public void ReinitialiseObjects()
 {
     bookInfo      = null;
     bookCatalogue = null;
     loanTrans     = null;
     transDetail   = null;
     bookID        = 0;
     transID       = 0;
     bookStatus    = "";
 }
 public void DefineBookObjects()
 {
     bookInfo      = context.BookInfoes.Where(x => x.BookID == bookID).First();
     bookCatalogue = context.BookCatalogues.Where(x => x.ISBN == bookInfo.ISBN).First();
 }
        private void buttonLoan_Click(object sender, EventArgs e)
        {
            using (TransactionScope loanTransScope = new TransactionScope())
            {
                int currentTransID = context.LoanTrans.OrderByDescending(x => x.TransID).First().TransID;
                try
                {
                    if (IsOverLoanLimit(availableLimit, booksToLoan.Count()) == true)
                    {
                        ((FormMain)this.MdiParent).SetToolStripLabelText = "Number of Books has exceeded borrowing limit.";
                        return;
                    }

                    if (IsBookAlreadyOut(context.BookInfoes.Where(x => x.BookID == bookID).Select(x => x.Status).First().ToString()) == true)
                    {
                        ((FormMain)this.MdiParent).SetToolStripLabelText = "Invalid Request. Book is already on loan.";
                        return;
                    }
                }
                catch (InvalidOperationException)
                {
                    ((FormMain)this.MdiParent).SetToolStripLabelText = "Invalid Operation!";
                }
                try
                {
                    Member memberinfo = context.Members.Where(x => x.MemberID == memberID).First();
                }
                catch (InvalidOperationException)
                {
                    ((FormMain)this.MdiParent).SetToolStripLabelText = "Invalid operation. Please make sure enter a valid Member ID and Book ID!";
                    return;
                }

                int createTransID = ++currentTransID;


                //need to have some function to bind bookinfo to the database
                foreach (BookInfo i in booksToLoan)
                {
                    BookInfo bookPersistence = context.BookInfoes.Where(x => x.BookID == i.BookID).First();
                    bookPersistence.Status = "OUT";

                    BookCatalogue bookInvenPersis = context.BookCatalogues.Where(x => x.ISBN == bookPersistence.ISBN).First();
                    bookInvenPersis.StockInLibrary = --bookInvenPersis.StockInLibrary;
                    bookInvenPersis.NumberOnLoan   = ++bookInvenPersis.NumberOnLoan;
                }


                //create new loan transaction
                LoanTran newTrans = new LoanTran();

                newTrans.TransID       = createTransID;
                newTrans.MemberID      = memberID;
                newTrans.NumberOfBooks = booksToLoan.Count();
                context.LoanTrans.Add(newTrans);

                //Create new transDetails information
                foreach (BookInfo i in booksToLoan)
                {
                    int         newSn        = int.Parse(context.TransDetails.OrderByDescending(x => x.Sn).OrderByDescending(x => x.Sn).First().Sn.ToString());
                    TransDetail transDetails = new TransDetail();
                    transDetails.Sn     = ++newSn;
                    transDetails.BookID = i.BookID;
                    try
                    {
                        if (dateTimeLoanDatePicker.Value.Date > DateTime.Today || dateTimeDueDatePicker.Value < dateTimeLoanDatePicker.Value || dateTimeDueDatePicker.Value.Date < DateTime.Today)
                        {
                            throw new LoanException();
                        }
                    }
                    catch (LoanException)
                    {
                        ((FormMain)this.MdiParent).SetToolStripLabelText = "Invalid Loan or Due Date";
                        return;
                    }
                    transDetails.LoanDate = dateTimeLoanDatePicker.Value.Date;
                    transDetails.DueDate  = dateTimeDueDatePicker.Value.Date;
                    transDetails.TransID  = currentTransID;
                    context.TransDetails.Add(transDetails);
                    context.SaveChanges();
                }


                context.SaveChanges();
                ((FormMain)this.MdiParent).SetToolStripLabelText = "Loan Processed Succesfully!";
                booksToLoan.Clear();
                memberInfoClear();
                clearBookInfo();
            }
        }
Exemplo n.º 4
0
        //create new entry in BookCatalogue and BookInfo tables
        private void btnSave_Click(object sender, EventArgs e)
        {
            using (TransactionScope addBookTransScope = new TransactionScope())
            {
                ((FormMain)this.MdiParent).SetToolStripLabelText = "...";
                try
                {
                    int totalStock = Convert.ToInt16(txtTotalStock.Text);
                }
                catch (FormatException)
                {
                    ((FormMain)this.MdiParent).SetToolStripLabelText = "Invalid book number entered in the stock field.";
                    return;
                }

                if (Math.Abs(Convert.ToInt16(txtTotalStock.Text)) > 100)
                {
                    ((FormMain)this.MdiParent).SetToolStripLabelText = "Too many books added. Maximum allowable is 100 books per transaction.";
                    return;
                }
                if (dateTimePickerPub.Value > DateTime.Today)
                {
                    ((FormMain)this.MdiParent).SetToolStripLabelText = "Year published cannot be in the future";
                    return;
                }
                BookCatalogue bookCatalogue = new BookCatalogue();

                try
                {
                    bookCatalogue.ISBN = Convert.ToDouble(txtISBN.Text);
                }
                catch (FormatException)
                {
                    ((FormMain)this.MdiParent).SetToolStripLabelText = "Invalid ISBN String Format.";
                    return;
                }

                double ISBNinput;

                {
                    if (Convert.ToDouble(txtISBN.Text) > 1000000000000)
                    {
                        ISBNinput = Convert.ToDouble(txtISBN.Text);
                        if (doesISBNexist(ISBNinput) == true)
                        {
                            ((FormMain)this.MdiParent).SetToolStripLabelText = "ISBN already exists. Please use 'Update Book' function to update book information.";
                            return;
                        }
                        else
                        {
                            bookCatalogue.ISBN = ISBNinput;
                        }
                    }
                    else
                    {
                        InvalidLabel(lblISBN);
                        return;
                    }
                }

                bookCatalogue.BookTitle         = txtTitle.Text;
                bookCatalogue.Author            = txtauthor.Text;
                bookCatalogue.YearOfPublication = dateTimePickerPub.Value;
                bookCatalogue.Publisher         = txtPub.Text;
                bookCatalogue.CallNumber        = txtCall.Text;
                bookCatalogue.ShelfNumber       = txtShelf.Text;
                bookCatalogue.Description       = txtdesc.Text;
                bookCatalogue.StockInLibrary    = Math.Abs(Convert.ToInt16(txtTotalStock.Text));
                bookCatalogue.Subject           = cbosub.Text;
                bookCatalogue.Type         = cbotype.Text;
                bookCatalogue.NumberOnLoan = 0;


                if (string.IsNullOrEmpty(txtISBN.Text) ||
                    (string.IsNullOrEmpty(txtTitle.Text) ||
                     (string.IsNullOrEmpty(txtauthor.Text) ||
                      (string.IsNullOrEmpty(txtTotalStock.Text)))))

                {
                    ((FormMain)this.MdiParent).SetToolStripLabelText = "Please enter ISBN, Title and Author";
                    return;
                }

                context.BookCatalogues.Add(bookCatalogue);
                context.SaveChanges();


                for (int i = 0; i <= Convert.ToInt16(txtTotalStock.Text); i++)
                {
                    double bookIDnew = context.BookInfoes.OrderByDescending(x => x.BookID).First().BookID;

                    BookInfo bookInfo = new BookInfo();
                    bookInfo.BookID    = ++bookIDnew;
                    bookInfo.ISBN      = Convert.ToDouble(txtISBN.Text);
                    bookInfo.BookTitle = txtTitle.Text;
                    bookInfo.Remarks   = "";
                    bookInfo.Status    = "IN";
                    context.BookInfoes.Add(bookInfo);
                    context.SaveChanges();
                }

                context.SaveChanges();
                MessageBox.Show("New Book has been saved!");
                this.Close();
            }
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            using (TransactionScope updateBookTransScope = new TransactionScope())
            {
                int booksNewStock;
                try
                {
                    booksNewStock = Math.Abs(Convert.ToInt16(txtNewStock.Text));
                }
                catch (FormatException)
                {
                    MessageBox.Show("Error! Please enter a valid number in the New Stock field.");
                    return;
                }
                if (booksNewStock > 100)
                {
                    MessageBox.Show("Too many books added. Maximum allowable is 100 books per transaction.");
                    return;
                }
                BookCatalogue bkCatalogue = context.BookCatalogues.Where(x => x.ISBN == inputISBN).First();
                bkCatalogue.BookTitle         = txtTitle.Text;
                bkCatalogue.Author            = txtauthor.Text;
                bkCatalogue.YearOfPublication = dateTimePickerPub.Value;
                bkCatalogue.Publisher         = txtPub.Text;
                bkCatalogue.Description       = txtDesc.Text;
                bkCatalogue.Type        = cbotType.Text;
                bkCatalogue.Subject     = cboSub.Text;
                bkCatalogue.CallNumber  = txtCall.Text;
                bkCatalogue.ShelfNumber = txtShelf.Text;
                try
                {
                    bkCatalogue.StockInLibrary = Convert.ToInt16(txtTotalStock.Text) + booksNewStock;
                }
                catch (Exception ex)
                {
                    if (ex is FormatException)
                    {
                        MessageBox.Show("Invalid Character entered. Please enter a number for number of New Stock arrived");
                        return;
                    }
                    else if (ex is OverflowException)
                    {
                        MessageBox.Show("Invalid number entry entered. Maximum allowedable number is 37000 books");
                        return;
                    }
                }

                if (booksNewStock > 0)

                {
                    for (int i = 0; i < booksNewStock - 1; i++)
                    {
                        double   bookIDnew = double.Parse(context.BookInfoes.OrderByDescending(x => x.BookID).First().BookID.ToString());
                        BookInfo bookInfo  = new BookInfo();
                        bookInfo.BookID    = ++bookIDnew;
                        bookInfo.BookTitle = txtTitle.Text;
                        bookInfo.ISBN      = Convert.ToDouble(txtISBN.Text);
                        bookInfo.BookTitle = txtTitle.Text;
                        bookInfo.Remarks   = "";
                        bookInfo.Status    = "IN";
                        context.BookInfoes.Add(bookInfo);
                        context.SaveChanges();
                    }
                }
                context.SaveChanges();
                MessageBox.Show("Book updated successfully!");
                this.Close();
            }
        }