Skip to content

Commit

Permalink
Close #1037 Add Quantity to Customer and Add Query or Create Variant …
Browse files Browse the repository at this point in the history
…in Subscription
  • Loading branch information
Regene27 committed Mar 7, 2024
1 parent 1c85479 commit 00231bc
Show file tree
Hide file tree
Showing 18 changed files with 385 additions and 20 deletions.
10 changes: 10 additions & 0 deletions app/controllers/spree/billing/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ def scope
current_vendor.products.where(subscribable: true)
end

def show
if request.xhr?
@variant = @product.variants.build

render layout: false
else
redirect_to edit_billing_product_url(@product)
end
end

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

Expand Down
33 changes: 30 additions & 3 deletions app/controllers/spree/billing/subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
module Spree
module Billing
class SubscriptionsController < Spree::Billing::BaseController
before_action :load_customer
before_action :customer
before_action :load_subscription, if: -> { member_action? }
before_action :product, only: :create

def create
find_or_create_variant

@subscription = @variant.subscriptions.build(subscription_params.merge(variant_id: @variant.id))

if @subscription.save
flash[:success] = flash_message_for(@subscription, :successfully_created)
else
flash[:error] = flash_message_for(@subscription, :not_created)
end
redirect_to billing_customer_subscriptions_url(@customer)
end

protected

Expand All @@ -13,14 +27,19 @@ def collection
@collection = @search.result.page(page).per(per_page)
end

def load_customer
customer
def product
product_id = params[:product_id] || params.dig(:variant, :product_id)
@product = Spree::Product.find(product_id)
end

def load_subscription
@subscription = @object
end

def find_or_create_variant
@variant = SpreeCmCommissioner::VariantChecker.new(@product, variant_params, current_vendor).find_or_create_variant
end

def customer
@customer ||= SpreeCmCommissioner::Customer.find(params[:customer_id])
end
Expand All @@ -39,6 +58,14 @@ def object_name
def collection_url(options = {})
billing_customer_subscriptions_url(options)
end

def variant_params
params.require(:variant).permit(:product_id, :sku, :price, option_value_ids: [])
end

def subscription_params
params.require(:spree_cm_commissioner_subscription).permit(:start_date, :customer_id, :status)
end
end
end
end
3 changes: 3 additions & 0 deletions app/models/spree_cm_commissioner/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Subscription < SpreeCmCommissioner::Base

belongs_to :variant, class_name: 'Spree::Variant'
belongs_to :customer, class_name: 'SpreeCmCommissioner::Customer'
belongs_to :product, class_name: 'Spree::Product'

has_many :orders, -> { order(:created_at) }, class_name: 'Spree::Order', dependent: :nullify
has_many :line_items, through: :orders, class_name: 'Spree::LineItem'
Expand All @@ -23,6 +24,8 @@ class Subscription < SpreeCmCommissioner::Base
after_create :create_order
after_commit :update_customer_active_subscriptions_count

accepts_nested_attributes_for :variant

def create_order
SpreeCmCommissioner::SubscribedOrderCreator.call(subscription: self)
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/spree_cm_commissioner/variant_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def self.prepended(base)
base.validate :validate_option_types

base.scope :subscribable, -> { active.joins(:product).where(product: { subscribable: true, status: :active }) }

base.has_many :subscriptions, class_name: 'SpreeCmCommissioner::Subscription', dependent: :destroy
end

def selected_option_value_ids
Expand Down
31 changes: 31 additions & 0 deletions app/services/spree_cm_commissioner/sku_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module SpreeCmCommissioner
class SkuGenerator
def initialize(product, variant_params)
@product = product
@variant_params = variant_params
end

def generate_sku
load_product

return if @variant_params.nil? || @variant_params.blank?

option_values = @variant_params[:option_value_ids].map { |id| Spree::OptionValue.find(id) }
sku_parts = [@product.name]

option_values.each do |option_value|
sku_parts << "#{option_value.option_type.name}-#{option_value.name}"
end

sku_parts << "price-#{@variant_params[:price]}"

sku_parts.join('-').gsub(' ', '-').downcase
end

private

def load_product
@product ||= Spree::Product.find(@variant_params[:product_id])
end
end
end
32 changes: 32 additions & 0 deletions app/services/spree_cm_commissioner/variant_checker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module SpreeCmCommissioner
class VariantChecker
attr_reader :variant

def initialize(product, variant_params, current_vendor)
@product = product
@variant_params = variant_params
@current_vendor = current_vendor
end

def find_or_create_variant
find_variant_by_sku || create_variant
end

private

def find_variant_by_sku
@variant = @product.variants.where(sku: variant_sku).first
end

def create_variant
variant_creator = SpreeCmCommissioner::VariantCreator.new(@product, @variant_params, @current_vendor)
variant_creator.create_variant
@variant = variant_creator.variant
end

def variant_sku
sku_generator = SpreeCmCommissioner::SkuGenerator.new(@product, @variant_params)
sku_generator.generate_sku
end
end
end
37 changes: 37 additions & 0 deletions app/services/spree_cm_commissioner/variant_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module SpreeCmCommissioner
class VariantCreator
attr_reader :variant

def initialize(product, variant_params, current_vendor)
@product = product
@variant_params = variant_params
@current_vendor = current_vendor
end

def create_variant
@variant = @product.variants.build
generate_sku
set_variant_attributes
@variant.save
@variant
end

