Order refactoring part two: Balancing workflow was adapted to the new order schema. Article modification is still missing.

This commit is contained in:
Benjamin Meichsner 2009-01-29 21:28:22 +01:00
parent 9eb2125f15
commit 190a777278
53 changed files with 568 additions and 603 deletions

View file

@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20090102171850
# Schema version: 20090120184410
#
# Table name: groups
#
@ -21,6 +21,7 @@
# task_name :string(255)
# task_description :string(255)
# task_required_users :integer(4) default(1)
# deleted_at :datetime
#
# Groups organize the User.

View file

@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20090114101610
# Schema version: 20090120184410
#
# Table name: group_orders
#
@ -9,7 +9,7 @@
# price :decimal(8, 2) default(0.0), not null
# lock_version :integer(4) default(0), not null
# updated_on :datetime not null
# updated_by_user_id :integer(4) default(0), not null
# updated_by_user_id :integer(4)
#
# A GroupOrder represents an Order placed by an Ordergroup.
@ -23,7 +23,6 @@ class GroupOrder < ActiveRecord::Base
validates_presence_of :order_id
validates_presence_of :ordergroup_id
validates_presence_of :updated_by
validates_numericality_of :price
validates_uniqueness_of :ordergroup_id, :scope => :order_id # order groups can only order once per order

View file

@ -20,12 +20,13 @@ class GroupOrderArticle < ActiveRecord::Base
belongs_to :order_article
has_many :group_order_article_quantities, :dependent => :destroy
validates_presence_of :group_order_id
validates_presence_of :order_article_id
validates_presence_of :group_order_id, :order_article_id
validates_inclusion_of :quantity, :in => 0..99
validates_inclusion_of :tolerance, :in => 0..99
validates_uniqueness_of :order_article_id, :scope => :group_order_id # just once an article per group order
attr_accessor :ordergroup_id # To create an new GroupOrder if neccessary
# Updates the quantity/tolerance for this GroupOrderArticle by updating both GroupOrderArticle properties
# and the associated GroupOrderArticleQuantities chronologically.
#

View file

@ -43,4 +43,9 @@ class Invoice < ActiveRecord::Base
def deposit_credit=(deposit)
self[:deposit_credit] = String.delocalized_decimal(deposit)
end
# Amount without deposit
def net_amount
amount - deposit + deposit_credit
end
end

View file

@ -82,9 +82,13 @@ class Order < ActiveRecord::Base
memoize :get_articles
# Returns the defecit/benefit for the foodcoop
def profit(with_markup = true)
groups_sum = with_markup ? sum(:groups) : sum(:groups_without_markup)
groups_sum - invoice_amount + deposit - deposit_credit
# Requires a valid invoice, belonging to this order
def profit(options = {})
markup = options[:with_markup] || true
if invoice
groups_sum = markup ? sum(:groups) : sum(:groups_without_markup)
groups_sum - invoice.net_amount
end
end
# Returns the all round price of a finished order

View file

@ -24,19 +24,39 @@ class OrderArticle < ActiveRecord::Base
validates_presence_of :order_id
validates_presence_of :article_id
validates_uniqueness_of :article_id, :scope => :order_id # an article can only have one record per order
validate :article_and_price_exist
named_scope :ordered, :conditions => "units_to_order >= 1"
# TODO: How to create/update articles/article_prices during balancing
# # Accessors for easy create of new order_articles in balancing process
# attr_accessor :name, :order_number, :units_to_order, :unit_quantity, :unit, :net_price, :tax, :deposit
#
# before_validation_on_create :create_new_article
# This method returns either the Article or the ArticlePrice
# The latter will be set, when the the order is finished
def price
article_price || article
end
# Count quantities of belonging group_orders.
# In balancing this can differ from ordered (by supplier) quantity for this article.
def group_orders_sum
quantity = group_order_articles.collect(&:quantity).sum
{:quantity => quantity, :price => quantity * price.fc_price}
end
private
def validate
errors.add(:article, "muss angegeben sein und einen aktuellen Preis haben") if !(article = Article.find(article_id)) || article.fc_price.nil?
end
def article_and_price_exist
errors.add(:article, "muss angegeben sein und einen aktuellen Preis haben") if !(article = Article.find(article_id)) || article.fc_price.nil?
end
# def create_new_article
# old_article = order.articles.find_by_name(name) # Check if there is already an Article with this name
# unless old_article
# self.article.build
# end
# end
end

View file

@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20090102171850
# Schema version: 20090120184410
#
# Table name: groups
#
@ -21,6 +21,7 @@
# task_name :string(255)
# task_description :string(255)
# task_required_users :integer(4) default(1)
# deleted_at :datetime
#
# Ordergroups can order, they are "children" of the class Group

View file

@ -151,7 +151,12 @@ class User < ActiveRecord::Base
def find_ordergroup
ordergroups.first
end
def ordergroup_name
ordergroup = find_ordergroup
ordergroup ? ordergroup.name : "keine Bestellgruppe"
end
# Find all tasks, for which the current user should be responsible
# but which aren't accepted yet
def unaccepted_tasks

View file

@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20090113111624
# Schema version: 20090120184410
#
# Table name: groups
#
@ -21,6 +21,7 @@
# task_name :string(255)
# task_description :string(255)
# task_required_users :integer(4) default(1)
# deleted_at :datetime
#
class Workgroup < Group