end of day commit

This commit is contained in:
2012-08-23 18:50:06 +02:00
parent 13dd2bf335
commit 0bae1bcbed
37 changed files with 1157 additions and 83 deletions
+19 -2
View File
@@ -1,15 +1,18 @@
class List
include SimplyStored::Couch
property :state
property :state, default: 'active' # active, #closed
property :need_help, type: :boolean, default: false
property :needs_payment, type: :boolean, default: false
property :closed_at, type: Time
has_many :orders
has_many :orders, dependent: :destroy
belongs_to :table
has_and_belongs_to_many :users, storing_keys: true
validates :table_id, presence: true
def close!
#TODO: close orders
self.state = 'closed'
self.closed_at = Time.now
save
@@ -18,4 +21,18 @@ class List
def supplier
table.supplier
end
def active?
state == 'active'
end
def place_order(products)
return unless products.any?
@order = Order.create list: self, supplier: supplier
return unless @order.id
products.each do |product_id, number|
number = number.to_i
ProductOrder.create order: @order, product_id: product_id, amount: number if number > 0
end
end
end
+38 -2
View File
@@ -1,9 +1,45 @@
class Order
include SimplyStored::Couch
property :state, default: 'placed' # placed, active, delivered, cancelled
belongs_to :list
belongs_to :user
belongs_to :supplier
has_many :product_orders
has_many :products, through: :product_orders
has_many :product_orders, dependent: :destroy
#has_many :products, through: :product_orders
validates :supplier_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, 1);
}
}], reduce_function: '_sum'
def self.active_for_supplier(supplier_id)
database.view(active_for_supplier_view(key: supplier_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'
save
end
def is_delivered!
self.state = 'delivered'
save
end
end
+4 -1
View File
@@ -1,6 +1,9 @@
class ProductOrder
include SimplyStored::Couch
property :amount, type: Fixnum
belongs_to :product
belongs_to :order
end
+27 -5
View File
@@ -2,9 +2,31 @@ class Supplier
include SimplyStored::Couch
property :name
has_many :lists
has_many :orders, through: :lists
has_many :products
has_many :product_categories
has_many :tables
#has_many :orders, through: :lists
has_many :products, dependent: :destroy
has_many :product_categories, dependent: :destroy
has_many :tables, dependent: :destroy
#has_many :lists, through: :tables
has_many :orders
def active_orders
return @active_orders if @active_orders
@active_orders = Order.active_for_supplier(id)
@active_orders.include_relation(list: :table, product_orders: :order)
@active_orders
end
def active_lists
return @active_lists if @active_lists
@tables ||= active_tables
@tables.include_relation(:lists)
@active_lists = @tables.map(&:lists).flatten.select(&:active?)
@active_lists.include_relations(:table, orders: {product_orders: :product})
end
def active_tables
tables
end
end