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
187
188
189
190
191
192
193
194
195
196
# 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.include SpreeCmCommissioner::Integrations::IntegrationMappable

  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.before_save :set_default_service_origin, if: :new_record?

  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

  # currently a vendor can have only one integration, but in future it can be many.
  base.has_one :integration, class_name: 'SpreeCmCommissioner::Integration', dependent: :destroy, inverse_of: :vendor

  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.belongs_to :service_origin, class_name: 'Spree::Taxon', optional: true
  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 :routes, class_name: 'SpreeCmCommissioner::Route', dependent: :destroy
  base.has_many :popular_routes, -> { order(fulfilled_order_count: :desc, order_count: :desc) },
                class_name: 'SpreeCmCommissioner::Route'

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

  base.has_many :vehicle_types, class_name: 'SpreeCmCommissioner::VehicleType', dependent: :destroy
  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(state_id: default_state_id) unless default_state_id.nil?
  end
end

Instance Method Details

#generate_codeObject



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

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

#index_dataObject



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

def index_data
  {}
end

#organizer_urlObject



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

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

#search_dataObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 148

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



198
199
200
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 198

def selected_option_value_vendors_ids
  option_value_vendors.pluck(:option_value_id)
end

#selected_place_referencesObject



144
145
146
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 144

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

#send_telegram_vendor_creation_alertObject



217
218
219
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 217

def send_telegram_vendor_creation_alert
  VendorCreationTelegramAlertSenderJob.perform_later(vendor_id: id)
end

#set_default_service_originObject



221
222
223
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 221

def set_default_service_origin
  self.service_origin ||= Spree::Taxon.find_by(name: 'Cambodian', kind: :nationality)
end

#update_customer_numbersObject



140
141
142
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 140

def update_customer_numbers
  customers.each(&:update_number)
end

#update_locationObject



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

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

#update_min_max_priceObject



181
182
183
184
185
186
187
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 181

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



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

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

#update_total_inventoryObject



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

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

#vendor_and_tenant_nameObject



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

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

#vendor_rulesObject



210
211
212
213
214
215
# File 'app/models/spree_cm_commissioner/vendor_decorator.rb', line 210

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