{% load i18n %}

{% trans 'Fetching IATI Data' %}

{xhrmessage}


 

var tag = this; tag.store = stores.oipaStore; tag.mixin('TabMixin'); function symSetDiff(setA, setB) { var _difference = new Set(setA); for (var elem of setB) { if (_difference.has(elem)) { _difference.delete(elem); } else { _difference.add(elem); } } return _difference; } tag.has_changed = function() { window.data.iati_data.curr_sync_state = tag.store.curr_sync_state; var _difference = symSetDiff(tag.store.curr_sync_state, tag.store.saved_sync_state); if (_difference.size > 0) { $.confirm({ type: 'orange', theme: 'modern', icon: 'glyphicon glyphicon-warning-sign', columnClass: 'medium', title: 'IATI Sync has unsaved changes!', content: 'Navigating away discards all un-saved changes to IATI Sync settings. Are you sure you want to do that?', buttons: { save: { btnClass: 'btn-success', action: function () { $.confirm({ closeIcon: false, type: 'blue', theme: 'modern', columnClass: 'medium', title: 'Saving IATI Sync settings', content: function() { var self = this; var form_data = {}; Array.from(window.data.iati_data.curr_sync_state).forEach(function(data){ form_data[data] = '1'; }); form_data['aims-activity-id'] = data.iati_data.link_info.activity_id; self.setContentAppend('
'+'{% trans "Saving changes to IATI Sync settings" %}'+'...
'); return $.ajax({ type: 'POST', url: "{% url 'oipa_activity_link_update' %}", data: form_data, headers: { 'X-CSRFTOKEN': window.csrf_token } }).done(function (res) { var makeSyncActivitiesRequest = function(job){ var url = "{% url 'oipa_sync_activities' %}" + res.activity_id + '?celerize'; var request; if (job) { url = url + '?job=' + job}; request = $.ajax({ type: 'GET', url: url }); request.done(function(res){ if (request.status == 202){ /* 2-second Loop on a "202:Accepted" return code */ return setTimeout(function(){makeSyncActivitiesRequest(res.job)}, 2000); } else { $('#iatiSyncRunning').hide(300, function() { self.setContentAppend('
'+" {% trans "IATI Sync completed." %}"+'
'); self.buttons.close_success.show(); }); } }); request.fail(function (err) { $('#iatiSyncRunning').hide(300, function() { self.setContentAppend('
'+" {% trans "Error running IATI Sync." %}"+'
'); self.buttons.close_fail.show(); }); }); self.setContentAppend('
'+" {% trans "IATI Sync settings updated." %}"+'
'); self.setContentAppend('
'+"{% trans "Full IATI Sync of data is running..." %}"+'
'); self.setContentAppend('

'); makeSyncActivitiesRequest(); }; }).fail(function (err) { $('#iatiSyncRunning').hide(300, function() { self.setContentAppend('
'+" {% trans "Error updating IATI Sync settings." %}"+'
'); self.buttons.close_fail.show(); }); }); }, buttons: { close_success: { text: "Refresh", isHidden: true, action: function() { window.location.reload(); } }, close_fail: { text: "Close", isHidden: true, action: function() { } } } }); } }, discard: { btnClass: 'btn-default', action: function () { } } } }); return undefined; } } tag.oipa_missing = data.iati_data.link_info.activity_id == ""; tag.async_fetching = true; tag.openly_iati_id = data.activity.iati_identifier; tag.update_iati_id = function() { window.modals.update_iati_link.toggle(); } tag.fields = [ {name: 'Budgets', selector: 'B'}, {name: 'Commitments', selector: 'C'}, {name: 'Outgoing Funds', selector: 'OF'}, {name: 'Incoming Funds', selector: 'IF'}, ]; function isEmpty(obj) { return Object.keys(obj).length === 0 && obj.constructor === Object } function processTransactions(selector) { if (selector == 'B') { // filter for budgets var filtered_objs = data.budgets; var sum = _(filtered_objs).map('value').map(_.toNumber).sum(); var currency = (sum === 0) ? 'USD' : filtered_objs[0]['currency_id']; } else { // filter for transactions var filtered_objs = []; if (selector == 'OF') { // filter for both disbursements and expenditures var other_selectors = ['D', 'E']; other_selectors.forEach(function(s) { filtered_objs.push.apply(filtered_objs, _.filter(data.transactions, function (t) { return t.transaction_type === s })); }); } else { // filter vanilla committments and incoming funds var filtered_objs = _.filter(data.transactions, function(t){return t.transaction_type === selector}); } var sum = _(filtered_objs).map('value').map(_.toNumber).sum(); var currency = (sum === 0) ? 'USD' : filtered_objs[0]['currency']; } return { currency: currency, total: sum }; } tag.on('mount', function() { if (!tag.oipa_missing) { tag.async_fetching = true; // fetch IATI data from OIPA server if (_.isNull(data.activity.iati_identifier)){ tag.oipa_missing = true; tag.async_fetching = true; return tag.update(); } function updateRequest(){ var request_data = { 'openly_id': document.getElementById('aims-activity-id').value, 'iati_identifier': document.getElementById('oipa-iati-id').value, 'celerize': 'True', }; var xhr; // Hopefully, we have a 'job id' if we did not get a success code if (tag.job)(request_data.job = tag.job); xhr = $.ajax({ type: 'GET', url: "{% url 'oipa_activity_by_iati_identifier' %}", data: request_data, headers: { 'X-CSRFTOKEN': window.csrf_token } }).done(function (res) { /* Assert that this is type 200 OR type 202 AND has a "job" UUID in json Return type 202: Accepted indicated the server is still waiting for OIPA Exit and call updateRequest until we have a 200 */ if (xhr.status == 202){ tag.job = tag.job || res.job; if (res.info) { tag.update({xhrmessage:res.info}) }; return setTimeout(function(){updateRequest()}, 2000); } if (res['activity'] && res['transactions'] && res['validation_results']) { tag.oipa_missing = false; // set iati activity and transaction data from response data.iati_data.activity = res['activity']; data.iati_data.transactions = res['transactions']; data.iati_data.validation_results = res['validation_results']; // set tag shortcuts vars to IATI data elements tag.oipa_agg = data.iati_data.transactions; tag.oipa_link = data.iati_data.link_info; tag.oipa_validations = data.iati_data.validation_results; tag.oipa_missing = data.iati_data.activity.iati_id == ""; tag.sync_history = data.iati_data.sync_records; tag.sync_history.forEach(function(r) { r.short_date = r.sync_datetime.substring(0, 10); r.all_zeros = ((r.b_added + r.if_added + r.c_added + r.of_added) == 0); }); tag.sync_history = _.filter(tag.sync_history, function (r) { return r.all_zeros !== true; }) if (tag.sync_history.length > 0) { tag.last_fetched = tag.sync_history[tag.sync_history.length-1].short_date; tag.got_history = true; } else { tag.last_fetched = false; tag.got_history = false; } if (!tag.oipa_missing) { tag.fields.forEach(function(f) { f.oipa_valid = tag.oipa_validations[f.selector]; openly_transactions = processTransactions(f.selector); f.openly_curr = openly_transactions['currency']; f.openly_sum = openly_transactions['total']; f.oipa_sum = 0; f.oipa_curr = "USD"; var oipa_trans = tag.oipa_agg[f.selector]; if (oipa_trans != undefined && oipa_trans.length > 0) { oipa_trans.forEach(function (i) { f.oipa_sum += parseFloat(i.value); }); if (f.selector == "B") { f.oipa_curr = oipa_trans[0]['currency_id']; } else { f.oipa_curr = oipa_trans[0]['currency']; } } f.linked = (tag.oipa_link.oipa_fields.indexOf(f.selector) != -1); }); tag.curr_states = tag.fields; } tag.async_fetching = false; tag.update(); } else { tag.async_fetching = true; tag.oipa_missing = true; tag.update(); } }).fail(function(err) { tag.oipa_missing = true; tag.async_fetching = true; tag.update(); }); }; // Start the request cycle updateRequest(); } });

{% trans 'Setup IATI Sync to automatically import the financial transactions for this activity from your officially published IATI activity file found in the IATI Registry' %} - https://www.iatiregistry.org/publisher).

var tag = this; tag.oipa_missing = data.iati_data.link_info.activity_id == "";
var tag = this;
{% trans 'Locally Reported' %} {% trans 'Reported to IATI' %} {% trans 'Sync' %}
{ name } { accounting.formatMoney(openly_sum, openly_curr + ' ', 0) } { accounting.formatMoney(data.iati_data.transactions[selector].clean_sum, data.iati_data.transactions[selector].currency + ' ', 0) }
var tag = this; tag.sync_update_toggle = function(e) { window.modals.sync_update.toggle() }; tag.sync_delete_toggle = function(e) { window.modals.sync_delete.toggle() }; tag.parent.fields.forEach(function(f) { $('.publish-popover-' + f.selector).mouseover(function(){ $('#publish-popover-' + f.selector).show(); }); $('#publish-popover-'+ f.selector +'-cancel').on('click', function(){ $('#publish-popover-' + f.selector).hide(); }); }); tag.update(); tag.on("mount", function(){ tag.update(); }); tag.update_sync_state = function(e) { if (e.currentTarget.checked) { tag.parent.store.curr_sync_state.add(e.item.selector); } else { tag.parent.store.curr_sync_state.delete(e.item.selector); } }
{% trans 'Last Fetched' %} { parent.last_fetched }
{% trans 'No sync history available.' %}
{% trans 'Sync Date' %} {% trans 'Budgets' %} {% trans 'Commitments' %} {% trans 'Outgoing Funds' %} {% trans 'Incoming Funds' %}
{ short_date } { b_added } { c_added } { of_added } { if_added }
var tag = this;
{% trans 'Invalid IATI Data' %} var tag = this; tag.update(); {% include 'tags/finances/tab-iati-sync-modals.html' %}