Exemple #1
0
        public static void DeleteAccount(int AccountId)
        {
            if (!CanWork(AccountId))
            {
                throw new AccessDeniedException();
            }

            int ParentId = DBFinance.GetParentAccountId(AccountId);

            if (ParentId <= 0)
            {
                throw new AccessDeniedException();
            }

            using (DbTransaction tran = DbTransaction.Begin())
            {
                // Улалим счёт и всех его детей
                DBFinance.DeleteAccount(AccountId);

                // Найдём всех сестёр
                ArrayList siblings = new ArrayList();
                using (IDataReader reader = DBFinance.GetListChildrenAccounts(ParentId))
                {
                    while (reader.Read())
                    {
                        siblings.Add((int)reader["AccountId"]);
                    }
                }

                if (siblings.Count == 0)
                {
                    DBFinance.UpdateIsSummary(ParentId, false);
                }
                else
                {
                    ProcessRenumber(ParentId, siblings);
                }

                // Пересчёт вверх по иерархии
                RecalculateParentAccounts(AccountId, false);

                tran.Commit();
            }
        }
Exemple #2
0
        // Перенумерация OutlineNumber и OutlineLevel
        private static void ProcessRenumber(int AccountId, ArrayList children)
        {
            string OutlineNumber;
            int    OutlineLevel;

            using (IDataReader reader = DBFinance.GetAccount(AccountId))
            {
                reader.Read();
                OutlineNumber = reader["OutlineNumber"].ToString();
                OutlineLevel  = (int)reader["OutlineLevel"];
            }

            int pos = 0;

            foreach (int childId in children)
            {
                // Найдём всех внуков
                ArrayList grandChildren = new ArrayList();
                using (IDataReader reader = DBFinance.GetListChildrenAccounts(childId))
                {
                    while (reader.Read())
                    {
                        grandChildren.Add((int)reader["AccountId"]);
                    }
                }

                // Обновим OutlineNumber
                pos++;
                string newNumber = String.Format("{0}.{1}", OutlineNumber, pos);
                DBFinance.UpdateOutlineLevelAndNumber(childId, OutlineLevel + 1, newNumber);

                // Рекурсия
                if (grandChildren.Count > 0)
                {
                    ProcessRenumber(childId, grandChildren);
                }
            }
        }
Exemple #3
0
        public static void MoveAccount(int AccountId, int NewParentId)
        {
            // Проверка на соответствие проектов:
            int oldProjectId = -1;
            int newProjectId = -1;

            using (IDataReader reader = DBFinance.GetAccount(AccountId))
            {
                reader.Read();
                oldProjectId = (int)reader["ProjectId"];
            }
            using (IDataReader reader = DBFinance.GetAccount(NewParentId))
            {
                reader.Read();
                newProjectId = (int)reader["ProjectId"];
            }
            if (oldProjectId != newProjectId)
            {
                throw new AccessDeniedException();
            }

            // Проверка на права
            if (!Project.CanEditFinances(newProjectId))
            {
                throw new AccessDeniedException();
            }

            // Проверка на дочерние элементы
            bool CanMove = false;

            using (IDataReader reader = DBFinance.GetListAccountsForMove(AccountId))
            {
                while (reader.Read())
                {
                    if ((int)reader["AccountId"] == NewParentId)
                    {
                        CanMove = true;
                        break;
                    }
                }
            }
            if (!CanMove)
            {
                throw new AccessDeniedException();
            }

            // Предварительная работа
            int oldParentId = DBFinance.GetParentAccountId(AccountId);

            // Найдём всех детей (для перенумерации)
            ArrayList children = new ArrayList();

            using (IDataReader reader = DBFinance.GetListChildrenAccounts(AccountId))
            {
                while (reader.Read())
                {
                    children.Add((int)reader["AccountId"]);
                }
            }

            // Обработка
            using (DbTransaction tran = DbTransaction.Begin())
            {
                // Move
                DBFinance.MoveAccount(AccountId, NewParentId);

                // Найдём всех бывших сестёр (для перенумерации)
                ArrayList siblings = new ArrayList();
                using (IDataReader reader = DBFinance.GetListChildrenAccounts(oldParentId))
                {
                    while (reader.Read())
                    {
                        siblings.Add((int)reader["AccountId"]);
                    }
                }

                // Перенумеруем новую ветку
                ProcessRenumber(AccountId, children);

                // Обработаем старую ветку
                if (siblings.Count == 0)
                {
                    DBFinance.UpdateIsSummary(oldParentId, false);
                }
                else
                {
                    ProcessRenumber(oldParentId, siblings);
                }

                // Пересчитаем новую ветку
                RecalculateParentAccounts(AccountId, false);

                // Пересчитаем старую ветку
                RecalculateParentAccounts(oldParentId, true);

                tran.Commit();
            }
        }