mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-20 17:54:30 +08:00
support set balance when creating account
This commit is contained in:
@@ -135,6 +135,11 @@ func (a *AccountsApi) AccountCreateHandler(c *core.Context) (interface{}, *errs.
|
|||||||
return nil, errs.ErrParentAccountCannotSetCurrency
|
return nil, errs.ErrParentAccountCannotSetCurrency
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if accountCreateReq.Balance != 0 {
|
||||||
|
log.WarnfWithRequestId(c, "[accounts.AccountCreateHandler] parent account cannot set balance")
|
||||||
|
return nil, errs.ErrParentAccountCannotSetBalance
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < len(accountCreateReq.SubAccounts); i++ {
|
for i := 0; i < len(accountCreateReq.SubAccounts); i++ {
|
||||||
subAccount := accountCreateReq.SubAccounts[i]
|
subAccount := accountCreateReq.SubAccounts[i]
|
||||||
|
|
||||||
@@ -345,6 +350,7 @@ func (a *AccountsApi) createNewAccountModel(uid int64, accountCreateReq *models.
|
|||||||
Icon: accountCreateReq.Icon,
|
Icon: accountCreateReq.Icon,
|
||||||
Color: accountCreateReq.Color,
|
Color: accountCreateReq.Color,
|
||||||
Currency: accountCreateReq.Currency,
|
Currency: accountCreateReq.Currency,
|
||||||
|
Balance: accountCreateReq.Balance,
|
||||||
Comment: accountCreateReq.Comment,
|
Comment: accountCreateReq.Comment,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-5
@@ -9,9 +9,10 @@ var (
|
|||||||
ErrAccountHaveNoSubAccount = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 3, http.StatusBadRequest, "account must have at least one sub account")
|
ErrAccountHaveNoSubAccount = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 3, http.StatusBadRequest, "account must have at least one sub account")
|
||||||
ErrAccountCannotHaveSubAccounts = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 4, http.StatusBadRequest, "account cannot have sub accounts")
|
ErrAccountCannotHaveSubAccounts = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 4, http.StatusBadRequest, "account cannot have sub accounts")
|
||||||
ErrParentAccountCannotSetCurrency = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 5, http.StatusBadRequest, "parent account cannot set currency")
|
ErrParentAccountCannotSetCurrency = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 5, http.StatusBadRequest, "parent account cannot set currency")
|
||||||
ErrSubAccountCategoryNotEqualsToParent = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 6, http.StatusBadRequest, "sub account category not equals to parent")
|
ErrParentAccountCannotSetBalance = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 6, http.StatusBadRequest, "parent account cannot set balance")
|
||||||
ErrSubAccountTypeInvalid = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 7, http.StatusBadRequest, "sub account type invalid")
|
ErrSubAccountCategoryNotEqualsToParent = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 7, http.StatusBadRequest, "sub account category not equals to parent")
|
||||||
ErrCannotAddOrDeleteSubAccountsWhenModify = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 8, http.StatusBadRequest, "cannot add or delete sub accounts when modify account")
|
ErrSubAccountTypeInvalid = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 8, http.StatusBadRequest, "sub account type invalid")
|
||||||
ErrSourceAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 9, http.StatusBadRequest, "source account not found")
|
ErrCannotAddOrDeleteSubAccountsWhenModify = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 9, http.StatusBadRequest, "cannot add or delete sub accounts when modify account")
|
||||||
ErrDestinationAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 10, http.StatusBadRequest, "destination account not found")
|
ErrSourceAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 10, http.StatusBadRequest, "source account not found")
|
||||||
|
ErrDestinationAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 11, http.StatusBadRequest, "destination account not found")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ type AccountCreateRequest struct {
|
|||||||
Icon int64 `json:"icon,string" binding:"required,min=1"`
|
Icon int64 `json:"icon,string" binding:"required,min=1"`
|
||||||
Color string `json:"color" binding:"required,len=6,validHexRGBColor"`
|
Color string `json:"color" binding:"required,len=6,validHexRGBColor"`
|
||||||
Currency string `json:"currency" binding:"required,len=3,validCurrency"`
|
Currency string `json:"currency" binding:"required,len=3,validCurrency"`
|
||||||
|
Balance int64 `json:"balance"`
|
||||||
Comment string `json:"comment" binding:"max=255"`
|
Comment string `json:"comment" binding:"max=255"`
|
||||||
SubAccounts []*AccountCreateRequest `json:"subAccounts" binding:"omitempty"`
|
SubAccounts []*AccountCreateRequest `json:"subAccounts" binding:"omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/mayswind/lab/pkg/utils"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
@@ -100,7 +101,10 @@ func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAcc
|
|||||||
return errs.ErrUserIdInvalid
|
return errs.ErrUserIdInvalid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
now := time.Now().Unix()
|
||||||
|
|
||||||
allAccounts := make([]*models.Account, len(childrenAccounts)+1)
|
allAccounts := make([]*models.Account, len(childrenAccounts)+1)
|
||||||
|
var allInitTransactions []*models.Transaction
|
||||||
|
|
||||||
mainAccount.AccountId = s.GenerateUuid(uuid.UUID_TYPE_ACCOUNT)
|
mainAccount.AccountId = s.GenerateUuid(uuid.UUID_TYPE_ACCOUNT)
|
||||||
allAccounts[0] = mainAccount
|
allAccounts[0] = mainAccount
|
||||||
@@ -117,10 +121,31 @@ func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAcc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
transactionTime := utils.GetTransactionTimeFromUnixTime(now)
|
||||||
|
|
||||||
for i := 0; i < len(allAccounts); i++ {
|
for i := 0; i < len(allAccounts); i++ {
|
||||||
allAccounts[i].Deleted = false
|
allAccounts[i].Deleted = false
|
||||||
allAccounts[i].CreatedUnixTime = time.Now().Unix()
|
allAccounts[i].CreatedUnixTime = now
|
||||||
allAccounts[i].UpdatedUnixTime = time.Now().Unix()
|
allAccounts[i].UpdatedUnixTime = now
|
||||||
|
|
||||||
|
if allAccounts[i].Balance != 0 {
|
||||||
|
newTransaction := &models.Transaction{
|
||||||
|
TransactionId: s.GenerateUuid(uuid.UUID_TYPE_TRANSACTION),
|
||||||
|
Uid: allAccounts[i].Uid,
|
||||||
|
Deleted: false,
|
||||||
|
Type: models.TRANSACTION_TYPE_MODIFY_BALANCE,
|
||||||
|
TransactionTime: transactionTime,
|
||||||
|
SourceAccountId: allAccounts[i].AccountId,
|
||||||
|
DestinationAccountId: allAccounts[i].AccountId,
|
||||||
|
SourceAmount: allAccounts[i].Balance,
|
||||||
|
DestinationAmount: allAccounts[i].Balance,
|
||||||
|
CreatedUnixTime: now,
|
||||||
|
UpdatedUnixTime: now,
|
||||||
|
}
|
||||||
|
|
||||||
|
transactionTime++
|
||||||
|
allInitTransactions = append(allInitTransactions, newTransaction)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.UserDataDB(mainAccount.Uid).DoTransaction(func(sess *xorm.Session) error {
|
return s.UserDataDB(mainAccount.Uid).DoTransaction(func(sess *xorm.Session) error {
|
||||||
@@ -133,6 +158,15 @@ func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAcc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(allInitTransactions); i++ {
|
||||||
|
transaction := allInitTransactions[i]
|
||||||
|
_, err := sess.Insert(transaction)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -172,7 +206,7 @@ func (s *AccountService) HideAccount(uid int64, ids []int64, hidden bool) error
|
|||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
|
||||||
updateModel := &models.Account{
|
updateModel := &models.Account{
|
||||||
Hidden: hidden,
|
Hidden: hidden,
|
||||||
UpdatedUnixTime: now,
|
UpdatedUnixTime: now,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,7 +256,7 @@ func (s *AccountService) DeleteAccounts(uid int64, ids []int64) error {
|
|||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
|
||||||
updateModel := &models.Account{
|
updateModel := &models.Account{
|
||||||
Deleted: true,
|
Deleted: true,
|
||||||
DeletedUnixTime: now,
|
DeletedUnixTime: now,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,3 +11,7 @@ func FormatToLongDateTime(t time.Time) string {
|
|||||||
func ParseFromLongDateTime(t string) (time.Time, error) {
|
func ParseFromLongDateTime(t string) (time.Time, error) {
|
||||||
return time.Parse(LongDateTimeFormat, t)
|
return time.Parse(LongDateTimeFormat, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetTransactionTimeFromUnixTime(unixTime int64) int64 {
|
||||||
|
return unixTime * 1000
|
||||||
|
}
|
||||||
|
|||||||
@@ -300,6 +300,7 @@ export default {
|
|||||||
'account must have at least one sub account': 'Account must have at least one sub account',
|
'account must have at least one sub account': 'Account must have at least one sub account',
|
||||||
'account cannot have sub accounts': 'Account cannot have sub accounts',
|
'account cannot have sub accounts': 'Account cannot have sub accounts',
|
||||||
'parent account cannot set currency': 'Parent account cannot set currency',
|
'parent account cannot set currency': 'Parent account cannot set currency',
|
||||||
|
'parent account cannot set balance': 'Parent account cannot set balance',
|
||||||
'sub account category not equals to parent': 'Sub account category does not equal to parent',
|
'sub account category not equals to parent': 'Sub account category does not equal to parent',
|
||||||
'sub account type invalid': 'Sub account type is invalid',
|
'sub account type invalid': 'Sub account type is invalid',
|
||||||
'cannot add or delete sub accounts when modify account': 'You cannot add or delete sub accounts when modify account',
|
'cannot add or delete sub accounts when modify account': 'You cannot add or delete sub accounts when modify account',
|
||||||
|
|||||||
@@ -300,6 +300,7 @@ export default {
|
|||||||
'account must have at least one sub account': '账户必须包含至少一个子账户',
|
'account must have at least one sub account': '账户必须包含至少一个子账户',
|
||||||
'account cannot have sub accounts': '账户不能包含子账户',
|
'account cannot have sub accounts': '账户不能包含子账户',
|
||||||
'parent account cannot set currency': '父账户不能设置货币',
|
'parent account cannot set currency': '父账户不能设置货币',
|
||||||
|
'parent account cannot set balance': '父账户不能设置余额',
|
||||||
'sub account category not equals to parent': '子账户类别与父账户不同',
|
'sub account category not equals to parent': '子账户类别与父账户不同',
|
||||||
'sub account type invalid': '子账户类型无效',
|
'sub account type invalid': '子账户类型无效',
|
||||||
'cannot add or delete sub accounts when modify account': '您不能在修改账户时添加或删除子账户',
|
'cannot add or delete sub accounts when modify account': '您不能在修改账户时添加或删除子账户',
|
||||||
|
|||||||
Reference in New Issue
Block a user