Class: Supplier
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Supplier
- Includes:
- CustomFields, MarkAsDeletedWithName
- Defined in:
- app/models/supplier.rb
Instance Attribute Summary
Attributes included from CustomFields
Class Method Summary collapse
- .ransackable_associations(_auth_object = nil) ⇒ Object
- .ransackable_attributes(_auth_object = nil) ⇒ Object
Instance Method Summary collapse
- #deleted? ⇒ Boolean
-
#has_tolerance? ⇒ Boolean
Whether there are articles that would use tolerance (unit_quantity > 1).
- #mark_as_deleted ⇒ Object
-
#shared_sync_method ⇒ Object
default value.
-
#sync_all ⇒ Object
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).
-
#sync_from_file(file, options = {}) ⇒ Object
Synchronise articles with spreadsheet.
-
#uniqueness_of_name ⇒ Object
protected
Make sure, the name is uniq, add usefull message if uniq group is already deleted.
-
#valid_shared_sync_method ⇒ Object
protected
make sure the shared_sync_method is allowed for the shared supplier.
Class Method Details
.ransackable_associations(_auth_object = nil) ⇒ Object
31 32 33 |
# File 'app/models/supplier.rb', line 31 def self.ransackable_associations(_auth_object = nil) %w[articles stock_articles orders] end |
.ransackable_attributes(_auth_object = nil) ⇒ Object
27 28 29 |
# File 'app/models/supplier.rb', line 27 def self.ransackable_attributes(_auth_object = nil) %w[id name] end |
Instance Method Details
#deleted? ⇒ Boolean
124 125 126 |
# File 'app/models/supplier.rb', line 124 def deleted? deleted_at.present? end |
#has_tolerance? ⇒ Boolean
Returns Whether there are articles that would use tolerance (unit_quantity > 1).
137 138 139 |
# File 'app/models/supplier.rb', line 137 def has_tolerance? articles.where('articles.unit_quantity > 1').any? end |
#mark_as_deleted ⇒ Object
128 129 130 131 132 133 134 |
# File 'app/models/supplier.rb', line 128 def mark_as_deleted transaction do super update_column :iban, nil articles.each(&:mark_as_deleted) end end |
#shared_sync_method ⇒ Object
default value
118 119 120 121 122 |
# File 'app/models/supplier.rb', line 118 def shared_sync_method return unless shared_supplier self[:shared_sync_method] || 'import' end |
#sync_all ⇒ Object
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)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/supplier.rb', line 39 def sync_all updated_article_pairs = [] outlisted_articles = [] new_articles = [] existing_articles = Set.new for article in articles.undeleted # try to find the associated shared_article shared_article = article.shared_article(self) if shared_article # article will be updated existing_articles.add(shared_article.id) unequal_attributes = article.shared_article_changed?(self) if unequal_attributes.present? # skip if shared_article has not been changed article.attributes = unequal_attributes updated_article_pairs << [article, unequal_attributes] end # Articles with no order number can be used to put non-shared articles # in a shared supplier, with sync keeping them. elsif article.order_number.present? # article isn't in external database anymore outlisted_articles << article end end # Find any new articles, unless the import is manual if %w[all_available all_unavailable].include?(shared_sync_method) # build new articles shared_supplier .shared_articles .where.not(id: existing_articles.to_a) .find_each { |new_shared_article| new_articles << new_shared_article.build_new_article(self) } # make them unavailable when desired new_articles.each { |new_article| new_article.availability = false } if shared_sync_method == 'all_unavailable' end [updated_article_pairs, outlisted_articles, new_articles] end |
#sync_from_file(file, options = {}) ⇒ Object
Synchronise articles with spreadsheet.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/supplier.rb', line 81 def sync_from_file(file, = {}) all_order_numbers = [] updated_article_pairs = [] outlisted_articles = [] new_articles = [] FoodsoftFile.parse file, do |status, new_attrs, line| article = articles.undeleted.where(order_number: new_attrs[:order_number]).first new_attrs[:article_category] = ArticleCategory.find_match(new_attrs[:article_category]) new_attrs[:tax] ||= FoodsoftConfig[:tax_default] new_article = articles.build(new_attrs) if status.nil? if article.nil? new_articles << new_article else unequal_attributes = article.unequal_attributes(new_article, .slice(:convert_units)) unless unequal_attributes.empty? article.attributes = unequal_attributes updated_article_pairs << [article, unequal_attributes] end end elsif status == :outlisted && article.present? outlisted_articles << article # stop when there is a parsing error elsif status.is_a? String # @todo move I18n key to model raise I18n.t('articles.model.error_parse', msg: status, line: line.to_s) end all_order_numbers << article.order_number if article end outlisted_articles += articles.undeleted.where.not(order_number: all_order_numbers + [nil]) if [:outlist_absent] [updated_article_pairs, outlisted_articles, new_articles] end |
#uniqueness_of_name ⇒ Object (protected)
Make sure, the name is uniq, add usefull message if uniq group is already deleted
151 152 153 154 155 156 157 158 |
# File 'app/models/supplier.rb', line 151 def uniqueness_of_name supplier = Supplier.where(name: name) supplier = supplier.where.not(id: id) unless new_record? return unless supplier.exists? = supplier.first.deleted? ? :taken_with_deleted : :taken errors.add :name, end |
#valid_shared_sync_method ⇒ Object (protected)
make sure the shared_sync_method is allowed for the shared supplier
144 145 146 147 148 |
# File 'app/models/supplier.rb', line 144 def valid_shared_sync_method return unless shared_supplier && !shared_supplier.shared_sync_methods.include?(shared_sync_method) errors.add :shared_sync_method, :included end |