Skip to content

Commit

Permalink
Merge pull request #36 from jHetzer/pre_release/0.4.1
Browse files Browse the repository at this point in the history
Pre release/0.4.1
  • Loading branch information
jHetzer authored Jun 8, 2020
2 parents b8f119c + 6c0086f commit 5e7b83b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 20 deletions.
2 changes: 1 addition & 1 deletion erpnextfints/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*- # noqa: D100, D104
from __future__ import unicode_literals

__version__ = '0.3.4'
__version__ = '0.4.1'
14 changes: 12 additions & 2 deletions erpnextfints/erpnextfints/doctype/fints_import/fints_import.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
frappe.ui.form.on('FinTS Import', {
onload: function(frm) {
erpnextfints.interactive.progressbar(frm);
if(frm.doc.docstatus == 1){
if(frm.doc.docstatus === 1){
frm.set_df_property('import_transaction', 'label', __("Re-Import Transactions"));
frm.toggle_display("import_details_section",true);
}
if(frm.doc.docstatus == 0 && !(frm.doc.to_date)){
if(frm.doc.docstatus === 0 && !(frm.doc.to_date)){
frm.set_value(
"to_date",
frappe.datetime.add_days(frappe.datetime.nowdate(),-1)
Expand All @@ -25,6 +25,16 @@ frappe.ui.form.on('FinTS Import', {
},*/
refresh: function(frm) {
if(cur_frm.doc.__islocal == null){
if(frm.doc.file_url){
frm.add_custom_button(__('Donwload JSON File'), function () {
var base_url = "/api/method/erpnextfints.utils.client.get_fints_import_file/";
var filename = frm.doc.file_url.replace(/^.*[\\/]/, '');
window.open(
(base_url + filename + "?fints_import=" + frm.doc.name)
, '_blank'
);
});
}
if(frm.doc.fints_login && frm.doc.docstatus == 0){
frm.toggle_display("import_transaction",true);
frm.page.set_primary_action(__("Start Import"), function() {
Expand Down
24 changes: 22 additions & 2 deletions erpnextfints/public/js/controllers/fints_interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,37 @@ frappe.provide("erpnextfints.interactive");

erpnextfints.interactive = {
progressbar: function(frm) {
let ajaxErrorCloseHandler = function(/* e */){
// Queue close function on ajax error
setTimeout(function(){
frappe.hide_progress();
},2000);
};
let pageUnloadHandler = function(){
$(document).unbind("ajaxError",ajaxErrorCloseHandler);
setTimeout(function(){
// Queue unbind action on page leave
$(document).unbind("form-unload",pageUnloadHandler);
},0);
};
frappe.ui.form.on(frm.doc.doctype, {
refresh: function() {
$(document).ajaxError(ajaxErrorCloseHandler);
}
});
$(document).on("form-unload", pageUnloadHandler);

frappe.realtime.on("fints_progressbar", function(data) {
if(data.docname === frm.doc.name) {
if(data.reload && data.reload === true) {
frm.reload_doc();
}
if(data.progress==100) {
if(data.progress === 100) {
frappe.hide_progress();
} else {
frappe.show_progress(data.docname,data.progress,100,data.message);
}
}
});
},
}
};
39 changes: 39 additions & 0 deletions erpnextfints/utils/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,45 @@ def get_accounts(fints_login, user_scope):
}


@frappe.whitelist()
def get_fints_import_file(fints_import):
"""View or download FinTS transactions file.
:param fints_import: fints_import doc name
:type fints_import: str
:return: FinTS transactions file
"""
from erpnextfints.utils.fints_controller import FinTSController
from werkzeug.wrappers import Response
from frappe import _
from frappe.core.doctype.access_log.access_log import make_access_log

curr_doc = frappe.get_doc("FinTS Import", fints_import)

# check user permission
if not curr_doc.has_permission("read"):
frappe.throw(_("Not permitted"), frappe.PermissionError)
frappe.msgprint(frappe.get_user().name)

# build web response
response = Response()
response.data = frappe.as_json(
FinTSController(curr_doc.fints_login)
.get_fints_import_file_content(curr_doc)
)
response.mimetype = 'application/json'
response.charset = 'utf-8'

# record user fie access
make_access_log(
doctype='FinTS Import',
document=curr_doc.name,
file_type=curr_doc.file_url
)

return response


@frappe.whitelist()
def new_bank_account(payment_doc, bankData):
"""Create new bank account.
Expand Down
33 changes: 18 additions & 15 deletions erpnextfints/utils/fints_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def __init_fints_connection(self):
:return: None
"""
self.interactive.show_progress_realtime(
_("Initialise connection"), 10, reload=False
)
try:
self.interactive.show_progress_realtime(
_("Initialise connection"), 10, reload=False
)
if not hasattr(self, 'fints_connection'):
password = self.fints_login.get_password('fints_password')
self.fints_connection = FinTS3PinTanClient(
Expand Down Expand Up @@ -95,6 +95,20 @@ def __get_fints_account_by_key(self, key, value):
# Account can be None
return account

def get_fints_import_file_content(self, fints_import):
if fints_import.file_url:
content = get_file(fints_import.file_url)[1]
# Check content hash for file manipulations
if fints_import.file_hash == get_content_hash(content):
return frappe.json.loads(
content,
strict=False
)
else:
raise ValueError('File hash does not match')
else:
return frappe.json.loads('[]')

def get_fints_connection(self):
"""Get the FinTS Connection object.
Expand Down Expand Up @@ -190,17 +204,7 @@ def import_fints_transactions(self, fints_import):
curr_doc.to_date
)
else:
if curr_doc.file_url:
content = get_file(curr_doc.file_url)[1]
# Check content hash for file manipulations
if curr_doc.file_hash == get_content_hash(content):
tansactions = frappe.json.loads(
content
)
else:
raise ValueError('File hash does not match')
else:
tansactions = frappe.json.loads("[]")
tansactions = self.get_fints_import_file_content(curr_doc)

if(len(tansactions) == 0):
frappe.msgprint(_("No transaction found"))
Expand Down Expand Up @@ -268,7 +272,6 @@ def import_fints_transactions(self, fints_import):
"assignment": auto_assignment
}
except Exception as e:
self.interactive.close_progress_realtime()
frappe.throw(_(
"Error parsing transactions<br>{0}"
).format(str(e)), frappe.get_traceback())
Expand Down

0 comments on commit 5e7b83b

Please sign in to comment.