Class: SpreeCmCommissioner::Integrations::VireakBuntham::Polling::SyncVariant

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/integrations/vireak_buntham/polling/sync_variant.rb

Overview

SyncVariant — one variant per VET schedule.

Mirrors Larryta::Polling::SyncVariant. SKU: “variant-vendor_id-schedule_id”. Price is the effective local price — foreigner delta applied by SyncNationalityPricing.

Instance Method Summary collapse

Constructor Details

#initialize(sync_result:, vendor:) ⇒ SyncVariant

Returns a new instance of SyncVariant.



10
11
12
13
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/polling/sync_variant.rb', line 10

def initialize(sync_result:, vendor:)
  @sync_result = sync_result
  @vendor = vendor
end

Instance Method Details

#call(product:, schedule:, price:, on_date:) ⇒ Spree::Variant

Parameters:

  • product (Spree::Product)
  • schedule (Resources::Schedule)
  • price (Numeric)

    effective local price (USD)

  • on_date (String)

    YYYY-MM-DD sync date

Returns:

  • (Spree::Variant)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/polling/sync_variant.rb', line 20

def call(product:, schedule:, price:, on_date:)
  sku = "variant-#{@vendor.id}-#{schedule.id}-#{on_date}"
  variant = product.variants.find_by(sku: sku) || product.variants.build(sku: sku)

  @sync_result.track(:variant, variant) do |tracker|
    variant.assign_attributes(
      vendor: @vendor,
      price: price,
      track_inventory: true,
      product_type: :transit,
      option_values: [default_option_value],
      manual_generate_inventory_items: true
    )
    tracker.save_if_changed!(variant)
  end

  variant
end