-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.brreg.js
145 lines (128 loc) · 5.11 KB
/
jquery.brreg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*!
* jQuery Plugin: bbreg Plugin (Norwegian Company lookups)
* https://github.com/gitleif/brregPlugin/
*
* Copyright (c) 2018-2020, Leif Nesheim and Systemsmia DA http://www.systemsmia.no/
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* Usage:
* getCompanies(request, callback) -> Returns to callback all companys matching request
* getCompany(organizationid, callback) -> Get all registered information about one single company, defined by organizationid
* getCompanyEHF(organizationid, callback) -> Get EHF information about one single comapny, defined by organizationid
*
*
* Author: [email protected]
* Version: 1.0
* Date: 02/11/2018
*/
// Create an immediately invoked functional expression to wrap our code
(function()
{
// Added 24042020 -> IE->if StartsWith not exist
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}
// Define our constructor
this.brregPlugin = function()
{
// Define option defaults
//filter:"startswith(navn,'%req%')"
var defaults = {
size: 30,
filter:"%req%"
};
// Create options by extending defaults with the passed in arugments
if (typeof arguments[0] !== 'undefined')
{
if (arguments[0] && typeof arguments[0] === "object") {
this.options = extendDefaults(defaults, arguments[0]);
}
}
else
{
this.options = defaults;
}
// This returns all companies matching the filter to the callback func
this.getCompanies = function(request, callback)
{
var $filter = ("?navn=" + this.options.filter.replace("%req%", encodeURIComponent(request)) + "&size=" +this.options.size);
var obj = $.getJSON( "https://data.brreg.no/enhetsregisteret/api/enheter" + $filter,
function(jsonres)
{
var ret = [];
if(typeof jsonres._embedded !== 'undefined')
{
for (var i = 0; i < jsonres._embedded.enheter.length; i++)
{
var enhet = jsonres._embedded.enheter[i];
if(enhet.underAvvikling==false && enhet.navn.startsWith(request.toUpperCase())) // Slik at ein kun får opp firma som er aktive
{
ret.push({"label":fixPublisherName(enhet.navn), "id":enhet.organisasjonsnummer});
}
}
}
callback(ret);
});
}
// This returns all the available company data from brreg about the
// requested company.
this.getCompany = function(organizationID, callback)
{
var obj = $.getJSON( "https://data.brreg.no/enhetsregisteret/api/enheter/" + encodeURIComponent(organizationID),
function(jsonres)
{
if(jsonres!=null || jsonres!=="undefined")
{
// Change the uppertext style
jsonres.navn = fixPublisherName(jsonres.navn);
if(jsonres.forretningsadresse===null || jsonres.forretningsadresse==="undefined")
{
// Init som data if it does not exist
jsonres.forretningsadresse = {adresse:null,kommune:null, kommunenummer:null, land:null, landkode:null,postnummer:null, poststed:null}
}
callback(jsonres)
}
else
{
callback(null)
}
});
}
this.getCompanyEHF = function(organizationID, callback)
{
if(organizationID.toString().length==9 && isNaN(parseFloat(organizationID))==false)
{
//$.getJSON('https://hotell.difi.no/api/json/difi/elma/capabilities?query=' + encodeURIComponent(organizationID),
$.getJSON('https://hotell.difi.no/api/json/difi/elma/participants?PEPPOLBIS_3_0_BILLING_01_UBL=Ja&query=' + encodeURIComponent(organizationID),
function(jsonres)
{
// Std settings
callback((extendDefaults({ehf_invoice_2:false,ehf_creditnote_2:false}, jsonres.entries[0])));
return true;
});
};
}
}
// Utility method to extend defaults with user options
function extendDefaults(source, properties) {
var property;
for (property in properties) {
if (properties.hasOwnProperty(property)) {
source[property] = properties[property];
}
}
return source;
}
function capitalizeFirstLetter(string)
{
return(string.toLowerCase().split(' ').map(function(s){ return s.charAt(0).toUpperCase() + s.substring(1)}).join(' '));
}
function fixPublisherName(string)
{
return(decodeURI(capitalizeFirstLetter(string)));
}
}());