generated from uchicago-library/folio-template-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpol_fund.py
506 lines (422 loc) · 15.3 KB
/
pol_fund.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
"""Update the funds in purchase order lines.
Use this to update funds on POLs, especially when moving encumbrances to different
funds as part of post-fiscal year rollover activity.
Workflow:
1. Look up POL by line number,
2. look up unreleased encumbrances,
3. release the encumbrances, and
4. recreate encumbrances of the same amounts on the new fund
Note: all entries in the fund distribution will be set to the same new fund. This
is a limitation of this automated process as when we give a specific new fund per
POL.
"""
##
# TODO:
#
# Clean up folioclient import lines
#
# Update FolioClient version and check for relevant get_* methods for financial data
#
# Remove dead code
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=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
# formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
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 CSV dialect (default: excel)",
default="excel",
)
parser.add_argument(
"-O",
"--out_dialect",
help="output CSV 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) -> dict:
"""
Return current fiscal year as a dictionary.
This implementation uses the Python date objects, which are naive about timezones and work at the full day level.
FY start and end are formatted as full ISO dates, but seem to apply to the whole day.
Looking at real data from Lotus, observe that the end of FY2022 is a full 24 hour before the start of FY2023:
FY2022:
"periodStart": "2021-07-01T05:00:00.000+00:00",
"periodEnd": "2022-06-25T00:00:00.000+00:00",
FY2023:
"periodStart": "2022-06-26T00:00:00.000+00:00",
"periodEnd": "2023-06-30T00:00:00.000+00:00",
Args:
client: intialized FolioClient object
Returns:
A dictionary containing the current fiscal year, None if there is no fiscal year covering the current date.
"""
today = date.today()
fyList = client.folio_get("/finance/fiscal-years")["fiscalYears"]
for fy in fyList:
start = datetime.fromisoformat(fy["periodStart"]).date()
end = datetime.fromisoformat(fy["periodEnd"]).date()
if start <= today and today <= end:
return fy
return None
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:
"""Look up all encumbrances for a POL in a fiscal year.
Args:
client: intialized FolioClient object
pol_id: POL UUID
fy_id: fiscal year UUID
Returns:
A list object containing the transaction data.
"""
enc_result = client.folio_get(
"/finance-storage/transactions",
query=f"?query=(encumbrance.sourcePoLineId={pol_id} and fiscalYearId={fy_id})",
)
return enc_result["transactions"]
def set_pol_fund(
client: FolioClient,
pol: dict,
fund_code: str,
funds: dict,
fiscal_year: 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
fund_code: new fund_code code to assign
funds: dictionary of funds indexed by code
fiscal_year: current fiscal year as dictionary
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']}"
# TODO: release old encumbrances
fundDistList = pol["fundDistribution"]
fundDistListOrig = copy.deepcopy(fundDistList)
# Identify the current Fiscal Year
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")
# Encumber on the new fund
for fdist in fundDistList:
fdist["code"] = fund_code
fdist["fundId"] = funds[fund_code]["id"]
# setting new encumbrance ID causes an a new encumbrance to be created on the fund
fdist["encumbrance"] = str(uuid.uuid4())
if verbose:
err_fp.write("updated POL fund dist:\n")
json.dump(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(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...
updated_pol = client.folio_get(pol_path)
if verbose:
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 write_result(out, output):
"""Write output (placeholder function)."""
out.write(output)
def main_loop(client, 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
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
"""
funds = get_funds(client)
fiscal_year = get_fiscal_year(client)
out_csv.writeheader()
for row in in_csv:
pol_no = row[0]
fund = row[1]
pol_id = None
status_code = None
msg = None
# check whether the new fund code actually exists, report error and move on if it does not.
if funds.get(fund) is None:
out_csv.writerow(
{
"timestamp": datetime.now(timezone.utc),
"pol_no": pol_no,
"fund": fund,
"message": "fund code does not exist",
"manual_review": "Y",
}
)
continue
# result = process_pol(client, pol_no, fund)
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,
"fund": fund,
"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
# Check if there is more than one fund distribution, report for manual review if so
if len(pol["fundDistribution"]) > 1:
out_csv.writerow(
{
"timestamp": datetime.now(timezone.utc),
"pol_no": pol_no,
"message": f"POL has {len(pol['fundDistribution'])} fund distributions",
"manual_review": "Y",
}
)
continue
#
# Get encumbrances on this POL from this Fiscal Year and release
#
# enc_list = get_encumbrances(client, pol['id'], fiscal_year['id'])
enc_list = client.folio_get(
"/finance-storage/transactions",
key="transactions",
query=f"?query=(encumbrance.sourcePoLineId={pol['id']} and fiscalYearId={fiscal_year['id']} and encumbrance.status=Unreleased)",
)
if len(enc_list) != 1:
out_csv.writerow(
{
"timestamp": datetime.now(timezone.utc),
"pol_no": pol_no,
"message": f"POL has {len(enc_list)} unreleased encumbrances",
"manual_review": "Y",
}
)
continue
for enc in enc_list:
resp = requests.post(
client.okapi_url + f"/finance/release-encumbrance/{enc['id']}",
json={"id": enc["id"]},
headers=client.okapi_headers,
)
if resp.status_code != 204:
out_csv.writerow(
{
"timestamp": datetime.now(timezone.utc),
"pol_no": pol_no,
"status_code": resp.status_code,
"message": "failed to release encumbrance: "
+ json.dumps(resp.text),
"manual_review": "Y",
}
)
continue
# This code is from when we thought we would have to update fund distributions individually.
# Now it looks like the /orders/order-lines API takes care of this in the business logic.
# Remove this code when we confirm.
if False:
fundDist = pol["fundDistribution"]
(status_code, msg) = reset_fund_dist(client, fundDist, fund)
if status_code == "204":
(status_code, msg) = set_pol_fund(client, pol, fund)
pass
(status_code, msg, fundDistOrig) = set_pol_fund(
client, pol, fund, funds, fiscal_year, verbose, err_fp
)
out_csv.writerow(
{
"timestamp": datetime.now(timezone.utc),
"pol_no": pol_no,
"fund": fund,
"pol_id": pol["id"],
"status_code": status_code,
"message": msg,
"original_fund_distribution": fundDistOrig,
"manual_review": "N",
}
)
def main():
"""Read command line arguments and config file and call main loop."""
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_fund_distribution",
"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
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
print("Interrupted")
sys.exit(0)