Class: Supplier

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveModel::MassAssignmentSecurity, CustomFields, MarkAsDeletedWithName
Defined in:
app/models/supplier.rb

Instance Attribute Summary

Attributes included from CustomFields

#custom_fields

Instance Method Summary collapse

Instance Method Details

#deleted?Boolean


111
112
113
# File 'app/models/supplier.rb', line 111

def deleted?
  deleted_at.present?
end

#has_tolerance?Boolean


123
124
125
# File 'app/models/supplier.rb', line 123

def has_tolerance?
  articles.where('articles.unit_quantity > 1').any?
end

#mark_as_deletedObject


115
116
117
118
119
120
# File 'app/models/supplier.rb', line 115

def mark_as_deleted
  transaction do
    super
    articles.each(&:mark_as_deleted)
  end
end

#shared_sync_methodObject

default value


106
107
108
109
# File 'app/models/supplier.rb', line 106

def shared_sync_method
  return unless shared_supplier
  self[:shared_sync_method] || 'import'
end

#sync_allObject

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)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/supplier.rb', line 33

def sync_all
  updated_article_pairs, outlisted_articles, new_articles = [], [], []
  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
      unequal_attributes = article.shared_article_changed?(self)
      unless unequal_attributes.blank? # 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 not article.order_number.blank?
      # article isn't in external database anymore
      outlisted_articles << article
    end
  end
  # 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_article_pairs, outlisted_articles, new_articles]
end

#sync_from_file(file, options = {}) ⇒ Object

Synchronise articles with spreadsheet.

Options Hash (options):

  • :outlist_absent (Boolean)

    Set to true to remove articles not in spreadsheet.

  • :convert_units (Boolean)

    Omit or set to true to keep current units, recomputing unit quantity and price.


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/supplier.rb', line 69

def sync_from_file(file, options={})
  all_order_numbers = []
  updated_article_pairs, outlisted_articles, new_articles = [], [], []
  FoodsoftFile::parse file, options 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, options.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
  if options[:outlist_absent]
    outlisted_articles += articles.undeleted.where.not(id: all_order_numbers+[nil])
  end
  return [updated_article_pairs, outlisted_articles, new_articles]
end

#uniqueness_of_nameObject (protected)

Make sure, the name is uniq, add usefull message if uniq group is already deleted


137
138
139
140
141
142
143
144
# File 'app/models/supplier.rb', line 137

def uniqueness_of_name
  supplier = Supplier.where(name: name)
  supplier = supplier.where.not(id: self.id) unless new_record?
  if supplier.exists?
    message = supplier.first.deleted? ? :taken_with_deleted : :taken
    errors.add :name, message
  end
end

#valid_shared_sync_methodObject (protected)

make sure the shared_sync_method is allowed for the shared supplier


130
131
132
133
134
# File 'app/models/supplier.rb', line 130

def valid_shared_sync_method
  if shared_supplier && !shared_supplier.shared_sync_methods.include?(shared_sync_method)
    errors.add :name, :included
  end
end