Class: Admin::LaborsController

Inherits:
ApplicationController
  • Object
show all
Includes:
ActivityTypeControllerHelper, AdminLayoutHelper, ExtensibleObjectHelper
Defined in:
app/controllers/admin/labors_controller/slimtimer.rb,
app/controllers/admin/labors_controller.rb

Instance Method Summary collapse

Methods included from ExtensibleObjectHelper

append_features

Methods included from AdminLayoutHelper

append_features, #controller_url, #define_layout_variables

Methods included from ApplicationHelper

#controller_id, #define_application_layout_variables, #h_money, #money_for_input

Instance Method Details

#edit_with_slimtimerObject Also known as: edit



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/admin/labors_controller/slimtimer.rb', line 5

def edit_with_slimtimer
  self.active_scaffold_config.configure do |config|
    config.columns << :slimtimer_task
    config.columns[:slimtimer_task].label = 'SLIM<em>TIMER</em> Task'
    
    # This looks real iffy, but seems to be the best way of doing this... even if its super ugly
    config.update.columns.each do |uc| 
      if uc.label == 'Activity'
        uc.add :slimtimer_task
        uc.move_column_under :slimtimer_task, :occurred_on
      end
    end
  end
  
  # Here we'll fetch a default client if we can ascertain one:
  begin
    @record.activity.client_id = @record.slimtimer_time_entry.slimtimer_task.default_client_id if @record.activity.client_id.nil?
  rescue
    
  end
  
  edit_without_slimtimer
end

#on_labor_observationObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/admin/labors_controller.rb', line 34

def on_labor_observation
  # This got really complicated. Oh well, refactoring most of this in the model would be kind of cool...
  
  record_id = (/^[\d]+$/.match(params[:record_id])) ? params[:record_id].to_i : nil
  labor_before = (record_id.nil?) ? Activity::Labor.new : Activity::Labor.find(record_id)

  labor_after = labor_before.clone 

  labor_after.duration = params[:duration]
  labor_after.comments = params[:comments]
  labor_after.employee_id = params[:employee]
  labor_after.activity.client_id = params[:client]
  labor_after.activity.cost = params[:cost]

  observed_column = params[:observed_column]
  
  allowed_error_fields = /^(employee|client|employee_id|client_id)$/

  render(:update) do |page|
    
    updated_cost = nil
    updated_duration = nil
    auto_cost = nil
    
    # See if we can figure out a good automatic cost:
    if labor_after.employee and labor_after.activity.client
      labor_rate = labor_after.employee.labor_rate_for labor_after.activity.client

      auto_cost = labor_after.minute_duration.to_f/60*labor_rate.hourly_rate.to_f if !labor_rate.nil? and !labor_rate.hourly_rate.nil? and !labor_after.minute_duration.nil?
    end

    # Win or lose, we probably want to do this, (its possible, they change the value from the existing 0:30 to 30m):
    updated_duration = labor_after.clock_duration

    if labor_after.valid?
      if observed_column == 'cost'
        # This means they manually changed the cost themselves
        updated_cost = labor_after.cost
      elsif !auto_cost.nil?
        # Something else changed - let's update the auto-calc price
        updated_cost = auto_cost unless auto_cost.nil?
      end

    else
      page["record_#{observed_column}_#{record_id}"].focus if (
        !allowed_error_fields.match( observed_column ) and
        labor_after.errors.invalid?( observed_column )
      )

      labor_after.errors.each do |attr,msg|
        page.visual_effect( 
          :highlight, 
          "record_#{attr}_#{record_id}", 
          :duration => 3, 
          :startcolor => "#FF0000"
        ) unless allowed_error_fields.match attr
      end

      updated_duration = labor_before.clock_duration if labor_after.errors.invalid? :duration
      updated_cost = (auto_cost.nil?) ? labor_after.cost : auto_cost
    end

    page["record_cost_#{record_id}"].value = to_money(updated_cost) unless updated_cost.nil?
    page["record_duration_#{record_id}"].value = updated_duration unless updated_duration.nil?
  end

end