Class: Workarea::Shipping
- Inherits:
-
Object
- Object
- Workarea::Shipping
- 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
-
.find_by_order(order_id) ⇒ Shipping
Finds the first Shipping for the given order ID.
Instance Method Summary collapse
-
#adjust_pricing(options = {}) ⇒ self
Adds a price adjustment to the shipping service.
-
#apply_shipping_service(attrs = {}) ⇒ Boolean
Set but do not persist shipping service attributes on the shipping.
-
#base_price ⇒ Money?
Price of shipping service from carrier - without taxes or discounts.
-
#find_method_options(packages) ⇒ Array<ShippingOption>
Lookup available options for this shipping based on the subtotal passed in.
-
#name ⇒ String
For compatibility with admin features, models must respond to this method.
- #partial? ⇒ Boolean
-
#reset_adjusted_shipping_pricing ⇒ Array<PriceAdjustment>
Remove any PriceAdjustments on the shipping cost except the base shipping cost adjustment which was set with other shipping service info.
-
#set_address(attrs = {}) ⇒ self
Set shipping address on the order.
-
#set_shipping_service(attrs = {}) ⇒ Boolean
Set and persist shipping service attributes on the shipping.
-
#shippable? ⇒ Boolean
Whether this Shipping is shippable.
Methods included from DiscountIds
Methods included from ApplicationDocument
Methods included from Sidekiq::Callbacks
add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list
Methods included from Mongoid::Document
Class Method Details
.find_by_order(order_id) ⇒ Shipping
Finds the first Shipping for the given order ID.
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.
135 136 137 |
# File 'app/models/workarea/shipping.rb', line 135 def adjust_pricing( = {}) price_adjustments.build() 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.
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_price ⇒ Money?
Price of shipping service from carrier - without taxes or discounts.
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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/models/workarea/shipping.rb', line 67 def (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 |
#name ⇒ String
For compatibility with admin features, models must respond to this method
44 45 46 |
# File 'app/models/workarea/shipping.rb', line 44 def name order_id end |
#partial? ⇒ Boolean
57 58 59 |
# File 'app/models/workarea/shipping.rb', line 57 def partial? quantities.present? end |
#reset_adjusted_shipping_pricing ⇒ Array<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.
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.
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.
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.
53 54 55 |
# File 'app/models/workarea/shipping.rb', line 53 def shippable? valid? && address.present? && shipping_service.present? end |