Ejemplo n.º 1
0
        private void bDeletar_Click(object sender, EventArgs e)
        {
            PFContext       context      = new PFContext();
            int             indice       = this.dataGridView1.CurrentRow.Index;
            DataGridViewRow rowData      = dataGridView1.Rows[indice];
            String          aux          = rowData.Cells[5].Value.ToString().Trim();
            int             clienteIdAux = Convert.ToInt32(aux);
            Cliente         pf           = context.PessoaFisicas.Single(x => x.ClienteID == clienteIdAux);

            string            message = "Deseja realmente remover este Cliente?";
            string            caption = "Error";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult      result;



            result = MessageBox.Show(message, caption, buttons);

            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                context.PessoaFisicas.Attach(pf);
                context.PessoaFisicas.Remove(pf);
                context.SaveChanges();
                MessageBox.Show("Cliente removido com sucesso.", "Alerta");
                this.PreencheGrid();
            }
        }
Ejemplo n.º 2
0
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            if (nameSourceOfIncome.Text.Length == 0)
            {
                errorText.Text = "Введите название";
                return;
            }

            using (PFContext db = new PFContext())
            {
                if (income != null)
                {
                    income.Name = nameSourceOfIncome.Text;
                    db.SourceOfIncome.Update(income);
                }
                else
                {
                    SourceOfIncome incomeNew = new SourceOfIncome
                    {
                        Name = nameSourceOfIncome.Text
                    };
                    db.SourceOfIncome.Add(incomeNew);
                }
                db.SaveChanges();
            }
            GoToPreviousPage();
        }
Ejemplo n.º 3
0
        // Add CostCategories
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            if (nameCostCategories.Text.Length == 0)
            {
                errorText.Text = "Введите название";
                return;
            }

            using (PFContext db = new PFContext())
            {
                if (costCategor != null)
                {
                    costCategor.Name = nameCostCategories.Text;
                    db.CostCategories.Update(costCategor);
                }
                else
                {
                    CostCategories costCategorNew = new CostCategories
                    {
                        Name = nameCostCategories.Text
                    };
                    db.CostCategories.Add(costCategorNew);
                }
                db.SaveChanges();
            }
            GoToPreviousPage();
        }
