@tamu-dor/famis-db 2.0.5 → 2.0.6
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/index.js +715 -598
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,27 +1,34 @@
|
|
|
1
|
-
const knex = require(
|
|
2
|
-
const { createId } = require(
|
|
3
|
-
const { each, map } = require(
|
|
4
|
-
const revisionHash = require(
|
|
5
|
-
|
|
1
|
+
const knex = require("knex");
|
|
2
|
+
const { createId } = require("@paralleldrive/cuid2");
|
|
3
|
+
const { each, map } = require("awaity");
|
|
4
|
+
const revisionHash = require("rev-hash");
|
|
5
|
+
const customerAccountPrefixes = ["68", "RC", "56"];
|
|
6
6
|
const famisInvoiceNumbers = [
|
|
7
7
|
{
|
|
8
8
|
start: 300128,
|
|
9
9
|
end: 399999,
|
|
10
|
-
prefix:
|
|
11
|
-
aliases: [
|
|
12
|
-
'ilabs',
|
|
13
|
-
]
|
|
10
|
+
prefix: "I",
|
|
11
|
+
aliases: ["ilabs"],
|
|
14
12
|
},
|
|
15
13
|
{
|
|
16
14
|
start: 100000,
|
|
17
15
|
end: 999999,
|
|
18
|
-
prefix:
|
|
19
|
-
aliases: [
|
|
20
|
-
'mosaic',
|
|
21
|
-
]
|
|
16
|
+
prefix: "V",
|
|
17
|
+
aliases: ["mosaic"],
|
|
22
18
|
},
|
|
23
19
|
];
|
|
24
|
-
const returningColumns = [
|
|
20
|
+
const returningColumns = [
|
|
21
|
+
"uid",
|
|
22
|
+
"number",
|
|
23
|
+
"lab",
|
|
24
|
+
"totalAmount",
|
|
25
|
+
"data",
|
|
26
|
+
"date",
|
|
27
|
+
"type",
|
|
28
|
+
"famisInvoiceNumber",
|
|
29
|
+
"customerAccount",
|
|
30
|
+
"sellingAccount",
|
|
31
|
+
];
|
|
25
32
|
|
|
26
33
|
const pad = (str, max, right) => {
|
|
27
34
|
let retVal = str;
|
|
@@ -38,15 +45,16 @@ const pad = (str, max, right) => {
|
|
|
38
45
|
};
|
|
39
46
|
|
|
40
47
|
const sanitizeAccountNumber = (account) => {
|
|
41
|
-
if (account.substring(0,2) ===
|
|
42
|
-
return account.replace(/\W/g,
|
|
48
|
+
if (account.substring(0, 2) === "RC") {
|
|
49
|
+
return account.replace(/\W/g, "");
|
|
43
50
|
} else {
|
|
44
|
-
return pad(account.replace(/\D/g,
|
|
51
|
+
return pad(account.replace(/\D/g, ""), 13, true);
|
|
45
52
|
}
|
|
46
53
|
};
|
|
47
54
|
|
|
48
55
|
const getCoreAccountNumber = (famis) => {
|
|
49
|
-
const { sellingCampusCode, sellingAccountNumber, sellingSupportAccount } =
|
|
56
|
+
const { sellingCampusCode, sellingAccountNumber, sellingSupportAccount } =
|
|
57
|
+
famis;
|
|
50
58
|
const account = `${sellingCampusCode}${sellingAccountNumber}${sellingSupportAccount}`;
|
|
51
59
|
return sanitizeAccountNumber(account);
|
|
52
60
|
};
|
|
@@ -55,23 +63,22 @@ const sortObjectKeys = (obj) => {
|
|
|
55
63
|
if (obj === null || obj === undefined) {
|
|
56
64
|
return obj;
|
|
57
65
|
}
|
|
58
|
-
if (typeof obj !==
|
|
66
|
+
if (typeof obj !== "object") {
|
|
59
67
|
// it is a primitive: number/string (in an array)
|
|
60
68
|
return obj;
|
|
61
69
|
}
|
|
62
|
-
const result = Object.keys(obj)
|
|
63
|
-
(
|
|
70
|
+
const result = Object.keys(obj)
|
|
71
|
+
.sort()
|
|
72
|
+
.reduce((acc, key) => {
|
|
64
73
|
if (Array.isArray(obj[key])) {
|
|
65
74
|
acc[key] = obj[key].map(sortObjectKeys);
|
|
66
|
-
} else if (typeof obj[key] ===
|
|
75
|
+
} else if (typeof obj[key] === "object") {
|
|
67
76
|
acc[key] = sortObjectKeys(obj[key]);
|
|
68
77
|
} else {
|
|
69
78
|
acc[key] = obj[key];
|
|
70
79
|
}
|
|
71
80
|
return acc;
|
|
72
|
-
},
|
|
73
|
-
{},
|
|
74
|
-
);
|
|
81
|
+
}, {});
|
|
75
82
|
return result;
|
|
76
83
|
};
|
|
77
84
|
|
|
@@ -84,7 +91,7 @@ class FAMISDb {
|
|
|
84
91
|
|
|
85
92
|
init(config, people) {
|
|
86
93
|
this.db = knex({
|
|
87
|
-
client:
|
|
94
|
+
client: "pg",
|
|
88
95
|
connection: config,
|
|
89
96
|
pool: {
|
|
90
97
|
min: 0,
|
|
@@ -113,14 +120,29 @@ class FAMISDb {
|
|
|
113
120
|
poNumber: null,
|
|
114
121
|
};
|
|
115
122
|
|
|
116
|
-
if (
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
123
|
+
if (
|
|
124
|
+
famis.customerAccountNumber ||
|
|
125
|
+
(replace_data && replace_data.customerAccountNumber)
|
|
126
|
+
) {
|
|
127
|
+
retVal.accountNumber =
|
|
128
|
+
replace_data && replace_data.customerAccountNumber
|
|
129
|
+
? replace_data.customerAccountNumber
|
|
130
|
+
: famis.customerAccountNumber;
|
|
131
|
+
} else if (
|
|
132
|
+
famis.customerPONumber.includes("|") ||
|
|
133
|
+
(replace_data && replace_data.customerPONumber.includes("|"))
|
|
134
|
+
) {
|
|
135
|
+
const result =
|
|
136
|
+
replace_data && replace_data.customerPONumber.includes("|")
|
|
137
|
+
? replace_data.customerPONumber.split("|")
|
|
138
|
+
: famis.customerPONumber.split("|");
|
|
120
139
|
retVal.accountNumber = sanitizeAccountNumber(result[0]);
|
|
121
140
|
retVal.poNumber = result[1].trim();
|
|
122
141
|
} else {
|
|
123
|
-
retVal.accountNumber =
|
|
142
|
+
retVal.accountNumber =
|
|
143
|
+
replace_data && replace_data.customerPONumber
|
|
144
|
+
? sanitizeAccountNumber(replace_data.customerPONumber)
|
|
145
|
+
: sanitizeAccountNumber(famis.customerPONumber);
|
|
124
146
|
}
|
|
125
147
|
|
|
126
148
|
return retVal;
|
|
@@ -134,40 +156,70 @@ class FAMISDb {
|
|
|
134
156
|
* @returns {Promise<object>} invoice object
|
|
135
157
|
* @memberof FAMISDb
|
|
136
158
|
*/
|
|
137
|
-
async processInvoice(invoice, historical=false) {
|
|
159
|
+
async processInvoice(invoice, historical = false) {
|
|
138
160
|
// console.log(invoice);
|
|
139
161
|
try {
|
|
140
162
|
const account = getCoreAccountNumber(invoice.data.details[0].famis);
|
|
141
163
|
invoice.core = await this.getCoreByAccount(account);
|
|
142
164
|
if (invoice.core) {
|
|
143
|
-
invoice.core.systemPart = await this.getSystemById(
|
|
165
|
+
invoice.core.systemPart = await this.getSystemById(
|
|
166
|
+
invoice.core.systemId,
|
|
167
|
+
);
|
|
144
168
|
} else {
|
|
145
|
-
const e = Error(
|
|
146
|
-
|
|
169
|
+
const e = Error(
|
|
170
|
+
`Revenue account ${account} is not associated with a core.`,
|
|
171
|
+
);
|
|
172
|
+
throw e;
|
|
147
173
|
}
|
|
148
|
-
const campus = account.substring(0,2);
|
|
149
|
-
const revenuAccount = await this.getByAccountNumber(
|
|
150
|
-
|
|
151
|
-
|
|
174
|
+
const campus = account.substring(0, 2);
|
|
175
|
+
const revenuAccount = await this.getByAccountNumber(
|
|
176
|
+
account,
|
|
177
|
+
campus,
|
|
178
|
+
historical,
|
|
179
|
+
);
|
|
180
|
+
const customerAccountNumber = this.getAccountNumber(
|
|
181
|
+
invoice.data.details[0].famis,
|
|
182
|
+
invoice.replace_data,
|
|
183
|
+
);
|
|
184
|
+
invoice.customerAccountInfo = await this.getByAccountNumber(
|
|
185
|
+
customerAccountNumber.accountNumber,
|
|
186
|
+
campus,
|
|
187
|
+
historical,
|
|
188
|
+
);
|
|
152
189
|
if (invoice.customerAccountInfo) {
|
|
153
|
-
let idtAllowed =
|
|
154
|
-
invoice.customerAccountInfo
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
190
|
+
let idtAllowed =
|
|
191
|
+
"idt_allowed" in invoice.customerAccountInfo &&
|
|
192
|
+
invoice.customerAccountInfo.idt_allowed !== null
|
|
193
|
+
? invoice.customerAccountInfo.idt_allowed
|
|
194
|
+
: "parent_account_idt_allowed" in invoice.customerAccountInfo &&
|
|
195
|
+
invoice.customerAccountInfo.parent_account_idt_allowed;
|
|
196
|
+
idtAllowed =
|
|
197
|
+
idtAllowed &&
|
|
198
|
+
!(
|
|
199
|
+
invoice.replace_data &&
|
|
200
|
+
typeof invoice.replace_data === "object" &&
|
|
159
201
|
invoice.replace_data.forcedAr
|
|
160
202
|
);
|
|
161
|
-
const isIdt =
|
|
203
|
+
const isIdt =
|
|
204
|
+
customerAccountNumber &&
|
|
205
|
+
customerAccountNumber.accountNumber.substring(0, 2) ===
|
|
206
|
+
account.substring(0, 2) &&
|
|
207
|
+
idtAllowed;
|
|
162
208
|
invoice.isIdt = isIdt;
|
|
163
209
|
} else {
|
|
164
210
|
invoice.isIdt = false;
|
|
165
211
|
}
|
|
166
|
-
let poNumber =
|
|
212
|
+
let poNumber = customerAccountPrefixes.includes(
|
|
213
|
+
customerAccountNumber.accountNumber.substring(0, 2),
|
|
214
|
+
)
|
|
215
|
+
? customerAccountNumber.poNumber
|
|
216
|
+
: `${campus}-${revenuAccount.department}`;
|
|
167
217
|
invoice.poNumber = poNumber;
|
|
168
218
|
invoice.customerAccount = customerAccountNumber.accountNumber;
|
|
169
219
|
if (invoice.customerAccountInfo && this.people) {
|
|
170
|
-
const pi = invoice.customerAccountInfo.uin
|
|
220
|
+
const pi = invoice.customerAccountInfo.uin
|
|
221
|
+
? await this.people.getByUin(invoice.customerAccountInfo.uin)
|
|
222
|
+
: [];
|
|
171
223
|
if (pi.length) {
|
|
172
224
|
invoice.pi = JSON.parse(JSON.stringify(pi[0]));
|
|
173
225
|
}
|
|
@@ -180,7 +232,6 @@ class FAMISDb {
|
|
|
180
232
|
return invoice;
|
|
181
233
|
}
|
|
182
234
|
|
|
183
|
-
|
|
184
235
|
/**
|
|
185
236
|
*
|
|
186
237
|
* Get invoice by uid
|
|
@@ -193,14 +244,14 @@ class FAMISDb {
|
|
|
193
244
|
let retVal;
|
|
194
245
|
|
|
195
246
|
try {
|
|
196
|
-
records = await this.db(
|
|
247
|
+
records = await this.db("invoices").where("uid", uid);
|
|
197
248
|
if (records.length) {
|
|
198
249
|
retVal = this.processInvoice(records[0]);
|
|
199
|
-
}
|
|
250
|
+
}
|
|
200
251
|
} catch (e) {
|
|
201
252
|
throw e;
|
|
202
253
|
}
|
|
203
|
-
|
|
254
|
+
|
|
204
255
|
return retVal;
|
|
205
256
|
}
|
|
206
257
|
|
|
@@ -216,7 +267,7 @@ class FAMISDb {
|
|
|
216
267
|
let retVal;
|
|
217
268
|
|
|
218
269
|
try {
|
|
219
|
-
records = await this.db(
|
|
270
|
+
records = await this.db("invoices").whereILike("number", `${number}%`);
|
|
220
271
|
if (records.length) {
|
|
221
272
|
retVal = this.processInvoice(records[0]);
|
|
222
273
|
}
|
|
@@ -238,10 +289,10 @@ class FAMISDb {
|
|
|
238
289
|
let retVal;
|
|
239
290
|
|
|
240
291
|
try {
|
|
241
|
-
records = await this.db(
|
|
292
|
+
records = await this.db("invoices").where("fingerprint", fingerPrint);
|
|
242
293
|
if (records.length) {
|
|
243
294
|
retVal = this.processInvoice(records[0]);
|
|
244
|
-
}
|
|
295
|
+
}
|
|
245
296
|
} catch (e) {
|
|
246
297
|
throw e;
|
|
247
298
|
}
|
|
@@ -261,28 +312,29 @@ class FAMISDb {
|
|
|
261
312
|
let retVal;
|
|
262
313
|
|
|
263
314
|
try {
|
|
264
|
-
let records = await this.db(
|
|
265
|
-
.whereNull(
|
|
266
|
-
.andWhere(function() {
|
|
267
|
-
this.where(
|
|
268
|
-
this.orWhere(
|
|
269
|
-
this.orWhereRaw("replace_data->>'customerAccountNumber' = ?",[
|
|
270
|
-
|
|
315
|
+
let records = await this.db("invoices")
|
|
316
|
+
.whereNull("deleted")
|
|
317
|
+
.andWhere(function () {
|
|
318
|
+
this.where("customerAccount", accountNumber);
|
|
319
|
+
this.orWhere("sellingAccount", accountNumber);
|
|
320
|
+
this.orWhereRaw("replace_data->>'customerAccountNumber' = ?", [
|
|
321
|
+
accountNumber,
|
|
322
|
+
]);
|
|
323
|
+
this.orWhereRaw("replace_data->>'customerPONumber' LIKE ?", [
|
|
324
|
+
`${accountNumber}%`,
|
|
325
|
+
]);
|
|
271
326
|
});
|
|
272
327
|
if (records.length) {
|
|
273
|
-
records = await map(
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
}
|
|
279
|
-
);
|
|
280
|
-
}
|
|
328
|
+
records = await map(records, async (record) => {
|
|
329
|
+
const rec = self.processInvoice(record);
|
|
330
|
+
return rec;
|
|
331
|
+
});
|
|
332
|
+
}
|
|
281
333
|
retVal = records;
|
|
282
334
|
} catch (e) {
|
|
283
335
|
throw e;
|
|
284
336
|
}
|
|
285
|
-
|
|
337
|
+
|
|
286
338
|
return retVal;
|
|
287
339
|
}
|
|
288
340
|
|
|
@@ -297,31 +349,34 @@ class FAMISDb {
|
|
|
297
349
|
* @returns {Promise<array>} invoice objects
|
|
298
350
|
* @memberof FAMISDb
|
|
299
351
|
*/
|
|
300
|
-
async getAllInvoices(
|
|
352
|
+
async getAllInvoices(
|
|
353
|
+
orderBy = "date",
|
|
354
|
+
dir = "desc",
|
|
355
|
+
deleted = false,
|
|
356
|
+
page = null,
|
|
357
|
+
limit = null,
|
|
358
|
+
) {
|
|
301
359
|
const self = this;
|
|
302
360
|
let records;
|
|
303
361
|
let metadata;
|
|
304
362
|
try {
|
|
305
|
-
const sql = this.db(
|
|
306
|
-
const meta = this.db(
|
|
363
|
+
const sql = this.db("invoices");
|
|
364
|
+
const meta = this.db("invoices");
|
|
307
365
|
if (!deleted) {
|
|
308
|
-
sql.whereNull(
|
|
309
|
-
meta.whereNull(
|
|
366
|
+
sql.whereNull("deleted");
|
|
367
|
+
meta.whereNull("deleted");
|
|
310
368
|
}
|
|
311
369
|
//console.log(page, limit);
|
|
312
370
|
if (page && limit) {
|
|
313
|
-
sql.limit(limit).offset((page-1)*limit);
|
|
371
|
+
sql.limit(limit).offset((page - 1) * limit);
|
|
314
372
|
}
|
|
315
373
|
records = await sql.orderBy(orderBy, dir);
|
|
316
374
|
metadata = await meta.orderBy(orderBy, dir);
|
|
317
375
|
//console.log(records);
|
|
318
|
-
records = await map(
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
return rec;
|
|
323
|
-
}
|
|
324
|
-
);
|
|
376
|
+
records = await map(records, async (record) => {
|
|
377
|
+
const rec = self.processInvoice(record, true);
|
|
378
|
+
return rec;
|
|
379
|
+
});
|
|
325
380
|
} catch (e) {
|
|
326
381
|
throw e;
|
|
327
382
|
}
|
|
@@ -330,7 +385,7 @@ class FAMISDb {
|
|
|
330
385
|
metadata: {
|
|
331
386
|
total: metadata.length,
|
|
332
387
|
},
|
|
333
|
-
}
|
|
388
|
+
};
|
|
334
389
|
}
|
|
335
390
|
|
|
336
391
|
/**
|
|
@@ -344,18 +399,15 @@ class FAMISDb {
|
|
|
344
399
|
let records;
|
|
345
400
|
|
|
346
401
|
try {
|
|
347
|
-
records = await this.db(
|
|
348
|
-
.whereNull(
|
|
349
|
-
.whereIn(
|
|
350
|
-
this.max(
|
|
402
|
+
records = await this.db("invoices")
|
|
403
|
+
.whereNull("deleted")
|
|
404
|
+
.whereIn("date", function () {
|
|
405
|
+
this.max("date").from("invoices");
|
|
351
406
|
});
|
|
352
|
-
records = await map(
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
return rec;
|
|
357
|
-
}
|
|
358
|
-
);
|
|
407
|
+
records = await map(records, async (record) => {
|
|
408
|
+
const rec = self.processInvoice(record);
|
|
409
|
+
return rec;
|
|
410
|
+
});
|
|
359
411
|
} catch (e) {
|
|
360
412
|
throw e;
|
|
361
413
|
}
|
|
@@ -365,34 +417,36 @@ class FAMISDb {
|
|
|
365
417
|
/**
|
|
366
418
|
*
|
|
367
419
|
* Returns all held invoices
|
|
368
|
-
* @returns {Promise<array>} held invoice objects
|
|
420
|
+
* @returns {Promise<array>} held invoice objects
|
|
369
421
|
* @memberof FAMISDb
|
|
370
422
|
*/
|
|
371
|
-
async getHeldInvoices(
|
|
423
|
+
async getHeldInvoices(
|
|
424
|
+
orderBy = "date",
|
|
425
|
+
dir = "desc",
|
|
426
|
+
page = null,
|
|
427
|
+
limit = null,
|
|
428
|
+
) {
|
|
372
429
|
const self = this;
|
|
373
430
|
let records;
|
|
374
431
|
let metadata;
|
|
375
|
-
const sql = this.db(
|
|
376
|
-
const meta = this.db(
|
|
432
|
+
const sql = this.db("invoices");
|
|
433
|
+
const meta = this.db("invoices");
|
|
377
434
|
try {
|
|
378
435
|
if (page && limit) {
|
|
379
|
-
sql.limit(limit).offset((page-1)*limit);
|
|
436
|
+
sql.limit(limit).offset((page - 1) * limit);
|
|
380
437
|
}
|
|
381
438
|
records = await sql
|
|
382
|
-
.whereNull(
|
|
383
|
-
.andWhere(
|
|
439
|
+
.whereNull("deleted")
|
|
440
|
+
.andWhere("invoiced", "0")
|
|
384
441
|
.orderBy(orderBy, dir);
|
|
385
442
|
metadata = await meta
|
|
386
|
-
.whereNull(
|
|
387
|
-
.andWhere(
|
|
443
|
+
.whereNull("deleted")
|
|
444
|
+
.andWhere("invoiced", "0")
|
|
388
445
|
.orderBy(orderBy, dir);
|
|
389
|
-
records = await map(
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
return rec;
|
|
394
|
-
}
|
|
395
|
-
);
|
|
446
|
+
records = await map(records, async (record) => {
|
|
447
|
+
const rec = self.processInvoice(record);
|
|
448
|
+
return rec;
|
|
449
|
+
});
|
|
396
450
|
} catch (e) {
|
|
397
451
|
throw e;
|
|
398
452
|
}
|
|
@@ -407,7 +461,7 @@ class FAMISDb {
|
|
|
407
461
|
/**
|
|
408
462
|
*
|
|
409
463
|
* Returns all awaiting email invoices
|
|
410
|
-
* @returns {Promise<array>} awaiting email invoice objects
|
|
464
|
+
* @returns {Promise<array>} awaiting email invoice objects
|
|
411
465
|
* @memberof FAMISDb
|
|
412
466
|
*/
|
|
413
467
|
async getInvoicesAwaitingEmail() {
|
|
@@ -415,16 +469,13 @@ class FAMISDb {
|
|
|
415
469
|
let records;
|
|
416
470
|
|
|
417
471
|
try {
|
|
418
|
-
records = await this.db(
|
|
419
|
-
.whereNull(
|
|
420
|
-
.andWhere(
|
|
421
|
-
records = await map(
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
return rec;
|
|
426
|
-
}
|
|
427
|
-
);
|
|
472
|
+
records = await this.db("invoices")
|
|
473
|
+
.whereNull("deleted")
|
|
474
|
+
.andWhere("emailed", "False");
|
|
475
|
+
records = await map(records, async (record) => {
|
|
476
|
+
const rec = self.processInvoice(record);
|
|
477
|
+
return rec;
|
|
478
|
+
});
|
|
428
479
|
} catch (e) {
|
|
429
480
|
throw e;
|
|
430
481
|
}
|
|
@@ -444,18 +495,15 @@ class FAMISDb {
|
|
|
444
495
|
let retVal;
|
|
445
496
|
|
|
446
497
|
try {
|
|
447
|
-
records = await this.db(
|
|
448
|
-
.whereNull(
|
|
449
|
-
.andWhere(
|
|
498
|
+
records = await this.db("invoices")
|
|
499
|
+
.whereNull("deleted")
|
|
500
|
+
.andWhere("number", "ilike", `${number}%`);
|
|
450
501
|
if (records.length) {
|
|
451
|
-
retVal = await map(
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
}
|
|
457
|
-
);
|
|
458
|
-
}
|
|
502
|
+
retVal = await map(records, async (record) => {
|
|
503
|
+
const rec = self.processInvoice(record);
|
|
504
|
+
return rec;
|
|
505
|
+
});
|
|
506
|
+
}
|
|
459
507
|
} catch (e) {
|
|
460
508
|
throw e;
|
|
461
509
|
}
|
|
@@ -475,18 +523,15 @@ class FAMISDb {
|
|
|
475
523
|
let retVal;
|
|
476
524
|
|
|
477
525
|
try {
|
|
478
|
-
records = await this.db(
|
|
479
|
-
.whereNull(
|
|
480
|
-
.andWhere(
|
|
526
|
+
records = await this.db("invoices")
|
|
527
|
+
.whereNull("deleted")
|
|
528
|
+
.andWhere("famisInvoiceNumber", "ilike", `${famis}%`);
|
|
481
529
|
if (records.length) {
|
|
482
|
-
retVal = await map(
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
}
|
|
488
|
-
);
|
|
489
|
-
}
|
|
530
|
+
retVal = await map(records, async (record) => {
|
|
531
|
+
const rec = self.processInvoice(record);
|
|
532
|
+
return rec;
|
|
533
|
+
});
|
|
534
|
+
}
|
|
490
535
|
} catch (e) {
|
|
491
536
|
throw e;
|
|
492
537
|
}
|
|
@@ -506,12 +551,15 @@ class FAMISDb {
|
|
|
506
551
|
let records;
|
|
507
552
|
|
|
508
553
|
try {
|
|
509
|
-
famisInvoiceNumber =
|
|
510
|
-
|
|
511
|
-
|
|
554
|
+
famisInvoiceNumber =
|
|
555
|
+
typeof famisInvoiceNumber === "undefined" ? null : famisInvoiceNumber;
|
|
556
|
+
famisInvoiceNumber = famisInvoiceNumber
|
|
557
|
+
? famisInvoiceNumber
|
|
558
|
+
: await this.getNextFAMISInvoiceNumber(type);
|
|
559
|
+
invoice.details.forEach((d) => {
|
|
512
560
|
d.famis.invoiceNumber = famisInvoiceNumber;
|
|
513
561
|
if (!d.other) {
|
|
514
|
-
d.other =
|
|
562
|
+
d.other = "0.00";
|
|
515
563
|
}
|
|
516
564
|
if (!d.comments) {
|
|
517
565
|
d.comments = null;
|
|
@@ -520,7 +568,7 @@ class FAMISDb {
|
|
|
520
568
|
d.billingPeriod = null;
|
|
521
569
|
}
|
|
522
570
|
});
|
|
523
|
-
records = await this.db(
|
|
571
|
+
records = await this.db("invoices")
|
|
524
572
|
.returning(returningColumns)
|
|
525
573
|
.insert({
|
|
526
574
|
number: invoice.number,
|
|
@@ -532,7 +580,8 @@ class FAMISDb {
|
|
|
532
580
|
fingerprint: invoice.fingerprint,
|
|
533
581
|
type: type,
|
|
534
582
|
famisInvoiceNumber: famisInvoiceNumber,
|
|
535
|
-
customerAccount: this.getAccountNumber(invoice.details[0].famis)
|
|
583
|
+
customerAccount: this.getAccountNumber(invoice.details[0].famis)
|
|
584
|
+
.accountNumber,
|
|
536
585
|
sellingAccount: getCoreAccountNumber(invoice.details[0].famis),
|
|
537
586
|
});
|
|
538
587
|
} catch (e) {
|
|
@@ -552,9 +601,9 @@ class FAMISDb {
|
|
|
552
601
|
*/
|
|
553
602
|
async updateInvoice(uid, invoice) {
|
|
554
603
|
let records;
|
|
555
|
-
invoice.details.forEach(d => {
|
|
604
|
+
invoice.details.forEach((d) => {
|
|
556
605
|
if (!d.other) {
|
|
557
|
-
d.other =
|
|
606
|
+
d.other = "0.00";
|
|
558
607
|
}
|
|
559
608
|
if (!d.comments) {
|
|
560
609
|
d.comments = null;
|
|
@@ -564,7 +613,7 @@ class FAMISDb {
|
|
|
564
613
|
}
|
|
565
614
|
});
|
|
566
615
|
try {
|
|
567
|
-
records = await this.db(
|
|
616
|
+
records = await this.db("invoices")
|
|
568
617
|
.returning(returningColumns)
|
|
569
618
|
.update({
|
|
570
619
|
number: invoice.number,
|
|
@@ -572,12 +621,13 @@ class FAMISDb {
|
|
|
572
621
|
totalAmount: parseFloat(invoice.totalAmount),
|
|
573
622
|
data: JSON.stringify(invoice),
|
|
574
623
|
date: invoice.date,
|
|
575
|
-
customerAccount: this.getAccountNumber(invoice.details[0].famis)
|
|
624
|
+
customerAccount: this.getAccountNumber(invoice.details[0].famis)
|
|
625
|
+
.accountNumber,
|
|
576
626
|
sellingAccount: getCoreAccountNumber(invoice.details[0].famis),
|
|
577
627
|
fingerprint: invoice.fingerprint ? invoice.fingerprint : null,
|
|
578
628
|
replace_data: invoice.replace_data,
|
|
579
629
|
})
|
|
580
|
-
.where(
|
|
630
|
+
.where("uid", uid);
|
|
581
631
|
} catch (e) {
|
|
582
632
|
throw e;
|
|
583
633
|
}
|
|
@@ -586,7 +636,7 @@ class FAMISDb {
|
|
|
586
636
|
|
|
587
637
|
/**
|
|
588
638
|
*
|
|
589
|
-
* Toggles an invoice as paid
|
|
639
|
+
* Toggles an invoice as paid
|
|
590
640
|
* @param {string} uid
|
|
591
641
|
* @param {boolean} invoiced
|
|
592
642
|
* @returns {Promise<object>} invoice object
|
|
@@ -596,12 +646,12 @@ class FAMISDb {
|
|
|
596
646
|
let records;
|
|
597
647
|
|
|
598
648
|
try {
|
|
599
|
-
records = await this.db(
|
|
649
|
+
records = await this.db("invoices")
|
|
600
650
|
.returning(returningColumns)
|
|
601
651
|
.update({
|
|
602
652
|
invoiced,
|
|
603
653
|
})
|
|
604
|
-
.where(
|
|
654
|
+
.where("uid", uid);
|
|
605
655
|
} catch (e) {
|
|
606
656
|
throw e;
|
|
607
657
|
}
|
|
@@ -610,7 +660,7 @@ class FAMISDb {
|
|
|
610
660
|
|
|
611
661
|
/**
|
|
612
662
|
*
|
|
613
|
-
* Toggles an invoice as emailed
|
|
663
|
+
* Toggles an invoice as emailed
|
|
614
664
|
* @param {string} uid
|
|
615
665
|
* @param {boolean} emailed
|
|
616
666
|
* @returns {Promise<object>} invoice object
|
|
@@ -620,12 +670,12 @@ class FAMISDb {
|
|
|
620
670
|
let records;
|
|
621
671
|
|
|
622
672
|
try {
|
|
623
|
-
records = await this.db(
|
|
673
|
+
records = await this.db("invoices")
|
|
624
674
|
.returning(returningColumns)
|
|
625
675
|
.update({
|
|
626
676
|
emailed,
|
|
627
677
|
})
|
|
628
|
-
.where(
|
|
678
|
+
.where("uid", uid);
|
|
629
679
|
} catch (e) {
|
|
630
680
|
throw e;
|
|
631
681
|
}
|
|
@@ -644,12 +694,12 @@ class FAMISDb {
|
|
|
644
694
|
let records;
|
|
645
695
|
|
|
646
696
|
try {
|
|
647
|
-
records = await this.db(
|
|
697
|
+
records = await this.db("invoices")
|
|
648
698
|
.returning(returningColumns)
|
|
649
699
|
.update({
|
|
650
700
|
deleted,
|
|
651
701
|
})
|
|
652
|
-
.where(
|
|
702
|
+
.where("uid", uid);
|
|
653
703
|
} catch (e) {
|
|
654
704
|
throw e;
|
|
655
705
|
}
|
|
@@ -658,7 +708,7 @@ class FAMISDb {
|
|
|
658
708
|
|
|
659
709
|
/**
|
|
660
710
|
*
|
|
661
|
-
* Sets an invoice's posted date
|
|
711
|
+
* Sets an invoice's posted date
|
|
662
712
|
* @param {string} uid
|
|
663
713
|
* @param {date} postedDate
|
|
664
714
|
* @returns {Promise<object>} invoice object
|
|
@@ -668,12 +718,12 @@ class FAMISDb {
|
|
|
668
718
|
let records;
|
|
669
719
|
|
|
670
720
|
try {
|
|
671
|
-
records = await this.db(
|
|
721
|
+
records = await this.db("invoices")
|
|
672
722
|
.returning(returningColumns)
|
|
673
723
|
.update({
|
|
674
724
|
postedDate,
|
|
675
725
|
})
|
|
676
|
-
.where(
|
|
726
|
+
.where("uid", uid);
|
|
677
727
|
} catch (e) {
|
|
678
728
|
throw e;
|
|
679
729
|
}
|
|
@@ -692,12 +742,12 @@ class FAMISDb {
|
|
|
692
742
|
let records;
|
|
693
743
|
|
|
694
744
|
try {
|
|
695
|
-
records = await this.db(
|
|
745
|
+
records = await this.db("invoices")
|
|
696
746
|
.returning(returningColumns)
|
|
697
747
|
.update({
|
|
698
748
|
fingerprint,
|
|
699
749
|
})
|
|
700
|
-
.where(
|
|
750
|
+
.where("uid", uid);
|
|
701
751
|
} catch (e) {
|
|
702
752
|
throw e;
|
|
703
753
|
}
|
|
@@ -713,59 +763,60 @@ class FAMISDb {
|
|
|
713
763
|
* @returns {Promise<object>} account object
|
|
714
764
|
* @memberof FAMISDb
|
|
715
765
|
*/
|
|
716
|
-
async getByAccountNumber(number, campus, historical=false) {
|
|
766
|
+
async getByAccountNumber(number, campus, historical = false) {
|
|
717
767
|
let records;
|
|
718
768
|
let retVal;
|
|
719
|
-
let table = historical ?
|
|
769
|
+
let table = historical ? "accounts_all" : "accounts";
|
|
720
770
|
const self = this;
|
|
721
771
|
|
|
722
772
|
if (!number) {
|
|
723
|
-
throw Error(
|
|
773
|
+
throw Error("Null account number");
|
|
724
774
|
}
|
|
725
775
|
try {
|
|
726
776
|
records = await this.db(`${table}`)
|
|
727
|
-
.leftJoin(
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
)
|
|
734
|
-
.leftJoin(`${table} as parent_account`,
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
.
|
|
739
|
-
|
|
740
|
-
.
|
|
741
|
-
|
|
742
|
-
|
|
777
|
+
.leftJoin("departments", function () {
|
|
778
|
+
this.on(`${table}.department`, "=", "departments.dept").on(
|
|
779
|
+
"departments.part",
|
|
780
|
+
"=",
|
|
781
|
+
self.db.raw("?", [number.substring(0, 2)]),
|
|
782
|
+
);
|
|
783
|
+
})
|
|
784
|
+
.leftJoin(`${table} as parent_account`, function () {
|
|
785
|
+
this.on(`${table}.campus_code`, "=", "parent_account.campus_code")
|
|
786
|
+
.on(`${table}.account`, "=", "parent_account.account")
|
|
787
|
+
.on(
|
|
788
|
+
"parent_account.support_account",
|
|
789
|
+
"=",
|
|
790
|
+
self.db.raw("?", ["00000"]),
|
|
791
|
+
)
|
|
792
|
+
.on(`${table}.fiscal_year`, "=", "parent_account.fiscal_year");
|
|
793
|
+
})
|
|
743
794
|
.select(
|
|
744
795
|
`${table}.*`,
|
|
745
|
-
|
|
746
|
-
|
|
796
|
+
"departments.idt_email as billingEmailId",
|
|
797
|
+
"parent_account.idt_allowed as parent_account_idt_allowed",
|
|
747
798
|
)
|
|
748
|
-
.whereNull(
|
|
749
|
-
.andWhere(function() {
|
|
750
|
-
this.whereNull(
|
|
799
|
+
.whereNull("departments.sub")
|
|
800
|
+
.andWhere(function () {
|
|
801
|
+
this.whereNull("departments.exp_date").orWhere(
|
|
802
|
+
"departments.exp_date",
|
|
803
|
+
">",
|
|
804
|
+
self.db.raw("CURRENT_DATE"),
|
|
805
|
+
);
|
|
751
806
|
})
|
|
752
807
|
.andWhere(`${table}.account_number`, number);
|
|
753
808
|
if (records.length) {
|
|
754
809
|
retVal = records[0];
|
|
755
810
|
} else {
|
|
756
|
-
records = await this.db(
|
|
757
|
-
.where(
|
|
758
|
-
.andWhere(
|
|
811
|
+
records = await this.db("customers")
|
|
812
|
+
.where("account_number", number)
|
|
813
|
+
.andWhere("campus_code", campus);
|
|
759
814
|
if (records.length) {
|
|
760
|
-
retVal = Object.assign(
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
parent_account_idt_allowed: false,
|
|
766
|
-
billingEmailId: records[0].email,
|
|
767
|
-
}
|
|
768
|
-
);
|
|
815
|
+
retVal = Object.assign({}, records[0], {
|
|
816
|
+
idt_allowed: false,
|
|
817
|
+
parent_account_idt_allowed: false,
|
|
818
|
+
billingEmailId: records[0].email,
|
|
819
|
+
});
|
|
769
820
|
}
|
|
770
821
|
}
|
|
771
822
|
} catch (e) {
|
|
@@ -786,30 +837,41 @@ class FAMISDb {
|
|
|
786
837
|
const self = this;
|
|
787
838
|
|
|
788
839
|
try {
|
|
789
|
-
const pis = await this.db(
|
|
790
|
-
.leftJoin(
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
)
|
|
797
|
-
.leftJoin(
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
840
|
+
const pis = await this.db("accounts")
|
|
841
|
+
.leftJoin("departments", function () {
|
|
842
|
+
this.on("accounts.department", "=", "departments.dept").on(
|
|
843
|
+
"departments.part",
|
|
844
|
+
"=",
|
|
845
|
+
self.db.raw("?", [number.substring(0, 2)]),
|
|
846
|
+
);
|
|
847
|
+
})
|
|
848
|
+
.leftJoin("account_balances", function () {
|
|
849
|
+
this.on(
|
|
850
|
+
"account_balances.account_number",
|
|
851
|
+
"=",
|
|
852
|
+
"accounts.budget_account_number",
|
|
853
|
+
).on("account_balances.fiscal_year", "=", "accounts.fiscal_year");
|
|
854
|
+
})
|
|
855
|
+
.select(
|
|
856
|
+
"accounts.*",
|
|
857
|
+
"departments.idt_email as billingEmailId",
|
|
858
|
+
"account_balances.balance",
|
|
803
859
|
)
|
|
804
|
-
.
|
|
805
|
-
.
|
|
806
|
-
|
|
807
|
-
|
|
860
|
+
.whereNull("departments.sub")
|
|
861
|
+
.andWhere(function () {
|
|
862
|
+
this.whereNull("departments.exp_date").orWhere(
|
|
863
|
+
"departments.exp_date",
|
|
864
|
+
">",
|
|
865
|
+
self.db.raw("CURRENT_DATE"),
|
|
866
|
+
);
|
|
808
867
|
})
|
|
809
|
-
.where(
|
|
810
|
-
const customers = await this.db(
|
|
811
|
-
.where(
|
|
812
|
-
.whereNotIn(
|
|
868
|
+
.where("accounts.account_number", "ilike", `%${number}%`);
|
|
869
|
+
const customers = await this.db("customers")
|
|
870
|
+
.where("customers.account_number", "ilike", `%${number}%`)
|
|
871
|
+
.whereNotIn(
|
|
872
|
+
"customers.account_number",
|
|
873
|
+
pis.map((p) => p.account_number),
|
|
874
|
+
);
|
|
813
875
|
records = [...customers, ...pis];
|
|
814
876
|
} catch (e) {
|
|
815
877
|
throw e;
|
|
@@ -829,21 +891,28 @@ class FAMISDb {
|
|
|
829
891
|
const self = this;
|
|
830
892
|
|
|
831
893
|
try {
|
|
832
|
-
records = await this.db(
|
|
833
|
-
.join(
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
894
|
+
records = await this.db("accounts")
|
|
895
|
+
.join(
|
|
896
|
+
"departments",
|
|
897
|
+
function () {
|
|
898
|
+
this.on("accounts.department", "=", "departments.dept").on(
|
|
899
|
+
"accounts.campus_code",
|
|
900
|
+
"=",
|
|
901
|
+
"departments.part",
|
|
902
|
+
);
|
|
838
903
|
},
|
|
839
|
-
|
|
904
|
+
"left",
|
|
840
905
|
)
|
|
841
|
-
.select(
|
|
842
|
-
.whereNull(
|
|
843
|
-
.andWhere(function() {
|
|
844
|
-
this.whereNull(
|
|
906
|
+
.select("accounts.*", "departments.idt_email as billingEmailId")
|
|
907
|
+
.whereNull("departments.sub")
|
|
908
|
+
.andWhere(function () {
|
|
909
|
+
this.whereNull("departments.exp_date").orWhere(
|
|
910
|
+
"departments.exp_date",
|
|
911
|
+
">",
|
|
912
|
+
self.db.raw("CURRENT_DATE"),
|
|
913
|
+
);
|
|
845
914
|
})
|
|
846
|
-
.where(
|
|
915
|
+
.where("uin", uin);
|
|
847
916
|
} catch (e) {
|
|
848
917
|
throw e;
|
|
849
918
|
}
|
|
@@ -862,21 +931,28 @@ class FAMISDb {
|
|
|
862
931
|
const self = this;
|
|
863
932
|
|
|
864
933
|
try {
|
|
865
|
-
records = await this.db(
|
|
866
|
-
.join(
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
934
|
+
records = await this.db("accounts")
|
|
935
|
+
.join(
|
|
936
|
+
"departments",
|
|
937
|
+
function () {
|
|
938
|
+
this.on("accounts.department", "=", "departments.dept").on(
|
|
939
|
+
"accounts.campus_code",
|
|
940
|
+
"=",
|
|
941
|
+
"departments.part",
|
|
942
|
+
);
|
|
871
943
|
},
|
|
872
|
-
|
|
944
|
+
"left",
|
|
873
945
|
)
|
|
874
|
-
.select(
|
|
875
|
-
.whereNull(
|
|
876
|
-
.andWhere(function() {
|
|
877
|
-
this.whereNull(
|
|
946
|
+
.select("accounts.*", "departments.idt_email as billingEmailId")
|
|
947
|
+
.whereNull("departments.sub")
|
|
948
|
+
.andWhere(function () {
|
|
949
|
+
this.whereNull("departments.exp_date").orWhere(
|
|
950
|
+
"departments.exp_date",
|
|
951
|
+
">",
|
|
952
|
+
self.db.raw("CURRENT_DATE"),
|
|
953
|
+
);
|
|
878
954
|
})
|
|
879
|
-
.where(
|
|
955
|
+
.where("responsible_uin", uin);
|
|
880
956
|
} catch (e) {
|
|
881
957
|
throw e;
|
|
882
958
|
}
|
|
@@ -894,16 +970,19 @@ class FAMISDb {
|
|
|
894
970
|
let records;
|
|
895
971
|
let prefix;
|
|
896
972
|
let retVal;
|
|
897
|
-
famisInvoiceNumbers.forEach(f => {
|
|
973
|
+
famisInvoiceNumbers.forEach((f) => {
|
|
898
974
|
if (f.aliases.includes(type)) {
|
|
899
975
|
prefix = f;
|
|
900
976
|
}
|
|
901
977
|
});
|
|
902
978
|
try {
|
|
903
979
|
retVal = `${prefix.prefix}${prefix.start}`;
|
|
904
|
-
records = await this.db(
|
|
980
|
+
records = await this.db("invoices")
|
|
981
|
+
.whereIn("type", prefix.aliases)
|
|
982
|
+
.orderBy("famisInvoiceNumber", "desc");
|
|
905
983
|
if (records.length) {
|
|
906
|
-
const number =
|
|
984
|
+
const number =
|
|
985
|
+
parseInt(records[0].famisInvoiceNumber.substring(1), 10) + 1;
|
|
907
986
|
retVal = `${prefix.prefix}${number}`;
|
|
908
987
|
}
|
|
909
988
|
} catch (e) {
|
|
@@ -923,10 +1002,12 @@ class FAMISDb {
|
|
|
923
1002
|
let records;
|
|
924
1003
|
let retVal;
|
|
925
1004
|
try {
|
|
926
|
-
records = await this.db(
|
|
1005
|
+
records = await this.db("codes")
|
|
1006
|
+
.where("revenue", revenue)
|
|
1007
|
+
.orderBy("id", "desc");
|
|
927
1008
|
if (records.length) {
|
|
928
1009
|
retVal = records[0].expense;
|
|
929
|
-
}
|
|
1010
|
+
}
|
|
930
1011
|
} catch (e) {
|
|
931
1012
|
throw e;
|
|
932
1013
|
}
|
|
@@ -944,10 +1025,12 @@ class FAMISDb {
|
|
|
944
1025
|
let records;
|
|
945
1026
|
let retVal;
|
|
946
1027
|
try {
|
|
947
|
-
records = await this.db(
|
|
1028
|
+
records = await this.db("codes")
|
|
1029
|
+
.where("revenue", revenue)
|
|
1030
|
+
.orderBy("id", "desc");
|
|
948
1031
|
if (records.length) {
|
|
949
1032
|
retVal = records[0];
|
|
950
|
-
}
|
|
1033
|
+
}
|
|
951
1034
|
} catch (e) {
|
|
952
1035
|
throw e;
|
|
953
1036
|
}
|
|
@@ -961,14 +1044,16 @@ class FAMISDb {
|
|
|
961
1044
|
* @returns {Promise<object>} expense code object
|
|
962
1045
|
* @memberof FAMISDb
|
|
963
1046
|
*/
|
|
964
|
-
|
|
1047
|
+
async getExpenseCode(code) {
|
|
965
1048
|
let records;
|
|
966
1049
|
let retVal;
|
|
967
1050
|
try {
|
|
968
|
-
records = await this.db(
|
|
1051
|
+
records = await this.db("codes")
|
|
1052
|
+
.where("expense", code)
|
|
1053
|
+
.orderBy("id", "desc");
|
|
969
1054
|
if (records.length) {
|
|
970
1055
|
retVal = records[0];
|
|
971
|
-
}
|
|
1056
|
+
}
|
|
972
1057
|
} catch (e) {
|
|
973
1058
|
throw e;
|
|
974
1059
|
}
|
|
@@ -990,33 +1075,35 @@ class FAMISDb {
|
|
|
990
1075
|
let d = new Date();
|
|
991
1076
|
let year = fiscal ? fiscal : this.getCurrentFiscalYear();
|
|
992
1077
|
try {
|
|
993
|
-
records = await this.db(
|
|
994
|
-
.select(
|
|
995
|
-
.where(
|
|
996
|
-
.andWhere(
|
|
997
|
-
.andWhere(
|
|
998
|
-
.andWhere(
|
|
999
|
-
.andWhereRaw(
|
|
1078
|
+
records = await this.db("subcode_edits")
|
|
1079
|
+
.select("allowed")
|
|
1080
|
+
.where("campus_code", accountNumber.substring(0, 2))
|
|
1081
|
+
.andWhere("account", accountNumber.substring(2, 8))
|
|
1082
|
+
.andWhere("support_account", accountNumber.substring(8, 13))
|
|
1083
|
+
.andWhere("fiscal_year", year)
|
|
1084
|
+
.andWhereRaw("? BETWEEN low AND high", [parseInt(code)]);
|
|
1000
1085
|
if (records.length) {
|
|
1001
1086
|
retVal = records[0].allowed;
|
|
1002
1087
|
} else {
|
|
1003
|
-
records = await this.db(
|
|
1004
|
-
.select(
|
|
1005
|
-
.join(
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
.on(
|
|
1010
|
-
.on(
|
|
1088
|
+
records = await this.db("abr_rules")
|
|
1089
|
+
.select("abr_rules.*")
|
|
1090
|
+
.join(
|
|
1091
|
+
"accounts",
|
|
1092
|
+
function () {
|
|
1093
|
+
this.on("accounts.abr_rule", "=", "abr_rules.abr_rule")
|
|
1094
|
+
.on("accounts.campus_code", "=", "abr_rules.campus_code")
|
|
1095
|
+
.on("accounts.fiscal_year", "=", "abr_rules.fiscal_year");
|
|
1011
1096
|
},
|
|
1012
|
-
|
|
1097
|
+
"left",
|
|
1013
1098
|
)
|
|
1014
|
-
.where(
|
|
1015
|
-
.andWhere(
|
|
1016
|
-
.andWhereRaw(
|
|
1099
|
+
.where("accounts.account_number", accountNumber)
|
|
1100
|
+
.andWhere("accounts.fiscal_year", year)
|
|
1101
|
+
.andWhereRaw("? BETWEEN abr_rules.low AND abr_rules.high", [
|
|
1102
|
+
parseInt(code),
|
|
1103
|
+
]);
|
|
1017
1104
|
if (records.length) {
|
|
1018
1105
|
retVal = true;
|
|
1019
|
-
}
|
|
1106
|
+
}
|
|
1020
1107
|
}
|
|
1021
1108
|
} catch (e) {
|
|
1022
1109
|
throw e;
|
|
@@ -1041,50 +1128,50 @@ class FAMISDb {
|
|
|
1041
1128
|
};
|
|
1042
1129
|
let year = fiscal ? fiscal : this.getCurrentFiscalYear();
|
|
1043
1130
|
try {
|
|
1044
|
-
subcode = await this.db(
|
|
1045
|
-
.select(
|
|
1046
|
-
.where(
|
|
1047
|
-
.andWhere(
|
|
1048
|
-
.andWhere(
|
|
1049
|
-
.andWhere(
|
|
1050
|
-
.andWhere(
|
|
1051
|
-
.orderBy(
|
|
1052
|
-
disallowed = await this.db(
|
|
1053
|
-
.select(
|
|
1054
|
-
.where(
|
|
1055
|
-
.andWhere(
|
|
1056
|
-
.andWhere(
|
|
1057
|
-
.andWhere(
|
|
1058
|
-
.andWhere(
|
|
1059
|
-
.orderBy(
|
|
1060
|
-
abr = await this.db(
|
|
1061
|
-
.select(
|
|
1062
|
-
.join(
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
.on(
|
|
1067
|
-
.on(
|
|
1131
|
+
subcode = await this.db("subcode_edits")
|
|
1132
|
+
.select("low", "high")
|
|
1133
|
+
.where("campus_code", accountNumber.substring(0, 2))
|
|
1134
|
+
.andWhere("account", accountNumber.substring(2, 8))
|
|
1135
|
+
.andWhere("support_account", accountNumber.substring(8, 13))
|
|
1136
|
+
.andWhere("fiscal_year", year)
|
|
1137
|
+
.andWhere("allowed", true)
|
|
1138
|
+
.orderBy("low", "asc");
|
|
1139
|
+
disallowed = await this.db("subcode_edits")
|
|
1140
|
+
.select("low", "high")
|
|
1141
|
+
.where("campus_code", accountNumber.substring(0, 2))
|
|
1142
|
+
.andWhere("account", accountNumber.substring(2, 8))
|
|
1143
|
+
.andWhere("support_account", accountNumber.substring(8, 13))
|
|
1144
|
+
.andWhere("fiscal_year", year)
|
|
1145
|
+
.andWhere("allowed", false)
|
|
1146
|
+
.orderBy("low", "asc");
|
|
1147
|
+
abr = await this.db("abr_rules")
|
|
1148
|
+
.select("abr_rules.low", "abr_rules.high")
|
|
1149
|
+
.join(
|
|
1150
|
+
"accounts",
|
|
1151
|
+
function () {
|
|
1152
|
+
this.on("accounts.abr_rule", "=", "abr_rules.abr_rule")
|
|
1153
|
+
.on("accounts.campus_code", "=", "abr_rules.campus_code")
|
|
1154
|
+
.on("accounts.fiscal_year", "=", "abr_rules.fiscal_year");
|
|
1068
1155
|
},
|
|
1069
|
-
|
|
1156
|
+
"left",
|
|
1070
1157
|
)
|
|
1071
|
-
.where(
|
|
1072
|
-
.andWhere(
|
|
1073
|
-
.orderBy(
|
|
1158
|
+
.where("accounts.account_number", accountNumber)
|
|
1159
|
+
.andWhere("accounts.fiscal_year", year)
|
|
1160
|
+
.orderBy("low", "asc");
|
|
1074
1161
|
if (abr.length || subcode.length) {
|
|
1075
1162
|
const allowed = [...abr, ...subcode];
|
|
1076
1163
|
retVal = {
|
|
1077
1164
|
allowed,
|
|
1078
1165
|
disallowed,
|
|
1079
1166
|
};
|
|
1080
|
-
}
|
|
1167
|
+
}
|
|
1081
1168
|
} catch (e) {
|
|
1082
1169
|
throw e;
|
|
1083
1170
|
}
|
|
1084
1171
|
return retVal;
|
|
1085
1172
|
}
|
|
1086
1173
|
|
|
1087
|
-
|
|
1174
|
+
/**
|
|
1088
1175
|
*
|
|
1089
1176
|
* Get core by id
|
|
1090
1177
|
* @param {string} id
|
|
@@ -1095,54 +1182,52 @@ class FAMISDb {
|
|
|
1095
1182
|
let records;
|
|
1096
1183
|
let retVal;
|
|
1097
1184
|
try {
|
|
1098
|
-
const maxDates = this.db(
|
|
1099
|
-
.select(
|
|
1100
|
-
|
|
1101
|
-
)
|
|
1102
|
-
.
|
|
1103
|
-
.groupBy('coreId')
|
|
1104
|
-
.where('effective', '<=', this.db.raw('now()'))
|
|
1185
|
+
const maxDates = this.db("core_accounts")
|
|
1186
|
+
.select("coreId")
|
|
1187
|
+
.max("effective", { as: "effective" })
|
|
1188
|
+
.groupBy("coreId")
|
|
1189
|
+
.where("effective", "<=", this.db.raw("now()"))
|
|
1105
1190
|
.as("max_dates");
|
|
1106
|
-
const accounts = this.db(
|
|
1191
|
+
const accounts = this.db("core_accounts")
|
|
1107
1192
|
.select(
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
)
|
|
1112
|
-
.join(maxDates,
|
|
1113
|
-
function() {
|
|
1114
|
-
this
|
|
1115
|
-
.on('core_accounts.coreId', '=', 'max_dates.coreId')
|
|
1116
|
-
.andOn('core_accounts.effective', '=', 'max_dates.effective')
|
|
1117
|
-
}
|
|
1193
|
+
"core_accounts.coreId",
|
|
1194
|
+
"core_accounts.account",
|
|
1195
|
+
"core_accounts.effective",
|
|
1118
1196
|
)
|
|
1197
|
+
.join(maxDates, function () {
|
|
1198
|
+
this.on("core_accounts.coreId", "=", "max_dates.coreId").andOn(
|
|
1199
|
+
"core_accounts.effective",
|
|
1200
|
+
"=",
|
|
1201
|
+
"max_dates.effective",
|
|
1202
|
+
);
|
|
1203
|
+
})
|
|
1119
1204
|
.as("core_accounts");
|
|
1120
|
-
records = await this.db(
|
|
1205
|
+
records = await this.db("cores")
|
|
1121
1206
|
.select(
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1207
|
+
"cores.id",
|
|
1208
|
+
"core_accounts.account",
|
|
1209
|
+
"core_accounts.effective",
|
|
1210
|
+
"cores.facility",
|
|
1211
|
+
"cores.contact",
|
|
1212
|
+
"cores.contactEmail",
|
|
1213
|
+
"cores.systemId",
|
|
1214
|
+
"system.name as partName",
|
|
1215
|
+
"system.part as partNumber",
|
|
1216
|
+
"cores.enabled",
|
|
1217
|
+
"cores.deleted",
|
|
1218
|
+
"cores.coreEmail",
|
|
1219
|
+
"cores.corePhone",
|
|
1220
|
+
"cores.coreAddress",
|
|
1221
|
+
"cores.from_email",
|
|
1222
|
+
"cores.invoice_email",
|
|
1138
1223
|
)
|
|
1139
|
-
.join(accounts,
|
|
1140
|
-
.join(
|
|
1141
|
-
.where(
|
|
1142
|
-
.orderBy(
|
|
1224
|
+
.join(accounts, "cores.id", "=", "core_accounts.coreId")
|
|
1225
|
+
.join("system", "cores.systemId", "=", "system.id")
|
|
1226
|
+
.where("cores.id", id)
|
|
1227
|
+
.orderBy("cores.id", "desc");
|
|
1143
1228
|
if (records.length) {
|
|
1144
1229
|
retVal = records[0];
|
|
1145
|
-
}
|
|
1230
|
+
}
|
|
1146
1231
|
} catch (e) {
|
|
1147
1232
|
throw e;
|
|
1148
1233
|
}
|
|
@@ -1160,30 +1245,30 @@ class FAMISDb {
|
|
|
1160
1245
|
let records;
|
|
1161
1246
|
let retVal;
|
|
1162
1247
|
try {
|
|
1163
|
-
records = await this.db(
|
|
1248
|
+
records = await this.db("cores")
|
|
1164
1249
|
.select(
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1250
|
+
"cores.id",
|
|
1251
|
+
"core_accounts.account",
|
|
1252
|
+
"core_accounts.effective",
|
|
1253
|
+
"core_accounts.active",
|
|
1254
|
+
"cores.facility",
|
|
1255
|
+
"cores.contact",
|
|
1256
|
+
"cores.contactEmail",
|
|
1257
|
+
"cores.systemId",
|
|
1258
|
+
"cores.enabled",
|
|
1259
|
+
"cores.deleted",
|
|
1260
|
+
"cores.coreEmail",
|
|
1261
|
+
"cores.corePhone",
|
|
1262
|
+
"cores.coreAddress",
|
|
1263
|
+
"cores.from_email",
|
|
1264
|
+
"cores.invoice_email",
|
|
1180
1265
|
)
|
|
1181
|
-
.join(
|
|
1182
|
-
.where(
|
|
1183
|
-
.orderBy(
|
|
1266
|
+
.join("core_accounts", "cores.id", "=", "core_accounts.coreId")
|
|
1267
|
+
.where("core_accounts.account", account)
|
|
1268
|
+
.orderBy("cores.id", "desc");
|
|
1184
1269
|
if (records.length) {
|
|
1185
1270
|
retVal = records[0];
|
|
1186
|
-
}
|
|
1271
|
+
}
|
|
1187
1272
|
} catch (e) {
|
|
1188
1273
|
throw e;
|
|
1189
1274
|
}
|
|
@@ -1201,51 +1286,49 @@ class FAMISDb {
|
|
|
1201
1286
|
let records;
|
|
1202
1287
|
let retVal;
|
|
1203
1288
|
try {
|
|
1204
|
-
const maxDates = this.db(
|
|
1205
|
-
.select(
|
|
1206
|
-
|
|
1207
|
-
)
|
|
1208
|
-
.
|
|
1209
|
-
.
|
|
1210
|
-
.where('effective', '<=', this.db.raw('now()'))
|
|
1211
|
-
.andWhere('coreId', '=', coreId)
|
|
1289
|
+
const maxDates = this.db("core_accounts")
|
|
1290
|
+
.select("coreId")
|
|
1291
|
+
.max("effective", { as: "effective" })
|
|
1292
|
+
.groupBy("coreId")
|
|
1293
|
+
.where("effective", "<=", this.db.raw("now()"))
|
|
1294
|
+
.andWhere("coreId", "=", coreId)
|
|
1212
1295
|
.as("max_dates");
|
|
1213
|
-
const accounts = this.db(
|
|
1296
|
+
const accounts = this.db("core_accounts")
|
|
1214
1297
|
.select(
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
)
|
|
1219
|
-
.join(maxDates,
|
|
1220
|
-
function() {
|
|
1221
|
-
this
|
|
1222
|
-
.on('core_accounts.coreId', '=', 'max_dates.coreId')
|
|
1223
|
-
.andOn('core_accounts.effective', '=', 'max_dates.effective')
|
|
1224
|
-
}
|
|
1298
|
+
"core_accounts.coreId",
|
|
1299
|
+
"core_accounts.account",
|
|
1300
|
+
"core_accounts.effective",
|
|
1225
1301
|
)
|
|
1302
|
+
.join(maxDates, function () {
|
|
1303
|
+
this.on("core_accounts.coreId", "=", "max_dates.coreId").andOn(
|
|
1304
|
+
"core_accounts.effective",
|
|
1305
|
+
"=",
|
|
1306
|
+
"max_dates.effective",
|
|
1307
|
+
);
|
|
1308
|
+
})
|
|
1226
1309
|
.as("core_accounts");
|
|
1227
|
-
records = await this.db(
|
|
1310
|
+
records = await this.db("cores")
|
|
1228
1311
|
.select(
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1312
|
+
"cores.id",
|
|
1313
|
+
"core_accounts.account",
|
|
1314
|
+
"core_accounts.effective",
|
|
1315
|
+
"cores.facility",
|
|
1316
|
+
"cores.contact",
|
|
1317
|
+
"cores.contactEmail",
|
|
1318
|
+
"cores.systemId",
|
|
1319
|
+
"cores.enabled",
|
|
1320
|
+
"cores.deleted",
|
|
1321
|
+
"cores.coreEmail",
|
|
1322
|
+
"cores.corePhone",
|
|
1323
|
+
"cores.coreAddress",
|
|
1324
|
+
"cores.from_email",
|
|
1325
|
+
"cores.invoice_email",
|
|
1243
1326
|
)
|
|
1244
|
-
.join(accounts,
|
|
1245
|
-
.orderBy(
|
|
1327
|
+
.join(accounts, "cores.id", "=", "core_accounts.coreId")
|
|
1328
|
+
.orderBy("cores.id", "desc");
|
|
1246
1329
|
if (records.length) {
|
|
1247
1330
|
retVal = records[0];
|
|
1248
|
-
}
|
|
1331
|
+
}
|
|
1249
1332
|
} catch (e) {
|
|
1250
1333
|
throw e;
|
|
1251
1334
|
}
|
|
@@ -1263,10 +1346,10 @@ class FAMISDb {
|
|
|
1263
1346
|
let records;
|
|
1264
1347
|
let retVal;
|
|
1265
1348
|
try {
|
|
1266
|
-
records = await this.db(
|
|
1349
|
+
records = await this.db("system").where("id", id).orderBy("id", "desc");
|
|
1267
1350
|
if (records.length) {
|
|
1268
1351
|
retVal = records[0];
|
|
1269
|
-
}
|
|
1352
|
+
}
|
|
1270
1353
|
} catch (e) {
|
|
1271
1354
|
throw e;
|
|
1272
1355
|
}
|
|
@@ -1284,10 +1367,12 @@ class FAMISDb {
|
|
|
1284
1367
|
let records;
|
|
1285
1368
|
let retVal;
|
|
1286
1369
|
try {
|
|
1287
|
-
records = await this.db(
|
|
1370
|
+
records = await this.db("system")
|
|
1371
|
+
.where("part", number)
|
|
1372
|
+
.orderBy("id", "desc");
|
|
1288
1373
|
if (records.length) {
|
|
1289
1374
|
retVal = records[0];
|
|
1290
|
-
}
|
|
1375
|
+
}
|
|
1291
1376
|
} catch (e) {
|
|
1292
1377
|
throw e;
|
|
1293
1378
|
}
|
|
@@ -1305,51 +1390,49 @@ class FAMISDb {
|
|
|
1305
1390
|
let records;
|
|
1306
1391
|
let retVal = [];
|
|
1307
1392
|
try {
|
|
1308
|
-
const maxDates = this.db(
|
|
1309
|
-
.select(
|
|
1310
|
-
|
|
1311
|
-
)
|
|
1312
|
-
.
|
|
1313
|
-
.groupBy('coreId')
|
|
1314
|
-
.where('effective', '<=', date ? date : this.db.raw('now()'))
|
|
1393
|
+
const maxDates = this.db("core_accounts")
|
|
1394
|
+
.select("coreId")
|
|
1395
|
+
.max("effective", { as: "effective" })
|
|
1396
|
+
.groupBy("coreId")
|
|
1397
|
+
.where("effective", "<=", date ? date : this.db.raw("now()"))
|
|
1315
1398
|
.as("max_dates");
|
|
1316
|
-
const accounts = this.db(
|
|
1399
|
+
const accounts = this.db("core_accounts")
|
|
1317
1400
|
.select(
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
)
|
|
1322
|
-
.join(maxDates,
|
|
1323
|
-
function() {
|
|
1324
|
-
this
|
|
1325
|
-
.on('core_accounts.coreId', '=', 'max_dates.coreId')
|
|
1326
|
-
.andOn('core_accounts.effective', '=', 'max_dates.effective')
|
|
1327
|
-
}
|
|
1401
|
+
"core_accounts.coreId",
|
|
1402
|
+
"core_accounts.account",
|
|
1403
|
+
"core_accounts.effective",
|
|
1328
1404
|
)
|
|
1405
|
+
.join(maxDates, function () {
|
|
1406
|
+
this.on("core_accounts.coreId", "=", "max_dates.coreId").andOn(
|
|
1407
|
+
"core_accounts.effective",
|
|
1408
|
+
"=",
|
|
1409
|
+
"max_dates.effective",
|
|
1410
|
+
);
|
|
1411
|
+
})
|
|
1329
1412
|
.as("core_accounts");
|
|
1330
|
-
records = await this.db(
|
|
1413
|
+
records = await this.db("cores")
|
|
1331
1414
|
.select(
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1415
|
+
"cores.id",
|
|
1416
|
+
"core_accounts.account",
|
|
1417
|
+
"core_accounts.effective",
|
|
1418
|
+
"cores.facility",
|
|
1419
|
+
"cores.contact",
|
|
1420
|
+
"cores.contactEmail",
|
|
1421
|
+
"cores.systemId",
|
|
1422
|
+
"cores.enabled",
|
|
1423
|
+
"cores.deleted",
|
|
1424
|
+
"cores.coreEmail",
|
|
1425
|
+
"cores.corePhone",
|
|
1426
|
+
"cores.coreAddress",
|
|
1427
|
+
"cores.from_email",
|
|
1428
|
+
"cores.invoice_email",
|
|
1346
1429
|
)
|
|
1347
|
-
.join(accounts,
|
|
1348
|
-
.where(
|
|
1349
|
-
.orderBy(
|
|
1430
|
+
.join(accounts, "cores.id", "=", "core_accounts.coreId")
|
|
1431
|
+
.where("enabled", "1")
|
|
1432
|
+
.orderBy("cores.id", "desc");
|
|
1350
1433
|
if (records.length) {
|
|
1351
1434
|
retVal = records;
|
|
1352
|
-
}
|
|
1435
|
+
}
|
|
1353
1436
|
} catch (e) {
|
|
1354
1437
|
throw e;
|
|
1355
1438
|
}
|
|
@@ -1366,10 +1449,10 @@ class FAMISDb {
|
|
|
1366
1449
|
let records;
|
|
1367
1450
|
let retVal = [];
|
|
1368
1451
|
try {
|
|
1369
|
-
records = await this.db(
|
|
1452
|
+
records = await this.db("cores").orderBy("id", "asc");
|
|
1370
1453
|
if (records.length) {
|
|
1371
1454
|
retVal = records;
|
|
1372
|
-
}
|
|
1455
|
+
}
|
|
1373
1456
|
} catch (e) {
|
|
1374
1457
|
throw e;
|
|
1375
1458
|
}
|
|
@@ -1386,14 +1469,14 @@ class FAMISDb {
|
|
|
1386
1469
|
async getCoreUsers(coreId) {
|
|
1387
1470
|
let retVal;
|
|
1388
1471
|
try {
|
|
1389
|
-
retVal = await this.db(
|
|
1390
|
-
.join(
|
|
1391
|
-
.where(
|
|
1472
|
+
retVal = await this.db("user_cores")
|
|
1473
|
+
.join("users", "user_cores.userId", "=", "users.id")
|
|
1474
|
+
.where("coreId", coreId)
|
|
1392
1475
|
.select(
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1476
|
+
"users.*",
|
|
1477
|
+
"user_cores.id as userCoreId",
|
|
1478
|
+
"user_cores.coreId",
|
|
1479
|
+
"user_cores.email",
|
|
1397
1480
|
);
|
|
1398
1481
|
} catch (e) {
|
|
1399
1482
|
throw e;
|
|
@@ -1411,15 +1494,15 @@ class FAMISDb {
|
|
|
1411
1494
|
async getUserCores(userId) {
|
|
1412
1495
|
let retVal;
|
|
1413
1496
|
try {
|
|
1414
|
-
retVal = await this.db(
|
|
1415
|
-
.join(
|
|
1416
|
-
.join(
|
|
1417
|
-
.where(
|
|
1497
|
+
retVal = await this.db("user_cores")
|
|
1498
|
+
.join("cores", "user_cores.coreId", "=", "cores.id")
|
|
1499
|
+
.join("users", "user_cores.userId", "=", "users.id")
|
|
1500
|
+
.where("users.userId", userId)
|
|
1418
1501
|
.select(
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1502
|
+
"cores.*",
|
|
1503
|
+
"user_cores.id as userCoreId",
|
|
1504
|
+
"user_cores.userId",
|
|
1505
|
+
"user_cores.email",
|
|
1423
1506
|
);
|
|
1424
1507
|
} catch (e) {
|
|
1425
1508
|
throw e;
|
|
@@ -1437,37 +1520,37 @@ class FAMISDb {
|
|
|
1437
1520
|
let records;
|
|
1438
1521
|
let retVal = [];
|
|
1439
1522
|
try {
|
|
1440
|
-
const accounts = this.db(
|
|
1523
|
+
const accounts = this.db("core_accounts")
|
|
1441
1524
|
.select(
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1525
|
+
"core_accounts.coreId",
|
|
1526
|
+
"core_accounts.account",
|
|
1527
|
+
"core_accounts.active",
|
|
1445
1528
|
)
|
|
1446
|
-
.where({active: true})
|
|
1529
|
+
.where({ active: true })
|
|
1447
1530
|
.as("core_accounts");
|
|
1448
|
-
records = await this.db(
|
|
1531
|
+
records = await this.db("cores")
|
|
1449
1532
|
.select(
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1533
|
+
"cores.id",
|
|
1534
|
+
"core_accounts.account",
|
|
1535
|
+
"core_accounts.active",
|
|
1536
|
+
"cores.facility",
|
|
1537
|
+
"cores.contact",
|
|
1538
|
+
"cores.contactEmail",
|
|
1539
|
+
"cores.systemId",
|
|
1540
|
+
"cores.enabled",
|
|
1541
|
+
"cores.deleted",
|
|
1542
|
+
"cores.coreEmail",
|
|
1543
|
+
"cores.corePhone",
|
|
1544
|
+
"cores.coreAddress",
|
|
1545
|
+
"cores.from_email",
|
|
1546
|
+
"cores.invoice_email",
|
|
1464
1547
|
)
|
|
1465
|
-
.join(accounts,
|
|
1466
|
-
.where({enabled: true})
|
|
1467
|
-
.orderBy(
|
|
1548
|
+
.join(accounts, "cores.id", "=", "core_accounts.coreId")
|
|
1549
|
+
.where({ enabled: true })
|
|
1550
|
+
.orderBy("cores.id", "desc");
|
|
1468
1551
|
if (records.length) {
|
|
1469
1552
|
retVal = records;
|
|
1470
|
-
}
|
|
1553
|
+
}
|
|
1471
1554
|
} catch (e) {
|
|
1472
1555
|
throw e;
|
|
1473
1556
|
}
|
|
@@ -1484,14 +1567,14 @@ class FAMISDb {
|
|
|
1484
1567
|
async getSystemUsers(systemId) {
|
|
1485
1568
|
let retVal;
|
|
1486
1569
|
try {
|
|
1487
|
-
retVal = await this.db(
|
|
1488
|
-
.join(
|
|
1489
|
-
.where(
|
|
1570
|
+
retVal = await this.db("user_systems")
|
|
1571
|
+
.join("users", "user_systems.userId", "=", "users.id")
|
|
1572
|
+
.where("systemId", systemId)
|
|
1490
1573
|
.select(
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1574
|
+
"users.*",
|
|
1575
|
+
"user_systems.id as userSystemId",
|
|
1576
|
+
"user_systems.systemId",
|
|
1577
|
+
"user_systems.email",
|
|
1495
1578
|
);
|
|
1496
1579
|
} catch (e) {
|
|
1497
1580
|
throw e;
|
|
@@ -1510,15 +1593,15 @@ class FAMISDb {
|
|
|
1510
1593
|
let records;
|
|
1511
1594
|
let retVal;
|
|
1512
1595
|
try {
|
|
1513
|
-
retVal = await this.db(
|
|
1514
|
-
.join(
|
|
1515
|
-
.join(
|
|
1516
|
-
.where(
|
|
1596
|
+
retVal = await this.db("user_systems")
|
|
1597
|
+
.join("system", "user_systems.systemId", "=", "system.id")
|
|
1598
|
+
.join("users", "user_systems.userId", "=", "users.id")
|
|
1599
|
+
.where("users.userId", userId)
|
|
1517
1600
|
.select(
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1601
|
+
"system.*",
|
|
1602
|
+
"user_systems.id as userSystemId",
|
|
1603
|
+
"user_systems.userId",
|
|
1604
|
+
"user_systems.email",
|
|
1522
1605
|
);
|
|
1523
1606
|
} catch (e) {
|
|
1524
1607
|
throw e;
|
|
@@ -1536,7 +1619,7 @@ class FAMISDb {
|
|
|
1536
1619
|
let records;
|
|
1537
1620
|
let retVal;
|
|
1538
1621
|
try {
|
|
1539
|
-
retVal = await this.db(
|
|
1622
|
+
retVal = await this.db("system").orderBy("id", "asc");
|
|
1540
1623
|
} catch (e) {
|
|
1541
1624
|
throw e;
|
|
1542
1625
|
}
|
|
@@ -1549,10 +1632,10 @@ class FAMISDb {
|
|
|
1549
1632
|
* @returns {Promise<array>} array of user objects
|
|
1550
1633
|
* @memberof FAMISDb
|
|
1551
1634
|
*/
|
|
1552
|
-
|
|
1635
|
+
async getAdminUsers() {
|
|
1553
1636
|
let retVal;
|
|
1554
1637
|
try {
|
|
1555
|
-
retVal = await this.db(
|
|
1638
|
+
retVal = await this.db("users").where("admin", true);
|
|
1556
1639
|
} catch (e) {
|
|
1557
1640
|
throw e;
|
|
1558
1641
|
}
|
|
@@ -1570,12 +1653,12 @@ class FAMISDb {
|
|
|
1570
1653
|
let records;
|
|
1571
1654
|
let retVal;
|
|
1572
1655
|
try {
|
|
1573
|
-
records = await this.db(
|
|
1656
|
+
records = await this.db("users").where("userId", userId);
|
|
1574
1657
|
if (records.length) {
|
|
1575
1658
|
retVal = records[0];
|
|
1576
1659
|
retVal.cores = await this.getUserCores(userId);
|
|
1577
1660
|
retVal.systems = await this.getUserSystemParts(userId);
|
|
1578
|
-
}
|
|
1661
|
+
}
|
|
1579
1662
|
} catch (e) {
|
|
1580
1663
|
throw e;
|
|
1581
1664
|
}
|
|
@@ -1589,14 +1672,23 @@ class FAMISDb {
|
|
|
1589
1672
|
* @returns {Promise<object>} user object
|
|
1590
1673
|
* @memberof FAMISDb
|
|
1591
1674
|
*/
|
|
1592
|
-
|
|
1675
|
+
async updateUser(user) {
|
|
1593
1676
|
let retVal;
|
|
1594
|
-
const columns = [
|
|
1677
|
+
const columns = [
|
|
1678
|
+
"id",
|
|
1679
|
+
"userId",
|
|
1680
|
+
"admin",
|
|
1681
|
+
"role",
|
|
1682
|
+
"roleName",
|
|
1683
|
+
"roleEmail",
|
|
1684
|
+
"deleted",
|
|
1685
|
+
];
|
|
1595
1686
|
try {
|
|
1596
|
-
if (
|
|
1597
|
-
.
|
|
1598
|
-
|
|
1599
|
-
|
|
1687
|
+
if ("id" in user)
|
|
1688
|
+
retVal = await this.db("users")
|
|
1689
|
+
.returning(columns)
|
|
1690
|
+
.update(user)
|
|
1691
|
+
.where("id", user.id);
|
|
1600
1692
|
} catch (e) {
|
|
1601
1693
|
throw e;
|
|
1602
1694
|
}
|
|
@@ -1617,8 +1709,10 @@ class FAMISDb {
|
|
|
1617
1709
|
const code = pad(campusCode, 2);
|
|
1618
1710
|
|
|
1619
1711
|
try {
|
|
1620
|
-
records = await this.db(
|
|
1621
|
-
|
|
1712
|
+
records = await this.db("customers").where(
|
|
1713
|
+
"uid",
|
|
1714
|
+
`${code}${accountNumber}`,
|
|
1715
|
+
);
|
|
1622
1716
|
if (records.length) {
|
|
1623
1717
|
retVal = records[0];
|
|
1624
1718
|
}
|
|
@@ -1637,7 +1731,7 @@ class FAMISDb {
|
|
|
1637
1731
|
getCurrentFiscalYear() {
|
|
1638
1732
|
let year;
|
|
1639
1733
|
let today = new Date();
|
|
1640
|
-
if (
|
|
1734
|
+
if (today.getMonth() + 1 >= 9) {
|
|
1641
1735
|
year = today.getFullYear() + 1;
|
|
1642
1736
|
} else {
|
|
1643
1737
|
year = today.getFullYear();
|
|
@@ -1654,10 +1748,9 @@ class FAMISDb {
|
|
|
1654
1748
|
*/
|
|
1655
1749
|
getInvoiceFingerPrint(invoice) {
|
|
1656
1750
|
const fingerPrint = revisionHash(
|
|
1657
|
-
JSON.stringify(
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
)
|
|
1751
|
+
JSON.stringify(sortObjectKeys(invoice), (k, v) =>
|
|
1752
|
+
undefined ? "undef" : v,
|
|
1753
|
+
),
|
|
1661
1754
|
);
|
|
1662
1755
|
return fingerPrint;
|
|
1663
1756
|
}
|
|
@@ -1670,16 +1763,19 @@ class FAMISDb {
|
|
|
1670
1763
|
* @memberof FAMISDb
|
|
1671
1764
|
*/
|
|
1672
1765
|
formatAccountNumber(accountNumber) {
|
|
1673
|
-
const formatted = accountNumber.replace(
|
|
1766
|
+
const formatted = accountNumber.replace(
|
|
1767
|
+
/(\d{2})(\d{6})(\d{5})/,
|
|
1768
|
+
"$1-$2-$3",
|
|
1769
|
+
);
|
|
1674
1770
|
|
|
1675
1771
|
return formatted;
|
|
1676
1772
|
}
|
|
1677
1773
|
|
|
1678
1774
|
/**
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1775
|
+
* @typedef {Object} HighRisk
|
|
1776
|
+
* @property {boolean} isHighRisk - indicates if invoice triggers SRS high risk policy
|
|
1777
|
+
* @property {object} flaggedTransactions - object of transactions that flag
|
|
1778
|
+
*/
|
|
1683
1779
|
|
|
1684
1780
|
/**
|
|
1685
1781
|
*
|
|
@@ -1693,25 +1789,46 @@ class FAMISDb {
|
|
|
1693
1789
|
const objectCodes = new Map();
|
|
1694
1790
|
//console.log(invoice);
|
|
1695
1791
|
try {
|
|
1696
|
-
await each(
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1792
|
+
await each(invoice.data.details, async (detail) => {
|
|
1793
|
+
//console.log(detail);
|
|
1794
|
+
if (
|
|
1795
|
+
detail.famis.customerAccountNumber &&
|
|
1796
|
+
detail.famis.sellingCampusCode
|
|
1797
|
+
) {
|
|
1798
|
+
const acct = await this.getByAccountNumber(
|
|
1799
|
+
detail.famis.customerAccountNumber,
|
|
1800
|
+
detail.famis.sellingCampusCode,
|
|
1801
|
+
);
|
|
1802
|
+
const acctNum = this.formatAccountNumber(
|
|
1803
|
+
detail.famis.customerAccountNumber,
|
|
1804
|
+
);
|
|
1805
|
+
const objCode = await this.getRevenueCode(detail.sellingObjectCode);
|
|
1806
|
+
const isFederal =
|
|
1807
|
+
acct &&
|
|
1808
|
+
acct.cfda_no &&
|
|
1809
|
+
acct.sponsor_type &&
|
|
1810
|
+
acct.sponsor_type === "F";
|
|
1811
|
+
const srsApproved =
|
|
1812
|
+
invoice.replace_data && "srs_approved" in invoice.replace_data
|
|
1813
|
+
? invoice.replace_data.srs_approved
|
|
1814
|
+
: false;
|
|
1815
|
+
if (
|
|
1816
|
+
objCode &&
|
|
1817
|
+
objCode.srs &&
|
|
1818
|
+
acct &&
|
|
1819
|
+
acct.srs &&
|
|
1820
|
+
isFederal &&
|
|
1821
|
+
!srsApproved &&
|
|
1822
|
+
invoice.isIdt
|
|
1823
|
+
) {
|
|
1824
|
+
srsHighRisk = true;
|
|
1825
|
+
let val = [detail];
|
|
1826
|
+
if (objectCodes.has(objCode.expense))
|
|
1827
|
+
val.push(...objectCodes.get(objCode.expense));
|
|
1828
|
+
objectCodes.set(objCode.expense, val);
|
|
1712
1829
|
}
|
|
1713
1830
|
}
|
|
1714
|
-
);
|
|
1831
|
+
});
|
|
1715
1832
|
} catch (e) {
|
|
1716
1833
|
//console.log(invoice);
|
|
1717
1834
|
console.log(e);
|
|
@@ -1719,7 +1836,7 @@ class FAMISDb {
|
|
|
1719
1836
|
return {
|
|
1720
1837
|
isHighRisk: srsHighRisk,
|
|
1721
1838
|
flaggedTransactions: Object.fromEntries(objectCodes),
|
|
1722
|
-
}
|
|
1839
|
+
};
|
|
1723
1840
|
}
|
|
1724
1841
|
|
|
1725
1842
|
/**
|
|
@@ -1743,25 +1860,27 @@ class FAMISDb {
|
|
|
1743
1860
|
*/
|
|
1744
1861
|
async saveComment(comment) {
|
|
1745
1862
|
const returningColumns = [
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1863
|
+
"id",
|
|
1864
|
+
"uid",
|
|
1865
|
+
"invoice_id",
|
|
1866
|
+
"user_id",
|
|
1867
|
+
"comment",
|
|
1868
|
+
"type",
|
|
1869
|
+
"created",
|
|
1870
|
+
"deleted",
|
|
1754
1871
|
];
|
|
1755
1872
|
let response;
|
|
1756
1873
|
try {
|
|
1757
|
-
const records = await this.db(
|
|
1874
|
+
const records = await this.db("invoice_comments").where({
|
|
1875
|
+
uid: comment.uid,
|
|
1876
|
+
});
|
|
1758
1877
|
if (records.length) {
|
|
1759
|
-
response = await this.db(
|
|
1878
|
+
response = await this.db("invoice_comments")
|
|
1760
1879
|
.returning(returningColumns)
|
|
1761
1880
|
.update(comment)
|
|
1762
|
-
.where(
|
|
1881
|
+
.where("uid", comment.uid);
|
|
1763
1882
|
} else {
|
|
1764
|
-
response = await this.db(
|
|
1883
|
+
response = await this.db("invoice_comments")
|
|
1765
1884
|
.returning(returningColumns)
|
|
1766
1885
|
.insert(comment);
|
|
1767
1886
|
}
|
|
@@ -1772,7 +1891,7 @@ class FAMISDb {
|
|
|
1772
1891
|
} catch (e) {
|
|
1773
1892
|
//console.log(JSON.stringify(comment, null, 2));
|
|
1774
1893
|
console.log(e);
|
|
1775
|
-
throw
|
|
1894
|
+
throw e;
|
|
1776
1895
|
}
|
|
1777
1896
|
return response;
|
|
1778
1897
|
}
|
|
@@ -1788,30 +1907,25 @@ class FAMISDb {
|
|
|
1788
1907
|
let self = this;
|
|
1789
1908
|
let response;
|
|
1790
1909
|
try {
|
|
1791
|
-
const records = await this.db(
|
|
1792
|
-
.select(
|
|
1793
|
-
'invoice_comments.*',
|
|
1794
|
-
)
|
|
1910
|
+
const records = await this.db("invoice_comments")
|
|
1911
|
+
.select("invoice_comments.*")
|
|
1795
1912
|
.where({ invoice_id: invoiceUid })
|
|
1796
|
-
.orderBy(
|
|
1797
|
-
response = await map(
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
if (
|
|
1804
|
-
|
|
1805
|
-
if (user) {
|
|
1806
|
-
retVal.user = user;
|
|
1807
|
-
}
|
|
1913
|
+
.orderBy("created", "desc");
|
|
1914
|
+
response = await map(records, async (r) => {
|
|
1915
|
+
const retVal = r;
|
|
1916
|
+
const files = await self.getCommentFiles(r.uid);
|
|
1917
|
+
r.files = files;
|
|
1918
|
+
if (self.people) {
|
|
1919
|
+
const user = await self.people.getByUid(r.user_id);
|
|
1920
|
+
if (user) {
|
|
1921
|
+
retVal.user = user;
|
|
1808
1922
|
}
|
|
1809
|
-
return retVal;
|
|
1810
1923
|
}
|
|
1811
|
-
|
|
1924
|
+
return retVal;
|
|
1925
|
+
});
|
|
1812
1926
|
} catch (e) {
|
|
1813
1927
|
console.log(e);
|
|
1814
|
-
throw
|
|
1928
|
+
throw e;
|
|
1815
1929
|
}
|
|
1816
1930
|
return response;
|
|
1817
1931
|
}
|
|
@@ -1837,25 +1951,27 @@ class FAMISDb {
|
|
|
1837
1951
|
*/
|
|
1838
1952
|
async saveCommentFile(commentFile) {
|
|
1839
1953
|
const returningColumns = [
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1954
|
+
"id",
|
|
1955
|
+
"uid",
|
|
1956
|
+
"invoice_id",
|
|
1957
|
+
"comment_id",
|
|
1958
|
+
"original_filename",
|
|
1959
|
+
"filename",
|
|
1960
|
+
"created",
|
|
1961
|
+
"deleted",
|
|
1848
1962
|
];
|
|
1849
1963
|
let response;
|
|
1850
1964
|
try {
|
|
1851
|
-
const records = await this.db(
|
|
1965
|
+
const records = await this.db("invoice_comment_files").where({
|
|
1966
|
+
uid: commentFile.uid,
|
|
1967
|
+
});
|
|
1852
1968
|
if (records.length) {
|
|
1853
|
-
response = await this.db(
|
|
1969
|
+
response = await this.db("invoice_comment_files")
|
|
1854
1970
|
.returning(returningColumns)
|
|
1855
1971
|
.update(commentFile)
|
|
1856
|
-
.where(
|
|
1972
|
+
.where("uid", commentFile.uid);
|
|
1857
1973
|
} else {
|
|
1858
|
-
response = await this.db(
|
|
1974
|
+
response = await this.db("invoice_comment_files")
|
|
1859
1975
|
.returning(returningColumns)
|
|
1860
1976
|
.insert(commentFile);
|
|
1861
1977
|
}
|
|
@@ -1866,7 +1982,7 @@ class FAMISDb {
|
|
|
1866
1982
|
} catch (e) {
|
|
1867
1983
|
//console.log(JSON.stringify(comment, null, 2));
|
|
1868
1984
|
console.log(e);
|
|
1869
|
-
throw
|
|
1985
|
+
throw e;
|
|
1870
1986
|
}
|
|
1871
1987
|
return response;
|
|
1872
1988
|
}
|
|
@@ -1881,13 +1997,13 @@ class FAMISDb {
|
|
|
1881
1997
|
async getCommentFiles(commentUid) {
|
|
1882
1998
|
let response;
|
|
1883
1999
|
try {
|
|
1884
|
-
const records = await this.db(
|
|
2000
|
+
const records = await this.db("invoice_comment_files")
|
|
1885
2001
|
.where({ comment_id: commentUid })
|
|
1886
|
-
.orderBy(
|
|
2002
|
+
.orderBy("created", "desc");
|
|
1887
2003
|
response = records;
|
|
1888
2004
|
} catch (e) {
|
|
1889
2005
|
console.log(e);
|
|
1890
|
-
throw
|
|
2006
|
+
throw e;
|
|
1891
2007
|
}
|
|
1892
2008
|
return response;
|
|
1893
2009
|
}
|
|
@@ -1899,15 +2015,16 @@ class FAMISDb {
|
|
|
1899
2015
|
* @returns {Promise(<CommentFile>} array of comment file objects
|
|
1900
2016
|
* @memberof FAMISDb
|
|
1901
2017
|
*/
|
|
1902
|
-
|
|
2018
|
+
async getCommentFile(fileUid) {
|
|
1903
2019
|
let response;
|
|
1904
2020
|
try {
|
|
1905
|
-
const records = await this.db(
|
|
1906
|
-
|
|
2021
|
+
const records = await this.db("invoice_comment_files").where({
|
|
2022
|
+
uid: fileUid,
|
|
2023
|
+
});
|
|
1907
2024
|
if (records.length) response = records[0];
|
|
1908
2025
|
} catch (e) {
|
|
1909
2026
|
console.log(e);
|
|
1910
|
-
throw
|
|
2027
|
+
throw e;
|
|
1911
2028
|
}
|
|
1912
2029
|
return response;
|
|
1913
2030
|
}
|