Ejemplo n.º 1
0
 def __confirm_and_create_invoice(self, session: Session,
                                  apartment_application):
     has_invoice = bool(
         session.query(Invoice).filter(
             Invoice.type == InvoiceType.RENT).filter(
                 Invoice.apartment_application_id ==
                 apartment_application.id).count())
     if (not has_invoice and apartment_application.status
             == ApartmentApplicationStatus.AWARDED):
         redirect_url: str = config.MOLLIE_REDIRECT_URL
         webhook_url: str = config.MOLLIE_WEBHOOK_URL
         start_date = util.get_current_date()
         start_date_text = util.get_human_readable_date(start_date)
         next_date = util.get_date_x_days_from(
             start_date, config.RENT_PAYMENT_DURATION_DAYS)
         next_date_text = util.get_human_readable_date(next_date)
         description = (
             f"Invoice for payment from {start_date_text} to {next_date_text}"
         )
         amount = round(apartment_application.apartment.total_price, 2)
         invoice = Invoice(
             apartment_application_id=apartment_application.id,
             type=InvoiceType.RENT,
             status=InvoiceStatus.PENDING,
             amount=amount,
             description=description,
             next_date=next_date,
         )
         session.add(invoice)
         session.flush()
         mollie_amount = util.float_to_mollie_amount(invoice.amount)
         print("\n\n\n\n")
         print(f"Amount to charge is {mollie_amount}")
         print("\n\n\n\n")
         payment = mollie_client.payments.create({
             "amount": {
                 "currency": "EUR",
                 "value": mollie_amount
             },
             "description": invoice.description,
             "redirectUrl": redirect_url,
             "webhookUrl": webhook_url,
             "metadata": {
                 "type": invoice.type,
                 "invoice_id": str(invoice.id),
                 "created_date": str(start_date),
                 "next_date": str(next_date),
             },
         })
         invoice.payment_id = payment.id
Ejemplo n.º 2
0
def create_subscription_payment(session: Session, user: User) -> Invoice:
    role_amount_mapping = {
        UserRole.TENANT: config.USER_SUBSCRIPTION_AMOUNT,
        UserRole.LANDLORD: config.LANDLORD_SUBSCRIPTION_AMOUNT,
    }
    redirect_url: str = config.MOLLIE_REDIRECT_URL
    webhook_url: str = config.MOLLIE_WEBHOOK_URL
    start_date = util.get_current_date()
    start_date_text = util.get_human_readable_date(start_date)
    next_date = util.get_date_x_days_from(
        start_date, config.SUBSCRIPTION_PAYMENT_DURATION_DAYS)
    next_date_text = util.get_human_readable_date(next_date)
    # TODO generate in util
    description = f"Invoice for payment from {start_date_text} to {next_date_text}"
    amount = round(role_amount_mapping[user.role], 2)
    invoice = Invoice(
        user_id=user.id,
        type=InvoiceType.SUBSCRIPTION,
        status=InvoiceStatus.PENDING,
        amount=amount,
        description=description,
        next_date=next_date,
    )
    session.add(invoice)
    session.flush()
    mollie_amount = util.float_to_mollie_amount(invoice.amount)
    payment = mollie_client.payments.create({
        "amount": {
            "currency": "EUR",
            "value": mollie_amount
        },
        "description": description,
        "redirectUrl": redirect_url,
        "webhookUrl": webhook_url,
        "metadata": {
            "type": invoice.type,
            "invoice_id": str(invoice.id),
            "created_date": str(start_date),
            "next_date": str(next_date),
        },
    })
    invoice.payment_id = payment.id
    return invoice
Ejemplo n.º 3
0
def create_rent_payment(
        session: Session,
        apartment_application: ApartmentApplication) -> Invoice:
    redirect_url: str = config.MOLLIE_REDIRECT_URL
    webhook_url: str = config.MOLLIE_WEBHOOK_URL
    start_date = util.get_current_date()
    start_date_text = util.get_human_readable_date(start_date)
    next_date = util.get_date_x_days_from(start_date,
                                          config.RENT_PAYMENT_DURATION_DAYS)
    next_date_text = util.get_human_readable_date(next_date)
    # TODO generate in util
    description = f"Invoice for payment from {start_date_text} to {next_date_text}"
    amount = round(apartment_application.apartment.total_price, 2)
    invoice = Invoice(
        apartment_application_id=apartment_application.id,
        type=InvoiceType.RENT,
        status=InvoiceStatus.PENDING,
        amount=amount,
        description=description,
        next_date=next_date,
    )
    session.add(invoice)
    session.flush()
    mollie_amount = util.float_to_mollie_amount(invoice.amount)
    payment = mollie_client.payments.create({
        "amount": {
            "currency": "EUR",
            "value": mollie_amount
        },
        "description": description,
        "redirectUrl": redirect_url,
        "webhookUrl": webhook_url,
        "metadata": {
            "type": invoice.type,
            "invoice_id": str(invoice.id),
            "created_date": str(start_date),
            "next_date": str(next_date),
        },
    })
    invoice.payment_id = payment.id
    return invoice
Ejemplo n.º 4
0
def generate_subscription_invoices(*args):
    session: Session = SessionLocal()
    try:
        print("\n\n\n\n\nStarting subscription invoice worker")
        # ? should be active users TODO
        non_admin_users: List[User] = (session.query(User).filter(
            User.role != UserRole.ADMIN).all())
        for user in non_admin_users:
            latest_invoice: Invoice = (session.query(Invoice).filter(
                Invoice.type == InvoiceType.SUBSCRIPTION).filter(
                    Invoice.user_id == user.id).order_by(
                        Invoice.created_at.desc()).first())
            current_date = get_current_date()
            if latest_invoice.next_date <= current_date:
                # current date is greater than or equal to last invoice's next date
                create_subscription_invoice.delay(user.id)
        print("\n\n\n\nEnd subscription invoice worker")
    except Exception:
        session.rollback()
    finally:
        session.close()
Ejemplo n.º 5
0
def generate_rent_invoices(*args):
    session: Session = SessionLocal()
    try:
        print("\n\n\n\n\nStarting rent invoice worker")
        all_completed_apartment_applications: List[
            models.ApartmentApplication] = (session.query(
                models.ApartmentApplication).filter(
                    models.ApartmentApplication.status ==
                    ApartmentApplicationStatus.COMPLETED).all())
        for apartment_application in all_completed_apartment_applications:
            latest_invoice: Invoice = (session.query(Invoice).filter(
                Invoice.type == InvoiceType.RENT).filter(
                    Invoice.apartment_application_id ==
                    apartment_application.id).order_by(
                        Invoice.created_at.desc()).first())
            current_date = get_current_date()
            if latest_invoice.next_date <= current_date:
                # current date is greater than or equal to last invoice's next date
                create_rent_invoice.delay(apartment_application.id)
        print("\n\n\n\nEnd rent invoice worker")
    except Exception:
        session.rollback()
    finally:
        session.close()