diff --git a/app/controllers/account/entries_controller.rb b/app/controllers/account/entries_controller.rb index 02896ebb..21e971a1 100644 --- a/app/controllers/account/entries_controller.rb +++ b/app/controllers/account/entries_controller.rb @@ -40,7 +40,9 @@ class Account::EntriesController < ApplicationController end def update - @entry.update! entry_params + @entry.assign_attributes entry_params + @entry.amount = amount if nature.present? + @entry.save! @entry.sync_account_later respond_to do |format| @@ -84,6 +86,18 @@ class Account::EntriesController < ApplicationController .permit(:name, :date, :amount, :currency, :entryable_type, entryable_attributes: permitted_entryable_attributes) end + def amount + if nature.income? + entry_params[:amount].to_d.abs * -1 + else + entry_params[:amount].to_d.abs + end + end + + def nature + params[:account_entry][:nature].to_s.inquiry + end + # entryable_type is required here because Rails expects both of these params in this exact order (potential upstream bug) def entry_params_with_defaults(params) params.with_defaults(entryable_type: params[:entryable_type], entryable_attributes: {}) diff --git a/app/views/account/entries/entryables/transaction/_show.html.erb b/app/views/account/entries/entryables/transaction/_show.html.erb index 9b9644d5..1ff9d7ba 100644 --- a/app/views/account/entries/entryables/transaction/_show.html.erb +++ b/app/views/account/entries/entryables/transaction/_show.html.erb @@ -30,6 +30,16 @@ <%= form_with model: [account, entry], url: account_entry_path(account, entry), html: { data: { controller: "auto-submit-form" } } do |f| %>
<%= f.text_field :name, label: t(".name_label"), "data-auto-submit-form-target": "auto" %> + <% unless entry.marked_as_transfer? %> +
+
+ <%= f.select :nature, [["Expense", "expense"], ["Income", "income"]], { label: t(".nature"), selected: entry.amount.negative? ? "income" : "expense" }, "data-auto-submit-form-target": "auto" %> +
+
+ <%= f.number_field :amount, value: entry.amount.abs, label: t(".amount"), step: "0.01", "data-auto-submit-form-target": "auto", "data-autosubmit-trigger-event": "change" %> +
+
+ <% end %> <%= f.date_field :date, label: t(".date_label"), max: Date.current, "data-auto-submit-form-target": "auto" %> <%= f.fields_for :entryable do |ef| %> diff --git a/config/locales/views/account/entries/en.yml b/config/locales/views/account/entries/en.yml index f6f3793b..da29b2e4 100644 --- a/config/locales/views/account/entries/en.yml +++ b/config/locales/views/account/entries/en.yml @@ -15,6 +15,7 @@ en: account_label: Account account_placeholder: Select an account additional: Additional + amount: Amount category_label: Category category_placeholder: Select a category date_label: Date @@ -28,6 +29,7 @@ en: merchant_label: Merchant merchant_placeholder: Select a merchant name_label: Name + nature: Transaction type note_label: Notes note_placeholder: Enter a note overview: Overview