z230
This commit is contained in:
@@ -5,14 +5,25 @@ from openpyxl import load_workbook
|
||||
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
|
||||
from openpyxl.utils import get_column_letter
|
||||
|
||||
# STUDY = "42847922MDD3003"
|
||||
STUDY = "77242113UCO3001"
|
||||
STUDY = "42847922MDD3003"
|
||||
#STUDY = "77242113UCO3001"
|
||||
|
||||
INVENTORY_DIR = Path(f"xls_reports_{STUDY}")
|
||||
DESTRUCTION_DIR = Path(f"xls_ip_destruction_{STUDY}")
|
||||
SHIPMENTS_FILE = Path(f"xls_shipments_{STUDY}/shipments_report_{STUDY}.xlsx")
|
||||
DETAILS_DIR = Path(f"xls_shipment_details_{STUDY}")
|
||||
OUTPUT_DIR = Path("output")
|
||||
OUTPUT_FILE = OUTPUT_DIR / f"{date.today().strftime('%Y-%m-%d')} {STUDY} CZ IWRS overview.xlsx"
|
||||
|
||||
SHIPMENT_DROP_COLS = {
|
||||
"Location", "Shipped Date", "Delivered Date [UTC]",
|
||||
"Delivery Recipient", "Delivery Details", "Cancelled Date",
|
||||
"Tracking #", "Total Medication IDs",
|
||||
"Shipping Category", "Study", "Destination Location", "Destination Site",
|
||||
"Medication type", "Container ID", "Quantity of Medication IDs",
|
||||
"Packaged Lot description",
|
||||
}
|
||||
|
||||
# ── Shared constants ──────────────────────────────────────────────────────────
|
||||
|
||||
COLUMN_RENAMES = {
|
||||
@@ -144,6 +155,85 @@ def format_sheet(ws, header_color, highlight_col=None, highlight_color=None):
|
||||
ws.freeze_panes = "A2"
|
||||
|
||||
|
||||
# ── Shipment helpers ─────────────────────────────────────────────────────────
|
||||
|
||||
def build_shipments():
|
||||
sh = pd.read_excel(SHIPMENTS_FILE, sheet_name=0, header=5)
|
||||
sh.columns = sh.columns.str.strip()
|
||||
sh = sh.dropna(how="all")
|
||||
sh["Shipment ID"] = sh["Shipment ID"].astype(str).str.strip()
|
||||
sh = sh.drop(columns=[c for c in SHIPMENT_DROP_COLS if c in sh.columns])
|
||||
shipment_cols = list(sh.columns)
|
||||
|
||||
all_rows = []
|
||||
for _, s_row in sh.iterrows():
|
||||
sid = s_row["Shipment ID"]
|
||||
path = DETAILS_DIR / f"shipment_details_{sid}.xlsx"
|
||||
if not path.exists():
|
||||
continue
|
||||
det = pd.read_excel(path, sheet_name=0, header=5)
|
||||
det.columns = det.columns.str.strip()
|
||||
det = det.dropna(how="all")
|
||||
det["Shipment"] = det["Shipment"].astype(str).str.strip()
|
||||
extra_cols = [c for c in det.columns if c not in shipment_cols and c != "Shipment" and c not in SHIPMENT_DROP_COLS]
|
||||
for _, d_row in det.iterrows():
|
||||
all_rows.append({**s_row.to_dict(), **{c: d_row[c] for c in extra_cols}})
|
||||
|
||||
result = pd.DataFrame(all_rows)
|
||||
all_cols = shipment_cols + [c for c in extra_cols if c in result.columns]
|
||||
result = result[all_cols]
|
||||
|
||||
for col in ["Request Date", "Received Date", "Expiration Date"]:
|
||||
if col in result.columns:
|
||||
result[col] = pd.to_datetime(result[col], errors="coerce")
|
||||
|
||||
print(f" Shipments: {result['Shipment ID'].nunique()} shipments, {len(result)} kitu")
|
||||
return result
|
||||
|
||||
|
||||
def build_site_summary(result):
|
||||
STATUS_COLS = ["Available", "Assigned", "Dispensed", "Returned by Subject"]
|
||||
pivot = result.groupby("Ship To:")["Status"].value_counts().unstack(fill_value=0)
|
||||
for s in STATUS_COLS:
|
||||
if s not in pivot.columns:
|
||||
pivot[s] = 0
|
||||
pivot = pivot[STATUS_COLS].reset_index().rename(columns={
|
||||
"Ship To:": "Site", "Returned by Subject": "Returned"
|
||||
})
|
||||
pivot = pivot.sort_values("Site").reset_index(drop=True)
|
||||
pivot["Total"] = pivot[["Available", "Assigned", "Dispensed", "Returned"]].sum(axis=1)
|
||||
print(f" Site Summary: {len(pivot)} center")
|
||||
return pivot
|
||||
|
||||
|
||||
def format_shipment_sheet(ws, header_color_ship, header_color_detail, n_ship_cols):
|
||||
thin = Side(style="thin", color="000000")
|
||||
border = Border(left=thin, right=thin, top=thin, bottom=thin)
|
||||
hfont = Font(bold=True, color="FFFFFF", name="Arial", size=10)
|
||||
dfont = Font(name="Arial", size=10)
|
||||
fill_ship = PatternFill("solid", start_color=header_color_ship)
|
||||
fill_detail = PatternFill("solid", start_color=header_color_detail)
|
||||
|
||||
for cell in ws[1]:
|
||||
cell.fill = fill_ship if cell.column <= n_ship_cols else fill_detail
|
||||
cell.font = hfont
|
||||
cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
||||
cell.border = border
|
||||
ws.column_dimensions[get_column_letter(cell.column)].width = min(len(str(cell.value or "")) + 4, 35)
|
||||
ws.row_dimensions[1].height = 30
|
||||
|
||||
for row in ws.iter_rows(min_row=2, max_row=ws.max_row):
|
||||
for cell in row:
|
||||
cell.font = dfont
|
||||
cell.border = border
|
||||
cell.alignment = Alignment(horizontal="center", vertical="center")
|
||||
if cell.value.__class__.__name__ in ("datetime", "date", "Timestamp"):
|
||||
cell.number_format = "DD-MMM-YYYY"
|
||||
|
||||
ws.auto_filter.ref = ws.dimensions
|
||||
ws.freeze_panes = "A2"
|
||||
|
||||
|
||||
# ── Build DataFrames ──────────────────────────────────────────────────────────
|
||||
|
||||
def build_main(lookup):
|
||||
@@ -234,13 +324,19 @@ def main():
|
||||
not_returned_df = build_not_returned(df)
|
||||
destruction_df = build_kits_for_destruction(df)
|
||||
|
||||
shipments_df = build_shipments()
|
||||
site_summary_df = build_site_summary(shipments_df)
|
||||
n_ship_cols = shipments_df.columns.tolist().index("Investigator") # first detail col index (0-based)
|
||||
|
||||
# Write all sheets
|
||||
with pd.ExcelWriter(OUTPUT_FILE, engine="openpyxl") as writer:
|
||||
df.to_excel( writer, index=False, sheet_name="CountryMedicationOverview")
|
||||
expired_df.to_excel( writer, index=False, sheet_name=expired_sheet)
|
||||
assigned_df.to_excel( writer, index=False, sheet_name="Assigned not dispensed")
|
||||
not_returned_df.to_excel(writer, index=False, sheet_name="Not returned")
|
||||
destruction_df.to_excel( writer, index=False, sheet_name="Kits for destruction")
|
||||
df.to_excel( writer, index=False, sheet_name="CountryMedicationOverview")
|
||||
expired_df.to_excel( writer, index=False, sheet_name=expired_sheet)
|
||||
assigned_df.to_excel( writer, index=False, sheet_name="Assigned not dispensed")
|
||||
not_returned_df.to_excel( writer, index=False, sheet_name="Not returned")
|
||||
destruction_df.to_excel( writer, index=False, sheet_name="Kits for destruction")
|
||||
shipments_df.to_excel( writer, index=False, sheet_name="Shipments")
|
||||
site_summary_df.to_excel( writer, index=False, sheet_name="Site Summary")
|
||||
|
||||
# Format all sheets
|
||||
wb = load_workbook(OUTPUT_FILE)
|
||||
@@ -261,9 +357,12 @@ def main():
|
||||
format_sheet(wb["Assigned not dispensed"], header_color="833C00", highlight_col="Subject ID", highlight_color="FFF2CC")
|
||||
format_sheet(wb["Not returned"], header_color="375623", highlight_col="Max Visit Date", highlight_color="E2EFDA")
|
||||
format_sheet(wb["Kits for destruction"], header_color="595959")
|
||||
format_shipment_sheet(wb["Shipments"], "1F4E79", "375623", n_ship_cols)
|
||||
format_sheet(wb["Site Summary"], header_color="1F4E79")
|
||||
|
||||
wb.save(OUTPUT_FILE)
|
||||
print(f"\nSaved: {OUTPUT_FILE} ({len(df)} rows on main sheet, {wb.sheetnames})")
|
||||
|
||||
|
||||
main()
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user