diff --git a/app/controllers/spree/billing/subscriptions_controller.rb b/app/controllers/spree/billing/subscriptions_controller.rb index 488d304b0..30337e4c6 100644 --- a/app/controllers/spree/billing/subscriptions_controller.rb +++ b/app/controllers/spree/billing/subscriptions_controller.rb @@ -4,6 +4,37 @@ class SubscriptionsController < Spree::Billing::BaseController before_action :load_customer before_action :load_subscription, if: -> { member_action? } + def load_option_types_form + load_product + + @variant = @product.variants.build + + render partial: 'spree/billing/subscriptions/option_types_form' + end + + def create + load_product + + sku_generator = SpreeCmCommissioner::SkuGenerator.new(@product, params[:variant]) + variant_sku = sku_generator.generate_sku + + @variant = @product.variants.where(sku: variant_sku).first_or_initialize if variant_sku.present? + + unless @variant.persisted? + variant_creator = SpreeCmCommissioner::VariantCreator.variant.new(@product, params[:variant], @current_vendor) + @variant = variant_creator.create_variant + end + + @subscription = @variant.subscriptions.new(subscription_params) + @subscription.variant_id = @variant.id + + @subscription.save + return unless @subscription + + flash[:success] = flash_message_for(@subscription, :successfully_created) + redirect_to billing_customer_subscriptions_url(@customer) + end + protected def collection @@ -13,6 +44,11 @@ def collection @collection = @search.result.page(page).per(per_page) end + def load_product + product_id = params[:product_id] || params.dig(:variant, :product_id) + @product = Spree::Product.find(product_id) + end + def load_customer customer end @@ -39,6 +75,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 diff --git a/app/models/spree_cm_commissioner/subscription.rb b/app/models/spree_cm_commissioner/subscription.rb index 159bda4cc..dc148ca1c 100644 --- a/app/models/spree_cm_commissioner/subscription.rb +++ b/app/models/spree_cm_commissioner/subscription.rb @@ -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' @@ -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 diff --git a/app/models/spree_cm_commissioner/variant_decorator.rb b/app/models/spree_cm_commissioner/variant_decorator.rb index 754f5afae..8eb1d3396 100644 --- a/app/models/spree_cm_commissioner/variant_decorator.rb +++ b/app/models/spree_cm_commissioner/variant_decorator.rb @@ -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 diff --git a/app/services/spree_cm_commissioner/sku_generator.rb b/app/services/spree_cm_commissioner/sku_generator.rb new file mode 100644 index 000000000..d58c6ee9c --- /dev/null +++ b/app/services/spree_cm_commissioner/sku_generator.rb @@ -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 diff --git a/app/services/spree_cm_commissioner/variant_creator.rb b/app/services/spree_cm_commissioner/variant_creator.rb new file mode 100644 index 000000000..a4c4664f3 --- /dev/null +++ b/app/services/spree_cm_commissioner/variant_creator.rb @@ -0,0 +1,35 @@ +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.new + generate_sku + set_variant_attributes + 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] + @variant.stock_items.new(stock_location_id: @current_vendor.id, count_on_hand: 1) + end + + def save_variant + @variant.save + end + end +end diff --git a/app/views/spree/billing/customers/_form.html.erb b/app/views/spree/billing/customers/_form.html.erb index 8f8f5cc9e..e0a695365 100644 --- a/app/views/spree/billing/customers/_form.html.erb +++ b/app/views/spree/billing/customers/_form.html.erb @@ -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 %> diff --git a/app/views/spree/billing/shared/_invoice.html.erb b/app/views/spree/billing/shared/_invoice.html.erb index b5ad7c154..e508ad06a 100644 --- a/app/views/spree/billing/shared/_invoice.html.erb +++ b/app/views/spree/billing/shared/_invoice.html.erb @@ -8,8 +8,10 @@ @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 + @total_price = @price.to_s.gsub(/[^\d.]/, '').to_d * @quantity @total = @invoice.order.display_total @from_date = @invoice.order.line_items.first.to_date.strftime("%B %Y") @date = @invoice.date.strftime('%Y-%m-%d') @@ -92,8 +94,10 @@
ទឹកប្រាក់ត្រូវទូទាត់ / Total Amount due
- <%= @total %> + <%= @total_price %>