-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjav.diff
363 lines (334 loc) · 10.3 KB
/
jav.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
From 332dd37c4fe49ec64341492bf4c377714b30c87f Mon Sep 17 00:00:00 2001
From: Jbarcena<[email protected]>
Date: Wed, 4 Mar 2009 14:17:51 +1100
Subject: [PATCH 1/3] Added code to model - and some seed data
---
app/models/account.rb | 78 ++++++++++++++++++--------
config/database.yml | 8 ---
db/migrate/20090219134958_create_accounts.rb | 5 ++
db/migrate/20090304020429_seed_accounts.rb | 63 +++++++++++++++++++++
db/schema.rb | 5 ++
5 files changed, 127 insertions(+), 32 deletions(-)
create mode 100644 db/migrate/20090304020429_seed_accounts.rb
diff --git a/app/models/account.rb b/app/models/account.rb
index eda5e6d..6012286 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -1,58 +1,88 @@
class Account < ActiveRecord::Base
+ belongs_to :parent, :class_name => 'Account'
+ has_many :children, :class_name => 'Account', :foreign_key => :parent_id, :dependent => :destroy, :order => :code
+
+ named_scope :tops, :conditions => 'parent_id IS NULL', :order => :code
+
has_many :unit_transactions
validates_presence_of :name
- validates_uniqueness_of :name
-
- cattr_reader :per_page
- @@per_page = 2
+ validates_uniqueness_of :name
+
+ validates_uniqueness_of :code
+ before_save :check_code
+ @@padding = { 1 => nil, 2 => nil, 3 => nil, 4 => nil, 5 => 2, 6 => 4 }
+
+ cattr_reader :per_page
+ @@per_page = 2
def history(since = 7.days.ago)
UnitTransaction.find(:all,
- :conditions => ['from_id = ? OR to_id = ? AND created_at > ?', self.id, self.id, since],
- :order => 'created_at DESC')
+ :conditions => ['from_id = ? OR to_id = ? AND created_at > ?', self.id, self.id, since],
+ :order => 'created_at DESC')
end
-
-
-
- ##Calculos
-
+
+ ##Calculos
+
def ingresos
-
end
-
+
def egresos
-
end
-
-
+
def base
@base ||= unit_transactions.inject(0) {|total, il| total += il.total}
@base = 0.0 if @base.nil?
@base
end
-
-
+
+
# total_crub
#
def total_crub
base * (crub / 100.0)
end
-
+
# nqn
#
def total_nqn
base * (nqn / 100.0)
end
-
+
# total
#
def total
base - total_crub + total_nqn
end
-
-
-
-
+
+ def level
+ self.code.present? ? self.code.count('.') + 1 : self.parent.code.count('.') + 2
+ end
+
+ def pad(s)
+ if @@padding[self.level].nil?
+ s.to_s
+ else
+ sprintf( "%0#{@@padding[self.level]}d", s)
+ end
+ end
+
+ def parent_prefix
+ self.parent.present? ? self.parent.code + '.' : ''
+ end
+
+ def check_code # make sure we have a sensible code, and if not create one
+ # a code is sensible if it contains our parent's code plus a dot and a
+ # string
+ if self.code.blank? || self.code !~ /^#{self.parent_prefix}\d+$/
+ self.create_code
+ end
+ end
+
+ def create_code
+ _coll = self.parent.present? ? self.parent.children : self.class.tops
+ _code = self.pad( _coll.empty? ? 1 : _coll[-1].code.sub(/.*\./,'').to_i + 1 )
+ self.code = self.parent_prefix + _code
+ end
end
diff --git a/config/database.yml b/config/database.yml
index 4c3f2f9..025d62a 100644
--- a/config/database.yml
+++ b/config/database.yml
@@ -6,14 +6,6 @@ development:
pool: 5
timeout: 5000
-development:
- adapter: mysql
- database: account_development
- username: root
- password:
- socket: /var/run/mysqld/mysqld.sock
-
-
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
diff --git a/db/migrate/20090219134958_create_accounts.rb b/db/migrate/20090219134958_create_accounts.rb
index e9550b4..4eea59c 100644
--- a/db/migrate/20090219134958_create_accounts.rb
+++ b/db/migrate/20090219134958_create_accounts.rb
@@ -3,9 +3,14 @@ class CreateAccounts < ActiveRecord::Migration
create_table :accounts do |t|
t.string :name
t.decimal :balance, :precision => 8, :scale => 2, :default => 0
+ t.integer :parent_id
+ t.string :code, :null => false
t.timestamps
end
+
+ add_index :accounts, :code, :unique => true
+ add_index :accounts, :parent_id
end
def self.down
diff --git a/db/migrate/20090304020429_seed_accounts.rb b/db/migrate/20090304020429_seed_accounts.rb
new file mode 100644
index 0000000..f4de654
--- /dev/null
+++ b/db/migrate/20090304020429_seed_accounts.rb
@@ -0,0 +1,63 @@
+class SeedAccounts < ActiveRecord::Migration
+ def self.up
+ [
+ { :code => '1', :name => 'ACTIVO' },
+ { :code => '2', :name => 'PASIVO' },
+ { :code => '3', :name => 'PATRIM. NETO' },
+ { :code => '4', :name => 'RESULTADOS' },
+ { :code => '5', :name => 'CTAS. DE ORDEN' }
+ ].each {|r|
+ Account.create(r)
+ }
+
+ p1 = Account.find_by_code('1')
+ [
+ { :name => 'ACTIVO CORRIENTE' },
+ { :name => 'ACTIVO NO CORRIENTE' },
+ ].each {|r|
+ p1.children.create(r)
+ }
+
+ p2 = Account.find_by_code('2')
+ [
+ { :name => 'PASIVO CORRIENTE' },
+ { :name => 'PASIVO NO CORRIENTE' }
+ ].each {|r|
+ p2.children.create(r)
+ }
+
+ p11 = Account.find_by_code('1.1')
+ [
+ { :name => 'CAJA Y BANCOS' },
+ { :name => 'INVERSIONES CORRIENTES' },
+ { :name => 'CREDITOS' },
+ { :name => 'BIENES DE CAMBIO' }
+ ].each {|r|
+ p11.children.create(r)
+ }
+
+ p111 = Account.find_by_code('1.1.1')
+ [
+ { :name => 'CAJA' },
+ { :name => 'BANCOS EN MONEDA NACIONAL' },
+ { :name => 'BANCOS EN MONEDA EXTRANJERA' }
+ ].each {|r|
+ p111.children.create(r)
+ }
+
+ p1111 = Account.find_by_code('1.1.1.1')
+ [
+ { :name => 'CAJA EN MONEDA NACIONAL' },
+ { :name => 'FONDO FIJO' },
+ { :name => 'CAJA EN MONEDA EXTRANJERA' }
+ ].each {|r|
+ p1111.children.create(r)
+ }
+
+ end
+
+ def self.down
+ Account.delete_all
+ end
+end
+
diff --git a/db/schema.rb b/db/schema.rb
index 890083d..ad2419f 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -14,10 +14,15 @@ ActiveRecord::Schema.define(:version => 20090226110242) do
create_table "accounts", :force => true do |t|
t.string "name"
t.decimal "balance", :precision => 8, :scale => 2, :default => 0.0
+ t.integer "parent_id"
+ t.string "code", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
+ add_index "accounts", ["code"], :name => "index_accounts_on_code", :unique => true
+ add_index "accounts", ["parent_id"], :name => "index_accounts_on_parent_id"
+
create_table "departamentos", :force => true do |t|
t.string "nombre"
t.datetime "created_at"
--
1.6.1
From 8f2a3153e3444c116ddf8e4423241ed5f8f5f330 Mon Sep 17 00:00:00 2001
From: Jason King <[email protected]>
Date: Wed, 4 Mar 2009 14:27:27 +1100
Subject: [PATCH 2/3] Adding the code (and order) into the view
---
app/controllers/accounts_controller.rb | 7 +------
app/views/accounts/index.html.erb | 2 +-
2 files changed, 2 insertions(+), 7 deletions(-)
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
index e2414d2..27e1e97 100644
--- a/app/controllers/accounts_controller.rb
+++ b/app/controllers/accounts_controller.rb
@@ -18,12 +18,7 @@ class AccountsController < ApplicationController
end
def index
-
- @accounts = Account.find(:all)
- #@accounts = Account.paginate(:order => 'created_at DESC',
-#:per_page => 20,
-#:page => params[:page])
-
+ @accounts = Account.all( :order => :code )
end
def list
diff --git a/app/views/accounts/index.html.erb b/app/views/accounts/index.html.erb
index 5a9370d..ebb4bd3 100644
--- a/app/views/accounts/index.html.erb
+++ b/app/views/accounts/index.html.erb
@@ -21,7 +21,7 @@
<% for account in @accounts %>
<tr class="<%= cycle('even', 'odd') %>">
- <td><%= link_to account.name, account_path(account) %></td>
+ <td><%= link_to "#{account.code} #{account.name}", account_path(account) %></td>
<td><span class="green"><%= account.balance %>$</span></td>
<td> <%= link_to 'Destroy', account_path(account), :confirm => 'Are you sure?', :method => :delete %></td>
--
1.6.1
From 7469f7e3bd45d3a7a4f8ef75fb91cc74e6367bfb Mon Sep 17 00:00:00 2001
From: Jason King <[email protected]>
Date: Wed, 4 Mar 2009 14:33:41 +1100
Subject: [PATCH 3/3] Adding parents to new accounts
---
app/controllers/accounts_controller.rb | 2 +-
app/models/account.rb | 3 ++-
app/views/accounts/new.html.erb | 4 ++++
3 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
index 27e1e97..f76445f 100644
--- a/app/controllers/accounts_controller.rb
+++ b/app/controllers/accounts_controller.rb
@@ -33,7 +33,7 @@ class AccountsController < ApplicationController
def new
- @account = Account.new
+ @account = Account.new
end
diff --git a/app/models/account.rb b/app/models/account.rb
index 6012286..88c8912 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -57,7 +57,8 @@ class Account < ActiveRecord::Base
end
def level
- self.code.present? ? self.code.count('.') + 1 : self.parent.code.count('.') + 2
+ return 1 unless self.parent
+ self.code.present? ? self.code.count('.') + 1 : self.parent.level + 1
end
def pad(s)
diff --git a/app/views/accounts/new.html.erb b/app/views/accounts/new.html.erb
index dc5fc9e..37b00a5 100644
--- a/app/views/accounts/new.html.erb
+++ b/app/views/accounts/new.html.erb
@@ -5,6 +5,10 @@
<%= f.error_messages %>
<p>
+ <%= f.label :parent_id %><br />
+ <%= f.collection_select :parent_id, Account.all(:order => :code), :id, :code, :include_blank => true %>
+ </p>
+ <p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
--
1.6.1