Qwaiter supplier on Ember 1.0

This commit is contained in:
2013-09-30 17:49:22 +02:00
parent 6d7647c2c5
commit 8ea2e79dc2
44 changed files with 378 additions and 156 deletions
+10 -9
View File
@@ -243,12 +243,13 @@ class List
end
end
# Store the final list price in a property
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)
order_total += (product_order.quantity * product_order.price).round(2)
end
list_total += order_total.round(2)
end
@@ -269,23 +270,23 @@ class List
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
products.each do |product_id, quantity|
quantity = quantity.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, quantity: quantity, price: product.price if quantity > 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, 'list_update', list: with_info_as_json
broadcast_supplier supplier.id, 'list_update', active_model_serializer.new(self).as_json
broadcast_supplier supplier.id, 'new_order', order: order.with_products_as_json
order
end
def as_json(*args)
super.merge(table_number: table_number, has_active_orders: has_active_orders? )
super.merge(id: id, table_number: table_number, has_active_orders: has_active_orders? )
end
def with_orders_as_json
@@ -301,8 +302,8 @@ class List
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}
order_total += (product_order.quantity * product_order.price).round(2)
ho[:products] << {name: product_order.product_name, id: product_order.product_id, number: product_order.quantity, price: product_order.price}
end
ho[:total_amount] = order_total.round(2)
ho[:state] = order.state
@@ -343,7 +344,7 @@ class List
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[:total_amount] = orders.inject(0.0){|sum, o| sum + o.product_orders.inject(0.0){|s, po| s + (po.quantity * po.price).round(2)}}.round(2)
hl[:section_title] = table.section.try(:title)
@with_info_as_json = hl
end