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

Add subscription details to backend #109

Merged
merged 9 commits into from
Jun 16, 2020
23 changes: 23 additions & 0 deletions app/controllers/spree/admin/installments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Spree
module Admin
class InstallmentsController < ResourceController
belongs_to 'subscription', model_class: SolidusSubscriptions::Subscription

skip_before_action :load_resource, only: :index

def index
@search = collection.ransack(params[:q])

@installments = @search.result(distinct: true).
page(params[:page]).
per(params[:per_page] || Spree::Config[:orders_per_page])
end

private

def model_class
::SolidusSubscriptions::Installment
end
end
end
end
6 changes: 3 additions & 3 deletions app/controllers/spree/admin/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def cancel
notice = @subscription.errors.full_messages.to_sentence
end

redirect_to spree.admin_subscriptions_path, notice: notice
redirect_back(fallback_location: spree.admin_subscriptions_path, notice: notice)
end

def activate
Expand All @@ -48,7 +48,7 @@ def activate
notice = @subscription.errors.full_messages.to_sentence
end

redirect_to spree.admin_subscriptions_path, notice: notice
redirect_back(fallback_location: spree.admin_subscriptions_path, notice: notice)
end

def skip
Expand All @@ -59,7 +59,7 @@ def skip
date: @subscription.actionable_date
)

redirect_to spree.admin_subscriptions_path, notice: notice
redirect_back(fallback_location: spree.admin_subscriptions_path, notice: notice)
end

private
Expand Down
7 changes: 7 additions & 0 deletions app/models/solidus_subscriptions/installment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ def fulfilled?
details.where(success: true).exists?
end

# Returns the state of this fulfillment
#
# @return [Symbol] :fulfilled/:unfulfilled
def state
fulfilled? ? :fulfilled : :unfulfilled
end

# Mark this installment as having a failed payment
#
# @param order [Spree::Order] The order generated for this processing
Expand Down
8 changes: 8 additions & 0 deletions app/views/spree/admin/installments/_state_pill.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% state_class = {
fulfilled: 'active',
unfulfilled: 'error',
}[installment.state.to_sym] %>

<span class="pill pill-<%= state_class %>">
<%= SolidusSubscriptions::Installment.human_attribute_name("state.#{installment.state}") %>
</span>
36 changes: 36 additions & 0 deletions app/views/spree/admin/installments/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<% content_for(:page_title, t('.title')) %>

<%= render 'spree/admin/shared/subscription_breadcrumbs', subscription: @subscription %>
<%= render 'spree/admin/shared/subscription_sidebar', subscription: @subscription %>
<%= render 'spree/admin/shared/subscription_tabs', current: :installments, subscription: @subscription %>
<%= render 'spree/admin/shared/subscription_actions', subscription: @subscription %>

<fieldset class="no-border-bottom">
<legend><%= t('spree.admin.installments.index.title') %></legend>

<%= paginate @installments, theme: 'solidus_admin' %>

<table id="listing_installments" class="index">
<thead>
<tr data-hook="admin_installments_index_headers">
<th><%= sort_link(@search, :actionable_date, SolidusSubscriptions::Installment.human_attribute_name(:actionable_date)) %></th>
<th><%= sort_link(@search, :created_at, SolidusSubscriptions::Installment.human_attribute_name(:created_at)) %></th>
<th><%= SolidusSubscriptions::Installment.human_attribute_name(:fulfilled) %></th>
<th data-hook="admin_installments_index_header_actions" class="actions"></th>
</tr>
</thead>

<tbody>
<% @installments.each do |installment| %>
<tr>
<td><%= l(installment.actionable_date.to_date) if installment.actionable_date %></td>
<td><%= l(installment.created_at.to_date) %></td>
<td><%= render 'state_pill', installment: installment %></td>
<td class="actions"></td>
</tr>
<% end %>
</tbody>
</table>

<%= paginate @installments, theme: 'solidus_admin' %>
</fieldset>
35 changes: 35 additions & 0 deletions app/views/spree/admin/shared/_subscription_actions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<% content_for :page_actions do %>
<% if subscription.state_events.include?(:cancel) %>
<%=
link_to(
t('spree.admin.subscriptions.actions.cancel'),
spree.cancel_admin_subscription_path(subscription),
method: :delete,
data: { confirm: t('spree.admin.subscriptions.actions.cancel_alert') },
class: 'btn btn-primary'
)
%>
<% end %>

