Exemple #1
0
def create_invoice_object(family, class_map, note):
    """Create an invoice object from a family object.
    Formats names and emails, collates classes by teacher, and sums
    class fees by teacher."""
    invoice = {}
    parents = get_parents(family)
    students = get_students(family)
    invoice['parent'] = []
    for parent in parents:
        invoice['parent'].append([
            parent[Column.LAST_NAME] + ', ' + parent[Column.FIRST_NAME],
            parent[Column.EMAIL]
        ])
    payable = {}
    invoice['students'] = {}
    invoice['total'] = Decimal(0.00)
    invoice['note'] = note
    for student in students:
        name = student[Column.LAST_NAME] + ', ' + student[Column.FIRST_NAME]
        invoice['students'][name] = []
        for class_name in student[Column.CLASSES]:
            teacher, fee = class_map[class_name]
            # Ensure fee has two digits after the decimal:
            fee += Decimal('0.00')
            invoice['students'][name].append([class_name, teacher, fee])
            invoice['total'] += fee
            try:
                payable[teacher] += fee
            except KeyError:
                payable[teacher] = fee
    invoice['payable'] = payable
    return invoice
Exemple #2
0
def generate_master(families, class_map, term, output_file):
    rml = io.StringIO()
    start_rml(rml,
              template=RML_BEGIN_TEMPLATE_PORTRAIT,
              title='Class Enrollment Master List',
              term=term,
              footer='Page <pageNumber/>')
    for family in families.values():
        if get_students(family):
            generate_master_rml_for_family(family, class_map, rml)
    finish_rml(rml)
    # logger.debug('rml: %s', rml.getvalue())
    rml.seek(0)
    rml2pdf.go(rml, outputFileName=output_file)
def create_drafts(subject,
                  body,
                  cc,
                  families,
                  class_map,
                  note,
                  term,
                  progress,
                  errors=None):
    if errors is None:
        errors = []
    msg_prefix = progress.GetMessage()
    gmail_service = get_gmail_service()
    profile = gmail_service.users().getProfile(userId='me').execute()
    sender = profile['emailAddress']
    drafts = []
    for n, family in enumerate(families.values()):
        if progress.WasCancelled():
            break
        last_name = family['last_name']
        msg = f"{msg_prefix}{last_name}"
        progress.Update(n, newmsg=msg)
        if get_students(family):
            logger.info(f'processing family {n}: {last_name}')
            pdf_buffer = MyBytesIO()
            generate_one_invoice(family, class_map, note, term, pdf_buffer)
            parents = family['parents']
            recipients = [
                f'"{p[Column.FIRST_NAME]} {p[Column.LAST_NAME]}" <{p[Column.EMAIL]}>'
                for p in parents if p[Column.EMAIL]
            ]
            if recipients:
                logger.info(f'  sending to email addresses: {recipients}')
                msg = create_message_with_attachment(
                    sender=sender,
                    recipients=recipients,
                    cc=cc,
                    subject=subject,
                    body=body,
                    data=pdf_buffer.getvalue())
                draft = create_draft(gmail_service, 'me', msg)
                drafts.append({'family': family, 'draft_id': draft['id']})
            else:
                errors.append(f'Family {last_name} has no parents with email')
    return drafts
Exemple #4
0
def generate_invoices(progress, families, class_map, note, term, output_file):
    rml = io.StringIO()
    start_rml(rml,
              template=RML_BEGIN_TEMPLATE_PORTRAIT,
              title='Class Enrollment Invoice',
              term=term)
    for n, family in enumerate(families.values()):
        if progress.WasCancelled():
            break
        msg = "Please wait...\n\n" \
              f"Generating invoice for family: {family['last_name']}"
        # logger.debug(f'updating progress {n}: {msg}')
        progress.Update(n, newmsg=msg)
        students = get_students(family)
        if students:
            logger.debug(
                f'processing {len(students)} students in family {n}: {family["last_name"]}'
            )
            invoice = create_invoice_object(family, class_map, note)
            generate_invoice_page_rml(invoice, rml)
    finish_rml(rml)
    # logger.debug('rml: %s', rml.getvalue())
    rml.seek(0)
    rml2pdf.go(rml, outputFileName=output_file)