Class: Renalware::Medications::HomeDelivery::EventsController

Inherits:
BaseController show all
Defined in:
app/controllers/renalware/medications/home_delivery/events_controller.rb

Overview

Works with a modal.

Instance Method Summary collapse

Methods inherited from BaseController

#patient

Instance Method Details

#newObject

A major REST faux pas, our #new action actually creates something - an instance of a HomeDelivery::Event. We use this to render a modal form where the user can choose which drug types they want to print a PDF for, and what some parameters are for the PDF e.g. the prescription duration. See also Drugs::HomecareForm. The overall idea is that when you bring up the modal dialog to print a home delivery PDF there is always an Event object created behind it, and when you change the params (drug type, prescription duration) it updates the stored event, ready for printing. Although this means we might create rows that are never printed if the user cancels out of the dialog, this is OK as the #printed flag will not be set on those rows so they can be housekept away



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/renalware/medications/home_delivery/events_controller.rb', line 20

def new
  homecare_form = Drugs::HomecareForm.first

  event = Delivery::Event.create!(
    patient: patient,
    drug_type_id: homecare_form&.drug_type&.id,
    homecare_form: homecare_form,
    reference_number: Medications::Delivery::PurchaseOrderNumber.next,
    prescription_duration: homecare_form&.prescription_duration_default,
    by: current_user
  )
  authorize event

  event.prescriptions = prescriptions_for(event)
  event.valid?
  render(:edit, layout: false, locals: { event: event })
end

#showObject

GET PDF - display the pdf



80
81
82
83
84
85
# File 'app/controllers/renalware/medications/home_delivery/events_controller.rb', line 80

def show
  event = find_and_auth_event
  respond_to do |format|
    format.pdf { render_renalware_forms_pdf(event) }
  end
end

#updateObject

Once #new is called when the modal is first displayed, successive updates come in here



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/renalware/medications/home_delivery/events_controller.rb', line 39

def update
  event = find_and_auth_event
  event.assign_attributes(event_params)
  event.homecare_form = homecare_form_for(event)
  event.prescriptions = prescriptions_for(event)

  if event.changed.include?("drug_type_id")
    event.prescription_duration = event.homecare_form&.prescription_duration_default
  end
  event.printed = event_params[:printed].present? # button

  if event.update_by(current_user, {})
    if event.printed?
      # If printed == true that means the user has clicked on the 'Printed' successfully
      # so we just hide the modal - we are done.
      event.update!(printed: true)
      event.prescriptions.each do |pres|
        last_delivery_date = Time.zone.today
        unit_count = event.prescription_duration.to_i
        unit = event.homecare_form.prescription_duration_unit.to_sym
        next_delivery_date = last_delivery_date + unit_count.send(unit) # eg. 6.months

        pres.update_columns(
          last_delivery_date: last_delivery_date,
          next_delivery_date: next_delivery_date
        )
      end
      render :update, locals: { event: event }
    else
      # The user has updated the drug id or prescription duration in the dialog
      # and we have come in here as an ajax PATCH as a result. We are updating the event as
      # the options are changes in the dialog, so when the clicks on the Print link, the
      # event reflects the chosen options.
      render :edit, locals: { event: event }
    end
  else
    render :edit, locals: { event: event }
  end
end