Relevant DiscountOrder tag rules are applied to orders on update

This commit is contained in:
Rob Harrington
2016-03-16 18:00:16 +11:00
parent bf72864c2b
commit f902474591
9 changed files with 159 additions and 68 deletions

View File

@@ -0,0 +1,11 @@
angular.module("admin.enterprises").directive "invertNumber", ->
restrict: "A"
require: "ngModel"
link: (scope, element, attrs, ngModel) ->
ngModel.$parsers.push (viewValue) ->
return -parseInt(viewValue) unless isNaN(parseInt(viewValue))
viewValue
ngModel.$formatters.push (modelValue) ->
return -parseInt(modelValue) unless isNaN(parseInt(modelValue))
modelValue

View File

@@ -24,13 +24,16 @@
name: "enterprise[tag_rules_attributes][{{tagGroup.startIndex + $index}}][calculator_attributes][id]",
ng: { value: "rule.calculator.id" } }
%input{ type: "hidden",
name: "enterprise[tag_rules_attributes][{{tagGroup.startIndex + $index}}][calculator_attributes][preferred_flat_percent]",
ng: { value: "rule.calculator.preferred_flat_percent" } }
%span.text-normal {{ $index + 1 }}. Apply a discount of
%span.input-symbol.after
%span.text-normal %
%input.text-big{ type: "number",
id: "enterprise_tag_rules_attributes_{{tagGroup.startIndex + $index}}_calculator_attributes_preferred_flat_percent",
name: "enterprise[tag_rules_attributes][{{tagGroup.startIndex + $index}}][calculator_attributes][preferred_flat_percent]",
min: 0,
min: -100,
max: 100,
ng: { model: "rule.calculator.preferred_flat_percent" } }
ng: { model: "rule.calculator.preferred_flat_percent" }, 'invert-number' => true }
%span.text-normal to order subtotals

View File

@@ -17,7 +17,8 @@ Spree::Order.class_eval do
attr_accessible :order_cycle_id, :distributor_id
before_validation :shipping_address_from_distributor
before_validation :associate_customer, unless: :customer_is_valid?
before_validation :associate_customer, unless: :customer_id?
before_validation :ensure_customer, unless: :customer_is_valid?
checkout_flow do
go_to_state :address
@@ -179,6 +180,10 @@ Spree::Order.class_eval do
if order_cycle
OpenFoodNetwork::EnterpriseFeeCalculator.new.create_order_adjustments_for self
end
if distributor.present? && customer.present?
distributor.apply_tag_rules_to(self, customer: customer)
end
end
end
@@ -289,14 +294,18 @@ Spree::Order.class_eval do
customer.present? && customer.enterprise_id == distributor_id && customer.email == (user.andand.email || email)
end
def email_for_customer
user.andand.email || email
end
def associate_customer
email_for_customer = user.andand.email || email
existing_customer = Customer.of(distributor).find_by_email(email_for_customer)
if existing_customer
self.customer = existing_customer
else
new_customer = Customer.create(enterprise: distributor, email: email_for_customer, user: user)
self.customer = new_customer
return customer if customer.present?
self.customer = Customer.of(distributor).find_by_email(email_for_customer)
end
def ensure_customer
unless associate_customer
self.customer = Customer.create(enterprise: distributor, email: email_for_customer, user: user)
end
end
end

View File

@@ -31,6 +31,7 @@ class TagRule < ActiveRecord::Base
def customer_tags_match?
context_customer_tags = context.andand[:customer].andand.tag_list || []
( context_customer_tags & preferred_customer_tags.split(",") ).any?
preferred_tags = preferred_customer_tags.split(",")
( context_customer_tags & preferred_tags ).any?
end
end

View File

@@ -5,9 +5,7 @@ class TagRule::DiscountOrder < TagRule
# Warning: this should only EVER be called via TagRule#apply
def apply!
percentage = "%.2f" % (calculator.preferred_flat_percent * -1)
label = I18n.t("tag_rules.discount_order.label", percentage: percentage)
create_adjustment(label, subject, subject)
create_adjustment(I18n.t("tag_rules.discount_order.discount"), subject, subject)
end
def subject_class