allow to synchronize all articles of a shared supplier
This commit is contained in:
parent
3918e22214
commit
647b7f0430
16 changed files with 194 additions and 77 deletions
|
|
@ -59,7 +59,9 @@ class Article < ActiveRecord::Base
|
|||
validates_numericality_of :price, :greater_than_or_equal_to => 0
|
||||
validates_numericality_of :unit_quantity, :greater_than => 0
|
||||
validates_numericality_of :deposit, :tax
|
||||
validates_uniqueness_of :name, :scope => [:supplier_id, :deleted_at, :type]
|
||||
#validates_uniqueness_of :name, :scope => [:supplier_id, :deleted_at, :type], if: Proc.new {|a| a.supplier.shared_sync_method.blank? or a.supplier.shared_sync_method == 'import' }
|
||||
#validates_uniqueness_of :name, :scope => [:supplier_id, :deleted_at, :type, :unit, :unit_quantity]
|
||||
validate :uniqueness_of_name
|
||||
|
||||
# Callbacks
|
||||
before_save :update_price_history
|
||||
|
|
@ -214,4 +216,18 @@ class Article < ActiveRecord::Base
|
|||
def price_changed?
|
||||
changed.detect { |attr| attr == 'price' || 'tax' || 'deposit' || 'unit_quantity' } ? true : false
|
||||
end
|
||||
|
||||
# We used have the name unique per supplier+deleted_at+type. With the addition of shared_sync_method all,
|
||||
# this came in the way, and we now allow duplicate names for the 'all' methods - expecting foodcoops to
|
||||
# make their own choice among products with different units by making articles available/unavailable.
|
||||
def uniqueness_of_name
|
||||
matches = Article.where(name: name, supplier_id: supplier_id, deleted_at: deleted_at, type: type)
|
||||
matches = matches.where.not(id: id) unless new_record?
|
||||
if supplier.shared_sync_method.blank? or supplier.shared_sync_method == 'import'
|
||||
errors.add :name, :taken if matches.any?
|
||||
else
|
||||
errors.add :name, :taken_with_unit if matches.where(unit: unit, unit_quantity: unit_quantity).any?
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -14,5 +14,13 @@ class SharedSupplier < ActiveRecord::Base
|
|||
whitelist = %w(name address phone fax email url delivery_days note)
|
||||
attributes.select { |k,_v| whitelist.include?(k) }
|
||||
end
|
||||
|
||||
# return list of synchronisation methods available for this supplier
|
||||
def shared_sync_methods
|
||||
methods = []
|
||||
methods += %w(all_available all_unavailable) if shared_articles.count < 200
|
||||
methods += %w(import) # perhaps, in the future: if shared_articles.count > 20
|
||||
methods
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -9,12 +9,13 @@ class Supplier < ActiveRecord::Base
|
|||
|
||||
include ActiveModel::MassAssignmentSecurity
|
||||
attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number,
|
||||
:delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity
|
||||
:delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity, :shared_sync_method
|
||||
|
||||
validates :name, :presence => true, :length => { :in => 4..30 }
|
||||
validates :phone, :presence => true, :length => { :in => 8..25 }
|
||||
validates :address, :presence => true, :length => { :in => 8..50 }
|
||||
validates_length_of :order_howto, :note, maximum: 250
|
||||
validate :valid_shared_sync_method
|
||||
validate :uniqueness_of_name
|
||||
|
||||
scope :undeleted, -> { where(deleted_at: nil) }
|
||||
|
|
@ -22,9 +23,11 @@ class Supplier < ActiveRecord::Base
|
|||
# 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
|
||||
# also returns an array with new articles, which should be added (depending on shared_sync_method)
|
||||
def sync_all
|
||||
updated_articles = Array.new
|
||||
outlisted_articles = Array.new
|
||||
new_articles = Array.new
|
||||
for article in articles.undeleted
|
||||
# try to find the associated shared_article
|
||||
shared_article = article.shared_article
|
||||
|
|
@ -63,7 +66,21 @@ class Supplier < ActiveRecord::Base
|
|||
outlisted_articles << article
|
||||
end
|
||||
end
|
||||
return [updated_articles, outlisted_articles]
|
||||
# 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'
|
||||
end
|
||||
|
||||
def deleted?
|
||||
|
|
@ -79,6 +96,13 @@ class Supplier < ActiveRecord::Base
|
|||
|
||||
protected
|
||||
|
||||
# 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
|
||||
|
||||
# Make sure, the name is uniq, add usefull message if uniq group is already deleted
|
||||
def uniqueness_of_name
|
||||
supplier = Supplier.where(name: name)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue