@@ -5,6 +5,12 @@ def confirm_create
55 @account = Current . family . accounts . find ( params . dig ( :entry , :account_id ) )
66 @entry = @account . entries . build ( entry_params . merge ( currency : @account . currency ) )
77
8+ @reconciliation_dry_run = @entry . account . create_reconciliation (
9+ balance : entry_params [ :amount ] ,
10+ date : entry_params [ :date ] ,
11+ dry_run : true
12+ )
13+
814 render :confirm_create
915 end
1016
@@ -13,19 +19,28 @@ def confirm_update
1319 @account = @entry . account
1420 @entry . assign_attributes ( entry_params . merge ( currency : @account . currency ) )
1521
22+ @reconciliation_dry_run = @entry . account . update_reconciliation (
23+ @entry ,
24+ balance : entry_params [ :amount ] ,
25+ date : entry_params [ :date ] ,
26+ dry_run : true
27+ )
28+
1629 render :confirm_update
1730 end
1831
1932 def create
2033 account = Current . family . accounts . find ( params . dig ( :entry , :account_id ) )
21- result = perform_balance_update ( account , entry_params . merge ( currency : account . currency ) )
2234
23- if result . success?
24- @success_message = result . updated? ? "Balance updated" : "No changes made. Account is already up to date."
35+ result = account . create_reconciliation (
36+ balance : entry_params [ :amount ] ,
37+ date : entry_params [ :date ] ,
38+ )
2539
40+ if result . success?
2641 respond_to do |format |
27- format . html { redirect_back_or_to account_path ( account ) , notice : @success_message }
28- format . turbo_stream { stream_redirect_back_or_to ( account_path ( account ) , notice : @success_message ) }
42+ format . html { redirect_back_or_to account_path ( account ) , notice : "Account updated" }
43+ format . turbo_stream { stream_redirect_back_or_to ( account_path ( account ) , notice : "Account updated" ) }
2944 end
3045 else
3146 @error_message = result . error_message
@@ -34,13 +49,22 @@ def create
3449 end
3550
3651 def update
37- result = perform_balance_update ( @entry . account , entry_params . merge ( currency : @entry . currency , existing_valuation_id : @entry . id ) )
52+ # Notes updating is independent of reconciliation, just a simple CRUD operation
53+ @entry . update! ( notes : entry_params [ :notes ] ) if entry_params [ :notes ] . present?
3854
39- if result . success?
55+ if entry_params [ :date ] . present? && entry_params [ :amount ] . present?
56+ result = @entry . account . update_reconciliation (
57+ @entry ,
58+ balance : entry_params [ :amount ] ,
59+ date : entry_params [ :date ] ,
60+ )
61+ end
62+
63+ if result . nil? || result . success?
4064 @entry . reload
4165
4266 respond_to do |format |
43- format . html { redirect_back_or_to account_path ( @entry . account ) , notice : result . updated? ? "Balance updated" : "No changes made. Account is already up to date. " }
67+ format . html { redirect_back_or_to account_path ( @entry . account ) , notice : "Entry updated" }
4468 format . turbo_stream do
4569 render turbo_stream : [
4670 turbo_stream . replace (
@@ -60,17 +84,6 @@ def update
6084
6185 private
6286 def entry_params
63- params . require ( :entry )
64- . permit ( :date , :amount , :currency , :notes )
65- end
66-
67- def perform_balance_update ( account , params )
68- account . update_balance (
69- balance : params [ :amount ] ,
70- date : params [ :date ] ,
71- currency : params [ :currency ] ,
72- notes : params [ :notes ] ,
73- existing_valuation_id : params [ :existing_valuation_id ]
74- )
87+ params . require ( :entry ) . permit ( :date , :amount , :notes )
7588 end
7689end
0 commit comments