public static DateTime GetLastMaterialClosureDateTime(ModelTMSContainer Context)
        {
            DateTime LastClosure = GetSystemSettingDateTime(Context, "LastMaterialClosureDateTime", new DateTime(2000, 1, 1));

            if (LastClosure.Year == 2000)
            {
                // the date has not been set

                // assume today is the last closure datetime
                LastClosure = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 23, 59, 59);

                // check if this is correct
                try
                {
                    MaterialMutation mt = Context.MaterialMutationSet.OrderBy(o => o.CreateDateTime).First <MaterialMutation>();
                    if (mt != null)
                    {
                        // there have been earlier material mutations. Use that date.
                        LastClosure = new DateTime(mt.MutationDateTime.Year, mt.MutationDateTime.Month, mt.MutationDateTime.Day, 23, 59, 59);
                    }
                }
                catch
                {
                }
            }
            LastClosure = new DateTime(LastClosure.Year, LastClosure.Month, LastClosure.Day, 23, 59, 59);
            return(LastClosure);
        }
        protected void ButtonProcess_Click(object sender, EventArgs e)
        {
            Double MatPrice = 0, MatAmount = 0;

            try
            {
                MatPrice  = Convert.ToDouble(TextBoxPrice.Text);
                MatAmount = Convert.ToDouble(TextBoxAmount.Text);
            }
            catch
            {
            }

            if ((MatPrice.ToString() != TextBoxPrice.Text) || (MatAmount.ToString() != TextBoxAmount.Text))
            {
                Common.InformUser(Page, "De opgegeven getallen voor prijs en hoeveelheid kunnen niet worden herkend. Corrigeer deze naar de juiste waarde.");
                TextBoxPrice.Text  = MatPrice.ToString();
                TextBoxAmount.Text = MatAmount.ToString();
            }
            else
            {
                // process this as a Material Mutation
                MaterialMutation mt = new MaterialMutation();

                mt.Material     = (DataItem as Material);
                mt.Description  = "CORR / Correctie voorraad " + mt.Material.Description + " dd " + mt.CreateDateTime.ToString();
                mt.MutationType = RadioButtonListBuyOrSell.SelectedValue;
                mt.Amount       = MatAmount;
                mt.PricePerUnit = MatPrice;
                mt.TotalPrice   = MatPrice * mt.Amount;
                mt.GenerateMutationNumber(ControlObjectContext);

                if (mt.MutationType == "Sell")
                {
                    mt.Amount = -mt.Amount; // sales are always a negative amount
                }

                mt.RecalcTotals();
                mt.ProcessToStockLevel(false); // do not update average prices
                mt.ProcessToLedgerMutation(ControlObjectContext);

                if (mt.MutationType == "Sell")
                {
                    mt.TotalPrice = MatPrice * mt.Amount; // sales are stored as a negative price so that the total calculation will be correct on the daily closure
                }

                ControlObjectContext.AddToMaterialMutationSet(mt);
                ControlObjectContext.SaveChanges();
                Common.InformUser(Page, "Deze mutatie is succesvol doorgevoerd. U kunt dit popup scherm nu sluiten of nog een mutatie op dit materiaal doorvoeren.");
            }
        }