public void SetBillingAccount(AssociationBillingAccount billingAccount)
 {
     BillingAccount = billingAccount;
     BillingAccount.Users.ForEach(u => u.SupressValidationOnPropertyChanges = true);
 }
     private void FixupChargeBack(AssociationBillingAccount previousValue)
     {
         if (IsDeserializing)
         {
             return;
         }
 
         if (ChargeBack != null)
         {
             ChargeBackId = ChargeBack.Id;
         }
 
         if (ChangeTracker.ChangeTrackingEnabled)
         {
             if (ChangeTracker.OriginalValues.ContainsKey("ChargeBack")
                 && (ChangeTracker.OriginalValues["ChargeBack"] == ChargeBack))
             {
                 ChangeTracker.OriginalValues.Remove("ChargeBack");
             }
             else
             {
                 ChangeTracker.RecordOriginalValue("ChargeBack", previousValue);
             }
             if (ChargeBack != null && !ChargeBack.ChangeTracker.ChangeTrackingEnabled)
             {
                 ChargeBack.StartTracking();
             }
         }
     }
     public bool Equals(AssociationBillingAccount other)
     {
         if (ReferenceEquals(null, other)) return false;
         if (ReferenceEquals(this, other)) return true;
 		if (other.Id == 0 && Id == 0)
 			return false;
 		else
 			return other.Id == Id;
     }
        public AssociationBillingAccount SaveBillingAccountUsers(AssociationBillingAccount billingAccount)
        {
            using (TransactionScope scope = new TransactionScope())
            using (var db = new LomsContext())
            {
                var userCache = new Dictionary<int, AssociationUser>();

                var existedUsers = new ObservableCollection<AssociationBillingAccountUser>((from cbu in db.AssociationBillingAccountUsers.Include("User")
                                                                                            where cbu.BillingAccountId == billingAccount.Id
                                                                                            select cbu));
                bool hasErrors = false;
                foreach (var billingAccountUser in billingAccount.Users)
                {
                    AssociationUser user = null;

                    var existed = existedUsers.FirstOrDefault(u => u.UserId == billingAccountUser.UserId);
                    if (existed != null)
                    {
                        if (!userCache.TryGetValue(billingAccountUser.UserId, out user))
                        {
                            user = (from u in db.AssociationUsers
                                    where u.Id == billingAccountUser.UserId
                                    select u)
                                    .SingleOrDefault();

                            if (user != null)
                                userCache[user.Id] = user;
                        }

                        if (user != null && user.Email == billingAccountUser.Email)
                        {
                            existedUsers.Remove(existed);
                            continue;
                        }
                    }

                    user = (from u in db.AssociationUsers
                            where u.AssociationId == CurrentAssociationId && u.Email == billingAccountUser.Email
                            select u)
                            .SingleOrDefault();

                    if (user != null)
                        userCache[user.Id] = user;

                    if (user == null || !user.HasOnlineAccess)
                        billingAccountUser.Errors.AddError("Email", "User is not registered!");

                    if (billingAccountUser.Errors.Count() > 0 || hasErrors)
                    {
                        hasErrors = true;
                        continue;
                    }


                    //add
                    billingAccountUser.BillingAccountId = billingAccount.Id;
                    billingAccountUser.UserId = user.Id;
                    billingAccountUser.Email = user.Email;
                    billingAccountUser.FirstName = user.FirstName;
                    billingAccountUser.LastName = user.LastName;
                    db.AssociationBillingAccountUsers.ApplyChanges(billingAccountUser);
                    db.SaveChanges();
                }

                if (hasErrors)
                {
                    billingAccount.AddError("Users", "Invalid email addresses");
                    return billingAccount;
                }
                else
                {
                    foreach (var existed in existedUsers)
                        db.AssociationBillingAccountUsers.DeleteObject(existed);

                    db.SaveChanges();
                }

                scope.Complete();

                return billingAccount;
            }
        }
     private void FixupBillingAccount(AssociationBillingAccount previousValue)
     {
         if (IsDeserializing)
         {
             return;
         }
 
         if (BillingAccount != null)
         {
             BillingAccountId = BillingAccount.Id;
         }
 
         if (ChangeTracker.ChangeTrackingEnabled)
         {
             if (ChangeTracker.OriginalValues.ContainsKey("BillingAccount")
                 && (ChangeTracker.OriginalValues["BillingAccount"] == BillingAccount))
             {
                 ChangeTracker.OriginalValues.Remove("BillingAccount");
             }
             else
             {
                 ChangeTracker.RecordOriginalValue("BillingAccount", previousValue);
             }
             if (BillingAccount != null && !BillingAccount.ChangeTracker.ChangeTrackingEnabled)
             {
                 BillingAccount.StartTracking();
             }
         }
     }