feat(promotion code): Promotion code system done

I added the features for users to use promotion code
and for promoters to create on their events.
May be rewrite to discount code?
This commit is contained in:
kbe
2025-09-29 15:25:52 +02:00
parent 72d54e02ab
commit 87ccebf229
19 changed files with 391 additions and 302 deletions

View File

@@ -54,8 +54,8 @@
</div>
<div>
<%= form.label :discount_amount_cents, "Montant de la réduction (en euros)", class: "block text-sm font-medium text-gray-700 mb-2" %>
<%= form.number_field :discount_amount_cents, class: "w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-colors", placeholder: "10", min: 0, step: "0.01", value: number_with_precision(@promotion_code.discount_amount_cents.to_f / 100, precision: 2) %>
<%= form.label :discount_amount_euros, "Montant de la réduction (en euros)", class: "block text-sm font-medium text-gray-700 mb-2" %>
<%= form.number_field :discount_amount_euros, class: "w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-colors", placeholder: "10", min: 0, step: "0.01", value: number_with_precision(@promotion_code.discount_amount_cents.to_f / 100, precision: 2) %>
<p class="text-sm text-gray-500 mt-2">Entrez le montant en euros (ex: 10, 5.50, 25)</p>
</div>

View File

@@ -51,7 +51,7 @@
<div class="flex items-start justify-between mb-4">
<div>
<h3 class="text-xl font-semibold text-gray-900 mb-2">
<%= link_to promotion_code.code, promoter_event_promotion_code_path(@event, promotion_code), class: "hover:text-purple-600 transition-colors" %>
<%= promotion_code.code %>
</h3>
<p class="text-gray-600 mb-3">Réduction de <%= number_to_currency(promotion_code.discount_amount_cents / 100.0, unit: "€") %></p>
</div>

View File

@@ -49,14 +49,14 @@
<div class="space-y-6">
<div>
<%= form.label :code, "Code de réduction", class: "block text-sm font-medium text-gray-700 mb-2" %>
<%= form.text_field :code, class: "w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-colors", placeholder: "Ex: SUMMER2024, BIENVENUE10, etc." %>
<p class="text-sm text-gray-500 mt-2">Ce code sera visible par les clients lors du paiement</p>
<%= form.text_field :code, class: "w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-colors", placeholder: "Ex: BIENVENUE10, VIP20" %>
<p class="text-sm text-gray-500 mt-2">Ce code sera à appliquer par le client lors du paiement.</p>
</div>
<div>
<%= form.label :discount_amount_cents, "Montant de la réduction (en euros)", class: "block text-sm font-medium text-gray-700 mb-2" %>
<%= form.number_field :discount_amount_cents, class: "w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-colors", placeholder: "10", min: 0, step: "0.01", value: number_with_precision(@promotion_code.discount_amount_cents.to_f / 100, precision: 2) %>
<p class="text-sm text-gray-500 mt-2">Entrez le montant en euros (ex: 10, 5.50, 25)</p>
<%= form.label :discount_amount_euros, "Montant de la réduction (en euros)", class: "block text-sm font-medium text-gray-700 mb-2" %>
<%= form.number_field :discount_amount_euros, class: "w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-colors", placeholder: "10", min: 0, step: "0.01", value: number_with_precision(@promotion_code.discount_amount_cents.to_f / 100, precision: 2) %>
<p class="text-sm text-gray-500 mt-2">Entrez le montant en euros</p>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
@@ -93,4 +93,4 @@
</div>
<% end %>
</div>
</div>
</div>

View File

