2019-01-13 07:05:54 +01:00
|
|
|
class SharedSupplier < ApplicationRecord
|
2011-05-07 20:50:39 +02:00
|
|
|
# connect to database from sharedLists-Application
|
2012-08-24 19:52:38 +02:00
|
|
|
SharedSupplier.establish_connection(FoodsoftConfig[:shared_lists])
|
2011-05-07 20:50:39 +02:00
|
|
|
# set correct table_name in external DB
|
2012-10-19 01:12:47 +02:00
|
|
|
self.table_name = 'suppliers'
|
|
|
|
|
2019-06-13 10:41:16 +02:00
|
|
|
has_many :suppliers, -> { undeleted }
|
2023-05-12 13:01:12 +02:00
|
|
|
has_many :shared_articles, foreign_key: :supplier_id
|
2013-06-09 18:02:47 +02:00
|
|
|
|
2019-01-15 02:56:21 +01:00
|
|
|
def find_article_by_number(order_number)
|
2023-05-12 13:01:12 +02:00
|
|
|
# NOTE: that `shared_articles` uses number instead order_number
|
2019-01-15 02:56:21 +01:00
|
|
|
cached_articles.detect { |a| a.number == order_number }
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_articles
|
|
|
|
@cached_articles ||= shared_articles.all
|
|
|
|
end
|
|
|
|
|
2013-06-09 18:02:47 +02:00
|
|
|
# These set of attributes are used to autofill attributes of new supplier,
|
|
|
|
# when created by import from shared supplier feature.
|
|
|
|
def autofill_attributes
|
2023-05-12 13:01:12 +02:00
|
|
|
whitelist = %w[name address phone fax email url delivery_days note]
|
2021-03-01 15:27:26 +01:00
|
|
|
attributes.select { |k, _v| whitelist.include?(k) }
|
2013-06-09 18:02:47 +02:00
|
|
|
end
|
2014-05-21 21:24:03 +02:00
|
|
|
|
|
|
|
# return list of synchronisation methods available for this supplier
|
|
|
|
def shared_sync_methods
|
|
|
|
methods = []
|
2023-05-12 13:01:12 +02:00
|
|
|
if shared_articles.count < FoodsoftConfig[:shared_supplier_article_sync_limit]
|
|
|
|
methods += %w[all_available
|
|
|
|
all_unavailable]
|
|
|
|
end
|
|
|
|
methods += %w[import]
|
2014-05-21 21:24:03 +02:00
|
|
|
methods
|
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
end
|