-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreporting.py
More file actions
63 lines (53 loc) · 2.44 KB
/
reporting.py
File metadata and controls
63 lines (53 loc) · 2.44 KB
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
# Generates CSV reports for library analytics
import csv
from datetime import datetime
from config import BOOKS_FILE, BORROWERS_FILE, TRANSACTIONS_FILE
def generate_book_report():
"""Generates report of all books with availability status."""
try:
with open(BOOKS_FILE, mode="r") as file:
books = list(csv.DictReader(file))
report_data = []
for book in books:
report_data.append({
"Book ID": book["BookID"],
"Title": book["Title"],
"Available Copies": book["Quantity"],
"Status": "Available" if int(book["Quantity"]) > 0 else "Out of Stock"
})
timestamp = datetime.now().strftime("%Y%m%d_%H%M")
filename = f"Reports/books_report_{timestamp}.csv"
with open(filename, mode="w", newline="") as file:
writer = csv.DictWriter(file, fieldnames=["Book ID", "Title", "Available Copies", "Status"])
writer.writeheader()
writer.writerows(report_data)
return filename
except Exception as e:
print(f"Report generation failed: {e}")
return None
def generate_overdue_report():
"""Identifies overdue books with calculated fines."""
try:
with open(TRANSACTIONS_FILE, mode="r") as file:
transactions = list(csv.DictReader(file))
overdue_books = []
for txn in transactions:
if txn["DueDate"] and datetime.strptime(txn["DueDate"], "%Y-%m-%d") < datetime.now():
overdue_days = (datetime.now() - datetime.strptime(txn["DueDate"], "%Y-%m-%d")).days
overdue_books.append({
"Transaction ID": txn["TransactionID"],
"Book ID": txn["BookID"],
"Member ID": txn["MemberID"],
"Days Overdue": overdue_days,
"Fine": 50 * overdue_days
})
timestamp = datetime.now().strftime("%Y%m%d_%H%M")
filename = f"Reports/overdue_report_{timestamp}.csv"
with open(filename, mode="w", newline="") as file:
writer = csv.DictWriter(file, fieldnames=["Transaction ID", "Book ID", "Member ID", "Days Overdue", "Fine"])
writer.writeheader()
writer.writerows(overdue_books)
return filename
except Exception as e:
print(f"Overdue report failed: {e}")
return None