Ejemplo n.º 4
0
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            string value = ConvertToStringFormat(rateBox.Text);
            double finalRate;

            if (!Double.TryParse(ConvertToStringFormat(value), out finalRate))
            {
                errorText.Text = "Некоректный курс";
                return;
            }
            if (finalRate <= 0)
            {
                errorText.Text = "Некоректный курс";
                return;
            }

            using (PFContext db = new PFContext())
            {
                currency.Rate = finalRate;
                db.Currency.Update(currency);

                db.SaveChanges();
            }
            GoToPreviousPage();
        }
        private void DeleteAccumulationItem(Accumulation a)
        {
            using (PFContext db = new PFContext())
            {
                try
                {
                    foreach (AccumulationOperation ao in db.AccumulationOperation)
                    {
                        if (ao.AccumulationId == a.Id)
                        {
                            db.AccumulationOperation.Remove(ao);
                        }
                    }

                    db.Accumulation.Remove(a);
                    db.SaveChanges();

                    accumulationCollection = new ObservableCollection <Accumulation>(db.Accumulation
                                                                                     .Include(x => x.Currency).ToList());
                    accumulationList.ItemsSource = accumulationCollection;
                }
                catch
                {
                    errorText.Text = "Не возможно удалить цель. Попробуйте позже.";
                }
            }
        }
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            string   currentSum = ConvertToStringFormat(accumCurrentSum.Text);
            string   finalSum   = ConvertToStringFormat(accumFinalSum.Text);
            Currency cur        = currencyList.SelectedItem as Currency;
            double   cSumm;
            double   fSumm;

            if (accumNameBox.Text.Length == 0)
            {
                errorText.Text = "Введите имя";
                return;
            }
            if (!Double.TryParse(currentSum, out cSumm) || !Double.TryParse(finalSum, out fSumm))
            {
                errorText.Text = "Некоректная сумма";
                return;
            }
            if (cur == null)
            {
                errorText.Text = "Выберите валюту";
                return;
            }

            using (PFContext db = new PFContext())
            {
                if (accumulation != null)
                {
                    accumulation.Name         = accumNameBox.Text;
                    accumulation.CurrentSumma = cSumm;
                    accumulation.FinalSumma   = fSumm;
                    accumulation.Currency     = cur;
                    accumulation.CurrencyId   = cur.Id;

                    db.Accumulation.Update(accumulation);
                }
                else
                {
                    Accumulation accumulationNew = new Accumulation
                    {
                        Name         = accumNameBox.Text,
                        CurrentSumma = cSumm,
                        FinalSumma   = fSumm,
                        CurrencyId   = cur.Id,
                        Currency     = cur
                    };
                    db.Currency.Attach(cur);
                    db.Accumulation.Add(accumulationNew);
                }
                db.SaveChanges();
            }
            GoToPreviousPage();
        }
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            double sum;
            Purse  purseElement = purseListCB.SelectedItem as Purse;

            if (purseElement == null)
            {
                errorText.Text = "Не выбран счет";
                return;
            }
            if (!Double.TryParse(ConvertToStringFormat(accumulationSum.Text), out sum))
            {
                errorText.Text = "Некоректная сумма";
                return;
            }
            if (sum < 0)
            {
                errorText.Text = "Некоректная сумма";
                return;
            }

            using (PFContext db = new PFContext())
            {
                Purse    purseUpdate;
                Currency currrencyElement = db.Currency.FirstOrDefault(c => c.Id == purseElement.CurrencyId);
                AccumulationOperation accumulationOperation = new AccumulationOperation
                {
                    AccumulationId = accumulation.Id,
                    Summa          = sum,
                    PurseId        = purseElement.Id,
                    CurrencyId     = currrencyElement.Id,
                    Purse          = purseElement,
                    Currency       = currrencyElement,
                    Accumulation   = accumulation
                };
                db.Purse.Attach(purseElement);
                db.Currency.Attach(currrencyElement);
                db.Accumulation.Attach(accumulation);
                /* Update Purse */
                purseUpdate         = db.Purse.FirstOrDefault(p => p.Id == accumulationOperation.PurseId);
                purseUpdate.Balance = purseUpdate.Balance - accumulationOperation.Summa;
                db.Purse.Update(purseUpdate);
                /* Update Accumulation */
                accumulation.CurrentSumma += accumulationOperation.Summa;
                db.Update(accumulation);

                db.AccumulationOperation.Add(accumulationOperation);
                db.SaveChanges();
            }
            GoToPreviousPage();
        }
Ejemplo n.º 8
0
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            string   value = ConvertToStringFormat(purseBalanceBox.Text);
            double   balance;
            Currency cur = currencyList.SelectedItem as Currency;

            if (purseNameBox.Text.Length == 0)
            {
                errorText.Text = "Введите имя счета";
                return;
            }
            if (!Double.TryParse(value, out balance))
            {
                errorText.Text = "Некоректный баланс";
                return;
            }
            if (cur == null)
            {
                errorText.Text = "Выберите валюту";
                return;
            }

            using (PFContext db = new PFContext())
            {
                if (purse != null)
                {
                    purse.Name       = purseNameBox.Text;
                    purse.Balance    = balance;
                    purse.CurrencyId = cur.Id;
                    purse.Currency   = cur;
                    db.Purse.Update(purse);
                }
                else
                {
                    Purse purseNew = new Purse
                    {
                        Name       = purseNameBox.Text,
                        Balance    = balance,
                        Currency   = cur,
                        CurrencyId = cur.Id
                    };
                    db.Currency.Attach(cur);
                    db.Purse.Add(purseNew);
                }
                db.SaveChanges();
            }
            GoToPreviousPage();
        }
