Qwaiter supplier on Ember 1.0
This commit is contained in:
+10
-9
@@ -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
|
||||
|
||||
+4
-3
@@ -1,5 +1,6 @@
|
||||
class Order
|
||||
include SimplyStored::Couch
|
||||
include ActiveModel::SerializerSupport
|
||||
|
||||
property :state, default: 'placed' # placed, active, delivered, cancelled, closed
|
||||
|
||||
@@ -115,9 +116,9 @@ class Order
|
||||
ho[:product_orders] = []
|
||||
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}
|
||||
ho[:product_orders] << {product_name: product_order.product.name, id: product_order.id, quantity: 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}
|
||||
ho[:product_orders] << {product_name: product_order.product.name, id: product_order.id, quantity: product_order.quantity, price: product_order.price}
|
||||
end
|
||||
ho[:total_amount] = order_total.round(2)
|
||||
@with_products_as_json = ho
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class ProductOrder
|
||||
include SimplyStored::Couch
|
||||
|
||||
property :amount, type: Fixnum
|
||||
property :quantity, type: Fixnum
|
||||
property :price, type: Float
|
||||
|
||||
belongs_to :product
|
||||
|
||||
@@ -11,7 +11,7 @@ class Supplier
|
||||
#LOCATION
|
||||
property :lat, type: Float, default: 52.08062426379751
|
||||
property :lng, type: Float, default: 4.312562942504883
|
||||
|
||||
|
||||
#WIFI
|
||||
property :offer_wifi
|
||||
property :wifi_ssid
|
||||
@@ -112,6 +112,12 @@ class Supplier
|
||||
self.devise_mailer.confirmation_instructions(self).deliver
|
||||
end
|
||||
|
||||
def find_order(id)
|
||||
order = Order.find(id)
|
||||
raise SimplyStored::RecordNotFound unless order.supplier_id == self.id
|
||||
order
|
||||
end
|
||||
|
||||
def send_creation_notifications
|
||||
SupplierMailer.creation(self).deliver
|
||||
end
|
||||
|
||||
@@ -7,6 +7,8 @@ class User
|
||||
|
||||
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :token_authenticatable # , :registerable
|
||||
|
||||
property :authentication_token
|
||||
|
||||
has_and_belongs_to_many :lists, storing_keys: false
|
||||
has_many :orders
|
||||
|
||||
|
||||
Reference in New Issue
Block a user