Class: Achoo::Achievo::HourRegistrationForm

Inherits:
Object
  • Object
show all
Defined in:
lib/achoo/achievo/hour_registration_form.rb

Direct Known Subclasses

HourRegistrationFormRanged

Instance Method Summary collapse

Constructor Details

#initializeHourRegistrationForm

Returns a new instance of HourRegistrationForm.



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
# File 'lib/achoo/achievo/hour_registration_form.rb', line 9

def initialize
  @page  = AGENT.get(RC[:hour_registration_url])
  @form  = @page.form('entryform')

  if @form.nil?
    # Happens if the user has viewed a day or week report for a
    # locked month. Fetching todays day report should fix this in
    # most cases.

    # FIX Ugly call to a private method using send()
    haf    = HourAdministrationForm.new
    @page  = haf.send(:set_page_to_view_for_date, 'dayview', Date.today)
    @form  = @page.form('entryform')
  end

  if @form.nil?
    raise "Failed to retrieve the hour registration form.\nThe likely cause is that you have locked the current month, which is a silly thing to do."
  end

  @projects_seen = {}
  @phases_seen   = {}

  # Need to preselect this for some reason
  @form.field_with(:name => 'billpercent').options.first.select
  # Preselecting this one as well, just in case
  @form.field_with(:name => 'workperiod').options.first.select
end

Instance Method Details

#all_projectsObject



115
116
117
# File 'lib/achoo/achievo/hour_registration_form.rb', line 115

def all_projects
  @@allprojects_cache ||= get_all_projects
end

#billing=(billing) ⇒ Object



65
66
67
# File 'lib/achoo/achievo/hour_registration_form.rb', line 65

def billing=(billing)
  @form.billpercent = "billpercent.id='#{billing}'"
end

#billing_optionsObject



73
74
75
# File 'lib/achoo/achievo/hour_registration_form.rb', line 73

def billing_options
  collect_options('billpercent')
end

#collect_options(field_name) ⇒ Object



77
78
79
80
81
# File 'lib/achoo/achievo/hour_registration_form.rb', line 77

def collect_options(field_name)
  @form.field_with(:name => field_name).options.collect do |opt|
    [opt.value.match(/#{field_name}\.id='(\d+)'/)[1], opt.text]
  end
end

#get_all_projectsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/achoo/achievo/hour_registration_form.rb', line 119

def get_all_projects
  puts "Getting project page #1..."
  projects_page = AGENT.get(projects_url)
  projects = scrape_projects(projects_page)

  i = 2
  while (link = projects_page.link_with(:text => 'Next'))
    puts "Getting project page ##{i}..."
    projects_page = link.click
    projects.merge!(scrape_projects(projects_page))
    i += 1
  end

  projects.keys.sort.collect do |name|
    id   = projects[name][0]
    text = "#{projects[name][1]}: #{name}"
    @projects_seen[id] = text
    [id, text]
  end
end

#hours=(hours) ⇒ Object



49
50
51
# File 'lib/achoo/achievo/hour_registration_form.rb', line 49

def hours=(hours)
  @form.time = hours
end

#phaseObject



53
54
55
# File 'lib/achoo/achievo/hour_registration_form.rb', line 53

def phase
  @form.phaseid.match(/phase\.id='(\d+)'/)[1]
end

#phase=(phaseid) ⇒ Object



57
58
59
# File 'lib/achoo/achievo/hour_registration_form.rb', line 57

def phase=(phaseid)
  @form.phaseid   = "phase.id='#{phaseid}'"
end

#phases_for_selected_projectObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/achoo/achievo/hour_registration_form.rb', line 83

def phases_for_selected_project
  partial_page = retrieve_project_phases_page
  page         = create_page_from_partial(partial_page)
  field        = page.forms.first.field_with(:name => 'phaseid')
  
  phases = []
  if field.respond_to?(:options)
    field.options.each do |opt|
      phases << [extract_number_from_phaseid(opt.value), opt.text]
    end
  else
    partial_page.body.match(/(^[^<]+)&nbsp;&nbsp;</)
    phases << [extract_number_from_phaseid(field.value), $1]
  end
  
  phases.each {|p| @phases_seen[p[0]] = p[1]}

  return phases
end


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/achoo/achievo/hour_registration_form.rb', line 140

def print_values
  format = "%10s: \"%s\"\n"
  printf format, 'date',     date_to_s
  printf format, 'project',  @projects_seen[project]
  printf format, 'phase',    @phases_seen[phase]
  printf format, 'remark',   @form.remark
  printf format, 'hours',    @form.time
  printf format, 'worktime', @form.field_with(:name => 'workperiod').selected_options.first.text
  printf format, 'billing',  @form.field_with(:name => 'billpercent').selected_options.first.text

  # @form.fields.each do |field|
  #   printf format, field.name, field.value
  # end

end

#projectObject



37
38
39
# File 'lib/achoo/achievo/hour_registration_form.rb', line 37

def project
  extract_number_from_projectid(@form.projectid)
end

#project=(projectid) ⇒ Object



41
42
43
# File 'lib/achoo/achievo/hour_registration_form.rb', line 41

def project=(projectid)
  @form.projectid = "project.id='#{projectid}'"
end

#recent_projectsObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/achoo/achievo/hour_registration_form.rb', line 103

def recent_projects
  projects = []
  @form.field_with(:name => 'projectid').options.each do |opt|
    val = opt.value["project.id='".length..-2]
    projects << [val, opt.text]
  end

  projects.each {|p| @projects_seen[p[0]] = p[1]}

  projects
end

#remark=(remark) ⇒ Object



45
46
47
# File 'lib/achoo/achievo/hour_registration_form.rb', line 45

def remark=(remark)
  @form.remark = remark
end

#submitObject



156
157
158
# File 'lib/achoo/achievo/hour_registration_form.rb', line 156

def submit
  @form.submit()
end

#workperiod=(workperiod) ⇒ Object



61
62
63
# File 'lib/achoo/achievo/hour_registration_form.rb', line 61

def workperiod=(workperiod)
  @form.workperiod = "workperiod.id='#{workperiod}'"
end

#worktime_periodsObject



69
70
71
# File 'lib/achoo/achievo/hour_registration_form.rb', line 69

def worktime_periods
  collect_options('workperiod')
end