Skip to content

Commit 0b31ba9

Browse files
sc-16765 Fix aggregate_state_totals patient_days calculation (#5706)
**Story card:** [sc-16765](https://app.shortcut.com/simpledotorg/story/16765/fix-drug-stock-calculation-in-bd) ## Because The Drug Stock table in the Bangladesh dashboard is showing incorrect patient days. Currently, we are summing patient days across all facilities and districts within a division, which misrepresents the actual values. Patient days should instead be calculated using the existing patient day formula. ## This addresses Patient days now reflect the proper calculation instead of a simple sum of facilities and districts. At the division and subtotal levels, the value is computed based on: - The subtotal of drugs - The total number of patients - The patient day calculation formula Key changes: - Fixed patient day calculation in the Drug Stock table - Removed incorrect summing of facilities and district patient days - Ensures values at division and subtotal levels match the intended formula ## Test instructions - Navigate to the Drug Stock section. - Hover over the patient days value to view the calculation formula being applied.
1 parent 4d05e5e commit 0b31ba9

File tree

3 files changed

+77
-16
lines changed

3 files changed

+77
-16
lines changed

app/helpers/drug_stock_helper.rb

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def drug_stock_for(report, drug)
3030
end
3131
end
3232

33-
def aggregate_state_totals(districts, drugs_by_category)
33+
def aggregate_state_totals(districts, drugs_by_category, state = nil)
3434
totals = Hash.new(0)
3535
patient_days = Hash.new(0)
3636
patient_count = 0
@@ -43,14 +43,56 @@ def aggregate_state_totals(districts, drugs_by_category)
4343
drugs.each do |drug|
4444
totals[drug.rxnorm_code] += drug_stock_for(report, drug)
4545
end
46+
end
47+
end
4648

47-
if report.dig(:total_patient_days, drug_category, :patient_days)
48-
patient_days[drug_category] += report.dig(:total_patient_days, drug_category, :patient_days).to_i
49-
end
49+
patient_days_report = {}
50+
drugs_by_category.each do |drug_category, drugs|
51+
state_coeffs = state ? Reports::DrugStockCalculation.new(
52+
state: state,
53+
protocol_drugs: drugs,
54+
drug_category: drug_category,
55+
current_drug_stocks: districts.map { |_, d| d[:report][:drugs] }.flatten,
56+
patient_count: patient_count
57+
).patient_days_coefficients(state) : {}
58+
59+
load_coefficient = state_coeffs[:load_coefficient] || 1
60+
new_patient_coefficient = state_coeffs.dig(:drug_categories, drug_category, :new_patient_coefficient) || 1
61+
62+
adjusted_stock_sum = drugs.sum do |drug|
63+
coefficient = state_coeffs.dig(:drug_categories, drug_category, drug.rxnorm_code) || 1
64+
totals[drug.rxnorm_code].to_f * coefficient
65+
end
66+
67+
patient_days[drug_category] = if patient_count.positive?
68+
(adjusted_stock_sum / (patient_count * load_coefficient * new_patient_coefficient)).to_i
69+
else
70+
0
5071
end
72+
73+
stocks = drugs.map do |drug|
74+
{
75+
protocol_drug: drug,
76+
in_stock: totals[drug.rxnorm_code].to_i,
77+
coefficient: state_coeffs.dig(:drug_categories, drug_category, drug.rxnorm_code) || 1
78+
}
79+
end
80+
81+
patient_days_report[drug_category] = {
82+
stocks_on_hand: stocks,
83+
patient_count: patient_count,
84+
load_coefficient: load_coefficient,
85+
new_patient_coefficient: new_patient_coefficient,
86+
patient_days: patient_days[drug_category]
87+
}
5188
end
5289

53-
{totals: totals, patient_days: patient_days, patient_count: patient_count}
90+
{
91+
totals: totals,
92+
patient_days: patient_days,
93+
patient_count: patient_count,
94+
patient_days_report: patient_days_report
95+
}
5496
end
5597

5698
def grouped_district_reports(district_reports)

app/views/my_facilities/drug_stocks/_all_district_drug_stock_table.html.erb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
</thead>
8484

8585
<% grouped.each do |state, districts| %>
86-
<% state_data = aggregate_state_totals(districts, first_drugs_by_category) %>
86+
<% state_data = aggregate_state_totals(districts, first_drugs_by_category, state) %>
8787
<% state_totals = state_data[:totals] %>
8888
<% state_patient_days = state_data[:patient_days] %>
8989
<% state_patient_count = state_data[:patient_count] %>
@@ -105,9 +105,19 @@
105105
<% end %>
106106
<% unless drug_category == "diabetes" %>
107107
<% if state_patient_days[drug_category].present? %>
108-
<td class="type-percent">
109-
<em class="<%= patient_days_css_class(state_patient_days[drug_category]) %>"><%= state_patient_days[drug_category] %></em>
110-
</td>
108+
<td class="type-percent"
109+
data-html="true"
110+
data-toggle="tooltip"
111+
data-placement="top"
112+
data-trigger="hover focus"
113+
data-template="<%= render 'wide_tooltip_template' %>"
114+
data-original-title="<%= render 'drug_stocks_tooltip', report: state_data[:patient_days_report][drug_category] %>"
115+
data-sort-value="<%= state_patient_days[drug_category] %>">
116+
<em class="<%= patient_days_css_class(state_patient_days[drug_category]) %>">
117+
<%= state_patient_days[drug_category] %>
118+
</em>
119+
</td>
120+
111121
<% else %>
112122
<td class="type-blank"><span>&#8212;</span></td>
113123
<% end %>

spec/helpers/drug_stock_helper_spec.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,28 @@
6565
report: {
6666
total_drugs_in_stock: {"979467" => 10, "316764" => 0, "329528" => 5},
6767
total_patient_days: {"hypertension_arb" => {patient_days: 3}, "hypertension_ccb" => {patient_days: 1}},
68-
district_patient_count: 7
68+
district_patient_count: 7,
69+
patient_days_report: {
70+
"hypertension_arb" => {patient_days: 1},
71+
"hypertension_ccb" => {patient_days: 0}
72+
}
6973
}
7074
}
7175
}
7276
end
7377

74-
it "aggregates totals, patient_days, and patient_count correctly" do
78+
it "aggregates totals, patient_days, patient_count, and patient_days_report correctly" do
7579
result = helper.aggregate_state_totals(districts, drugs_by_category)
76-
expect(result).to eq(
77-
totals: {"979467" => 10, "316764" => 0, "329528" => 5},
78-
patient_days: {"hypertension_arb" => 3, "hypertension_ccb" => 1},
79-
patient_count: 7
80-
)
80+
81+
expect(result[:totals]).to eq({"979467" => 10, "316764" => 0, "329528" => 5})
82+
expect(result[:patient_count]).to eq(7)
83+
expect(result[:patient_days]).to eq({"hypertension_arb" => 1, "hypertension_ccb" => 0})
84+
85+
report_arb = result[:patient_days_report]["hypertension_arb"]
86+
report_ccb = result[:patient_days_report]["hypertension_ccb"]
87+
88+
expect(report_arb[:patient_days]).to eq(1)
89+
expect(report_ccb[:patient_days]).to eq(0)
8190
end
8291
end
8392

0 commit comments

Comments
 (0)