Skip to content

Commit

Permalink
devtracker.rb: Add money library for currency formatting, Add h2 Acti…
Browse files Browse the repository at this point in the history
…vities in transaction page routing for getting title for Commitments transaction. project_helpers.rb: Add h2Activity_title function to get Title for H2 Activity, Add sum_transaction_value function to sum up the total value for all transaction type (C,D,E, IF). transactions.html.erb: Correct Currency Formatting, Add Sum up value for each Transaction Type, Reorder the transaction based on soonest transaction date
  • Loading branch information
g-shafiullah committed Sep 21, 2015
1 parent c42e566 commit 55f10a2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
19 changes: 13 additions & 6 deletions devtracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'active_support'
require 'kramdown'
require 'pony'
require 'money'

#helpers path
require_relative 'helpers/formatters.rb'
Expand All @@ -25,10 +26,10 @@
include ProjectHelpers

# Developer Machine: set global settings
#set :oipa_api_url, 'http://dfid-oipa.zz-clients.net/api/'
set :oipa_api_url, 'http://dfid-oipa.zz-clients.net/api/'

# Server Machine: set global settings
set :oipa_api_url, 'http://127.0.0.1:6081/api/'
#set :oipa_api_url, 'http://127.0.0.1:6081/api/'

#ensures that we can use the extension html.erb rather than just .erb
Tilt.register Tilt::ERBTemplate, 'html.erb'
Expand Down Expand Up @@ -160,9 +161,14 @@
project = JSON.parse(oipa)

# get the transactions from the API
oipa_tx = RestClient.get settings.oipa_api_url + "activities/#{n}/transactions?format=json" #TEST: for Partner Project
tx = JSON.parse(oipa_tx)
transactions = tx['results']
oipaTransactionsJSON = RestClient.get settings.oipa_api_url + "transactions?format=json&activity_related_activity_id=#{n}&page_size=400"
transactionsJSON = JSON.parse(oipaTransactionsJSON)
transactions = transactionsJSON['results']

#get details of H2 Activities from the API
oipaH2ActivitiesJSON = RestClient.get settings.oipa_api_url + "activities?format=json&related_activity_id=#{n}&page_size=400"
h2ActivitiesJSON=JSON.parse(oipaH2ActivitiesJSON)
h2Activities=h2ActivitiesJSON['results']

# get the funded projects from the API
fundedProjectsAPI = RestClient.get settings.oipa_api_url + "activities?format=json&transaction_provider_activity=#{n}&page_size=1000"
Expand All @@ -172,7 +178,8 @@
:layout => :'layouts/layout',
:locals => {
project: project,
transactions: transactions,
transactions: transactions,
h2Activities: h2Activities,
fundedProjectsCount: fundedProjectsData['count']
}
end
Expand Down
13 changes: 13 additions & 0 deletions helpers/project_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,19 @@ def coerce_budget_vs_spend_items(cursor, type)
}
end

#New Function for API Based Devtracker
def h2Activity_title(h2Activities,h2ActivityId)
h2Activity = h2Activities.select {|activity| activity['id'] == h2ActivityId}.first
h2Activity['title']['narratives'][0]['text']
end

def sum_transaction_value(transactionType)
summedBudgets = transactionType.reduce(0) {|memo, t| memo + t['value'].to_f}

#total_transaction_value['summedBudgets']

end

end

