class List include SimplyStored::Couch per_page_method :limit_value #kaminari property :state, default: 'active' # active, #closed property :needs_help, type: :boolean, default: false property :needs_payment, type: :boolean, default: false property :closed_at, type: Time property :join_requests, type: Array, default: [] property :price, type: Float has_many :orders, dependent: :destroy belongs_to :table belongs_to :supplier belongs_to :section has_and_belongs_to_many :users, storing_keys: true validates :table_id, presence: true validates :supplier_id, presence: true view :by_supplier_id_and_id, key: [:supplier_id, :_id] view :active_by_table_id, type: :custom, map_function: %|function(doc){ if(doc.ruby_class == 'List' && doc.state == 'active'){ emit(doc.table_id, 1); } }|, reduce_function: '_sum' view :for_user_view, type: :custom, map_function: %|function(doc){ if(doc.ruby_class == 'List' && doc.user_ids && doc.user_ids.length){ doc.user_ids.forEach(function(uid){ emit([uid, doc.created_at], 1); }) } }|, 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 list.add_user user list.save user.active_list_id = list.id user.save list end def self.for_user(user, options = {}) with_pagination_options(options) do |options| database.view(for_user_view({startkey: ["#{user.id}\u9999"], endkey: [user.id], include_docs: true, reduce: false, descending: true}.merge(options))) end end def self.for_user_created_at(user, range, options = {}) database.view(for_user_view({startkey: [user.id, range.last], endkey: [user.id, range.first], include_docs: true, reduce: false, descending: true}.merge(options))) end def close! orders.include_relation(:product_orders) set_price orders.map(&:close!) self.state = 'closed' self.closed_at = Time.now save end 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) end list_total += order_total.round(2) end self.price = list_total.round(2) end def table_number @table_number ||= table.number end def active? state == 'active' end 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 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 end @order end def move_to_table to_table UserTableMove.create list: self, from_table_id: table_id, to_table: to_table self.table = to_table self.section_id = to_table.section_id save end 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