Class: Edi::Amazon::JsonListingGenerator::Attributes::PurchasableOffer

Inherits:
BaseAttribute
  • Object
show all
Defined in:
app/services/edi/amazon/json_listing_generator/attributes/purchasable_offer.rb

Overview

Service object: purchasable offer.

Constant Summary

Constants inherited from BaseAttribute

BaseAttribute::RUBY_UNIT_TO_AMAZON_UNIT

Instance Attribute Summary

Attributes inherited from BaseAttribute

#amazon_schema, #attribute_name, #catalog_item, #enum_mapper, #fba, #item, #language_tag, #marketplace, #marketplace_country_iso, #marketplace_id, #value_attribute_name, #variation

Instance Method Summary collapse

Methods inherited from BaseAttribute

#fetch_unit, #fetch_value, #initialize, #map_unit, #value

Constructor Details

This class inherits a constructor from Edi::Amazon::JsonListingGenerator::Attributes::BaseAttribute

Instance Method Details

#buildObject



5
6
7
8
9
10
# File 'app/services/edi/amazon/json_listing_generator/attributes/purchasable_offer.rb', line 5

def build
  offers = []
  offers << build_standard_offer
  offers << build_business_offer if @marketplace.supports_business_offer?
  offers
end

#build_business_offerObject



47
48
49
50
51
52
53
54
55
# File 'app/services/edi/amazon/json_listing_generator/attributes/purchasable_offer.rb', line 47

def build_business_offer
  offer = {}
  offer[:currency] = @catalog_item.currency
  offer[:audience] = 'B2B'
  value_with_tax = @fba ? @catalog_item.amazon_business_price_with_tax_fba : @catalog_item.amazon_business_price_with_tax
  offer[:our_price] = [{ schedule: [{ value_with_tax: value_with_tax }] }]
  offer[:quantity_discount_plan] = build_quantity_discount_plan if @catalog_item.quantity_discount_price_type.present?
  offer
end

#build_quantity_discount_planObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/edi/amazon/json_listing_generator/attributes/purchasable_offer.rb', line 57

def build_quantity_discount_plan
  return if @catalog_item.quantity_discount_price_type.blank?

  levels = []
  (1..5).to_a.each do |threshold_index|
    price_discount = @catalog_item.send(:"quantity_#{threshold_index}_price_discount")
    lower_bound = @catalog_item.send(:"quantity_#{threshold_index}_lower_bound")
    next unless lower_bound.present? && price_discount.present?

    levels << { lower_bound: lower_bound, value: price_discount }
  end
  return if levels.blank?

  [{ schedule: [{ discount_type: @catalog_item.quantity_discount_price_type, levels: levels }] }]
end

#build_standard_offerObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/edi/amazon/json_listing_generator/attributes/purchasable_offer.rb', line 12

def build_standard_offer
  offer = {}
  offer[:currency] = @catalog_item.currency
  offer[:audience] = 'ALL'
  value_with_tax = @fba ? @catalog_item.amazon_price_with_tax_fba : @catalog_item.amazon_price_with_tax

  offer[:our_price] = [{ schedule: [{ value_with_tax: value_with_tax }] }]

  if @catalog_item.sale_price_in_effect?
    sale_start_date = @catalog_item.sale_price_effective_date&.beginning_of_day || Time.current.beginning_of_day
    sale_end_date = @catalog_item.sale_price_expiration_date&.end_of_day || 30.days.from_now.end_of_day
    sale_value_with_tax = @fba ? @catalog_item.amazon_sale_price_with_tax_fba : @catalog_item.amazon_sale_price_with_tax

    offer[:discounted_price] = [
      {
        schedule: [
          {
            start_at: sale_start_date.utc.iso8601,
            end_at: sale_end_date.utc.iso8601,
            value_with_tax: sale_value_with_tax
          }
        ]
      }
    ]
  end
  maximum_seller_allowed_price = @fba ? @catalog_item.amazon_maximum_seller_allowed_price_with_tax_fba : @catalog_item.amazon_maximum_seller_allowed_price_with_tax
  minimum_seller_allowed_price = @fba ? @catalog_item.amazon_minimum_seller_allowed_price_with_tax_fba : @catalog_item.amazon_minimum_seller_allowed_price_with_tax

  offer[:maximum_seller_allowed_price] = [{ schedule: [{ value_with_tax: maximum_seller_allowed_price }] }]
  # ensure we only send positive reasonable MAP price here, because we can get negative (through landed costs bug), so just skip unless positive and > 10% of the value_with_tax
  offer[:minimum_seller_allowed_price] = [{ schedule: [{ value_with_tax: minimum_seller_allowed_price }] }] if minimum_seller_allowed_price.positive? && minimum_seller_allowed_price > 0.1 * value_with_tax
  offer[:marketplace_id] = @marketplace_id
  offer
end