Module: Admin::ActivityTaxControllerHelper

Included in:
ActivitiesController, MaterialsController
Defined in:
app/helpers/admin/activity_tax_controller_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object



40
41
42
43
44
45
46
# File 'app/helpers/admin/activity_tax_controller_helper.rb', line 40

def self.append_features(base)
  super

  base.class_eval do
    observe_active_scaffold_form_fields :fields => %w(apply_tax cost), :for_activities => 'material', :action => :on_tax_observation
  end
end

Instance Method Details

#on_tax_observationObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/helpers/admin/activity_tax_controller_helper.rb', line 3

def on_tax_observation
  render(:update) do |page|
    begin
      tax_percent, tax_flat = Setting.grab :sales_tax_percent, :sales_tax_flat

      tax_percent = tax_percent.to_f
      tax_flat = tax_flat.to_f

      id_suffix = (params[:eid] ? '_' : '')+params[:record_id]
      
      tax_field_id  = options_for_column(:tax)[:id] + id_suffix
      cost_field_id = options_for_column(:cost)[:id] + id_suffix
      tax_field_hidden_id  = "%s_hidden"  % tax_field_id

      page[cost_field_id].value = money_for_input(params[:cost].to_f)

      if params['apply_tax'] == 'yes'
        # Calculate the new auto-tax amount here...
        page[tax_field_id].value = money_for_input(params[:cost].to_f * (tax_percent/100) + tax_flat)

        # Now enable the control:          
        page[tax_field_id].enable
        page[tax_field_hidden_id].disable
        page[tax_field_id].removeClassName "disabled"
      else
        page[tax_field_id].value = ''
        page[tax_field_id].disable
        page[tax_field_hidden_id].enable
        page[tax_field_id].addClassName "disabled"
      end

    rescue
      page.alert 'Error updating form Record(%s) "%s"' % [params[:record_id], $!]
    end
  end
end