Class: SalesRepWeight

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

Overview

== Schema Information

Table name: sales_rep_weights
Database name: primary

id :integer not null, primary key
remaining :integer default(0), not null
role :string(255) not null
weight :integer default(0), not null
employee_id :integer not null
sales_rep_queue_entry_id :integer not null

Indexes

idx_sales_rep_weights (sales_rep_queue_entry_id,role,employee_id) UNIQUE
sales_rep_weights_employee_id_idx (employee_id)

Foreign Keys

sales_rep_weights_employee_id_fk (employee_id => parties.id) ON DELETE => cascade
sales_rep_weights_sales_rep_queue_entry_id_fk (sales_rep_queue_entry_id => sales_rep_queue_entries.id) ON DELETE => cascade

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

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::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#remainingInteger (readonly)

Returns:

  • (Integer)


35
# File 'app/models/sales_rep_weight.rb', line 35

validates :remaining, numericality: { only_integer: true, less_than_or_equal_to: :weight }

#roleString (readonly)

Returns:

  • (String)


37
38
# File 'app/models/sales_rep_weight.rb', line 37

validates :role, inclusion: { in: %w[primary_sales_rep secondary_sales_rep local_sales_rep],
message: "%{value} is not a valid role" }

#weightInteger (readonly)

Returns:

  • (Integer)


33
# File 'app/models/sales_rep_weight.rb', line 33

validates :weight, numericality: { only_integer: true, greater_than: 0 }

Class Method Details

.assignableActiveRecord::Relation<SalesRepWeight>

A relation of SalesRepWeights that are assignable. Active Record Scope

Returns:

See Also:



44
# File 'app/models/sales_rep_weight.rb', line 44

scope :assignable, -> { where(SalesRepWeight[:remaining].gt(0)) }

.availability_check_dateDate

The day a rep's availability is judged against: after 4pm (or on a
weekend) the queue is filling the next business day's work, so the rep
has to be working then rather than now.

Returns:

  • (Date)


55
56
57
58
59
60
61
# File 'app/models/sales_rep_weight.rb', line 55

def self.availability_check_date
  if Time.current.hour > 16 || !Date.current.on_weekday?
    Date.current.next_business_day
  else
    Date.current
  end
end

.local_sales_repsActiveRecord::Relation<SalesRepWeight>

A relation of SalesRepWeights that are local sales reps. Active Record Scope

Returns:

See Also:



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

scope :local_sales_reps, -> { where(role: :local_sales_rep) }

.primary_sales_repsActiveRecord::Relation<SalesRepWeight>

A relation of SalesRepWeights that are primary sales reps. Active Record Scope

Returns:

See Also:



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

scope :primary_sales_reps, -> { where(role: :primary_sales_rep) }

.secondary_sales_repsActiveRecord::Relation<SalesRepWeight>

A relation of SalesRepWeights that are secondary sales reps. Active Record Scope

Returns:

See Also:



46
# File 'app/models/sales_rep_weight.rb', line 46

scope :secondary_sales_reps, -> { where(role: :secondary_sales_rep) }

.with_employee_sortedActiveRecord::Relation<SalesRepWeight>

A relation of SalesRepWeights that are with employee sorted. Active Record Scope

Returns:

See Also:



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

scope :with_employee_sorted, -> { joins(:employee).order(Employee[:full_name]) }

Instance Method Details

#effective_remainingInteger

Returns:

  • (Integer)


64
65
66
67
68
69
# File 'app/models/sales_rep_weight.rb', line 64

def effective_remaining
  # if rep is not working, weight is going to return 0
  return 0 unless employee&.working_on_day?(self.class.availability_check_date)

  remaining
end

#employeeEmployee?

Returns:



30
# File 'app/models/sales_rep_weight.rb', line 30

belongs_to :employee, optional: true

#sales_rep_queue_entrySalesRepQueueEntry?

Returns:

Validations:



28
# File 'app/models/sales_rep_weight.rb', line 28

belongs_to :sales_rep_queue_entry, inverse_of: :sales_rep_weights, optional: true