bkper-js 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ /**
3
+ * Bkper REST API Node client.
4
+ *
5
+ * Learn more at https://bkper.com/docs
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.Permission = exports.DecimalSeparator = exports.AccountType = exports.Periodicity = exports.Connection = exports.Integration = exports.User = exports.TransactionIterator = exports.Transaction = exports.Group = exports.File = exports.Collection = exports.Book = exports.Account = exports.Bkper = exports.Amount = exports.App = void 0;
11
+ var App_1 = require("./model/App");
12
+ Object.defineProperty(exports, "App", { enumerable: true, get: function () { return App_1.App; } });
13
+ var Amount_1 = require("./model/Amount");
14
+ Object.defineProperty(exports, "Amount", { enumerable: true, get: function () { return Amount_1.Amount; } });
15
+ var Bkper_1 = require("./model/Bkper");
16
+ Object.defineProperty(exports, "Bkper", { enumerable: true, get: function () { return Bkper_1.Bkper; } });
17
+ var Account_1 = require("./model/Account");
18
+ Object.defineProperty(exports, "Account", { enumerable: true, get: function () { return Account_1.Account; } });
19
+ var Book_1 = require("./model/Book");
20
+ Object.defineProperty(exports, "Book", { enumerable: true, get: function () { return Book_1.Book; } });
21
+ var Collection_1 = require("./model/Collection");
22
+ Object.defineProperty(exports, "Collection", { enumerable: true, get: function () { return Collection_1.Collection; } });
23
+ var File_1 = require("./model/File");
24
+ Object.defineProperty(exports, "File", { enumerable: true, get: function () { return File_1.File; } });
25
+ var Group_1 = require("./model/Group");
26
+ Object.defineProperty(exports, "Group", { enumerable: true, get: function () { return Group_1.Group; } });
27
+ var Transaction_1 = require("./model/Transaction");
28
+ Object.defineProperty(exports, "Transaction", { enumerable: true, get: function () { return Transaction_1.Transaction; } });
29
+ var TransactionIterator_1 = require("./model/TransactionIterator");
30
+ Object.defineProperty(exports, "TransactionIterator", { enumerable: true, get: function () { return TransactionIterator_1.TransactionIterator; } });
31
+ var User_1 = require("./model/User");
32
+ Object.defineProperty(exports, "User", { enumerable: true, get: function () { return User_1.User; } });
33
+ var Integration_1 = require("./model/Integration");
34
+ Object.defineProperty(exports, "Integration", { enumerable: true, get: function () { return Integration_1.Integration; } });
35
+ var Connection_1 = require("./model/Connection");
36
+ Object.defineProperty(exports, "Connection", { enumerable: true, get: function () { return Connection_1.Connection; } });
37
+ var Enums_1 = require("./model/Enums");
38
+ Object.defineProperty(exports, "Periodicity", { enumerable: true, get: function () { return Enums_1.Periodicity; } });
39
+ Object.defineProperty(exports, "AccountType", { enumerable: true, get: function () { return Enums_1.AccountType; } });
40
+ Object.defineProperty(exports, "DecimalSeparator", { enumerable: true, get: function () { return Enums_1.DecimalSeparator; } });
41
+ Object.defineProperty(exports, "Permission", { enumerable: true, get: function () { return Enums_1.Permission; } });
42
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,385 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.Account = void 0;
36
+ const AccountService = __importStar(require("../service/account-service"));
37
+ const GroupService = __importStar(require("../service/group-service"));
38
+ const Group_1 = require("./Group");
39
+ const utils_1 = require("../utils");
40
+ const Amount_1 = require("./Amount");
41
+ const Utils = __importStar(require("../utils"));
42
+ /**
43
+ *
44
+ * This class defines an [Account](https://en.wikipedia.org/wiki/Account_(bookkeeping)) of a [[Book]].
45
+ *
46
+ * It mantains a balance of all amount [credited and debited](http://en.wikipedia.org/wiki/Debits_and_credits) in it by [[Transactions]].
47
+ *
48
+ * An Account can be grouped by [[Groups]].
49
+ *
50
+ * @public
51
+ */
52
+ class Account {
53
+ /**
54
+ *
55
+ * @returns The wrapped plain json object
56
+ */
57
+ json() {
58
+ return this.wrapped;
59
+ }
60
+ /**
61
+ * Gets the account internal id.
62
+ */
63
+ getId() {
64
+ return this.wrapped.id;
65
+ }
66
+ /**
67
+ * Gets the account name.
68
+ */
69
+ getName() {
70
+ return this.wrapped.name;
71
+ }
72
+ /**
73
+ *
74
+ * Sets the name of the Account.
75
+ *
76
+ * @returns This Account, for chainning.
77
+ */
78
+ setName(name) {
79
+ this.wrapped.name = name;
80
+ return this;
81
+ }
82
+ /**
83
+ * @returns The name of this account without spaces or special characters.
84
+ */
85
+ getNormalizedName() {
86
+ if (this.wrapped.normalizedName) {
87
+ return this.wrapped.normalizedName;
88
+ }
89
+ else {
90
+ return (0, utils_1.normalizeText)(this.getName());
91
+ }
92
+ }
93
+ /**
94
+ * @returns The type for of this account.
95
+ */
96
+ getType() {
97
+ return this.wrapped.type;
98
+ }
99
+ /**
100
+ *
101
+ * Sets the type of the Account.
102
+ *
103
+ * @returns This Account, for chainning
104
+ */
105
+ setType(type) {
106
+ this.wrapped.type = type;
107
+ return this;
108
+ }
109
+ /**
110
+ * Gets the custom properties stored in this Account.
111
+ */
112
+ getProperties() {
113
+ return this.wrapped.properties != null ? Object.assign({}, this.wrapped.properties) : {};
114
+ }
115
+ /**
116
+ * Sets the custom properties of the Account
117
+ *
118
+ * @param properties - Object with key/value pair properties
119
+ *
120
+ * @returns This Account, for chainning.
121
+ */
122
+ setProperties(properties) {
123
+ this.wrapped.properties = Object.assign({}, properties);
124
+ return this;
125
+ }
126
+ /**
127
+ * Gets the property value for given keys. First property found will be retrieved
128
+ *
129
+ * @param keys - The property key
130
+ */
131
+ getProperty(...keys) {
132
+ for (let index = 0; index < keys.length; index++) {
133
+ const key = keys[index];
134
+ let value = this.wrapped.properties != null ? this.wrapped.properties[key] : null;
135
+ if (value != null && value.trim() != '') {
136
+ return value;
137
+ }
138
+ }
139
+ return null;
140
+ }
141
+ /**
142
+ * Sets a custom property in the Account.
143
+ *
144
+ * @param key - The property key
145
+ * @param value - The property value
146
+ *
147
+ * @returns This Account, for chainning.
148
+ */
149
+ setProperty(key, value) {
150
+ if (key == null || key.trim() == '') {
151
+ return this;
152
+ }
153
+ if (this.wrapped.properties == null) {
154
+ this.wrapped.properties = {};
155
+ }
156
+ this.wrapped.properties[key] = value;
157
+ return this;
158
+ }
159
+ /**
160
+ * Delete a custom property
161
+ *
162
+ * @param key - The property key
163
+ *
164
+ * @returns This Account, for chainning.
165
+ */
166
+ deleteProperty(key) {
167
+ this.setProperty(key, null);
168
+ return this;
169
+ }
170
+ /**
171
+ * Gets the balance based on credit nature of this Account.
172
+ * @deprecated Use `Book.getBalancesReport` instead.
173
+ * @returns The balance of this account.
174
+ */
175
+ getBalance() {
176
+ var balance = new Amount_1.Amount('0');
177
+ if (this.wrapped.balance != null) {
178
+ balance = (0, utils_1.round)(this.wrapped.balance, this.book.getFractionDigits());
179
+ }
180
+ return balance;
181
+ }
182
+ /**
183
+ * Gets the raw balance, no matter credit nature of this Account.
184
+ * @deprecated Use `Book.getBalancesReport` instead.
185
+ * @returns The balance of this account.
186
+ */
187
+ getBalanceRaw() {
188
+ var balance = new Amount_1.Amount('0');
189
+ if (this.wrapped.balance != null) {
190
+ balance = (0, utils_1.round)(this.wrapped.balance, this.book.getFractionDigits());
191
+ }
192
+ return balance;
193
+ }
194
+ /**
195
+ * Tell if this account is archived.
196
+ */
197
+ isArchived() {
198
+ return this.wrapped.archived;
199
+ }
200
+ /**
201
+ * Set account archived/unarchived.
202
+ *
203
+ * @returns This Account, for chainning.
204
+ */
205
+ setArchived(archived) {
206
+ this.wrapped.archived = archived;
207
+ return this;
208
+ }
209
+ /**
210
+ * Tell if the Account has any transaction already posted.
211
+ *
212
+ * Accounts with transaction posted, even with zero balance, can only be archived.
213
+ */
214
+ hasTransactionPosted() {
215
+ return this.wrapped.hasTransactionPosted;
216
+ }
217
+ /**
218
+ *
219
+ * Tell if the account is permanent.
220
+ *
221
+ * Permanent Accounts are the ones which final balance is relevant and keep its balances over time.
222
+ *
223
+ * They are also called [Real Accounts](http://en.wikipedia.org/wiki/Account_(accountancy)#Based_on_periodicity_of_flow)
224
+ *
225
+ * Usually represents assets or tangibles, capable of being perceived by the senses or the mind, like bank accounts, money, debts and so on.
226
+ *
227
+ * @returns True if its a permanent Account
228
+ */
229
+ isPermanent() {
230
+ return this.wrapped.permanent;
231
+ }
232
+ /**
233
+ * Tell if the account has a Credit nature or Debit otherwise
234
+ *
235
+ * Credit accounts are just for representation purposes. It increase or decrease the absolute balance. It doesn't affect the overall balance or the behavior of the system.
236
+ *
237
+ * The absolute balance of credit accounts increase when it participate as a credit/origin in a transaction. Its usually for Accounts that increase the balance of the assets, like revenue accounts.
238
+ *
239
+ * ```
240
+ * Crediting a credit
241
+ * Thus ---------------------> account increases its absolute balance
242
+ * Debiting a debit
243
+ *
244
+ *
245
+ * Debiting a credit
246
+ * Thus ---------------------> account decreases its absolute balance
247
+ * Crediting a debit
248
+ * ```
249
+ *
250
+ * As a rule of thumb, and for simple understanding, almost all accounts are Debit nature (NOT credit), except the ones that "offers" amount for the books, like revenue accounts.
251
+ */
252
+ isCredit() {
253
+ return this.wrapped.credit;
254
+ }
255
+ /**
256
+ * Get the [[Groups]] of this account.
257
+ */
258
+ getGroups() {
259
+ return __awaiter(this, void 0, void 0, function* () {
260
+ let groups = yield GroupService.getGroupsByAccountId(this.book.getId(), this.getId());
261
+ let groupsObj = Utils.wrapObjects(new Group_1.Group(), groups);
262
+ for (const group of groupsObj) {
263
+ group.book = this.book;
264
+ }
265
+ return groupsObj;
266
+ });
267
+ }
268
+ /**
269
+ * Sets the groups of the Account.
270
+ *
271
+ * @returns This Account, for chainning.
272
+ */
273
+ setGroups(groups) {
274
+ this.wrapped.groups = null;
275
+ if (groups != null) {
276
+ groups.forEach(group => this.addGroup(group));
277
+ }
278
+ return this;
279
+ }
280
+ /**
281
+ * Add a group to the Account.
282
+ *
283
+ * @returns This Account, for chainning.
284
+ */
285
+ addGroup(group) {
286
+ if (this.wrapped.groups == null) {
287
+ this.wrapped.groups = [];
288
+ }
289
+ if (group instanceof Group_1.Group) {
290
+ this.wrapped.groups.push(group.json());
291
+ }
292
+ else {
293
+ this.wrapped.groups.push(group);
294
+ }
295
+ return this;
296
+ }
297
+ /**
298
+ * Remove a group from the Account.
299
+ */
300
+ removeGroup(group) {
301
+ return __awaiter(this, void 0, void 0, function* () {
302
+ if (this.wrapped.groups != null) {
303
+ let groupObject = null;
304
+ if (group instanceof Group_1.Group) {
305
+ groupObject = group;
306
+ }
307
+ else if (typeof group == "string") {
308
+ groupObject = yield this.book.getGroup(group);
309
+ }
310
+ if (groupObject) {
311
+ for (let i = 0; i < this.wrapped.groups.length; i++) {
312
+ const groupId = this.wrapped.groups[i];
313
+ if (groupId == groupObject.getId()) {
314
+ this.wrapped.groups.splice(i, 1);
315
+ }
316
+ }
317
+ }
318
+ }
319
+ return this;
320
+ });
321
+ }
322
+ /**
323
+ * Tell if this account is in the [[Group]]
324
+ *
325
+ * @param group - The Group name, id or object
326
+ */
327
+ isInGroup(group) {
328
+ return __awaiter(this, void 0, void 0, function* () {
329
+ if (group == null) {
330
+ return false;
331
+ }
332
+ //Group object
333
+ if (group instanceof Group_1.Group) {
334
+ return this.isInGroupObject_(group);
335
+ }
336
+ //id or name
337
+ var foundGroup = yield this.book.getGroup(group);
338
+ if (foundGroup == null) {
339
+ return false;
340
+ }
341
+ return this.isInGroupObject_(foundGroup);
342
+ });
343
+ }
344
+ /** @internal */
345
+ isInGroupObject_(group) {
346
+ if (this.wrapped.groups == null) {
347
+ return false;
348
+ }
349
+ for (var i = 0; i < this.wrapped.groups.length; i++) {
350
+ if (this.wrapped.groups[i] == group.getId()) {
351
+ return true;
352
+ }
353
+ }
354
+ return false;
355
+ }
356
+ /**
357
+ * Perform create new account.
358
+ */
359
+ create() {
360
+ return __awaiter(this, void 0, void 0, function* () {
361
+ this.wrapped = yield AccountService.createAccount(this.book.getId(), this.wrapped);
362
+ return this;
363
+ });
364
+ }
365
+ /**
366
+ * Perform update account, applying pending changes.
367
+ */
368
+ update() {
369
+ return __awaiter(this, void 0, void 0, function* () {
370
+ this.wrapped = yield AccountService.updateAccount(this.book.getId(), this.wrapped);
371
+ return this;
372
+ });
373
+ }
374
+ /**
375
+ * Perform delete account.
376
+ */
377
+ remove() {
378
+ return __awaiter(this, void 0, void 0, function* () {
379
+ this.wrapped = yield AccountService.deleteAccount(this.book.getId(), this.wrapped);
380
+ return this;
381
+ });
382
+ }
383
+ }
384
+ exports.Account = Account;
385
+ //# sourceMappingURL=Account.js.map