예제 #1
0
 /// <summary>
 /// Adds an invoice line item to the database.
 /// </summary>
 /// <param name="invoiceLineItems">Invoice line item to be added.</param>
 public static void AddInvoiceLineItem(InvoiceLineItems invoiceLineItems)
 {
     using (LumberContext context = new LumberContext())
     {
         context.InvoiceLineItems.Add(invoiceLineItems);
         context.SaveChanges();
     }
 }
        /// <summary>
        /// Adds a product to an invoice.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddBtn_Click(object sender, EventArgs e)
        {
            Invoice invoice = (Invoice)invoiceCmbBox.SelectedItem;

            if (invoice != null)
            {
                Product product = (Product)productCmbBox.SelectedItem;
                if (product == null)
                {
                    MessageBox.Show("No product selected", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                InvoiceLineItems invoiceLineItems = new InvoiceLineItems();
                invoiceLineItems.ProductID = product.ProductID;
                invoiceLineItems.InvoiceID = invoice.InvoiceID;

                if (Validator.IsShort(quantityTxtBox.Text))
                {
                    short quantity = Convert.ToInt16(quantityTxtBox.Text);
                    invoiceLineItems.Quantity = quantity;

                    try
                    {
                        ProductDb.AddInvoiceLineItem(invoiceLineItems);
                        product.Sold += quantity;
                        ProductDb.Update(product);
                        quantityTxtBox.Text = String.Empty;
                        messageLbl.Text     = "Product added successfully";
                    }
                    catch (SqlException)
                    {
                        messageLbl.Text = "Failed to add product";
                    }
                }
            }
            else
            {
                MessageBox.Show("No invoice is selected", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }