erp_txns_and_accts 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/GPL-3-LICENSE +674 -0
  2. data/README.rdoc +2 -0
  3. data/Rakefile +30 -0
  4. data/app/assets/javascripts/erp_txns_and_accts/application.js +9 -0
  5. data/app/assets/stylesheets/erp_txns_and_accts/application.css +7 -0
  6. data/app/controllers/erp_txns_and_accts/application_controller.rb +4 -0
  7. data/app/helpers/erp_txns_and_accts/application_helper.rb +4 -0
  8. data/app/models/base_txn_context.rb +6 -0
  9. data/app/models/biz_acct_txn_task.rb +3 -0
  10. data/app/models/biz_txn_acct_party_role.rb +23 -0
  11. data/app/models/biz_txn_acct_pty_rtype.rb +3 -0
  12. data/app/models/biz_txn_acct_rel_type.rb +3 -0
  13. data/app/models/biz_txn_acct_relationship.rb +8 -0
  14. data/app/models/biz_txn_acct_root.rb +21 -0
  15. data/app/models/biz_txn_acct_status.rb +3 -0
  16. data/app/models/biz_txn_acct_status_type.rb +3 -0
  17. data/app/models/biz_txn_acct_type.rb +4 -0
  18. data/app/models/biz_txn_agreement_role.rb +7 -0
  19. data/app/models/biz_txn_agreement_role_type.rb +4 -0
  20. data/app/models/biz_txn_event.rb +61 -0
  21. data/app/models/biz_txn_event_desc.rb +5 -0
  22. data/app/models/biz_txn_party_role.rb +7 -0
  23. data/app/models/biz_txn_party_role_type.rb +4 -0
  24. data/app/models/biz_txn_rel_type.rb +4 -0
  25. data/app/models/biz_txn_relationship.rb +8 -0
  26. data/app/models/biz_txn_status.rb +3 -0
  27. data/app/models/biz_txn_task.rb +3 -0
  28. data/app/models/biz_txn_task_type.rb +3 -0
  29. data/app/models/biz_txn_type.rb +36 -0
  30. data/app/models/extensions/charge_line.rb +8 -0
  31. data/app/models/extensions/charge_line_payment_txn.rb +3 -0
  32. data/app/models/extensions/money.rb +6 -0
  33. data/app/models/extensions/party.rb +27 -0
  34. data/app/models/financial_txn.rb +71 -0
  35. data/app/models/financial_txn_account.rb +71 -0
  36. data/app/views/layouts/erp_txns_and_accts/application.html.erb +14 -0
  37. data/config/routes.rb +2 -0
  38. data/db/data_migrations/20101014142230_financial_txn_types.rb +15 -0
  39. data/db/migrate/20080805000030_base_txns_and_accts.rb +360 -0
  40. data/db/migrate/20110609230152_update_financial_txns.rb +21 -0
  41. data/lib/erp_txns_and_accts.rb +6 -0
  42. data/lib/erp_txns_and_accts/delayed_jobs/payment_gateway_job.rb +24 -0
  43. data/lib/erp_txns_and_accts/engine.rb +12 -0
  44. data/lib/erp_txns_and_accts/extensions.rb +3 -0
  45. data/lib/erp_txns_and_accts/extensions/active_record/acts_as_biz_txn_account.rb +63 -0
  46. data/lib/erp_txns_and_accts/extensions/active_record/acts_as_biz_txn_event.rb +72 -0
  47. data/lib/erp_txns_and_accts/extensions/active_record/acts_as_financial_txn_account.rb +70 -0
  48. data/lib/erp_txns_and_accts/version.rb +3 -0
  49. data/lib/tasks/erp_txns_and_accts_tasks.rake +4 -0
  50. metadata +144 -0
