public void AddPractice(PracticeInfo practice)
        {
            try
            {
                CashFlowManagerEntities db = new CashFlowManagerEntities();

                if (practice.Id == Guid.Empty)
                    practice.Id = Guid.NewGuid();
                db.Practices.Add(practice.ToEntity<Practice>());
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }
        public void AddBankAccount(BankAccountInfo bankAccount)
        {
            try
            {
                CashFlowManagerEntities db = new CashFlowManagerEntities();

                if (bankAccount.Id == Guid.Empty)
                    bankAccount.Id = Guid.NewGuid();
                db.BankAccounts.Add(bankAccount.ToEntity<BankAccount>());
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }
        public void AddClient(List<ClientInfo> clients)
        {
            try
            {
                CashFlowManagerEntities db = new CashFlowManagerEntities();

                foreach (ClientInfo c in clients)
                {
                    if (c.Id == Guid.Empty)
                        c.Id = Guid.NewGuid();
                    db.Clients.Add(c.ToEntity<Client>());
                }

                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }
        public void AddTransactions(List<TransactionInfo> transactions)
        {
            try
            {
                CashFlowManagerEntities db = new CashFlowManagerEntities();

                foreach (TransactionInfo t in transactions)
                {
                    if (t.Id == Guid.Empty)
                        t.Id = Guid.NewGuid();
                    db.Transactions.Add(t.ToEntity<Transaction>());
                }

                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }
        public void UpdateTransaction(TransactionInfo transaction)
        {
            try
            {
                CashFlowManagerEntities db = new CashFlowManagerEntities();

                var tran = (from t in db.Transactions where t.Id == transaction.Id select t).SingleOrDefault();

                if (tran != null)
                {
                    tran.Narration = transaction.Narration;
                    tran.ScheduleId = transaction.ScheduleId;
                    tran.Amount = transaction.Amount;
                    tran.StartDate = transaction.StartDate;
                    tran.TransactionTypeId = transaction.TransactionTypeId;

                    db.Entry(tran).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }
 public void UpdateBankAccount(BankAccountInfo bankAccount)
 {
     try
     {
         CashFlowManagerEntities db = new CashFlowManagerEntities();
         BankAccount ba = (from b in db.BankAccounts where b.Id == bankAccount.Id select b).SingleOrDefault();
         if (ba != null)
         {
             ba.AccountName = bankAccount.AccountName;
             ba.AccountNumber = bankAccount.AccountNumber;
             ba.Balance = bankAccount.Balance;
             ba.ClientId = bankAccount.ClientId;
         }
         db.Entry(ba).State = EntityState.Modified;
         db.SaveChanges();
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         throw;
     }
 }
        public void DeleteTransaction(Guid transactionId)
        {
            try
            {
                CashFlowManagerEntities db = new CashFlowManagerEntities();

                var tran = (from t in db.Transactions where t.Id == transactionId select t).SingleOrDefault();

                if (tran != null)
                {
                    db.Transactions.Remove(tran);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }
 public void DeleteBankAccount(BankAccountInfo bankAccount)
 {
     try
     {
         CashFlowManagerEntities db = new CashFlowManagerEntities();
         BankAccount ba = (from b in db.BankAccounts where b.Id == bankAccount.Id select b).SingleOrDefault();
         if (ba != null)
         {
             db.BankAccounts.Remove(ba);
             db.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         throw;
     }
 }
        public void AssociateAndUpdateUser(int userId, Guid? practiceId, string email)
        {
            try
            {
                CashFlowManagerEntities db = new CashFlowManagerEntities();

                if (practiceId != Guid.Empty)
                {
                    var user = (from u in db.UserProfiles where u.UserId == userId select u).SingleOrDefault();

                    if (user != null)
                    {
                        user.PracticeId = practiceId;
                        user.Email = email;
                        db.Entry(user).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }