public static bool CanWork(int AccountId) { int ProjectId = -1; using (IDataReader reader = DBFinance.GetAccount(AccountId)) { reader.Read(); ProjectId = (int)reader["ProjectId"]; } return(Project.CanEditFinances(ProjectId)); }
// Перенумерация 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); } } }
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(); } }
public static void UpdateEstimatedAccount(int AccountId, decimal TotalValue, bool PreserveParentValue) { if (!CanWork(AccountId)) { throw new AccessDeniedException(); } decimal ESub = 0; decimal ETotal = 0; using (IDataReader reader = DBFinance.GetAccount(AccountId)) { reader.Read(); ESub = (decimal)reader["ESub"]; // сумма дочерних ETotal = (decimal)reader["ETotal"]; // старый общий итог } // Новый итог не может быть меньше суммы старых дочерних значений if (TotalValue < ESub) { throw new WrongDataException(); } // Oleg Rylin [5/23/2006] // Проверка на то, чтобы не вылезти из родительского диапазона // Т.е. разница между тем, что было (ETotal) и тем, что стало (TotalValue), // не должна быть больше ECur родителя (parentECur) if (PreserveParentValue) { decimal parentECur = 0; int ParentId = DBFinance.GetParentAccountId(AccountId); if (ParentId > 0) { using (IDataReader reader = DBFinance.GetAccount(ParentId)) { reader.Read(); parentECur = (decimal)reader["ECur"]; // настолько можно изменить значение } if (TotalValue - ETotal > parentECur) { throw new WrongDataException(); } } } decimal ECur = TotalValue - ESub; using (DbTransaction tran = DbTransaction.Begin()) { DBFinance.UpdateAccountECur(AccountId, ECur, Security.CurrentUser.UserID); if (PreserveParentValue) { RecalculateParentAccountWithPreserving(AccountId); } else { RecalculateParentAccounts(AccountId, false); } tran.Commit(); } }