Hi,
I’m trying to generate a custom file format based on the ASCII charset. If anyone has worked with JS Blobs or tried to generate custom file formats, kindly share your method.
Thanks!
What do you want to achieve?
Pass child table rows into an array and download a .txt file for it. Basically I work with Australian banks and they mostly accept aba file formats. I’ve worked out the format requisites, only problem is generating and downloading the file itself
My current code:
frappe.ui.form.on('ABA File Generator', {
genfile:function(frm) {
var rowright = "000-000000000000HH HHHH HHHHHHHH00000000";
var claimref = "";
var claimlen = 0;
var claimrepeat = 0;
var claimf = "";
var acctitle = "";
var acctitlelen = 0;
var acctitlerepeat = 0;
var acctitlef= "";
var amountb = "";
var amountlen = 0;
var amountrepeat = 0;
var amountfinal = "";
var amountsymrem = "";
var fifty = "50";
var singlespace = " ";
var singlezero = "0";
var bsbnl1 = 0;
var bsbnl2 = 0;
var bsbnlr = "";
var bsbnr = "";
var bsbnf = "";
var accnolen = 0;
var accnol = "";
var accnof = "";
var accnolr = "";
var one = "1";
var newline = "\n";
var headleft = "0 01NAB HH HHHH Payment 000000HH HHHH NDIS";
var headrightunformatted = frm.doc.today_date;
var headrightformatted = "";
//headrightformatted = headrightunformatted.toString();
var bar = '' + headrightunformatted;
var header = headleft + headrightformatted + " ";
var rowwise = "";
var rowadd = "";
frappe.call({
method:"frappe.client.get_list",
args:{
doctype:"NDIS Claims",
filters: [
["supportsdeliveredfrom","<=", frm.doc.from_date],["supportsdeliveredto","<=", frm.doc.to_date] // You can set any filter you want
],
fields:["claimreference","claimreferencelength","accountnamelength","accountnumberlength","amountlength","bsbnlength","account_name","account_number","bsb","amount","bank_state_branch_number"] // you can fetch as many fields as you want separated by a comma
},
callback: function (response) {
if (response.message) {
$.each(response.message, function(i,d) { // row can be anything, it is merely a name
var child_add = cur_frm.add_child("claims"); // child_add can be anything
child_add.bsbn = d.bank_state_branch_number;
child_add.accno = d.account_number;
child_add.amount = d.amount;
child_add.receiver = d.account_name;
child_add.lodgement_reference = d.claimreference;
child_add.claimreferencelength = d.claimreferencelength;
child_add.accountnamelength = d.accountnamelength;
child_add.accountnumberlength = d.accountnumberlength;
child_add.amountlength = d.amountlength;
child_add.bsbnlength = d.bsbnlength;
refresh_field("claims");
bsbnr = d.bank_state_branch_number;
bsbnl1 = 7 - d.bsbnlength;
bsbnlr = " ".repeat(bsbnl1);
bsbnf = bsbnr + bsbnlr;
accnol = d.account_number;
accnolen = 9 - d.accountnumberlength;
accnolr = " ".repeat(accnolen);
accnof = accnolr + accnol + singlespace + fifty;
amountb = d.amount;
amountlen = 11 - d.amountlength;
amountrepeat = singlezero.repeat(amountlen);
amountsymrem = amountb.replace(".", "");
amountfinal = amountrepeat + amountsymrem;
acctitle = d.account_name;
acctitlelen = 32 - d.accountnamelength;
acctitlerepeat = " ".repeat(acctitlelen);
acctitlef = acctitle + acctitlerepeat;
claimref = d.claimreference;
claimlen = 18 - d.claimreferencelength;
claimrepeat = " ".repeat(claimlen);
claimf = claimref + claimrepeat;
rowwise += newline + one + bsbnf + accnof + amountfinal + acctitlef + claimf + rowright;
console.log(rowwise);
frm.refresh_fields("claims");// not really necessary just so you can view the message in the console to check for possible errors
});
rowadd = header + rowwise;
frm.set_value("test2",rowadd);
refresh_field("test2");
}
}
});
var final = rowadd;
var blob1 = new Blob ([frm.doc.test2], {type: 'text/txt'});
msgprint(frm.doc.test2);
var text = "hello world",
rowf = ([final,text]);
var blob = new Blob([blob1], { type: 'text/aba' }),
anchor = document.createElement('a');
anchor.download = "hello.aba";
anchor.href = (window.webkitURL || window.URL).createObjectURL(blob);
anchor.dataset.downloadurl = ['text/plain', anchor.download, anchor.href].join(':');
anchor.click();
}
});
I use server side functions like frappe.render_template to generate those files.
strBody is something like
<Price>
<PriceAmount currencyID="TRY">{{docCurrentLine.rate}}</PriceAmount>
</Price>
Then you can force to download those files like
frappe.local.response.filename = strDocName + ".xml"
frappe.local.response.filecontent = strDocXML
frappe.local.response.type = "download"
I’ll try this. Thanks for your help!