tbr 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c14b3a149fa446a5777fc978a22ed6f525755825
4
+ data.tar.gz: 36c40d1947d3f027b7929a5a7776142f0ed91168
5
+ SHA512:
6
+ metadata.gz: 7e69392f796d8ac96eba528c32c7a51a2f54e92151d5e1ba073b6ea7dc5ff249fff198bdc007e9b926a1f75d72481fbbb02cdeda686e68752f8a380df9e4e533
7
+ data.tar.gz: 4e4884991307774dcbadb648c8d66a8aa6d8d7149cea2284969afecae1dd64bfbaa81209d0ba8fa9bc1cd1710e2d274a2615ae55e7e0f4379684a002aadb1e89
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ *.pdf
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.bundle
20
+ *.so
21
+ *.o
22
+ *.a
23
+ mkmf.log
@@ -0,0 +1,8 @@
1
+ # TBR Change Log
2
+
3
+ ## Release 0.0.1
4
+ Unsurprisingly, the firat release! Contains all the basic functionality ported from the [original GTK project.](https://github.com/Bwian/tbr_gtk)
5
+
6
+ ## Release 0.0.2
7
+ Changed API to encapsulate the processing functionality in its own class (Processor). Functionality now initialised by a simple instance method rather than the previous class method.
8
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tbr.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Pyrrho Pty Ltd
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # TBR
2
+
3
+ The Telstra Billing Reporter gem (tbr) provides functionality to parse monthly billing files produced in accordance with Telstra On Line Billing Service technical specfication version 6.4 and produces detailed and summary billing reports in PDF format. Minor updates to the Telstra specification are unlikely to cause any problems.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tbr'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tbr
18
+
19
+ ## Usage
20
+
21
+ Create an instance of Tbr::Processor. Most instance variables can be set using arguments to initialize or by calling smae named methods on the instance.
22
+
23
+ ##### initialize(*args)
24
+ Accepts a hash of optional elments with the following keys:
25
+
26
+ _:output_ - the pathname of the output directory. Defaults to /tmp.
27
+
28
+ _:services_ - a two dimensional array, each row containing the elements service number, group, description and cost centre. Defaults to an empty array meaning services will not be grouped.
29
+
30
+ _:log_ - logfile pathname. Defaults to /dev/null. STDOUT, STDERR or a file pathname are valid arguments.
31
+
32
+ _:replace_ - boolean flag which determines replacement of pre-existing output files. Defaults to false.
33
+
34
+ _:logo_ - logo pathname. Defaults to the logo.jpg in the tbr gem
35
+
36
+ _:original_ - original filename. Used for log messages if uploaded file uses an anonymous temporary filename
37
+
38
+ ##### import__services(filename)
39
+ Accepts the path name of a csv file in which the first four cells in each row equate to those in the array of the _:services_ argument to _initialize()_. The imported array is stored in the _services_ instance variable.
40
+
41
+ ##### process(filename)
42
+ Raises ArgumentError along argument isn't a String.
43
+
44
+ Raises IOError if the input file doesn't exist.
45
+
46
+ Log messages are written to track progress and record use of default settings.
47
+
48
+ A top-level directory is created in the output path named in the format _YYYYDD_, the underlying date being extracted from the Telstra billing file. A _details_ subdirectory contains separate files for each service. A _summaries_ subdirectory contains summaries for each service by service group as specified in the _:services_ array argument to _process()_. The top-level directory also contains an overall _Service Totals_ file containing one line per service.
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it ( https://github.com/[my-github-username]/tbr/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create a new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
8
+
9
+
Binary file
@@ -0,0 +1,18 @@
1
+ require 'logger'
2
+ require "tbr/version"
3
+
4
+ module Tbr
5
+
6
+ class << self
7
+ def log
8
+ @log || Logger.new(STDOUT)
9
+ end
10
+
11
+ def log=(logger)
12
+ @log = logger
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ require "tbr/processor"
@@ -0,0 +1,24 @@
1
+ require 'time'
2
+
3
+ class CallDetail
4
+
5
+ # OBS record type DC - page 41
6
+
7
+ attr_reader :call_type, :duration, :destination, :area, :start_date, :start_time, :cost
8
+
9
+ def initialize(record,call_type)
10
+ fields = record.split(',')
11
+ if fields[0] == 'DC' && fields[8] == 'D' then # Call Detail record
12
+ @call_type = call_type.desc(fields[9])
13
+ @duration = Time.at(fields[10].to_i).gmtime.strftime('%R:%S')
14
+ @destination = fields[12]
15
+ @area = fields[16]
16
+ @start_date = Time.parse(fields[18]).strftime('%d/%m/%Y')
17
+ @start_time = fields[20]
18
+ @cost = fields[33].to_f
19
+ else
20
+ raise ArgumentError, "Invalid record type - " + fields[0], caller
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,23 @@
1
+ class CallType
2
+
3
+ def initialize
4
+ @types = Hash.new
5
+ end
6
+
7
+ def load(filename)
8
+ file = File.new(filename)
9
+ @types = Hash.new
10
+ file.each_line do |line|
11
+ fields = line.split(',')
12
+ @types[fields[1]] = fields[3] if fields[0] == 'TC'
13
+ end
14
+ end
15
+
16
+ def size
17
+ @types.size
18
+ end
19
+
20
+ def desc(code)
21
+ @types[code] ? @types[code] : "Unknown service type - " + code
22
+ end
23
+ end
@@ -0,0 +1,176 @@
1
+ require 'fileutils'
2
+ require 'prawn'
3
+ require 'prawn/table'
4
+
5
+ class CreateFiles
6
+ ROW_COLOUR_ODD = "EBD6D6"
7
+ ROW_COLOUR_EVEN = "FFFFFF"
8
+ ROW_COLOUR_HEAD = "D6ADAD"
9
+ UCB_GREEN = "666633"
10
+ UCB_RED = "993333"
11
+
12
+ # Header for each phone service
13
+ attr_reader :invoice_month, :dir_root
14
+ attr_accessor :logo
15
+
16
+ def initialize(invoice_date,dir_out,replace)
17
+ @invoice_month = Time.parse(invoice_date).strftime('%B %Y')
18
+ @dir_root = "#{dir_out || '/tmp'}/#{invoice_date[0..5]}"
19
+ @dir_summaries = "#{@dir_root}/summaries"
20
+ @dir_details = "#{@dir_root}/details"
21
+ @logo = Tbr::Processor::LOGO_DEFAULT
22
+
23
+ FileUtils.rm_rf(@dir_root) if replace
24
+ if File.exist?(@dir_root)
25
+ message = "Output directory #{dir_full_root} already exists."
26
+ Tbr.log.fatal(message)
27
+ raise IOError, message
28
+ end
29
+
30
+ Dir.mkdir(@dir_root)
31
+ Dir.mkdir(@dir_summaries)
32
+ Dir.mkdir(@dir_details)
33
+ end
34
+
35
+ def group_summary(group)
36
+ create_group_summary(group)
37
+ end
38
+
39
+ def call_details(service)
40
+ create_call_details(service)
41
+ end
42
+
43
+ def service_totals(services)
44
+ create_service_totals(services)
45
+ end
46
+
47
+ def dir_full_root
48
+ File.realdirpath(@dir_root)
49
+ end
50
+
51
+ private
52
+
53
+ def header(pdf,heading,name)
54
+ logo_path = @logo
55
+ pdf.table([
56
+ [{:image => logo_path, :scale => 0.1, :rowspan => 2},
57
+ {:content => heading, :rowspan => 2, :text_color => UCB_GREEN, :size => 20, :font_style => :bold },
58
+ {:content => @invoice_month, :text_color => UCB_RED, :font_style => :bold}],
59
+ [{:content => name, :text_color => UCB_RED, :font_style => :bold}]
60
+ ], :column_widths => [130,260,130]) do
61
+ cells.padding = 0
62
+ cells.borders = []
63
+ column(2).style(:align => :right)
64
+ column(1).style(:align => :center)
65
+ end
66
+ end
67
+
68
+ def footer(pdf)
69
+ page_no = '<page> of <total>'
70
+ pdf.number_pages(page_no, {:at => [0,0], :align => :center})
71
+ end
72
+
73
+ def create_group_summary(group)
74
+ fname = "#{@dir_summaries}/#{group.name} - #{@invoice_month}.pdf"
75
+ Prawn::Document.generate(fname, :page_layout => :portrait, :page_size => "A4") do |pdf|
76
+ header(pdf,"Telstra Billing Summary","Manager: #{group.name}")
77
+ group.each do |service|
78
+ format_summary(pdf,service)
79
+ end
80
+ footer(pdf)
81
+ end
82
+ end
83
+
84
+ def create_call_details(service)
85
+ fname = "#{@dir_details}/#{service.service_number} - #{@invoice_month}.pdf"
86
+
87
+ data = [['Start','','Type','Destination','Area','Duration','Cost']]
88
+ service.call_details.each do |cd|
89
+ data << [cd.start_date, cd.start_time, cd.call_type, cd.destination, cd.area, cd.duration, sprintf("%.2f",cd.cost)]
90
+ end
91
+
92
+ Prawn::Document.generate(fname, :page_layout => :portrait, :page_size => "A4") do |pdf|
93
+ header(pdf,"Telstra Billing Details","")
94
+ format_summary(pdf,service)
95
+
96
+ pdf.font_size 10
97
+ pdf.move_down 18
98
+
99
+ pdf.table(data,
100
+ :row_colors => [ROW_COLOUR_EVEN, ROW_COLOUR_ODD],
101
+ :header => :true,
102
+ :column_widths => [55,45,155,100,80,45,40]) do
103
+ cells.padding = 2
104
+ cells.borders = []
105
+
106
+ row(0).font_style = :bold
107
+ row(0).background_color = ROW_COLOUR_HEAD
108
+ column(6).style(:align => :right)
109
+ end
110
+
111
+ footer(pdf)
112
+ end
113
+ end
114
+
115
+ def create_service_totals(services)
116
+ fname = "#{@dir_root}/Service Totals - #{@invoice_month}.pdf"
117
+
118
+ grand_total = 0.0
119
+ data = [["Number", "Name", "CC", "Total"]]
120
+ services.each do |service|
121
+ data << [service.service_number_format, service.name, service.cost_centre, sprintf("%.2f", service.total)]
122
+ grand_total += service.total
123
+ end
124
+ data << ['','','Grand Total:', sprintf('$%8.2f',grand_total)]
125
+
126
+ Prawn::Document.generate(fname, :page_layout => :portrait, :page_size => "A4") do |pdf|
127
+ header(pdf,"Telstra Service Totals","")
128
+
129
+ pdf.font_size 10
130
+ pdf.move_down 18
131
+
132
+ pdf.table(data,
133
+ :row_colors => [ROW_COLOUR_EVEN, ROW_COLOUR_ODD],
134
+ :header => :true,
135
+ :column_widths => [100,270,100,50]) do
136
+ cells.padding = 2
137
+ cells.borders = []
138
+
139
+ last = data.size - 1
140
+ row([0,last]).font_style = :bold
141
+ row([0,last]).background_color = ROW_COLOUR_HEAD
142
+ column(3).style(:align => :right)
143
+ end
144
+
145
+ footer(pdf)
146
+ end
147
+ end
148
+
149
+ def format_summary(pdf,service)
150
+ total = 0.0
151
+ data = [["Call Type", "Service", "", "", "Cost"]]
152
+ service.service_summaries.each do |ss|
153
+ data << ss.to_a
154
+ total += ss.cost
155
+ end
156
+ data << ['','','','Total:', sprintf('$%7.2f',total)]
157
+
158
+ pdf.font_size 10
159
+ pdf.move_down 18
160
+ pdf.text("#{service.service_number_format} - #{service.name}", :style => :bold, :align => :center)
161
+ pdf.move_down 6
162
+
163
+ pdf.table(data,
164
+ :row_colors => [ROW_COLOUR_ODD, ROW_COLOUR_EVEN],
165
+ :column_widths => [220,150,50,50,50]) do
166
+ cells.padding = 2
167
+ cells.borders = []
168
+
169
+ last = data.size - 1
170
+ row([0,last]).font_style = :bold
171
+ row([0,last]).background_color = ROW_COLOUR_HEAD
172
+ column([2,4]).style(:align => :right)
173
+ end
174
+ end
175
+
176
+ end
@@ -0,0 +1,22 @@
1
+ class Group
2
+
3
+ # Phone services grouped for
4
+ attr_reader :name
5
+
6
+ def initialize(name)
7
+ @name = name
8
+ @services = Array.new
9
+ end
10
+
11
+ def add_service(service)
12
+ @services << service
13
+ end
14
+
15
+ def size
16
+ @services.size
17
+ end
18
+
19
+ def each(&blk)
20
+ @services.each(&blk)
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ class Groups
2
+
3
+ # Groups container
4
+
5
+ def initialize
6
+ @groups = Hash.new
7
+ end
8
+
9
+ def group(name)
10
+ return nil if name.nil? or name.empty?
11
+
12
+ unless @groups.include?(name)
13
+ @groups[name] = Group.new(name)
14
+ end
15
+ @groups[name]
16
+ end
17
+
18
+ def size
19
+ @groups.size
20
+ end
21
+
22
+ def each(&blk)
23
+ Hash[@groups.sort].each_value(&blk)
24
+ end
25
+ end
@@ -0,0 +1,96 @@
1
+ require 'csv'
2
+
3
+ class ParseFiles
4
+
5
+ SERVICE_NUMBER = 0
6
+ SERVICE_GROUP = 1
7
+ SERVICE_NAME = 2
8
+ SERVICE_CC = 3
9
+
10
+ DH_INVOICE_DATE = 2
11
+ OBR_SERVICE_NUMBER = 6
12
+
13
+ def self.parse_services_file(services_file)
14
+ out = []
15
+ begin
16
+ CSV.foreach(services_file) do |fields|
17
+ out << fields if valid_fields(fields)
18
+ end
19
+ Tbr.log.warn("Empty or invalid services file: #{services_file}. All services will be classified as unassigned") if out.empty?
20
+ rescue Errno::ENOENT
21
+ message = "Error accessing services file: #{services_file}"
22
+ Tbr.log.error(message)
23
+ raise IOError, message
24
+ end
25
+ out
26
+ end
27
+
28
+ def self.map_services(groups,services,services_list)
29
+ services_list.each do |fields|
30
+ next if !valid_fields(fields)
31
+
32
+ group = groups.group(fields[SERVICE_GROUP])
33
+ service = services.service(fields[SERVICE_NUMBER])
34
+ service.name = fields[SERVICE_NAME]
35
+ service.cost_centre = fields[SERVICE_CC]
36
+ group.add_service(service)
37
+ end
38
+ Tbr.log.warn("Empty services list. All services will be classified as unassigned") if services.size == 0
39
+ end
40
+
41
+ def self.parse_bill_file(services,call_type,bill_file)
42
+ invoice_date = ''
43
+
44
+ begin
45
+ file = File.new(bill_file)
46
+
47
+ file.each_line do |line|
48
+ fields = line.split(',')
49
+ service_number = fields[OBR_SERVICE_NUMBER]
50
+
51
+ case fields[0]
52
+ when "DH"
53
+ invoice_date = fields[DH_INVOICE_DATE]
54
+
55
+ when "DS"
56
+ service = services.service(service_number)
57
+ service_summary = ServiceSummary.new(line,call_type)
58
+ service.add_service_summary(service_summary)
59
+
60
+ when "DC"
61
+ service = services.service(service_number)
62
+ call_detail = CallDetail.new(line,call_type)
63
+ service.add_call_detail(call_detail)
64
+ end
65
+ end
66
+
67
+ if invoice_date.empty?
68
+ message = "Invalid billing file: #{bill_file}"
69
+ Tbr.log.fatal(message)
70
+ raise ArgumentError, message
71
+ end
72
+
73
+ rescue Errno::ENOENT
74
+ message = "Error accessing billing file: #{bill_file}"
75
+ Tbr.log.fatal(message)
76
+ raise IOError, message
77
+
78
+ rescue ArgumentError => e
79
+ message = "Error parsing billing file: #{bill_file}"
80
+ Tbr.log.fatal(message) unless e.message.start_with?('Invalid')
81
+ raise ArgumentError, message
82
+ end
83
+
84
+ invoice_date
85
+ end
86
+
87
+ def self.valid_fields(fields)
88
+ return false if fields.size < 4
89
+ return false if fields[0].nil? or fields[0].empty? or fields[1].nil? or fields[1].empty?
90
+ return false unless fields[0].match(/[0-9]/)
91
+
92
+ return true
93
+ end
94
+
95
+ private_class_method :valid_fields
96
+ end