Class: Workarea::GlobalE::Discount

Inherits:
Object
  • Object
show all
Defined in:
app/services/workarea/global_e/discount.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(discount, order:, price_adjustment:) ⇒ Discount

Returns a new instance of Discount.



6
7
8
9
10
# File 'app/services/workarea/global_e/discount.rb', line 6

def initialize(discount, order:, price_adjustment:)
  @discount = discount
  @order = order
  @price_adjustment = price_adjustment
end

Instance Attribute Details

#discountObject (readonly)

Returns the value of attribute discount.



4
5
6
# File 'app/services/workarea/global_e/discount.rb', line 4

def discount
  @discount
end

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'app/services/workarea/global_e/discount.rb', line 4

def order
  @order
end

#price_adjustmentObject (readonly)

Returns the value of attribute price_adjustment.



4
5
6
# File 'app/services/workarea/global_e/discount.rb', line 4

def price_adjustment
  @price_adjustment
end

Instance Method Details

#as_json(*_args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/workarea/global_e/discount.rb', line 12

def as_json(*_args)
  {
    OriginalDiscountValue: original_discount_value,
    VATRate: vat_rate,
    LocalVATRate: local_vat_rate,
    DiscountValue: discount_value,
    Name: name,
    Description: description,
    CouponCode: coupon_code,
    ProductCartItemId: product_cart_item_id,
    DiscountCode: discount_code,
    LoyaltyVoucherCode: loyalty_voucher_code,
    DiscountType: discount_type,
    CalculationMode: calculation_mode
  }.compact
end

#calculation_modeObject

One of the following possible values of CalculationModeOptions enumeration denoting the calculation mode of a discount to be implemented by GEM:

| CalculationMode Option Value | Name | Description | | 1 (default) | Percentage discount | Discount value specified in OriginalDiscountValue should be used for calculating the discount’s

DiscountValue as percentage of the full product’s price (specified in Product.OriginalSalePrice
for line item level discounts) or cart price (sum of all Product.OriginalSalePrice values) for cart level discounts. |

| 2 | Fixed in original currency | Discount value specified in OriginalDiscountValue denotes the fixed value in the merchant’s

currency. When calculating the discount’s DiscountValue, only the respective FX rate should be
applied to OriginalDiscountValue. No other price modifications (such as country coefficient) should be performed. |

| 3 | Fixed in customer currency | Discount value specified in DiscountValue denotes the fixed value nominated in the end

customer’s currency that shouldn’t be affected by any price modifications (such as country coefficient). |

return [Integer]



172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/services/workarea/global_e/discount.rb', line 172

def calculation_mode
  return 3 if order.fixed_pricing?
  return 1 if free_gift?

  amount_type = discount.try(:amount_type)
  case amount_type
  when :percent
    1
  when :flat
    2
  end
end

#coupon_codeString

Merchant’s coupon code used for this discount (applicable to coupon-based discounts only)

Returns:

  • (String)


99
100
101
# File 'app/services/workarea/global_e/discount.rb', line 99

def coupon_code
  (discount_promo_codes & order_promo_codes).first
end

#descriptionString

Discount textual description

Returns:

  • (String)


90
91
92
# File 'app/services/workarea/global_e/discount.rb', line 90

def description
  "#{discount.class.name.demodulize.underscore.titleize} - #{name}"
end

#discount_codeString

Discount code used to identify the discount on the Merchant’s site. This property may be optionally specified in SendCart method only, so that the same value could be posted back when creating the order on the Merchant’s site with SendOrderToMerchant method.

Returns:

  • (String)


123
124
125
# File 'app/services/workarea/global_e/discount.rb', line 123

def discount_code
  price_adjustment.global_e_discount_code
end

#discount_typeInteger

One of the following possible values of DiscountTypeOptions enumeration denoting a type of a discount:

| DiscountType Option Value | Name | Description | | 1 | Cart Discount | Discount related to volume, amount, coupon or another promotion value. | | 2 | Shipping Discount | Discount aimed to sponsor international shipping. | | 3 | Loyalty points discount | Discount applied against the Merchant’s loyalty points to be spent for this purchase. | | 4 | Duties discount | Discount aimed to sponsor taxes & duties pre- paid by the end customer in Global-e checkout. | | 5 | Checkout Loyalty Points Discount | Discount applied against the Merchant’s loyalty points in Global-e checkout. | | 6 | Payment Charge | Discount aimed to sponsor “Cash on Delivery” fee. |

Returns:

  • (Integer)


148
149
150
151
152
153
154
# File 'app/services/workarea/global_e/discount.rb', line 148

def discount_type
  if GlobalE.shipping_discount_types.include? discount.class.name
    2
  else
    1
  end
end

#discount_valueFloat

Discount value as displayed to the customer, after applying country coefficient, FX conversion and IncludeVAT handling.

Returns:

  • (Float)


68
69
70
71
72
73
74
75
76
# File 'app/services/workarea/global_e/discount.rb', line 68

def discount_value
  return 0 unless order.fixed_pricing?

  if free_gift? && pricing_sku.present?
    pricing_sku.find_price(quantity: 1).regular.to_f
  else
    price_adjustment.amount.abs.to_f
  end
end

#local_vat_rateFloat

VAT rate that would be applied to this discount if the order was placed by the local customer. This value must be specified if UseCountryVAT for the current Country is TRUE and therefore VATRate property actually denotes the VAT for the target country.

Returns:

  • (Float)


60
61
# File 'app/services/workarea/global_e/discount.rb', line 60

def local_vat_rate
end

#loyalty_voucher_codeString

Code used on the Merchant’s site to identify the Loyalty Voucher that this discount is based on

Returns:

  • (String)


132
133
# File 'app/services/workarea/global_e/discount.rb', line 132

def loyalty_voucher_code
end

#nameString

Discount name

Returns:

  • (String)


82
83
84
# File 'app/services/workarea/global_e/discount.rb', line 82

def name
  discount.name
end

#original_discount_valueFloat

Discount value in original Merchant’s currency including the local VAT, before applying any price modifications. This property always denotes the discount value in the default Merchant’s country, regardless of UseCountryVAT for the end customer’s current country.

Returns:

  • (Float)


36
37
38
39
40
41
42
43
44
# File 'app/services/workarea/global_e/discount.rb', line 36

def original_discount_value
  return 0 if order.fixed_pricing?

  if free_gift? && pricing_sku.present?
    pricing_sku.find_price(quantity: 1).regular.to_f
  else
    price_adjustment.amount.abs.to_f
  end
end

#product_cart_item_idString

Identifier of the product cart item related to this discount on the Merchant’s site. This property may be optionally specified in SendCart method only, so that the same value could be posted back when creating the order on the Merchant’s site with SendOrderToMerchant method.

Returns:

  • (String)


110
111
112
113
114
# File 'app/services/workarea/global_e/discount.rb', line 110

def product_cart_item_id
  if price_adjustment.price == "item"
    price_adjustment&._parent&.id&.to_s
  end
end

#vat_rateFloat

VAT rate applied to this discount

Returns:

  • (Float)


50
51
# File 'app/services/workarea/global_e/discount.rb', line 50

def vat_rate
end