cirandas.net

ref: master

plugins/suppliers/lib/ext/profile.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require_dependency 'profile'
require_dependency 'community'

class Profile

  def create_product?
    true
  end

  # use profile.products.supplied to include own products
  has_many :distributed_products, class_name: 'SuppliersPlugin::DistributedProduct', foreign_key: :profile_id

  has_many :from_products, through: :products
  has_many :to_products, through: :products

  has_many :suppliers, -> {
    order('name ASC')
  }, class_name: 'SuppliersPlugin::Supplier', foreign_key: :consumer_id, dependent: :destroy
  has_many :consumers, -> {
    order('name ASC')
  }, class_name: 'SuppliersPlugin::Consumer', foreign_key: :profile_id, dependent: :destroy

  has_many :hubs, class_name: 'SuppliersPlugin::Hub', dependent: :destroy

  def supplier_settings
    @supplier_settings ||= SuppliersPlugin::Settings.new self
  end

  def dummy?
    !self.visible
  end

  def orgs_consumers
    @orgs_consumers ||= self.consumers.except_people.except_self
  end

  def self_supplier
    @self_supplier ||= if new_record?
      self.suppliers_without_self_supplier.build profile: self
    else
      suppliers_without_self_supplier.select{ |s| s.profile_id == s.consumer_id }.first || self.suppliers_without_self_supplier.create(profile: self)
    end
  end
  def suppliers_with_self_supplier
    self_supplier # guarantee that the self_supplier is created
    suppliers_without_self_supplier
  end
  alias_method_chain :suppliers, :self_supplier

  def add_consumer consumer_profile
    consumer = self.consumers.where(consumer_id: consumer_profile.id).first
    consumer ||= self.consumers.create! profile: self, consumer: consumer_profile
  end
  def remove_consumer consumer_profile
    consumer = self.consumers.of_consumer(consumer_profile).first
    consumer.destroy if consumer
  end

  def add_supplier supplier_profile, attrs={}
    supplier = self.suppliers.where(profile_id: supplier_profile.id).first
    supplier ||= self.suppliers.create! attrs.merge(profile: supplier_profile, consumer: self)
  end
  def remove_supplier supplier_profile
    supplier_profile.remove_consumer self
  end

  def not_distributed_products supplier
    raise "'#{supplier.name}' is not a supplier of #{self.name}" if self.suppliers.of_profile(supplier).blank?

    # FIXME: only select all products if supplier is dummy
    supplier.profile.products.unarchived.own - self.from_products.unarchived.by_profile(supplier.profile)
  end

  delegate :margin_percentage, :margin_percentage=, to: :supplier_settings
  extend CurrencyFields::ClassMethods
  has_number_with_locale :margin_percentage

  def supplier_products_default_margins
    self.class.transaction do
      self.distributed_products.unarchived.each do |product|
        product.default_margin_percentage = true
        product.save!
      end
    end
  end
end