@@ -1,212 +0,0 @@
<% content_for(:title, "Code de réduction #{@promotion_code.code} - #{@event.name}") %>
<div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<!-- Breadcrumb -->
<%= render 'components/breadcrumb', crumbs: [
{ name: 'Accueil', path: root_path },
{ name: 'Tableau de bord', path: dashboard_path },
{ name: 'Mes événements', path: promoter_events_path },
{ name: @event.name, path: promoter_event_path(@event) },
{ name: 'Codes de réduction', path: promoter_event_promotion_codes_path(@event) },
{ name: @promotion_code.code }
] %>
<div class="max-w-4xl mx-auto">
<div class="mb-8">
<div class="flex items-center space-x-4 mb-4">
<%= link_to promoter_event_promotion_codes_path(@event), class: "text-gray-400 hover:text-gray-600 transition-colors" do %>
<i data-lucide="arrow-left" class="w-5 h-5"></i>
<% end %>
<div class="flex-1">
<h1 class="text-3xl font-bold text-gray-900 mb-2">Détails du code de réduction</h1>
<p class="text-gray-600">
<code class="bg-gray-100 px-2 py-1 rounded text-sm"><%= @promotion_code.code %></code> pour <%= link_to @event.name, promoter_event_path(@event), class: "text-purple-600 hover:text-purple-800" %>
</p>
</div>
</div>
</div>
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6 lg:gap-8">
<!-- Main content -->
<div class="lg:col-span-2 space-y-6 lg:space-y-8">
<!-- Code details -->
<div class="bg-white rounded-2xl border border-gray-200 p-6">
<h3 class="text-lg font-semibold text-gray-900 mb-6">Informations du code</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<div class="text-sm text-gray-500 mb-2">Code</div>
<div class="text-lg font-semibold bg-gray-100 px-3 py-2 rounded-lg font-mono">
<%= @promotion_code.code %>
</div>
</div>
<div>
<div class="text-sm text-gray-500 mb-2">Statut</div>
<div>
<% if @promotion_code.active? && (promotion_code.expires_at.nil? || promotion_code.expires_at > Time.current) %>
<span class="inline-flex px-3 py-1 text-sm font-semibold rounded-full bg-green-100 text-green-800">
<i data-lucide="check-circle" class="w-4 h-4 mr-1"></i>
Actif
</span>
<% elsif @promotion_code.expires_at && @promotion_code.expires_at <= Time.current %>
<span class="inline-flex px-3 py-1 text-sm font-semibold rounded-full bg-red-100 text-red-800">
<i data-lucide="x-circle" class="w-4 h-4 mr-1"></i>
Expiré
</span>
<% else %>
<span class="inline-flex px-3 py-1 text-sm font-semibold rounded-full bg-gray-100 text-gray-800">
<i data-lucide="pause-circle" class="w-4 h-4 mr-1"></i>
Inactif
</span>
<% end %>
</div>
</div>
<div>
<div class="text-sm text-gray-500 mb-2">Montant de la réduction</div>
<div class="text-lg font-semibold text-green-600">
<%= number_to_currency(@promotion_code.discount_amount_cents / 100.0, unit: "€") %>
</div>
</div>
<div>
<div class="text-sm text-gray-500 mb-2">Événement</div>
<div class="text-lg font-semibold">
<%= link_to @event.name, promoter_event_path(@event), class: "text-purple-600 hover:text-purple-800" %>
</div>
</div>
</div>
</div>
<!-- Usage stats -->
<div class="bg-white rounded-2xl border border-gray-200 p-6">
<h3 class="text-lg font-semibold text-gray-900 mb-6">Statistiques d'utilisation</h3>
<div class="grid grid-cols-2 md:grid-cols-4 gap-4">
<div class="text-center p-4 bg-gray-50 rounded-lg">
<div class="text-2xl font-bold text-gray-900">
<%= @promotion_code.uses_count %>
</div>
<div class="text-sm text-gray-500">Utilisations</div>
</div>
<div class="text-center p-4 bg-gray-50 rounded-lg">
<div class="text-2xl font-bold text-blue-600">
<% if @promotion_code.usage_limit %>
<%= @promotion_code.usage_limit - @promotion_code.uses_count %>
<% else %>
<% end %>
</div>
<div class="text-sm text-gray-500">Restants</div>
</div>
<div class="text-center p-4 bg-gray-50 rounded-lg">
<div class="text-2xl font-bold text-purple-600">
<%= @promotion_code.orders.count %>
</div>
<div class="text-sm text-gray-500">Commandes</div>
</div>
<div class="text-center p-4 bg-gray-50 rounded-lg">
<div class="text-2xl font-bold text-green-600">
<%= number_to_currency(@promotion_code.orders.sum(:total_amount_cents) / 100.0, unit: "€") %>
</div>
<div class="text-sm text-gray-500">Montant total</div>
</div>
</div>
</div>
<!-- Orders using this code -->
<% if @promotion_code.orders.any? %>
<div class="bg-white rounded-2xl border border-gray-200 p-6">
<h3 class="text-lg font-semibold text-gray-900 mb-4">Commandes utilisant ce code</h3>
<div class="space-y-3">
<% @promotion_code.orders.includes(:user).order(created_at: :desc).limit(5).each do |order| %>
<div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
<div>
<div class="font-medium text-gray-900">Commande #<%= order.id %></div>
<div class="text-sm text-gray-500">
<%= order.user.email %> • <%= l(order.created_at, format: :short) %>
</div>
</div>
<div class="text-right">
<div class="font-medium text-gray-900"><%= number_to_currency(order.total_amount_cents / 100.0, unit: "€") %></div>
<div class="text-sm text-gray-500"><%= order.status %></div>
</div>
</div>
<% end %>
<% if @promotion_code.orders.count > 5 %>
<div class="text-center pt-2">
<span class="text-sm text-gray-500">
Et <%= @promotion_code.orders.count - 5 %> autres commandes...
</span>
</div>
<% end %>
</div>
</div>
<% end %>
</div>
<!-- Sidebar -->
<div class="space-y-6">
<!-- Additional info -->
<div class="bg-white rounded-2xl border border-gray-200 p-6">
<h3 class="text-lg font-semibold text-gray-900 mb-4">Informations supplémentaires</h3>
<div class="space-y-4">
<div>
<span class="text-sm text-gray-500">Créé par</span>
<p class="text-sm font-medium"><%= @promotion_code.user.email %></p>
</div>
<div>
<span class="text-sm text-gray-500">Créé le</span>
<p class="text-sm"><%= l(@promotion_code.created_at, format: :long) %></p>
</div>
<div>
<span class="text-sm text-gray-500">Modifié le</span>
<p class="text-sm"><%= l(@promotion_code.updated_at, format: :long) %></p>
</div>
<% if @promotion_code.expires_at %>
<div>
<span class="text-sm text-gray-500">Date d'expiration</span>
<p class="text-sm"><%= l(@promotion_code.expires_at, format: :long) %></p>
</div>
<% else %>
<div>
<span class="text-sm text-gray-500">Date d'expiration</span>
<p class="text-sm">Jamais</p>
</div>
<% end %>
</div>
</div>
<!-- Actions -->
<div class="bg-white rounded-2xl border border-gray-200 p-6">
<h3 class="text-lg font-semibold text-gray-900 mb-4">Actions</h3>
<div class="space-y-3">
<%= link_to edit_promoter_event_promotion_code_path(@event, @promotion_code), class: "w-full inline-flex items-center justify-center px-4 py-3 bg-gray-900 text-white font-medium rounded-lg hover:bg-gray-800 transition-colors duration-200" do %>
<i data-lucide="edit" class="w-4 h-4 mr-2"></i>
Modifier
<% end %>
<% if @promotion_code.orders.empty? %>
<%= button_to promoter_event_promotion_code_path(@event, @promotion_code), method: :delete,
data: { confirm: "Êtes-vous sûr de vouloir supprimer ce code de réduction ?" },
class: "w-full inline-flex items-center justify-center px-4 py-3 text-red-600 font-medium rounded-lg hover:bg-red-50 transition-colors duration-200" do %>
<i data-lucide="trash-2" class="w-4 h-4 mr-2"></i>
Supprimer
<% end %>
<% end %>
<%= link_to promoter_event_promotion_codes_path(@event), class: "w-full inline-flex items-center justify-center px-4 py-3 border border-gray-300 text-gray-700 font-medium rounded-lg hover:bg-gray-50 transition-colors duration-200" do %>
<i data-lucide="arrow-left" class="w-4 h-4 mr-2"></i>
Retour à la liste
<% end %>
</div>
</div>
</div>
</div>
</div>
</div>