Class: Tbr::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/tbr/processor.rb

Constant Summary collapse

UNASSIGNED =
'Unassigned'
USAGE =
'usage: Tbr::Processor.new([ output: output_pathname, services: service_array, log: log_filename, replace: true/false, logo: logo_filename, original: original_filename ])'
LOG_DEFAULT =
'/tmp/tbr.log'
LOGO_DEFAULT =
File.join(File.dirname(__FILE__), '../logo.jpg')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Processor

Returns a new instance of Processor.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tbr/processor.rb', line 23

def initialize(options = {})
  raise ArgumentError, USAGE unless options.class == Hash
  
  @log = Tbr.log
  @log.info('Initialising Telstra Billing Reporter')
  
  self.output   = options[:output]
  self.     = options[:logo]
  self.services = options[:services]
  @replace      = options[:replace] || false
  @original     = options[:original] 
end

Instance Attribute Details

#logoObject

Returns the value of attribute logo.



21
22
23
# File 'lib/tbr/processor.rb', line 21

def 
  
end

#logpathObject (readonly)

Returns the value of attribute logpath.



21
22
23
# File 'lib/tbr/processor.rb', line 21

def logpath
  @logpath
end

#originalObject

Returns the value of attribute original.



20
21
22
# File 'lib/tbr/processor.rb', line 20

def original
  @original
end

#outputObject

Returns the value of attribute output.



21
22
23
# File 'lib/tbr/processor.rb', line 21

def output
  @output
end

#replaceObject

Returns the value of attribute replace.



20
21
22
# File 'lib/tbr/processor.rb', line 20

def replace
  @replace
end

#servicesObject

Returns the value of attribute services.



21
22
23
# File 'lib/tbr/processor.rb', line 21

def services
  @services
end

Instance Method Details

#import_services(services_file) ⇒ Object



36
37
38
# File 'lib/tbr/processor.rb', line 36

def import_services(services_file)
  @services = ParseFiles::parse_services_file(services_file)
end

#process(input) ⇒ Object

Raises:

  • (ArgumentError)


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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tbr/processor.rb', line 63

def process(input)    
  fname = @original || input  # if anonymous tmp file used after upload
  @log.info("Processing Telstra Bill File: #{fname || 'nil'}")
  
  raise ArgumentError, 'Filename not provided' unless input.class == String

  unless File.exist?(input)
    message = "Input file #{input} does not exist."
    @log.error(message)
    raise IOError, message
  end    

  @log.warn("All services will be classified as unassigned") if @services.empty?
  @log.info("Files being written to #{output}")
  
  @log.info("Extracting Call Types from #{fname}")
  call_type = CallType.new
  call_type.load(input)

  services = Services.new
  groups = Groups.new

  @log.info("Extracting billing data from #{fname}")
  ParseFiles.map_services(groups,services,@services)
  invoice_date = ParseFiles.parse_bill_file(services,call_type,input)

  @log.info("Building Unassigned group")
  group = groups.group(UNASSIGNED)
  services.each do |service|
    group.add_service(service) if service.name == UNASSIGNED
  end
  
  unassigned_count = groups.group(UNASSIGNED).size
  @log.warn("#{unassigned_count} unassigned services") if unassigned_count > 0

  cf = CreateFiles.new(invoice_date,output,replace)
  cf. = 

  @log.info("Creating group summaries")
  groups.each do |group|
    @log.info("Creating summary for #{group.name}")
    cf.group_summary(group)
  end

  @log.info("Creating service details")
  services.each do |service|
    @log.info("Creating report for #{service.service_number}")
    cf.call_details(service)
  end

  @log.info("Creating cost centre summary")
  cost_centres = {}
  services.each do |service|
    cc = service.cost_centre
    unless cc.empty?
      cc_total = cost_centres[cc] || 0.0
      cost_centres[cc] = cc_total + service.total
    end
  end
  cf.cost_centre_totals(cost_centres)
  
  @log.info("Creating service totals summary")
  cf.service_totals(services)

  @log.info("PDF report files can be found in #{cf.dir_full_root}.") 
  @log.info("Telstra billing data extract completed.") 
end