borutus 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -1
- data/Rakefile +9 -3
- data/app/models/borutus/account.rb +39 -1
- data/lib/borutus/version.rb +1 -1
- data/spec/models/account_spec.rb +64 -0
- data/spec/spec_helper.rb +16 -4
- metadata +2 -4
- data/spec/spec.opts +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ed73fe6e709a22d33f2a11f0360c57c0df95b940fe3b67ba97ea22171b941fc
|
4
|
+
data.tar.gz: 23ededc317f6e6373938e23bc4225bb96685fcb4bc240474147ed951f2b7d449
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9eed73e765fbac8b03d41792381c8ced5c8c6e97dec7351ad52fed8c4f6d6aaeb8cf06744fff475590941d5f85050aebe8fa7afb40cfb4c67a3a4edd3ef2d879
|
7
|
+
data.tar.gz: 3935d58c029083d95125f2e2d5aed927b718c16c6d457d232f86434b2ac4c968c6d287df9e4c0c5781eb74b72c6b2e894b965b5c2d2a70b7d3fab6a91d4e1ad4
|
data/README.md
CHANGED
@@ -11,6 +11,7 @@ Compatibility
|
|
11
11
|
=============
|
12
12
|
|
13
13
|
* Rails versions: ~> 5.0
|
14
|
+
* PostgreSQL: > 9.4
|
14
15
|
|
15
16
|
Installation
|
16
17
|
============
|
@@ -325,7 +326,9 @@ mount Borutus::Engine => "/borutus", :as => "borutus"
|
|
325
326
|
Testing
|
326
327
|
=======
|
327
328
|
|
328
|
-
|
329
|
+
Run `docker-compose up pg`, this will create a local postgresql database. Then create a DB name `borutus_fixture_test`, you can do this via `createdb --username=postgres --host=localhost --port=5432 --template=template0 borutus_fixture_test`
|
330
|
+
|
331
|
+
[Rspec](http://rspec.info/) tests are provided. Run `bundle install` then `bundle exec rspec spec`.
|
329
332
|
|
330
333
|
Contributing and Contributors
|
331
334
|
=============================
|
data/Rakefile
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
require 'bundler/setup'
|
2
2
|
|
3
|
+
APP_RAKEFILE = File.expand_path('./fixture_rails_root/Rakefile')
|
4
|
+
load 'rails/tasks/engine.rake'
|
5
|
+
load 'rails/tasks/statistics.rake'
|
6
|
+
|
7
|
+
require 'bundler/gem_tasks'
|
3
8
|
require 'rspec/core/rake_task'
|
4
9
|
|
5
|
-
desc
|
10
|
+
desc 'run specs'
|
11
|
+
|
6
12
|
RSpec::Core::RakeTask.new do |t|
|
7
|
-
t.rspec_opts = [
|
13
|
+
t.rspec_opts = ['-c', '-f d', '-r ./spec/spec_helper.rb']
|
8
14
|
t.pattern = 'spec/**/*_spec.rb'
|
9
15
|
end
|
10
16
|
|
11
|
-
task :
|
17
|
+
task default: :spec
|
@@ -35,7 +35,45 @@ module Borutus
|
|
35
35
|
has_many :amounts
|
36
36
|
has_many :credit_amounts, :extend => AmountsExtension, :class_name => 'Borutus::CreditAmount'
|
37
37
|
has_many :debit_amounts, :extend => AmountsExtension, :class_name => 'Borutus::DebitAmount'
|
38
|
-
has_many :entries, through: :amounts, source: :entry
|
38
|
+
has_many :entries, through: :amounts, source: :entry do
|
39
|
+
def with_running_balance
|
40
|
+
account = proxy_association.owner
|
41
|
+
credit_amounts = account.credit_amounts
|
42
|
+
debit_amounts = account.debit_amounts
|
43
|
+
|
44
|
+
credit_table = credit_amounts.joins(:entry).select(
|
45
|
+
:id,
|
46
|
+
:entry_id,
|
47
|
+
%{ SUM("borutus_amounts".amount) AS amount }
|
48
|
+
).group(:entry_id, :id)
|
49
|
+
|
50
|
+
debit_table = debit_amounts.joins(:entry).select(
|
51
|
+
:id,
|
52
|
+
:entry_id,
|
53
|
+
%{ SUM("borutus_amounts".amount) AS amount }
|
54
|
+
).group(:entry_id, :id)
|
55
|
+
|
56
|
+
select_statement = if account.normal_credit_balance
|
57
|
+
%{ COALESCE("credit_table"."amount", 0) - coalesce("debit_table"."amount", 0) }
|
58
|
+
else
|
59
|
+
%{ COALESCE("debit_table"."amount", 0) - coalesce("credit_table"."amount", 0) }
|
60
|
+
end
|
61
|
+
|
62
|
+
joins(%{
|
63
|
+
LEFT OUTER JOIN (#{credit_table.to_sql}) AS "credit_table" ON "credit_table".entry_id = "borutus_entries".id
|
64
|
+
LEFT OUTER JOIN (#{debit_table.to_sql}) AS "debit_table" ON "debit_table".entry_id = "borutus_entries".id
|
65
|
+
}).select(%{
|
66
|
+
"borutus_entries".*,
|
67
|
+
SUM(#{select_statement}) OVER(ORDER BY "borutus_entries"."created_at") AS balance
|
68
|
+
}).group(
|
69
|
+
:id,
|
70
|
+
%{
|
71
|
+
"debit_table".amount,
|
72
|
+
"credit_table".amount
|
73
|
+
}
|
74
|
+
).order(created_at: :asc)
|
75
|
+
end
|
76
|
+
end
|
39
77
|
has_many :credit_entries, :through => :credit_amounts, :source => :entry, :class_name => 'Borutus::Entry'
|
40
78
|
has_many :debit_entries, :through => :debit_amounts, :source => :entry, :class_name => 'Borutus::Entry'
|
41
79
|
|
data/lib/borutus/version.rb
CHANGED
data/spec/models/account_spec.rb
CHANGED
@@ -14,6 +14,70 @@ module Borutus
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
describe ".entries.with_running_balance" do
|
18
|
+
let(:mock_document) { FactoryGirl.create(:asset) }
|
19
|
+
let!(:accounts_receivable) do
|
20
|
+
FactoryGirl.create(:asset, name: "Accounts Receivable")
|
21
|
+
end
|
22
|
+
let!(:sales_revenue) do
|
23
|
+
FactoryGirl.create(:revenue, name: "Sales Revenue")
|
24
|
+
end
|
25
|
+
let!(:sales_tax_payable) do
|
26
|
+
FactoryGirl.create(:liability, name: "Sales Tax Payable")
|
27
|
+
end
|
28
|
+
let!(:entry_1) do
|
29
|
+
Borutus::Entry.new({
|
30
|
+
description: "Sold some widgets",
|
31
|
+
commercial_document: mock_document,
|
32
|
+
debits: [{account: accounts_receivable, amount: 50}],
|
33
|
+
credits: [
|
34
|
+
{account: sales_revenue, amount: 45},
|
35
|
+
{account: sales_tax_payable, amount: 5}
|
36
|
+
]
|
37
|
+
})
|
38
|
+
end
|
39
|
+
let!(:entry_2) do
|
40
|
+
Borutus::Entry.new({
|
41
|
+
description: "Cancel Accounts receivable some widgets again",
|
42
|
+
commercial_document: mock_document,
|
43
|
+
debits: [
|
44
|
+
{account: accounts_receivable, amount: -30},
|
45
|
+
],
|
46
|
+
credits: [
|
47
|
+
{account: sales_revenue, amount: -25},
|
48
|
+
{account: sales_tax_payable, amount: -5},
|
49
|
+
]
|
50
|
+
})
|
51
|
+
end
|
52
|
+
let!(:entry_3) do
|
53
|
+
Borutus::Entry.new({
|
54
|
+
description: "Cancel Accounts receivable",
|
55
|
+
commercial_document: mock_document,
|
56
|
+
debits: [{account: sales_tax_payable, amount: 15}],
|
57
|
+
credits: [{account: accounts_receivable, amount: 15}],
|
58
|
+
})
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns entries for only the account with a balance column" do
|
62
|
+
entry_1.save
|
63
|
+
entry_2.save
|
64
|
+
entry_3.save
|
65
|
+
|
66
|
+
receivable_entries = accounts_receivable.entries.with_running_balance
|
67
|
+
expect(receivable_entries.to_a.count).to eq 3
|
68
|
+
expect(receivable_entries.first.balance).to eq 50 # inital 50
|
69
|
+
expect(receivable_entries.second.balance).to eq 20 # deduct 30 due to entry_2
|
70
|
+
expect(receivable_entries.last.balance).to eq 5 # deduct 5 due to entry_3
|
71
|
+
|
72
|
+
payable_entries = sales_tax_payable.entries.with_running_balance
|
73
|
+
.order(created_at: :asc)
|
74
|
+
expect(payable_entries.to_a.count).to eq 3
|
75
|
+
expect(payable_entries.first.balance).to eq 5
|
76
|
+
expect(payable_entries.second.balance).to eq 0 # deduct 5 due to entry_2
|
77
|
+
expect(payable_entries.last.balance).to eq -15 # deduct 15 due to entry_3
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
17
81
|
describe "when using a child type" do
|
18
82
|
let(:account) { FactoryGirl.create(:account, type: "Finance::Asset") }
|
19
83
|
it { is_expected.to be_valid }
|
data/spec/spec_helper.rb
CHANGED
@@ -1,22 +1,34 @@
|
|
1
1
|
require 'coveralls'
|
2
2
|
Coveralls.wear!
|
3
3
|
|
4
|
+
require 'pry'
|
5
|
+
|
4
6
|
ENV["RAILS_ENV"] ||= 'test'
|
5
|
-
require File.expand_path(File.dirname(__FILE__) + "/../fixture_rails_root/config/environment")
|
6
7
|
|
8
|
+
require File.expand_path(File.dirname(__FILE__) + "/../fixture_rails_root/config/environment")
|
7
9
|
require Rails.root.join('db/schema').to_s
|
10
|
+
|
8
11
|
require 'rspec/rails'
|
9
12
|
|
10
13
|
$: << File.expand_path(File.dirname(__FILE__) + '/../lib/')
|
14
|
+
|
11
15
|
require 'borutus'
|
12
16
|
require 'kaminari'
|
13
17
|
|
14
|
-
Dir[
|
18
|
+
Dir[
|
19
|
+
File.expand_path(
|
20
|
+
File.join(File.dirname(__FILE__), 'support', '**', '*.rb')
|
21
|
+
)
|
22
|
+
].each do |f|
|
23
|
+
require f
|
24
|
+
end
|
15
25
|
|
16
26
|
require 'factory_girl'
|
17
|
-
borutus_definitions = File.expand_path(File.join(File.dirname(__FILE__), 'factories'))
|
18
|
-
FactoryGirl.definition_file_paths << borutus_definitions
|
19
27
|
|
28
|
+
borutus_definitions = File.expand_path(
|
29
|
+
File.join(File.dirname(__FILE__), 'factories')
|
30
|
+
)
|
31
|
+
FactoryGirl.definition_file_paths << borutus_definitions
|
20
32
|
|
21
33
|
RSpec.configure do |config|
|
22
34
|
config.use_transactional_fixtures = true
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: borutus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ramon Tayag
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2019-01-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -182,7 +182,6 @@ files:
|
|
182
182
|
- spec/rcov.opts
|
183
183
|
- spec/routing/accounts_routing_spec.rb
|
184
184
|
- spec/routing/entries_routing_spec.rb
|
185
|
-
- spec/spec.opts
|
186
185
|
- spec/spec_helper.rb
|
187
186
|
- spec/support/account_shared_examples.rb
|
188
187
|
- spec/support/active_support_helpers.rb
|
@@ -234,7 +233,6 @@ test_files:
|
|
234
233
|
- spec/rcov.opts
|
235
234
|
- spec/routing/accounts_routing_spec.rb
|
236
235
|
- spec/routing/entries_routing_spec.rb
|
237
|
-
- spec/spec.opts
|
238
236
|
- spec/spec_helper.rb
|
239
237
|
- spec/support/account_shared_examples.rb
|
240
238
|
- spec/support/active_support_helpers.rb
|
data/spec/spec.opts
DELETED