Class: MalawiHivProgramReports::Clinic::DrugDispensations

Inherits:
Object
  • Object
show all
Defined in:
app/services/malawi_hiv_program_reports/clinic/drug_dispensations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_date:, end_date:, **_kwargs) ⇒ DrugDispensations

Returns a new instance of DrugDispensations.



12
13
14
15
# File 'app/services/malawi_hiv_program_reports/clinic/drug_dispensations.rb', line 12

def initialize(start_date:, end_date:, **_kwargs)
  @start_date = ActiveRecord::Base.connection.quote(start_date)
  @end_date = ActiveRecord::Base.connection.quote(end_date)
end

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



10
11
12
# File 'app/services/malawi_hiv_program_reports/clinic/drug_dispensations.rb', line 10

def end_date
  @end_date
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



10
11
12
# File 'app/services/malawi_hiv_program_reports/clinic/drug_dispensations.rb', line 10

def start_date
  @start_date
end

Instance Method Details

#find_reportObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/malawi_hiv_program_reports/clinic/drug_dispensations.rb', line 17

def find_report
  ActiveRecord::Base.connection.select_all <<~SQL
    SELECT drug.name AS drug_name,
           dispensation.value_numeric AS pack_size,
           COUNT(*) AS packs_dispensed
    FROM obs AS dispensation
    INNER JOIN encounter
      ON encounter.encounter_id = dispensation.encounter_id
      AND encounter.program_id IN (SELECT program_id FROM program WHERE name = 'HIV PROGRAM')
      AND encounter.encounter_type IN (SELECT encounter_type_id FROM encounter_type WHERE name = 'Dispensing')
      AND encounter.voided = 0
    INNER JOIN orders
      ON orders.order_id = dispensation.order_id
      AND orders.start_date BETWEEN #{start_date} AND #{end_date}
      AND orders.order_type_id IN (SELECT order_type_id FROM order_type WHERE name = 'Drug order')
      AND orders.voided = 0
    INNER JOIN drug_order
      ON drug_order.order_id = orders.order_id
      AND drug_order.drug_inventory_id IN (SELECT drug_id FROM arv_drug)
      AND drug_order.quantity > 0
    INNER JOIN drug
      ON  drug.drug_id = drug_order.drug_inventory_id
      AND drug.retired = 0
    WHERE dispensation.voided = 0
      AND dispensation.value_numeric > 0
      AND dispensation.concept_id IN (SELECT concept_id FROM concept_name WHERE name = 'Amount dispensed' AND voided = 0)
    GROUP BY dispensation.value_numeric
  SQL
end