private static void RecalculateParentAccountWithPreserving(int AccountId) { int ParentId = DBFinance.GetParentAccountId(AccountId); if (ParentId > 0) { DBFinance.RecalculateParentAccountWithPreserving(ParentId, Security.CurrentUser.UserID); } }
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(); } }
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(); } }