2009-01-08 16:33:27 +01:00
|
|
|
# == Schema Information
|
2009-01-20 19:37:15 +01:00
|
|
|
# Schema version: 20090119155930
|
2009-01-08 16:33:27 +01:00
|
|
|
#
|
|
|
|
# Table name: suppliers
|
|
|
|
#
|
2009-02-02 16:35:43 +01:00
|
|
|
# id :integer not null, primary key
|
2009-01-08 16:33:27 +01:00
|
|
|
# name :string(255) default(""), not null
|
|
|
|
# address :string(255) default(""), not null
|
|
|
|
# phone :string(255) default(""), not null
|
|
|
|
# phone2 :string(255)
|
|
|
|
# fax :string(255)
|
|
|
|
# email :string(255)
|
|
|
|
# url :string(255)
|
|
|
|
# contact_person :string(255)
|
|
|
|
# customer_number :string(255)
|
|
|
|
# delivery_days :string(255)
|
|
|
|
# order_howto :string(255)
|
|
|
|
# note :string(255)
|
2009-02-02 16:35:43 +01:00
|
|
|
# shared_supplier_id :integer
|
2009-01-08 16:33:27 +01:00
|
|
|
# min_order_quantity :string(255)
|
2009-01-20 19:37:15 +01:00
|
|
|
# deleted_at :datetime
|
2009-01-08 16:33:27 +01:00
|
|
|
#
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
class Supplier < ActiveRecord::Base
|
2009-01-20 19:37:15 +01:00
|
|
|
acts_as_paranoid # Avoid deleting the supplier for consistency of order-results
|
|
|
|
|
2009-02-04 16:41:01 +01:00
|
|
|
has_many :articles, :dependent => :destroy,
|
|
|
|
:include => [:article_category], :order => 'article_categories.name, articles.name'
|
2009-02-06 16:26:35 +01:00
|
|
|
has_many :stock_articles
|
2009-01-06 11:49:19 +01:00
|
|
|
has_many :orders
|
2009-01-08 16:33:27 +01:00
|
|
|
has_many :deliveries
|
|
|
|
has_many :invoices
|
|
|
|
|
2009-01-06 11:49:19 +01: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_length_of :name, :in => 4..30
|
|
|
|
validates_uniqueness_of :name
|
|
|
|
|
|
|
|
validates_length_of :phone, :in => 8..20
|
|
|
|
validates_length_of :address, :in => 8..50
|
|
|
|
|
|
|
|
# for the sharedLists-App
|
|
|
|
belongs_to :shared_supplier
|
|
|
|
|
|
|
|
# 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
|
2009-02-04 16:41:01 +01:00
|
|
|
for article in articles
|
2009-01-06 11:49:19 +01:00
|
|
|
# try to find the associated shared_article
|
|
|
|
shared_article = article.shared_article
|
|
|
|
if shared_article
|
|
|
|
# article will be updated
|
|
|
|
|
|
|
|
# skip if shared_article has not been changed
|
|
|
|
unequal_attributes = article.shared_article_changed?
|
|
|
|
unless unequal_attributes.blank?
|
|
|
|
# update objekt but don't save it
|
|
|
|
|
|
|
|
# try to convert different units
|
|
|
|
new_price, new_unit_quantity = article.convert_units
|
|
|
|
if new_price and new_unit_quantity
|
2009-01-29 01:57:51 +01:00
|
|
|
article.price = new_price
|
2009-01-06 11:49:19 +01:00
|
|
|
article.unit_quantity = new_unit_quantity
|
|
|
|
else
|
2009-01-29 01:57:51 +01:00
|
|
|
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
|