Class: ViewOpportunity

Inherits:
ApplicationViewRecord show all
Includes:
Models::SearchableView
Defined in:
app/models/view_opportunity.rb

Overview

== Schema Information

Table name: view_opportunities
Database name: primary

id :integer primary key
adequate_sales_follow_up :boolean
awaiting_iq_transmission :boolean
close_date :date
closed_sales_activity :bigint
company_name :string(255)
completed_sales_activity :bigint
contact_full_name :string(255)
contactable :boolean
customer_full_name :string(255)
customer_local_sales_rep_full_name :string(255)
email :string(255)
last_activity_notes :text
last_quote :integer
last_sales_activity_completed_on :datetime
latitude :decimal(9, 6)
local_sales_rep_full_name :string(255)
longitude :decimal(9, 6)
media :string(255)
name :string(80)
open_sales_activity :bigint
opportunity_local_sales_rep_full_name :string(255)
opportunity_reception_type :string(255)
opportunity_type :string(1)
phone :string(255)
planned_installation_date :datetime
pricing_program :text
primary_sales_rep_full_name :string(255)
report_grouping :string
sales_priority_index :integer
secondary_sales_rep_full_name :string(255)
ship_to_state :string(255)
ship_to_state_code :text
source_full_name :string(500)
state :string(255)
technical_support_rep_full_name :string(255)
value :decimal(, )
won_lost_date :datetime
created_at :datetime
updated_at :datetime
company_id :integer
contact_id :integer
customer_id :integer
customer_local_sales_rep_id :integer
local_sales_rep_id :integer
next_sales_activity_id :integer
opportunity_local_sales_rep_id :integer
primary_sales_rep_id :integer
secondary_sales_rep_id :integer
source_id :integer
technical_support_rep_id :integer

Belongs to collapse

Has many collapse

Delegated Instance Attributes collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::SearchableView

#crm_link, #crm_link_subtitle, #has_columns?, #main_resource, #readonly?

Methods inherited from ApplicationViewRecord

create, create!, #readonly?

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.contains_product_line_idActiveRecord::Relation<ViewOpportunity>

A relation of ViewOpportunities that are contains product line id. Active Record Scope

Returns:

See Also:



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
# File 'app/models/view_opportunity.rb', line 74

scope :contains_product_line_id, ->(*product_line_ids) {
  ids = product_line_ids.flatten.uniq.map(&:presence).compact.map(&:to_i)
  return none if ids.empty?

  # Use ltree descendant check: primary_pl_path_ids <@ ANY(paths from given IDs)
  pl_sql = %{
    EXISTS(SELECT 1
           FROM line_items li
           INNER JOIN items i ON li.item_id = i.id
             AND i.primary_pl_path_ids <@ ANY(SELECT ltree_path_ids FROM product_lines WHERE id = ANY(ARRAY[?]::integer[]))
           INNER JOIN orders ord ON li.resource_id = ord.id AND li.resource_type = 'Order'
           WHERE ord.opportunity_id = view_opportunities.id)
    OR
    EXISTS(SELECT 1
           FROM line_items li
           INNER JOIN items i ON li.item_id = i.id
             AND i.primary_pl_path_ids <@ ANY(SELECT ltree_path_ids FROM product_lines WHERE id = ANY(ARRAY[?]::integer[]))
           INNER JOIN quotes quo ON li.resource_id = quo.id AND li.resource_type = 'Quote'
           WHERE quo.opportunity_id = view_opportunities.id)
    OR
    EXISTS(SELECT 1
           FROM line_items li
           INNER JOIN items i ON li.item_id = i.id
             AND i.primary_pl_path_ids <@ ANY(SELECT ltree_path_ids FROM product_lines WHERE id = ANY(ARRAY[?]::integer[]))
           INNER JOIN room_configurations rc ON li.resource_id = rc.id AND li.resource_type = 'RoomConfiguration'
           WHERE rc.opportunity_id = view_opportunities.id)
  }
  where(pl_sql, ids, ids, ids)
}

.ransackable_scopes(_auth_object = nil) ⇒ Object



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

def self.ransackable_scopes(_auth_object = nil)
  super + %i[source_id contains_product_line_id]
end

.source_idActiveRecord::Relation<ViewOpportunity>

A relation of ViewOpportunities that are source id. Active Record Scope

Returns:

See Also:



67
68
69
70
71
72
# File 'app/models/view_opportunity.rb', line 67

scope :source_id, ->(*sids) {
  sids = sids.flatten.compact
  next all if sids.blank?

  where(source_id: Source.self_and_descendants_ids(sids))
}

Instance Method Details

#callable_contact_points_arrayObject



111
112
113
114
115
116
117
118
119
120
# File 'app/models/view_opportunity.rb', line 111

def callable_contact_points_array
  ar = []
  ar += ContactPoint.where(party_id: customer_id).voice_callable.order(:position)
  # ar += contact_contact_points.voice_callable.order(:position)
  c = Party.where(customer_id: customer_id).map{ |r| r.id }
  ContactPoint.where(party_id: c).voice_callable.order(:position).each do |ccp|
    ar << ccp unless ar.detect{|cp| cp.detail == ccp.detail}
  end
  ar
end

#contact_contact_pointsActiveRecord::Relation<ContactPoint>

Returns:

See Also:



63
# File 'app/models/view_opportunity.rb', line 63

has_many :contact_contact_points, primary_key: :contact_id, foreign_key: :party_id, class_name: 'ContactPoint'

#customer_contact_pointsActiveRecord::Relation<ContactPoint>

Returns:

See Also:



62
# File 'app/models/view_opportunity.rb', line 62

has_many :customer_contact_points, primary_key: :customer_id, foreign_key: :party_id, class_name: 'ContactPoint'

#distance_friendlyObject



122
123
124
125
126
# File 'app/models/view_opportunity.rb', line 122

def distance_friendly
  dist = distance.round(2) rescue nil
  return unless dist
  "#{dist} mi"
end

#locationObject

Alias for Opportunity#location

Returns:

  • (Object)

    Opportunity#location

See Also:



65
# File 'app/models/view_opportunity.rb', line 65

delegate :location, to: :opportunity

#opportunityOpportunity



60
# File 'app/models/view_opportunity.rb', line 60

belongs_to :opportunity, foreign_key: :id, optional: true