js-klikvet-logic-worker 2.1.5 → 2.3.18
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/.vscode/launch.json +17 -0
- package/index.mjs +20 -0
- package/package.json +12 -10
- package/worker.mjs +1353 -0
- package/index.js +0 -4
- package/worker.js +0 -801
package/worker.js
DELETED
|
@@ -1,801 +0,0 @@
|
|
|
1
|
-
//@ts-check
|
|
2
|
-
|
|
3
|
-
const DEFAULT_SERVICE_NAME = 'klikvet'
|
|
4
|
-
|
|
5
|
-
const Helpers = require('cry-helpers');
|
|
6
|
-
const logic = require('cry-klikvet-logic');
|
|
7
|
-
const { Worker, Client } = require('cry-ebus2')
|
|
8
|
-
/** @type {any} */
|
|
9
|
-
const assert = require('assert');
|
|
10
|
-
const clonedeep = require('lodash.clonedeep');
|
|
11
|
-
const Mongo = require('cry-db').Mongo;
|
|
12
|
-
const packageJson = require('./package.json');
|
|
13
|
-
|
|
14
|
-
let client = null
|
|
15
|
-
|
|
16
|
-
let log = new Helpers.Log(['KLIKVET'])
|
|
17
|
-
let mongo = new Mongo(null)
|
|
18
|
-
|
|
19
|
-
/** @typedef {import('cry-klikvet-logic').Blagovnica} Blagovnica */
|
|
20
|
-
/** @typedef {import('cry-klikvet-logic').Nastavitve} Nastavitve */
|
|
21
|
-
/** @typedef {import('cry-klikvet-logic').ZalogaDbAccessor} ZalogaDbAccessor */
|
|
22
|
-
/** @typedef {import('cry-klikvet-logic').Racun} Racun */
|
|
23
|
-
|
|
24
|
-
function round(num, dec = 2)
|
|
25
|
-
{
|
|
26
|
-
return Number(num.toFixed(dec))
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
*
|
|
31
|
-
* @param {string} tenant
|
|
32
|
-
* @returns {Promise<Nastavitve>}
|
|
33
|
-
*/
|
|
34
|
-
async function dbNastavitve(tenant) {
|
|
35
|
-
mongo.setDb(tenant)
|
|
36
|
-
return (/** @type {Nastavitve} */ (await mongo.findOne("nastavitve",{})))
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async function dbFindById(db, collection, _id)
|
|
40
|
-
{
|
|
41
|
-
mongo.setDb(db)
|
|
42
|
-
return await mongo.findById(collection,_id)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async function dbFind(db, collection, query) {
|
|
46
|
-
mongo.setDb(db)
|
|
47
|
-
return await mongo.find(collection,query)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async function dbUpdateById(db, collection, _id, update) {
|
|
51
|
-
mongo.setDb(db)
|
|
52
|
-
return await mongo.updateOne(collection, { _id },update)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async function razknjiziBlagovnico(_id, tenant)
|
|
56
|
-
{
|
|
57
|
-
let nastavitve = await dbNastavitve(tenant);
|
|
58
|
-
mongo.setDb(tenant)
|
|
59
|
-
await logic.zakljuciBlagovnico(_id, mongo);
|
|
60
|
-
|
|
61
|
-
/** @type {Blagovnica}} */
|
|
62
|
-
let blagovnica = (/** @type {Blagovnica} */ await dbFindById(tenant, "blagovnice", _id))
|
|
63
|
-
if (!blagovnica) throw new Error(`ne morem razknjižiti blagovnice, ki je ni: ${_id}, tenant ${tenant}`)
|
|
64
|
-
|
|
65
|
-
mongo.setDb(tenant)
|
|
66
|
-
await logic.spremeniZalogo.spremeniZalogoNaPodlagiBlagovnice(blagovnica, nastavitve, mongo)
|
|
67
|
-
console.log('poknjižena');
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async function registerAllInvoices(data)
|
|
72
|
-
{
|
|
73
|
-
assert(data.tenant, "must specify tenant")
|
|
74
|
-
|
|
75
|
-
let err = null;
|
|
76
|
-
let napake = new Set()
|
|
77
|
-
try {
|
|
78
|
-
|
|
79
|
-
err = "Podsistem db ni dosegljiv."
|
|
80
|
-
|
|
81
|
-
mongo.setDb(data.tenant)
|
|
82
|
-
let myquery = { eor: null, leto: { $gte: 2020 } };
|
|
83
|
-
let all = await mongo.find("racuni", myquery, { project: { _id: 1 }, limit: 1000, })
|
|
84
|
-
err = "Napaka pri potrjevanju računov."
|
|
85
|
-
|
|
86
|
-
let n = 0;
|
|
87
|
-
try {
|
|
88
|
-
for await (let r of all) {
|
|
89
|
-
let res = await registerInvoice({ tenant: data.tenant, _id: r._id.toString() }, true);
|
|
90
|
-
if (res.eor) n++;
|
|
91
|
-
if (res.fursError) napake.add(res.fursError)
|
|
92
|
-
}
|
|
93
|
-
} catch (e) {
|
|
94
|
-
err = e.message;
|
|
95
|
-
console.log('err',err);
|
|
96
|
-
throw new Error()
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return { nall: all.length, nok: n, napake: [...napake] };
|
|
100
|
-
|
|
101
|
-
} catch {
|
|
102
|
-
console.error('registerAllInvoices', err);
|
|
103
|
-
throw new Error(err);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
async function registerCert(db,davcnaStevilka)
|
|
108
|
-
{
|
|
109
|
-
let cert = (await client.requestPromise('db', {
|
|
110
|
-
operation: 'findOne',
|
|
111
|
-
db,
|
|
112
|
-
collection: "certifikati",
|
|
113
|
-
query: { davcnaStevilka }
|
|
114
|
-
}, null, 2000)).data;
|
|
115
|
-
|
|
116
|
-
let res = (await client.requestPromise('durs', { operation: 'add cert', data: { cert: cert.certBase64, password: cert.certPass } }, null, 2000)).data;
|
|
117
|
-
console.log('res',res);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async function registerInvoice(data, kasneje=false)
|
|
121
|
-
{
|
|
122
|
-
assert(data._id, "must specify _id of racun")
|
|
123
|
-
assert(data.tenant, "must specify tenant")
|
|
124
|
-
let timeoutError = "";
|
|
125
|
-
try {
|
|
126
|
-
// poišči račun
|
|
127
|
-
timeoutError = "Db podsistem se ni odzval."
|
|
128
|
-
let racun = await dbFindById(data.tenant,"racuni",data._id)
|
|
129
|
-
let blagajna = await dbFindById(data.tenant, "blagajne", racun.blagajna_id)
|
|
130
|
-
if (!blagajna) throw new Error("cannot locate blagajna: "+racun.blagajna_id)
|
|
131
|
-
let poslovalnica = await dbFindById(data.tenant, "poslovalnice", blagajna.poslovalnica_id)
|
|
132
|
-
if (!blagajna) throw new Error("cannot locate poslovalnica: "+racun.blagajna_id)
|
|
133
|
-
let veterina = await dbFindById(data.tenant, "nastavitve", poslovalnica.veterina_id)
|
|
134
|
-
if (!veterina) throw new Error("cannot locate poslovalnica: " + poslovalnica.veterina_id)
|
|
135
|
-
if (!racun.izdalDs) throw new Error("Uporabnik verjetno nima vpisane davčne številke.")
|
|
136
|
-
|
|
137
|
-
// če gre za popravek računa, poveži izvorni račun na ta popravek
|
|
138
|
-
if (racun.predhodniRacun && racun.predhodniRacun._id) {
|
|
139
|
-
dbUpdateById(data.tenant, "racuni", racun.predhodniRacun._id.toString(), {
|
|
140
|
-
naslednjiRacun: {
|
|
141
|
-
_id: data._id.toString(),
|
|
142
|
-
datum: racun.datum,
|
|
143
|
-
stevilka: racun.stevilka,
|
|
144
|
-
}
|
|
145
|
-
})
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
let TaxNumber = (veterina.dsCertifikata || veterina.davcnaStevilka)
|
|
149
|
-
TaxNumber = parseInt(TaxNumber.toString().match(/\d+/));
|
|
150
|
-
|
|
151
|
-
let Invoice = {
|
|
152
|
-
TaxNumber,
|
|
153
|
-
IssueDateTime: new Date(racun.datum),
|
|
154
|
-
NumberingStructure: "B",
|
|
155
|
-
InvoiceIdentifier: {
|
|
156
|
-
BusinessPremiseID: poslovalnica.koda,
|
|
157
|
-
ElectronicDeviceID: blagajna.nazivZaIzpis,
|
|
158
|
-
InvoiceNumber: racun.stRacuna.toString(),
|
|
159
|
-
},
|
|
160
|
-
InvoiceAmount: round(racun.mpc),
|
|
161
|
-
PaymentAmount: round(racun.mpc),
|
|
162
|
-
TaxesPerSeller: [{
|
|
163
|
-
VAT: racun.davki.map(d => {
|
|
164
|
-
return {
|
|
165
|
-
TaxRate: round(d.dst * 100),
|
|
166
|
-
TaxableAmount: round(d.osnova),
|
|
167
|
-
TaxAmount: round(d.davek),
|
|
168
|
-
}
|
|
169
|
-
})
|
|
170
|
-
}],
|
|
171
|
-
OperatorTaxNumber: Number(racun.izdalDs),
|
|
172
|
-
//ProtectedID: null,
|
|
173
|
-
SubsequentSubmit: kasneje,
|
|
174
|
-
// ReferenceInvoice: [{
|
|
175
|
-
// ReferenceInvoiceIdentifier: {
|
|
176
|
-
// BusinessPremiseID: "36CF",
|
|
177
|
-
// ElectronicDeviceID: "BLAG2",
|
|
178
|
-
// InvoiceNumber: "144"
|
|
179
|
-
// },
|
|
180
|
-
// ReferenceInvoiceIssueDateTime: new Date()
|
|
181
|
-
// }],
|
|
182
|
-
};
|
|
183
|
-
if (racun.predhodniRacun) {
|
|
184
|
-
let s = racun.predhodniRacun.stevilka.split("-");
|
|
185
|
-
Invoice.ReferenceInvoice = [{
|
|
186
|
-
ReferenceInvoiceIdentifier: {
|
|
187
|
-
BusinessPremiseID: s[2],
|
|
188
|
-
ElectronicDeviceID: s[1],
|
|
189
|
-
InvoiceNumber: s[0],
|
|
190
|
-
},
|
|
191
|
-
ReferenceInvoiceIssueDateTime: racun.predhodniRacun.datum,
|
|
192
|
-
}];
|
|
193
|
-
}
|
|
194
|
-
let DursEnv = veterina.okolje;
|
|
195
|
-
|
|
196
|
-
console.dir(Invoice, {depth:10});
|
|
197
|
-
|
|
198
|
-
// davčno potrdi račun
|
|
199
|
-
let furs={}, napaka=""
|
|
200
|
-
try {
|
|
201
|
-
|
|
202
|
-
timeoutError = "FURS podsistem se ni odzval."
|
|
203
|
-
furs = (await client.requestPromise('durs', {
|
|
204
|
-
operation: 'register invoice',
|
|
205
|
-
data: { Invoice, DursEnv, }
|
|
206
|
-
}, null, 4000)).data.Durs;
|
|
207
|
-
|
|
208
|
-
console.log('furs',furs);
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
if (furs.error && furs.error.match(/no such file/g)) {
|
|
212
|
-
console.warn("no certificate, register it");
|
|
213
|
-
// register cert
|
|
214
|
-
timeoutError = "Samodejna registracija digitalnega potrdila furs spodletela."
|
|
215
|
-
|
|
216
|
-
await registerCert(data.tenant, Invoice.TaxNumber);
|
|
217
|
-
// re-submit invoice
|
|
218
|
-
furs = (await client.requestPromise('durs', {
|
|
219
|
-
operation: 'register invoice',
|
|
220
|
-
data: { Invoice, DursEnv, }
|
|
221
|
-
}, null, 4000)).data.Durs;
|
|
222
|
-
}
|
|
223
|
-
if (furs.error && furs.error.match(/SSL alert number 40/gi)) {
|
|
224
|
-
console.warn("certifikat potekel");
|
|
225
|
-
timeoutError = "Je digitalno potrdilo FURS pretečeno?"
|
|
226
|
-
throw new Error(furs.error)
|
|
227
|
-
}
|
|
228
|
-
if (furs.error && furs.error.match(/no such file/g)) {
|
|
229
|
-
console.warn("no certificate, register it");
|
|
230
|
-
// register cert
|
|
231
|
-
timeoutError = "Samodejna registracija digitalnega potrdila furs spodletela."
|
|
232
|
-
|
|
233
|
-
await registerCert(data.tenant, Invoice.TaxNumber);
|
|
234
|
-
// re-submit invoice
|
|
235
|
-
furs = (await client.requestPromise('durs', {
|
|
236
|
-
operation: 'register invoice',
|
|
237
|
-
data: { Invoice, DursEnv, }
|
|
238
|
-
}, null, 4000)).data.Durs;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
} catch (err) {
|
|
242
|
-
console.error("Err calling durs register.invoice", err)
|
|
243
|
-
napaka = `${timeoutError} (${err&&err.data&&err.data.message})`
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
napaka = napaka || furs.error;
|
|
247
|
-
if (!napaka && !furs.EOR) napaka = "furs sistem ni vrnil EOR"
|
|
248
|
-
let fursObj = napaka ? {
|
|
249
|
-
zoi: furs.ZOI,
|
|
250
|
-
eor: furs.EOR,
|
|
251
|
-
qr: furs.QR,
|
|
252
|
-
fursError: `furs/račun: ${napaka}`,
|
|
253
|
-
davcnoSpodletloPrvic: racun.davcnoSpodletloPrvic || new Date(),
|
|
254
|
-
davcnoSpodletloZadnjic: new Date(),
|
|
255
|
-
davcnoSpodletloPoskusov: (racun.davcnoSpodletloPoskusov||0)+1
|
|
256
|
-
} : {
|
|
257
|
-
zoi: furs.ZOI,
|
|
258
|
-
eor: furs.EOR,
|
|
259
|
-
qr: furs.QR,
|
|
260
|
-
davcnoPotrjeno: new Date(),
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// posodobi račun
|
|
264
|
-
timeoutError = "Napaka pri vpisu davčno potrjenega računa: Db podsistem se ne odziva več."
|
|
265
|
-
let updated = await dbUpdateById(data.tenant, "racuni", data._id, fursObj);
|
|
266
|
-
|
|
267
|
-
return updated;
|
|
268
|
-
|
|
269
|
-
} catch (err) {
|
|
270
|
-
|
|
271
|
-
return {
|
|
272
|
-
fursError: `furs/račun: ${err.message||err}`,
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Stornira račun" tako, da kreira nov odprtRacun z negativnimi postavkami.
|
|
279
|
-
* @param {string} db
|
|
280
|
-
* @param {string} id
|
|
281
|
-
*/
|
|
282
|
-
async function storno(db,id)
|
|
283
|
-
{
|
|
284
|
-
// poglej, če morda storno že poteka
|
|
285
|
-
|
|
286
|
-
mongo.setDb(db)
|
|
287
|
-
let obstojeci = await mongo.findOne("odprtiRacuni", { "predhodniRacun._id": id })
|
|
288
|
-
|
|
289
|
-
// (await client.requestPromise('db', {
|
|
290
|
-
// operation: 'findOne',
|
|
291
|
-
// db,
|
|
292
|
-
// collection: 'odprtiRacuni',
|
|
293
|
-
// query: { "predhodniRacun._id": id }
|
|
294
|
-
// }, null, 2000)).data;
|
|
295
|
-
|
|
296
|
-
if (obstojeci) return obstojeci;
|
|
297
|
-
|
|
298
|
-
let racun = clonedeep(await dbFindById(db, "racuni", id) );
|
|
299
|
-
|
|
300
|
-
if (!racun) throw new Error(`storno: ni računa ${id}`)
|
|
301
|
-
|
|
302
|
-
// povezi racuna
|
|
303
|
-
racun.predhodniRacun = {
|
|
304
|
-
_id: racun._id,
|
|
305
|
-
stevilka: racun.stevilka,
|
|
306
|
-
datum: racun.datum
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
// ustvari negativne postavke
|
|
310
|
-
racun.postavke.forEach(postavka => {
|
|
311
|
-
['kolicina', 'tpop', 'tnbc', 'tdav', 'tprc', 'tmpc'].forEach(f => {
|
|
312
|
-
postavka[f] = -postavka[f]
|
|
313
|
-
})
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
// ustvari negativne vsote
|
|
317
|
-
['mpc', 'prc', 'dav', 'pop', 'nbc'].forEach(f => {
|
|
318
|
-
racun[f] = -racun[f]
|
|
319
|
-
});
|
|
320
|
-
// zbrisi polja, ki jih ne sme biti na novem računu
|
|
321
|
-
["zapSt", "stRacuna", "odprl", "pripis", "izdal", "opis", "zoi", "eor", "stKopij", "stevilka"].forEach(f => {
|
|
322
|
-
delete racun[f]
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
racun.datum = new Date();
|
|
326
|
-
// tole nastavi GUI
|
|
327
|
-
racun.placila = []
|
|
328
|
-
racun.davki = []
|
|
329
|
-
|
|
330
|
-
delete racun._id;
|
|
331
|
-
|
|
332
|
-
mongo.setDb(db)
|
|
333
|
-
let odprtiRacun = mongo.insert("odprtiRacuni",racun)
|
|
334
|
-
|
|
335
|
-
// (await client.requestPromise('db', {
|
|
336
|
-
// operation: 'insert',
|
|
337
|
-
// db,
|
|
338
|
-
// collection: "odprtiRacuni",
|
|
339
|
-
// insert: racun,
|
|
340
|
-
// }, null, 2000)).data;
|
|
341
|
-
|
|
342
|
-
return odprtiRacun;
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
class KlikvetLogicWorker extends Worker {
|
|
347
|
-
constructor(opts) {
|
|
348
|
-
super(DEFAULT_SERVICE_NAME, opts)
|
|
349
|
-
mongo.emitPublishEvents(true)
|
|
350
|
-
mongo.on('publish', this.publishMsg.bind(this))
|
|
351
|
-
client = new Client({ clientChecksBrokerWithHeartbeat: false });
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* @param {import("cry-db").Types.PublishSpec} what
|
|
356
|
-
*/
|
|
357
|
-
publishMsg(what) {
|
|
358
|
-
super.publish(what.channel, what.payload)
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
async process(data) {
|
|
362
|
-
|
|
363
|
-
log.debug(`klikvet worker request of type ${typeof data}`,data)
|
|
364
|
-
|
|
365
|
-
if (data === 'ping') {
|
|
366
|
-
return 'pong';
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
if (data === 'ver') {
|
|
370
|
-
return packageJson.version
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
if (data.operation === 'ping') {
|
|
374
|
-
return { ping: 'pong' }
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
if (data.operation === 'help') {
|
|
378
|
-
return {
|
|
379
|
-
'promet': 'run aggregation for promet',
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
assert(data.operation, 'must specify operation')
|
|
383
|
-
|
|
384
|
-
if (data.operation === 'promet-get-config') {
|
|
385
|
-
return logic.promet;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
if (data.operation === 'promet-create-pipeline') {
|
|
389
|
-
assert(data.spec, 'must specify data.spec')
|
|
390
|
-
return logic.promet.createAggregationPipeline(data.spec)
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
if (data.operation === 'racuni-isci-create-pipeline') {
|
|
394
|
-
assert(data.spec, 'must specify data.spec')
|
|
395
|
-
return logic.racuni.isci.createAggregationPipeline(data.spec)
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
assert(data.tenant, 'must specify tenant')
|
|
400
|
-
|
|
401
|
-
if (data.operation === 'promet-exec-pipeline') {
|
|
402
|
-
assert(data.pipeline || data.spec, 'must specify data.spec or data.pipeline')
|
|
403
|
-
if (!data.pipeline) data.pipeline = await logic.promet.createAggregationPipeline(data.spec)
|
|
404
|
-
let res = await logic.promet.execPipeline(data.pipeline.pipeline || data.pipeline, data.tenant)
|
|
405
|
-
// res.pipeline = data.pipeline;
|
|
406
|
-
return res;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
if (data.operation === 'racuni-isci-exec-pipeline') {
|
|
410
|
-
assert(data.pipeline || data.spec, 'must specify data.spec or data.pipeline')
|
|
411
|
-
if (!data.pipeline) data.pipeline = await logic.racuni.isci.createAggregationPipeline(data.spec)
|
|
412
|
-
let res = await logic.racuni.isci.execPipeline(data.tenant, data.pipeline.pipeline || data.pipeline)
|
|
413
|
-
//res.pipeline = data.pipeline;
|
|
414
|
-
return res;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
if (data.operation === 'porocilo-racunovodji') {
|
|
418
|
-
let pipeline = logic.racunovodji.createAggregationPipeline(data)
|
|
419
|
-
let podatki = await logic.racunovodji.execPipeline(pipeline.pipeline, data.tenant)
|
|
420
|
-
let racuni = await logic.racunovodji.getTaxBills(data.tenant, pipeline.match)
|
|
421
|
-
let stornirani = await logic.zakljucekBlagajne.storniraniRacuni(pipeline.match, data.tenant)
|
|
422
|
-
|
|
423
|
-
return { podatki, racuni, stornirani };
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
if (data.operation === 'zakljucek-blagajne') {
|
|
427
|
-
let pipeline = logic.zakljucekBlagajne.createAggregationPipeline(data)
|
|
428
|
-
console.dir(pipeline, { depth: 10, colors: true })
|
|
429
|
-
let zakljucek = await logic.zakljucekBlagajne.execPipeline(pipeline.pipeline, data.tenant)
|
|
430
|
-
let stornirani = await logic.zakljucekBlagajne.storniraniRacuni(pipeline.match, data.tenant)
|
|
431
|
-
return { zakljucek, stornirani };
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
if (data.operation === 'moj-promet') {
|
|
435
|
-
assert(data.username, 'must specify username')
|
|
436
|
-
assert(data.timeQuery, 'must specify timeQuery')
|
|
437
|
-
|
|
438
|
-
let pipeline = logic.mojPromet.createAggregationPipeline(data)
|
|
439
|
-
let podatki = await logic.mojPromet.execPipeline(pipeline.pipeline, data.tenant)
|
|
440
|
-
return podatki;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
if (data.operation === 'prodano') {
|
|
444
|
-
assert(data.tenant, 'must specify tenant')
|
|
445
|
-
assert(data.timeQuery, 'must specify timeQuery')
|
|
446
|
-
|
|
447
|
-
let pipeline = logic.prodano.createAggregationPipeline(data)
|
|
448
|
-
let podatki = await logic.prodano.execPipeline(pipeline.pipeline, data.tenant)
|
|
449
|
-
return podatki;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
if (data.operation === 'prodano-porabljeno') {
|
|
453
|
-
assert(data.tenant, 'must specify tenant')
|
|
454
|
-
let podatki = await logic.prodanoInPorabljeno(data)
|
|
455
|
-
return podatki;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
if (data.operation === 'skupine-izdelkov') {
|
|
459
|
-
let res = await logic.artikli.skupineIzdelkov(data.tenant)
|
|
460
|
-
return res;
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
if (data.operation === 'skupine-strank') {
|
|
464
|
-
let res = await logic.stranke.skupineStrank(data.tenant)
|
|
465
|
-
return res;
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
if (data.operation === 'stranka-info') {
|
|
469
|
-
let res = await logic.stranke.infoStranke(data.tenant, data._id || data)
|
|
470
|
-
return res;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
if (data.operation === 'get-certs') {
|
|
474
|
-
let res = await logic.certs.getAll(data.tenant)
|
|
475
|
-
return res;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
if (data.operation === 'get-certs-valid') {
|
|
479
|
-
let res = await logic.certs.getValid(data.tenant, data.query)
|
|
480
|
-
return res;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
if (data.operation === 'furs-register-cert') {
|
|
484
|
-
assert(data.certBase64, "must specify certBase64")
|
|
485
|
-
assert(data.certPass, "must specify certPass")
|
|
486
|
-
let timeoutError = ""
|
|
487
|
-
|
|
488
|
-
try {
|
|
489
|
-
timeoutError = "furs/cert: FURS podsistem se ni odzval."
|
|
490
|
-
let res = (await client.requestPromise('durs', { operation: 'add cert', data: { cert: data.certBase64, password: data.certPass } }, null, 2000)).data;
|
|
491
|
-
log.debug('furs-reg-cert: durs add cert returned“', res);
|
|
492
|
-
|
|
493
|
-
timeoutError = "furs/cert: spodletelo je pisanje v bazo."
|
|
494
|
-
|
|
495
|
-
await client.requestPromise('db', {
|
|
496
|
-
operation: 'hardDeleteOne',
|
|
497
|
-
collection: 'certifikati',
|
|
498
|
-
db: data.tenant,
|
|
499
|
-
query: { serijskaStevilka: res.certSerial }
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
let cert = (await client.requestPromise('db', {
|
|
503
|
-
operation: 'upsert',
|
|
504
|
-
collection: 'certifikati',
|
|
505
|
-
db: data.tenant,
|
|
506
|
-
query: { serijskaStevilka: res.certSerial },
|
|
507
|
-
update: {
|
|
508
|
-
certOk: true,
|
|
509
|
-
davcnaStevilka: res.certTaxNo,
|
|
510
|
-
podjetje: res.certName,
|
|
511
|
-
serijskaStevilka: res.certSerial,
|
|
512
|
-
veljaDo: res.certValidUntil,
|
|
513
|
-
izdajatelj: res.certIssuer,
|
|
514
|
-
certBase64: data.certBase64,
|
|
515
|
-
certPass: data.certPass,
|
|
516
|
-
}
|
|
517
|
-
}, null, 2000)).data;
|
|
518
|
-
log.debug('furs-reg-cert: cert saved to db', cert);
|
|
519
|
-
|
|
520
|
-
if (!cert) return {
|
|
521
|
-
certMessage: "Pisanje v bazo je spodletelo. Ponovno dodajte potrdilo.",
|
|
522
|
-
certOk: false,
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
let ret = {
|
|
526
|
-
certOk: true,
|
|
527
|
-
certMessage: "digitalno potrdilo sprejeto",
|
|
528
|
-
cert,
|
|
529
|
-
tenant: data.tenant,
|
|
530
|
-
};
|
|
531
|
-
|
|
532
|
-
log.info("furs-reg-cert returns", ret)
|
|
533
|
-
|
|
534
|
-
return ret;
|
|
535
|
-
|
|
536
|
-
} catch (err) {
|
|
537
|
-
|
|
538
|
-
// console.log('err',err);
|
|
539
|
-
|
|
540
|
-
if (err.reason === "reject" && err.data.message && /Invalid password/gi.test(err.data.message)) {
|
|
541
|
-
return {
|
|
542
|
-
certMessage: "Napačno geslo.",
|
|
543
|
-
certOk: false,
|
|
544
|
-
}
|
|
545
|
-
} else if (err.reason === "timeout") {
|
|
546
|
-
return {
|
|
547
|
-
certMessage: timeoutError,
|
|
548
|
-
certOk: false,
|
|
549
|
-
}
|
|
550
|
-
} else {
|
|
551
|
-
log.error("furs-reg-cert", err.message)
|
|
552
|
-
return {
|
|
553
|
-
certMessage: "furs/cert: " + (err.reason),
|
|
554
|
-
certOk: false,
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
if (data.operation === 'furs-remove-cert') {
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
assert(data.certId, "must specify certId")
|
|
565
|
-
assert(data.taxNo, "must specify taxNo")
|
|
566
|
-
|
|
567
|
-
log.debug('removing cert', data);
|
|
568
|
-
|
|
569
|
-
let result = await client.requestPromise('durs',
|
|
570
|
-
{ operation: 'remove cert', data: { taxNo: data.taxNo, } }, null, 500);
|
|
571
|
-
|
|
572
|
-
log.trace('cert removed', result);
|
|
573
|
-
|
|
574
|
-
return {
|
|
575
|
-
ok: true,
|
|
576
|
-
result: result.data
|
|
577
|
-
};
|
|
578
|
-
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
if (data.operation === "furs-register-premise") {
|
|
582
|
-
assert(data.poslovniProstor, "must specify poslovniProstor (its _id)")
|
|
583
|
-
assert(data.Premise, "must specify Premise")
|
|
584
|
-
assert(data.DursEnv, "must specify DursEnv")
|
|
585
|
-
assert(data.tenant, "must specify tenant")
|
|
586
|
-
assert(data.taxNo, "must specify taxNo")
|
|
587
|
-
let timeoutError = ""
|
|
588
|
-
|
|
589
|
-
try {
|
|
590
|
-
timeoutError = "furs/pp: FURS podsistem se ni odzval."
|
|
591
|
-
let res = (await client.requestPromise('durs', {
|
|
592
|
-
operation: 'register premise',
|
|
593
|
-
data: { Premise: data.Premise, DursEnv: data.DursEnv }
|
|
594
|
-
}, null, 2000)).data;
|
|
595
|
-
log.debug('furs-register-premise: durs register premise returned', res);
|
|
596
|
-
|
|
597
|
-
if (res.success === false && /ENOENT\: no such file or directory/.test(res.error)) {
|
|
598
|
-
// no cert, try to register it
|
|
599
|
-
let certData = (await client.requestPromise('db', {
|
|
600
|
-
operation: 'findOne',
|
|
601
|
-
collection: 'certifikati',
|
|
602
|
-
db: data.tenant,
|
|
603
|
-
query: { davcnaStevilka: Number(data.taxNo) }
|
|
604
|
-
})).data;
|
|
605
|
-
if (!certData) return {
|
|
606
|
-
success: false,
|
|
607
|
-
message: `Digitalno potrdilo za ${data.taxNo} ni registrirano niti ga ni v bazi.`
|
|
608
|
-
}
|
|
609
|
-
let certRes = (await client.requestPromise('durs', { operation: 'add cert', data: { cert: certData.certBase64, password: certData.certPass } }, null, 2000)).data;
|
|
610
|
-
log.trace("... certRes", certRes)
|
|
611
|
-
if (!certRes || !certRes.certSerial) return {
|
|
612
|
-
success: false,
|
|
613
|
-
message: `Ponovna namestitev digitalnega potrdila ${data.taxNo} je spodletela.`
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
res = (await client.requestPromise('durs', {
|
|
617
|
-
operation: 'register premise',
|
|
618
|
-
data: { Premise: data.Premise, DursEnv: data.DursEnv }
|
|
619
|
-
}, null, 2000)).data;
|
|
620
|
-
log.debug('furs-register-premise: durs register premise RETRY returned', res);
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
if (!res.success) return res;
|
|
624
|
-
|
|
625
|
-
timeoutError = "furs/pp: spodletelo je pisanje v bazo."
|
|
626
|
-
|
|
627
|
-
let dbres = (await client.requestPromise('db', {
|
|
628
|
-
operation: 'upsert',
|
|
629
|
-
collection: 'poslovalnice',
|
|
630
|
-
db: data.tenant,
|
|
631
|
-
query: { _id: data.poslovniProstor },
|
|
632
|
-
update: {
|
|
633
|
-
potrditevFurs: JSON.stringify(res.result)
|
|
634
|
-
}
|
|
635
|
-
}, null, 2000)).data;
|
|
636
|
-
log.debug('furs-register-premise: premise saved to db', dbres);
|
|
637
|
-
|
|
638
|
-
if (!dbres) return {
|
|
639
|
-
message: "Pisanje v bazo je spodletelo. Ponovno prijavite poslovni prostor.",
|
|
640
|
-
success: false,
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
let ret = {
|
|
644
|
-
success: true,
|
|
645
|
-
message: "poslovni prostor je prijavljen FURS",
|
|
646
|
-
poslovalnica: dbres
|
|
647
|
-
};
|
|
648
|
-
|
|
649
|
-
log.info("furs-registger-premise returns", ret)
|
|
650
|
-
|
|
651
|
-
return ret;
|
|
652
|
-
|
|
653
|
-
} catch (err) {
|
|
654
|
-
|
|
655
|
-
// console.log('err', err);
|
|
656
|
-
|
|
657
|
-
if (err.reason === "reject" && err.data.message && /Invalid password/gi.test(err.data.message)) {
|
|
658
|
-
return {
|
|
659
|
-
message: "Napačno geslo.",
|
|
660
|
-
success: false,
|
|
661
|
-
}
|
|
662
|
-
} else if (err.reason === "timeout") {
|
|
663
|
-
return {
|
|
664
|
-
message: timeoutError,
|
|
665
|
-
success: false,
|
|
666
|
-
}
|
|
667
|
-
} else {
|
|
668
|
-
log.error("furs-register-premise", err.message)
|
|
669
|
-
return {
|
|
670
|
-
message: "furs/pp: " + (err.message || err.data && err.data.message),
|
|
671
|
-
success: false,
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
if (data.operation === "furs-register-invoice") {
|
|
678
|
-
//setTimeout(() => registerAllInvoices(data), 10000 );
|
|
679
|
-
return await registerInvoice(data);
|
|
680
|
-
};
|
|
681
|
-
|
|
682
|
-
if (data.operation === "furs-register-all-invoices") {
|
|
683
|
-
let all = await registerAllInvoices(data);
|
|
684
|
-
console.log('all', all);
|
|
685
|
-
return all
|
|
686
|
-
};
|
|
687
|
-
|
|
688
|
-
if (data.operation === "racun-storno") {
|
|
689
|
-
assert(data.tenant, "must specify tenant")
|
|
690
|
-
assert(data, "must specify data")
|
|
691
|
-
assert(data._id, "must specify _id of the invoice")
|
|
692
|
-
return await storno(data.tenant, data.id || data._id)
|
|
693
|
-
};
|
|
694
|
-
|
|
695
|
-
if (data.operation === "razknjizi-racun") {
|
|
696
|
-
assert(data.tenant, "must specify tenant")
|
|
697
|
-
assert(data._id, "must specify _id of the invoice")
|
|
698
|
-
let racun = await dbFindById(data.tenant, "racuni", data._id);
|
|
699
|
-
let nastavitve = await dbNastavitve(data.tenant);
|
|
700
|
-
mongo.setDb(data.tenant)
|
|
701
|
-
logic.spremeniZalogo.spremeniZalogoNaPodlagiRacuna(racun, nastavitve, mongo)
|
|
702
|
-
return { ok: true }
|
|
703
|
-
};
|
|
704
|
-
|
|
705
|
-
if (data.operation === "razknjizi-racune") {
|
|
706
|
-
assert(data.tenant, "must specify tenant")
|
|
707
|
-
assert(data._id, "must specify _id of the invoice")
|
|
708
|
-
mongo.setDb(data.tenant)
|
|
709
|
-
let nerazknijzeni = await mongo.find("racuni", { leto: { $gte: (new Date().getFullYear() - 1) }, razknjizen: { $exists: false } }, {})
|
|
710
|
-
let nastavitve = await dbNastavitve(data.tenant);
|
|
711
|
-
for await (let racun of nerazknijzeni) {
|
|
712
|
-
await logic.spremeniZalogo.spremeniZalogoNaPodlagiRacuna(racun, nastavitve, mongo)
|
|
713
|
-
}
|
|
714
|
-
return { ok: true }
|
|
715
|
-
};
|
|
716
|
-
|
|
717
|
-
if (data.operation === "razknjizi-blagovnice") {
|
|
718
|
-
assert(data.tenant, "must specify tenant")
|
|
719
|
-
let nerazknijzeni = await dbFind(data.tenant, "odprteBlagovnice", { leto: { $gte: (new Date().getFullYear() - 1) }, razknjizen: { $exists: false } })
|
|
720
|
-
let vse = 0, razknjizene = 0
|
|
721
|
-
let napake = []
|
|
722
|
-
for await (let blagovnica of nerazknijzeni) {
|
|
723
|
-
try {
|
|
724
|
-
vse++
|
|
725
|
-
await razknjiziBlagovnico(blagovnica._id.toString(), data.tenant)
|
|
726
|
-
razknjizene++
|
|
727
|
-
} catch (err) {
|
|
728
|
-
napake.push(err)
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
return { ok: vse === razknjizene, vse, razknjizene, napake }
|
|
732
|
-
};
|
|
733
|
-
|
|
734
|
-
if (data.operation === "razknjizi-blagovnico") {
|
|
735
|
-
assert(data._id, "must specify _id of the Blagovnica")
|
|
736
|
-
assert(data.tenant, "must specify tenant")
|
|
737
|
-
razknjiziBlagovnico(data._id, data.tenant)
|
|
738
|
-
return { ok: true }
|
|
739
|
-
};
|
|
740
|
-
|
|
741
|
-
if (data.operation === "kartica") {
|
|
742
|
-
assert(data.tenant, "must specify tenant")
|
|
743
|
-
assert(data.artikel_id, "must specify _id of Artikel")
|
|
744
|
-
|
|
745
|
-
return await logic.karticaArtikla(data.artikel_id, data.tenant, data.skladisca_ids)
|
|
746
|
-
};
|
|
747
|
-
|
|
748
|
-
if (data.operation === "blagovnice") {
|
|
749
|
-
assert(data.tenant, "must specify tenant")
|
|
750
|
-
return await logic.poisciBlagovnice(data)
|
|
751
|
-
};
|
|
752
|
-
|
|
753
|
-
if (data.operation === "poraba-zdravil") {
|
|
754
|
-
assert(data.tenant, "must specify tenant")
|
|
755
|
-
return await logic.porabaInProdajaZdravil(data)
|
|
756
|
-
};
|
|
757
|
-
|
|
758
|
-
if (data.operation === "ensure-indexes") {
|
|
759
|
-
assert(data.tenant, "must specify tenant")
|
|
760
|
-
return await logic.ensureIndexes(data.tenant)
|
|
761
|
-
};
|
|
762
|
-
|
|
763
|
-
if (data.operation === "zaloga-na-dan") {
|
|
764
|
-
assert(data.tenant, "must specify tenant")
|
|
765
|
-
return await logic.zalogaNaDan(data)
|
|
766
|
-
};
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
if (data.operation === "zaloga-po-dnevih") {
|
|
770
|
-
assert(data.tenant, "must specify tenant")
|
|
771
|
-
return await logic.zalogaPoDnevih(data)
|
|
772
|
-
};
|
|
773
|
-
|
|
774
|
-
if (data.operation === "neuporabljeno-poisci") {
|
|
775
|
-
assert(data.tenant, "must specify tenant")
|
|
776
|
-
assert(data.od, "must specify od")
|
|
777
|
-
return await logic.poisciNeuporabljeno(data)
|
|
778
|
-
};
|
|
779
|
-
|
|
780
|
-
if (data.operation === "neuporabljeno-pobrisi") {
|
|
781
|
-
assert(data.tenant, "must specify tenant")
|
|
782
|
-
assert(data.od, "must specify od")
|
|
783
|
-
return await logic.pobrisiNeuporabljeno(data)
|
|
784
|
-
};
|
|
785
|
-
|
|
786
|
-
if (data.operation === "polna-inventura") {
|
|
787
|
-
assert(data.tenant, "must specify tenant")
|
|
788
|
-
assert(data.od, "must specify od")
|
|
789
|
-
assert(data.razknjizba, "must specify razknjizba")
|
|
790
|
-
assert(data.skladisce_id, "must specify skladisce_id")
|
|
791
|
-
assert(data.izvedel, "must specify izvedel")
|
|
792
|
-
assert(data.izlociTisteBrezZaloge, "must specify izlociTisteBrezZaloge")
|
|
793
|
-
return await logic.zakljucekPolneInventureSestaviInZapisi(data);
|
|
794
|
-
};
|
|
795
|
-
|
|
796
|
-
assert(false, 'unsupported operation ' + data.operation)
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
module.exports = { KlikvetLogicWorker }
|