from datetime import datetime from ..constants import XLSX_REPORTS_FEATURE from . import features from .fields import REPORT_FIELDS from .report_writer import CsvReportWriter, Report, ReportWriter, XlsxReportWriter def _get_report_writer_class(account_id): if features.is_feature_enabled_for_account(XLSX_REPORTS_FEATURE, account_id): return XlsxReportWriter return CsvReportWriter def _get_created_date(): """Create tuple representing creation date as required by ZipInfo.""" now = datetime.now() return ( now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond, ) def create_report( report_id: int, collaborator_id: int, total: str, currency: str, collaborator_name: str, collaborator_internal_id: str | None, filename: str, report_run_name: str, period_name: str, vendor_id: int, report_run_uuid: str, number_format: str, ): print(f"Creating report for collaborator {collaborator_id} with value {total} {currency or ''}") report_writer_class = _get_report_writer_class(vendor_id) writer = report_writer_class( report=Report( report_id=report_id, collaborator_id=collaborator_id, collaborator_name=collaborator_name, vendor_id=vendor_id, total=total, currency=currency, filename=filename, report_run_uuid=report_run_uuid, number_format=number_format, created_date=_get_created_date(), contract_totals={}, ) ) # Write summary section writer.write_row(("Report Run Name", report_run_name)) writer.write_row(("Report Date", writer.cast("report_date", datetime.now()))) writer.write_row(("Report Accounting Period", period_name)) writer.write_row(()) writer.write_row(("Label ID", vendor_id)) writer.write_row(("Collaborator Name", collaborator_name)) writer.write_row(("Collaborator Internal ID", collaborator_internal_id)) writer.write_row(("Earned This Report", writer.cast("total", total), currency)) writer.write_row(()) # Write column headers writer.write_row(REPORT_FIELDS.values()) return writer def write_row(writer: ReportWriter, row: dict): writer.write_row(writer.cast(key, row[key]) for key in REPORT_FIELDS)