erp_invoicing 3.0.2 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  class BillingAccount < ActiveRecord::Base
2
2
  has_relational_dynamic_attributes
3
3
  acts_as_financial_txn_account
4
-
5
4
 
5
+ belongs_to :calculate_balance_strategy_type
6
6
  has_many :invoices, :dependent => :destroy do
7
7
  def by_invoice_date
8
8
  order('invoice_date desc')
@@ -22,13 +22,21 @@ class BillingAccount < ActiveRecord::Base
22
22
  end
23
23
  has_one :recurring_payment, :dependent => :destroy
24
24
 
25
+ def self.find_by_account_number(account_number)
26
+ self.includes(:financial_txn_account).where(:financial_txn_accounts => {:account_number => account_number.to_s}).first
27
+ end
28
+
25
29
  def has_recurring_payment_enabled?
26
30
  !self.recurring_payment.nil? and self.recurring_payment.enabled
27
31
  end
28
32
 
29
33
  def has_payments?(status)
30
34
  selected_payment_applications = self.get_payment_applications(status)
31
- !(selected_payment_applications.nil? or selected_payment_applications.empty?)
35
+ !(selected_payment_applications.nil? or selected_payment_applications.empty?)
36
+ end
37
+
38
+ def all_documents
39
+ (self.invoices.collect(&:document) | self.documents).flatten
32
40
  end
33
41
 
34
42
  def get_payment_applications(status=:all)
@@ -48,12 +56,27 @@ class BillingAccount < ActiveRecord::Base
48
56
  selected_payment_applications
49
57
  end
50
58
 
59
+ def calculate_balance
60
+ unless self.calculate_balance_strategy_type.nil?
61
+ case self.calculate_balance_strategy_type.internal_identifier
62
+ when 'invoices_and_payments'
63
+ (self.invoices.balance.amount - self.total_payments)
64
+ when 'payments'
65
+ (self.balance - self.total_payments)
66
+ else
67
+ self.balance
68
+ end
69
+ else
70
+ self.balance
71
+ end
72
+ end
73
+
51
74
  def has_outstanding_balance?
52
75
  (outstanding_balance > 0)
53
76
  end
54
77
 
55
78
  def outstanding_balance
56
- (balance - total_pending_payments)
79
+ (calculate_balance - total_pending_payments)
57
80
  end
58
81
 
59
82
  def total_pending_payments
@@ -66,8 +89,8 @@ class BillingAccount < ActiveRecord::Base
66
89
 
67
90
  #payment due is determined by last invoice
68
91
  def payment_due
69
- if self.calculate_balance and !self.invoices.empty?
70
- self.invoices.by_invoice_date.last.payment_due
92
+ if !self.calculate_balance_strategy_type.nil? and self.calculate_balance_strategy_type.iid == 'invoices_and_payments' and !self.invoices.empty?
93
+ self.current_invoice.payment_due
71
94
  else
72
95
  self.financial_txn_account.payment_due.amount
73
96
  end
@@ -87,6 +110,23 @@ class BillingAccount < ActiveRecord::Base
87
110
  self.financial_txn_account.payment_due.save
88
111
  end
89
112
 
113
+ def balance
114
+ self.financial_txn_account.balance.amount
115
+ end
116
+
117
+ def balance=(amount, currency=Currency.usd)
118
+ if amount.is_a?(Array)
119
+ currency = amount.last
120
+ amount = amount.first
121
+ end
122
+ if self.financial_txn_account.balance
123
+ self.financial_txn_account.balance.amount = amount
124
+ else
125
+ self.financial_txn_account.balance = Money.create(:amount => amount, :currency => currency)
126
+ end
127
+ self.financial_txn_account.balance.save
128
+ end
129
+
90
130
  def billing_date
91
131
  unless self.invoices.empty?
92
132
  current_invoice.invoice_date
@@ -117,28 +157,6 @@ class BillingAccount < ActiveRecord::Base
117
157
  end
118
158
  end
119
159
 
120
- #override balance to use invoices is calculate_balance is set to true
121
- def balance
122
- if self.calculate_balance
123
- self.invoices.balance
124
- else
125
- (self.financial_txn_account.balance.amount - self.total_payments)
126
- end
127
- end
128
-
129
- def balance=(amount, currency=Currency.usd)
130
- if amount.is_a?(Array)
131
- currency = amount.last
132
- amount = amount.first
133
- end
134
- if self.financial_txn_account.balance
135
- self.financial_txn_account.balance.amount = amount
136
- else
137
- self.financial_txn_account.balance = Money.create(:amount => amount, :currency => currency)
138
- end
139
- self.financial_txn_account.balance.save
140
- end
141
-
142
160
  def current_invoice
