mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 12:04:08 +00:00
Initial split transaction support (#1230)
* Initial split transaction support * Add support to unsplit and edit split * Update show.html.erb * FIX address reviews * Improve UX * Update show.html.erb * Reviews * Update edit.html.erb * Add parent category to dialog * Update en.yml * Add UI indication to totals * FIX ui update * Add category select like rest of app --------- Signed-off-by: Juan José Mata <juanjo.mata@gmail.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
This commit is contained in:
95
app/controllers/splits_controller.rb
Normal file
95
app/controllers/splits_controller.rb
Normal file
@@ -0,0 +1,95 @@
|
||||
class SplitsController < ApplicationController
|
||||
before_action :set_entry
|
||||
|
||||
def new
|
||||
@categories = Current.family.categories.alphabetically
|
||||
end
|
||||
|
||||
def create
|
||||
unless @entry.transaction.splittable?
|
||||
redirect_back_or_to transactions_path, alert: t("splits.create.not_splittable")
|
||||
return
|
||||
end
|
||||
|
||||
raw_splits = split_params[:splits]
|
||||
raw_splits = raw_splits.values if raw_splits.respond_to?(:values)
|
||||
|
||||
splits = raw_splits.map do |s|
|
||||
{ name: s[:name], amount: s[:amount].to_d * -1, category_id: s[:category_id].presence }
|
||||
end
|
||||
|
||||
@entry.split!(splits)
|
||||
@entry.sync_account_later
|
||||
|
||||
redirect_back_or_to transactions_path, notice: t("splits.create.success")
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
redirect_back_or_to transactions_path, alert: e.message
|
||||
end
|
||||
|
||||
def edit
|
||||
resolve_to_parent!
|
||||
|
||||
unless @entry.split_parent?
|
||||
redirect_to transactions_path, alert: t("splits.edit.not_split")
|
||||
return
|
||||
end
|
||||
|
||||
@categories = Current.family.categories.alphabetically
|
||||
@children = @entry.child_entries.includes(:entryable)
|
||||
end
|
||||
|
||||
def update
|
||||
resolve_to_parent!
|
||||
|
||||
unless @entry.split_parent?
|
||||
redirect_to transactions_path, alert: t("splits.edit.not_split")
|
||||
return
|
||||
end
|
||||
|
||||
raw_splits = split_params[:splits]
|
||||
raw_splits = raw_splits.values if raw_splits.respond_to?(:values)
|
||||
|
||||
splits = raw_splits.map do |s|
|
||||
{ name: s[:name], amount: s[:amount].to_d * -1, category_id: s[:category_id].presence }
|
||||
end
|
||||
|
||||
Entry.transaction do
|
||||
@entry.unsplit!
|
||||
@entry.split!(splits)
|
||||
end
|
||||
|
||||
@entry.sync_account_later
|
||||
|
||||
redirect_to transactions_path, notice: t("splits.update.success")
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
redirect_to transactions_path, alert: e.message
|
||||
end
|
||||
|
||||
def destroy
|
||||
resolve_to_parent!
|
||||
|
||||
unless @entry.split_parent?
|
||||
redirect_to transactions_path, alert: t("splits.edit.not_split")
|
||||
return
|
||||
end
|
||||
|
||||
@entry.unsplit!
|
||||
@entry.sync_account_later
|
||||
|
||||
redirect_to transactions_path, notice: t("splits.destroy.success")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_entry
|
||||
@entry = Current.family.entries.find(params[:transaction_id])
|
||||
end
|
||||
|
||||
def resolve_to_parent!
|
||||
@entry = @entry.parent_entry if @entry.split_child?
|
||||
end
|
||||
|
||||
def split_params
|
||||
params.require(:split).permit(splits: [ :name, :amount, :category_id ])
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,8 @@
|
||||
class Transactions::BulkDeletionsController < ApplicationController
|
||||
def create
|
||||
destroyed = Current.family.entries.destroy_by(id: bulk_delete_params[:entry_ids])
|
||||
# Exclude split children from bulk delete - they must be deleted via unsplit on parent
|
||||
entries_scope = Current.family.entries.where(parent_entry_id: nil)
|
||||
destroyed = entries_scope.destroy_by(id: bulk_delete_params[:entry_ids])
|
||||
destroyed.map(&:account).uniq.each(&:sync_later)
|
||||
redirect_back_or_to transactions_url, notice: "#{destroyed.count} transaction#{destroyed.count == 1 ? "" : "s"} deleted"
|
||||
end
|
||||
|
||||
@@ -3,8 +3,10 @@ class Transactions::BulkUpdatesController < ApplicationController
|
||||
end
|
||||
|
||||
def create
|
||||
# Skip split parents from bulk update - update children instead
|
||||
updated = Current.family
|
||||
.entries
|
||||
.excluding_split_parents
|
||||
.where(id: bulk_update_params[:entry_ids])
|
||||
.bulk_update!(bulk_update_params, update_tags: tags_provided?)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user