Skip to content

Commit

Permalink
Bug found and fixed in report5 when closing matched operations, added…
Browse files Browse the repository at this point in the history
… closed by tag
  • Loading branch information
Marwen Blel committed Dec 28, 2011
1 parent ac9bc1c commit e256501
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 46 deletions.
71 changes: 56 additions & 15 deletions app/controllers/payment_runs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ def edit

#print "#{op.amount} #{op2.amount} #{op.ledger.name} #{op2.ledger.name}\n"
j2 = op2.journal

kid_match = ((j.kid != nil and j2.kid !=nil) and (j.kid == j2.kid))
bill_match = ((j.bill_number != nil and j2.bill_number != nil) and (j.bill_number == j2.bill_number))
if kid_match
matched_by = "kid"
elsif bill_match
matched_by = "bill"
else
matched_by = nil
end

if (op.amount == -op2.amount) and (op.ledger == op2.ledger) and (kid_match or bill_match) then
@matches << [op, op2]
@matches << [op, op2,matched_by]
end
end

Expand All @@ -49,33 +55,47 @@ def create

ClosedOperation.transaction do

if params['mops'] then
#when locked by matched operations using "lock all"
if params['mops']

puts "-------------------------------------"
puts "Locked by matched operations"
puts "-------------------------------------"

params['mops'].each do
|idx, ops|
cl = ClosedOperation.new
cl.save
print "\n\n\nOp thingie #{ops}\n\n\n"
ops = ops.split(',').collect! {|n| n.to_i}
cl = ClosedOperation.new
cl.save
ops.each do
|op_id|
print "\n\n\nClose #{op_id}\n\n\n"
op = JournalOperation.where({:id => op_id}).first
op = JournalOperation.find(op_id)
op.closed_operation = cl
op.save
end
end
add_tag(cl.id)
end
else
puts "-------------------------------------"
puts "Locked manually"
puts "-------------------------------------"

print "WEEE!!!! #{params['ops']}\n\n"
#when locked manually
cl = ClosedOperation.new
cl.save
cl.save
if params['ops'].is_a?(String)
params['ops'] = params['ops'].split(',').collect! {|n| n.to_i}
end
params['ops'].each do
|op_id|
print "Close #{op_id}\n\n"
op = JournalOperation.where({:id => op_id}).first
|op_id|
puts op_id
op = JournalOperation.find(op_id)
op.closed_operation = cl
op.save
end
end
add_tag(cl.id)
end

# redirect_to("/payment_runs/#{params['pay_id']}/edit")
redirect_to :action => :edit, :id => params['pay_id']
end
Expand All @@ -87,4 +107,25 @@ def update
def destroy
end

private
def add_tag(clid)
#investigate if it is matched by kid or invoice
jos = JournalOperation.where(:closed_operation_id => clid)
count = jos.count
kid = jos.first.journal.kid
bill = jos.first.journal.bill_number

count_same_kid = Journal.joins(:journal_operations).where(:journal_operations => {:closed_operation_id => clid}).where("kid is not null").where(:journals=> {:kid => kid}).count
count_same_bill = Journal.joins(:journal_operations).where(:journal_operations => {:closed_operation_id => clid}).where("bill_number is not null").where(:journals=> {:bill_number => bill}).count

if count == count_same_kid
cop = ClosedOperation.find(clid)
cop.matched_by = "k"
cop.save
elsif count == count_same_bill
cop = ClosedOperation.find(clid)
cop.matched_by = "b"
cop.save
end
end
end
1 change: 1 addition & 0 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def dagbok
@project = ( project == 'null' ? nil : Project.find(project))
@car = ( car == 'null' ? nil : Car.find(car))
@journal_type = ( journal_type == 'null' ? nil : JournalType.find(journal_type))
puts @journal_operations.count
end

private
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/reports_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ def car(id)
jo.car.to_s unless jo.car_id.blank?
end

def closed_by(closed_op_id)
"closed by " + JournalOperation.where(:closed_operation_id => closed_op_id).first.closed_operation.matched_by.to_s
#t(:closed_status, :scope => :reports)
end

end
36 changes: 23 additions & 13 deletions app/views/payment_runs/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

<div><h2><%= t(:automatic_matching, :scope => :reports) %></h2></div>

