public MobileTransactionViewModel(MobileTransaction mobileTransaction)
 {
     ID = mobileTransaction.ID;
     MobileOperator = new MobileOperatorViewModel(mobileTransaction.MobileOperator);
     CreditSum = mobileTransaction.CreditSum;
     if (mobileTransaction.Transaction.Price != null) Price = mobileTransaction.Transaction.Price.Value;
     if (mobileTransaction.Transaction.Contragent != null) Client = new ContragentViewModel(mobileTransaction.Transaction.Contragent);
     Salesman = new SalesmanViewModel(mobileTransaction.Transaction.Operator.Salesman);
     Date = mobileTransaction.Transaction.Date;
     Profit = Price - CreditSum;
 }
        private void Add()
        {
            if (SelectedOperator == null) _mainViewModel.MobileTransactionsViewModel.AddDialogViewModel = null;

            Transaction transaction = new Transaction
            {
                Date = DateTime.Now,
                TypeID = (int) TranType.TopUp,
                OperatorID = OperatorManager.Instance.CurrentUserID,
                Price = Price
            };

            LastName = Trim(LastName);
            FirstName = Trim(FirstName);
            if (!string.IsNullOrEmpty(LastName) && !string.IsNullOrEmpty(FirstName))
            {
                var client =_mainViewModel.Context.Clients.FirstOrDefault(
                    x => x.Contragent.FirstName == FirstName && x.Contragent.LastName == LastName);

                if (client == null)
                {
                    var contragent = new Contragent
                    {
                        LastName = LastName,
                        FirstName = FirstName
                    };

                    client = new Client
                    {
                        Contragent = contragent
                    };

                    _mainViewModel.Context.Clients.Add(client);
                }

                transaction.Contragent = client.Contragent;
            }

            MobileTransaction mobileTransaction = new MobileTransaction
            {
                Transaction = transaction,
                CreditSum = CreditSum,
                MobileOperatorID = SelectedOperator.ID
            };

            _mainViewModel.Context.MobileTransactions.Add(mobileTransaction);
            try
            {
                _mainViewModel.Context.SaveChanges();
            }
            catch (Exception e)
            {
                MessageBox.Show(("Не удалось провести пополнение"), "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                Logging.WriteToLog("Failed add new mobile transaction. " + e.Message);
            }

            _mainViewModel.MobileTransactionsViewModel.Update();
            _mainViewModel.CashInHand += Price;

            _mainViewModel.MobileTransactionsViewModel.AddDialogViewModel = null;
        }