<% if subscription.state_events.include?(:activate) %>
<%=
link_to(
t('spree.admin.subscriptions.actions.activate'),
spree.activate_admin_subscription_path(subscription),
method: :post,
class: 'btn btn-primary',
)
%>
<% end %>

<% if subscription.active? %>
<%=
link_to(
t('spree.admin.subscriptions.actions.skip'),
spree.skip_admin_subscription_path(subscription),
method: :post,
class: 'btn btn-default',
)
%>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<% admin_breadcrumb(
link_to(plural_resource_name(SolidusSubscriptions::Subscription), admin_subscriptions_path),
link_to("#{subscription.id}", edit_admin_subscription_path(subscription))
) %>
18 changes: 18 additions & 0 deletions app/views/spree/admin/shared/_subscription_sidebar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% content_for :sidebar_title do %>
<%= t("spree.admin.subscriptions.edit.sidebar") %>
<% end %>

<% content_for :sidebar do %>
<nav class="menu">
<fieldset class="no-border-top no-border-bottom" data-hook="admin_user_lifetime_stats">
<dl id="user-lifetime-stats">
<dt><%= SolidusSubscriptions::Subscription.human_attribute_name(:created_at) %>:</dt>
<dd><%= l(subscription.created_at.to_date) %></dd>
<dt><%= SolidusSubscriptions::Subscription.human_attribute_name(:state) %>:</dt>
<dd><%= render 'spree/admin/subscriptions/state_pill', subscription: subscription %></dd>
<dt><%= SolidusSubscriptions::Subscription.human_attribute_name(:processing_state) %>:</dt>
<dd><%= render 'spree/admin/subscriptions/processing_state_pill', subscription: subscription %></dd>
</dl>
</fieldset>
</nav>
<% end %>
12 changes: 12 additions & 0 deletions app/views/spree/admin/shared/_subscription_tabs.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% content_for :tabs do %>
<nav>
<ul class='tabs' data-hook="admin_subscription_tab_options">
<li<%== ' class="active"' if current == :details %>>
<%= link_to t("spree.admin.subscriptions.edit.details"), spree.edit_admin_subscription_path(subscription) %>
</li>
<li<%== ' class="active"' if current == :installments %>>
<%= link_to t("spree.admin.subscriptions.edit.installments"), spree.admin_subscription_installments_path(subscription) %>
</li>
</ul>
</nav>
<% end %>
114 changes: 58 additions & 56 deletions app/views/spree/admin/subscriptions/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,100 +1,102 @@
<fieldset data-hook="new_subscription">
<legend><%= t('.subscription') %></legend>
<div data-hook="new_subscription">
<fieldset class="no-border-bottom">
<legend><%= t('.subscription') %></legend>

<div data-hook="admin_subscription_form_fields">
<% if f.object.new_record? %>
<div class="field">
<%= f.label :user_id, I18n.t('spree.user'), class: "required" %>
<%= f.collection_select :user_id, Spree::User.all, :id, :email, {}, class: "select2 fullwidth" %>
</div>
<% end %>

<div data-hook="admin_subscription_form_fields">
<% if f.object.new_record? %>
<div class="field">
<%= f.label :user_id, I18n.t('spree.user'), class: "required" %>
<%= f.collection_select :user_id, Spree::User.all, :id, :email, {}, class: "select2 fullwidth" %>
<%= f.label :actionable_date %>
<%= f.text_field :actionable_date, class: "form-control fullwidth datepicker" %>
</div>
<% end %>

<div class="field">
<%= f.label :actionable_date %>
<%= f.text_field :actionable_date, class: "form-control fullwidth datepicker" %>
</div>

<div class="row">
<div class="col-6">
<div class="field">
<%= f.label :interval_length %>
<%= f.number_field :interval_length, class: "form-control fullwidth" %>
<div class="row">
<div class="col-6">
<div class="field">
<%= f.label :interval_length %>
<%= f.number_field :interval_length, class: "form-control fullwidth" %>
</div>
</div>
</div>

<div class="col-6">
<div class="field">
<%
unit_values = SolidusSubscriptions::LineItem.interval_units.keys
units = unit_values.map do |unit|
[
SolidusSubscriptions::LineItem.human_attribute_name("interval_units.#{unit}"),
unit,
]
end
%>
<div class="col-6">
<div class="field">
<%
unit_values = SolidusSubscriptions::LineItem.interval_units.keys
units = unit_values.map do |unit|
[
SolidusSubscriptions::LineItem.human_attribute_name("interval_units.#{unit}"),
unit,
]
end
%>

