@@ -35,48 +35,54 @@ def aggregate_state_totals(districts, drugs_by_category, state = nil)
3535 patient_days = Hash . new ( 0 )
3636 patient_count = 0
3737
38- districts . each do |_ , data |
39- report = data [ :report ]
38+ districts &.each do |_ , data |
39+ report = data &.[]( :report )
40+ next unless report
41+
4042 patient_count += patient_count_for ( report )
4143
42- drugs_by_category . each do |drug_category , drugs |
43- drugs . each do |drug |
44- totals [ drug . rxnorm_code ] += drug_stock_for ( report , drug )
44+ drugs_by_category &.each do |_ , drugs |
45+ drugs &.each do |drug |
46+ code = drug &.rxnorm_code &.to_s
47+ totals [ code ] += drug_stock_for ( report , drug ) if code
4548 end
4649 end
4750 end
4851
4952 patient_days_report = { }
50- drugs_by_category . each do |drug_category , drugs |
51- state_coeffs = state ? Reports ::DrugStockCalculation . new (
53+
54+ drugs_by_category &.each do |drug_category , drugs |
55+ state_coeffs = ( state && Reports ::DrugStockCalculation . new (
5256 state : state ,
5357 protocol_drugs : drugs ,
5458 drug_category : drug_category ,
55- current_drug_stocks : districts . map { |_ , d | d [ :report ] [ :drugs ] } . flatten ,
59+ current_drug_stocks : districts & .map { |_ , d | d &.[] ( :report ) &.[] ( :drugs ) } & .flatten || [ ] ,
5660 patient_count : patient_count
57- ) . patient_days_coefficients ( state ) : { }
61+ ) . patient_days_coefficients ( state ) ) || { }
5862
59- load_coefficient = state_coeffs [ :load_coefficient ] || 1
60- new_patient_coefficient = state_coeffs . dig ( : drug_categories, drug_category , : new_patient_coefficient) || 1
63+ load_coefficient = state_coeffs &.[] ( "load_coefficient" ) || 1
64+ new_patient_coefficient = state_coeffs & .dig ( " drug_categories" , drug_category . to_s , " new_patient_coefficient" ) || 1
6165
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+ adjusted_stock_sum = drugs &.sum do |drug |
67+ code = drug &.rxnorm_code &.to_s
68+ coeff = state_coeffs &.dig ( "drug_categories" , drug_category . to_s , code ) || 1
69+ totals [ code ] . to_f * coeff
70+ end || 0
6671
6772 patient_days [ drug_category ] = if patient_count . positive?
6873 ( adjusted_stock_sum / ( patient_count * load_coefficient * new_patient_coefficient ) ) . to_i
6974 else
7075 0
7176 end
7277
73- stocks = drugs . map do |drug |
78+ stocks = drugs &.map do |drug |
79+ code = drug &.rxnorm_code &.to_s
7480 {
7581 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
82+ in_stock : totals [ code ] . to_i ,
83+ coefficient : state_coeffs & .dig ( " drug_categories" , drug_category . to_s , code ) || 1
7884 }
79- end
85+ end || [ ]
8086
8187 patient_days_report [ drug_category ] = {
8288 stocks_on_hand : stocks ,
0 commit comments