Skip to content

Commit

Permalink
support setting the time of the initial balance when creating a new a…
Browse files Browse the repository at this point in the history
…ccount
  • Loading branch information
mayswind committed Nov 10, 2024
1 parent 06b4960 commit bff6ca7
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 10 deletions.
12 changes: 7 additions & 5 deletions pkg/api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (a *AccountsApi) AccountCreateHandler(c *core.WebContext) (any, *errs.Error
}

mainAccount := a.createNewAccountModel(uid, &accountCreateReq, maxOrderId+1)
childrenAccounts := a.createSubAccountModels(uid, &accountCreateReq)
childrenAccounts, childrenAccountBalanceTimes := a.createSubAccountModels(uid, &accountCreateReq)

if a.CurrentConfig().EnableDuplicateSubmissionsCheck && accountCreateReq.ClientSessionId != "" {
found, remark := a.GetSubmissionRemark(duplicatechecker.DUPLICATE_CHECKER_TYPE_NEW_ACCOUNT, uid, accountCreateReq.ClientSessionId)
Expand Down Expand Up @@ -255,7 +255,7 @@ func (a *AccountsApi) AccountCreateHandler(c *core.WebContext) (any, *errs.Error
}
}

err = a.accounts.CreateAccounts(c, mainAccount, childrenAccounts, utcOffset)
err = a.accounts.CreateAccounts(c, mainAccount, accountCreateReq.BalanceTime, childrenAccounts, childrenAccountBalanceTimes, utcOffset)

if err != nil {
log.Errorf(c, "[accounts.AccountCreateHandler] failed to create account \"id:%d\" for user \"uid:%d\", because %s", mainAccount.AccountId, uid, err.Error())
Expand Down Expand Up @@ -483,18 +483,20 @@ func (a *AccountsApi) createNewAccountModel(uid int64, accountCreateReq *models.
}
}