Ejemplo n.º 9
0
        private void btSalvar_Click(object sender, EventArgs e)
        {
            if (modificaPessoa == false)
            {
                string   nome      = tbNome.Text;
                string   endereco  = tbEndereco.Text;
                string   cpf       = tbCpf.Text;
                string   telefone  = tbTelefone.Text;
                DateTime data_nasc = DateTime.Parse(tbData_Nasc.Text);
                Cliente  pf        = new Cliente();

                pf.Nome      = nome;
                pf.Endereco  = endereco;
                pf.Cpf       = cpf;
                pf.Telefone  = telefone;
                pf.Data_Nasc = data_nasc;

                var context = new PFContext();
                context.PessoaFisicas.Add(pf);

                context.SaveChanges();
                MessageBox.Show("Cliente Inserido com sucesso.", "Alerta");
                LimpaCamposPessoa();
                PreencheGrid();
            }
            else
            {
                var     context = new PFContext();
                int     aux     = Convert.ToInt32(id);
                Cliente pf      = context.PessoaFisicas.Single(x => x.ClienteID == aux);

                pf.Nome      = tbNome.Text.ToString();
                pf.Endereco  = tbEndereco.Text.ToString();
                pf.Cpf       = tbCpf.Text.ToString();
                pf.Data_Nasc = DateTime.Parse(tbData_Nasc.Text);
                pf.Telefone  = tbTelefone.Text.ToString();

                context.PessoaFisicas.Attach(pf);
                context.Entry(pf).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();
                LimpaCamposPessoa();

                MessageBox.Show("Cliente Alterado com sucesso.", "Alerta");
                modificaPessoa = false;
                PreencheGrid();
            }
        }
        private void DeleteIncomeItem(Income i)
        {
            Purse purseChangeBalance;

            using (PFContext db = new PFContext())
            {
                db.Income.Remove(i);
                purseChangeBalance         = db.Purse.FirstOrDefault(p => p.Id == i.PurseId);
                purseChangeBalance.Balance = purseChangeBalance.Balance - i.Summa;
                db.Purse.Update(purseChangeBalance);
                db.SaveChanges();

                incomesCollection = new ObservableCollection <Income>(db.Income.Include(x => x.SourceOfIncome)
                                                                      .Include(x => x.Purse).Include(x => x.Currency).ToList());
                SortIncomeDateDesc(incomesCollection);
            }
        }
