Created
May 13, 2011 01:04
-
-
Save gvc/969768 to your computer and use it in GitHub Desktop.
Class that represents a medical appointment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
class Agendamento::Marcacao < Agendamento::Agendamento | |
belongs_to :procedimento, class_name: 'Agendamento::Procedimento' | |
belongs_to :convenio | |
belongs_to :paciente | |
belongs_to :atendimento | |
validates_presence_of :horario, :procedimento | |
validates_presence_of :nome_paciente, if: lambda { |m| m.paciente.blank? } | |
validates_presence_of :paciente, if: lambda { |m| m.nome_paciente.blank? } | |
validates_presence_of :motivo_cancelamento, if: lambda { |m| m.cancelada? } | |
validates_presence_of :convenio, :telefone | |
validates :telefone, telefone: true | |
scope :com_procedimento, ->(p) { where(procedimento_id: p) } | |
scope :retorno, where(tipo: :retorno) | |
scope :ativa, where('status <> ?', :cancelada) | |
enum_attr :tipo, %w(^primeira_vez continuidade retorno) do | |
label primeira_vez: '1ª Vez' | |
end | |
enum_attr :status, %w(^marcado confirmado presente em_atendimento atendido cancelada) | |
validate :permite_remarcacao?, if: lambda { |a| a.remarcado and a.remarcado_changed? } | |
validate :horario_permite_encaixe?, if: ->(a) { a.horario and a.data } | |
validate :horario_permite_retorno?, if: ->(a) { a.horario and a.data } | |
before_validation :encaixar!, if: ->(a) { a.horario and a.data } | |
before_save :atribuir_status_modificado_em! | |
before_save :atribuir_nome_paciente! | |
before_create :atribuir_hora_fim_marcacao! | |
after_update :desencaixar_horario_antigo_no_cancelamento!, if: ->() { self.status_is_in_cancelada? } | |
def self.tempo_de_atraso(marcacao) | |
marcacao.agenda.tipo_is_hora_marcada? ? (Time.now.seconds_since_midnight - marcacao.hora.seconds_since_midnight).to_i / 60 : 0 | |
end | |
def cadastro_incompleto? | |
self.paciente_id.nil? | |
end | |
def existe_marcacao? | |
Agendamento::Marcacao.where("status <> 'cancelada'").where(horario_id: self.horario_id).where(data: self.data).where(hora: self.hora).any? | |
end | |
def permite_encaixe? | |
if self.encaixe | |
self.horario.permite_encaixe_na_data_na_hora?(self.data, self.hora, self.id) | |
else | |
true | |
end | |
end | |
private | |
def atribuir_status_modificado_em! | |
self.status_modificado_em = Time.now | |
end | |
def atribuir_nome_paciente! | |
return if self.paciente.nil? | |
self.nome_paciente = self.paciente.nome | |
end | |
def permite_remarcacao? | |
if not(self.data_changed? or self.hora.seconds_since_midnight != self.hora_was.seconds_since_midnight) | |
self.errors.add(:base, 'Não é possível remarcar para o mesmo horário') | |
end | |
end | |
def encaixar! | |
return if self.horario.blank? | |
self.encaixe = if self.horario.agenda.tipo_is_hora_marcada? | |
query = Agendamento::Marcacao.ativa.do_horario(self.horario).na_data(self.data).na_hora(self.hora) | |
query = query.where('id <> ?', self.id) if self.id | |
query.any? | |
else | |
query = Agendamento::Marcacao.ativa.do_horario(self.horario).na_data(self.data) | |
query = query.where('id <> ?', self.id) if self.id | |
query.count >= self.horario.quantidade_consultas | |
end | |
true | |
end | |
def desencaixar_horario_antigo_na_remarcacao! | |
marcacoes_do_horario_antigo = Agendamento::Marcacao.where('id <> ?', self.id).where(horario_id: self.horario).where(data: self.data_was).where(hora: self.hora_was) | |
if marcacoes_do_horario_antigo.size == 1 | |
marcacao = marcacoes_do_horario_antigo.first | |
marcacao.encaixe = false | |
marcacao.save! | |
end | |
true | |
end | |
def atribuir_hora_fim_marcacao! | |
self.hora_fim_marcacao = Time.now | |
end | |
def horario_permite_encaixe? | |
self.errors.add(:base, 'Limite máximo de encaixes alcançado') if self.encaixe_changed? && not(self.permite_encaixe?) | |
end | |
def horario_permite_retorno? | |
if self.tipo_retorno? and self.tipo_changed? and self.horario.limite_retornos | |
qtd_retornos_atual = Agendamento::Marcacao.do_horario(self.horario).na_data(self.data).retorno.ativo | |
qtd_retornos_atual = qtd_retornos_atual.where('id <> ?', self.id) if self.id | |
if qtd_retornos_atual.count >= self.horario.limite_retornos | |
self.errors.add(:base, 'Limite máximo de retornos alcançado') | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment