2013-03-16 17:53:24 +01:00
|
|
|
# encoding: utf-8
|
2009-01-06 11:49:19 +01:00
|
|
|
class Supplier < ActiveRecord::Base
|
2014-02-20 15:04:53 +01:00
|
|
|
has_many :articles, -> { where(:type => nil).includes(:article_category).order('article_categories.name', 'articles.name') }
|
|
|
|
has_many :stock_articles, -> { includes(:article_category).order('article_categories.name', 'articles.name') }
|
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-02-11 15:23:59 +01:00
|
|
|
belongs_to :shared_supplier # for the sharedLists-App
|
2009-01-08 16:33:27 +01:00
|
|
|
|
2014-02-20 15:04:53 +01:00
|
|
|
include ActiveModel::MassAssignmentSecurity
|
2011-05-18 15:52:06 +02:00
|
|
|
attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number,
|
2014-05-21 21:24:03 +02:00
|
|
|
:delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity, :shared_sync_method
|
2011-05-18 15:52:06 +02:00
|
|
|
|
2013-02-24 23:26:16 +01:00
|
|
|
validates :name, :presence => true, :length => { :in => 4..30 }
|
2013-07-15 17:57:00 +02:00
|
|
|
validates :phone, :presence => true, :length => { :in => 8..25 }
|
2011-05-18 15:52:06 +02:00
|
|
|
validates :address, :presence => true, :length => { :in => 8..50 }
|
2012-10-17 20:45:52 +02:00
|
|
|
validates_length_of :order_howto, :note, maximum: 250
|
2014-05-21 21:24:03 +02:00
|
|
|
validate :valid_shared_sync_method
|
2013-02-24 23:26:16 +01:00
|
|
|
validate :uniqueness_of_name
|
2009-02-11 15:23:59 +01:00
|
|
|
|
2013-03-16 17:53:24 +01:00
|
|
|
scope :undeleted, -> { where(deleted_at: nil) }
|
|
|
|
|
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
|
2014-05-21 21:24:03 +02:00
|
|
|
# also returns an array with new articles, which should be added (depending on shared_sync_method)
|
2009-01-06 11:49:19 +01:00
|
|
|
def sync_all
|
|
|
|
updated_articles = Array.new
|
|
|
|
outlisted_articles = Array.new
|
2014-05-21 21:24:03 +02:00
|
|
|
new_articles = Array.new
|
2013-03-16 17:53:24 +01:00
|
|
|
for article in articles.undeleted
|
2009-01-06 11:49:19 +01:00
|
|
|
# try to find the associated shared_article
|
2014-05-21 21:44:35 +02:00
|
|
|
shared_article = article.shared_article(self)
|
2009-02-10 13:26:10 +01:00
|
|
|
|
|
|
|
if shared_article # article will be updated
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2014-05-21 21:44:35 +02:00
|
|
|
unequal_attributes = article.shared_article_changed?(self)
|
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
|
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
|
2014-01-24 22:10:00 +01:00
|
|
|
# Articles with no order number can be used to put non-shared articles
|
|
|
|
# in a shared supplier, with sync keeping them.
|
|
|
|
elsif not article.order_number.blank?
|
2009-01-06 11:49:19 +01:00
|
|
|
# article isn't in external database anymore
|
|
|
|
outlisted_articles << article
|
|
|
|
end
|
|
|
|
end
|
2014-05-21 21:24:03 +02:00
|
|
|
# Find any new articles, unless the import is manual
|
|
|
|
unless shared_sync_method == 'import'
|
|
|
|
for shared_article in shared_supplier.shared_articles
|
|
|
|
unless articles.undeleted.find_by_order_number(shared_article.number)
|
|
|
|
new_articles << shared_article.build_new_article(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return [updated_articles, outlisted_articles, new_articles]
|
|
|
|
end
|
|
|
|
|
|
|
|
# default value
|
|
|
|
def shared_sync_method
|
|
|
|
return unless shared_supplier
|
|
|
|
self[:shared_sync_method] || 'import'
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2013-02-24 23:26:16 +01:00
|
|
|
|
2013-03-16 17:53:24 +01:00
|
|
|
def deleted?
|
|
|
|
deleted_at.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_as_deleted
|
|
|
|
transaction do
|
|
|
|
update_column :deleted_at, Time.now
|
|
|
|
articles.each(&:mark_as_deleted)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-24 23:26:16 +01:00
|
|
|
protected
|
|
|
|
|
2014-05-21 21:24:03 +02:00
|
|
|
# make sure the shared_sync_method is allowed for the shared supplier
|
|
|
|
def valid_shared_sync_method
|
|
|
|
if shared_supplier and !shared_supplier.shared_sync_methods.include?(shared_sync_method)
|
|
|
|
errors.add :name, :included
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-24 23:26:16 +01:00
|
|
|
# Make sure, the name is uniq, add usefull message if uniq group is already deleted
|
|
|
|
def uniqueness_of_name
|
2014-02-20 15:04:53 +01:00
|
|
|
supplier = Supplier.where(name: name)
|
|
|
|
supplier = supplier.where.not(id: self.id) unless new_record?
|
2013-07-15 18:03:02 +02:00
|
|
|
if supplier.exists?
|
|
|
|
message = supplier.first.deleted? ? :taken_with_deleted : :taken
|
2013-02-24 23:26:16 +01:00
|
|
|
errors.add :name, message
|
|
|
|
end
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-05-07 21:55:24 +02:00
|
|
|
|