Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement voucher per event and for all events of an organizer: apply voucher in billing setting #488

Open
wants to merge 10 commits into
base: development
Choose a base branch
from

Conversation

HungNgien
Copy link
Contributor

@HungNgien HungNgien commented Dec 27, 2024

image
Add a field to input invoice voucher for organizers

image

This PR resolves #382

Summary by Sourcery

Add support for invoice vouchers in billing settings. Apply voucher discounts to organizer or event-specific invoices.

New Features:

  • Introduce invoice vouchers to provide discounts on ticket fees.

Tests:

  • Update tests to cover new voucher functionality.

Copy link

sourcery-ai bot commented Dec 27, 2024

Reviewer's Guide by Sourcery

This pull request implements the ability for organizers to apply voucher codes to their monthly invoices. It introduces a new "Invoice Voucher" field in the organizer billing settings. The backend logic now calculates the total amount due, applies any valid voucher, and updates the billing invoice accordingly. The voucher application logic considers voucher limitations, such as organizer restrictions and event-specific usage.

Sequence diagram for monthly billing collection with voucher application

sequenceDiagram
    participant System
    participant Event
    participant BillingInvoice
    participant InvoiceVoucher

    System->>Event: collect_billing_invoice()
    Event->>BillingInvoice: Check existing invoice
    alt No existing invoice
        Event->>Event: Calculate total amount
        Event->>InvoiceVoucher: Check voucher validity
        alt Valid voucher
            InvoiceVoucher-->>Event: Apply voucher discount
        end
        Event->>BillingInvoice: Create new invoice with discount
        BillingInvoice-->>System: Return billing response
        alt Voucher applied
            System->>InvoiceVoucher: Increment redeemed count
        end
    end
Loading

Entity relationship diagram for billing and voucher system

erDiagram
    ORGANIZER ||--o{ EVENT : has
    ORGANIZER ||--o| ORGANIZER_BILLING : has
    ORGANIZER_BILLING ||--o| INVOICE_VOUCHER : uses
    EVENT ||--o{ BILLING_INVOICE : generates
    BILLING_INVOICE }|--|| INVOICE_VOUCHER : applies

    BILLING_INVOICE {
        decimal amount
        decimal ticket_fee
        decimal final_ticket_fee
        decimal voucher_discount
        date monthly_bill
    }

    INVOICE_VOUCHER {
        string code
        decimal value
        string price_mode
    }
Loading

Class diagram for billing and voucher models

classDiagram
    class BillingInvoice {
        +Decimal amount
        +String currency
        +Decimal ticket_fee
        +Decimal final_ticket_fee
        +Decimal voucher_discount
        +Date monthly_bill
        +save()
    }

    class OrganizerBillingModel {
        +InvoiceVoucher invoice_voucher
        +String stripe_customer_id
        +save()
    }

    class InvoiceVoucher {
        +calculate_price()
        +is_active()
    }

    OrganizerBillingModel --> InvoiceVoucher
    BillingInvoice --> OrganizerBillingModel
Loading

File-Level Changes

Change Details Files
Added a new Invoice Voucher field to the organizer billing settings form.
  • Added a new field to the organizer billing settings form to input and validate invoice voucher codes.
  • Updated the organizer billing settings template to include the new Invoice Voucher field.
  • Added validation to ensure the entered voucher code is valid, active, and applicable to the organizer.
  • If a valid voucher is provided, it's saved with the organizer's billing information.
src/pretix/control/forms/organizer_forms/organizer_form.py
src/pretix/control/templates/pretixcontrol/organizers/billing.html
Implemented voucher application logic in billing calculation.
  • Added logic to collect_billing_invoice to apply invoice vouchers and calculate discounts.
  • Added calculate_price method to the InvoiceVoucher model to calculate the discounted price based on the voucher's settings.
  • Updated the generate_invoice_pdf function to display the voucher discount and final ticket fee on the invoice.
  • Added final_ticket_fee and voucher_discount fields to the BillingInvoice model to store the discounted fee and the discount amount.
  • Added invoice_voucher field to the OrganizerBillingModel to store the applied voucher.
  • Added pydantic dependency for schema definitions.
  • Created a database migration to add the new fields to the BillingInvoice and OrganizerBillingModel models.
  • Created a CollectBillingResponse schema to structure the response of the billing collection process.
src/pretix/eventyay_common/tasks.py
src/pretix/base/models/vouchers.py
src/pretix/eventyay_common/billing_invoice.py
src/pretix/base/models/billing.py
src/pretix/base/models/organizer.py
pyproject.toml
src/pretix/base/migrations/0007_billinginvoice_final_ticket_fee_and_more.py
src/pretix/eventyay_common/schemas/billing.py
Updated billing-related tasks and functions to use the final ticket fee after voucher application.
  • Modified monthly_billing_collect to handle voucher application and update redeemed voucher count.
  • Updated process_auto_billing_charge to use the final_ticket_fee when charging organizers.
  • Updated retry_failed_payment and check_billing_status_for_warning to consider final_ticket_fee instead of the original ticket_fee.
  • Updated calculate_ticket_fee to return the original ticket fee, the final ticket fee after voucher application, and the voucher discount amount.
src/pretix/eventyay_common/tasks.py

Assessment against linked issues

Issue Objective Addressed Explanation
#382 Implement voucher functionality for a single event
#382 Implement voucher functionality for all events of an organizer

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@HungNgien HungNgien marked this pull request as ready for review December 30, 2024 02:21
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @HungNgien - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement voucher per event and for all events of an organizer
1 participant