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

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

#publish_event

Instance Attribute Details

#remainingObject (readonly)



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

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

#roleObject (readonly)



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

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

#weightObject (readonly)



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

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:



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

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

.local_sales_repsActiveRecord::Relation<SalesRepWeight>

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

Returns:

See Also:



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

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:



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

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:



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

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:



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

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

Instance Method Details

#effective_remainingObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/sales_rep_weight.rb', line 45

def effective_remaining
  # if rep is not working, weight is going to return 0
  # after 4pm, you need to be working the next day to have a weight
  if Time.current.hour > 16 || !Date.current.on_weekday?
    check_date = Date.current.next_business_day
  else
    check_date = Date.current
  end
  if employee.working_on_day?(check_date)
    remaining
  else
    0
  end
end

#employeeEmployee

Returns:

See Also:



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

belongs_to :employee, optional: true

#sales_rep_queue_entrySalesRepQueueEntry

Validations:



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

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