func (a *AccountsApi) createSubAccountModels(uid int64, accountCreateReq *models.AccountCreateRequest) []*models.Account {
func (a *AccountsApi) createSubAccountModels(uid int64, accountCreateReq *models.AccountCreateRequest) ([]*models.Account, []int64) {
if len(accountCreateReq.SubAccounts) <= 0 {
return nil
return nil, nil
}

childrenAccounts := make([]*models.Account, len(accountCreateReq.SubAccounts))
childrenAccountBalanceTimes := make([]int64, len(accountCreateReq.SubAccounts))

for i := int32(0); i < int32(len(accountCreateReq.SubAccounts)); i++ {
childrenAccounts[i] = a.createNewAccountModel(uid, accountCreateReq.SubAccounts[i], i+1)
childrenAccountBalanceTimes[i] = accountCreateReq.SubAccounts[i].BalanceTime
}

return childrenAccounts
return childrenAccounts, childrenAccountBalanceTimes
}

func (a *AccountsApi) getToUpdateAccount(uid int64, accountModifyReq *models.AccountModifyRequest, oldAccount *models.Account) *models.Account {
Expand Down
1 change: 1 addition & 0 deletions pkg/models/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type AccountCreateRequest struct {
Color string `json:"color" binding:"required,len=6,validHexRGBColor"`
Currency string `json:"currency" binding:"required,len=3,validCurrency"`
Balance int64 `json:"balance"`
BalanceTime int64 `json:"balanceTime" binding:"required,min=1"`
Comment string `json:"comment" binding:"max=255"`
SubAccounts []*AccountCreateRequest `json:"subAccounts" binding:"omitempty"`
ClientSessionId string `json:"clientSessionId"`
Expand Down
12 changes: 9 additions & 3 deletions pkg/services/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (s *AccountService) GetMaxSubAccountDisplayOrder(c core.Context, uid int64,
}

// CreateAccounts saves a new account model to database
func (s *AccountService) CreateAccounts(c core.Context, mainAccount *models.Account, childrenAccounts []*models.Account, utcOffset int16) error {
func (s *AccountService) CreateAccounts(c core.Context, mainAccount *models.Account, mainAccountBalanceTime int64, childrenAccounts []*models.Account, childrenAccountBalanceTimes []int64, utcOffset int16) error {
if mainAccount.Uid <= 0 {
return errs.ErrUserIdInvalid
}
Expand Down Expand Up @@ -230,8 +230,6 @@ func (s *AccountService) CreateAccounts(c core.Context, mainAccount *models.Acco
}
}

transactionTime := utils.GetMinTransactionTimeFromUnixTime(now)

for i := 0; i < len(allAccounts); i++ {
allAccounts[i].Deleted = false
allAccounts[i].CreatedUnixTime = now
Expand All @@ -244,6 +242,14 @@ func (s *AccountService) CreateAccounts(c core.Context, mainAccount *models.Acco
return errs.ErrSystemIsBusy
}

transactionTime := utils.GetMinTransactionTimeFromUnixTime(now)

if i == 0 && mainAccountBalanceTime > 0 {
transactionTime = utils.GetMinTransactionTimeFromUnixTime(mainAccountBalanceTime)
} else if i > 0 && len(childrenAccountBalanceTimes) > i-1 && childrenAccountBalanceTimes[i-1] > 0 {
transactionTime = utils.GetMinTransactionTimeFromUnixTime(childrenAccountBalanceTimes[i-1])
}

newTransaction := &models.Transaction{
TransactionId: transactionId,
Uid: allAccounts[i].Uid,
Expand Down
1 change: 1 addition & 0 deletions src/lib/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function setAccountModelByAnotherAccount(account, account2) {
account.color = account2.color;
account.currency = account2.currency;
account.balance = account2.balance;
account.balanceTime = account2.balanceTime;
account.comment = account2.comment;
account.visible = !account2.hidden;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export default {
getAccount: ({ id }) => {
return axios.get('v1/accounts/get.json?id=' + id);
},
addAccount: ({ category, type, name, icon, color, currency, balance, comment, subAccounts, clientSessionId }) => {
addAccount: ({ category, type, name, icon, color, currency, balance, balanceTime, comment, subAccounts, clientSessionId }) => {
return axios.post('v1/accounts/add.json', {
category,
type,
Expand All @@ -264,6 +264,7 @@ export default {
color,
currency,
balance,
balanceTime,
comment,
subAccounts,
clientSessionId
Expand Down
3 changes: 3 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,7 @@
"parentId": "Parent Node ID",
"categoryId": "Category ID",
"time": "Time",
"balanceTime": "Balance Time",
"startTime": "Start Time",
"endTime": "End Time",
"amountFilter": "Amount Filter",
Expand Down Expand Up @@ -1461,6 +1462,8 @@
"Currency": "Currency",
"Account Balance": "Account Balance",
"Sub-account Balance": "Sub-account Balance",
"Balance Time": "Balance Time",
"Sub-account Balance Time": "Sub-account Balance Time",
"Description": "Description",
"Your account description (optional)": "Your account description (optional)",
"Your sub-account description (optional)": "Your sub-account description (optional)",
Expand Down
3 changes: 3 additions & 0 deletions src/locales/zh_Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,7 @@
"parentId": "父节点ID",
"categoryId": "分类ID",
"time": "时间",
"balanceTime": "余额时间",
"startTime": "开始时间",
"endTime": "结束时间",
"amountFilter": "金额过滤",
Expand Down Expand Up @@ -1461,6 +1462,8 @@
"Currency": "货币",
"Account Balance": "账户余额",
"Sub-account Balance": "子账户余额",
"Balance Time": "余额时间",
"Sub-account Balance Time": "子账户余额时间",
"Description": "描述",
"Your account description (optional)": "你的账户描述 (可选)",
"Your sub-account description (optional)": "你的子账户描述 (可选)",
Expand Down
9 changes: 9 additions & 0 deletions src/stores/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import colorConstants from '@/consts/color.js';
import services from '@/lib/services.js';
import logger from '@/lib/logger.js';
import { isNumber, isEquals } from '@/lib/common.js';
import { getCurrentUnixTime } from '@/lib/datetime.js';
import { getCategorizedAccountsMap, getAllFilteredAccountsBalance } from '@/lib/account.js';

function loadAccountList(state, accounts) {
Expand Down Expand Up @@ -254,6 +255,7 @@ export const useAccountsStore = defineStore('accounts', {
actions: {
generateNewAccountModel() {
const userStore = useUserStore();
const now = getCurrentUnixTime();

return {
category: 1,
Expand All @@ -263,12 +265,14 @@ export const useAccountsStore = defineStore('accounts', {
color: colorConstants.defaultAccountColor,
currency: userStore.currentUserDefaultCurrency,
balance: 0,
balanceTime: now,
comment: '',
visible: true
};
},
generateNewSubAccountModel(parentAccount) {
const userStore = useUserStore();
const now = getCurrentUnixTime();

return {
category: null,
Expand All @@ -278,6 +282,7 @@ export const useAccountsStore = defineStore('accounts', {
color: parentAccount.color,
currency: userStore.currentUserDefaultCurrency,
balance: 0,
balanceTime: now,
comment: '',
visible: true
};
Expand Down Expand Up @@ -758,6 +763,8 @@ export const useAccountsStore = defineStore('accounts', {
if (isEdit) {
submitAccount.id = subAccount.id;
submitAccount.hidden = !subAccount.visible;
} else {
submitAccount.balanceTime = subAccount.balanceTime;
}

submitSubAccounts.push(submitAccount);
Expand All @@ -783,6 +790,8 @@ export const useAccountsStore = defineStore('accounts', {
if (isEdit) {
submitAccount.id = account.id;
submitAccount.hidden = !account.visible;
} else {
submitAccount.balanceTime = account.balanceTime;
}

const oldAccount = submitAccount.id ? self.allAccountsMap[submitAccount.id] : null;
Expand Down
14 changes: 13 additions & 1 deletion src/views/desktop/accounts/list/dialogs/EditDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,23 @@
</template>
</v-autocomplete>
</v-col>
<v-col cols="12" md="12" v-if="account.type === allAccountTypes.SingleAccount || currentAccountIndex >= 0">
<v-col cols="12" :md="!editAccountId && selectedAccount.balance ? 6 : 12"
v-if="account.type === allAccountTypes.SingleAccount || currentAccountIndex >= 0">
<amount-input :disabled="loading || submitting || !!editAccountId"
:persistent-placeholder="true"
:currency="selectedAccount.currency"
:label="currentAccountIndex < 0 ? $t('Account Balance') : $t('Sub-account Balance')"
:placeholder="currentAccountIndex < 0 ? $t('Account Balance') : $t('Sub-account Balance')"
v-model="selectedAccount.balance"/>
</v-col>
<v-col cols="12" md="6" v-show="selectedAccount.balance"
v-if="!editAccountId && (account.type === allAccountTypes.SingleAccount || currentAccountIndex >= 0)">
<date-time-select
:disabled="loading || submitting"
:label="$t('Balance Time')"
v-model="selectedAccount.balanceTime"
@error="showDateTimeError" />
</v-col>
<v-col cols="12" md="12">
<v-textarea
type="text"
Expand Down Expand Up @@ -452,6 +461,9 @@ export default {
this.subAccounts.push(subAccount);
}
}
},
showDateTimeError(error) {
this.$refs.snackbar.showError(error);
}
}
}
Expand Down
82 changes: 82 additions & 0 deletions src/views/mobile/accounts/EditPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,26 @@
></number-pad-sheet>
</f7-list-item>

<f7-list-item
class="account-edit-balancetime list-item-with-header-and-title"
link="#" no-chevron
v-show="account.balance"
v-if="!editAccountId"
>
<template #header>
<div class="account-edit-balancetime-header" @click="showDateTimeDialog(account, 'time')">{{ $t('Balance Time') }}</div>
</template>
<template #title>
<div class="account-edit-balancetime-title">
<div @click="showDateTimeDialog(account, 'date')">{{ getAccountBalanceDate(account.balanceTime) }}</div>&nbsp;<div class="account-edit-balancetime-time" @click="showDateTimeDialog(account, 'time')">{{ getAccountBalanceTime(account.balanceTime) }}</div>
</div>
</template>
<date-time-selection-sheet :init-mode="account.balanceDateTimeSheetMode"
v-model:show="account.showBalanceDateTimeSheet"
v-model="account.balanceTime">
</date-time-selection-sheet>
</f7-list-item>

<f7-list-item :title="$t('Visible')" v-if="editAccountId">
<f7-toggle :checked="account.visible" @toggle:change="account.visible = $event"></f7-toggle>
</f7-list-item>
Expand Down Expand Up @@ -381,6 +401,26 @@
></number-pad-sheet>
</f7-list-item>

<f7-list-item
class="account-edit-balancetime list-item-with-header-and-title"
link="#" no-chevron
v-show="subAccount.balance"
v-if="!editAccountId"
>
<template #header>
<div class="account-edit-balancetime-header" @click="showDateTimeDialog(subAccount, 'time')">{{ $t('Sub-account Balance Time') }}</div>
</template>
<template #title>
<div class="account-edit-balancetime-title">
<div @click="showDateTimeDialog(subAccount, 'date')">{{ getAccountBalanceDate(subAccount.balanceTime) }}</div>&nbsp;<div class="account-edit-balancetime-time" @click="showDateTimeDialog(subAccount, 'time')">{{ getAccountBalanceTime(subAccount.balanceTime) }}</div>
</div>
</template>
<date-time-selection-sheet :init-mode="subAccount.balanceDateTimeSheetMode"
v-model:show="subAccount.showBalanceDateTimeSheet"
v-model="subAccount.balanceTime">
</date-time-selection-sheet>
</f7-list-item>

<f7-list-item :title="$t('Visible')" v-if="editAccountId">
<f7-toggle :checked="subAccount.visible" @toggle:change="subAccount.visible = $event"></f7-toggle>
</f7-list-item>
Expand Down Expand Up @@ -433,6 +473,11 @@ import {
setAccountModelByAnotherAccount,
setAccountSuitableIcon
} from '@/lib/account.js';
import {
getTimezoneOffsetMinutes,
getBrowserTimezoneOffsetMinutes,
getActualUnixTimeForStore
} from '@/lib/datetime.js';
export default {
props: [
Expand All @@ -445,6 +490,8 @@ export default {
newAccount.showIconSelectionSheet = false;
newAccount.showColorSelectionSheet = false;
newAccount.showBalanceSheet = false;
newAccount.showBalanceDateTimeSheet = false;
newAccount.balanceDateTimeSheetMode = 'time';
return {
editAccountId: null,
Expand Down Expand Up @@ -534,6 +581,8 @@ export default {
subAccount.showIconSelectionSheet = false;
subAccount.showColorSelectionSheet = false;
subAccount.showBalanceSheet = false;
subAccount.showBalanceDateTimeSheet = false;
subAccount.balanceDateTimeSheetMode = 'time';
self.subAccounts.push(subAccount);
}
Expand Down Expand Up @@ -566,6 +615,8 @@ export default {
subAccount.showIconSelectionSheet = false;
subAccount.showColorSelectionSheet = false;
subAccount.showBalanceSheet = false;
subAccount.showBalanceDateTimeSheet = false;
subAccount.balanceDateTimeSheetMode = 'time';
this.subAccounts.push(subAccount);
},
Expand Down Expand Up @@ -639,6 +690,10 @@ export default {
}
});
},
showDateTimeDialog(account, sheetMode) {
account.balanceDateTimeSheetMode = sheetMode;
account.showBalanceDateTimeSheet = true;
},
getCurrencyName(currencyCode) {
return this.$locale.getCurrencyName(currencyCode);
},
Expand All @@ -651,6 +706,12 @@ export default {
getAccountBalance(account) {
return this.getDisplayCurrency(account.balance, account.currency);
},
getAccountBalanceDate(balanceTime) {
return this.$locale.formatUnixTimeToLongDate(this.userStore, getActualUnixTimeForStore(balanceTime, getTimezoneOffsetMinutes(), getBrowserTimezoneOffsetMinutes()));
},
getAccountBalanceTime(balanceTime) {
return this.$locale.formatUnixTimeToLongTime(this.userStore, getActualUnixTimeForStore(balanceTime, getTimezoneOffsetMinutes(), getBrowserTimezoneOffsetMinutes()));
},
getDisplayCurrency(value, currencyCode) {
return this.$locale.formatAmountWithCurrency(this.settingsStore, this.userStore, value, currencyCode);
},
Expand Down Expand Up @@ -694,6 +755,27 @@ export default {
</script>

<style>
.account-edit-balancetime .item-title {
width: 100%;
}
.account-edit-balancetime .item-title > .item-header > .account-edit-balancetime-header {
display: block;
width: 100%;
}
.account-edit-balancetime .item-title > .account-edit-balancetime-title {
display: flex;
width: 100%;
}
.account-edit-balancetime .item-title > .account-edit-balancetime-title > .account-edit-balancetime-time {
flex-grow: 1;
overflow: hidden;
text-overflow: ellipsis;
}
.subaccount-edit-list {
--f7-list-group-title-height: 40px;
}
Expand Down

0 comments on commit bff6ca7

Please sign in to comment.