316 lines
10 KiB
Ruby
316 lines
10 KiB
Ruby
class List
|
|
include SimplyStored::Couch
|
|
per_page_method :limit_value #kaminari
|
|
|
|
property :state, default: 'active' # active, #closed
|
|
property :needs_help, type: :boolean, default: false
|
|
property :needs_payment, type: :boolean, default: false
|
|
property :closed_at, type: Time
|
|
property :join_requests, type: Array, default: []
|
|
property :price, type: Float
|
|
property :is_payed, type: :boolean, default: false
|
|
property :payed_at, type: Time
|
|
|
|
has_many :orders, dependent: :destroy
|
|
belongs_to :table
|
|
belongs_to :supplier
|
|
belongs_to :section
|
|
has_and_belongs_to_many :users, storing_keys: true
|
|
|
|
attr_protected :supplier_id
|
|
|
|
validates :table_id, presence: true
|
|
validates :supplier_id, presence: true
|
|
|
|
view :by_supplier_id_and_id, key: [:supplier_id, :_id]
|
|
view :by_supplier_id, key: :supplier_id
|
|
view :for_supplier_view, key: [:supplier_id, :created_at]
|
|
|
|
view :active_by_table_id_view, type: :custom, map_function: %|function(doc){
|
|
if(doc.ruby_class == 'List' && doc.state == 'active'){
|
|
emit(doc.table_id, 1);
|
|
}
|
|
}|, reduce_function: '_sum'
|
|
|
|
view :active_by_supplier_id_view, type: :custom, map_function: %|function(doc){
|
|
if(doc.ruby_class == 'List' && doc.state == 'active'){
|
|
emit(doc.supplier_id, 1);
|
|
}
|
|
}|, reduce_function: '_sum'
|
|
|
|
view :active_by_section_id_view, type: :custom, map_function: %|function(doc){
|
|
if(doc.ruby_class == 'List' && doc.state == 'active' && doc.section_id){
|
|
emit(doc.section_id, 1);
|
|
}
|
|
}|, reduce_function: '_sum'
|
|
|
|
view :active_for_supplier_and_section_view, type: :custom, map_function: %[function(doc){
|
|
if(doc.ruby_class == 'List' && doc.state == 'active'){
|
|
emit([doc.supplier_id, doc.section_id], 1);
|
|
}
|
|
}], reduce_function: '_sum'
|
|
|
|
view :for_user_view, type: :custom, map_function: %|function(doc){
|
|
if(doc.ruby_class == 'List' && doc.user_ids && doc.user_ids.length){
|
|
doc.user_ids.forEach(function(uid){
|
|
emit([uid, doc.created_at], 1);
|
|
})
|
|
}
|
|
}|, reduce_function: '_sum'
|
|
|
|
def self.active
|
|
database.view(active_by_supplier_id_view(reduce: false, include_docs: true))
|
|
end
|
|
|
|
# Create, a list given a table and a user
|
|
def self.from_table table, user
|
|
return if user.has_active_list?
|
|
list = new table: table, section_id: table.section_id
|
|
list.supplier_id = table.supplier_id
|
|
list.add_user user
|
|
list.save
|
|
user.active_list_id = list.id
|
|
user.save
|
|
#list.broadcast_supplier list.supplier_id, 'list_added', list.with_info_as_json
|
|
list
|
|
end
|
|
|
|
def self.active_for_supplier(supplier_id, options = {})
|
|
database.view(active_by_supplier_id_view(key: supplier_id, reduce: false, include_docs: true))
|
|
end
|
|
|
|
def self.active_for_section(section_id, options = {})
|
|
database.view(active_by_section_id_view(key: section_id, reduce: false, include_docs: true))
|
|
end
|
|
|
|
# Return all currently active orders for a given section
|
|
def self.active_for_supplier_and_section(supplier, section_id, options = {})
|
|
database.view(active_for_supplier_and_section_view(key: [supplier.id, section_id], reduce: false, include_docs: true))
|
|
end
|
|
|
|
def self.active_for_table(table_id, options = {})
|
|
database.view(active_by_table_id_view(options.reverse_merge(key: table_id, reduce: false, include_docs: true)))
|
|
end
|
|
|
|
def self.for_user(user, options = {})
|
|
total_entries = database.view(for_user_view({startkey: ["#{user.id}\u9999"], endkey: [user.id], include_docs: false, reduce: true, descending: true}))
|
|
options[:total_entries] = total_entries
|
|
with_pagination_options(options) do |options|
|
|
database.view(for_user_view({startkey: ["#{user.id}\u9999"], endkey: [user.id], include_docs: true, reduce: false, descending: true}.merge(options)))
|
|
end
|
|
end
|
|
|
|
def self.for_user_created_at(user, range, options = {})
|
|
database.view(for_user_view({startkey: [user.id, range.last], endkey: [user.id, range.first], include_docs: true, reduce: false, descending: true}.merge(options)))
|
|
end
|
|
|
|
def self.for_supplier_created_at(supplier, range, options = {})
|
|
database.view(for_supplier_view({startkey: [supplier.id, range.last], endkey: [supplier.id, range.first], include_docs: true, reduce: false, descending: true}.merge(options)))
|
|
end
|
|
|
|
def mark_as_payed
|
|
self.is_payed = true
|
|
self.payed_at = Time.now
|
|
end
|
|
|
|
def close!
|
|
orders.include_relation(:product_orders)
|
|
set_price
|
|
orders.map(&:close!)
|
|
self.state = 'closed'
|
|
self.closed_at = Time.now
|
|
if save
|
|
for user in users
|
|
user.active_list_id = nil
|
|
user.save
|
|
broadcast_user user.id, 'list_closed', id: id
|
|
end
|
|
broadcast_supplier supplier_id, 'list_closed', id: id
|
|
end
|
|
end
|
|
|
|
def is_helped!
|
|
self.needs_help = false
|
|
if save
|
|
for user_id in user_ids
|
|
broadcast_user user_id, 'list_helped', id: id
|
|
end
|
|
broadcast_supplier supplier_id, 'list_helped', id: id
|
|
end
|
|
end
|
|
|
|
def needs_payment!
|
|
self.needs_payment = true
|
|
if save
|
|
for user_id in user_ids
|
|
broadcast_user user_id, 'list_needs_payment', id: id
|
|
end
|
|
broadcast_supplier supplier_id, 'list_needs_payment', id: id
|
|
end
|
|
end
|
|
|
|
def move_to_table! to_table
|
|
UserTableMove.create list: self, from_table_id: table_id, to_table: to_table
|
|
self.table = to_table
|
|
self.section_id = to_table.section_id
|
|
if save
|
|
for user_id in user_ids
|
|
broadcast_user user_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title)
|
|
end
|
|
broadcast_supplier supplier_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title)
|
|
end
|
|
end
|
|
|
|
def needs_help!
|
|
self.needs_help = true
|
|
if save
|
|
for user_id in user_ids
|
|
broadcast_user user_id, 'list_needs_help', id: id
|
|
end
|
|
broadcast_supplier(supplier_id, 'list_needs_help', id: id)
|
|
end
|
|
end
|
|
|
|
def approve_join_request_for_user!(user)
|
|
if join_requests.include?(user.id)
|
|
join_requests.delete(user.id)
|
|
user.active_list_id = self.id
|
|
add_user(user)
|
|
user.save
|
|
self.is_dirty
|
|
if save
|
|
broadcast_user user.id, 'join_request_approved'
|
|
end
|
|
end
|
|
end
|
|
|
|
def unlink_user(user)
|
|
changed = join_requests.delete(user.id)
|
|
changed ||= user_ids.delete(user.id)
|
|
if user.active_list_id == id
|
|
user.active_list_id = nil
|
|
user.save
|
|
end
|
|
save if changed
|
|
end
|
|
|
|
def send_table_join_request_for_user!(requester)
|
|
unless join_requests.include?(requester.id)
|
|
self.join_requests << requester.id
|
|
self.is_dirty
|
|
if save
|
|
for user in users
|
|
broadcast_user user.id, 'user_join_request', list_id: id, user_email: requester.email, user_id: requester.id
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def reject_join_request_for_user!(user_id)
|
|
if join_requests.include?(user_id)
|
|
join_requests.delete(user_id)
|
|
self.is_dirty
|
|
if save
|
|
broadcast_user user_id, 'join_request_rejected'
|
|
end
|
|
end
|
|
end
|
|
|
|
def set_price
|
|
list_total = 0.0
|
|
for order in orders
|
|
order_total = 0.0
|
|
for product_order in order.product_orders
|
|
order_total += (product_order.amount * product_order.price).round(2)
|
|
end
|
|
list_total += order_total.round(2)
|
|
end
|
|
self.price = list_total.round(2)
|
|
end
|
|
|
|
def table_number
|
|
@table_number ||= table.number
|
|
end
|
|
|
|
def active?
|
|
state == 'active'
|
|
end
|
|
|
|
def place_order(user, products)
|
|
return false unless products.any?
|
|
return false unless user
|
|
order = Order.create list: self, supplier: supplier, user: user, section_id: section_id
|
|
return unless order.id
|
|
loaded_products = self.class.database.load_document products.keys
|
|
products.each do |product_id, number|
|
|
number = number.to_i
|
|
product = loaded_products.find{|p| p.id == product_id} # to get the price
|
|
ProductOrder.create order: order, product_id: product_id, amount: number, price: product.price if number > 0
|
|
end
|
|
set_price
|
|
save
|
|
for user_id in user_ids
|
|
broadcast_user user_id, 'new_order', order: order.with_products_as_json, total_amount: price
|
|
end
|
|
broadcast_supplier supplier.id, 'new_order', order.with_products_as_json
|
|
broadcast_supplier supplier.id, 'list_update', with_info_as_json
|
|
order
|
|
end
|
|
|
|
def as_json(*args)
|
|
super.merge(table_number: table_number)
|
|
end
|
|
|
|
def with_orders_as_json
|
|
return @with_orders_as_json if @with_orders_as_json.present?
|
|
orders.include_relations(product_orders: :product)
|
|
h = as_json
|
|
h[:orders] = []
|
|
h[:list_active] = active?
|
|
list_total = 0.0
|
|
for order in orders
|
|
ho = {products: []}
|
|
ho[:id] = order.id
|
|
ho[:state] = order.state
|
|
order_total = 0.0
|
|
for product_order in order.product_orders
|
|
order_total += (product_order.amount * product_order.price).round(2)
|
|
ho[:products] << {name: product_order.product_name, id: product_order.product_id, number: product_order.amount, price: product_order.price}
|
|
end
|
|
ho[:total_amount] = order_total.round(2)
|
|
ho[:state] = order.state
|
|
list_total += ho[:total_amount]
|
|
h[:orders] << ho
|
|
end
|
|
h[:total_amount] = list_total.round(2)
|
|
@with_orders_as_json = h
|
|
end
|
|
|
|
def with_orders_and_join_requests_as_json
|
|
return @with_orders_and_join_requests_as_json if @with_orders_and_join_requests_as_json.present?
|
|
@with_orders_and_join_requests_as_json = with_orders_as_json.merge(join_requests_as_json)
|
|
end
|
|
|
|
# Return a join requests object in the form of:
|
|
# {join_request: [{user_id: '1saf3...', user_email: 'info@qwaiter.com'}, [....]]}
|
|
def join_requests_as_json
|
|
return @join_requests_as_json if @join_requests_as_json.present?
|
|
h = {join_requests: []}
|
|
# Handle join requests
|
|
if join_requests.any?
|
|
for user in self.class.database.load_document(join_requests)
|
|
h[:join_requests] << {user_id: user.id, user_email: user.email}
|
|
end
|
|
end
|
|
@join_requests_as_json = h
|
|
end
|
|
|
|
def with_info_as_json
|
|
return @with_info_as_json if @with_info_as_json.present?
|
|
hl = as_json
|
|
hl[:total_amount] = orders.inject(0.0){|sum, o| sum + o.product_orders.inject(0.0){|s, po| s + (po.amount * po.price).round(2)}}.round(2)
|
|
hl[:section_title] = table.section.try(:title)
|
|
@with_info_as_json = hl
|
|
end
|
|
end
|