<form method='post' action='/payment_runs/create'>
<button type='submit'>Lukk alle</button>
<input type='hidden' name='pay_id' value='<%= @pay_id %>'>
Expand All @@ -6,14 +9,12 @@
@matches.each_with_index do
|ops, idx|
%>
<input type='hidden' name='mops[<%= idx %>][0]' value='<%=ops[0].id %>'>
<input type='hidden' name='mops[<%= idx %>][1]' value='<%=ops[1].id %>'>
<input type='hidden' name='mops[<%= idx %>]' value='<%=ops[0].id%>,<%=ops[1].id%>'>
<%
end
%>
</form>


<table class='repport5'>

<%
Expand All @@ -35,7 +36,7 @@
ledger = ops[0].ledger
%>
<tr>
<th colspan='12'>
<th colspan='13'>
<%=ledger.number.to_s + " " + ledger.name.to_s %>
</th>
</tr>
Expand Down Expand Up @@ -66,7 +67,9 @@
Tekst
</th>
<th>
<%= t(:matched_by, :scope => :reports) %>
</th>
<th></th>
</tr>

<%
Expand All @@ -81,7 +84,7 @@
<% jrnl = Journal.find(ops[0].journal_id)%>

<td> <%= jrnl.journal_type.to_s %> </td>
<td> <%= jrnl.number %> </td>
<td> <%= link_to jrnl.number, edit_journal_path(jrnl) %> </td>
<td> <%= jrnl.period.to_s %> </td>
<td> <%= jrnl.journal_date.to_s %> </td>

Expand Down Expand Up @@ -109,10 +112,12 @@
<td>
<%= ops[0].journal.description %>
</td>
<td>
<%= ops[2] %>
</td>
<td rowspan='2'>
<form method='post' action='/payment_runs/create'>
<input type='hidden' name='ops[0]' value='<%=ops[0].id %>'>
<input type='hidden' name='ops[1]' value='<%=ops[1].id %>'>
<input type='hidden' name='ops' value=<%=ops[0].id%>,<%=ops[1].id%>>
<input type='hidden' name='pay_id' value='<%= @pay_id %>'>
<button type='submit'>Lukk</button>
</form>
Expand All @@ -123,7 +128,7 @@
<% jrnl = Journal.find(ops[1].journal_id)%>

<td> <%= jrnl.journal_type.to_s %> </td>
<td> <%= jrnl.number %> </td>
<td> <%= link_to jrnl.number, edit_journal_path(jrnl)%> </td>
<td> <%= jrnl.period.to_s %> </td>
<td> <%= jrnl.journal_date.to_s %> </td>
<td class='number'>
Expand All @@ -150,12 +155,16 @@
<td>
<%= ops[1].journal.description %>
</td>
<td></td>
</tr>

<% end %>

</table>


<div><h2><%= t(:manual_matching, :scope => :reports) %></h2></div>

