You've already forked openaccounting-server
mirror of
https://github.com/openaccounting/oa-server.git
synced 2025-12-09 09:00:42 +13:00
Merge pull request #12 from pnagurny/bug/account-parent
fix update account parent bugs
This commit is contained in:
@@ -91,6 +91,17 @@ func (model *Model) UpdateAccount(account *types.Account, userId string) (err er
|
|||||||
return errors.New("account cannot be its own parent")
|
return errors.New("account cannot be its own parent")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// don't allow setting parent that has transactions
|
||||||
|
count, err := model.db.GetSplitCountByAccountId(account.Parent)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if count != 0 {
|
||||||
|
return errors.New("cannot set parent to account with transactions")
|
||||||
|
}
|
||||||
|
|
||||||
userAccounts, err := model.GetAccounts(account.OrgId, userId, "")
|
userAccounts, err := model.GetAccounts(account.OrgId, userId, "")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -101,6 +112,16 @@ func (model *Model) UpdateAccount(account *types.Account, userId string) (err er
|
|||||||
return errors.New(fmt.Sprintf("%s %s", "user does not have permission to access account", account.Parent))
|
return errors.New(fmt.Sprintf("%s %s", "user does not have permission to access account", account.Parent))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure parent is not a descendant of account
|
||||||
|
accountMap := model.makeAccountMap(userAccounts)
|
||||||
|
children := model.getChildren(account.Id, accountMap)
|
||||||
|
|
||||||
|
for _, childAccount := range children {
|
||||||
|
if childAccount.Id == account.Parent {
|
||||||
|
return errors.New("cannot set parent to descendant account")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = model.db.UpdateAccount(account)
|
err = model.db.UpdateAccount(account)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user