generated from uchicago-library/folio-template-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpol_expenseclasses.py
591 lines (494 loc) · 17.9 KB
/
pol_expenseclasses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
"""Reset expense classes on purchase order lines.
Workflow:
1. retrieve the POL and save the old fund distribution in memory
2. delete the fund distribution from the POL and save the POL, this will delete
the old transactions
3. Put new expense class ids in the fund distribution that was saved in memory,
re-add to the POL and save. This should trigger new encumbrance transaction,
keep the same funds and encumbrances, etc.
"""
# feature branch
import argparse
import configparser
import copy
import csv
import json
import logging
import sys
import uuid
from datetime import date, datetime, timezone
import requests
from folioclient import FolioClient
from folioclient.FolioClient import FolioClient
def error_exit(status, msg):
"""Write out an error message and terminate with an exit status (convenience function)."""
sys.stderr.write(msg)
sys.exit(status)
def read_config(filename: str):
"""Parse the named config file and return an config object."""
config = configparser.ConfigParser()
try:
config.read_file(open(filename))
except FileNotFoundError as err:
msg = f"{type(err).__name__}: {err}\n"
error_exit(1, msg)
except configparser.MissingSectionHeaderError as err:
msg = f"{type(err).__name__}: {err}\n"
error_exit(2, msg)
return config
def init_client(config):
"""Return an initialized client object.
This small function is convenient when using the interactive interpreter.
Args:
config: ConfigParser object contianing config file data
"""
return FolioClient(
config["Okapi"]["okapi_url"],
config["Okapi"]["tenant_id"],
config["Okapi"]["username"],
config["Okapi"]["password"],
)
def parse_args():
"""Parse command line arguments and return a Namespace object."""
parser = argparse.ArgumentParser(
description="Reset expense classes on purchase order lines",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-D",
"--dump_expense_classes",
help="Read and print out expense classes.",
action="store_true",
)
parser.add_argument(
"-i",
"--infile",
help="Input file (default: stdin)",
default=sys.stdin,
type=argparse.FileType("r"),
)
parser.add_argument(
"-o",
"--outfile",
help="Output file (truncate if exists, default: stdout)",
default=sys.stdout,
type=argparse.FileType("w"),
)
parser.add_argument(
"-I",
"--in_dialect",
help="input dialect (default: excel)",
default="excel",
)
parser.add_argument(
"-O",
"--out_dialect",
help="output dialect (default: excel)",
default="excel",
)
parser.add_argument(
"-C", "--config_file", help="Name of config file", default="config.ini"
)
parser.add_argument(
"-v", "--verbose", action="count", default=0, help="Increase verbosity level"
)
parser.epilog = (
"Input file column 0 must contain the PO line no., column 1 must contain the fund code.\n"
+ "\n"
+ "Input and output files can be in any dialect the csv parser class understands:\n"
+ "\t"
+ ", ".join(csv.list_dialects())
+ "\n"
+ "See https://docs.python.org/3/library/csv.html for more details"
)
return parser.parse_args()
def get_fiscal_year(client: FolioClient, code: str) -> dict:
"""
Look up fiscal year by code.
Args:
client: intialized FolioClient object
code: code for the desired fiscal year
Returns:
A dictionary containing the current fiscal year, None if there is no fiscal year covering the current date.
"""
# TODO: modify query to only return the needed fiscal year (can't seem to get this right)
fyList = client.folio_get("/finance/fiscal-years")["fiscalYears"]
for fy in fyList:
if fy["code"] == code:
return fy
return None
def get_expense_classes(client: FolioClient) -> list:
"""
Return a list of all expense class objects.
Args:
client: intialized FolioClient object
"""
exp_classes = []
exp_classes.extend(client.get_all("/finance/expense-classes", "expenseClasses"))
return exp_classes
def dump_expense_classes(expense_classes: list, file=sys.stdout):
"""
Write out a human-readable summary of the expense classes.
Args:
expense_classes: dictionary of expense classes, indexed by "code"
file: text file to write to (default: stdout)
"""
for ec in expense_classes:
file.write(f'{ec["id"]}\t{ec["code"]}\t{ec["name"]}\n')
def get_funds(client: FolioClient) -> dict:
"""
Return a dictionary of all funds, indexed by fund code.
Args:
client: intialized FolioClient object
"""
funds = {}
for f in client.get_all("/finance/funds", "funds"):
funds[f["code"]] = f
return funds
def get_pol_by_line_no(client: FolioClient, pol_no: str) -> dict:
"""
Look up POL by line number.
Args:
client: intialized FolioClient object
pol_no: POL number
Returns:
A dictionary object containing the POL data.
Raises:
Exception if more than one POL matches the pol_no.
"""
path = "/orders/order-lines"
query = f'?query=poLineNumber=="{pol_no}"'
res = client.folio_get(path, None, query)
if res["totalRecords"] == 0:
return None
if res["totalRecords"] > 1:
raise Exception(
f'query for POL num {pol_no} resulted in {res["totalRecords"]} results, should be unique'
)
pol = res["poLines"][0]
return pol
def get_encumbrances(client: FolioClient, pol_id: str, fy_id: str) -> list:
enc_result = client.folio_get(
"/finance-storage/transactions",
query=f"?query=(encumbrance.sourcePoLineId={pol_id} and fiscalYearId={fy_id})",
)
return enc_result["transactions"]
def reencumber_pol(
client: FolioClient,
pol: dict,
verbose: bool,
err_fp,
) -> tuple[str, str, str]:
"""
Set the fund for the POL, release encumbrance on old fund and re-encumber on new fund.
If there is more than one fund distribution, this will update all fund distributions to the new fund
Args:
client: intialized FolioClient object
pol: purchase order line as dictionary
verbose: enable more diagnostic messages to the error output
err_fp: file pointer for error messages
Returns:
Tuple of HTTP status code, plus message and original fund distribution list if error.
"""
pol_path = f"/orders/order-lines/{pol['id']}"
pol_url = f"{client.okapi_url}/orders/order-lines/{pol['id']}"
my_pol = copy.deepcopy(pol)
fundDistList = pol["fundDistribution"]
fundDistListOrig = copy.deepcopy(fundDistList)
if verbose:
err_fp.write("original POL fund dist:\n")
json.dump(pol["fundDistribution"], err_fp, indent=2)
err_fp.write("\nEND original POL fund dist:\n")
# delete fundDistribution
my_pol.pop("fundDistribution")
resp = requests.put(pol_url, headers=client.okapi_headers, data=json.dumps(my_pol))
if verbose:
err_fp.write(pol_url + "\n")
err_fp.write(
f"Delete fundDistribution:\nstatus = {resp.status_code};\ntext = {resp.text}\n"
)
err_fp.write(pol_url + "\n")
if resp.status_code != 204:
return (
resp.status_code,
"failed to remove fund distribution: \n" + resp.text,
json.dumps(fundDistListOrig),
)
# Reencumber
for fdist in fundDistList:
# setting new encumbrance ID causes an a new encumbrance to be created on the fund
fdist["encumbrance"] = str(uuid.uuid4())
my_pol["fundDistribution"] = fundDistList
if verbose:
err_fp.write("updated POL fund dist:\n")
json.dump(my_pol["fundDistribution"], err_fp, indent=2)
err_fp.write("\nEND updated POL fund dist:\n")
resp = requests.put(pol_url, headers=client.okapi_headers, data=json.dumps(my_pol))
if verbose:
err_fp.write(pol_url + "\n")
err_fp.write(f"status = {resp.status_code};\ntext = {resp.text}\n")
err_fp.write(pol_url + "\n")
# Check updated POL...
if verbose:
updated_pol = client.folio_get(pol_path)
err_fp.write("updated POL fund dist:\n")
json.dump(updated_pol["fundDistribution"], err_fp, indent=2)
err_fp.write("\nEND updated POL fund dist:\n")
# ... and return the update results if the check is good
return (resp.status_code, resp.text, json.dumps(fundDistListOrig))
def reset_fund_dist(
client: FolioClient, fundDist, fund_code: str, funds: dict
) -> tuple[str, str]:
"""
Update all fund_code distributions.
For each fund_code distribution, first release the encumbrance.
"""
status_code = None
msg = None
for fdist in fundDist:
# release current encumbrance
release_url = f"/finance/release-encumbrance/{fdist['encumbrance']}"
r = requests.post(release_url, headers=client.okapi_headers)
status_code = r.status_code
msg = r.text
if status_code != "204":
return (status_code, json.dumps(msg))
new_fdist = fdist.copy()
new_fdist["code"] = fund_code
new_fdist["fundID"] = funds[code]["id"]
pop(new_fdist, "encumbrance", None)
# new_fdist["reEncumber"] = "true"
# re-encumber to new fund_code code
pass
return (status_code, msg)
def update_expense_class(
client: FolioClient,
pol: dict,
exp_class_id: str,
verbose: bool,
err_fp,
) -> tuple[str, str, str]:
"""
Set the expense class for the POL, release encumbrance on old expense class and re-encumber on new expense class.
If there is more than one fund distribution, this will update all fund distributions to the new expense class.
Args:
client: intialized FolioClient object
pol: purchase order line as dictionary
exp_class_id: UUID of the new expense class
verbose: enable more diagnostic messages to the error output
err_fp: file pointer for error messages
Returns:
Tuple of HTTP status code, plus message and original fund distribution list if error.
"""
pol_path = f"/orders/order-lines/{pol['id']}"
pol_url = f"{client.okapi_url}/orders/order-lines/{pol['id']}"
if verbose:
err_fp.write(f"Entered update_expense_class, Expense class id: {exp_class_id}")
my_pol = copy.deepcopy(pol)
# set up new fund distribution with updated expense classes
fund_dist_list = pol["fundDistribution"]
my_fund_dist_list = copy.deepcopy(fund_dist_list)
for fdist in my_fund_dist_list:
# setting new encumbrance ID causes an a new encumbrance to be created on the fund
fdist["encumbrance"] = str(uuid.uuid4())
fdist["expenseClassId"] = exp_class_id
if verbose:
err_fp.write("original POL fund dist:\n")
json.dump(pol["fundDistribution"], err_fp, indent=2)
err_fp.write("\nEND original POL fund dist:\n")
# delete fundDistribution
my_pol.pop("fundDistribution")
resp = requests.put(pol_url, headers=client.okapi_headers, data=json.dumps(my_pol))
if verbose:
err_fp.write(pol_url + "\n")
err_fp.write(
f"Delete fundDistribution:\nstatus = {resp.status_code};\ntext = {resp.text}\n"
)
err_fp.write(pol_url + "\n")
if resp.status_code != 204:
return (
resp.status_code,
"failed to remove fund distribution: \n" + resp.text,
json.dumps(fund_dist_list),
)
# Reencumber
my_pol["fundDistribution"] = my_fund_dist_list
if verbose:
err_fp.write("updated POL fund dist:\n")
json.dump(my_pol["fundDistribution"], err_fp, indent=2)
err_fp.write("\nEND updated POL fund dist:\n")
err_fp.write("Single line dump of POL:\n" + json.dumps(my_pol) + "\n")
resp = requests.put(pol_url, headers=client.okapi_headers, data=json.dumps(my_pol))
if verbose:
err_fp.write(pol_url + "\n")
err_fp.write(f"status = {resp.status_code};\ntext = {resp.text}\n")
err_fp.write(pol_url + "\n")
# Check updated POL...
if verbose:
updated_pol = client.folio_get(pol_path)
err_fp.write("updated POL fund dist:\n")
json.dump(updated_pol["fundDistribution"], err_fp, indent=2)
err_fp.write("\nEND updated POL fund dist:\n")
# ... and return the update results if the check is good
return (resp.status_code, resp.text, json.dumps(fund_dist_list))
def write_result(out, output):
"""Write output (placeholder)."""
out.write(output)
def main_loop(client, exp_classes: list, in_csv, out_csv, verbose: bool, err_fp):
"""
Update the fund code for each POL in input.
Iterates over the input file, assumes the POL number is in the first column
and new fund code is in the second column.
Writes an output row for each POL.
Args:
client: initialized FolioClient object
exp_classes: expense classes
in_csv: CSV reader object
out_csv: CSV writer object
verbose: enable more diagnostic messages to the error output
err_fp: file pointer for error messages
"""
#
# Set up needed look-up dictionaries
#
ec_by_id = {}
for ec in exp_classes:
ec_by_id[ec["id"]] = ec
ec_by_code = {}
for ec in exp_classes:
ec_by_code[ec["code"]] = ec
ec_by_name = {}
for ec in exp_classes:
ec_by_name[ec["name"]] = ec
out_csv.writeheader()
for row in in_csv:
pol_no = row[0]
exp_class_code = row[1]
pol_id = None
status_code = None
msg = None
pol = get_pol_by_line_no(client, pol_no)
if pol is None:
out_csv.writerow(
{
"timestamp": datetime.now(timezone.utc),
"pol_no": pol_no,
"message": f"No POL found for line number '{pol_no}'",
"manual_review": "Y",
}
)
continue
if pol.get("fundDistribution") is None or len(pol["fundDistribution"]) == 0:
out_csv.writerow(
{
"timestamp": datetime.now(timezone.utc),
"pol_no": pol_no,
"message": "POL has 0 fund distributions",
"manual_review": "Y",
}
)
continue
problem_fdist = None
for fdist in pol["fundDistribution"]:
if "expenseClassId" not in fdist:
problem_fdist = fdist
if problem_fdist is not None:
out_csv.writerow(
{
"timestamp": datetime.now(timezone.utc),
"pol_no": pol_no,
"expense_code": exp_class_code,
"pol_id": pol["id"],
"message": f"fund distribution has no expenseClassID: {problem_fdist}",
"manual_review": "y",
}
)
continue
# save fund(s) for logging output
funds = []
for fdist in pol["fundDistribution"]:
funds.append(fdist["code"])
(status_code, msg, fundDistOrig) = update_expense_class(
client, pol, ec_by_code[exp_class_code]["id"], verbose, err_fp
)
orig_exp_code = []
orig_exp_name = []
for fdist in json.loads(fundDistOrig):
orig_exp_code.append(ec_by_id[fdist["expenseClassId"]]["code"])
orig_exp_name.append(ec_by_id[fdist["expenseClassId"]]["name"])
out_csv.writerow(
{
"timestamp": datetime.now(timezone.utc),
"pol_no": pol_no,
"expense_code": exp_class_code,
"pol_id": pol["id"],
"status_code": status_code,
"message": msg,
"original_expense_code": " ".join(orig_exp_code),
"original_expense_name": " ".join(orig_exp_name),
"manual_review": "N",
}
)
def main2():
verbose = False
args = parse_args()
config = read_config(args.config_file)
# Logic or function to override config values from the command line arguments would go here
client = init_client(config)
fieldnames = [
"timestamp",
"pol_no",
"fund",
"pol_id",
"status_code",
"message",
"original_expense_code",
"original_expense_name",
"manual_review",
]
main_loop(
client,
csv.reader(args.infile, dialect=args.in_dialect),
csv.DictWriter(args.outfile, fieldnames=fieldnames, dialect=args.out_dialect),
verbose,
sys.stderr,
)
return 0
def main():
verbose = False
args = parse_args()
if args.verbose > 0:
verbose = True
config = read_config(args.config_file)
# Logic or function to override config values from the command line arguments would go here
client = init_client(config)
fieldnames = [
"timestamp",
"pol_no",
"expense_code",
"pol_id",
"status_code",
"message",
"original_expense_code",
"original_expense_name",
"manual_review",
]
expense_classes = get_expense_classes(client)
if args.dump_expense_classes:
dump_expense_classes(expense_classes)
sys.exit(0)
main_loop(
client,
expense_classes,
csv.reader(args.infile, dialect=args.in_dialect),
csv.DictWriter(args.outfile, fieldnames=fieldnames, dialect=args.out_dialect),
verbose,
sys.stderr,
)
return 0
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
print("Interrupted")
sys.exit(0)