First part of stock-integratino. Orders with supplier_id == 0 become stockorders.

This commit is contained in:
Benjamin Meichsner 2009-02-05 16:40:02 +01:00
parent 251ced4fa1
commit 1912a3fd80
30 changed files with 112 additions and 95 deletions

View file

@ -32,6 +32,7 @@ class Article < ActiveRecord::Base
belongs_to :supplier
belongs_to :article_category
has_many :article_prices, :order => "created_at"
has_many :stock_changes
named_scope :in_stock, :conditions => "quantity > 0", :order => 'suppliers.name', :include => :supplier
named_scope :available, :conditions => {:availability => true}
@ -172,8 +173,9 @@ class Article < ActiveRecord::Base
end
end
def update_quantity(amount)
update_attribute :quantity, quantity + amount
# Update the quantity of items in stock
def update_quantity!
update_attribute :quantity, stock_changes.collect(&:quantity).sum
end
protected

View file

@ -26,8 +26,8 @@ class GroupOrder < ActiveRecord::Base
validates_numericality_of :price
validates_uniqueness_of :ordergroup_id, :scope => :order_id # order groups can only order once per order
named_scope :open, lambda { {:conditions => ["order_id IN (?)", Order.open.collect{|o| o.id}]} }
named_scope :finished, lambda { {:conditions => ["order_id IN (?)", Order.finished.collect{|o| o.id}]} }
named_scope :open, lambda { {:conditions => ["order_id IN (?)", Order.open.collect(&:id)]} }
named_scope :finished, lambda { {:conditions => ["order_id IN (?)", Order.finished.collect(&:id)]} }
# Updates the "price" attribute.
# This will be the maximum value of an order

View file

@ -154,8 +154,8 @@ class GroupOrderArticle < ActiveRecord::Base
memoize :calculate_result
# Returns order result,
# either calcualted on the fly or fetched from quantity_/tolerance_result
# After an order is finished, there is only the result
# either calcualted on the fly or fetched from result attribute
# Result is set when finishing the order.
def result(type = :total)
self[:result] || calculate_result[type]
end

View file

@ -28,21 +28,44 @@ class Order < ActiveRecord::Base
belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by_user_id"
# Validations
validates_presence_of :supplier_id, :starts
validate_on_create :starts_before_ends, :include_articles
validates_presence_of :starts
validate :starts_before_ends, :include_articles
# Callbacks
after_update :update_price_of_group_orders
# Finders
named_scope :open, :conditions => { :state => 'open' },
:order => 'ends DESC'
named_scope :finished, :conditions => "state = 'finished' OR state = 'closed'",
:order => 'ends DESC'
named_scope :finished_not_closed, :conditions => { :state => 'finished' },
:order => 'ends DESC'
named_scope :closed, :conditions => { :state => 'closed' },
:order => 'ends DESC'
named_scope :open, :conditions => {:state => 'open'}, :order => 'ends DESC'
named_scope :finished, :conditions => "state = 'finished' OR state = 'closed'", :order => 'ends DESC'
named_scope :finished_not_closed, :conditions => {:state => 'finished'}, :order => 'ends DESC'
named_scope :closed, :conditions => {:state => 'closed'}, :order => 'ends DESC'
named_scope :stockit, :conditions => {:supplier_id => 0}, :order => 'ends DESC'
def stockit?
supplier_id == 0
end
def name
stockit? ? "Lager" : supplier.name
end
def articles_for_ordering
if stockit?
Article.in_stock.all(:include => :article_category,
:order => 'article_categories.name, articles.name').group_by { |a| a.article_category.name }
else
supplier.articles.available.all.group_by { |a| a.article_category.name }
end
end
# Fetch last orders from same supplier, to generate an article selection proposal
def templates
if stockit?
Order.stockit :limit => 5
else
supplier.orders.finished :limit => 5
end
end
# Create or destroy OrderArticle associations on create/update
def article_ids=(ids)
@ -163,7 +186,7 @@ class Order < ActiveRecord::Base
# Sets order.status to 'close' and updates all Ordergroup.account_balances
def close!(user)
raise "Bestellung wurde schon abgerechnet" if closed?
transaction_note = "Bestellung: #{supplier.name}, bis #{ends.strftime('%d.%m.%Y')}"
transaction_note = "Bestellung: #{name}, bis #{ends.strftime('%d.%m.%Y')}"
gos = group_orders.all(:include => :ordergroup) # Fetch group_orders
gos.each { |group_order| group_order.update_price! } # Update prices of group_orders

View file

@ -20,15 +20,11 @@ class StockChange < ActiveRecord::Base
validates_numericality_of :quantity
after_save :update_article_quantity
after_destroy :remove_added_quantity
after_destroy :update_article_quantity
protected
def update_article_quantity
article.update_quantity(quantity)
end
def remove_added_quantity
article.update_quantity(quantity * -1)
article.update_quantity!
end
end

View file

@ -41,11 +41,6 @@ class Supplier < ActiveRecord::Base
# for the sharedLists-App
belongs_to :shared_supplier
# Returns all articles for this supplier that are available, grouped by article category and ordered by name.
def get_articles_for_ordering
articles.available.all.group_by { |a| a.article_category.name }
end
# sync all articles with the external database
# returns an array with articles(and prices), which should be updated (to use in a form)
# also returns an array with outlisted_articles, which should be deleted