private void UpdateState()
 {
     try
     {
         long    lastStateChangeID = dbContext.Cashbox.Max(cb => cb.id);
         Cashbox lastStateInfo     = dbContext.Cashbox.Where(cb => cb.id == lastStateChangeID)
                                     .Select(stateInfo => stateInfo).First();
         lblStateInfo.Text = string.Format("Last State Change: {0}", lastStateInfo.last_change_dttm);
         lblAmount.Text    = string.Format("Available Amount: {0}$", lastStateInfo.amount_after);
     }
     catch (Exception)
     {
         MessageBox.Show("Cashbox State table is empty, please provide a starting amount."
                         , "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
 }
        private void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            if ("Load Cash".Equals(e.Button.Tag))
            {
                try
                {
                    long    lastStateChangeID = dbContext.Cashbox.Max(cb => cb.id);
                    Cashbox lastStateInfo     = dbContext.Cashbox.Where(cb => cb.id == lastStateChangeID)
                                                .Select(stateInfo => stateInfo).First();

                    Cashbox cashLoad = new Cashbox(lastStateInfo.amount_after, lastStateInfo.amount_after + 1000, DateTime.Now);
                    dbContext.Cashbox.Add(cashLoad);
                    EntityFrameworkTransactionsControl.Commit(dbContext);
                    UpdateState();
                }
                catch (Exception)
                {
                    MessageBox.Show("Cashbox State table is empty, please provide a starting amount."
                                    , "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            if ("Process".Equals(e.Button.Tag))
            {
                #region SaleMode
                if (saleMode == true)
                {
                    List <History> processableSales   = new List <History>();
                    int            totalSalesQuantity = 0;
                    decimal        cashGains          = 0M;
                    string         recipeHelper       = "";
                    int            recipeCount        = 1;
                    DateTime       dateOfSale         = DateTime.Now;

                    foreach (var sale in bsSalesHistory)
                    {
                        if (sale.quantity <= 0)
                        {
                            MessageBox.Show(string.Format("Skipping product {0}, because selected quantity is invalid.",
                                                          sale.product_name == null ? "[Unnamed]" : sale.product_name), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            continue;
                        }
                        sale.Date = dateOfSale;
                        if (dbContext.Products.Any(p => p.product_name == sale.product_name))
                        {
                            var anonymousProductDetails = dbContext.Products.
                                                          Where(p => p.product_name == sale.product_name).
                                                          Select(s => new { Price = s.price_sell, Id = s.id }).ToList();
                            long quantityId = anonymousProductDetails[0].Id;

                            StockQuantities quantityAccumulator = dbContext.StockQuantities
                                                                  .Where(sq => sq.product_id == quantityId)
                                                                  .FirstOrDefault();
                            if (quantityAccumulator != null)
                            {
                                if (quantityAccumulator.quantity - sale.quantity < 0)
                                {
                                    MessageBox.Show(
                                        string.Format("There are not enough items from product [{0}] in stock to finish the sale.", sale.product_name), "Information",
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    continue;
                                }
                                quantityAccumulator.quantity      -= sale.quantity;
                                quantityAccumulator.reference_date = dateOfSale;
                                EntityFrameworkTransactionsControl.Commit(dbContext);
                            }
                            else
                            {
                                MessageBox.Show(
                                    string.Format("There is no information about the quantity of product {0}. Unable to complete the sale of the current product.", sale.product_name),
                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                continue;
                            }

                            totalSalesQuantity += (int)sale.quantity;
                            cashGains          += sale.quantity * anonymousProductDetails[0].Price;
                            recipeHelper       +=
                                Environment.NewLine + string.Format("{0,-3})", recipeCount++) + string.Format("{0,-27}", sale.product_name) +
                                string.Format("{0,-13}", anonymousProductDetails[0].Price) + sale.quantity + " items.";

                            processableSales.Add(sale);
                        }
                        else
                        {
                            MessageBox.Show(string.Format("Product [{0}] is invalid. Please, register it first.", sale.product_name),
                                            "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    if (processableSales.Count > 0)
                    {
                        dbContext.History.AddRange(processableSales);
                        try
                        {
                            long lastStateChangeID = dbContext.Cashbox.Max(cb => cb.id);

                            decimal lastStateAmount = dbContext.Cashbox.
                                                      Where(cb => cb.id == lastStateChangeID).
                                                      Select(stateInfo => stateInfo).
                                                      First().amount_after;

                            dbContext.Cashbox.Add(new Cashbox(lastStateAmount, lastStateAmount + cashGains, DateTime.Now));
                            EntityFrameworkTransactionsControl.Commit(dbContext);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Cashbox State table is empty, please provide a starting amount.",
                                            "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            EntityFrameworkTransactionsControl.Rollback(dbContext);
                            return;
                        }

                        MessageBox.Show("Sale complete!" + Environment.NewLine + Environment.NewLine +
                                        "Product                       Price        Quantity" + Environment.NewLine +
                                        "---------------------------------------------------" +
                                        recipeHelper + Environment.NewLine +
                                        "---------------------------------------------------" + Environment.NewLine +
                                        string.Format("Total Items Sold {0}.", totalSalesQuantity) + Environment.NewLine +
                                        string.Format("Total Cash Earned {0}$.", cashGains), "Sales information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        UpdateState();
                    }
                }
                #endregion
                #region BuyMode
                else
                {
                    List <StockCharge> processChargeItems = new List <StockCharge>();
                    int      totalBoughtQuantity          = 0;
                    decimal  cashToPay    = 0M;
                    string   recipeHelper = "";
                    int      recipeCount  = 1;
                    DateTime dateOfBuying = DateTime.Now;

                    foreach (var charge in bsSalesHistory)
                    {
                        if (charge.quantity <= 0)
                        {
                            MessageBox.Show(string.Format("Skipping product {0}, because the selected quantity is invalid.",
                                                          charge.product_name == null ? "[Unnamed]" : charge.product_name),
                                            "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            continue;
                        }
                        charge.Date = dateOfBuying;
                        if (dbContext.Products.Any(p => p.product_name == charge.product_name))
                        {
                            var anonymousProductDetails = dbContext.Products.
                                                          Where(p => p.product_name == charge.product_name).
                                                          Select(s => new { Id = s.id, Price = s.price_charge }).ToList();
                            long quantityId = anonymousProductDetails[0].Id;

                            StockQuantities quantityAccumulator = dbContext.StockQuantities
                                                                  .Where(sq => sq.product_id == quantityId)
                                                                  .FirstOrDefault();
                            if (quantityAccumulator != null)
                            {
                                quantityAccumulator.quantity      += charge.quantity;
                                quantityAccumulator.reference_date = dateOfBuying;
                                EntityFrameworkTransactionsControl.Commit(dbContext);
                            }
                            else
                            {
                                MessageBox.Show(
                                    string.Format("There is no information about the quantity of product {0}. Unable to complete the purchase of the current product.", charge.product_name),
                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                continue;
                            }

                            totalBoughtQuantity += (int)charge.quantity;
                            cashToPay           += charge.quantity * anonymousProductDetails[0].Price;
                            recipeHelper        +=
                                Environment.NewLine + string.Format("{0,-3})", recipeCount++) + string.Format("{0,-27}", charge.product_name) +
                                string.Format("{0,-13}", anonymousProductDetails[0].Price) + charge.quantity + " items.";

                            processChargeItems.Add(new StockCharge(DateTime.Now, anonymousProductDetails[0].Id, charge.quantity));
                        }
                        else
                        {
                            MessageBox.Show(string.Format("Product [{0}] is invalid. Please, register it first.", charge.product_name),
                                            "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    if (processChargeItems.Count > 0)
                    {
                        dbContext.StockCharge.AddRange(processChargeItems);
                        try
                        {
                            long lastStateChangeID = dbContext.Cashbox.Max(cb => cb.id);

                            decimal lastStateAmount = dbContext.Cashbox.
                                                      Where(cb => cb.id == lastStateChangeID).
                                                      Select(stateInfo => stateInfo).
                                                      First().amount_after;
                            decimal remainingCash = lastStateAmount - cashToPay;
                            if (remainingCash > 0)
                            {
                                dbContext.Cashbox.Add(new Cashbox(lastStateAmount, remainingCash, DateTime.Now));
                                EntityFrameworkTransactionsControl.Commit(dbContext);
                            }
                            else
                            {
                                MessageBox.Show("There is not enough cash to pay the charge! Aborting the operation.",
                                                "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                EntityFrameworkTransactionsControl.Rollback(dbContext);
                                return;
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Cashbox State table is empty, please provide a starting amount.",
                                            "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            EntityFrameworkTransactionsControl.Rollback(dbContext);
                            return;
                        }

                        MessageBox.Show("Purchases complete!" + Environment.NewLine + Environment.NewLine +
                                        "Product                       Price        Quantity" + Environment.NewLine +
                                        "---------------------------------------------------" +
                                        recipeHelper + Environment.NewLine +
                                        "---------------------------------------------------" + Environment.NewLine +
                                        string.Format("Total Items Bought {0}.", totalBoughtQuantity) + Environment.NewLine +
                                        string.Format("Total Cash Payed {0}$.", cashToPay),
                                        "Purchases information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        UpdateState();
                    }
                }
                #endregion
            }
        }