<%= f.label :interval_units %>
<%= f.select(:interval_units, units, {}, class: 'fullwidth select2') %>
<%= f.label :interval_units %>
<%= f.select(:interval_units, units, {}, class: 'fullwidth select2') %>
</div>
</div>
</div>
</div>

<div class="field">
<%= f.label :end_date %>
<%= f.text_field :end_date, class: "fullwidth datepicker" %>
</div>
<div class="field">
<%= f.label :end_date %>
<%= f.text_field :end_date, class: "fullwidth datepicker" %>
</div>

<div class="field">
<%= f.label :store_id %>
<%= f.collection_select :store_id, Spree::Store.all, :id, :name, {}, class: 'fullwidth select2' %>
<div class="field">
<%= f.label :store_id %>
<%= f.collection_select :store_id, Spree::Store.all, :id, :name, {}, class: 'fullwidth select2' %>
</div>
</div>
</div>
</fieldset>

<div class="js-customer-details">
<fieldset class="no-border-bottom">
<legend>Shipping Address</legend>
<fieldset class="no-border-bottom">
<legend>Shipping Address</legend>

<div class="js-customer-details">
<div class="js-shipping-address">
<%= f.fields_for :shipping_address do |sa_form| %>
<%= render partial: 'spree/admin/shared/address_form', locals: { f: sa_form, type: "shipping" } %>
<% end %>
</div>
</fieldset>
</div>
</div>
</fieldset>

<fieldset class="no-border-bottom">
<fieldset>
<legend><%= t('.subscription_line_items') %></legend>

<%= f.fields_for :line_items do |lf| %>
<div class="row">
<div class="col-10">
<div class="col-8">
<div class="field">
<%= lf.label :subscribable_id %>
<%= lf.collection_select :subscribable_id, Spree::Variant.where(subscribable: true), :id, :pretty_name, {}, { class: "fullwidth select2" } %>
</div>
</div>

<div class="col-1">
<div class="col-2">
<div class="field">
<%= lf.label :quantity %>
<%= lf.number_field :quantity, min: 1, class: "form-control fullwidth" %>
</div>
</div>

<div class="col-1">
<div class="col-2">
<div class="field align-center">
<%= lf.label :_destroy %>
<%= lf.check_box :_destroy, class: "form-control", disabled: lf.object.new_record? %>
</div>
</div>
</div>
<% end %>
</fieldset>

<% if f.object.new_record? %>
<%= render partial: 'spree/admin/shared/new_resource_links' %>
<% else %>
<%= render partial: 'spree/admin/shared/edit_resource_links' %>
<% end %>
</fieldset>
<% if f.object.new_record? %>
<%= render partial: 'spree/admin/shared/new_resource_links' %>
<% else %>
<%= render partial: 'spree/admin/shared/edit_resource_links' %>
<% end %>
</fieldset>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% state_class = {
success: 'active',
failed: 'error',
pending: 'inactive',
}[subscription.processing_state.to_sym] %>

<span class="pill pill-<%= state_class %>">
<%= subscription.class.human_attribute_name("processing_state.#{subscription.processing_state}") %>
</span>
10 changes: 10 additions & 0 deletions app/views/spree/admin/subscriptions/_state_pill.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% state_class = {
active: 'active',
canceled: 'error',
pending_cancellation: 'warning',
inactive: 'inactive',
}[subscription.state.to_sym] %>

<span class="pill pill-<%= state_class %>">
<%= subscription.human_state_name %>
</span>
7 changes: 4 additions & 3 deletions app/views/spree/admin/subscriptions/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<% content_for(:page_title) { t('.title') } %>

<% content_for :sidebar_title do %>
Details
<% end %>
<%= render 'spree/admin/shared/subscription_breadcrumbs', subscription: @subscription %>
<%= render 'spree/admin/shared/subscription_sidebar', subscription: @subscription %>
<%= render 'spree/admin/shared/subscription_tabs', current: :details, subscription: @subscription %>
<%= render 'spree/admin/shared/subscription_actions', subscription: @subscription %>

<%= form_for @subscription, url: spree.admin_subscription_path(@subscription) do |f| %>
<%= render "form", f: f %>
Expand Down
Loading