Class: BuyingGroup

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/buying_group.rb

Overview

== Schema Information

Table name: buying_groups
Database name: primary

id :integer not null, primary key
conglomerate :string(20)
inactive :boolean default(FALSE), not null
jde_code_3 :string(255)
name :string(255)
report_grouping :string(255)
sales_watch :boolean default(FALSE), not null
created_at :datetime
updated_at :datetime
company_id :integer
corporate_account_id :integer
legacy_tier2_program_pricing_id :integer
tier2_program_pricing_id :integer

Indexes

buying_groups_company_id_idx (company_id)
buying_groups_tier2_program_pricing_id_idx (tier2_program_pricing_id)

Foreign Keys

buying_groups_company_id_fkey (company_id => companies.id)
buying_groups_tier2_program_pricing_id_fk (tier2_program_pricing_id => coupons.id)

Constant Summary collapse

DIRECT_BUY_CA_ID =
60
DIRECT_BUY_USA_ID =
17

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#company_idObject (readonly)



48
# File 'app/models/buying_group.rb', line 48

validates :company_id, :tier2_program_pricing_id, presence: true

#nameObject (readonly)



47
# File 'app/models/buying_group.rb', line 47

validates :name, presence: true, uniqueness: { case_sensitive: false }

#push_pricing_to_customersObject

Returns the value of attribute push_pricing_to_customers.



56
57
58
# File 'app/models/buying_group.rb', line 56

def push_pricing_to_customers
  @push_pricing_to_customers
end

#tier2_program_pricing_idObject (readonly)



48
# File 'app/models/buying_group.rb', line 48

validates :company_id, :tier2_program_pricing_id, presence: true

Class Method Details

.activeActiveRecord::Relation<BuyingGroup>

A relation of BuyingGroups that are active. Active Record Scope

Returns:

See Also:



45
# File 'app/models/buying_group.rb', line 45

scope :active, -> { where(inactive: false).order(:name) }

.by_conglomerateActiveRecord::Relation<BuyingGroup>

A relation of BuyingGroups that are by conglomerate. Active Record Scope

Returns:

See Also:



41
42
43
44
# File 'app/models/buying_group.rb', line 41

scope :by_conglomerate, ->(buying_group) {
  where('(buying_groups.conglomerate = ? and buying_groups.conglomerate IS NOT NULL) or buying_groups.id = ?', buying_group.conglomerate, buying_group.id)
    .order("buying_groups.id = #{buying_group.id} DESC, buying_groups.name".sql_safe)
}

.options_for_select(options = {}) ⇒ Object



92
93
94
95
96
97
98
# File 'app/models/buying_group.rb', line 92

def self.options_for_select(options = {})
  buying_groups = options[:include_inactive] ? BuyingGroup.all : BuyingGroup.active
  buying_groups = buying_groups.where(company_id: options[:company_id]) if options[:company_id]
  buying_groups_array = buying_groups.pluck(:name, (options[:use_for_id] || :id))
  buying_groups_array += BuyingGroup.where(id: options[:include_id]).pluck(:name, (options[:use_for_id] || :id)) if options[:include_id]
  buying_groups_array.uniq.sort_by(&:first)
end

.options_for_select_conglomerated(buying_group_id) ⇒ Object



100
101
102
103
# File 'app/models/buying_group.rb', line 100

def self.options_for_select_conglomerated(buying_group_id)
  selected_bg = BuyingGroup.find(buying_group_id)
  by_conglomerate(selected_bg).pluck(:name, :id)
end

Instance Method Details

#companyCompany

Returns:

See Also:



37
# File 'app/models/buying_group.rb', line 37

belongs_to :company, optional: true

#corporate_accountObject



72
73
74
# File 'app/models/buying_group.rb', line 72

def 
   ? Customer.find() : nil
end

#customersActiveRecord::Relation<Customer>

Returns:

See Also:



38
# File 'app/models/buying_group.rb', line 38

has_many :customers

#invoicesActiveRecord::Relation<Invoice>

Returns:

  • (ActiveRecord::Relation<Invoice>)

See Also:



39
# File 'app/models/buying_group.rb', line 39

has_many :invoices

#is_direct_buy?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/buying_group.rb', line 76

def is_direct_buy?
  is_direct_buy_usa? or is_direct_buy_can?
end

#is_direct_buy_can?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'app/models/buying_group.rb', line 84

def is_direct_buy_can?
  id == DIRECT_BUY_CA_ID
end

#is_direct_buy_usa?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/models/buying_group.rb', line 80

def is_direct_buy_usa?
  id == DIRECT_BUY_USA_ID
end

#pricing_program_unassigned?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/models/buying_group.rb', line 58

def pricing_program_unassigned?
  tier2_program_pricing == Coupon.find_by(code: 'PP-LEVEL-1')
end

#send_bg_created_notificationObject



62
63
64
# File 'app/models/buying_group.rb', line 62

def send_bg_created_notification
  InternalMailer.new_buying_group_created(self).deliver_later
end

#set_default_pricing_programObject



118
119
120
# File 'app/models/buying_group.rb', line 118

def set_default_pricing_program
  self.tier2_program_pricing = Coupon.find_by(code: 'PP-LEVEL-1') if tier2_program_pricing.nil?
end

#sync_pricing_on_customersObject



66
67
68
69
70
# File 'app/models/buying_group.rb', line 66

def sync_pricing_on_customers
  customers.each do |c|
    c.update(tier2_program_pricing_id: tier2_program_pricing_id)
  end
end

#sync_report_groupingObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/buying_group.rb', line 105

def sync_report_grouping
  Rails.logger.info "Synching report grouping to invoices for buying group id #{id} #{name}"
  rg = report_grouping.present? ? "'#{report_grouping}'" : 'null'
  sql = <<-EOS
    update invoices
    set report_grouping = #{rg}
    from parties c
    where invoices.customer_id = c.id
    and c.buying_group_id = #{id};
  EOS
  ActiveRecord::Base.connection.execute(sql)
end

#tier2_program_pricingCoupon::Tier2ProgramPricing



36
# File 'app/models/buying_group.rb', line 36

belongs_to :tier2_program_pricing, class_name: 'Coupon::Tier2ProgramPricing', optional: true

#to_sObject



88
89
90
# File 'app/models/buying_group.rb', line 88

def to_s
  name
end