foodsoft/app/models/supplier.rb

71 lines
2.8 KiB
Ruby
Raw Normal View History

2009-01-06 11:49:19 +01:00
class Supplier < ActiveRecord::Base
acts_as_paranoid # Avoid deleting the supplier for consistency of order-results
2009-02-10 13:26:10 +01:00
has_many :articles, :dependent => :destroy, :conditions => {:type => nil},
:include => [:article_category], :order => 'article_categories.name, articles.name'
2009-02-10 13:26:10 +01:00
has_many :stock_articles, :include => [:article_category], :order => 'article_categories.name, articles.name'
2009-01-06 11:49:19 +01:00
has_many :orders
has_many :deliveries
has_many :invoices
belongs_to :shared_supplier # for the sharedLists-App
2011-05-18 15:52:06 +02:00
attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number,
:delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity
validates :name, :presence => true, :length => { :in => 4..30 }, :uniqueness => true
validates :phone, :presence => true, :length => { :in => 8..20 }
validates :address, :presence => true, :length => { :in => 8..50 }
2012-10-17 20:45:52 +02:00
validates_length_of :order_howto, :note, maximum: 250
2011-05-18 15:52:06 +02:00
# validates_length_of :name, :in => 4..30
# validates_uniqueness_of :name
2009-01-06 11:49:19 +01:00
validates_length_of :phone, :in => 8..20
validates_length_of :address, :in => 8..50
2009-01-06 11:49:19 +01:00
# 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
def sync_all
updated_articles = Array.new
outlisted_articles = Array.new
for article in articles
2009-01-06 11:49:19 +01:00
# try to find the associated shared_article
shared_article = article.shared_article
2009-02-10 13:26:10 +01:00
if shared_article # article will be updated
2009-01-06 11:49:19 +01:00
unequal_attributes = article.shared_article_changed?
2009-02-10 13:26:10 +01:00
unless unequal_attributes.blank? # skip if shared_article has not been changed
2009-01-06 11:49:19 +01:00
# try to convert different units
new_price, new_unit_quantity = article.convert_units
if new_price and new_unit_quantity
article.price = new_price
2009-01-06 11:49:19 +01:00
article.unit_quantity = new_unit_quantity
else
article.price = shared_article.price
2009-01-06 11:49:19 +01:00
article.unit_quantity = shared_article.unit_quantity
article.unit = shared_article.unit
end
# update other attributes
article.attributes = {
:name => shared_article.name,
:manufacturer => shared_article.manufacturer,
:origin => shared_article.origin,
:shared_updated_on => shared_article.updated_on,
:tax => shared_article.tax,
:deposit => shared_article.deposit,
:note => shared_article.note
}
updated_articles << [article, unequal_attributes]
end
else
# article isn't in external database anymore
outlisted_articles << article
end
end
return [updated_articles, outlisted_articles]
end
end