class Order include SimplyStored::Couch property :state, default: 'placed' # placed, active, delivered, cancelled, closed belongs_to :list belongs_to :user belongs_to :supplier belongs_to :section has_many :product_orders, dependent: :destroy #has_many :products, through: :product_orders validates :supplier_id, presence: true validates :user_id, presence: true view :active_for_supplier_view, type: :custom, map_function: %[function(doc){ if(doc.ruby_class == 'Order' && (doc.state == 'placed' || doc.state == 'active')){ emit([doc.supplier_id, doc.list_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(startkey: [supplier_id], endkey: [supplier_id, {}], reduce: false, include_docs: true)) end # Return all currently active orders for a given supplier and list def self.active_for_supplier_and_list(supplier_id, list_id) supplier_id = supplier_id.id if supplier_id.is_a?(SimplyStored::Couch) list_id = list_id.id if list_id.is_a?(SimplyStored::Couch) database.view(active_for_supplier_view(key: [supplier_id, list_id], reduce: false, include_docs: true)) end def self.count_active_for_supplier_and_list(supplier_id, list_id) supplier_id = supplier_id.id if supplier_id.is_a?(SimplyStored::Couch) list_id = list_id.id if list_id.is_a?(SimplyStored::Couch) database.view(active_for_supplier_view(key: [supplier_id, list_id], reduce: true, include_docs: false)) end # Return all currently active orders for a given section def self.active_for_supplier_and_section(supplier, section_id) supplier_id = supplier.is_a?(SimplyStored::Couch) ? supplier.id : supplier section_id = section_id.id if section_id.is_a?(SimplyStored::Couch) 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 alias table_nr table_number def placed? state == 'placed' end def active? state == 'active' end def is_being_processed! self.state = 'active' if save for user_id in list.user_ids broadcast_user user_id, 'order_being_processed', id: id, list_id: list_id end broadcast_supplier supplier_id, 'order_being_processed', id: id, list_id: list_id end end def is_delivered! self.state = 'delivered' if save for user_id in list.user_ids broadcast_user user_id, 'order_being_delivered', id: id, list_id: list_id end broadcast_supplier supplier_id, 'order_being_delivered', id: id, list_id: list_id end end def close! self.state = 'closed' if placed? || active? save end def as_json(*args) h = super.with_indifferent_access h[:table_number] = table_number h[:section_title] = list.table.section.try(:title) h end def with_products_as_json return @with_products_as_json if @with_products_as_json.present? product_orders.include_relation(:product) ho = as_json ho[:products] = [] 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} end ho[:total_amount] = order_total.round(2) @with_products_as_json = ho end end