Ejemplo n.º 11
0
        private void DeletePurseItem(Purse p)
        {
            using (PFContext db = new PFContext())
            {
                try
                {
                    foreach (Costs c in db.Costs)
                    {
                        if (c.PurseId == p.Id)
                        {
                            db.Costs.Remove(c);
                        }
                    }

                    foreach (Income i in db.Income)
                    {
                        if (i.PurseId == p.Id)
                        {
                            db.Income.Remove(i);
                        }
                    }
                    foreach (Displacement d in db.Displacement)
                    {
                        if (d.PurseId1 == p.Id || d.PurseId2 == p.Id)
                        {
                            db.Displacement.Remove(d);
                        }
                    }
                    foreach (AccumulationOperation a in db.AccumulationOperation)
                    {
                        if (a.PurseId == p.Id)
                        {
                            db.AccumulationOperation.Remove(a);
                        }
                    }

                    db.Purse.Remove(p);
                    db.SaveChanges();
                    purseList.ItemsSource = db.Purse.ToList();
                }
                catch
                {
                    errorText.Text = "Удаление не возможно.Повторите попытку позже";
                }
            }
        }
        private void DeleteCostItem(Costs c)
        {
            Purse purseChangeBalance;

            using (PFContext db = new PFContext())
            {
                db.Costs.Remove(c);
                purseChangeBalance         = db.Purse.FirstOrDefault(p => p.Id == c.PurseId);
                purseChangeBalance.Balance = purseChangeBalance.Balance + c.Summa;
                db.Purse.Update(purseChangeBalance);
                db.SaveChanges();

                costsCollection = new ObservableCollection <Costs>(db.Costs.Include(x => x.CostCategories)
                                                                   .Include(x => x.Purse).Include(x => x.Currency).ToList());
                SortCostDateDesc(costsCollection);
            }
        }
        private void DeleteSourceOfIncomeItem(SourceOfIncome sourceOfIncome)
        {
            Purse purseChangeBalance;

            using (PFContext db = new PFContext())
            {
                foreach (Income i in db.Income)
                {
                    if (i.SourceOfIncomeId == sourceOfIncome.Id)
                    {
                        db.Income.Remove(i);
                        purseChangeBalance         = db.Purse.FirstOrDefault(p => p.Id == i.PurseId);
                        purseChangeBalance.Balance = purseChangeBalance.Balance - i.Summa;
                        db.Purse.Update(purseChangeBalance);
                    }
                }
                db.SourceOfIncome.Remove(sourceOfIncome);
                db.SaveChanges();
                sourceOfIncomeList.ItemsSource = db.SourceOfIncome.ToList();
            }
        }
        private void DeleteCostCategorItem(CostCategories costCategor)
        {
            Purse purseChangeBalance;

            using (PFContext db = new PFContext())
            {
                foreach (Costs c in db.Costs)
                {
                    if (c.CostCategoriesId == costCategor.Id)
                    {
                        db.Costs.Remove(c);
                        purseChangeBalance         = db.Purse.FirstOrDefault(p => p.Id == c.PurseId);
                        purseChangeBalance.Balance = purseChangeBalance.Balance + c.Summa;
                        db.Purse.Update(purseChangeBalance);
                    }
                }
                db.CostCategories.Remove(costCategor);
                db.SaveChanges();
                costCategorList.ItemsSource = db.CostCategories.ToList();
            }
        }
        private void DeleteDisplacementItem(Displacement d)
        {
            Purse purseChangeBalances1;
            Purse purseChangeBalances2;

            using (PFContext db = new PFContext())
            {
                db.Displacement.Remove(d);
                purseChangeBalances1 = db.Purse.FirstOrDefault(p => p.Id == d.PurseId1);
                purseChangeBalances2 = db.Purse.FirstOrDefault(p => p.Id == d.PurseId2);

                purseChangeBalances1.Balance += d.SummaOut;
                purseChangeBalances2.Balance -= d.SummaIncome;

                db.Purse.Update(purseChangeBalances1);
                db.Purse.Update(purseChangeBalances2);

                db.SaveChanges();

                displacementCollection = new ObservableCollection <Displacement>(db.Displacement.Include(x => x.Currency).ToList());
                SortDisplacementDateDesc(displacementCollection);
            }
        }
