mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
Fix [1018]: Add Date field when entering Account Balance (#1068)
* Add new Date field when creating a new Account * Fix german translation * Update app/controllers/concerns/accountable_resource.rb Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Michel Roegl-Brunner <73236783+michelroegl-brunner@users.noreply.github.com> * Add missing opening_balance:date to update_params * Change label text --------- Signed-off-by: Michel Roegl-Brunner <73236783+michelroegl-brunner@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
0f78f54f90
commit
f8d3678a40
@@ -34,7 +34,15 @@ module AccountableResource
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@account = Current.family.accounts.create_and_sync(account_params.except(:return_to))
|
opening_balance_date = begin
|
||||||
|
account_params[:opening_balance_date].presence&.to_date
|
||||||
|
rescue Date::Error
|
||||||
|
nil
|
||||||
|
end || (Time.zone.today - 2.years)
|
||||||
|
@account = Current.family.accounts.create_and_sync(
|
||||||
|
account_params.except(:return_to, :opening_balance_date),
|
||||||
|
opening_balance_date: opening_balance_date
|
||||||
|
)
|
||||||
@account.lock_saved_attributes!
|
@account.lock_saved_attributes!
|
||||||
|
|
||||||
redirect_to account_params[:return_to].presence || @account, notice: t("accounts.create.success", type: accountable_type.name.underscore.humanize)
|
redirect_to account_params[:return_to].presence || @account, notice: t("accounts.create.success", type: accountable_type.name.underscore.humanize)
|
||||||
@@ -52,7 +60,7 @@ module AccountableResource
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Update remaining account attributes
|
# Update remaining account attributes
|
||||||
update_params = account_params.except(:return_to, :balance, :currency)
|
update_params = account_params.except(:return_to, :balance, :currency, :opening_balance_date)
|
||||||
unless @account.update(update_params)
|
unless @account.update(update_params)
|
||||||
@error_message = @account.errors.full_messages.join(", ")
|
@error_message = @account.errors.full_messages.join(", ")
|
||||||
render :edit, status: :unprocessable_entity
|
render :edit, status: :unprocessable_entity
|
||||||
@@ -85,6 +93,7 @@ module AccountableResource
|
|||||||
def account_params
|
def account_params
|
||||||
params.require(:account).permit(
|
params.require(:account).permit(
|
||||||
:name, :balance, :subtype, :currency, :accountable_type, :return_to,
|
:name, :balance, :subtype, :currency, :accountable_type, :return_to,
|
||||||
|
:opening_balance_date,
|
||||||
:institution_name, :institution_domain, :notes,
|
:institution_name, :institution_domain, :notes,
|
||||||
accountable_attributes: self.class.permitted_accountable_attributes
|
accountable_attributes: self.class.permitted_accountable_attributes
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ class Account < ApplicationRecord
|
|||||||
super(attribute, options)
|
super(attribute, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_and_sync(attributes, skip_initial_sync: false)
|
def create_and_sync(attributes, skip_initial_sync: false, opening_balance_date: nil)
|
||||||
attributes[:accountable_attributes] ||= {} # Ensure accountable is created, even if empty
|
attributes[:accountable_attributes] ||= {} # Ensure accountable is created, even if empty
|
||||||
# Default cash_balance to balance unless explicitly provided (e.g., Crypto sets it to 0)
|
# Default cash_balance to balance unless explicitly provided (e.g., Crypto sets it to 0)
|
||||||
attrs = attributes.dup
|
attrs = attributes.dup
|
||||||
@@ -91,7 +91,10 @@ class Account < ApplicationRecord
|
|||||||
account.save!
|
account.save!
|
||||||
|
|
||||||
manager = Account::OpeningBalanceManager.new(account)
|
manager = Account::OpeningBalanceManager.new(account)
|
||||||
result = manager.set_opening_balance(balance: initial_balance || account.balance)
|
result = manager.set_opening_balance(
|
||||||
|
balance: initial_balance || account.balance,
|
||||||
|
date: opening_balance_date
|
||||||
|
)
|
||||||
raise result.error if result.error
|
raise result.error if result.error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,13 @@
|
|||||||
<%= form.money_field :balance, label: t(".balance"), required: true, default_currency: Current.family.currency %>
|
<%= form.money_field :balance, label: t(".balance"), required: true, default_currency: Current.family.currency %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
<% if account.new_record? && !account.linked? %>
|
||||||
|
<%= form.date_field :opening_balance_date,
|
||||||
|
label: t(".opening_balance_date_label"),
|
||||||
|
value: Time.zone.today - 2.years,
|
||||||
|
required: true %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<%= yield form %>
|
<%= yield form %>
|
||||||
|
|
||||||
<details class="group">
|
<details class="group">
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ de:
|
|||||||
new_account: Neues Konto
|
new_account: Neues Konto
|
||||||
no_accounts: Noch keine Konten vorhanden
|
no_accounts: Noch keine Konten vorhanden
|
||||||
form:
|
form:
|
||||||
balance: Aktueller Kontostand
|
balance: "Kontostand zum Datum:"
|
||||||
|
opening_balance_date_label: Eröffnungsdatum des Kontostands
|
||||||
name_label: Kontoname
|
name_label: Kontoname
|
||||||
name_placeholder: Beispielkontoname
|
name_placeholder: Beispielkontoname
|
||||||
index:
|
index:
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ en:
|
|||||||
new_account: New account
|
new_account: New account
|
||||||
no_accounts: No accounts yet
|
no_accounts: No accounts yet
|
||||||
form:
|
form:
|
||||||
balance: Current balance
|
balance: "Balance on date:"
|
||||||
|
opening_balance_date_label: Opening balance date
|
||||||
name_label: Account name
|
name_label: Account name
|
||||||
name_placeholder: Example account name
|
name_placeholder: Example account name
|
||||||
additional_details: Additional details
|
additional_details: Additional details
|
||||||
|
|||||||
@@ -72,6 +72,27 @@ class AccountTest < ActiveSupport::TestCase
|
|||||||
assert_equal 1000, opening_anchor.entry.amount
|
assert_equal 1000, opening_anchor.entry.amount
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "create_and_sync uses provided opening balance date" do
|
||||||
|
Account.any_instance.stubs(:sync_later)
|
||||||
|
opening_date = Time.zone.today
|
||||||
|
|
||||||
|
account = Account.create_and_sync(
|
||||||
|
{
|
||||||
|
family: @family,
|
||||||
|
name: "Test Account",
|
||||||
|
balance: 1000,
|
||||||
|
currency: "USD",
|
||||||
|
accountable_type: "Depository",
|
||||||
|
accountable_attributes: {}
|
||||||
|
},
|
||||||
|
skip_initial_sync: true,
|
||||||
|
opening_balance_date: opening_date
|
||||||
|
)
|
||||||
|
|
||||||
|
opening_anchor = account.valuations.opening_anchor.first
|
||||||
|
assert_equal opening_date, opening_anchor.entry.date
|
||||||
|
end
|
||||||
|
|
||||||
test "gets short/long subtype label" do
|
test "gets short/long subtype label" do
|
||||||
investment = Investment.new(subtype: "hsa")
|
investment = Investment.new(subtype: "hsa")
|
||||||
account = @family.accounts.create!(
|
account = @family.accounts.create!(
|
||||||
|
|||||||
Reference in New Issue
Block a user