Module: SpreeCmCommissioner::VendorDecorator

Defined in:
app/models/spree_cm_commissioner/vendor_decorator.rb

Constant Summary collapse

STAR_RATING =
[0, 1, 2, 3, 4, 5].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



6
7
8
9
10
11
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 6

def self.prepended(base)
  base.include SpreeCmCommissioner::ProductType
  base.include SpreeCmCommissioner::VendorPromotable
  base.include SpreeCmCommissioner::VendorPreference
  base.include SpreeCmCommissioner::TenantUpdatable

  base.attr_accessor :service_availabilities

  base.store :preferences, accessors: %i[account_name account_number penalty_rate penalty_label six_months_discount twelve_months_discount]

  base.before_save :generate_code, if: :code.nil?

  base.has_many :photos, -> { order(:position) }, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::VendorPhoto'
  base.has_many :option_values, through: :products
  base.has_many :vendor_option_types, class_name: 'SpreeCmCommissioner::VendorOptionType'
  base.has_many :option_value_vendors, class_name: 'SpreeCmCommissioner::OptionValueVendor'
  base.has_many :option_types, through: :vendor_option_types
  base.has_many :nearby_places, -> { order(position: :asc) }, class_name: 'SpreeCmCommissioner::VendorPlace', dependent: :destroy

  base.has_many :stock_items, through: :variants, class_name: 'Spree::StockItem'

  base.has_many :taxon_vendors, class_name: 'SpreeCmCommissioner::TaxonVendor'
  base.has_many :taxons, through: :taxon_vendors

  base.has_many :promoted_option_types, -> { where(promoted: true).order(:position) },
                through: :vendor_option_types, source: :option_type

  base.has_many :vendor_kind_option_types, -> { where(kind: :vendor).order(:position) },
                through: :vendor_option_types, source: :option_type

  base.has_many :vendor_kind_option_values,
                through: :option_value_vendors, source: :option_value

  base.has_many :branches, -> { branch }, class_name: 'SpreeCmCommissioner::VendorPlace'
  base.has_many :stops, -> { stop }, class_name: 'SpreeCmCommissioner::VendorPlace'
  base.has_many :locations, -> { location }, class_name: 'SpreeCmCommissioner::VendorPlace'

  base.has_many :branch_places, through: :branches, class_name: 'SpreeCmCommissioner::Place', source: :place
  base.has_many :stop_places, through: :stops, class_name: 'SpreeCmCommissioner::Place', source: :place
  base.has_many :location_places, through: :locations, class_name: 'SpreeCmCommissioner::Place', source: :place

  base.has_many :places,
                through: :nearby_places, source: :place, class_name: 'SpreeCmCommissioner::Place'

  base.has_many :dynamic_fields, class_name: 'SpreeCmCommissioner::DynamicField', foreign_key: :vendor_id, dependent: :destroy

  base.has_one  :logo, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::VendorLogo'
  base.has_one  :payment_qrcode, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::VendorPaymentQrcode'
  base.has_one  :web_promotion_banner, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::VendorWebPromotionBanner'
  base.has_one  :app_promotion_banner, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::VendorAppPromotionBanner'
  base.has_one  :stock_location, -> { where(active: true) }, class_name: 'Spree::StockLocation', dependent: :destroy

  base.belongs_to :default_state, class_name: 'Spree::State', inverse_of: :vendors
  base.multi_tenant :tenant, class_name: 'SpreeCmCommissioner::Tenant'

  base.delegate :lat, :lon, to: :stock_location, allow_nil: true

  base.after_save :update_state_total_inventory, if: :saved_change_to_total_inventory?
  base.after_save :update_customer_numbers, if: :saved_change_to_code?

  base.has_many :promoted_option_values, -> { joins(:option_type).where('option_type.promoted' => true) },
                through: :option_value_vendors, source: :option_value

  base.accepts_nested_attributes_for :nearby_places, allow_destroy: true

  base.has_many :service_calendars, as: :calendarable, dependent: :destroy, class_name: 'SpreeCmCommissioner::ServiceCalendar'

  base.has_many :customers, class_name: 'SpreeCmCommissioner::Customer', dependent: :destroy
  base.has_many :invoices, class_name: 'SpreeCmCommissioner::Invoice', dependent: :destroy
  base.has_many :subscriptions, through: :customers, class_name: 'SpreeCmCommissioner::Subscription'
  base.has_many :subscription_orders, through: :subscriptions, class_name: 'Spree::Order', source: :orders
  base.has_many :promotion_categories, class_name: 'Spree::PromotionCategory', foreign_key: :vendor_id, dependent: :destroy

  base.has_many :homepage_section_relatables,
                as: :relatable,
                class_name: 'SpreeCmCommissioner::HomepageSectionRelatable',
                dependent: :destroy,
                inverse_of: :relatable

  base.has_many :vehicles, class_name: 'SpreeCmCommissioner::Vehicle', dependent: :destroy
  base.has_many :trips, class_name: 'SpreeCmCommissioner::Trip', dependent: :destroy
  base.has_many :trip_stops, through: :trips, class_name: 'SpreeCmCommissioner::TripStop'
  base.has_many :boarding_trip_stops, through: :trips, class_name: 'SpreeCmCommissioner::TripStop', source: :boarding_trip_stops
  base.has_many :drop_off_trip_stops, through: :trips, class_name: 'SpreeCmCommissioner::TripStop', source: :drop_off_trip_stops

  base.validates :account_name, :account_number, presence: true, if: lambda {
                                                                       payment_qrcode.present? && Spree::Store.default.code.include?('billing')
                                                                     }

  base.validates :commission_rate, presence: true, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 }
  base.validates :from_email, presence: true, if: :tenant_present?

  def base.by_vendor_id!(vendor_id)
    if vendor_id.to_s =~ /^\d+$/
      find(vendor_id)
    else
      find_by!(slug: vendor_id)
    end
  end

  # TODO: we will need searchkick later
  # unless Rails.env.test?
  #   base.searchkick(
  #     word_start: [:name],
  #     unscope: false,
  #   ) unless base.respond_to?(:searchkick_index)

  #   base.scope :search_import, lambda {
  #     includes(
  #       :option_values,
  #     )
  #   }
  # end

  extend Spree::DisplayMoney
  money_methods :min_price, :max_price

  def update_customer_numbers
    customers.each(&:update_number)
  end

  def selected_place_references
    places.where.not(reference: [nil, '']).pluck(:reference)
  end

  def search_data
    # option_values_presentation
    presentations = option_values.pluck(:presentation).uniq
    {
      id: id,
      name: name,
      slug: slug,
      active: active?,
      min_price: min_price,
      max_price: max_price,
      created_at: created_at,
      updated_at: updated_at,
      presentation: presentations
    }
  end

  def base.search_fields
    [:name]
  end

  def vendor_and_tenant_name
    tenant_name = tenant.present? ? " (#{tenant.name})" : ''
    "#{name}#{tenant_name}"
  end

  def index_data
    {}
  end

  def update_total_inventory
    update(total_inventory: stock_items.pluck(:count_on_hand).sum)
  end

  def update_min_max_price
    min_price = Spree::Product.min_price(self)
    max_price = Spree::Product.max_price(self)
    min_price = max_price if min_price.zero?

    update(min_price: min_price, max_price: max_price)
  end

  def update_location
    update(default_state_id: stock_locations.first&.state_id)
  end

  def update_state_total_inventory
    SpreeCmCommissioner::StateJob.perform_later(default_state_id) unless default_state_id.nil?
  end