helpers ProjectHelpers
65 changes: 33 additions & 32 deletions views/projects/transactions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ title: Development Tracker
<% #transactions.select {|group| group['transaction_type']['code'] == 'D' }.each do |commitment| %>
<% incoming_funds=transactions.select {|group| group['transaction_type']['code'] == 'IF' } %>
<%if !incoming_funds.nil? && incoming_funds.length > 0 %>
<% totalIncomingFund=sum_transaction_value(commitments).to_f %>
<div class="section-group-title trans-header-container">
<div class="trans-header-left">
<span class="trans-type-title">Incoming Funds</span>
Expand All @@ -28,8 +29,7 @@ title: Development Tracker
</div>
<div class="trans-header-right">
<div class="<%#= transaction_group["value"].to_f < 0 ? 'negative' : ''%> float-right trans-type-value"><%#=transaction_group["value"].to_f %>
<%#= number_to_currency(transaction_group["value"].to_f, :unit=> currency_symbol(project['currency']) || "£", :precision => 0)%>
<div class="<%= sum_transaction_value(incoming_funds).to_f < 0 ? 'negative' : ''%> float-right trans-type-value"><%= Money.new(totalIncomingFund*100, project['default_currency']['code']).format(:no_cents_if_whole => true,:sign_before_symbol => false)%>
</div>
</div>
</div>
Expand All @@ -41,13 +41,12 @@ title: Development Tracker
<th width="15%">Date</th>
<th width="15%" style="text-align:right;">Value</th>
</tr>
<% transactions.select {|group| group['transaction_type']['code'] == 'IF' }.each do |commitment| %>
<%# commitment.sort_by { | tx | -tx['transaction_date'] }.each do |transaction| %>
<% transactions.select {|group| group['transaction_type']['code'] == 'IF' }.sort{ |a,b| b['transaction_date'] <=> a['transaction_date'] }.each do |incomingFund| %>
<tr>
<td><%= commitment['description'] %></td>
<td width="30%"><%= commitment['provider_organisation_name'] + " (" + commitment['provider_activity'] + ")" %></td>
<td width="15%"><%= Date.parse(commitment['transaction_date']).strftime("%d %b %Y")%></td>
<td width="15%" class="<%= commitment['value'].to_f < 0 ? 'negative' : ''%>" style="text-align:right;"><%=commitment['value'] %><%#= number_to_currency(commitment['value'].to_f, :unit=> currency_symbol(project['currency']) || "£", :precision => 0) %></td>
<td><%= incomingFund['description'] %></td>
<td width="30%"><%= incomingFund['provider_organisation_name'] + " (" + incomingFund['provider_activity'] + ")" %></td>
<td width="15%"><%= Date.parse(incomingFund['transaction_date']).strftime("%d %b %Y")%></td>
<td width="15%" class="<%= incomingFund['value'].to_f < 0 ? 'negative' : ''%>" style="text-align:right;"><%= Money.new(incomingFund['value'].to_f*100, incomingFund['currency']['code']).format(:no_cents_if_whole => true,:sign_before_symbol => false) %></td>
</tr>
<% end %>
</tbody>
Expand All @@ -65,6 +64,7 @@ title: Development Tracker
<% #transactions.select {|group| group['transaction_type']['code'] == 'D' }.each do |commitment| %>
<% commitments=transactions.select {|group| group['transaction_type']['code'] == 'C' } %>
<%if !commitments.nil? && commitments.length > 0 %>
<% totalCommitments=sum_transaction_value(commitments).to_f %>
<div class="section-group-title trans-header-container">
<div class="trans-header-left">
<span class="trans-type-title">Commitment</span>
Expand All @@ -80,8 +80,9 @@ title: Development Tracker
</div>
<div class="trans-header-right">
<div class="<%#= transaction_group["value"].to_f < 0 ? 'negative' : ''%> float-right trans-type-value"><%#=transaction_group["value"].to_f %>
<div class="<%= totalCommitments < 0 ? 'negative' : ''%> float-right trans-type-value">
<%#= number_to_currency(transaction_group["value"].to_f, :unit=> currency_symbol(project['currency']) || "£", :precision => 0)%>
<%= Money.new(totalCommitments*100, project['default_currency']['code']).format(:no_cents_if_whole => true,:sign_before_symbol => false)%>
</div>
</div>
</div>
Expand All @@ -93,13 +94,13 @@ title: Development Tracker
<th width="15%">Date</th>
<th width="15%" style="text-align:right;">Value</th>
</tr>
<% transactions.select {|group| group['transaction_type']['code'] == 'C' }.each do |commitment| %>
<% transactions.select {|group| group['transaction_type']['code'] == 'C' }.sort{ |a,b| b['transaction_date'] <=> a['transaction_date'] }.each do |commitment| %>
<%# commitment.sort_by { | tx | -tx['transaction_date'] }.each do |transaction| %>
<tr>
<td><%= commitment['description'] %></td>
<td><%= h2Activity_title(h2Activities,commitment['activity']['id']) %></td>
<td width="15%"><%= commitment['activity']['id'] %></td>
<td width="15%"><%= Date.parse(commitment['transaction_date']).strftime("%d %b %Y")%></td>
<td width="15%" class="<%= commitment['value'].to_f < 0 ? 'negative' : ''%>" style="text-align:right;"><%=commitment['value'] %><%#= number_to_currency(commitment['value'].to_f, :unit=> currency_symbol(project['currency']) || "£", :precision => 0) %></td>
<td width="15%" class="<%= commitment['value'].to_f < 0 ? 'negative' : ''%>" style="text-align:right;"><%= Money.new(commitment['value'].to_f*100, commitment['currency']['code']).format(:no_cents_if_whole => true,:sign_before_symbol => false) %></td>
</tr>
<% end %>
</tbody>
Expand All @@ -115,8 +116,9 @@ title: Development Tracker
<div class="twelve columns transactions">
<% #transactions.select {|group| group['transaction_type']['code'] == 'C' }.each do |transaction_group| %>
<% #transactions.select {|group| group['transaction_type']['code'] == 'D' }.each do |commitment| %>
<% disbursement=transactions.select {|group| group['transaction_type']['code'] == 'D' } %>
<%if !disbursement.nil? && disbursement.length > 0 %>
<% disbursements=transactions.select {|group| group['transaction_type']['code'] == 'D' } %>
<%if !disbursements.nil? && disbursements.length > 0 %>
<% totalDisbursements=sum_transaction_value(disbursements).to_f %>
<div class="section-group-title trans-header-container">
<div class="trans-header-left">
<span class="trans-type-title">Disbursement</span>
Expand All @@ -132,8 +134,7 @@ title: Development Tracker
</div>
<div class="trans-header-right">
<div class="<%#= transaction_group["value"].to_f < 0 ? 'negative' : ''%> float-right trans-type-value"><%#=transaction_group["value"].to_f %>
<%#= number_to_currency(transaction_group["value"].to_f, :unit=> currency_symbol(project['currency']) || "£", :precision => 0)%>
<div class="<%= totalDisbursements < 0 ? 'negative' : ''%> float-right trans-type-value"><%= Money.new(totalDisbursements*100, project['default_currency']['code']).format(:no_cents_if_whole => true,:sign_before_symbol => false)%>
</div>
</div>
</div>
Expand All @@ -146,14 +147,14 @@ title: Development Tracker
<th width="15%">Date</th>
<th width="15%" style="text-align:right;">Value</th>
</tr>
<% transactions.select {|group| group['transaction_type']['code'] == 'D' }.each do |commitment| %>
<% transactions.select {|group| group['transaction_type']['code'] == 'D' }.sort{ |a,b| b['transaction_date'] <=> a['transaction_date'] }.each do |disbursement| %>
<%# commitment.sort_by { | tx | -tx['transaction_date'] }.each do |transaction| %>
<tr>
<td><%= commitment['description'] %></td>
<td width="15%"><%= commitment['receiver_organisation_name'] %></td>
<td width="15%"><%= commitment['activity']['id'] %></td>
<td width="15%"><%= Date.parse(commitment['transaction_date']).strftime("%d %b %Y")%></td>
<td width="15%" class="<%= commitment['value'].to_f < 0 ? 'negative' : ''%>" style="text-align:right;"><%=commitment['value'] %><%#= number_to_currency(commitment['value'].to_f, :unit=> currency_symbol(project['currency']) || "£", :precision => 0) %></td>
<td><%= disbursement['description'] %></td>
<td width="15%"><%= disbursement['receiver_organisation_name'] %></td>
<td width="15%"><%= disbursement['activity']['id'] %></td>
<td width="15%"><%= Date.parse(disbursement['transaction_date']).strftime("%d %b %Y")%></td>
<td width="15%" class="<%= disbursement['value'].to_f < 0 ? 'negative' : ''%>" style="text-align:right;"><%= Money.new(disbursement['value'].to_f*100, disbursement['currency']['code']).format(:no_cents_if_whole => true,:sign_before_symbol => false) %></td>
</tr>
<% end %>
</tbody>
Expand All @@ -169,8 +170,9 @@ title: Development Tracker
<div class="twelve columns transactions">
<% #transactions.select {|group| group['transaction_type']['code'] == 'C' }.each do |transaction_group| %>
<% #transactions.select {|group| group['transaction_type']['code'] == 'D' }.each do |commitment| %>
<% expenditure=transactions.select {|group| group['transaction_type']['code'] == 'E' } %>
<%if !expenditure.nil? && expenditure.length > 0 %>
<% expenditures=transactions.select {|group| group['transaction_type']['code'] == 'E' } %>
<%if !expenditures.nil? && expenditures.length > 0 %>
<% totalExpenditures=sum_transaction_value(expenditures).to_f %>
<div class="section-group-title trans-header-container">
<div class="trans-header-left">
<span class="trans-type-title">Expenditure</span>
Expand All @@ -186,8 +188,7 @@ title: Development Tracker
</div>
<div class="trans-header-right">
<div class="<%#= transaction_group["value"].to_f < 0 ? 'negative' : ''%> float-right trans-type-value"><%#=transaction_group["value"].to_f %>
<%#= number_to_currency(transaction_group["value"].to_f, :unit=> currency_symbol(project['currency']) || "£", :precision => 0)%>
<div class="<%= totalExpenditures < 0 ? 'negative' : ''%> float-right trans-type-value"><%= Money.new(totalExpenditures*100, project['default_currency']['code']).format(:no_cents_if_whole => true,:sign_before_symbol => false)%>
</div>
</div>
</div>
Expand All @@ -200,14 +201,14 @@ title: Development Tracker
<th width="15%">Date</th>
<th width="15%" style="text-align:right;">Value</th>
</tr>
<% transactions.select {|group| group['transaction_type']['code'] == 'E' }.each do |commitment| %>
<% transactions.select {|group| group['transaction_type']['code'] == 'E' }.sort{ |a,b| b['transaction_date'] <=> a['transaction_date'] }.each do |expenditure| %>
<%# commitment.sort_by { | tx | -tx['transaction_date'] }.each do |transaction| %>
<tr>
<td><%= commitment['description'] %></td>
<td width="15%"><%= commitment['receiver_organisation_name'] %></td>
<td width="15%"><%= commitment['activity']['id'] %></td>
<td width="15%"><%= Date.parse(commitment['transaction_date']).strftime("%d %b %Y")%></td>
<td width="15%" class="<%= commitment['value'].to_f < 0 ? 'negative' : ''%>" style="text-align:right;"><%=commitment['value'] %><%#= number_to_currency(commitment['value'].to_f, :unit=> currency_symbol(project['currency']) || "£", :precision => 0) %></td>
<td><%= expenditure['description'] %></td>
<td width="15%"><%= expenditure['receiver_organisation_name'] %></td>
<td width="15%"><%= expenditure['activity']['id'] %></td>
<td width="15%"><%= Date.parse(expenditure['transaction_date']).strftime("%d %b %Y")%></td>
<td width="15%" class="<%= expenditure['value'].to_f < 0 ? 'negative' : ''%>" style="text-align:right;"><%= Money.new(expenditure['value'].to_f*100, expenditure['currency']['code']).format(:no_cents_if_whole => true,:sign_before_symbol => false) %></td>
</tr>
<% end %>
</tbody>
Expand Down

0 comments on commit 55f10a2

Please sign in to comment.