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

SP-107,108,109 Bulk promo codes #285

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions app/controllers/spree/admin/promotion_batches_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Spree
module Admin
class PromotionBatchesController < ResourceController
before_action :set_template_promotion

def index
@promotion_batches = Spree::PromotionBatch.where(template_promotion: @template_promotion)
end

def new
@promotion_batch = @template_promotion.promotion_batches.build
end

def create
Spree::PromotionBatches::CreateWithRandomCodes.new.call(template_promotion: @template_promotion, amount: params[:amount].to_i, random_characters: params[:random_characters].to_i, prefix: params[:prefix], suffix: params[:suffix])
redirect_to(admin_template_promotion_promotion_batches_path(template_promotion_id: @template_promotion.id))
end

def import; end

def process_import
file = params[:file].read
Spree::PromotionBatches::CreateWithCodes.new.call(template_promotion: @template_promotion, codes: file.split("\n"))
redirect_to(admin_template_promotion_promotion_batches_path(template_promotion_id: @template_promotion.id))
end

def export
@promotion_batch = @template_promotion.promotion_batches.find(params[:promotion_batch_id])
csv = Spree::PromotionBatches::Export.new.call(promotion_batch: @promotion_batch)
send_data csv, filename: "codes_#{@promotion_batch}.csv"
end

private

def collection_url
admin_template_promotion_promotion_batches_url(@template_promotion)
end

def new_object_url(options = nil)
new_admin_template_promotion_promotion_batch_url(@template_promotion)
end

def set_template_promotion
@template_promotion = Spree::Promotion.templates.find(params[:template_promotion_id])
end
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/spree/admin/promotions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def collection
params[:q] ||= HashWithIndifferentAccess.new
params[:q][:s] ||= 'id desc'

@collection = super
@collection = super.where(template: false)
@search = @collection.ransack(params[:q])
@collection = @search.result(distinct: true).
includes(promotion_includes).
Expand Down
82 changes: 82 additions & 0 deletions app/controllers/spree/admin/template_promotions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module Spree
module Admin
class TemplatePromotionsController < ResourceController
before_action :load_data

def create
invoke_callbacks(:create, :before)
@object.attributes = permitted_resource_params
@object.template = true
if @object.save
invoke_callbacks(:create, :after)
flash[:success] = flash_message_for(@object, :successfully_created)
respond_with(@object) do |format|
format.turbo_stream if create_turbo_stream_enabled?
format.html { redirect_to location_after_save }
format.js { render layout: false }
end
else
invoke_callbacks(:create, :fails)
respond_with(@object) do |format|
format.html { render action: :new, status: :unprocessable_entity }
format.js { render layout: false, status: :unprocessable_entity }
end
end
end

private

def new_object_url(options = {})
spree.new_admin_template_promotion_url(options)
end

def collection_url(options = {})
spree.admin_template_promotions_url(options)
end

def resource
return @resource if @resource

parent_model_name = parent_data[:model_name] if parent_data
@resource = Spree::Admin::Resource.new 'admin/spree', 'template_promotions', parent_model_name, object_name
end

def load_data
@actions = Rails.application.config.spree.promotions.actions

@calculators = Rails.application.config.spree.calculators.promotion_actions_create_adjustments
@promotion_categories = Spree::PromotionCategory.order(:name)
@promotion = @object
end

def collection
return @collection if defined?(@collection)

params[:q] ||= HashWithIndifferentAccess.new
params[:q][:s] ||= 'id desc'

@collection = super
@collection = @collection.templates
@search = @collection.ransack(params[:q])
@collection = @search.result(distinct: true).
includes(promotion_includes).
page(params[:page]).
per(params[:per_page] || Spree::Backend::Config[:admin_promotions_per_page])

@promotions = @collection
end

def promotion_includes
[:promotion_actions, :promotions_from_template]
end

def model_class
Spree::Promotion
end

def permitted_resource_params
params.require(:promotion).permit!
end
end
end
end
8 changes: 8 additions & 0 deletions app/helpers/spree/admin/navigation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,14 @@ def variants_actions
def product_properties_actions
Rails.application.config.spree_backend.actions[:product_properties]
end

def template_promotion_actions
Rails.application.config.spree_backend.actions[:template_promotion]
end

def promotion_batches_actions
Rails.application.config.spree_backend.actions[:promotion_batches]
end
# rubocop:enable Metrics/ModuleLength
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module Spree
module Admin
module Actions
class PromotionBatchesDefaultActionsBuilder
include Spree::Core::Engine.routes.url_helpers