143
161
  self.invoices.by_invoice_date.last
144
162
  end
@@ -164,7 +182,7 @@ class BillingAccount < ActiveRecord::Base
164
182
 
165
183
  unless primary_party.billing_phone_number.nil? or !previous_cmm_evt.nil?
166
184
  message = SMS_NOTIFICATION_MESSAGE.gsub('payment_due',self.payment_due.to_s)
167
-
185
+
168
186
 
169
187
  # get cmm event purpose type
170
188
  sms_purpose = CommEvtPurposeType.find_by_internal_identifier('sms_notification')
@@ -0,0 +1,11 @@
1
+ class CalculateBalanceStrategyType < ActiveRecord::Base
2
+ has_many :billing_accounts
3
+
4
+ def self.find_by_iid(iid)
5
+ self.find_by_internal_identifier(iid)
6
+ end
7
+
8
+ def iid
9
+ self.internal_identifier
10
+ end
11
+ end
@@ -4,6 +4,8 @@ class Invoice < ActiveRecord::Base
4
4
  belongs_to :billing_account
5
5
  belongs_to :invoice_type
6
6
  belongs_to :invoice_payment_strategy_type
7
+ belongs_to :balance, :class_name => "Money", :foreign_key => 'balance_id', :dependent => :destroy
8
+ belongs_to :calculate_balance_strategy_type
7
9
  has_many :invoice_payment_term_sets, :dependent => :destroy
8
10
  has_many :payment_applications, :as => :payment_applied_to, :dependent => :destroy do
9
11
  def successful
@@ -52,8 +54,20 @@ class Invoice < ActiveRecord::Base
52
54
  selected_payment_applications
53
55
  end
54
56
 
55
- def balance
56
- (self.payment_due - self.total_payments)
57
+ def calculate_balance
58
+ unless self.calculate_balance_strategy_type.nil?
59
+ case self.calculate_balance_strategy_type.internal_identifier
60
+ when 'invoice_items_and_payments'
61
+ (self.items.all.sum(&:total_amount) - self.total_payments)
62
+ when 'payments'
63
+ (self.balance - self.total_payments)
64
+ else
65
+ self.balance
66
+ end
67
+ else
68
+ self.balance
69
+ end
70
+
57
71
  end
58
72
 
59
73
  def payment_due
@@ -1,11 +1,51 @@
1
1
  class PaymentApplication < ActiveRecord::Base
2
-
3
- belongs_to :financial_txn, :dependent => :destroy
4
- belongs_to :payment_applied_to, :polymorphic => true
5
- belongs_to :money, :foreign_key => 'applied_money_amount_id', :dependent => :destroy
2
+
3
+ belongs_to :financial_txn, :dependent => :destroy
4
+ belongs_to :payment_applied_to, :polymorphic => true
5
+ belongs_to :money, :foreign_key => 'applied_money_amount_id', :dependent => :destroy
6
+
7
+ before_destroy :unapply_payment
6
8
 
7
9
  def is_pending?
8
10
  (self.financial_txn.is_scheduled? or self.financial_txn.is_pending?) unless self.financial_txn.nil?
9
11
  end
10
-
12
+
13
+ def apply_payment
14
+ #check the calculate balance strategy, if it includes payments then do nothing
15
+ #if it doesn't include payments then update the balance on the model
16
+ unless self.payment_applied_to.calculate_balance_strategy_type.nil?
17
+ unless self.payment_applied_to.calculate_balance_strategy_type.iid =~ /payment/
18
+ update_applied_to_balance(:debit)
19
+ end
20
+ else
21
+ update_applied_to_balance(:debit)
22
+ end
23
+ end
24
+
25
+ def unapply_payment
26
+ #check the calculate balance strategy, if it includes payments then do nothing
27
+ #if it doesn't include payments then update the balance on the model
28
+ unless self.payment_applied_to.calculate_balance_strategy_type.nil?
29
+ unless self.payment_applied_to.calculate_balance_strategy_type.iid =~ /payment/
30
+ update_applied_to_balance(:credit)
31
+ end
32
+ else
33
+ update_applied_to_balance(:credit)
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def update_applied_to_balance(type)
40
+ #check if payment_applied_to model has a balance= method
41
+ if self.payment_applied_to.respond_to?(:balance=)
42
+ if type == :debit
43
+ self.payment_applied_to.balance = self.payment_applied_to.balance - self.money.amount
44
+ else
45
+ self.payment_applied_to.balance = self.payment_applied_to.balance + self.money.amount
46
+ end
47
+ self.payment_applied_to.save
48
+ end
49
+ end
50
+
11
51
  end
