improvements

This commit is contained in:
2012-09-06 14:49:22 +02:00
parent 27d7fd304e
commit 161da7a82d
18 changed files with 209 additions and 81 deletions
+44 -5
View File
@@ -24,6 +24,7 @@ class List
}
}|, reduce_function: '_sum'
# 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, supplier_id: table.supplier_id, section_id: table.section_id
@@ -45,10 +46,6 @@ class List
save
end
def supplier
table.supplier
end
def table_number
@table_number ||= table.number
end
@@ -60,7 +57,7 @@ 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
@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|
@@ -81,4 +78,46 @@ class List
def as_json
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: []}
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
end