def build
root = Root.new
add_view_promotions_action(root)
add_import_promotion_batch_action(root)
add_generate_promotion_batch_action(root)
root
end

private

def add_generate_promotion_batch_action(root)
action =
ActionBuilder.new('generate_codes', ->(template_promotion) { new_admin_template_promotion_promotion_batch_path(template_promotion_id: template_promotion.id) }).
with_label_translation_key('admin.promotion_batches.generate_codes').
with_icon_key('add.svg').
with_style(Spree::Admin::Actions::ActionStyle::PRIMARY).
with_create_ability_check(Spree::PromotionBatch).
build

root.add(action)
end

def add_import_promotion_batch_action(root)
action =
ActionBuilder.new('import_csv', ->(template_promotion) { import_admin_template_promotion_promotion_batches_path(template_promotion_id: template_promotion.id) }).
with_label_translation_key('admin.promotion_batches.import_csv').
with_icon_key('file-earmark-arrow-up.svg').
with_style(Spree::Admin::Actions::ActionStyle::LIGHT).
with_create_ability_check(Spree::PromotionBatch).
build

root.add(action)
end

def add_view_promotions_action(root)
action =
ActionBuilder.new('view_promotions', ->(template_promotion) { admin_promotions_path(q: { for_template_promotion_id: template_promotion.id }) }).
with_label_translation_key('admin.promotion_batches.view_promotions').
with_icon_key('list.svg').
with_style(Spree::Admin::Actions::ActionStyle::LIGHT).
with_manage_ability_check(Spree::Promotion).
build

root.add(action)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module Spree
module Admin
module Actions
class TemplatePromotionDefaultActionsBuilder
include Spree::Core::Engine.routes.url_helpers

def build
root = Root.new
add_view_promotion_batches_action(root)
add_import_promotion_batch_action(root)
add_generate_promotion_batch_action(root)
root
end

private

def add_generate_promotion_batch_action(root)
action =
ActionBuilder.new('generate_codes', ->(template_promotion) { new_admin_template_promotion_promotion_batch_path(template_promotion_id: template_promotion.id) }).
with_label_translation_key('admin.promotion_batches.generate_codes').
with_icon_key('add.svg').
with_style(Spree::Admin::Actions::ActionStyle::PRIMARY).
with_create_ability_check(Spree::PromotionBatch).
build

root.add(action)
end

def add_import_promotion_batch_action(root)
action =
ActionBuilder.new('import_csv', ->(template_promotion) { import_admin_template_promotion_promotion_batches_path(template_promotion_id: template_promotion.id) }).
with_label_translation_key('admin.promotion_batches.import_csv').
with_icon_key('file-earmark-arrow-up.svg').
with_style(Spree::Admin::Actions::ActionStyle::LIGHT).
with_create_ability_check(Spree::PromotionBatch).
build

root.add(action)
end

def add_view_promotion_batches_action(root)
action =
ActionBuilder.new('view_promotion_batches', ->(template_promotion) { admin_template_promotion_promotion_batches_path(template_promotion) }).
with_label_translation_key('admin.promotion_batches.view_promotion_batches').
with_icon_key('list.svg').
with_style(Spree::Admin::Actions::ActionStyle::LIGHT).
with_manage_ability_check(Spree::Promotion).
build

root.add(action)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ def add_promotions_section(root)
ItemBuilder.new('promotion_categories', admin_promotion_categories_path).
with_admin_ability_check(Spree::PromotionCategory).
with_label_translation_key('admin.tab.promotion_categories').
build,
ItemBuilder.new('bulk_promo_codes', admin_template_promotions_path).
with_admin_ability_check(Spree::Promotion, Spree::PromotionBatch).
with_label_translation_key('admin.tab.bulk_promo_codes').
build
]

Expand Down
26 changes: 26 additions & 0 deletions app/views/spree/admin/promotion_batches/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="row">
<div class="col-12 col-md-6">
<h4><%= Spree.t('admin.promotion_batches.general_settings') %></h4>
<div class="form-group">
<%= label_tag :amount, raw(Spree.t('admin.promotion_batches.amount') + required_span_tag) %>
<%= number_field_tag :amount, nil, class: 'form-control', required: :required %>
</div>

<hr/>
<h4><%= Spree.t('admin.promotion_batches.generator_settings') %></h4>
<div class="form-group">
<%= label_tag :random_characters, raw(Spree.t('admin.promotion_batches.random_characters') + required_span_tag) %>
<%= number_field_tag :random_characters, nil, class: 'form-control', required: :required %>
</div>

<div class="form-group">
<%= label_tag :prefix, Spree.t('admin.promotion_batches.prefix') %>
<%= text_field_tag :prefix, nil, class: 'form-control' %>
</div>

