move to evented mustache system
This commit is contained in:
+46
-6
@@ -60,6 +60,7 @@ class List
|
||||
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
|
||||
|
||||
@@ -95,7 +96,34 @@ class List
|
||||
orders.map(&:close!)
|
||||
self.state = 'closed'
|
||||
self.closed_at = Time.now
|
||||
save
|
||||
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 set_price
|
||||
@@ -121,15 +149,17 @@ class List
|
||||
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
|
||||
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
|
||||
ProductOrder.create order: order, product_id: product_id, amount: number, price: product.price if number > 0
|
||||
end
|
||||
@order
|
||||
broadcast_supplier supplier.id, 'new_order', order.with_products_as_json
|
||||
broadcast_supplier supplier.id, 'list_update', with_info_as_json
|
||||
order
|
||||
end
|
||||
|
||||
def move_to_table to_table
|
||||
@@ -139,7 +169,7 @@ class List
|
||||
save
|
||||
end
|
||||
|
||||
def as_json
|
||||
def as_json(*args)
|
||||
super.merge(table_number: table_number)
|
||||
end
|
||||
|
||||
@@ -152,6 +182,8 @@ class List
|
||||
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)
|
||||
@@ -184,4 +216,12 @@ class List
|
||||
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
|
||||
|
||||
+33
-2
@@ -50,16 +50,47 @@ class Order
|
||||
|
||||
def is_being_processed!
|
||||
self.state = 'active'
|
||||
save
|
||||
if save
|
||||
for user_id in list.user_ids
|
||||
broadcast_user user_id, 'order_being_processed', id: id
|
||||
end
|
||||
broadcast_supplier supplier_id, 'order_being_processed', id: id
|
||||
end
|
||||
end
|
||||
|
||||
def is_delivered!
|
||||
self.state = 'delivered'
|
||||
save
|
||||
if save
|
||||
for user_id in list.user_ids
|
||||
broadcast_user user_id, 'order_being_delivered', id: id
|
||||
end
|
||||
broadcast_supplier supplier_id, 'order_being_delivered', id: id
|
||||
end
|
||||
end
|
||||
|
||||
def close!
|
||||
self.state = 'closed' if placed? || active?
|
||||
save
|
||||
end
|
||||
|
||||
def as_json(*args)
|
||||
h = super.with_indifferent_access
|
||||
h[:table_number] = table_number
|
||||
h[:section_title] = list.table.section.try(:title)
|
||||
h
|
||||
end
|
||||
|
||||
def with_products_as_json
|
||||
return @with_products_as_json if @with_products_as_json.present?
|
||||
product_orders.include_relation(:product)
|
||||
ho = as_json
|
||||
ho[:products] = []
|
||||
order_total = 0.0
|
||||
for product_order in 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)
|
||||
@with_products_as_json = ho
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user