module ErpBaseErpSvcs::Extensions::ActiveRecord::HasTrackedStatus::InstanceMethods

Public Instance Methods

add_status(tracked_status_iid) click to toggle source

add_status aliases current_status= for legacy support

# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 213
def add_status(tracked_status_iid)
  self.current_status = tracked_status_iid
end
current_status() click to toggle source

gets current status's internal_identifier

# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 160
def current_status
  self.current_status_type.internal_identifier unless self.current_status_type.nil?
end
current_status=(args) click to toggle source

set current status of entity.

@param args [String, TrackedStatusType, Array] This can be a string of the internal identifier of the TrackedStatusType to set, a TrackedStatusType instance, or three params the status, options and party_id

# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 168
def current_status=(args)
  options = {}

  if args.is_a?(Array)
    status = args[0]
    options = args[1]
    party_id = args[2]
  else
    status = args
  end

  tracked_status_type = status.is_a?(TrackedStatusType) ? status : TrackedStatusType.find_by_internal_identifier(status.to_s)
  raise "TrackedStatusType does not exist #{status.to_s}" unless tracked_status_type

  # if passed status is current status then do nothing
  unless self.current_status_type && (self.current_status_type.id == tracked_status_type.id)
    #set current StatusApplication thru_date to now
    cta = self.current_status_application
    unless cta.nil?
      cta.thru_date = options[:thru_date].nil? ? Time.now : options[:thru_date]
      cta.save
    end

    status_application = StatusApplication.new
    status_application.tracked_status_type = tracked_status_type
    status_application.from_date = options[:from_date].nil? ? Time.now : options[:from_date]
    status_application.party_id = party_id
    status_application.save

    self.status_applications << status_application
    self.save
  end

end
current_status_application() click to toggle source

gets current StatusApplication record

# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 150
def current_status_application
  self.status_applications.where("status_applications.thru_date IS NULL").order('id DESC').first
end
current_status_type() click to toggle source

get's current status's tracked_status_type

# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 155
def current_status_type
  self.current_status_application.tracked_status_type unless self.current_status_application.nil?
end
destroy_status_applications() click to toggle source
# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 105
def destroy_status_applications
  self.status_applications.each do |status_application|
    status_application.destroy
  end
end
get_status_for_date_time(datetime) click to toggle source

get status for given date checks from_date attribute

# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 130
def get_status_for_date_time(datetime)
  status_applications = StatusApplication.arel_table

  arel_query = StatusApplication.where(status_applications[:from_date].gteq(datetime - 1.day).or(status_applications[:from_date].lteq(datetime + 1.day)))

  arel_query.all
end
get_statuses_for_date_time_range(from_date, thru_date) click to toggle source

get status for passed date range from_date and thru_date checks from_date attribute

# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 140
def get_statuses_for_date_time_range(from_date, thru_date)
  status_applications = StatusApplication.arel_table

  arel_query = StatusApplication.where(status_applications[:from_date].gteq(from_date - 1.day).or(status_applications[:from_date].lteq(from_date + 1.day)))
  arel_query = arel_query.where(status_applications[:thru_date].gteq(thru_date - 1.day).or(status_applications[:thru_date].lteq(thru_date + 1.day)))

  arel_query.all
end
had_status?(tracked_status_iid) click to toggle source

did it have this status in the past but NOT currently?

# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 117
def had_status?(tracked_status_iid)
  return false if has_status?(tracked_status_iid)
  has_had_status?(tracked_status_iid)
end
has_had_status?(tracked_status_iid) click to toggle source

does it now or has it ever had this status?

# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 123
def has_had_status?(tracked_status_iid)
  result = self.status_applications.joins(:tracked_status_types).where("tracked_status_types.internal_identifier = ?", tracked_status_iid)
  result.nil? ? false : true
end
has_status?(tracked_status_iid) click to toggle source

does this status match the current_status?

# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 112
def has_status?(tracked_status_iid)
  current_status == tracked_status_iid
end
previous_status() click to toggle source
# File lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb, line 203
def previous_status
  result = self.status_applications.joins(:tracked_status_type).order("status_applications.id desc").limit(2).all
  if result.count == 2
    result[1].tracked_status_type.internal_identifier
  else
    nil
  end
end