@@ -0,0 +1,71 @@
1
+ class FinancialTxnAccount < ActiveRecord::Base
2
+ acts_as_biz_txn_account
3
+
4
+ scope :accounts_for_agreements, lambda { |agreement_ids| where("agreement_id in (?)", agreement_ids).order('due_date ASC')}
5
+ scope :due, includes([:balance]).where("money.amount != 0")
6
+
7
+ belongs_to :agreement
8
+ belongs_to :balance, :dependent => :destroy
9
+ belongs_to :payment_due, :dependent => :destroy
10
+ belongs_to :financial_account, :polymorphic => true, :dependent => :destroy
11
+
12
+ def financial_txns(result_set, options={})
13
+ txns = nil
14
+
15
+ options.merge!({:conditions => "biz_txn_record_type = 'FinancialTxn'"})
16
+
17
+ if(result_set == :all)
18
+ txns = self.biz_txn_events.find(:all, options).collect{|biz_txn_event| biz_txn_event.biz_txn_record}
19
+ else
20
+ txns = self.biz_txn_events.find(:first, options).biz_txn_record
21
+ end
22
+
23
+ txns
24
+ end
25
+
26
+ def account_delinquent?
27
+ past_due = false
28
+
29
+ if self.due_date.nil?
30
+ past_due = false
31
+ elsif self.payment_due.nil? || self.payment_due.amount == 0
32
+ past_due = false
33
+ else
34
+ past_due = self.due_date.past?
35
+ end
36
+
37
+ past_due
38
+ end
39
+
40
+ def authorize_payment_txn(credit_card_info, gateway)
41
+ due_amount = self.payment_due.amount
42
+
43
+ financial_txn = FinancialTxn.create(:money => Money.create(:amount => due_amount))
44
+
45
+ financial_txn.account = self
46
+ financial_txn.description = "Payment on account #{self.account_number} of #{due_amount}"
47
+
48
+ financial_txn.txn_type = BizTxnType.iid('payment_txn')
49
+ financial_txn.save
50
+
51
+ financial_txn.authorize_payment(credit_card_info, gateway)
52
+ end
53
+
54
+ def finalize_payment_txn(credit_card_info, gateway)
55
+ financial_txn = self.biz_txn_events.where("biz_txn_record_type = 'FinancialTxn'").order('biz_txn_events.created_at desc').first
56
+ result = financial_txn.finalize_payment(credit_card_info, gateway)
57
+
58
+ if result[:success]
59
+ self.payment_due.amount = 0
60
+ self.payment_due.save
61
+ end
62
+
63
+ result
64
+ end
65
+
66
+ def rollback_last_txn(credit_card_info, gateway)
67
+ financial_txn = self.biz_txn_events.where("biz_txn_record_type = 'FinancialTxn'").order('biz_txn_events.created_at desc').first
68
+ financial_txn.reverse_authorization(credit_card_info, gateway)
69
+ end
70
+
71
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>ErpTxnsAndAccts</title>
5
+ <%= stylesheet_link_tag "erp_txns_and_accts/application" %>
6
+ <%= javascript_include_tag "erp_txns_and_accts/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ ErpTxnsAndAccts::Engine.routes.draw do
2
+ end
@@ -0,0 +1,15 @@
1
+ class FinancialTxnTypes
2
+
3
+ def self.up
4
+ BizTxnType.create(
5
+ :description => "Payment Transaction",
6
+ :internal_identifier => 'payment_txn'
7
+ )
8
+ end
9
+
10
+ def self.down
11
+ type = BizTxnType.iid('payment_txn')
12
+ type.destroy unless type.nil?
13
+ end
14
+
15
+ end
@@ -0,0 +1,360 @@
1
+ class BaseTxnsAndAccts < ActiveRecord::Migration
2
+ def self.up
3
+
4
+ unless table_exists?(:biz_txn_events)
5
+ create_table :biz_txn_events do |t|
6
+ t.column :description, :string
7
+ t.column :biz_txn_acct_root_id, :integer
8
+ t.column :biz_txn_type_id, :integer
9
+ t.column :entered_date, :datetime
10
+ t.column :post_date, :datetime
11
+ t.column :biz_txn_record_id, :integer
12
+ t.column :biz_txn_record_type, :string
13
+ t.column :external_identifier, :string
14
+ t.column :external_id_source, :string
15
+ t.timestamps
16
+ end
17
+
18
+ add_index :biz_txn_events, :biz_txn_acct_root_id
19
+ add_index :biz_txn_events, :biz_txn_type_id
20
+ add_index :biz_txn_events, [:biz_txn_record_id, :biz_txn_record_type], :name => "btai_1"
21
+ end
22
+
23
+ unless table_exists?(:biz_txn_event_descs)
24
+ create_table :biz_txn_event_descs do |t|
25
+ t.column :biz_txn_event_id, :integer
26
+ t.column :language_id, :integer
27
+ t.column :locale_id, :integer
28
+ t.column :priority, :integer
29
+ t.column :sequence, :integer
30
+ t.column :short_description, :string
31
+ t.column :long_description, :string
32
+ t.timestamps
33
+ end
34
+
35
+ add_index :biz_txn_event_descs, :biz_txn_event_id
36
+ add_index :biz_txn_event_descs, :language_id
37
+ add_index :biz_txn_event_descs, :locale_id
38
+ end
39
+
40
+ unless table_exists?(:biz_txn_types)
41
+ create_table :biz_txn_types do |t|
42
+ t.column :parent_id, :integer
43
+ t.column :lft, :integer
44
+ t.column :rgt, :integer
45
+
46
+ #custom columns go here
47
+ t.column :description, :string
48
+ t.column :comments, :string
49
+ t.column :internal_identifier, :string
50
+ t.column :external_identifier, :string
51
+ t.column :external_id_source, :string
52
+ t.timestamps
53
+ end
54
+
55
+ add_index :biz_txn_types, [:parent_id,:lft,:rgt], :name => 'biz_txn_type_nested_set_idx'
56
+ end
57
+
58
+ unless table_exists?(:biz_txn_relationships)
59
+ create_table :biz_txn_relationships do |t|
60
+ t.column :biz_txn_rel_type_id, :integer
61
+ t.column :description, :string
62
+ t.column :txn_event_id_from, :integer
63
+ t.column :txn_event_id_to, :integer
64
+ t.column :status_type_id, :integer
65
+ t.column :from_date, :date
66
+ t.column :thru_date, :date
67
+ t.timestamps
68
+ end
69
+
70
+ add_index :biz_txn_relationships, :biz_txn_rel_type_id
71
+ add_index :biz_txn_relationships, :status_type_id
72
+ end
73
+
74
+ unless table_exists?(:biz_txn_rel_types)
75
+ create_table :biz_txn_rel_types do |t|
76
+ t.column :parent_id, :integer
77
+ t.column :lft, :integer
78
+ t.column :rgt, :integer
79
+ #custom columns go here
80
+ t.column :description, :string
81
+ t.column :comments, :string
82
+ t.column :internal_identifier, :string
83
+ t.column :external_identifier, :string
84
+ t.column :external_id_source, :string
85
+ t.timestamps
86
+ end
87
+
88
+ add_index :biz_txn_rel_types, :parent_id
89
+ end
90
+
91
+ unless table_exists?(:biz_txn_statuses)
92
+ create_table :biz_txn_statuses do |t|
93
+ t.column :description, :string
94
+ t.column :comments, :string
95
+ t.timestamps
96
+ end
97
+ end
98
+
99
+ unless table_exists?(:biz_txn_tasks)
100
+ create_table :biz_txn_tasks do |t|
101
+ t.column :description, :string
102
+ t.timestamps
103
+ end
104
+ end
105
+
106
+ unless table_exists?(:biz_txn_task_types)
107
+ create_table :biz_txn_task_types do |t|
108
+ t.column :parent_id, :integer
109
+ t.column :lft, :integer
110
+ t.column :rgt, :integer
111
+ #custom columns go here
112
+ t.column :description, :string
113
+ t.column :comments, :string
114
+ t.timestamps
115
+ end
116
+
117
+ add_index :biz_txn_task_types, :parent_id
118
+ end
119
+
120
+ unless table_exists?(:biz_txn_party_roles)
121
+ create_table :biz_txn_party_roles do |t|
122
+ t.column :biz_txn_event_id, :integer
123
+ t.column :party_id, :integer
124
+ t.column :biz_txn_party_role_type_id, :integer
125
+ t.timestamps
126
+ end
127
+
128
+ add_index :biz_txn_party_roles, :biz_txn_event_id
129
+ add_index :biz_txn_party_roles, :party_id
130
+ add_index :biz_txn_party_roles, :biz_txn_party_role_type_id
131
+ end
132
+
133
+ unless table_exists?(:biz_txn_party_role_types)
134
+ create_table :biz_txn_party_role_types do |t|
135
+ t.column :parent_id, :integer
136
+ t.column :lft, :integer
137
+ t.column :rgt, :integer
138
+ #custom columns go here
139
+ t.column :description, :string
140
+ t.column :comments, :string
141
+ t.column :internal_identifier, :string
142
+ t.timestamps
143
+ end
144
+
145
+ add_index :biz_txn_party_role_types, :parent_id
146
+ end
147
+
148
+ unless table_exists?(:biz_txn_acct_roots)
149
+ create_table :biz_txn_acct_roots do |t|
150
+ t.column :description, :string
151
+ t.column :status, :integer
152
+ t.column :biz_txn_acct_id, :integer
153
+ t.column :biz_txn_acct_type, :string
154
+ t.column :external_identifier, :string
155
+ t.column :external_id_source, :string
156
+ t.timestamps
157
+ end
158
+
159
+ add_index :biz_txn_acct_roots, [:biz_txn_acct_id, :biz_txn_acct_type], :name => "btai_2"
160
+ end
161
+
162
+ unless table_exists?(:biz_txn_acct_status_types)
163
+ create_table :biz_txn_acct_status_types do |t|
164
+ t.timestamps
165
+ end
166
+ end
167
+
168
+ unless table_exists?(:biz_txn_acct_types)
169
+ create_table :biz_txn_acct_types do |t|
170
+ t.column :parent_id, :integer
171
+ t.column :lft, :integer
172
+ t.column :rgt, :integer
173
+ #custom columns go here
174
+ t.column :description, :string
175
+ t.column :comments, :string
176
+ t.column :internal_identifier, :string
177
+ t.column :external_identifier, :string
178
+ t.column :external_id_source, :string
179
+ t.timestamps
180
+ end
181
+
182
+ add_index :biz_txn_acct_types, :parent_id
183
+ end
184
+
185
+ unless table_exists?(:biz_txn_acct_statuses)
186
+ create_table :biz_txn_acct_statuses do |t|
187
+ t.timestamps
188
+ end
189
+ end
190
+
191
+ unless table_exists?(:biz_txn_acct_rel_types)
192
+ create_table :biz_txn_acct_rel_types do |t|
193
+ t.column :parent_id, :integer
194
+ t.column :lft, :integer
195
+ t.column :rgt, :integer
196
+ #custom columns go here
197
+ t.column :description, :string
198
+ t.column :comments, :string
199
+ t.column :internal_identifier, :string
200
+ t.column :external_identifier, :string
201
+ t.column :external_id_source, :string
202
+ t.timestamps
203
+ end
204
+
205
+ add_index :biz_txn_acct_rel_types, :parent_id
206
+ end
207
+
208
+ unless table_exists?(:biz_txn_acct_relationships)
209
+ create_table :biz_txn_acct_relationships do |t|
210
+ t.column :biz_txn_acct_rel_type_id, :integer
211
+ t.column :description, :string
212
+ t.column :biz_txn_acct_root_id_from, :integer
213
+ t.column :biz_txn_acct_root_id_to, :integer
214
+ t.column :status_type_id, :integer
215
+ t.column :from_date, :date
216
+ t.column :thru_date, :date
217
+ t.timestamps
218
+ end
219
+
220
+ add_index :biz_txn_acct_relationships, :biz_txn_acct_rel_type_id
221
+ add_index :biz_txn_acct_relationships, :status_type_id
222
+
223
+ end
224
+
225
+ unless table_exists?(:biz_txn_acct_party_roles)
226
+ create_table :biz_txn_acct_party_roles do |t|
227
+ t.column :description, :string
228
+ t.column :biz_txn_acct_root_id, :integer
229
+ t.column :party_id, :integer
230
+ t.column :biz_txn_acct_pty_rtype_id, :integer
231
+ t.column :is_default_billing_acct_flag, :integer
232
+ t.timestamps
233
+ end
234
+
235
+ add_index :biz_txn_acct_party_roles, :biz_txn_acct_root_id
236
+ add_index :biz_txn_acct_party_roles, :party_id
237
+ add_index :biz_txn_acct_party_roles, :biz_txn_acct_pty_rtype_id
238
+ end
239
+
240
+ unless table_exists?(:biz_txn_acct_pty_rtypes)
241
+ create_table :biz_txn_acct_pty_rtypes do |t|
242
+ t.column :parent_id, :integer
243
+ t.column :lft, :integer
244
+ t.column :rgt, :integer
245
+ #custom columns go here
246
+ t.column :description, :string
247
+ t.column :comments, :string
248
+ t.column :internal_identifier, :string
249
+ t.column :external_identifier, :string
250
+ t.column :external_id_source, :string
251
+ t.timestamps
252
+ end
253
+
254
+ add_index :biz_txn_acct_pty_rtypes, :parent_id
255
+ end
256
+
257
+ unless table_exists?(:biz_acct_txn_tasks)
258
+ create_table :biz_acct_txn_tasks do |t|
259
+ t.column :biz_txn_task_id, :integer
260
+ t.column :biz_txn_account_id, :integer
261
+ t.column :description, :string
262
+ t.column :comments, :string
263
+ t.column :entered_date, :datetime
264
+ t.column :requested_date, :datetime
265
+ t.timestamps
266
+ end
267
+
268
+ add_index :biz_acct_txn_tasks, :biz_txn_task_id
269
+ add_index :biz_acct_txn_tasks, :biz_txn_account_id
270
+ end
271
+
272
+ unless table_exists?(:biz_txn_agreement_role_types)
273
+ create_table :biz_txn_agreement_role_types do |t|
274
+ t.column :parent_id, :integer
275
+ t.column :lft, :integer
276
+ t.column :rgt, :integer
277
+ #custom columns go here
278
+ t.column :description, :string
279
+ t.column :comments, :string
280
+ t.column :internal_identifier, :string
281
+ t.timestamps
282
+ end
283
+
284
+ add_index :biz_txn_agreement_role_types, :parent_id
285
+ end
286
+
287
+ unless table_exists?(:biz_txn_agreement_roles)
288
+ create_table :biz_txn_agreement_roles do |t|
289
+ t.references :biz_txn_event, :polymorphic => true
290
+ t.column :agreement_id, :integer
291
+ t.column :biz_txn_agreement_role_type_id, :integer
292
+ t.timestamps
293
+ end
294
+
295
+ add_index :biz_txn_agreement_roles, :agreement_id
296
+ add_index :biz_txn_agreement_roles, :biz_txn_agreement_role_type_id
297
+ end
298
+
299
+ unless table_exists?(:financial_txns)
300
+ create_table :financial_txns do |t|
301
+ t.integer :money_id, :integer
302
+
303
+ t.timestamps
304
+ end
305
+ end
306
+
307
+ unless table_exists?(:financial_txn_assns)
308
+ create_table :financial_txn_assns do |t|
309
+ t.references :financial_txn
310
+ t.references :financial_txn_record, :polymorphic => true
311
+
312
+ t.timestamps
313
+ end
314
+ end
315
+
316
+ unless table_exists?(:financial_txn_accounts)
317
+ create_table :financial_txn_accounts do |t|
318
+ t.column :description, :string
319
+ t.column :account_number, :string
320
+ t.column :agreement_id, :integer
321
+ t.column :balance_id, :integer
322
+ t.column :payment_due_id, :integer
323
+ t.column :due_date, :datetime
324
+
325
+ #polymorphic tables
326
+ t.references :financial_account, :polymorphic => true
327
+
328
+ t.timestamps
329
+ end
330
+ end
331
+
332
+ unless table_exists?(:base_txn_contexts)
333
+ create_table :base_txn_contexts do |t|
334
+ t.references :biz_txn_event
335
+ t.references :txn_context_record, :polymorphic => true
336
+
337
+ t.timestamps
338
+ end
339
+
340
+ add_index :base_txn_contexts, [:txn_context_record_id, :txn_context_record_type], :name => 'txn_context_record_idx'
341
+ end
342
+
343
+ end
344
+
345
+ def self.down
346
+ [
347
+ :biz_txn_agreement_roles, :biz_txn_agreement_role_types, :biz_acct_txn_tasks,
348
+ :biz_txn_acct_pty_rtypes, :biz_txn_acct_party_roles, :biz_txn_acct_relationships,
349
+ :biz_txn_acct_rel_types, :biz_txn_acct_statuses, :biz_txn_acct_types,
350
+ :biz_txn_acct_status_types, :biz_txn_acct_roots, :biz_txn_party_role_types,
351
+ :biz_txn_party_roles, :biz_txn_task_types, :biz_txn_tasks,
352
+ :biz_txn_statuses, :biz_txn_rel_types, :biz_txn_relationships,:base_txn_contexts,
353
+ :biz_txn_types, :biz_txn_event_descs, :biz_txn_events,:financial_txn_accounts,:financial_txns
354
+ ].each do |tbl|
355
+ if table_exists?(tbl)
356
+ drop_table tbl
357
+ end
358
+ end
359
+ end
360
+ end