2019-02-02 12:40:57 +01:00
|
|
|
class PrinterJob < ActiveRecord::Base
|
|
|
|
belongs_to :order
|
|
|
|
belongs_to :created_by, class_name: 'User', foreign_key: 'created_by_user_id'
|
2020-08-01 02:49:15 +02:00
|
|
|
belongs_to :finished_by, optional: true, class_name: 'User', foreign_key: 'finished_by_user_id'
|
2019-02-02 12:40:57 +01:00
|
|
|
has_many :printer_job_updates
|
|
|
|
|
|
|
|
scope :finished, -> { where.not(finished_at: nil) }
|
|
|
|
scope :unfinished, -> { where(finished_at: nil).order(:id) }
|
2021-03-01 15:27:26 +01:00
|
|
|
scope :pending, -> { unfinished.includes(:order).where.not(orders: { state: 'open' }) }
|
|
|
|
scope :queued, -> { unfinished.includes(:order).where(orders: { state: 'open' }) }
|
2019-02-02 12:40:57 +01:00
|
|
|
|
|
|
|
def last_update_at
|
|
|
|
printer_job_updates.order(:created_at).last.try(&:created_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_update_state
|
|
|
|
printer_job_updates.order(:created_at).last.try(&:state)
|
|
|
|
end
|
|
|
|
|
2021-03-01 15:27:26 +01:00
|
|
|
def add_update!(state, message = nil)
|
2019-02-02 12:40:57 +01:00
|
|
|
return unless finished_at.nil?
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2019-02-02 12:40:57 +01:00
|
|
|
PrinterJobUpdate.create! printer_job: self, state: state, message: message
|
|
|
|
end
|
|
|
|
|
2021-03-01 15:27:26 +01:00
|
|
|
def finish!(user = nil)
|
2019-02-02 12:40:57 +01:00
|
|
|
return unless finished_at.nil?
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2022-10-13 18:25:52 +02:00
|
|
|
update(finished_at: Time.now, finished_by: user)
|
2019-02-02 12:40:57 +01:00
|
|
|
end
|
|
|
|
end
|