Ejemplo n.º 16
0
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            Purse          pList = purseListCB.SelectedItem as Purse;
            SourceOfIncome sList = incomeCategorListCB.SelectedItem as SourceOfIncome;
            double         sum;

            DateTimeOffset date       = (DateTimeOffset)dateIncome.Date;
            string         dateFormat = date.Date.ToString("dd.MM.yyyy");

            if (pList == null)
            {
                errorText.Text = "Выберите счет";
                return;
            }
            if (!Double.TryParse(ConvertToStringFormat(incomeSum.Text), out sum))
            {
                errorText.Text = "Некоректная сумма";
                return;
            }
            if (sum < 0)
            {
                errorText.Text = "Некоректная сумма";
                return;
            }
            if (sList == null)
            {
                errorText.Text = "Выберите категорию";
                return;
            }
            //if (incomeComment.Text.Length == 0)
            //{
            //    incomeComment.Text = " ";
            //}

            using (PFContext db = new PFContext())
            {
                if (income != null)
                {
                    int    purseID_Old    = income.PurseId;
                    double summaIncom_Old = income.Summa;
                    currency = db.Currency.FirstOrDefault(c => c.Id == pList.CurrencyId);

                    if (pList.Id == purseID_Old && summaIncom_Old == sum) // неизменный кошелек и сумма
                    {
                        income.Summa            = sum;
                        income.DateOperation    = dateFormat;
                        income.PurseId          = pList.Id;
                        income.SourceOfIncomeId = sList.Id;
                        income.CurrencyId       = pList.CurrencyId;
                        income.Comment          = incomeComment.Text;
                        income.Purse            = pList;
                        income.SourceOfIncome   = sList;
                        income.Currency         = currency;
                    }
                    else if (pList.Id == purseID_Old && summaIncom_Old != sum) // изменяется только сумма
                    {
                        Purse purseChangeSumElement = db.Purse.FirstOrDefault(p => p.Id == pList.Id);
                        purseChangeSumElement.Balance = purseChangeSumElement.Balance + (sum - summaIncom_Old);
                        db.Purse.Update(purseChangeSumElement);

                        income.Summa            = sum;
                        income.DateOperation    = dateFormat;
                        income.PurseId          = pList.Id;
                        income.SourceOfIncomeId = sList.Id;
                        income.CurrencyId       = pList.CurrencyId;
                        income.Comment          = incomeComment.Text;
                        income.Purse            = pList;
                        income.SourceOfIncome   = sList;
                        income.Currency         = currency;
                    }
                    else if (pList.Id != purseID_Old)  //изменяется кошелек
                    {
                        Purse purseChangeOld;
                        Purse purseChangeNew;

                        purseChangeOld         = db.Purse.FirstOrDefault(p => p.Id == purseID_Old);
                        purseChangeOld.Balance = purseChangeOld.Balance - summaIncom_Old;
                        db.Purse.Update(purseChangeOld);

                        purseChangeNew         = db.Purse.FirstOrDefault(p => p.Id == pList.Id);
                        purseChangeNew.Balance = purseChangeNew.Balance + sum;
                        db.Purse.Update(purseChangeNew);

                        income.Summa            = sum;
                        income.DateOperation    = dateFormat;
                        income.PurseId          = pList.Id;
                        income.SourceOfIncomeId = sList.Id;
                        income.CurrencyId       = pList.CurrencyId;
                        income.Comment          = incomeComment.Text;
                        income.Purse            = pList;
                        income.SourceOfIncome   = sList;
                        income.Currency         = currency;
                    }
                    db.Income.Update(income);
                }
                else
                {
                    currency = db.Currency.FirstOrDefault(c => c.Id == pList.CurrencyId);
                    Income incomeNew = new Income
                    {
                        Summa            = sum,
                        DateOperation    = dateFormat,
                        PurseId          = pList.Id,
                        SourceOfIncomeId = sList.Id,
                        Comment          = incomeComment.Text,
                        Purse            = pList,
                        SourceOfIncome   = sList,
                        CurrencyId       = pList.CurrencyId,
                        Currency         = currency
                    };
                    db.Purse.Attach(pList);
                    db.SourceOfIncome.Attach(sList);
                    db.Currency.Attach(currency);
                    /* Update Purse */
                    purse         = db.Purse.FirstOrDefault(p => p.Id == incomeNew.PurseId);
                    purse.Balance = purse.Balance + incomeNew.Summa;
                    db.Purse.Update(purse);

                    db.Income.Add(incomeNew);
                }
                db.SaveChanges();
            }
            GoToPreviousPage();
        }
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            Purse          pList  = purseListCB.SelectedItem as Purse;
            CostCategories ccList = costCategorListCB.SelectedItem as CostCategories;
            double         sum;

            DateTimeOffset date       = (DateTimeOffset)dateCost.Date;
            string         dateFormat = date.Date.ToString("dd.MM.yyyy");


            if (pList == null)
            {
                errorText.Text = "Выберите счет";
                return;
            }
            if (!Double.TryParse(ConvertToStringFormat(costSum.Text), out sum))
            {
                errorText.Text = "Некоректная сумма";
                return;
            }
            if (sum < 0)
            {
                errorText.Text = "Некоректная сумма";
                return;
            }
            if (ccList == null)
            {
                errorText.Text = "Выберите категорию";
                return;
            }
            //if(costComment.Text.Length == 0)
            //{
            //    costComment.Text = " ";
            //}
            //dateFormat = dateFormat.ToString("");
            using (PFContext db = new PFContext())
            {
                if (cost != null)
                {
                    int    purseId_Old   = cost.PurseId;
                    double summaCost_Old = cost.Summa;
                    currency = db.Currency.FirstOrDefault(c => c.Id == pList.CurrencyId);

                    if (pList.Id == purseId_Old && summaCost_Old == sum) // неизменный кошелек и сумма
                    {
                        cost.Summa            = sum;
                        cost.DateOperation    = dateFormat;
                        cost.PurseId          = pList.Id;
                        cost.CostCategoriesId = ccList.Id;
                        cost.Comment          = costComment.Text;
                        cost.Purse            = pList;
                        cost.CostCategories   = ccList;
                        cost.CurrencyId       = pList.CurrencyId;
                        cost.Currency         = currency;
                    }
                    else if (pList.Id == purseId_Old && summaCost_Old != sum) // изменяется только сумма
                    {
                        Purse purseChangeSumElement = db.Purse.FirstOrDefault(p => p.Id == pList.Id);
                        purseChangeSumElement.Balance = purseChangeSumElement.Balance - (sum - summaCost_Old);
                        db.Purse.Update(purseChangeSumElement);

                        cost.Summa            = sum;
                        cost.DateOperation    = dateFormat;
                        cost.PurseId          = pList.Id;
                        cost.CostCategoriesId = ccList.Id;
                        cost.Comment          = costComment.Text;
                        cost.Purse            = pList;
                        cost.CostCategories   = ccList;
                        cost.CurrencyId       = pList.CurrencyId;
                        cost.Currency         = currency;
                    }

                    else if (pList.Id != purseId_Old) //изменяется кошелек
                    {
                        Purse purseChangeOld;
                        Purse purseChangeNew;

                        purseChangeOld         = db.Purse.FirstOrDefault(p => p.Id == purseId_Old);
                        purseChangeOld.Balance = purseChangeOld.Balance + summaCost_Old;
                        db.Purse.Update(purseChangeOld);

                        purseChangeNew         = db.Purse.FirstOrDefault(p => p.Id == pList.Id);
                        purseChangeNew.Balance = purseChangeNew.Balance - sum;
                        db.Purse.Update(purseChangeNew);

                        cost.Summa            = sum;
                        cost.DateOperation    = dateFormat;
                        cost.PurseId          = pList.Id;
                        cost.CostCategoriesId = ccList.Id;
                        cost.Comment          = costComment.Text;
                        cost.Purse            = pList;
                        cost.CostCategories   = ccList;
                        cost.CurrencyId       = pList.CurrencyId;
                        cost.Currency         = currency;
                    }

                    db.Costs.Update(cost);
                }
                else
                {
                    currency = db.Currency.FirstOrDefault(c => c.Id == pList.CurrencyId);
                    Costs costNew = new Costs
                    {
                        Summa            = sum,
                        DateOperation    = dateFormat,
                        PurseId          = pList.Id,
                        CostCategoriesId = ccList.Id,
                        Comment          = costComment.Text,
                        Purse            = pList,
                        CostCategories   = ccList,
                        CurrencyId       = pList.CurrencyId,
                        Currency         = currency
                    };
                    db.Purse.Attach(pList);
                    db.CostCategories.Attach(ccList);
                    db.Currency.Attach(currency);
                    /* Update Purse*/
                    purse         = db.Purse.FirstOrDefault(p => p.Id == costNew.PurseId);
                    purse.Balance = purse.Balance - costNew.Summa;
                    db.Purse.Update(purse);

                    db.Costs.Add(costNew);
                }
                db.SaveChanges();
            }
            GoToPreviousPage();
        }
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            purseSelect1 = purse_1ListCB.SelectedItem as Purse;
            purseSelect2 = purse_2ListCB.SelectedItem as Purse;

            DateTimeOffset date       = (DateTimeOffset)dateDispl.Date;
            string         dateFormat = date.Date.ToString("dd.MM.yyyy");

            double summa;
            double result;
            double curRate1;
            double curRate2;

            #region Verification
            if (part1.Visibility == Visibility.Collapsed)
            {
                rate1.Text = "1";
            }
            if (part2.Visibility == Visibility.Collapsed)
            {
                rate2.Text = "1";
            }
            if (CurrencesRates.Visibility == Visibility.Collapsed)
            {
                rate1.Text = "1";
                rate2.Text = "1";
            }

            if (purseSelect1 == null || purseSelect2 == null)
            {
                errorText.Text = "Выберите счет";
                return;
            }
            if (purseSelect1.Id == purseSelect2.Id)
            {
                errorText.Text = "Выберите разные счета";
                return;
            }
            if (!Double.TryParse(ConvertToStringFormat(summaDispl.Text), out summa))
            {
                errorText.Text = "Некоректная сумма";
                return;
            }
            if (!Double.TryParse(ConvertToStringFormat(rate1.Text), out curRate1))
            {
                errorText.Text = "Некоректный курс1";
                return;
            }
            if (!Double.TryParse(ConvertToStringFormat(rate2.Text), out curRate2))
            {
                errorText.Text = "Некоректный курс2";
                return;
            }
            #endregion
            curRate1 = Math.Round(curRate1, 2);
            curRate2 = Math.Round(curRate2, 2);
            result   = summa * curRate1 / curRate2;
            result   = Math.Round(result, 2);

            using (PFContext db = new PFContext())
            {
                if (displacement != null)
                {
                    Purse    purse1Return      = db.Purse.FirstOrDefault(p => p.Id == displacement.PurseId1);
                    Purse    purse2Return      = db.Purse.FirstOrDefault(p => p.Id == displacement.PurseId2);
                    Currency currencyForUpdate = db.Currency.FirstOrDefault(c => c.Id == purseSelect2.CurrencyId);

                    purse1Return.Balance += displacement.SummaOut;
                    purse2Return.Balance -= displacement.SummaIncome;

                    db.Purse.Update(purse1Return);
                    db.Purse.Update(purse2Return);

                    displacement.PurseId1      = purseSelect1.Id;
                    displacement.PurseId2      = purseSelect2.Id;
                    displacement.SummaOut      = summa;
                    displacement.SummaIncome   = result;
                    displacement.DateOperation = dateFormat;
                    displacement.RateCur1      = curRate1;
                    displacement.RateCur2      = curRate2;
                    displacement.CurrencyId    = purseSelect2.CurrencyId;
                    displacement.Currency      = currencyForUpdate;

                    purseUpdate1 = db.Purse.FirstOrDefault(p => p.Id == displacement.PurseId1);
                    purseUpdate2 = db.Purse.FirstOrDefault(p => p.Id == displacement.PurseId2);

                    purseUpdate1.Balance -= displacement.SummaOut;
                    purseUpdate2.Balance += displacement.SummaIncome;

                    db.Purse.Update(purseUpdate1);
                    db.Purse.Update(purseUpdate2);

                    db.Displacement.Update(displacement);
                }
                else
                {
                    currency = db.Currency.FirstOrDefault(c => c.Id == purseSelect2.CurrencyId);
                    Displacement displacementNew = new Displacement
                    {
                        PurseId1      = purseSelect1.Id,
                        PurseId2      = purseSelect2.Id,
                        SummaOut      = summa,
                        SummaIncome   = result,
                        DateOperation = dateFormat,
                        RateCur1      = curRate1,
                        RateCur2      = curRate2,
                        CurrencyId    = purseSelect2.CurrencyId,
                        //Purse = purseSelect2,
                        Currency = currency
                    };
                    //db.Purse.Attach(purseSelect2);
                    db.Currency.Attach(currency);
                    db.Displacement.Add(displacementNew);
                    /* Update Purse */
                    purseUpdate1          = db.Purse.FirstOrDefault(p => p.Id == displacementNew.PurseId1);
                    purseUpdate2          = db.Purse.FirstOrDefault(p => p.Id == displacementNew.PurseId2);
                    purseUpdate1.Balance -= displacementNew.SummaOut;
                    purseUpdate2.Balance += displacementNew.SummaIncome;
                    db.Purse.Update(purseUpdate1);
                    db.Purse.Update(purseUpdate2);
                }
                db.SaveChanges();
            }
            GoToPreviousPage();

            errorText.Text = "--- Result --- " + result.ToString();
        }