fiscali 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 asanghi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,170 @@
1
+ # Fiscali: Fiscal Year based calculations
2
+
3
+ Fiscali provides ONE thing:
4
+
5
+ * Additional methods on your Date and Time classes to jump around dates in your Financial Year.
6
+ * Not sure it's a point to be said, but YES, you can specify your financial year zone and if you dont know your zone, just provide the first month your financial year to the Date/Time class.
7
+
8
+ ## Quick Links
9
+
10
+ * [Wiki](http://wiki.github.com/asanghi/fiscali)
11
+ * [Bugs](http://github.com/asanghi/fiscali/issues)
12
+ * [Donate](http://pledgie.org/campaigns/5400)
13
+
14
+ ## Installing
15
+
16
+ # Install the gem:
17
+ sudo gem install asanghi-fiscali
18
+
19
+ ## Using in your Rails project
20
+
21
+ It's easy to get up and running. Update your config/environment.rb file with this gem
22
+
23
+ config.gem "asanghi-fiscali", :lib => 'fiscali', :sources => 'http://gems.github.com'
24
+
25
+ Next step is to provide your Date/Time class your start zone. Stick this in an initializer file. (If you didnt understand that, put it in $ENV[RAILS_ROOT]/config/initializers/fiscali.rb)
26
+
27
+ Date.fiscal_zone = :india
28
+ Time.fiscal_zone = :india
29
+ or
30
+ Date.start_month = 4
31
+
32
+
33
+ ### Default options
34
+
35
+ By default, the financial year start in January. (Correct me but thats not a common start of financial year!)
36
+ Known Zones are -
37
+ {:india => 4, :uk => 4, :us => 10, :pakistan => 7,
38
+ :australia => 7, :ireland => 1, :nz => 7, :japan => 4}
39
+
40
+ ## Date or Time Class Methods
41
+
42
+ Date.fy_start_month
43
+ => 1
44
+ Date.fiscal_zone = :india
45
+ => :india
46
+ Date.fy_start_month
47
+ => 4
48
+ Date.fiscal_zone
49
+ => :india
50
+ Date.fy_start_month = 7
51
+ => 7
52
+ Date.fy_start_month
53
+ => 7
54
+ Date.fiscal_zone
55
+ => nil
56
+
57
+ If you want to add your own fiscal zone
58
+
59
+ RisingSun::Fiscali::FISCAL_ZONE.merge!(:my_own_zone => 2)
60
+
61
+ should do the trick.
62
+
63
+ ## Date or Time Instance Methods
64
+
65
+ Date.fiscal_zone = :india
66
+ => :india
67
+ Date.fy_start_month
68
+ => 4
69
+
70
+ Indian Fiscal Year starts from 1st of April
71
+ Assume Date.today is 1st May 2009
72
+
73
+
74
+ ##### financial_year -> Returns the financial year of the date/time
75
+ Date.today.financial_year
76
+ => 2009
77
+ Date.today.beginning_of_year.financial_year
78
+ => 2008
79
+ Since 1st Jan 2009 in India falls in the 2008-09 financial year
80
+
81
+ ##### beginning_of_financial_year -> Returns the beginning of financial year
82
+ Date.today.beginning_of_financial_year
83
+ => 1st April 2009
84
+ Date.today.beginning_of_year.beginning_of_financial_year
85
+ => 1st April 2008
86
+
87
+ ##### end_of_financial_year
88
+
89
+ ##### beginning_of_financial_q1
90
+ ##### end_of_financial_q1
91
+
92
+ ##### beginning_of_financial_q2
93
+ ##### end_of_financial_q2
94
+
95
+ ##### beginning_of_financial_q3
96
+ ##### end_of_financial_q3
97
+
98
+ ##### beginning_of_financial_q4
99
+ ##### end_of_financial_q4
100
+
101
+ ##### beginning_of_financial_h1
102
+ ##### end_of_financial_h1
103
+
104
+ ##### beginning_of_financial_h2
105
+ ##### end_of_financial_h2
106
+
107
+ ##### financial_quarter -> Returns Q1, Q2, Q3, Q4 depending on where the date falls
108
+
109
+ Date.today.financial_quarter
110
+ => Q1
111
+ Date.today.beginning_of_year.financial_quarter
112
+ => Q4
113
+
114
+ ##### financial_half -> Returns H1, H2 depending on where the date falls
115
+
116
+ Date.today.financial_half
117
+ => H1
118
+ Date.today.beginning_of_year.financial_half
119
+ => H2
120
+
121
+ ##### next_financial_quarter -> Takes you to the beginning of the next financial quarter
122
+
123
+ Date.today.next_financial_quarter
124
+ => 1st July 2009
125
+
126
+ ##### next_financial_half -> Takes you to the beginning of the next financial half
127
+
128
+ Date.today.next_financial_half
129
+ => 1st October 2009
130
+
131
+ ##### beginning_of_financial_quarter -> Takes you to the beginning of the current financial quarter
132
+
133
+ Date.today.beginning_of_financial_quarter
134
+ => 1st April 2009
135
+ Date.today.beginning_of_year.beginning_of_financial_quarter
136
+ => 1st Jan 2009
137
+
138
+ ##### beginning_of_financial_half -> Takes you to the beginning of the current financial half
139
+
140
+ Date.today.beginning_of_financial_half
141
+ => 1st April 2009
142
+ Date.today.beginning_of_year.beginning_of_financial_quarter
143
+ => 1st Oct 2008
144
+
145
+ ##### previous_financial_quarter -> Takes you to the beginning of the previous financial quarter
146
+
147
+ Date.today.previous_financial_quarter
148
+ => 1st Jan 2009
149
+ Date.today.beginning_of_year.previous_financial_quarter
150
+ => 1st Oct 2008
151
+
152
+ ##### previous_financial_half -> Takes you to the beginning of the previous financial half
153
+
154
+ Date.today.previous_financial_half
155
+ => 1st Oct 2008
156
+ Date.today.beginning_of_year.previous_financial_quarter
157
+ => 1st Apr 2008
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "fiscali"
8
+ gem.summary = %Q{Fiscal Year Date Functions}
9
+ gem.email = "aditya.sanghi@risingsunbilling.com"
10
+ gem.homepage = "http://asanghi.github.com/fiscali"
11
+ gem.authors = ["Aditya Sanghi"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ gem.add_dependency('activesupport')
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ if File.exist?('VERSION.yml')
46
+ config = YAML.load(File.read('VERSION.yml'))
47
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "fiscali #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
57
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.2.5
@@ -0,0 +1,51 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{fiscali}
5
+ s.version = "1.2.5"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Aditya Sanghi"]
9
+ s.date = %q{2009-08-07}
10
+ s.email = %q{aditya.sanghi@risingsunbilling.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.markdown"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "fiscali.gemspec",
23
+ "install.rb",
24
+ "lib/fiscali.rb",
25
+ "lib/rising_sun/fiscali.rb",
26
+ "test/fiscali_test.rb",
27
+ "test/test_helper.rb"
28
+ ]
29
+ s.homepage = %q{http://asanghi.github.com/fiscali}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.5}
33
+ s.summary = %q{Fiscal Year Date Functions}
34
+ s.test_files = [
35
+ "test/fiscali_test.rb",
36
+ "test/test_helper.rb"
37
+ ]
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
45
+ else
46
+ s.add_dependency(%q<activesupport>, [">= 0"])
47
+ end
48
+ else
49
+ s.add_dependency(%q<activesupport>, [">= 0"])
50
+ end
51
+ end
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,3 @@
1
+ require 'rising_sun/fiscali'
2
+ Date.send(:include, RisingSun::Fiscali)
3
+ Time.send(:include, RisingSun::Fiscali)
@@ -0,0 +1,142 @@
1
+ module RisingSun
2
+ module Fiscali
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ unless included_modules.include? InstanceMethods
6
+ base.send(:include, InstanceMethods)
7
+ end
8
+ end
9
+
10
+ FISCAL_ZONE = {:india => 4, :uk => 4, :us => 10, :pakistan => 7,
11
+ :australia => 7, :ireland => 1, :nz => 7, :japan => 4}
12
+ FY_START_MONTH = 1
13
+
14
+ module ClassMethods
15
+ def fiscal_zone=(zone)
16
+ write_inheritable_attribute(:start_month, FISCAL_ZONE[zone] || FY_START_MONTH)
17
+ write_inheritable_attribute(:zone,zone)
18
+ end
19
+
20
+ def fy_start_month
21
+ read_inheritable_attribute(:start_month) || FY_START_MONTH
22
+ end
23
+
24
+ def fiscal_zone
25
+ read_inheritable_attribute(:zone)
26
+ end
27
+
28
+ def fy_start_month=(month)
29
+ write_inheritable_attribute(:zone, nil)
30
+ write_inheritable_attribute(:start_month,month)
31
+ end
32
+
33
+ def financial_year_start(year=Date.today.year)
34
+ Date.new(year,fy_start_month,1)
35
+ end
36
+
37
+ def financial_months
38
+ (1..12).map{|m| ((m - 1 + fy_start_month)%12.1).ceil }
39
+ end
40
+
41
+ end
42
+
43
+ module InstanceMethods
44
+ def financial_year
45
+ self.month < start_month ? self.year - 1 : self.year
46
+ end
47
+
48
+ def beginning_of_financial_year
49
+ change(:year => financial_year, :month => start_month, :day => 1)
50
+ end
51
+
52
+ def end_of_financial_year
53
+ (beginning_of_financial_year + 1.year - 1.month).end_of_month
54
+ end
55
+
56
+ alias :beginning_of_financial_q1 :beginning_of_financial_year
57
+ def end_of_financial_q1
58
+ end_of_financial_year - 9.months
59
+ end
60
+
61
+ def beginning_of_financial_q2
62
+ beginning_of_financial_year + 3.months
63
+ end
64
+
65
+ def end_of_financial_q2
66
+ end_of_financial_year - 6.months
67
+ end
68
+
69
+ def beginning_of_financial_q3
70
+ beginning_of_financial_year + 6.months
71
+ end
72
+
73
+ def end_of_financial_q3
74
+ end_of_financial_year - 3.months
75
+ end
76
+
77
+ def beginning_of_financial_q4
78
+ beginning_of_financial_year + 9.months
79
+ end
80
+ alias :end_of_financial_q4 :end_of_financial_year
81
+
82
+ alias :beginning_of_financial_h1 :beginning_of_financial_year
83
+ alias :end_of_financial_h1 :end_of_financial_q2
84
+
85
+ alias :beginning_of_financial_h2 :beginning_of_financial_q3
86
+ alias :end_of_financial_h2 :end_of_financial_year
87
+
88
+ def financial_quarter
89
+ "Q#{( months_between / 3 ).floor + 1} #{financial_year}"
90
+ end
91
+
92
+ def financial_half
93
+ "H#{( months_between / 6 ).floor + 1} #{financial_year}"
94
+ end
95
+
96
+ def next_financial_quarter
97
+ beginning_of_financial_year.months_since(((months_between / 3).floor + 1 ) * 3)
98
+ end
99
+
100
+ def next_financial_half
101
+ beginning_of_financial_year.months_since(((months_between / 6).floor + 1) * 6)
102
+ end
103
+
104
+ def beginning_of_financial_quarter
105
+ beginning_of_financial_year.months_since(((months_between / 3).floor) * 3)
106
+ end
107
+
108
+ def beginning_of_financial_half
109
+ beginning_of_financial_year.months_since(((months_between / 6).floor) * 6)
110
+ end
111
+
112
+ def previous_financial_quarter
113
+ beginning_of_financial_quarter.months_ago(3)
114
+ end
115
+
116
+ def previous_financial_half
117
+ beginning_of_financial_half.months_ago(6)
118
+ end
119
+
120
+ def financial_month_of(month)
121
+ if month < start_month
122
+ Date.new(year+1,month,1)
123
+ else
124
+ Date.new(year,month,1)
125
+ end
126
+ end
127
+
128
+ private
129
+
130
+ def months_between
131
+ soy = self.beginning_of_financial_year
132
+ (self.month - soy.month) + 12 * (self.year - soy.year)
133
+ end
134
+
135
+ def start_month
136
+ self.class.read_inheritable_attribute(:start_month) || FY_START_MONTH
137
+ end
138
+
139
+ end
140
+
141
+ end
142
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class FiscaliTest < ActiveSupport::TestCase
4
+ test "date test" do
5
+ Date.fiscal_zone = :india
6
+ assert_equal(Date.fiscal_zone,:india)
7
+ assert_equal(Date.fy_start_month,4)
8
+ Date.fy_start_month = 2
9
+ assert_equal(Date.fy_start_month,2)
10
+
11
+ Date.fiscal_zone = :india
12
+
13
+ d = Date.financial_year_start(2009)
14
+ assert_equal(d,Date.new(2009,4,1),'Financial Year start not correct')
15
+
16
+ d = Date.new(2009,1,1)
17
+ assert_equal(d.financial_year,2008,'Financial Year is not correct')
18
+ assert_equal(d.beginning_of_financial_year,Date.new(2008,4,1),'Beginning of FY is not correct')
19
+ assert_equal(d.beginning_of_financial_h1,Date.new(2008,4,1),'Beginning of h1 is not correct')
20
+ assert_equal(d.beginning_of_financial_h2,Date.new(2008,10,1),'Beginning of h2 is not correct')
21
+ assert_equal(d.beginning_of_financial_q1,Date.new(2008,4,1),'Beginning of q1 is not correct')
22
+ assert_equal(d.beginning_of_financial_q2,Date.new(2008,7,1),'Beginning of q2 is not correct')
23
+ assert_equal(d.beginning_of_financial_q3,Date.new(2008,10,1),'Beginning of q3 is not correct')
24
+ assert_equal(d.beginning_of_financial_q4,Date.new(2009,1,1),'Beginning of q4 is not correct')
25
+
26
+ assert_equal(d.end_of_financial_year,Date.new(2009,3,31),'End of FY is not correct')
27
+ assert_equal(d.end_of_financial_h1,Date.new(2008,9,30),'End of h1 is not correct')
28
+ assert_equal(d.end_of_financial_h2,Date.new(2009,3,31),'End of h2 is not correct')
29
+ assert_equal(d.end_of_financial_q1,Date.new(2008,6,30),'End of q1 is not correct')
30
+ assert_equal(d.end_of_financial_q2,Date.new(2008,9,30),'End of q2 is not correct')
31
+ assert_equal(d.end_of_financial_q3,Date.new(2008,12,31),'End of q3 is not correct')
32
+ assert_equal(d.end_of_financial_q4,Date.new(2009,3,31),'End of q4 is not correct')
33
+
34
+ assert_equal(d.financial_quarter,'Q4 2008','Financial Quarter is not correct')
35
+ assert_equal(Date.new(2008,4,1).financial_quarter,'Q1 2008','Financial Quarter is not correct')
36
+ assert_equal(d.financial_half,'H2 2008','Financial Half is not correct')
37
+ assert_equal(Date.new(2009,11,30).financial_half,'H2 2009','Financial Half is not correct')
38
+
39
+ assert_equal(d.next_financial_half,Date.new(2009,4,1),'Next Financial Half is not correct')
40
+ assert_equal(d.next_financial_quarter,Date.new(2009,4,1),'Next Financial Quarter is not correct')
41
+ assert_equal(Date.new(2009,6,1).next_financial_half,Date.new(2009,10,1), 'Next Financial Half is not correct')
42
+ assert_equal(Date.new(2009,10,30).next_financial_quarter,Date.new(2010,1,1),'Next Financial Quarter is not correct')
43
+
44
+ assert_equal(d.beginning_of_financial_half,
45
+ Date.new(2008,10,1),'Beginning of Financial Half is not correct')
46
+ assert_equal(d.beginning_of_financial_quarter,
47
+ Date.new(2009,1,1),'Beginning of Financial Quarter is not correct')
48
+ assert_equal(Date.new(2009,6,1).beginning_of_financial_half,
49
+ Date.new(2009,4,1), 'Beginning of Financial Half is not correct')
50
+ assert_equal(Date.new(2009,10,30).beginning_of_financial_quarter,
51
+ Date.new(2009,10,1),'Beginning of Financial Quarter is not correct')
52
+
53
+ assert_equal(d.previous_financial_half,
54
+ Date.new(2008,4,1),'Previous Financial Half is not correct')
55
+ assert_equal(d.previous_financial_quarter,
56
+ Date.new(2008,10,1),'Previous Financial Quarter is not correct')
57
+ assert_equal(Date.new(2009,6,1).previous_financial_half,
58
+ Date.new(2008,10,1), 'Previous Financial Half is not correct')
59
+ assert_equal(Date.new(2009,10,30).previous_financial_quarter,
60
+ Date.new(2009,7,1),'Previous Financial Quarter is not correct')
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fiscali
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.5
5
+ platform: ruby
6
+ authors:
7
+ - Aditya Sanghi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-07 00:00:00 +05:30
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: aditya.sanghi@risingsunbilling.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.markdown
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.markdown
39
+ - Rakefile
40
+ - VERSION
41
+ - fiscali.gemspec
42
+ - install.rb
43
+ - lib/fiscali.rb
44
+ - lib/rising_sun/fiscali.rb
45
+ - test/fiscali_test.rb
46
+ - test/test_helper.rb
47
+ has_rdoc: true
48
+ homepage: http://asanghi.github.com/fiscali
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.5
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Fiscal Year Date Functions
75
+ test_files:
76
+ - test/fiscali_test.rb
77
+ - test/test_helper.rb