From 646e4812398d93de85b22930c6a91e60f1d1aa5c Mon Sep 17 00:00:00 2001 From: Gazi Date: Wed, 7 Oct 2015 16:44:38 +0600 Subject: [PATCH] 1. devtracker.rb: Added Transaction and Quaterly Financial budget API Call for Transaction Page for both DFID and Partners Project and set current financial start and end date globally. 2. codelists.rb: add function to identify document mime type. 3. common_helpers.rb: add common functions those can be used to get desired outputs. 4. country_helpers.rb: change variable names to camel case. 5. document_helpers.rb: add function to get document category list for a document. 6. project_helpers.rb: add function to sum budgets for a project and will provide 0 if there is no budget. 7. country.html.erb: formating change on Total Project Budget. 8. _project-list.html.erb: Project Budget added into Project Search list with proper formatting and document type filter option disabled due performance issue. 9. _projects-header.html.erb: Breadcrumb issue fixed for project (specially for Partner Projects) 10. documents.html.erb: Add human readable document type in the page. 11. transactions.html.erb: Budgets and all type of transactions (IF,C,D,E) are generated for both DFID and Partner Projects. --- devtracker.rb | 46 ++++++++---- helpers/codelists.rb | 5 +- helpers/common_helpers.rb | 90 ++++++++++++++++++++++++ helpers/country_helpers.rb | 26 +++---- helpers/document_helpers.rb | 20 +++++- helpers/project_helpers.rb | 15 +++- views/countries/country.html.erb | 4 +- views/partials/_project_list.html.erb | 8 +-- views/partials/_projects-header.html.erb | 4 +- views/projects/documents.html.erb | 12 +--- views/projects/transactions.html.erb | 75 ++++++++++++++++---- 11 files changed, 243 insertions(+), 62 deletions(-) create mode 100644 helpers/common_helpers.rb diff --git a/devtracker.rb b/devtracker.rb index 3f08a565..57450c55 100644 --- a/devtracker.rb +++ b/devtracker.rb @@ -18,18 +18,23 @@ require_relative 'helpers/project_helpers.rb' require_relative 'helpers/sector_helpers.rb' require_relative 'helpers/country_helpers.rb' +require_relative 'helpers/document_helpers.rb' +require_relative 'helpers/common_helpers.rb' #Helper Modules include CountryHelpers include Formatters include SectorHelpers include ProjectHelpers +include CommonHelpers # 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/' +set :current_first_day_of_financial_year, first_day_of_financial_year(DateTime.now) +set :current_last_day_of_financial_year, last_day_of_financial_year(DateTime.now) # 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' @@ -46,8 +51,10 @@ # top5countries = JSON.parse(File.read('data/top5countries.json')) top5sectors = JSON.parse(File.read('data/top5sectors.json')) top5results = JSON.parse(File.read('data/top5results.json')) - - countriesJSON = RestClient.get settings.oipa_api_url + "activities/aggregations?reporting_organisation=GB-1&group_by=recipient_country&aggregations=budget&budget_period_start=2015-04-01&budget_period_end=2016-03-31&order_by=-budget&page_size=5" + #current_first_day_of_financial_year = first_day_of_financial_year(DateTime.now) + #current_last_day_of_financial_year = last_day_of_financial_year(DateTime.now) + + countriesJSON = RestClient.get settings.oipa_api_url + "activities/aggregations?reporting_organisation=GB-1&group_by=recipient_country&aggregations=budget&budget_period_start=#{settings.current_first_day_of_financial_year}&budget_period_end=#{settings.current_last_day_of_financial_year}&order_by=-budget&page_size=5" top5countries = JSON.parse(countriesJSON) erb :index, @@ -66,14 +73,14 @@ # Country summary page get '/countries/:country_code/?' do |n| - current_first_day_of_financial_year = first_day_of_financial_year(DateTime.now) - current_last_day_of_financial_year = last_day_of_financial_year(DateTime.now) + #current_first_day_of_financial_year = first_day_of_financial_year(DateTime.now) + #current_last_day_of_financial_year = last_day_of_financial_year(DateTime.now) country = countriesInfo.select {|country| country['code'] == n}.first oipa_total_projects = RestClient.get settings.oipa_api_url + "activities?reporting_organisation=GB-1&hierarchy=1&related_activity_recipient_country=#{n}&format=json" total_projects = JSON.parse(oipa_total_projects) oipa_active_projects = RestClient.get settings.oipa_api_url + "activities?reporting_organisation=GB-1&hierarchy=1&related_activity_recipient_country=#{n}&activity_status=2&format=json" active_projects = JSON.parse(oipa_active_projects) - oipa_total_project_budgets = RestClient.get settings.oipa_api_url + "activities/aggregations?format=json&reporting_organisation=GB-1&budget_period_start=#{current_first_day_of_financial_year}&budget_period_end=#{current_last_day_of_financial_year}&group_by=recipient_country&aggregations=budget&recipient_country=#{n}" + oipa_total_project_budgets = RestClient.get settings.oipa_api_url + "activities/aggregations?format=json&reporting_organisation=GB-1&budget_period_start=#{settings.current_first_day_of_financial_year}&budget_period_end=#{settings.current_last_day_of_financial_year}&group_by=recipient_country&aggregations=budget&recipient_country=#{n}" total_project_budgets= JSON.parse(oipa_total_project_budgets) oipa_year_wise_budgets=RestClient.get settings.oipa_api_url + "activities/aggregations?format=json&reporting_organisation=GB-1&group_by=budget_per_quarter&aggregations=budget&recipient_country=#{n}&order_by=year,quarter" year_wise_budgets= JSON.parse(oipa_year_wise_budgets) @@ -160,10 +167,18 @@ oipa = RestClient.get settings.oipa_api_url + "activities/#{n}?format=json" project = JSON.parse(oipa) - # get the transactions from the API - oipaTransactionsJSON = RestClient.get settings.oipa_api_url + "transactions?format=json&activity_related_activity_id=#{n}&page_size=400" + # get the transactions from the API & get quarterly budget for H1 Activity + if is_dfid_project(project['id']) then + oipaTransactionsJSON = RestClient.get settings.oipa_api_url + "transactions?format=json&activity_related_activity_id=#{n}&page_size=400&fields=activity,description,provider_organisation_name,provider_activity,receiver_organisation_name,transaction_date,transaction_type,value,currency" + oipaYearWiseBudgets=RestClient.get settings.oipa_api_url + "activities/aggregations?format=json&reporting_organisation=GB-1&group_by=budget_per_quarter&aggregations=budget&related_activity_id=#{n}&order_by=year,quarter" + else + oipaTransactionsJSON = RestClient.get settings.oipa_api_url + "transactions?format=json&activity=#{n}&page_size=400&fields=activity,description,provider_organisation_name,provider_activity,receiver_organisation_name,transaction_date,transaction_type,value,currency" + oipaYearWiseBudgets=RestClient.get settings.oipa_api_url + "activities/aggregations?format=json&group_by=budget_per_quarter&aggregations=budget&id=#{n}&order_by=year,quarter" + end + transactionsJSON = JSON.parse(oipaTransactionsJSON) - transactions = transactionsJSON['results'] + transactions = transactionsJSON['results'] + yearWiseBudgets= JSON.parse(oipaYearWiseBudgets) #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" @@ -179,6 +194,7 @@ :locals => { project: project, transactions: transactions, + yearWiseBudgets: yearWiseBudgets, h2Activities: h2Activities, fundedProjectsCount: fundedProjectsData['count'] } @@ -208,9 +224,9 @@ #Aid By Location Page get '/location/country/?' do - current_first_day_of_financial_year = first_day_of_financial_year(DateTime.now) - current_last_day_of_financial_year = last_day_of_financial_year(DateTime.now) - oipa_countries = RestClient.get settings.oipa_api_url + "activities/aggregations?format=json&reporting_organisation=GB-1&budget_period_start=#{current_first_day_of_financial_year}&budget_period_end=#{current_last_day_of_financial_year}&group_by=recipient_country&aggregations=count,budget&order_by=recipient_country" + #current_first_day_of_financial_year = first_day_of_financial_year(DateTime.now) + #current_last_day_of_financial_year = last_day_of_financial_year(DateTime.now) + oipa_countries = RestClient.get settings.oipa_api_url + "activities/aggregations?format=json&reporting_organisation=GB-1&budget_period_start=#{settings.current_first_day_of_financial_year}&budget_period_end=#{settings.current_last_day_of_financial_year}&group_by=recipient_country&aggregations=count,budget&order_by=recipient_country" countries = JSON.parse(oipa_countries) erb :'location/country/index', @@ -220,6 +236,10 @@ } end +get '/department' do + erb :'department/index', :layout => :'layouts/layout' +end + get '/about/?' do erb :'about/index', :layout => :'layouts/layout' end diff --git a/helpers/codelists.rb b/helpers/codelists.rb index bfeaf6a1..3b973028 100644 --- a/helpers/codelists.rb +++ b/helpers/codelists.rb @@ -69,7 +69,8 @@ module CodeLists "application/vnd.ms-excel" => "Excel Spreadsheet", "application/octet-stream" => "Binary file", "application/pdf" => "PDF Document", - "text/html" => "Web Page" + "text/html" => "Web Page", + "application/vnd.oasis.opendocument.text" => "Open Word Document" } @@ -298,7 +299,7 @@ def activity_status(code) end def iati_mime_type(code) - @@mime_types[code] + @@iati_mime_types[code] end def sector(code) diff --git a/helpers/common_helpers.rb b/helpers/common_helpers.rb new file mode 100644 index 00000000..48e20178 --- /dev/null +++ b/helpers/common_helpers.rb @@ -0,0 +1,90 @@ +module CommonHelpers + + #New One based on API Call + def financial_year_wise_budgets(yearWiseBudgets,type) + + projectBudgets = [] + + yearWiseBudgets.each do |y| + if y["quarter"]==1 + p=[y["quarter"],y["budget"],y["year"],y["year"]-1] + else + p=[y["quarter"],y["budget"],y["year"],y["year"]] + end + + projectBudgets << p + end + + #project_budgets + #project_budgets = year_wise_budgets.group_by{|b| b["year"]}.map{|year, budgets| + #summedBudgets = budgets.reduce(0) {|memo, budget| memo + budget["budget"]} + #[year, summedBudgets] + #}.sort + + finYearWiseBudgets = projectBudgets.group_by{|b| b[3]}.map{|year, budgets| + summedBudgets = budgets.reduce(0) {|memo, budget| memo + budget[1]} + [year, summedBudgets] + }.sort + + # determine what range to show + #current_financial_year = first_day_of_financial_year(DateTime.now) + currentFinancialYear = financial_year + + # if range is 6 or less just show it + if (type=="C") then + range = if finYearWiseBudgets.size < 7 then + finYearWiseBudgets + # if the last item in the list is less than or equal to + # the current financial year get the last 6 + elsif finYearWiseBudgets.last.first <= currentFinancialYear + finYearWiseBudgets.last(6) + # other wise show current FY - 3 years and cuurent FY + 3 years + else + index_of_now = finYearWiseBudgets.index { |i| i[0] == currentFinancialYear } + + if index_of_now.nil? then + finYearWiseBudgets.last(6) + else + finYearWiseBudgets[[index_of_now-3,0].max..index_of_now+2] + end + end + + # finally convert the range into a label format + range.each { |item| + item[0] = financial_year_formatter(item[0]) + } + elsif (type=="P") then + finYearWiseBudgets.each { |item| + item[0] = financial_year_formatter(item[0]) + } + + end + + end + + #New One based on API Call + def financial_year_formatter(y) + + #date = if(d.kind_of?(String)) then + #Date.parse d + #else + #d + #end + + #if date.month < 4 + #{}"FY#{(date.year-1).to_s[2..3]}/#{date.year.to_s[2..3]}" + #else + "FY#{y.to_s[2..3]}/#{(y+1).to_s[2..3]}" + #end + end + + def financial_year + now = Time.new + if(now.month < 4) + now.year-1 + else + now.year + end + end + +end diff --git a/helpers/country_helpers.rb b/helpers/country_helpers.rb index 854e490e..afe0a38c 100755 --- a/helpers/country_helpers.rb +++ b/helpers/country_helpers.rb @@ -75,21 +75,21 @@ def sector_groups(countryCode) end #New One based on API Call - def country_project_budgets(year_wise_budgets) + def country_project_budgets(yearWiseBudgets) #projectCodes = @cms_db['projects'].find({ #{}"projectType" => "country", "recipient" => country_code #}, :fields => ["iatiId"]).to_a.map{|b| b["iatiId"]} # - project_budgets = [] + projectBudgets = [] - year_wise_budgets.each do |y| + yearWiseBudgets.each do |y| if y["quarter"]==1 p=[y["quarter"],y["budget"],y["year"],y["year"]-1] else p=[y["quarter"],y["budget"],y["year"],y["year"]] end - project_budgets << p + projectBudgets << p end #project_budgets @@ -98,30 +98,30 @@ def country_project_budgets(year_wise_budgets) #[year, summedBudgets] #}.sort - fin_year_wise_budgets = project_budgets.group_by{|b| b[3]}.map{|year, budgets| + finYearWiseBudgets = projectBudgets.group_by{|b| b[3]}.map{|year, budgets| summedBudgets = budgets.reduce(0) {|memo, budget| memo + budget[1]} [year, summedBudgets] }.sort # determine what range to show #current_financial_year = first_day_of_financial_year(DateTime.now) - current_financial_year = financial_year + currentFinancialYear = financial_year # if range is 6 or less just show it - range = if fin_year_wise_budgets.size < 7 then - fin_year_wise_budgets + range = if finYearWiseBudgets.size < 7 then + finYearWiseBudgets # if the last item in the list is less than or equal to # the current financial year get the last 6 - elsif fin_year_wise_budgets.last.first <= current_financial_year - fin_year_wise_budgets.last(6) + elsif finYearWiseBudgets.last.first <= currentFinancialYear + finYearWiseBudgets.last(6) # other wise show current FY - 3 years and cuurent FY + 3 years else - index_of_now = fin_year_wise_budgets.index { |i| i[0] == current_financial_year } + index_of_now = finYearWiseBudgets.index { |i| i[0] == currentFinancialYear } if index_of_now.nil? then - fin_year_wise_budgets.last(6) + finYearWiseBudgets.last(6) else - fin_year_wise_budgets[[index_of_now-3,0].max..index_of_now+2] + finYearWiseBudgets[[index_of_now-3,0].max..index_of_now+2] end end diff --git a/helpers/document_helpers.rb b/helpers/document_helpers.rb index 91a52e8b..b6946755 100644 --- a/helpers/document_helpers.rb +++ b/helpers/document_helpers.rb @@ -36,4 +36,22 @@ def getR4DSearchLink(object_id) def getR4DDocsCountNotShowing(output_count) output_count.to_i - 5 -end \ No newline at end of file +end + +def get_document_category(projectId) + oipa = RestClient.get settings.oipa_api_url + "activities/#{projectId}?format=json" + project = JSON.parse(oipa) + documents=project['document_links'] + + if documents.length>0 then + result = ""; + documents.each do |document| + if !document['category']['code'].nil? then + result += document_category(document['category']['code'].to_s.strip) + '#' + end + end + result = result.chop! + else + result="" + end +end diff --git a/helpers/project_helpers.rb b/helpers/project_helpers.rb index 66e27091..8e15c74a 100755 --- a/helpers/project_helpers.rb +++ b/helpers/project_helpers.rb @@ -313,15 +313,26 @@ def coerce_budget_vs_spend_items(cursor, type) #New Function for API Based Devtracker def h2Activity_title(h2Activities,h2ActivityId) - h2Activity = h2Activities.select {|activity| activity['id'] == h2ActivityId}.first - h2Activity['title']['narratives'][0]['text'] + if h2Activities.length>0 then + h2Activity = h2Activities.select {|activity| activity['id'] == h2ActivityId}.first + h2Activity['title']['narratives'][0]['text'] + else + "" + end end def sum_transaction_value(transactionType) summedBudgets = transactionType.reduce(0) {|memo, t| memo + t['value'].to_f} #total_transaction_value['summedBudgets'] + end + def sum_budget_value(projectBudgets) + if !projectBudgets.nil? && projectBudgets.length > 0 then + summedBudgets = projectBudgets.reduce(0) {|memo, t| memo + t[1].to_f} + else + summedBudgets =0 + end end end diff --git a/views/countries/country.html.erb b/views/countries/country.html.erb index 8925e6eb..e0ac62ef 100755 --- a/views/countries/country.html.erb +++ b/views/countries/country.html.erb @@ -64,7 +64,7 @@ Project budget to date. Some values may not be included if projects are in an active procurement phase, so this figure will change over time. - <%= total_project_budgets[0]['budget'].to_f.round(0)%> + <%= Money.new(total_project_budgets[0]['budget'].to_f.round(0)*100, "GBP").format(:no_cents_if_whole => true,:sign_before_symbol => false)%>