end

Instance Method Details

#generate_codeObject



185
186
187
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 185

def generate_code
  self.code = (code.presence || name[0, 3].upcase)
end

#index_dataObject



156
157
158
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 156

def index_data
  {}
end

#organizer_urlObject



189
190
191
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 189

def organizer_url
  "#{Spree::Store.default.formatted_url}/organizers/#{slug}"
end

#search_dataObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 131

def search_data
  # option_values_presentation
  presentations = option_values.pluck(:presentation).uniq
  {
    id: id,
    name: name,
    slug: slug,
    active: active?,
    min_price: min_price,
    max_price: max_price,
    created_at: created_at,
    updated_at: updated_at,
    presentation: presentations
  }
end

#selected_option_value_vendors_idsObject



181
182
183
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 181

def selected_option_value_vendors_ids
  option_value_vendors.pluck(:option_value_id)
end

#selected_place_referencesObject



127
128
129
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 127

def selected_place_references
  places.where.not(reference: [nil, '']).pluck(:reference)
end

#update_customer_numbersObject



123
124
125
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 123

def update_customer_numbers
  customers.each(&:update_number)
end

#update_locationObject



172
173
174
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 172

def update_location
  update(default_state_id: stock_locations.first&.state_id)
end

#update_min_max_priceObject



164
165
166
167
168
169
170
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 164

def update_min_max_price
  min_price = Spree::Product.min_price(self)
  max_price = Spree::Product.max_price(self)
  min_price = max_price if min_price.zero?

  update(min_price: min_price, max_price: max_price)
end

#update_state_total_inventoryObject



176
177
178
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 176

def update_state_total_inventory
  SpreeCmCommissioner::StateJob.perform_later(default_state_id) unless default_state_id.nil?
end

#update_total_inventoryObject



160
161
162
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 160

def update_total_inventory
  update(total_inventory: stock_items.pluck(:count_on_hand).sum)
end

#vendor_and_tenant_nameObject



151
152
153
154
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 151

def vendor_and_tenant_name
  tenant_name = tenant.present? ? " (#{tenant.name})" : ''
  "#{name}#{tenant_name}"
end

#vendor_rulesObject



193
194
195
196
197
198
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 193

def vendor_rules
  rules_option_type = Spree::OptionType.rules_option_type
  return vendor_kind_option_values.none if rules_option_type.nil?

  vendor_kind_option_values.where(option_type_id: rules_option_type.id)
end