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

#initialize(agent) ⇒ HourRegistrationForm

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

def initialize(agent)
  @agent = agent
  @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(@agent)
    @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



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

def 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

#billing=(billing) ⇒ Object



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

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

#billing_optionsObject



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

def billing_options
  collect_options('billpercent')
end

#collect_options(field_name, pattern) ⇒ Object



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

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

#hours=(hours) ⇒ Object



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

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

#phaseObject



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

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

#phase=(phaseid) ⇒ Object



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

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

#phases_for_selected_projectObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/achoo/achievo/hour_registration_form.rb', line 90

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


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

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').options.first.text
  printf format, 'billing',  @form.field_with(:name => 'billpercent').options.first.text

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

end

#projectObject



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

def project
  extract_number_from_projectid(@form.projectid)
end

#project=(projectid) ⇒ Object



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

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

#recent_projectsObject



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/achoo/achievo/hour_registration_form.rb', line 110

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



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

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

#submitObject



159
160
161
# File 'lib/achoo/achievo/hour_registration_form.rb', line 159

def submit
  @form.submit()
end

#workperiod=(workperiod) ⇒ Object



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

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

#worktime_periodsObject



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

def worktime_periods
  collect_options('workperiod')
end