private

def generate_sku
sku_generator = SpreeCmCommissioner::SkuGenerator.new(@product, @variant_params)
@variant.sku = sku_generator.generate_sku
end

def set_variant_attributes
@variant.option_value_ids = @variant_params[:option_value_ids]
@variant.price = @variant_params[:price]
stock_item = @variant.stock_items.build(stock_location_id: stock_location, count_on_hand: 1, backorderable: false)
stock_item.save
end

def stock_location
@stock_location ||= Spree::StockLocation.find_by!(vendor_id: @current_vendor.id).id
end
end
end
5 changes: 5 additions & 0 deletions app/views/spree/billing/customers/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@
<%= f.collection_select(:taxon_id, Spree::Taxon.where(depth: 1), :id, :name, { include_blank: Spree.t('match_choices.none') }, { class: 'select2', required: true })%>
<%= f.error_message_on :taxon_id %>
<% end %>
<%= f.field_container :quantity do %>
<%= f.label :quantity, I18n.t('activerecord.attributes.spree/order.quantity') %>
<%= f.number_field :quantity, class: 'form-control', required: true %>
<%= f.error_message_on :quantity %>
<% end %>
</div>
</div>
33 changes: 33 additions & 0 deletions app/views/spree/billing/products/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%= fields_for @variant do |f| %>
<div class="card mb-3">
<div class="card-header">
<h5 class="mb-0"><%= Spree.t(:variant) %></h5>
</div>
<%= f.hidden_field :product_id, value: @product.id %>

<div class="row">
<div data-hook="billing_variant_form_fields" class="col-6 p-4">
<div data-hook="variants">
<div class="form-group " data-hook="price">
<%= f.label :price, Spree.t(:price) %>
<%= f.text_field :price, placeholder: I18n.t('spree.billing.subscription_price_placeholder', currency: @variant.currency), value: number_to_currency(@variant.price, unit: ''), class: 'form-control' %>
</div>
</div>
</div>
<div data-hook="billing_varaint_option_types_field" class="col-6 p-4">
<% @product.option_types.each do |option_type| %>
<div class="form-group" data-hook="presentation">
<%= label :new_variant, option_type.presentation %>
<% if option_type.name == 'color' %>
<%= f.collection_select 'option_value_ids', option_type.option_values, :id, :name,
{ include_blank: true }, { name: 'variant[option_value_ids][]', class: 'select2-clear form-control', id: "option_value_ids-#{option_type.id}" } %>
<% else %>
<%= f.collection_select 'option_value_ids', option_type.option_values, :id, :presentation,
{ include_blank: true }, { name: 'variant[option_value_ids][]', class: 'select2-clear form-control', id: "option_value_ids-#{option_type.id}" } %>
<% end %>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
31 changes: 30 additions & 1 deletion app/views/spree/billing/shared/_invoice.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
@email = @vendor.notification_email
@note = @vendor.note

@quantity = @customer.quantity
@product = @invoice.order.line_items.first.variant.sku
@price = @invoice.order.line_items.first.display_price
@sum_total = @price.to_s.gsub(/[^\d.]/, '').to_d * @quantity
@total_price = @sum_total
@sum_total = Spree::Money.new(@sum_total, currency: @price.currency).to_s
@order.adjustments.each do |adjustment|
@total_price += adjustment.amount
end
@total_price = Spree::Money.new(@total_price, currency: @price.currency).to_s
@total = @invoice.order.display_total
@from_date = @invoice.order.line_items.first.to_date.strftime("%B %Y")
@date = @invoice.date.strftime('%Y-%m-%d')
Expand Down Expand Up @@ -92,8 +100,10 @@
<tr>
<th scope="col">ល.រ​​​ / N</th>
<th scope="col">បរិយាយ / Description</th>
<th scope="col">ចំនួន / Quantity</th>
<th scope="col">ប្រចាំខែ / Month</th>
<th scope="col">ទឹកប្រាក់​ / Amount</th>
<th scope="col">ទឹកប្រាក់​សរុប / Total Amount</th>
</tr>
</thead>
<tbody class="list-unstyled">
Expand All @@ -104,6 +114,11 @@
<li> <%= @product %></li>
</ul>
</td>
<td>
<ul class="list-unstyled">
<li> <%= @quantity %></li>
</ul>
</td>
<td>
<ul class="list-unstyled">
<li> <%= @from_date %></li>
Expand All @@ -116,6 +131,11 @@
</li>
</ul>
</td>
<td>
<ul class="list-unstyled">
<li> <%= @sum_total %></li>
</ul>
</td>
</tr>
<% @order.adjustments.each do |adjustment| %>
<tr>
Expand All @@ -126,6 +146,15 @@
</ul>
</td>
<td></td>
<td>
</td>
<td>
<ul class="list-unstyled">
<li>
<%= adjustment.display_amount %>
</li>
</ul>
</td>
<td>
<ul class="list-unstyled">
<li>
Expand All @@ -142,7 +171,7 @@
<div class="d-flex justify-content-between align-items-center bg-primary pt-3 px-2 font-weight-bold text-white">
<p>ទឹកប្រាក់​ត្រូវទូទាត់​​ / Total Amount due</p>
<p>
<%= @total %>
<%= @total_price %>
</p>
</div>

Expand Down
Loading

0 comments on commit 00231bc

Please sign in to comment.