<div class="form-group">
<%= label_tag :suffix, Spree.t('admin.promotion_batches.suffix') %>
<%= text_field_tag :suffix, nil, class: 'form-control' %>
</div>
</div>
</div>
6 changes: 6 additions & 0 deletions app/views/spree/admin/promotion_batches/_size.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="col-12 col-lg-4">
<%= form_tag(duplicate_admin_promotion_batch_path(@promotion_batch), method: :post) do %>
<%= number_field_tag(:batch_size) %>
<%= submit_tag("Duplicate") %>
<% end %>
</div>
24 changes: 24 additions & 0 deletions app/views/spree/admin/promotion_batches/import.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<% content_for :page_title do %>
<%= link_to Spree.t(:template_promotions), admin_template_promotions_url %> /
<%= link_to @template_promotion.name, edit_admin_template_promotion_url(@template_promotion) %> /
<%= link_to Spree.t(:promotion_batches), admin_template_promotion_promotion_batches_url(@template_promotion) %> /
<%= Spree.t('admin.promotion_batches.import_csv') %>
<% end %>


<div class="card">
<div class="card-body">
<%= form_tag process_import_admin_template_promotion_promotion_batches_path(@template_promotion), method: :post, multipart: true do %>
<div class="row">
<div class="col-12 col-md-6">
<div class="form-group">
<%= label_tag :file, raw(Spree.t('admin.promotion_batches.csv_file') + required_span_tag), class: 'form-control-file' %>
<%= file_field_tag :file, accept: '.csv', required: :required %>
<small class="form-text text-muted"><%= Spree.t('admin.promotion_batches.csv_file_notice') %></small>
</div>
<%= render partial: 'spree/admin/shared/new_resource_links' %>
</div>
</div>
<% end %>
</div>
</div>
54 changes: 54 additions & 0 deletions app/views/spree/admin/promotion_batches/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<% content_for :page_title do %>
<%= link_to Spree.t(:template_promotions), admin_template_promotions_url %> /
<%= link_to @template_promotion.name, edit_admin_template_promotion_url(@template_promotion) %> /
<%= Spree.t(:promotion_batches) %>
<% end %>

<% content_for :page_actions do %>
<% promotion_batches_actions.items.each do |action| %>
<% next unless action.available?(current_ability) %>
<%= button_link_to(
Spree.t(action.label_translation_key),
action.url(@template_promotion),
class: action.classes,
icon: action.icon_key
) %>
<% end %>
<% end %>

<% if @promotion_batches.any? %>
<div class="table-responsive border rounded bg-white">
<table class="table">
<thead class="text-muted">
<tr>
<th><%= Spree.t('admin.id') %></th>
<th><%= Spree.t('admin.promotion_batches.generated_codes') %></th>
<th><%= Spree.t(:created_at) %></th>
<th><%= Spree.t(:state) %></th>
<th data-hook="admin_promotion_batches_index_header_actions" class="actions"></th>
</tr>
</thead>
<tbody>
<% @promotion_batches.each do |promotion_batch| %>
<tr id="<%= spree_dom_id promotion_batch %>">
<td><%= promotion_batch.id %></td>
<td><%= promotion_batch.promotions.count %></td>
<td><%= promotion_batch.created_at.strftime('%F %T %Z') %></td>
<td><%= promotion_batch.state %></td>
<td>
<%= button_link_to('', admin_promotions_path({ q: { promotion_batch_id_eq: promotion_batch.id }}), icon: 'list.svg', class: 'btn btn-light btn-sm with-tip icon-link', data: { 'original-title' => Spree.t('admin.promotion_batches.view_promotions') }) %>
<%= button_link_to('', admin_template_promotion_promotion_batch_export_path(template_promotion_id: @template_promotion.id, promotion_batch_id: promotion_batch.id), icon: 'file-earmark-arrow-down.svg', class: 'btn btn-light btn-sm with-tip icon-link', data: { 'original-title' => Spree.t('admin.promotion_batches.export_csv')} ) %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<div class="text-center no-objects-found m-5">
<%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::PromotionBatch)) %>,
<%= link_to Spree.t('admin.promotion_batches.generate_codes'), new_object_url if can?(:create, Spree::PromotionBatch) %>
<%= Spree.t(:or) %>
<%= link_to Spree.t('admin.promotion_batches.import_csv'), import_admin_template_promotion_promotion_batches_path(@template_promotion) if can?(:create, Spree::PromotionBatch) %>!
</div>
<% end %>
Loading
Loading