Module: CustomerShipping
- Extended by:
- ActiveSupport::Concern
- Included in:
- Customer
- Defined in:
- app/models/concerns/customer_shipping.rb
Overview
Shipping stats, carriers, Canadian Tire helpers, shipment methods.
Instance Method Summary collapse
- #bill_shipping_to_account_number_options_for_select(carrier, origin_address = nil) ⇒ Object
- #cached_active_shipping_account_numbers(country_iso3) ⇒ Object
- #default_shipping_option_name ⇒ Object
- #default_shipping_options(shipping_address_to_use = nil) ⇒ Object
- #distance_from_us_warehouse ⇒ Object
- #find_or_create_canadian_tire_address_from_store_number(store_number) ⇒ Object
- #jde_abn ⇒ Object
- #jde_b_ab ⇒ Object
- #jde_s_ab ⇒ Object
- #largest_shipping_cost_in_last_year ⇒ Object
- #last_invoice_shipping_cost ⇒ Object
-
#shipping_account_number(carrier, billing_address = nil, destination_address = nil) ⇒ Object
:reek:ControlParameter.
- #shipping_account_numbers_for_carrier(carrier, billing_address = nil, destination_address = nil) ⇒ Object
- #shipping_amount_last_30_days ⇒ Object
- #shipping_amount_last_month ⇒ Object
- #spent_20_on_shipping_in_last_year? ⇒ Boolean
Instance Method Details
#bill_shipping_to_account_number_options_for_select(carrier, origin_address = nil) ⇒ Object
80 81 82 83 84 85 86 |
# File 'app/models/concerns/customer_shipping.rb', line 80 def (carrier, origin_address = nil) return [] unless origin_address.present? && origin_address.supports_third_party_shipping all_acct_numbers = cached_active_shipping_account_numbers(origin_address.country_iso3) shipping_acct_numbers = all_acct_numbers.select { |sn| sn.carrier == carrier || sn.carrier.nil? } shipping_acct_numbers.uniq.map { |sn| ["#{sn.customer.name}: #{sn.carrier} #{sn.account_number}", sn.id] } end |
#cached_active_shipping_account_numbers(country_iso3) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'app/models/concerns/customer_shipping.rb', line 88 def cached_active_shipping_account_numbers(country_iso3) @cached_shipping_account_numbers ||= {} @cached_shipping_account_numbers[country_iso3] ||= begin acct_numbers = shipping_account_numbers.active.includes(:customer).where(country_iso3: country_iso3).to_a acct_numbers += parent_organization.shipping_account_numbers.active.includes(:customer).where(country_iso3: country_iso3).to_a if parent_organization acct_numbers end end |
#default_shipping_option_name ⇒ Object
62 63 64 65 66 67 |
# File 'app/models/concerns/customer_shipping.rb', line 62 def default_shipping_option_name res = preferred_shipping_method res = parent_organization.preferred_shipping_method if res.blank? && parent_organization.present? res = DEFAULT_SHIPPING_METHOD_NAME[store.country.iso3.to_sym] if res.blank? res end |
#default_shipping_options(shipping_address_to_use = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'app/models/concerns/customer_shipping.rb', line 52 def (shipping_address_to_use = nil) opts = ShippingOption.active .where(country: store.country.iso) .where.not(name: %w[legacy override]) .where(is_third_party_only: [false, nil]) .sort_by { |so| [-1.0 * so.days_commitment, so.description] }.to_a (opts, shipping_address_to_use) opts.sort_by { |opt| opt[:description] } end |
#distance_from_us_warehouse ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'app/models/concerns/customer_shipping.rb', line 9 def distance_from_us_warehouse return nil if billing_address.nil? us_warehouse_address = Store.find(1).warehouse_address distance = billing_address.distance_from(us_warehouse_address.lat, us_warehouse_address.lng) return nil if distance.nil? distance < 1 ? 1 : distance.round end |
#find_or_create_canadian_tire_address_from_store_number(store_number) ⇒ Object
97 98 99 100 101 |
# File 'app/models/concerns/customer_shipping.rb', line 97 def find_or_create_canadian_tire_address_from_store_number(store_number) return nil unless canadian_tire? find_existing_canadian_tire_address(store_number) || build_canadian_tire_address_from_store(store_number) end |
#jde_abn ⇒ Object
40 41 42 |
# File 'app/models/concerns/customer_shipping.rb', line 40 def jde_abn shipping_address&.jde_number end |
#jde_b_ab ⇒ Object
48 49 50 |
# File 'app/models/concerns/customer_shipping.rb', line 48 def jde_b_ab billing_address&.jde_number end |
#jde_s_ab ⇒ Object
44 45 46 |
# File 'app/models/concerns/customer_shipping.rb', line 44 def jde_s_ab shipping_address&.jde_number end |
#largest_shipping_cost_in_last_year ⇒ Object
32 33 34 |
# File 'app/models/concerns/customer_shipping.rb', line 32 def largest_shipping_cost_in_last_year invoices.where(shipped_date: (Date.current - 1.year)..).maximum(:shipping_cost) || 0 end |
#last_invoice_shipping_cost ⇒ Object
27 28 29 30 |
# File 'app/models/concerns/customer_shipping.rb', line 27 def last_invoice_shipping_cost row = invoices.order(shipped_date: :desc).pick(:shipping_cost) row.nil? ? 0 : row end |
#shipping_account_number(carrier, billing_address = nil, destination_address = nil) ⇒ Object
:reek:ControlParameter
69 70 71 |
# File 'app/models/concerns/customer_shipping.rb', line 69 def shipping_account_number(carrier, billing_address = nil, destination_address = nil) # :reek:ControlParameter shipping_account_numbers_for_carrier(carrier, billing_address, destination_address).first end |
#shipping_account_numbers_for_carrier(carrier, billing_address = nil, destination_address = nil) ⇒ Object
73 74 75 76 77 78 |
# File 'app/models/concerns/customer_shipping.rb', line 73 def shipping_account_numbers_for_carrier(carrier, billing_address = nil, destination_address = nil) ids = shipping_account_party_ids_for(billing_address) res = ShippingAccountNumber.active.where(customer_id: ids).where(carrier:) res = res.where(country_iso3: destination_address.country_iso3) if destination_address.present? res end |
#shipping_amount_last_30_days ⇒ Object
23 24 25 |
# File 'app/models/concerns/customer_shipping.rb', line 23 def shipping_amount_last_30_days invoices.where('shipped_date between ? and ?', Date.current - 30, Date.current).sum(:shipping_cost) end |
#shipping_amount_last_month ⇒ Object
19 20 21 |
# File 'app/models/concerns/customer_shipping.rb', line 19 def shipping_amount_last_month invoices.where('shipped_date between ? and ?', Date.current.last_month.beginning_of_month, Date.current.last_month.end_of_month).sum(:shipping_cost) end |
#spent_20_on_shipping_in_last_year? ⇒ Boolean
36 37 38 |
# File 'app/models/concerns/customer_shipping.rb', line 36 def spent_20_on_shipping_in_last_year? largest_shipping_cost_in_last_year >= 20 end |