diff --git a/app/controllers/promoter/promotion_codes_controller.rb b/app/controllers/promoter/promotion_codes_controller.rb index c168923..0682431 100644 --- a/app/controllers/promoter/promotion_codes_controller.rb +++ b/app/controllers/promoter/promotion_codes_controller.rb @@ -9,7 +9,7 @@ class Promoter::PromotionCodesController < ApplicationController @promotion_codes = @event.promotion_codes.includes(:user) end - + # GET /promoter/events/:event_id/promotion_codes/new # Show form to create a new promotion code def new diff --git a/app/models/order.rb b/app/models/order.rb index aea6182..8fea6d7 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -96,7 +96,7 @@ class Order < ApplicationRecord discount_total = promotion_codes.sum(:discount_amount_cents) # Ensure total doesn't go below zero - final_total = [ticket_total - discount_total, 0].max + final_total = [ ticket_total - discount_total, 0 ].max update!(total_amount_cents: final_total) end @@ -110,9 +110,9 @@ class Order < ApplicationRecord subtotal_amount_cents / 100.0 end - # Total discount amount from all promotion codes + # Total discount amount from all promotion codes (capped at subtotal) def discount_amount_cents - promotion_codes.sum(:discount_amount_cents) + [ promotion_codes.sum(:discount_amount_cents), subtotal_amount_cents ].min end # Discount amount in euros diff --git a/app/services/stripe_invoice_service.rb b/app/services/stripe_invoice_service.rb index 803993a..a2f7bd8 100644 --- a/app/services/stripe_invoice_service.rb +++ b/app/services/stripe_invoice_service.rb @@ -166,6 +166,23 @@ class StripeInvoiceService }) end + # Add promotion code discounts as negative line items + @order.promotion_codes.each do |promo_code| + Stripe::InvoiceItem.create({ + customer: customer.id, + invoice: invoice.id, + amount: -promo_code.discount_amount_cents, # Negative amount for discount + currency: "eur", + description: "Réduction promotionnelle (Code: #{promo_code.code})", + metadata: { + promotion_code_id: promo_code.id, + promotion_code: promo_code.code, + discount_amount_cents: promo_code.discount_amount_cents, + type: "promotion_discount" + } + }) + end + # No service fee on customer invoice; platform fee deducted from promoter payout end diff --git a/app/views/orders/invoice.html.erb b/app/views/orders/invoice.html.erb index dfb3f44..e79ddf1 100644 --- a/app/views/orders/invoice.html.erb +++ b/app/views/orders/invoice.html.erb @@ -121,13 +121,56 @@ <% end %>
+