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
180
181
182
183
184
185
186
# 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.include SpreeCmCommissioner::StoreMetadata

  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.after_create :send_telegram_vendor_creation_alert

  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 :vendor_routes, class_name: 'SpreeCmCommissioner::VendorRoute'

  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.has_many :pricing_models, class_name: 'SpreeCmCommissioner::PricingModel', foreign_key: :vendor_id, dependent: :destroy

  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?

  base. :term_and_condition_promotion, :string

  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



192
193
194
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 192

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

#index_dataObject



163
164
165
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 163

def index_data
  {}
end

#organizer_urlObject



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

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

#search_dataObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 138

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



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

def selected_option_value_vendors_ids
  option_value_vendors.pluck(:option_value_id)
end

#selected_place_referencesObject



134
135
136
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 134

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

#send_telegram_vendor_creation_alertObject



207
208
209
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 207

def send_telegram_vendor_creation_alert
  VendorCreationTelegramAlertSenderJob.perform_later(id)
end

#update_customer_numbersObject



130
131
132
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 130

def update_customer_numbers
  customers.each(&:update_number)
end

#update_locationObject



179
180
181
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 179

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

#update_min_max_priceObject



171
172
173
174
175
176
177
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 171

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



183
184
185
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 183

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

#update_total_inventoryObject



167
168
169
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 167

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

#vendor_and_tenant_nameObject



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

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

#vendor_rulesObject



200
201
202
203
204
205
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 200

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