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
|
# 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, class_name: 'SpreeCmCommissioner::Branch'
base.has_many :stops, class_name: 'SpreeCmCommissioner::Stop'
base.has_many :places,
through: :nearby_places, source: :place, class_name: 'SpreeCmCommissioner::Place'
base.has_many :vendor_stops, class_name: 'SpreeCmCommissioner::VendorStop', dependent: :destroy
base.has_many :boarding_points, -> { where(cm_vendor_stops: { stop_type: 0 }) },
through: :vendor_stops, source: :stop, class_name: 'Spree::Taxon'
base.has_many :drop_off_points, -> { where(cm_vendor_stops: { stop_type: 1 }) },
through: :vendor_stops, source: :stop, class_name: 'Spree::Taxon'
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 :homepage_section_relatables,
class_name: 'SpreeCmCommissioner::HomepageSectionRelatable',
dependent: :destroy, inverse_of: :relatable
base.has_many :vehicle_types, class_name: 'SpreeCmCommissioner::VehicleType', dependent: :destroy
base.has_many :vehicles, through: :vehicle_types, class_name: 'SpreeCmCommissioner::Vehicle', 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 }
def base.by_vendor_id!(vendor_id)
if vendor_id.to_s =~ /^\d+$/
find(vendor_id)
else
find_by!(slug: vendor_id)
end
end
extend Spree::DisplayMoney
money_methods :min_price, :max_price
def update_customer_numbers
customers.each(&:update_number)
end
def selected_place_references
places.pluck(:reference)
end
def search_data
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
|