<script>
paymentRuns.amounts = {
<%@ops.each do |op|
Expand All @@ -173,10 +182,10 @@ end
def footer(diff)
%>
<tr>
<th><%= t(:diff, :scope => :reports )%></th>
<th colspan='9'>
<%= diff %>
</th>
<th colspan="4"><%= t(:diff, :scope => :reports )%></th>
<th><%= diff if diff > 0 %></th>
<th><%= diff if diff <= 0%></th>
<th colspan="4"></th>
<th>
<span class='amount'></span>
</th>
Expand Down Expand Up @@ -253,9 +262,10 @@ end
%>

<tr>
<% puts "*","*","*","*",op.id,"*","*" if op.journal_id.blank? %>
<% jrnl = Journal.find(op.journal_id)%>
<td> <%= jrnl.journal_type.to_s %> </td>
<td> <%= jrnl.number %> </td>
<td> <%= link_to jrnl.number, edit_journal_path(jrnl)%> </td>
<td> <%= jrnl.period.to_s %> </td>
<td> <%= jrnl.journal_date.to_s %> </td>
<td class='number'>
Expand Down
34 changes: 16 additions & 18 deletions app/views/reports/subsidiary_ledger_journal.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,14 @@
<% unless @journal_operations.blank?
account_id = nil
period_id = nil
#current = -1
#diff = 0
end%>
<% @journal_operations.each do |jo| %>
<% #current += 1 #current is index of current element on @journal_operations array%>
<% if account_id != jo["ledgid"]
account_id = jo["ledgid"]
period_id = 0
#calculate diff for each account
#diff = 0
#counter = current
#while @journal_operations[counter] and @journal_operations[counter]["ledgid"] == account_id do
#diff += @journal_operations[counter]["balance"].to_f
#counter = counter + 1
#end
%>
<tr class="section1">
<td colspan="12">
<td colspan="13">
<%= Ledger.find(jo["ledgid"]).to_s_with_number %>
</td>
</tr>
Expand Down Expand Up @@ -88,7 +78,10 @@
<%= t(:bill , :scope => :reports) %>
</td>
<td style="text-align: right">
<%= t(:diff , :scope => :reports) %>
<%= t(:diff_by_invoice , :scope => :reports) %>
</td>
<td style="text-align: right">
<%= t(:diff_by_kid, :scope => :reports) %>
</td>
<td style="text-align: right">
<%= t(:operation_status, :scope => :reports) %>
Expand All @@ -98,7 +91,7 @@
</td>
</tr>
<% end %>
<tr class=" coloredtr <%= "kid"+jo["jkid"]+" " unless jo["jkid"].blank?%><%= "invoice"+jo["jbill"]+" " unless jo["jbill"].blank? %><%= "closed"+jo["status"]+" " unless jo["status"].blank? %>">
<tr class=" coloredtr <%= "l"+jo["ledgid"]+"kid"+jo["jkid"]+" " unless jo["jkid"].blank?%><%= "l"+jo["ledgid"]+"invoice"+jo["jbill"]+" " unless jo["jbill"].blank? %><%= "l"+jo["ledgid"]+"closed"+jo["status"]+" " unless jo["status"].blank? %>">
<td><%= jtype(jo["jtype"])%></td>
<td>
<%= jo["jdate"] %>
Expand All @@ -121,23 +114,28 @@
<td class="bill" style="text-align: right">
<%= jo["jbill"] %>
</td>
<td class="diff" style="text-align: right">
<td class="diff_invoice" style="text-align: right">
<% if jo["status"].blank? %>
<%= number_with_precision(( (jo["jkid"].blank? and jo["jbill"].blank?) ? jo["balance"] : nil ), :locale =>I18n.locale) %>
<% end %>
</td>
<td class="diff_kid" style="text-align: right">
<% if jo["status"].blank? %>
<%= number_with_precision(( (jo["jkid"].blank? and jo["jbill"].blank?) ? jo["balance"] : nil ), :locale =>I18n.locale) %>
<% end %>
</td>
<% unless jo["status"].blank? %>
<td class="status">
<%= t(:closed_status, :scope => :reports) %>
<%= closed_by(jo["status"]) %>
</td>
<td> <%= button_to_function t(:show_closed, :scope => :reports),"highlight('closed','.closed#{jo["status"]}')" unless jo["status"].blank?%> </td>
<td> <%= button_to_function t(:show_closed, :scope => :reports),"highlight('closed','.l#{jo["ledgid"]}closed#{jo["status"]}')" unless jo["status"].blank?%> </td>
<td> <%= button_to(t(:open, :scope => :reports), open_closed_operations_reports_path(:closed_op_id => jo["status"],:redirection => @stored_params)) %> </td>
<% else %>
<td class="status">
<%= t(:open_status, :scope => :reports) %>
</td>
<td> <%= button_to_function t(:show_same_kid, :scope => :reports),"highlight('kid','.kid#{jo["jkid"]}');" unless jo["jkid"].blank? %> </td>
<td> <%= button_to_function t(:show_same_invoice, :scope => :reports),"highlight('invoice','.invoice#{jo["jbill"]}');" unless jo["jbill"].blank? %> </td>
<td> <%= button_to_function t(:show_same_kid, :scope => :reports),"highlight('kid','.l#{jo["ledgid"]}kid#{jo["jkid"]}');" unless jo["jkid"].blank? %> </td>
<td> <%= button_to_function t(:show_same_invoice, :scope => :reports),"highlight('invoice','.l#{jo["ledgid"]}invoice#{jo["jbill"]}');" unless jo["jbill"].blank? %> </td>
<% end %>
</tr>
<%end%>
Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ en:
accounting: Accounting
budget: Budget
diff: Diff
diff_by_kid: KID Diff
diff_by_invoice: Invoice Diff
result: Result
revenue: Revenue
expenses: Expenses
Expand Down Expand Up @@ -511,6 +513,9 @@ en:
open_status: Open
show_same_kid: Show Same KID
show_same_invoice: Show Same Invoice
matched_by: Matched By
automatic_matching: Automatic Matching
manual_matching: Manual Matching


projects:
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20111225135445_add_matched_by_to_closed_operations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddMatchedByToClosedOperations < ActiveRecord::Migration

def self.up
add_column :closed_operations, :matched_by, :text, :default => nil
end

def self.down
remove_column :closed_operations, :matched_by
end

end

0 comments on commit e256501

Please sign in to comment.