mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-29 21:17:17 +00:00
Replace references to 'standing order' with 'subscription'
This commit is contained in:
@@ -56,7 +56,7 @@ module Admin
|
||||
end
|
||||
|
||||
# GET /admin/customers/:id/addresses
|
||||
# Used by standing orders form to load details for selected customer
|
||||
# Used by subscriptions form to load details for selected customer
|
||||
def addresses
|
||||
finder = OpenFoodNetwork::AddressFinder.new(@customer, @customer.email)
|
||||
bill_address = Api::AddressSerializer.new(finder.bill_address).serializable_hash
|
||||
@@ -65,7 +65,7 @@ module Admin
|
||||
end
|
||||
|
||||
# GET /admin/customers/:id/cards
|
||||
# Used by standing orders form to load details for selected customer
|
||||
# Used by subscriptions form to load details for selected customer
|
||||
def cards
|
||||
cards = Spree::CreditCard.where(user_id: @customer.user_id)
|
||||
render json: ActiveModel::ArraySerializer.new(cards, each_serializer: Api::CreditCardSerializer)
|
||||
|
||||
@@ -12,8 +12,8 @@ module Admin
|
||||
before_filter :remove_unauthorized_bulk_attrs, only: [:bulk_update]
|
||||
before_filter :check_editable_schedule_ids, only: [:create, :update]
|
||||
around_filter :protect_invalid_destroy, only: :destroy
|
||||
create.after :sync_standing_orders
|
||||
update.after :sync_standing_orders
|
||||
create.after :sync_subscriptions
|
||||
update.after :sync_subscriptions
|
||||
|
||||
def index
|
||||
respond_to do |format|
|
||||
@@ -204,14 +204,14 @@ module Admin
|
||||
params[:order_cycle][:schedule_ids] = result
|
||||
end
|
||||
|
||||
def sync_standing_orders
|
||||
def sync_subscriptions
|
||||
return unless params[:order_cycle][:schedule_ids]
|
||||
removed_ids = @existing_schedule_ids - @order_cycle.schedule_ids
|
||||
new_ids = @order_cycle.schedule_ids - @existing_schedule_ids
|
||||
if removed_ids.any? || new_ids.any?
|
||||
schedules = Schedule.where(id: removed_ids + new_ids)
|
||||
standing_orders = StandingOrder.where(schedule_id: schedules)
|
||||
syncer = OpenFoodNetwork::ProxyOrderSyncer.new(standing_orders)
|
||||
subscriptions = Subscription.where(schedule_id: schedules)
|
||||
syncer = OpenFoodNetwork::ProxyOrderSyncer.new(subscriptions)
|
||||
syncer.sync!
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,8 +4,8 @@ require 'open_food_network/proxy_order_syncer'
|
||||
module Admin
|
||||
class SchedulesController < ResourceController
|
||||
before_filter :check_editable_order_cycle_ids, only: [:create, :update]
|
||||
create.after :sync_standing_orders
|
||||
update.after :sync_standing_orders
|
||||
create.after :sync_subscriptions
|
||||
update.after :sync_subscriptions
|
||||
|
||||
respond_to :json
|
||||
|
||||
@@ -54,13 +54,13 @@ module Admin
|
||||
@permissions = OpenFoodNetwork::Permissions.new(spree_current_user)
|
||||
end
|
||||
|
||||
def sync_standing_orders
|
||||
def sync_subscriptions
|
||||
return unless params[:schedule][:order_cycle_ids]
|
||||
removed_ids = @existing_order_cycle_ids - @schedule.order_cycle_ids
|
||||
new_ids = @schedule.order_cycle_ids - @existing_order_cycle_ids
|
||||
return unless removed_ids.any? || new_ids.any?
|
||||
standing_orders = StandingOrder.where(schedule_id: @schedule)
|
||||
syncer = OpenFoodNetwork::ProxyOrderSyncer.new(standing_orders)
|
||||
subscriptions = Subscription.where(schedule_id: @schedule)
|
||||
syncer = OpenFoodNetwork::ProxyOrderSyncer.new(subscriptions)
|
||||
syncer.sync!
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require 'open_food_network/permissions'
|
||||
|
||||
module Admin
|
||||
class StandingOrdersController < ResourceController
|
||||
class SubscriptionsController < ResourceController
|
||||
before_filter :load_shops, only: [:index]
|
||||
before_filter :load_form_data, only: [:new, :edit]
|
||||
before_filter :strip_banned_attrs, only: [:update]
|
||||
@@ -12,7 +12,7 @@ module Admin
|
||||
def index
|
||||
respond_to do |format|
|
||||
format.html do
|
||||
if view_context.standing_orders_setup_complete?(@shops)
|
||||
if view_context.subscriptions_setup_complete?(@shops)
|
||||
@order_cycles = OrderCycle.joins(:schedules).managed_by(spree_current_user)
|
||||
@payment_methods = Spree::PaymentMethod.managed_by(spree_current_user)
|
||||
@shipping_methods = Spree::ShippingMethod.managed_by(spree_current_user)
|
||||
@@ -26,48 +26,48 @@ module Admin
|
||||
end
|
||||
|
||||
def new
|
||||
@standing_order.bill_address = Spree::Address.new
|
||||
@standing_order.ship_address = Spree::Address.new
|
||||
@subscription.bill_address = Spree::Address.new
|
||||
@subscription.ship_address = Spree::Address.new
|
||||
end
|
||||
|
||||
def create
|
||||
form = StandingOrderForm.new(@standing_order, params[:standing_order], fee_calculator)
|
||||
form = SubscriptionForm.new(@subscription, params[:subscription], fee_calculator)
|
||||
if form.save
|
||||
render_as_json @standing_order, fee_calculator: fee_calculator
|
||||
render_as_json @subscription, fee_calculator: fee_calculator
|
||||
else
|
||||
render json: { errors: form.json_errors }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
form = StandingOrderForm.new(@standing_order, params[:standing_order], fee_calculator)
|
||||
form = SubscriptionForm.new(@subscription, params[:subscription], fee_calculator)
|
||||
if form.save
|
||||
render_as_json @standing_order, fee_calculator: fee_calculator, order_update_issues: form.order_update_issues
|
||||
render_as_json @subscription, fee_calculator: fee_calculator, order_update_issues: form.order_update_issues
|
||||
else
|
||||
render json: { errors: form.json_errors }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def cancel
|
||||
@standing_order.cancel(@open_orders_to_keep || [])
|
||||
@subscription.cancel(@open_orders_to_keep || [])
|
||||
|
||||
respond_with(@standing_order) do |format|
|
||||
format.json { render_as_json @standing_order, fee_calculator: fee_calculator }
|
||||
respond_with(@subscription) do |format|
|
||||
format.json { render_as_json @subscription, fee_calculator: fee_calculator }
|
||||
end
|
||||
end
|
||||
|
||||
def pause
|
||||
unless params[:open_orders] == 'keep'
|
||||
@standing_order.proxy_orders.placed_and_open.each(&:cancel)
|
||||
@subscription.proxy_orders.placed_and_open.each(&:cancel)
|
||||
end
|
||||
|
||||
@standing_order.update_attributes(paused_at: Time.zone.now)
|
||||
render_as_json @standing_order, fee_calculator: fee_calculator
|
||||
@subscription.update_attributes(paused_at: Time.zone.now)
|
||||
render_as_json @subscription, fee_calculator: fee_calculator
|
||||
end
|
||||
|
||||
def unpause
|
||||
@standing_order.update_attributes(paused_at: nil)
|
||||
render_as_json @standing_order, fee_calculator: fee_calculator
|
||||
@subscription.update_attributes(paused_at: nil)
|
||||
render_as_json @subscription, fee_calculator: fee_calculator
|
||||
end
|
||||
|
||||
private
|
||||
@@ -79,68 +79,68 @@ module Admin
|
||||
|
||||
def collection
|
||||
if request.format.json?
|
||||
permissions.editable_standing_orders.ransack(params[:q]).result
|
||||
permissions.editable_subscriptions.ransack(params[:q]).result
|
||||
.preload([:shop, :customer, :schedule, :standing_line_items, :ship_address, :bill_address, proxy_orders: { order: :order_cycle }])
|
||||
else
|
||||
StandingOrder.where("1=0")
|
||||
Subscription.where("1=0")
|
||||
end
|
||||
end
|
||||
|
||||
def load_shops
|
||||
@shops = Enterprise.managed_by(spree_current_user).is_distributor.where(enable_standing_orders: true)
|
||||
@shops = Enterprise.managed_by(spree_current_user).is_distributor.where(enable_subscriptions: true)
|
||||
end
|
||||
|
||||
def load_form_data
|
||||
@customers = Customer.of(@standing_order.shop)
|
||||
@schedules = Schedule.with_coordinator(@standing_order.shop)
|
||||
@payment_methods = Spree::PaymentMethod.for_distributor(@standing_order.shop).for_standing_orders
|
||||
@shipping_methods = Spree::ShippingMethod.for_distributor(@standing_order.shop)
|
||||
@customers = Customer.of(@subscription.shop)
|
||||
@schedules = Schedule.with_coordinator(@subscription.shop)
|
||||
@payment_methods = Spree::PaymentMethod.for_distributor(@subscription.shop).for_subscriptions
|
||||
@shipping_methods = Spree::ShippingMethod.for_distributor(@subscription.shop)
|
||||
@order_cycles = OrderCycle.joins(:schedules).managed_by(spree_current_user)
|
||||
@fee_calculator = fee_calculator
|
||||
end
|
||||
|
||||
def fee_calculator
|
||||
shop, next_oc = @standing_order.shop, @standing_order.schedule.andand.current_or_next_order_cycle
|
||||
shop, next_oc = @subscription.shop, @subscription.schedule.andand.current_or_next_order_cycle
|
||||
return nil unless shop && next_oc
|
||||
OpenFoodNetwork::EnterpriseFeeCalculator.new(shop, next_oc)
|
||||
end
|
||||
|
||||
# Wrap :standing_line_items_attributes in :standing_order root
|
||||
# Wrap :standing_line_items_attributes in :subscription root
|
||||
def wrap_nested_attrs
|
||||
if params[:standing_line_items].is_a? Array
|
||||
attributes = params[:standing_line_items].map do |sli|
|
||||
sli.slice(*StandingLineItem.attribute_names + ["_destroy"])
|
||||
end
|
||||
params[:standing_order][:standing_line_items_attributes] = attributes
|
||||
params[:subscription][:standing_line_items_attributes] = attributes
|
||||
end
|
||||
wrap_bill_address_attrs if params[:bill_address]
|
||||
wrap_ship_address_attrs if params[:ship_address]
|
||||
end
|
||||
|
||||
def wrap_bill_address_attrs
|
||||
params[:standing_order][:bill_address_attributes] = params[:bill_address].slice(*Spree::Address.attribute_names)
|
||||
params[:subscription][:bill_address_attributes] = params[:bill_address].slice(*Spree::Address.attribute_names)
|
||||
end
|
||||
|
||||
def wrap_ship_address_attrs
|
||||
params[:standing_order][:ship_address_attributes] = params[:ship_address].slice(*Spree::Address.attribute_names)
|
||||
params[:subscription][:ship_address_attributes] = params[:ship_address].slice(*Spree::Address.attribute_names)
|
||||
end
|
||||
|
||||
def check_for_open_orders
|
||||
return if params[:open_orders] == 'cancel'
|
||||
@open_orders_to_keep = @standing_order.proxy_orders.placed_and_open.pluck(:id)
|
||||
@open_orders_to_keep = @subscription.proxy_orders.placed_and_open.pluck(:id)
|
||||
return if @open_orders_to_keep.empty? || params[:open_orders] == 'keep'
|
||||
render json: { errors: { open_orders: t('admin.standing_orders.confirm_cancel_open_orders_msg') } }, status: :conflict
|
||||
render json: { errors: { open_orders: t('admin.subscriptions.confirm_cancel_open_orders_msg') } }, status: :conflict
|
||||
end
|
||||
|
||||
def strip_banned_attrs
|
||||
params[:standing_order].delete :schedule_id
|
||||
params[:standing_order].delete :customer_id
|
||||
params[:subscription].delete :schedule_id
|
||||
params[:subscription].delete :customer_id
|
||||
end
|
||||
|
||||
# Overriding Spree method to load data from params here so that
|
||||
# we can authorise #create using an object with required attributes
|
||||
def build_resource
|
||||
StandingOrder.new(params[:standing_order])
|
||||
Subscription.new(params[:subscription])
|
||||
end
|
||||
|
||||
def ams_prefix_whitelist
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
module Admin
|
||||
module StandingOrdersHelper
|
||||
def standing_orders_setup_complete?(shops)
|
||||
module SubscriptionsHelper
|
||||
def subscriptions_setup_complete?(shops)
|
||||
return false unless shops.any?
|
||||
shops = shops.select{ |shop| shipping_and_payment_methods_ok?(shop) && customers_ok?(shop) }
|
||||
Schedule.joins(:order_cycles).where(order_cycles: { coordinator_id: shops }).any?
|
||||
end
|
||||
|
||||
def shipping_and_payment_methods_ok?(shop)
|
||||
shop.present? && shop.shipping_methods.any? && shop.payment_methods.for_standing_orders.any?
|
||||
shop.present? && shop.shipping_methods.any? && shop.payment_methods.for_subscriptions.any?
|
||||
end
|
||||
|
||||
def customers_ok?(shop)
|
||||
|
||||
@@ -102,7 +102,7 @@ module EnterprisesHelper
|
||||
order_changes_allowed? && current_order.finalised_line_items.present?
|
||||
end
|
||||
|
||||
def standing_orders_enabled?
|
||||
spree_current_user.admin? || spree_current_user.enterprises.where(enable_standing_orders: true).any?
|
||||
def subscriptions_enabled?
|
||||
spree_current_user.admin? || spree_current_user.enterprises.where(enable_subscriptions: true).any?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require 'open_food_network/standing_order_payment_updater'
|
||||
require 'open_food_network/standing_order_summarizer'
|
||||
require 'open_food_network/subscription_payment_updater'
|
||||
require 'open_food_network/subscription_summarizer'
|
||||
|
||||
class StandingOrderConfirmJob
|
||||
class SubscriptionConfirmJob
|
||||
def perform
|
||||
ids = proxy_orders.pluck(:id)
|
||||
proxy_orders.update_all(confirmed_at: Time.zone.now)
|
||||
@@ -19,13 +19,13 @@ class StandingOrderConfirmJob
|
||||
delegate :record_and_log_error, :send_confirmation_summary_emails, to: :summarizer
|
||||
|
||||
def summarizer
|
||||
@summarizer ||= OpenFoodNetwork::StandingOrderSummarizer.new
|
||||
@summarizer ||= OpenFoodNetwork::SubscriptionSummarizer.new
|
||||
end
|
||||
|
||||
def proxy_orders
|
||||
ProxyOrder.not_canceled.where('confirmed_at IS NULL AND placed_at IS NOT NULL')
|
||||
.joins(:order_cycle).merge(recently_closed_order_cycles)
|
||||
.joins(:standing_order).merge(StandingOrder.not_canceled.not_paused)
|
||||
.joins(:subscription).merge(Subscription.not_canceled.not_paused)
|
||||
.joins(:order).merge(Spree::Order.complete)
|
||||
end
|
||||
|
||||
@@ -43,16 +43,16 @@ class StandingOrderConfirmJob
|
||||
end
|
||||
|
||||
def update_payment!
|
||||
OpenFoodNetwork::StandingOrderPaymentUpdater.new(@order).update!
|
||||
OpenFoodNetwork::SubscriptionPaymentUpdater.new(@order).update!
|
||||
end
|
||||
|
||||
def send_confirm_email
|
||||
record_success(@order)
|
||||
StandingOrderMailer.confirmation_email(@order).deliver
|
||||
SubscriptionMailer.confirmation_email(@order).deliver
|
||||
end
|
||||
|
||||
def send_failed_payment_email
|
||||
record_and_log_error(:failed_payment, @order)
|
||||
StandingOrderMailer.failed_payment_email(@order).deliver
|
||||
SubscriptionMailer.failed_payment_email(@order).deliver
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
require 'open_food_network/standing_order_summarizer'
|
||||
require 'open_food_network/subscription_summarizer'
|
||||
|
||||
class StandingOrderPlacementJob
|
||||
class SubscriptionPlacementJob
|
||||
def perform
|
||||
ids = proxy_orders.pluck(:id)
|
||||
proxy_orders.update_all(placed_at: Time.zone.now)
|
||||
@@ -18,14 +18,14 @@ class StandingOrderPlacementJob
|
||||
delegate :record_and_log_error, :send_placement_summary_emails, to: :summarizer
|
||||
|
||||
def summarizer
|
||||
@summarizer ||= OpenFoodNetwork::StandingOrderSummarizer.new
|
||||
@summarizer ||= OpenFoodNetwork::SubscriptionSummarizer.new
|
||||
end
|
||||
|
||||
def proxy_orders
|
||||
# Loads proxy orders for open order cycles that have not been placed yet
|
||||
ProxyOrder.not_canceled.where(placed_at: nil)
|
||||
.joins(:order_cycle).merge(OrderCycle.active)
|
||||
.joins(:standing_order).merge(StandingOrder.not_canceled.not_paused)
|
||||
.joins(:subscription).merge(Subscription.not_canceled.not_paused)
|
||||
end
|
||||
|
||||
def process(order)
|
||||
@@ -71,11 +71,11 @@ class StandingOrderPlacementJob
|
||||
def send_placement_email(order, changes)
|
||||
record_issue(:changes, order) if changes.present?
|
||||
record_success(order) if changes.blank?
|
||||
StandingOrderMailer.placement_email(order, changes).deliver
|
||||
SubscriptionMailer.placement_email(order, changes).deliver
|
||||
end
|
||||
|
||||
def send_empty_email(order, changes)
|
||||
record_issue(:empty, order)
|
||||
StandingOrderMailer.empty_email(order, changes).deliver
|
||||
SubscriptionMailer.empty_email(order, changes).deliver
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class StandingOrderMailer < Spree::BaseMailer
|
||||
class SubscriptionMailer < Spree::BaseMailer
|
||||
helper CheckoutHelper
|
||||
|
||||
def confirmation_email(order)
|
||||
@@ -31,7 +31,7 @@ class StandingOrderMailer < Spree::BaseMailer
|
||||
@summary = summary
|
||||
mail(to: @shop.email,
|
||||
from: from_address,
|
||||
subject: "#{Spree::Config[:site_name]} #{t('standing_order_mailer.placement_summary_email.subject')}")
|
||||
subject: "#{Spree::Config[:site_name]} #{t('subscription_mailer.placement_summary_email.subject')}")
|
||||
end
|
||||
|
||||
def confirmation_summary_email(summary)
|
||||
@@ -39,7 +39,7 @@ class StandingOrderMailer < Spree::BaseMailer
|
||||
@summary = summary
|
||||
mail(to: @shop.email,
|
||||
from: from_address,
|
||||
subject: "#{Spree::Config[:site_name]} #{t('standing_order_mailer.confirmation_summary_email.subject')}")
|
||||
subject: "#{Spree::Config[:site_name]} #{t('subscription_mailer.confirmation_summary_email.subject')}")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -48,6 +48,6 @@ class ColumnPreference < ActiveRecord::Base
|
||||
# Arbitrary filtering of default_preferences
|
||||
def self.filter(default_preferences, user, action_name)
|
||||
return unless action_name == 'order_cycles_index'
|
||||
default_preferences.delete(:schedules) unless user.admin? || user.enterprises.where(enable_standing_orders: true).any?
|
||||
default_preferences.delete(:schedules) unless user.admin? || user.enterprises.where(enable_subscriptions: true).any?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class ProxyOrder < ActiveRecord::Base
|
||||
belongs_to :order, class_name: 'Spree::Order', dependent: :destroy
|
||||
belongs_to :standing_order
|
||||
belongs_to :subscription
|
||||
belongs_to :order_cycle
|
||||
|
||||
delegate :number, :completed_at, :total, to: :order, allow_nil: true
|
||||
@@ -43,19 +43,19 @@ class ProxyOrder < ActiveRecord::Base
|
||||
def initialise_order!
|
||||
return order if order.present?
|
||||
create_order!(
|
||||
customer_id: standing_order.customer_id,
|
||||
email: standing_order.customer.email,
|
||||
customer_id: subscription.customer_id,
|
||||
email: subscription.customer.email,
|
||||
order_cycle_id: order_cycle_id,
|
||||
distributor_id: standing_order.shop_id,
|
||||
shipping_method_id: standing_order.shipping_method_id
|
||||
distributor_id: subscription.shop_id,
|
||||
shipping_method_id: subscription.shipping_method_id
|
||||
)
|
||||
order.update_attribute(:user, standing_order.customer.user)
|
||||
standing_order.standing_line_items.each do |sli|
|
||||
order.update_attribute(:user, subscription.customer.user)
|
||||
subscription.standing_line_items.each do |sli|
|
||||
order.line_items.build(variant_id: sli.variant_id, quantity: sli.quantity, skip_stock_check: true)
|
||||
end
|
||||
order.update_attributes(bill_address: standing_order.bill_address.dup, ship_address: standing_order.ship_address.dup)
|
||||
order.update_attributes(bill_address: subscription.bill_address.dup, ship_address: subscription.ship_address.dup)
|
||||
order.update_distribution_charge!
|
||||
order.payments.create(payment_method_id: standing_order.payment_method_id, amount: order.reload.total)
|
||||
order.payments.create(payment_method_id: subscription.payment_method_id, amount: order.reload.total)
|
||||
|
||||
save!
|
||||
order
|
||||
@@ -64,7 +64,7 @@ class ProxyOrder < ActiveRecord::Base
|
||||
private
|
||||
|
||||
def paused?
|
||||
pending? && standing_order.paused?
|
||||
pending? && subscription.paused?
|
||||
end
|
||||
|
||||
def pending?
|
||||
|
||||
@@ -252,16 +252,16 @@ class AbilityDecorator
|
||||
|
||||
can [:create], Customer
|
||||
can [:admin, :index, :update, :destroy, :addresses, :cards], Customer, enterprise_id: Enterprise.managed_by(user).pluck(:id)
|
||||
can [:admin, :new, :index], StandingOrder
|
||||
can [:create, :edit, :update, :cancel, :pause, :unpause], StandingOrder do |standing_order|
|
||||
user.enterprises.include?(standing_order.shop)
|
||||
can [:admin, :new, :index], Subscription
|
||||
can [:create, :edit, :update, :cancel, :pause, :unpause], Subscription do |subscription|
|
||||
user.enterprises.include?(subscription.shop)
|
||||
end
|
||||
can [:admin, :build], StandingLineItem
|
||||
can [:destroy], StandingLineItem do |standing_line_item|
|
||||
user.enterprises.include?(standing_line_item.standing_order.shop)
|
||||
user.enterprises.include?(standing_line_item.subscription.shop)
|
||||
end
|
||||
can [:admin, :edit, :cancel, :resume], ProxyOrder do |proxy_order|
|
||||
user.enterprises.include?(proxy_order.standing_order.shop)
|
||||
user.enterprises.include?(proxy_order.subscription.shop)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ Spree::Order.class_eval do
|
||||
belongs_to :cart
|
||||
belongs_to :customer
|
||||
has_one :proxy_order
|
||||
has_one :standing_order, through: :proxy_order
|
||||
has_one :subscription, through: :proxy_order
|
||||
|
||||
validates :customer, presence: true, if: :require_customer?
|
||||
validate :products_available_from_new_distribution, :if => lambda { distributor_id_changed? || order_cycle_id_changed? }
|
||||
@@ -293,7 +293,7 @@ Spree::Order.class_eval do
|
||||
# Overrride of Spree method, that allows us to send separate confirmation emails to user and shop owners
|
||||
# And separately, to skip sending confirmation email completely for user invoice orders
|
||||
def deliver_order_confirmation_email
|
||||
unless account_invoice? || standing_order.present?
|
||||
unless account_invoice? || subscription.present?
|
||||
Delayed::Job.enqueue ConfirmOrderJob.new(id)
|
||||
end
|
||||
end
|
||||
@@ -320,10 +320,10 @@ Spree::Order.class_eval do
|
||||
errors.add(:base, e.message) and return result
|
||||
end
|
||||
|
||||
# Override or Spree method. Used to prevent payments on standing orders from being processed in the normal way.
|
||||
# Override or Spree method. Used to prevent payments on subscriptions from being processed in the normal way.
|
||||
# ie. they are 'hidden' from processing logic until after the order cycle has closed.
|
||||
def pending_payments
|
||||
return [] if standing_order.present? && order_cycle.orders_close_at.andand > Time.zone.now
|
||||
return [] if subscription.present? && order_cycle.orders_close_at.andand > Time.zone.now
|
||||
payments.select {|p| p.state == "checkout"} # Original definition
|
||||
end
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ Spree::PaymentMethod.class_eval do
|
||||
where('enterprises.id = ?', distributor)
|
||||
}
|
||||
|
||||
scope :for_standing_orders, where(type: StandingOrder::ALLOWED_PAYMENT_METHOD_TYPES)
|
||||
scope :for_subscriptions, where(type: Subscription::ALLOWED_PAYMENT_METHOD_TYPES)
|
||||
|
||||
scope :by_name, order('spree_payment_methods.name ASC')
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
class StandingLineItem < ActiveRecord::Base
|
||||
belongs_to :standing_order, inverse_of: :standing_line_items
|
||||
belongs_to :subscription, inverse_of: :standing_line_items
|
||||
belongs_to :variant, class_name: 'Spree::Variant'
|
||||
|
||||
validates :standing_order, presence: true
|
||||
validates :subscription, presence: true
|
||||
validates :variant, presence: true
|
||||
validates :quantity, presence: true, numericality: { only_integer: true }
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class StandingOrder < ActiveRecord::Base
|
||||
class Subscription < ActiveRecord::Base
|
||||
ALLOWED_PAYMENT_METHOD_TYPES = ["Spree::PaymentMethod::Check", "Spree::Gateway::StripeConnect"].freeze
|
||||
|
||||
belongs_to :shop, class_name: 'Enterprise'
|
||||
@@ -9,7 +9,7 @@ class StandingOrder < ActiveRecord::Base
|
||||
belongs_to :bill_address, foreign_key: :bill_address_id, class_name: Spree::Address
|
||||
belongs_to :ship_address, foreign_key: :ship_address_id, class_name: Spree::Address
|
||||
belongs_to :credit_card, foreign_key: :credit_card_id, class_name: 'Spree::CreditCard'
|
||||
has_many :standing_line_items, inverse_of: :standing_order
|
||||
has_many :standing_line_items, inverse_of: :subscription
|
||||
has_many :order_cycles, through: :schedule
|
||||
has_many :proxy_orders
|
||||
has_many :orders, through: :proxy_orders
|
||||
@@ -20,10 +20,10 @@ class StandingOrder < ActiveRecord::Base
|
||||
accepts_nested_attributes_for :standing_line_items, allow_destroy: true
|
||||
accepts_nested_attributes_for :bill_address, :ship_address
|
||||
|
||||
scope :not_ended, -> { where('standing_orders.ends_at > (?) OR standing_orders.ends_at IS NULL', Time.zone.now) }
|
||||
scope :not_canceled, -> { where('standing_orders.canceled_at IS NULL') }
|
||||
scope :not_paused, -> { where('standing_orders.paused_at IS NULL') }
|
||||
scope :active, -> { not_canceled.not_ended.not_paused.where('standing_orders.begins_at <= (?)', Time.zone.now) }
|
||||
scope :not_ended, -> { where('subscriptions.ends_at > (?) OR subscriptions.ends_at IS NULL', Time.zone.now) }
|
||||
scope :not_canceled, -> { where('subscriptions.canceled_at IS NULL') }
|
||||
scope :not_paused, -> { where('subscriptions.paused_at IS NULL') }
|
||||
scope :active, -> { not_canceled.not_ended.not_paused.where('subscriptions.begins_at <= (?)', Time.zone.now) }
|
||||
|
||||
def closed_proxy_orders
|
||||
proxy_orders.closed
|
||||
|
||||
@@ -8,7 +8,7 @@ module Api
|
||||
if object.total.present?
|
||||
object.total.to_money.to_s
|
||||
else
|
||||
object.standing_order.standing_line_items.sum(&:total_estimate)
|
||||
object.subscription.standing_line_items.sum(&:total_estimate)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module Api
|
||||
module Admin
|
||||
class StandingOrderSerializer < ActiveModel::Serializer
|
||||
class SubscriptionSerializer < ActiveModel::Serializer
|
||||
attributes :id, :shop_id, :customer_id, :schedule_id, :payment_method_id, :shipping_method_id, :begins_at, :ends_at
|
||||
attributes :customer_email, :schedule_name, :edit_path, :canceled_at, :paused_at, :state, :credit_card_id
|
||||
|
||||
@@ -36,7 +36,7 @@ module Api
|
||||
|
||||
def edit_path
|
||||
return '' unless object.id
|
||||
edit_admin_standing_order_path(object)
|
||||
edit_admin_subscription_path(object)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
# Responsible for keeping line items on initialised orders for a standing order in sync with
|
||||
# the standing line items on that standing order.
|
||||
# Responsible for keeping line items on initialised orders for a subscription in sync with
|
||||
# the standing line items on that subscription.
|
||||
|
||||
class LineItemSyncer
|
||||
def initialize(standing_order, order_update_issues)
|
||||
@standing_order = standing_order
|
||||
def initialize(subscription, order_update_issues)
|
||||
@subscription = subscription
|
||||
@order_update_issues = order_update_issues
|
||||
end
|
||||
|
||||
@@ -15,9 +15,9 @@ class LineItemSyncer
|
||||
|
||||
private
|
||||
|
||||
delegate :standing_line_items, to: :standing_order
|
||||
delegate :standing_line_items, to: :subscription
|
||||
|
||||
attr_reader :standing_order, :order_update_issues
|
||||
attr_reader :subscription, :order_update_issues
|
||||
|
||||
def update_item_quantities(order)
|
||||
changed_standing_line_items.each do |sli|
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
class OrderSyncer
|
||||
attr_reader :order_update_issues
|
||||
|
||||
def initialize(standing_order)
|
||||
@standing_order = standing_order
|
||||
def initialize(subscription)
|
||||
@subscription = subscription
|
||||
@order_update_issues = OrderUpdateIssues.new
|
||||
@line_item_syncer = LineItemSyncer.new(standing_order, order_update_issues)
|
||||
@line_item_syncer = LineItemSyncer.new(subscription, order_update_issues)
|
||||
end
|
||||
|
||||
def sync!
|
||||
@@ -21,13 +21,13 @@ class OrderSyncer
|
||||
|
||||
private
|
||||
|
||||
attr_reader :standing_order, :line_item_syncer
|
||||
attr_reader :subscription, :line_item_syncer
|
||||
|
||||
delegate :orders, :bill_address, :ship_address, :standing_line_items, to: :standing_order
|
||||
delegate :shop_id, :customer, :customer_id, to: :standing_order
|
||||
delegate :shipping_method, :shipping_method_id, :payment_method, :payment_method_id, to: :standing_order
|
||||
delegate :shipping_method_id_changed?, :shipping_method_id_was, to: :standing_order
|
||||
delegate :payment_method_id_changed?, :payment_method_id_was, to: :standing_order
|
||||
delegate :orders, :bill_address, :ship_address, :standing_line_items, to: :subscription
|
||||
delegate :shop_id, :customer, :customer_id, to: :subscription
|
||||
delegate :shipping_method, :shipping_method_id, :payment_method, :payment_method_id, to: :subscription
|
||||
delegate :shipping_method_id_changed?, :shipping_method_id_was, to: :subscription
|
||||
delegate :payment_method_id_changed?, :payment_method_id_was, to: :subscription
|
||||
|
||||
def update_associations_for(order)
|
||||
update_bill_address_for(order) if (bill_address.changes.keys & relevant_address_attrs).any?
|
||||
@@ -81,10 +81,10 @@ class OrderSyncer
|
||||
["firstname", "lastname", "address1", "zipcode", "city", "state_id", "country_id", "phone"]
|
||||
end
|
||||
|
||||
def addresses_match?(order_address, standing_order_address)
|
||||
def addresses_match?(order_address, subscription_address)
|
||||
relevant_address_attrs.all? do |attr|
|
||||
order_address[attr] == standing_order_address.send("#{attr}_was") ||
|
||||
order_address[attr] == standing_order_address[attr]
|
||||
order_address[attr] == subscription_address.send("#{attr}_was") ||
|
||||
order_address[attr] == subscription_address[attr]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Wrapper for a hash of issues encountered by instances of OrderSyncer and LineItemSyncer
|
||||
# Used to report issues to the user when they attempt to update a standing order
|
||||
# Used to report issues to the user when they attempt to update a subscription
|
||||
|
||||
class OrderUpdateIssues
|
||||
def initialize
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
require 'open_food_network/proxy_order_syncer'
|
||||
|
||||
class StandingOrderForm
|
||||
attr_accessor :standing_order, :params, :fee_calculator, :order_update_issues, :validator, :order_syncer
|
||||
class SubscriptionForm
|
||||
attr_accessor :subscription, :params, :fee_calculator, :order_update_issues, :validator, :order_syncer
|
||||
|
||||
delegate :json_errors, :valid?, to: :validator
|
||||
delegate :order_update_issues, to: :order_syncer
|
||||
|
||||
def initialize(standing_order, params = {}, fee_calculator = nil)
|
||||
@standing_order = standing_order
|
||||
def initialize(subscription, params = {}, fee_calculator = nil)
|
||||
@subscription = subscription
|
||||
@params = params
|
||||
@fee_calculator = fee_calculator
|
||||
@validator = StandingOrderValidator.new(standing_order)
|
||||
@order_syncer = OrderSyncer.new(standing_order)
|
||||
@validator = SubscriptionValidator.new(subscription)
|
||||
@order_syncer = OrderSyncer.new(subscription)
|
||||
end
|
||||
|
||||
def save
|
||||
validate_price_estimates
|
||||
standing_order.assign_attributes(params)
|
||||
subscription.assign_attributes(params)
|
||||
return false unless valid?
|
||||
standing_order.transaction do
|
||||
subscription.transaction do
|
||||
proxy_order_syncer.sync!
|
||||
order_syncer.sync!
|
||||
standing_order.save!
|
||||
subscription.save!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def proxy_order_syncer
|
||||
OpenFoodNetwork::ProxyOrderSyncer.new(standing_order)
|
||||
OpenFoodNetwork::ProxyOrderSyncer.new(subscription)
|
||||
end
|
||||
|
||||
def validate_price_estimates
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
# Encapsulation of all of the validation logic required for standing orders
|
||||
# Encapsulation of all of the validation logic required for subscriptions
|
||||
# Public interface consists of #valid? method provided by ActiveModel::Validations
|
||||
# and #json_errors which compiles a serializable hash of errors
|
||||
|
||||
class StandingOrderValidator
|
||||
class SubscriptionValidator
|
||||
include ActiveModel::Naming
|
||||
include ActiveModel::Conversion
|
||||
include ActiveModel::Validations
|
||||
|
||||
attr_reader :standing_order
|
||||
attr_reader :subscription
|
||||
|
||||
validates_presence_of :shop, :customer, :schedule, :shipping_method, :payment_method
|
||||
validates_presence_of :bill_address, :ship_address, :begins_at
|
||||
@@ -21,13 +21,13 @@ class StandingOrderValidator
|
||||
validate :standing_line_items_present?
|
||||
validate :requested_variants_available?
|
||||
|
||||
delegate :shop, :customer, :schedule, :shipping_method, :payment_method, to: :standing_order
|
||||
delegate :bill_address, :ship_address, :begins_at, :ends_at, to: :standing_order
|
||||
delegate :credit_card, :credit_card_id, to: :standing_order
|
||||
delegate :standing_line_items, to: :standing_order
|
||||
delegate :shop, :customer, :schedule, :shipping_method, :payment_method, to: :subscription
|
||||
delegate :bill_address, :ship_address, :begins_at, :ends_at, to: :subscription
|
||||
delegate :credit_card, :credit_card_id, to: :subscription
|
||||
delegate :standing_line_items, to: :subscription
|
||||
|
||||
def initialize(standing_order)
|
||||
@standing_order = standing_order
|
||||
def initialize(subscription)
|
||||
@subscription = subscription
|
||||
end
|
||||
|
||||
def json_errors
|
||||
@@ -52,7 +52,7 @@ class StandingOrderValidator
|
||||
|
||||
def payment_method_type_allowed?
|
||||
return unless payment_method
|
||||
return if StandingOrder::ALLOWED_PAYMENT_METHOD_TYPES.include? payment_method.type
|
||||
return if Subscription::ALLOWED_PAYMENT_METHOD_TYPES.include? payment_method.type
|
||||
errors.add(:payment_method, :invalid_type)
|
||||
end
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
- if @changes && @changes[item.id].present?
|
||||
%del.quantity_was= @changes[item.id]
|
||||
= item.quantity
|
||||
-# Report changes made to standing orders
|
||||
-# Report changes made to subscriptions
|
||||
|
||||
%td{:align => "right"}
|
||||
= item.display_amount_with_adjustments
|
||||
|
||||
Reference in New Issue
Block a user