@@ -57,7 +57,7 @@ def download
5757 query_string = params [ :search ]
5858
5959 if all_fields_checked
60- columns = KillBillClient ::Model ::AccountAttributes . instance_variable_get ( ' @json_attributes' )
60+ columns = KillBillClient ::Model ::AccountAttributes . instance_variable_get ( : @json_attributes)
6161 csv_headers = columns . dup
6262 Kaui ::Account ::REMAPPING_FIELDS . each do |k , v |
6363 index = csv_headers . index ( k )
@@ -97,36 +97,7 @@ def download
9797 csv << data
9898 end
9999 end
100- send_data csv_string , filename : "accounts-#{ Date . today } .csv" , type : 'text/csv'
101- end
102-
103- def new
104- @account = Kaui ::Account . new
105- end
106-
107- def create
108- @account = Kaui ::Account . new ( params . require ( :account ) . delete_if { |_key , value | value . blank? } )
109-
110- @account . errors . add ( :phone , :invalid_phone ) if !@account . phone . nil? && !@account . check_account_details_phone?
111-
112- @account . errors . add ( :check_account_details_bill_cycle_day_local , :invalid_bill_cycle_day_local ) if !@account . bill_cycle_day_local . nil? && !@account . check_account_details_bill_cycle_day_local?
113-
114- unless @account . errors . empty?
115- flash . now [ :errors ] = @account . errors . messages . values . flatten
116- render action : :new and return
117- end
118-
119- # Transform "1" into boolean
120- @account . is_migrated = @account . is_migrated == '1'
121-
122- begin
123- @account = @account . create ( current_user . kb_username , params [ :reason ] , params [ :comment ] , options_for_klient )
124-
125- redirect_to account_path ( @account . account_id ) , notice : 'Account was successfully created'
126- rescue StandardError => e
127- flash . now [ :error ] = "Error while creating account: #{ as_string ( e ) } "
128- render action : :new
129- end
100+ send_data csv_string , filename : "accounts-#{ Time . zone . today } .csv" , type : 'text/csv'
130101 end
131102
132103 # rubocop:disable Style/MultilineBlockChain
@@ -140,7 +111,7 @@ def show
140111 fetch_children = promise { @account . children ( false , false , 'NONE' , cached_options_for_klient ) }
141112 fetch_parent = @account . parent_account_id . nil? ? nil : promise { Kaui ::Account . find_by_id ( @account . parent_account_id , false , false , cached_options_for_klient ) }
142113 fetch_overdue_state = promise { @account . overdue ( cached_options_for_klient ) }
143- fetch_account_tags = promise { @account . tags ( false , 'NONE' , cached_options_for_klient ) . sort { | tag_a , tag_b | tag_a <=> tag_b } }
114+ fetch_account_tags = promise { @account . tags ( false , 'NONE' , cached_options_for_klient ) . sort }
144115 fetch_account_fields = promise { @account . custom_fields ( 'NONE' , cached_options_for_klient ) . sort { |cf_a , cf_b | cf_a . name . downcase <=> cf_b . name . downcase } }
145116 fetch_account_emails = promise { Kaui ::AccountEmail . find_all_sorted_by_account_id ( @account . account_id , 'NONE' , cached_options_for_klient ) }
146117 fetch_payments = promise { @account . payments ( cached_options_for_klient ) . map! { |payment | Kaui ::Payment . build_from_raw_payment ( payment ) } }
@@ -180,7 +151,7 @@ def show
180151 @custom_fields = wait ( fetch_account_fields )
181152 @account_emails = wait ( fetch_account_emails )
182153 wait ( fetch_payment_methods )
183- @payment_methods = wait ( fetch_payment_methods_with_details ) . map { |pm_f | wait ( pm_f ) } . compact
154+ @payment_methods = wait ( fetch_payment_methods_with_details ) . filter_map { |pm_f | wait ( pm_f ) }
184155 @available_tags = wait ( fetch_available_tags )
185156 @children = wait ( fetch_children )
186157 @account_parent = @account . parent_account_id . nil? ? nil : wait ( fetch_parent )
@@ -198,6 +169,52 @@ def show
198169
199170 params . permit!
200171 end
172+
173+ def new
174+ @account = Kaui ::Account . new
175+ end
176+
177+ def edit ; end
178+
179+ def create
180+ @account = Kaui ::Account . new ( params . require ( :account ) . permit! . to_h . compact_blank )
181+
182+ @account . errors . add ( :phone , :invalid_phone ) if !@account . phone . nil? && !@account . check_account_details_phone?
183+
184+ @account . errors . add ( :check_account_details_bill_cycle_day_local , :invalid_bill_cycle_day_local ) if !@account . bill_cycle_day_local . nil? && !@account . check_account_details_bill_cycle_day_local?
185+
186+ unless @account . errors . empty?
187+ flash . now [ :errors ] = @account . errors . messages . values . flatten
188+ render action : :new and return
189+ end
190+
191+ # Transform "1" into boolean
192+ @account . is_migrated = @account . is_migrated == '1'
193+
194+ begin
195+ @account = @account . create ( current_user . kb_username , params [ :reason ] , params [ :comment ] , options_for_klient )
196+
197+ redirect_to account_path ( @account . account_id ) , notice : 'Account was successfully created'
198+ rescue StandardError => e
199+ flash . now [ :error ] = "Error while creating account: #{ as_string ( e ) } "
200+ render action : :new
201+ end
202+ end
203+
204+ def update
205+ @account = Kaui ::Account . new ( params . require ( :account ) . permit! . to_h . compact_blank )
206+ @account . account_id = params . require ( :account_id )
207+
208+ # Transform "1" into boolean
209+ @account . is_migrated = @account . is_migrated == '1'
210+
211+ @account . update ( true , current_user . kb_username , params [ :reason ] , params [ :comment ] , options_for_klient )
212+
213+ redirect_to account_path ( @account . account_id ) , notice : 'Account successfully updated'
214+ rescue StandardError => e
215+ flash . now [ :error ] = "Error while updating account: #{ as_string ( e ) } "
216+ render action : :edit
217+ end
201218 # rubocop:enable Style/MultilineBlockChain
202219
203220 def destroy
@@ -261,23 +278,6 @@ def next_invoice_date
261278 end
262279 end
263280
264- def edit ; end
265-
266- def update
267- @account = Kaui ::Account . new ( params . require ( :account ) . delete_if { |_key , value | value . blank? } )
268- @account . account_id = params . require ( :account_id )
269-
270- # Transform "1" into boolean
271- @account . is_migrated = @account . is_migrated == '1'
272-
273- @account . update ( true , current_user . kb_username , params [ :reason ] , params [ :comment ] , options_for_klient )
274-
275- redirect_to account_path ( @account . account_id ) , notice : 'Account successfully updated'
276- rescue StandardError => e
277- flash . now [ :error ] = "Error while updating account: #{ as_string ( e ) } "
278- render action : :edit
279- end
280-
281281 def set_default_payment_method
282282 account_id = params . require ( :account_id )
283283 payment_method_id = params . require ( :payment_method_id )
@@ -309,7 +309,7 @@ def validate_external_key
309309 end
310310
311311 def link_to_parent
312- @account = Kaui ::Account . new ( params . require ( :account ) . delete_if { | _key , value | value . blank? } )
312+ @account = Kaui ::Account . new ( params . require ( :account ) . permit! . to_h . compact_blank )
313313 @account . account_id = params . require ( :account_id )
314314 @account . is_payment_delegated_to_parent = @account . is_payment_delegated_to_parent == '1'
315315
0 commit comments