Skip to content

Instantly share code, notes, and snippets.

@Alexander-Pop
Created May 22, 2019 19:28
Show Gist options
  • Save Alexander-Pop/eba1747d673f5863570f4d83f4ba1363 to your computer and use it in GitHub Desktop.
Save Alexander-Pop/eba1747d673f5863570f4d83f4ba1363 to your computer and use it in GitHub Desktop.
Generates tax rate table for magento import
Tax rate: <input id='taxrate' /> <input type='button' id='selecttable' value='Select content' />
<div id='result'>
</div>
<script>
var fields = [
'Code', 'Country', 'State', 'Zip/Post Code',
'Rate', 'Zip/Post is Range', 'Range From',
'Range To default'
];
var template = [
'', // name (idx 0)
'', // country (idx 1)
'*', // State, every
'', // zip code, just empty
0, // percentage (idx 4)
'', '','' // ranges
];
var cc = ['AD','AE','AF','AG','AI','AL','AM','AN','AO','AQ','AR','AS','AT','AU','AW','AX','AZ','BA','BB','BD','BE','BF','BG','BH','BI','BJ','BL','BM','BN','BO','BR','BS','BT','BV','BW','BY','BZ','CA','CC','CD','CF','CG','CH','CI','CK','CL','CM','CN','CO','CR','CU','CV','CX','CY','CZ','DE','DJ','DK','DM','DO','DZ','EC','EE','EG','EH','ER','ES','ET','FI','FJ','FK','FM','FO','FR','GA','GB','GD','GE','GF','GG','GH','GI','GL','GM','GN','GP','GQ','GR','GS','GT','GU','GW','GY','HK','HM','HN','HR','HT','HU','ID','IE','IL','IM','IN','IO','IQ','IR','IS','IT','JE','JM','JO','JP','KE','KG','KH','KI','KM','KN','KP','KR','KW','KY','KZ','LA','LB','LC','LI','LK','LR','LS','LT','LU','LV','LY','MA','MC','MD','ME','MF','MG','MH','MK','ML','MM','MN','MO','MP','MQ','MR','MS','MT','MU','MV','MW','MX','MY','MZ','NA','NC','NE','NF','NG','NI','NL','NO','NP','NR','NU','NZ','OM','PA','PE','PF','PG','PH','PK','PL','PM','PN','PR','PS','PT','PW','PY','QA','RE','RO','RS','RU','RW','SA','SB','SC','SD','SE','SG','SH','SI','SJ','SK','SL','SM','SN','SO','SR','ST','SV','SY','SZ','TC','TD','TF','TG','TH','TJ','TK','TL','TM','TN','TO','TR','TT','TV','TW','TZ','UA','UG','UM','US','UY','UZ','VA','VC','VE','VG','VI','VN','VU','WF','WS','YE','YT','ZA','ZM'
];
function calculate(i) {
var percentage = i.value
var rows = [];
var table = [];
rows.push(fields.slice());
cc.forEach(function (cci) {
var n = template.slice();
n[0] = percentage + "% " + cci;
n[1] = cci;
n[4] = percentage;
rows.push(n);
});
rows.forEach(function (row) {
table.push('<tr><td>' + row.join('</td><td>') + '</td></tr>');
});
document.getElementById('result').innerHTML = '<table>' + table.join('') + '</table>';
}
function selectElementContents(el) {
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
range.selectNode(el);
sel.addRange(range);
}
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
}
}
document.getElementById('taxrate').onkeyup = function () {
calculate(this);
};
document.getElementById('selecttable').onclick = function () {
selectElementContents(document.getElementById('result'));
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment