has_accounts 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,163 @@
1
+ class BookingTemplate < ActiveRecord::Base
2
+ # Access restrictions
3
+ attr_accessible :title, :code, :amount, :amount_relates_to, :comments
4
+
5
+ # Associations
6
+ belongs_to :debit_account, :foreign_key => 'debit_account_id', :class_name => "Account"
7
+ attr_accessible :debit_account_id
8
+ belongs_to :credit_account, :foreign_key => 'credit_account_id', :class_name => "Account"
9
+ attr_accessible :credit_account_id
10
+
11
+ # Default ordering
12
+ default_scope order(:code)
13
+
14
+ # Scopes
15
+ scope :by_type, lambda{|value| where("code LIKE ?", value + ':%')}
16
+
17
+ # Standard methods
18
+ include ApplicationHelper
19
+ def to_s(format = :default)
20
+ case format
21
+ when :short
22
+ "%s / %s %s" % [
23
+ credit_account ? credit_account.to_s(:short) : '?',
24
+ debit_account ? debit_account.to_s(:short) : '?',
25
+ amount ? "%0.2f" % amount.to_f : '?',
26
+ ]
27
+ when :long
28
+ "%s an %s %s, %s (%s)" % [
29
+ credit_account ? credit_account.to_s : '?',
30
+ debit_account ? debit_account.to_s : '?',
31
+ amount ? "%0.2f" % amount.to_f : '?',
32
+ title.present? ? title : '?',
33
+ comments.present? ? comments : '?'
34
+ ]
35
+ else
36
+ title
37
+ end
38
+ end
39
+
40
+ def amount_to_s
41
+ if amount_relates_to.present?
42
+ return "%.2f%%" % (amount.to_f * 100)
43
+ else
44
+ return currency_fmt(amount)
45
+ end
46
+ end
47
+
48
+ # Tagging
49
+ # =======
50
+ acts_as_taggable
51
+ attr_accessible :tag_list
52
+
53
+ def booking_parameters(params = {})
54
+ params = HashWithIndifferentAccess.new(params)
55
+
56
+ # Prepare parameters set by template
57
+ booking_params = attributes.reject{|key, value| !["title", "comments", "credit_account_id", "debit_account_id"].include?(key)}
58
+
59
+ # Calculate amount
60
+ booking_amount = BigDecimal.new(self.amount.to_s || '0')
61
+
62
+ # Lookup reference
63
+ reference = params['reference']
64
+ unless reference
65
+ ref_type = params['reference_type']
66
+ ref_id = params['reference_id']
67
+ if ref_type.present? and ref_id.present?
68
+ reference = ref_type.constantize.find(ref_id)
69
+ end
70
+ end
71
+
72
+ person_id = params.delete(:person_id)
73
+
74
+ if reference
75
+ # Calculate amount
76
+ booking_amount = amount(reference.value_date, :person_id => person_id) if person_id
77
+
78
+ case self.amount_relates_to
79
+ when 'reference_amount'
80
+ booking_amount *= reference.amount unless reference.amount.nil?
81
+ when 'reference_balance'
82
+ booking_amount *= reference.balance unless reference.balance.nil?
83
+ when 'reference_amount_minus_balance'
84
+ booking_amount *= reference.amount - reference.balance unless (reference.amount.nil? or reference.balance.nil?)
85
+ end
86
+ end
87
+
88
+ booking_amount = booking_amount.try(:round, 2)
89
+ booking_params['amount'] = booking_amount
90
+
91
+ # Override by passed in parameters
92
+ HashWithIndifferentAccess.new(booking_params.merge!(params))
93
+ end
94
+
95
+ # Factory methods
96
+ def build_booking(params = {})
97
+ Booking.new(booking_parameters(params))
98
+ end
99
+
100
+ def create_booking(params = {})
101
+ Booking.create(booking_parameters(params))
102
+ end
103
+
104
+ def self.build_booking(code, params = {})
105
+ find_by_code(code).try(:build_booking, params)
106
+ end
107
+
108
+ def self.create_booking(code, params = {})
109
+ find_by_code(code).try(:create_booking, params)
110
+ end
111
+
112
+ # LineItems
113
+ has_many :line_items
114
+
115
+ def build_line_item
116
+ if self.amount.match(/%/) or self.amount_relates_to.blank?
117
+ line_item_class = LineItem
118
+ else
119
+ line_item_class = SaldoLineItem
120
+ end
121
+
122
+ line_item = line_item_class.new(
123
+ :booking_template => self,
124
+ :title => self.title,
125
+ :code => self.code,
126
+ :credit_account => self.credit_account,
127
+ :debit_account => self.debit_account,
128
+ :position => self.position,
129
+ :include_in_saldo_list => self.include_in_saldo_list,
130
+ :reference_code => self.amount_relates_to
131
+ )
132
+
133
+ if self.amount.match(/%/)
134
+ line_item.quantity = '%'
135
+ line_item.times = self.amount.delete('%')
136
+ # TODO: hack
137
+ line_item.price = line_item.price
138
+ elsif self.amount_relates_to.present?
139
+ line_item.quantity = 'saldo_of'
140
+ # TODO: hack
141
+ line_item.price = line_item.price
142
+ else
143
+ line_item.quantity = 'x'
144
+ line_item.times = 1
145
+ line_item.price = self.amount
146
+ end
147
+
148
+ line_item
149
+ end
150
+
151
+ # Tagging
152
+ acts_as_taggable_on :include_in_saldo
153
+
154
+ # Importer
155
+ def self.import(struct)
156
+ templates = self.all.inject([]) do |found, template|
157
+ puts "matcher: " + template.matcher
158
+ puts 'text: ' + struct.text
159
+ found << template if not Regexp.new(template.matcher).match(struct.text).eql?nil
160
+ end
161
+ puts templates.inspect
162
+ end
163
+ end
@@ -0,0 +1,22 @@
1
+ class CreateBookingTemplates < ActiveRecord::Migration
2
+ def change
3
+ create_table :booking_templates do |t|
4
+ t.string "title"
5
+ t.string "amount"
6
+ t.integer "credit_account_id"
7
+ t.integer "debit_account_id"
8
+ t.text "comments"
9
+ t.datetime "created_at", :null => false
10
+ t.datetime "updated_at", :null => false
11
+ t.string "code"
12
+ t.string "matcher"
13
+ t.string "amount_relates_to"
14
+ t.string "type"
15
+ t.string "charge_rate_code"
16
+ t.string "salary_declaration_code"
17
+ t.integer "position"
18
+
19
+ t.timestamps
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module HasAccounts
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_accounts
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-19 00:00:00.000000000 Z
12
+ date: 2013-07-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -107,12 +107,14 @@ files:
107
107
  - app/models/bank.rb
108
108
  - app/models/bank_account.rb
109
109
  - app/models/booking.rb
110
+ - app/models/booking_template.rb
110
111
  - config/locales/de.yml
111
112
  - config/locales/en.yml
112
113
  - db/migrate/20111108000000_create_has_accounts_tables.rb
113
114
  - db/migrate/20111109093857_use_string_for_account_number.rb
114
115
  - db/migrate/20111230121259_bank_now_inherits_from_person.rb
115
116
  - db/migrate/20111230222702_add_template_to_bookings.rb
117
+ - db/migrate/20130707121400_create_booking_templates.rb
116
118
  - lib/has_accounts.rb
117
119
  - lib/has_accounts/class_methods.rb
118
120
  - lib/has_accounts/core_ext/rounding.rb