debitcredit 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a5ef2706b310724b17c5c806f1f833b3150c584d
4
- data.tar.gz: 3151004ca530b8cdc74e52b189ccd0a17812c945
3
+ metadata.gz: e0adbf3aea2d75c75e6a73e79ab55714f07ddb76
4
+ data.tar.gz: 17b30338b6134d6f03b4be289cbbce731f5692a3
5
5
  SHA512:
6
- metadata.gz: 68ad956fde9ff67286115563e8dd576be91de77fe7361a32aac2f4549755feac7f30760f88349fae57c009869f47f178ac67892b7afbd8fde52bdfb03387a72e
7
- data.tar.gz: be72dc033737575ac2118602123c9f0e5d483f091cd1425fe1b14767804710b4cc1eac4f907a6f4853dee3434f4c0a8fe1188f6dbd284663e4c94ad1683c11f5
6
+ metadata.gz: 4508bfa2c7d9d45d5d6bbdda550c9a91922f87ede097a87f2899083f9de20852398646dc30d12ce2d8684097a8ac35fc195c5510738a5debec9a9393c0473d20
7
+ data.tar.gz: a031a87bfc4d32ff90ff752d9305f62acde430272e041064770decbf8c2de65a9ea0951e9ac4fb4cefbee2a11529999e011cc3b86b1b73769ec854f8914411aa
@@ -3,12 +3,16 @@ module Debitcredit
3
3
  class Transaction < ActiveRecord::Base
4
4
  belongs_to :reference, polymorphic: true
5
5
  belongs_to :parent_transaction, class_name: 'Debitcredit::Transaction'
6
+ belongs_to :inverse_transaction, class_name: 'Debitcredit::Transaction'
6
7
  has_many :items, dependent: :destroy, autosave: true
7
8
 
8
9
  validates :reference, :description, presence: true
9
10
  validate :ensure_balanced
10
11
 
12
+ validate :parent_not_inversed, if: :is_inverse
13
+
11
14
  before_create :lock_and_update_balances
15
+ after_save :save_parent
12
16
 
13
17
  def self.prepare(opts = {}, &block)
14
18
  new(opts).tap do |t|
@@ -16,10 +20,17 @@ module Debitcredit
16
20
  end
17
21
  end
18
22
 
23
+ attr_accessor :is_inverse
19
24
  attr_accessor :ignore_overdraft
25
+
20
26
  def inverse(opts = {})
21
- self.class.new({ignore_overdraft: true}.merge(opts)) do |res|
27
+ self.class.new({ignore_overdraft: true, is_inverse: true}.merge(opts)) do |res|
22
28
  res.items = items.map(&:inverse)
29
+ res.description ||= "reverse of tr ##{id}: #{description}"
30
+ res.kind ||= 'rollback'
31
+ res.reference ||= reference
32
+ res.parent_transaction = self
33
+ self.inverse_transaction ||= res
23
34
  end
24
35
  end
25
36
 
@@ -42,5 +53,13 @@ module Debitcredit
42
53
  item.account.lock!.update_balance!(item, !ignore_overdraft)
43
54
  end
44
55
  end
56
+
57
+ def save_parent
58
+ parent_transaction.save! if parent_transaction.try(:inverse_transaction) == self
59
+ end
60
+
61
+ def parent_not_inversed
62
+ errors.add(:base, :already_inversed) if parent_transaction.inverse_transaction_id
63
+ end
45
64
  end
46
65
  end
@@ -5,6 +5,7 @@ en:
5
5
  models:
6
6
  debitcredit/transaction:
7
7
  unbalanced: item amounts must balance to 0
8
+ already_inversed: parent transaction already inversed
8
9
  debitcredit/account:
9
10
  overdraft: overdraft is not allowed
10
11
 
@@ -0,0 +1,5 @@
1
+ class AddInverseTransactionId < ActiveRecord::Migration
2
+ def change
3
+ add_column :debitcredit_transactions, :inverse_transaction_id, :integer
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module Debitcredit
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debitcredit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitaly Kushner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-30 00:00:00.000000000 Z
11
+ date: 2014-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -205,6 +205,7 @@ files:
205
205
  - db/migrate/20140128134203_longer_account_name.rb
206
206
  - db/migrate/20140128153104_add_account_overdraft.rb
207
207
  - db/migrate/20140130095257_no_overdraft_by_default.rb
208
+ - db/migrate/20140612113944_add_inverse_transaction_id.rb
208
209
  - lib/debitcredit/engine.rb
209
210
  - lib/debitcredit/version.rb
210
211
  - lib/debitcredit.rb