improvements
This commit is contained in:
+44
-5
@@ -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
|
||||
|
||||
@@ -18,11 +18,23 @@ class Order
|
||||
emit(doc.supplier_id, 1);
|
||||
}
|
||||
}], reduce_function: '_sum'
|
||||
view :active_for_supplier_and_section_view, type: :custom, map_function: %[function(doc){
|
||||
if(doc.ruby_class == 'Order' && (doc.state == 'placed' || doc.state == 'active')){
|
||||
emit([doc.supplier_id, doc.section_id], 1);
|
||||
}
|
||||
}], reduce_function: '_sum'
|
||||
view :by_supplier_id_and_id, key: [:supplier_id, :_id]
|
||||
|
||||
# Return all currently active orders for a given supplier
|
||||
def self.active_for_supplier(supplier_id)
|
||||
database.view(active_for_supplier_view(key: supplier_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)
|
||||
database.view(active_for_supplier_and_section_view(key: [supplier.id, section_id], reduce: false, include_docs: true))
|
||||
end
|
||||
|
||||
def table_number
|
||||
list.table.number
|
||||
end
|
||||
|
||||
@@ -22,24 +22,26 @@ class Supplier
|
||||
|
||||
after_create :add_section_on_create
|
||||
|
||||
def active_orders
|
||||
return @active_orders if @active_orders
|
||||
@active_orders = Order.active_for_supplier(id)
|
||||
def active_orders(options = {})
|
||||
return @active_orders if @active_orders && @active_orders_options == options
|
||||
@active_orders_options = options
|
||||
@active_orders = options[:section_id] ? Order.active_for_supplier_and_section(self, options[:section_id]) : Order.active_for_supplier(id)
|
||||
@active_orders.include_relation(list: {table: :section}, product_orders: :order)
|
||||
@active_orders
|
||||
end
|
||||
|
||||
def active_lists
|
||||
def active_lists(options = {})
|
||||
return @active_lists if @active_lists
|
||||
@tables ||= active_tables
|
||||
@tables ||= active_tables(options)
|
||||
@tables.include_relation(:lists)
|
||||
@active_lists = @tables.map(&:lists).flatten.select(&:active?)
|
||||
@active_lists.include_relations(table: :section, orders: {product_orders: :product})
|
||||
end
|
||||
|
||||
|
||||
def active_tables
|
||||
tables
|
||||
# Return the currently active tables for the supplier
|
||||
def active_tables(options = {})
|
||||
options[:section_id].present? ? tables.select{|t| t.section_id == options[:section_id]} : tables
|
||||
end
|
||||
|
||||
def non_placed_tables
|
||||
|
||||
Reference in New Issue
Block a user