Class: Workarea::Shipping

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument, DiscountIds
Defined in:
app/models/workarea/shipping.rb,
app/models/workarea/shipping/sku.rb,
app/models/workarea/shipping/rate.rb,
app/models/workarea/shipping/address.rb,
app/models/workarea/shipping/service.rb,
app/models/workarea/shipping/rate_lookup.rb,
app/models/workarea/shipping/location_query.rb,
app/models/workarea/shipping/service_selection.rb

Defined Under Namespace

Classes: Address, LocationQuery, Rate, RateLookup, Service, ServiceSelection, Sku

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DiscountIds

#discount_ids

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

assert_valid_config!, async, disable, enable, inline, #run_callbacks

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.find_by_order(order_id) ⇒ Shipping

Finds the first Shipping for the given order ID.

Parameters:

Returns:



36
37
38
# File 'app/models/workarea/shipping.rb', line 36

def self.find_by_order(order_id)
  where(order_id: order_id).desc(:created_at).first
end

Instance Method Details

#adjust_pricing(options = {}) ⇒ self

Adds a price adjustment to the shipping service. Does not persist.

Returns:

  • (self)


135
136
137
# File 'app/models/workarea/shipping.rb', line 135

def adjust_pricing(options = {})
  price_adjustments.build(options)
end

#apply_shipping_service(attrs = {}) ⇒ Boolean

Set but do not persist shipping service attributes on the shipping. Used when applying a shipping service to Workarea::Shipping to see if it qualifies for shipping discounts.

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/workarea/shipping.rb', line 111

def apply_shipping_service(attrs = {})
  build_shipping_service unless shipping_service

  shipping_service.attributes = attrs
                                 .with_indifferent_access
                                 .slice(*ServiceSelection.fields.keys)

  if attrs[:base_price].present?
    reset_shipping_pricing
    adjust_pricing(
      price: 'shipping',
      amount: attrs[:base_price],
      description: shipping_service.name,
      calculator: self.class.name,
      data: { 'tax_code' => attrs[:tax_code] }
    )
  end
end

#base_priceMoney?

Price of shipping service from carrier - without taxes or discounts.

Returns:



158
159
160
161
162
163
# File 'app/models/workarea/shipping.rb', line 158

def base_price
  price_adjustments.detect do |price_adjustment|
    price_adjustment.price == 'shipping' &&
      price_adjustment.calculator == self.class.name
  end.try(:amount)
end

#find_method_options(packages) ⇒ Array<ShippingOption>

Lookup available options for this shipping based on the subtotal passed in.

Parameters:

  • (Array<ActiveShipping::Package>)

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/workarea/shipping.rb', line 67

def find_method_options(packages)
  return [] if address.blank? || packages.blank?

  origin = ActiveShipping::Location.new(Workarea.config.shipping_origin)
  response = Workarea.config.gateways.shipping.find_rates(
    origin,
    address.to_active_shipping,
    packages
  )

  response.rates.sort_by(&:price).map do |rate|
    ShippingOption.from_rate_estimate(rate)
  end
end

#nameString

For compatibility with admin features, models must respond to this method

Returns:



44
45
46
# File 'app/models/workarea/shipping.rb', line 44

def name
  order_id
end

#partial?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/workarea/shipping.rb', line 57

def partial?
  quantities.present?
end

#reset_adjusted_shipping_pricingArray<PriceAdjustment>

Remove any PriceAdjustments on the shipping cost except the base shipping cost adjustment which was set with other shipping service info. This is used in the Pricing::Request when resetting shipping for pricing.

Returns:



146
147
148
149
150
151
152
# File 'app/models/workarea/shipping.rb', line 146

def reset_adjusted_shipping_pricing
  keepers = price_adjustments.select do |adjustment|
    adjustment.calculator == self.class.name
  end

  self.price_adjustments = keepers
end

#set_address(attrs = {}) ⇒ self

Set shipping address on the order.

Parameters:

  • attrs (Hash) (defaults to: {})

    the attributes of the shipping address

Returns:

  • (self)


89
90
91
92
93
94
# File 'app/models/workarea/shipping.rb', line 89

def set_address(attrs = {})
  build_address if address.blank?
  address.attributes = attrs
  save
  self
end

#set_shipping_service(attrs = {}) ⇒ Boolean

Set and persist shipping service attributes on the shipping.

Returns:

  • (Boolean)


100
101
102
103
# File 'app/models/workarea/shipping.rb', line 100

def set_shipping_service(attrs = {})
  apply_shipping_service(attrs)
  save
end

#shippable?Boolean

Whether this Workarea::Shipping is shippable. It is shippable if it has a valid shipping address and a valid shipping service.

Returns:

  • (Boolean)


53
54
55
# File 'app/models/workarea/shipping.rb', line 53

def shippable?
  valid? && address.present? && shipping_service.present?
end