@@ -0,0 +1,20 @@
1
+ class AddCalculateBalanceStrategyTypes
2
+
3
+ def self.up
4
+ [
5
+ ['Calculate with invoices and payments', 'invoices_and_payments'],
6
+ ['Calculate with payments', 'payments'],
7
+ ['Calculate with invoices items and payments', 'invoice_items_and_payments']
8
+ ].each do |item|
9
+ CalculateBalanceStrategyType.create(:description => item[0], :internal_identifier => item[1])
10
+ end
11
+
12
+ end
13
+
14
+ def self.down
15
+ ['invoices_and_payments','payments', 'invoice_items_and_payments'].each do |iid|
16
+ CalculateBalanceStrategyType.destroy_all("internal_identifier = #{iid}")
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,18 @@
1
+ class CreateCalcuateBalanceStrategyType < ActiveRecord::Migration
2
+ def up
3
+ unless table_exists?(:calculate_balance_strategy_types)
4
+ create_table :calculate_balance_strategy_types do |t|
5
+ t.string :internal_identifier
6
+ t.string :description
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
12
+
13
+ def down
14
+ if table_exists?(:calculate_balance_strategy_types)
15
+ drop_table calculate_balance_strategy_types
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ class AddCalculateBalanceStrategyToBillingAccount < ActiveRecord::Migration
2
+ def change
3
+ add_column :billing_accounts, :calculate_balance_strategy_type_id, :integer
4
+
5
+ add_index :billing_accounts, :calculate_balance_strategy_type_id
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ class AddBalanceToInvoice < ActiveRecord::Migration
2
+ def change
3
+ add_column :invoices, :balance_id, :integer
4
+ add_column :invoices, :calculate_balance_strategy_type_id, :integer
5
+
6
+ add_index :invoices, :balance_id
7
+ add_index :invoices, :calculate_balance_strategy_type_id
8
+ end
9
+ end
@@ -2,7 +2,7 @@ module ErpInvoicing
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
4
  MINOR = 0
5
- TINY = 2
5
+ TINY = 3
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].compact.join('.')
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erp_invoicing
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-25 00:00:00.000000000 Z
12
+ date: 2012-06-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: erp_inventory
16
- requirement: &2160818020 !ruby/object:Gem::Requirement
16
+ requirement: &2152724880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2160818020
24
+ version_requirements: *2152724880
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: erp_work_effort
27
- requirement: &2160817520 !ruby/object:Gem::Requirement
27
+ requirement: &2152723880 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2160817520
35
+ version_requirements: *2152723880
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: erp_commerce
38
- requirement: &2160817060 !ruby/object:Gem::Requirement
38
+ requirement: &2152722720 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3.0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2160817060
46
+ version_requirements: *2152722720
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: erp_dev_svcs
49
- requirement: &2160832980 !ruby/object:Gem::Requirement
49
+ requirement: &2152721260 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '3.0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2160832980
57
+ version_requirements: *2152721260
58
58
  description: ErpInvoicing adds models and services to the CompassAE core to handle
59
59
  invoicing and billing functions. It includes extensions to the core ERP classes
60
60
  for accounts and things that are 'billable' (products, work efforts), and additional
@@ -90,6 +90,7 @@ files:
90
90
  - app/controllers/erp_invoicing/sms_controller.rb
91
91
  - app/helpers/erp_invoicing/application_helper.rb
92
92
  - app/models/billing_account.rb
93
+ - app/models/calculate_balance_strategy_type.rb
93
94
  - app/models/extensions/document.rb
94
95
  - app/models/extensions/financial_txn.rb
95
96
  - app/models/extensions/financial_txn_account.rb
@@ -113,9 +114,13 @@ files:
113
114
  - db/data_migrations/20111121153349_create_bill_pay_organizer_application.rb
114
115
  - db/data_migrations/20120118181839_create_invoice_management_desktop_application.rb
115
116
  - db/data_migrations/20120229174322_add_billpay_widget.rb
117
+ - db/data_migrations/20120605154637_add_calculate_balance_strategy_types.rb
116
118
  - db/migrate/20111121000000_invoicing_services.rb
117
119
  - db/migrate/20120228184317_add_text_to_pay_to_recurring_payments.rb
118
120
  - db/migrate/20120301155722_add_billing_date_to_billing_account.rb
121
+ - db/migrate/20120605144227_create_calcuate_balance_strategy_type.rb
122
+ - db/migrate/20120605144900_add_calculate_balance_strategy_to_billing_account.rb
123
+ - db/migrate/20120605150425_add_balance_to_invoice.rb
119
124
  - lib/erp_invoicing/engine.rb
120
125
  - lib/erp_invoicing/version.rb
121
126
  - lib/erp_invoicing.rb