Skip to content

Commit

Permalink
Merge branch 'master' into Issue-simpledotorg#2108
Browse files Browse the repository at this point in the history
  • Loading branch information
benryder1988 committed Jul 21, 2021
2 parents 7c38c5f + 5051386 commit 1d4450e
Show file tree
Hide file tree
Showing 16 changed files with 252 additions and 69 deletions.
59 changes: 58 additions & 1 deletion app/assets/stylesheets/user_analytics.scss
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,61 @@ select.form-control, input.form-control {
box-shadow: 0 2px 1px rgba(0,0,0,.16);
box-sizing: border-box;
}
}
}

.switch {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
line-height: 0;
outline: none;
cursor: pointer;
position: relative;
}
.switch:before, .switch:after {
content: "";
position: absolute;
}
.switch:before {
border-radius: 1.25em;
background: #bdc3c7;
height: 1.875em;
left: -0.25em;
top: -0.1875em;
transition: background-color 0.25s ease-out 0.1s;
width: 4.5em;
}
.switch:after {
border-radius: 50%;
background: #fefefe;
height: 1.5em;
transform: translate(0, 0);
transition: transform 0.25s ease-out 0.1s;
width: 1.5em;
}
.switch:checked:after {
transform: translate(2.5em, 0);
}
.switch:checked:before {
background: #3498db;
}
.switch:active {
background-color: unset;
}

.redistribution-question {
display: flex;
justify-content: space-between;
margin-top: 20px;
margin-bottom: 20px;
}

.redistribution-switch-label {
display: flex;
align-items: center;
}

.redistribution-switch-wrapper {
width: 100px;
margin-bottom: 20px;
}
1 change: 1 addition & 0 deletions app/controllers/my_facilities/drug_stocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def drug_stocks_params
drug_stocks:
[:received,
:in_stock,
:redistributed,
:protocol_drug_id]
)
end
Expand Down
11 changes: 8 additions & 3 deletions app/controllers/webview/drug_stocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create
DrugStocksCreator.call(user: current_user,
facility: @current_facility,
for_end_of_month: @for_end_of_month,
drug_stocks_params: safe_params[:drug_stocks])
drug_stocks_params: drug_stocks_params)
redirect_to webview_drug_stocks_url(for_end_of_month: @for_end_of_month.to_s(:mon_year),
facility_id: current_facility.id,
user_id: current_user.id,
Expand Down Expand Up @@ -72,12 +72,17 @@ def set_current_facility
@current_facility = Facility.find(safe_params[:facility_id])
end

def drug_stocks_params
safe_params[:drug_stocks]&.values
end

def safe_params
params.permit(:access_token, :facility_id, :user_id, :for_end_of_month,
drug_stocks:
[:received,
[:protocol_drug_id,
:received,
:in_stock,
:protocol_drug_id])
:redistributed])
end

def set_bust_cache
Expand Down
45 changes: 32 additions & 13 deletions app/models/reports/drug_stock_calculation.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Reports
class DrugStockCalculation
include Memery
def initialize(state:, protocol_drugs:, drug_category:, current_drug_stocks:, previous_drug_stocks: DrugStock.none, patient_count: nil)
@protocol_drugs = protocol_drugs
@drug_category = drug_category
@in_stock_by_rxnorm_code = drug_attribute_sum_by_rxnorm_code(current_drug_stocks, :in_stock)
@received_by_rxnorm_code = drug_attribute_sum_by_rxnorm_code(current_drug_stocks, :received)
@previous_month_in_stock_by_rxnorm_code = drug_attribute_sum_by_rxnorm_code(previous_drug_stocks, :in_stock)
@current_drug_stocks = current_drug_stocks
@previous_drug_stocks = previous_drug_stocks
@patient_count = patient_count
@coefficients = patient_days_coefficients(state)
end
Expand All @@ -28,7 +28,7 @@ def patient_days
extra: {
coefficients: @coefficients,
drug_category: @drug_category,
in_stock_by_rxnorm_code: @in_stock_by_rxnorm_code,
in_stock_by_rxnorm_code: in_stock_by_rxnorm_code,
patient_count: @patient_count,
protocol: @protocol,
exception: e
Expand All @@ -40,10 +40,11 @@ def patient_days
def consumption
protocol_drugs = protocol_drugs_by_category[@drug_category]
drug_consumption = protocol_drugs.each_with_object({}) { |protocol_drug, consumption|
opening_balance = @previous_month_in_stock_by_rxnorm_code&.dig(protocol_drug.rxnorm_code)
received = @received_by_rxnorm_code&.dig(protocol_drug.rxnorm_code)
closing_balance = @in_stock_by_rxnorm_code&.dig(protocol_drug.rxnorm_code)
consumption[protocol_drug] = consumption_calculation(opening_balance, received, closing_balance)
opening_balance = previous_month_in_stock_by_rxnorm_code&.dig(protocol_drug.rxnorm_code)
received = received_by_rxnorm_code&.dig(protocol_drug.rxnorm_code)
redistributed = redistributed_by_rxnorm_code&.dig(protocol_drug.rxnorm_code)
closing_balance = in_stock_by_rxnorm_code&.dig(protocol_drug.rxnorm_code)
consumption[protocol_drug] = consumption_calculation(opening_balance, received, redistributed, closing_balance)
}
drug_consumption[:base_doses] = base_doses(drug_consumption)
drug_consumption
Expand All @@ -53,8 +54,8 @@ def consumption
extra: {
coefficients: @coefficients,
drug_category: @drug_category,
in_stock_by_rxnorm_code: @in_stock_by_rxnorm_code,
previous_month_in_stock_by_rxnorm_code: @previous_month_in_stock_by_rxnorm_code,
in_stock_by_rxnorm_code: in_stock_by_rxnorm_code,
previous_month_in_stock_by_rxnorm_code: previous_month_in_stock_by_rxnorm_code,
patient_count: @patient_count,
protocol: @protocol,
exception: e
Expand All @@ -66,7 +67,7 @@ def consumption
def stocks_on_hand
@stocks_on_hand ||= protocol_drugs_by_category[@drug_category].map do |protocol_drug|
rxnorm_code = protocol_drug.rxnorm_code
in_stock = @in_stock_by_rxnorm_code&.dig(rxnorm_code)
in_stock = in_stock_by_rxnorm_code&.dig(rxnorm_code)
next if in_stock.nil?
coefficient = drug_coefficient(rxnorm_code)
{protocol_drug: protocol_drug,
Expand All @@ -88,21 +89,23 @@ def patient_days_calculation
(stocks_on_hand.map { |stock| stock[:stock_on_hand] }.reduce(:+) / estimated_patients).floor
end

def consumption_calculation(opening_balance, received, closing_balance)
def consumption_calculation(opening_balance, received, redistributed, closing_balance)
return {consumed: nil} if [opening_balance, received, closing_balance].all?(&:nil?)

{
opening_balance: opening_balance,
received: received,
redistributed: redistributed,
closing_balance: closing_balance,
consumed: opening_balance + received - closing_balance
consumed: opening_balance + received - (redistributed || 0) - closing_balance
}
rescue => e
Sentry.capture_message("Consumption Calculation Error",
extra: {
protocol: @protocol,
opening_balance: opening_balance,
received: received,
redistributed: redistributed,
closing_balance: closing_balance,
exception: e
},
Expand Down Expand Up @@ -156,5 +159,21 @@ def drug_attribute_sum_by_rxnorm_code(drug_stocks, attribute)
.map { |rxnorm_code, drug_stocks| [rxnorm_code, drug_stocks.pluck(attribute).sum] }
.to_h
end

memoize def in_stock_by_rxnorm_code
drug_attribute_sum_by_rxnorm_code(@current_drug_stocks, :in_stock)
end

memoize def received_by_rxnorm_code
drug_attribute_sum_by_rxnorm_code(@current_drug_stocks, :received)
end

memoize def redistributed_by_rxnorm_code
drug_attribute_sum_by_rxnorm_code(@current_drug_stocks, :redistributed)
end

memoize def previous_month_in_stock_by_rxnorm_code
drug_attribute_sum_by_rxnorm_code(@previous_drug_stocks, :in_stock)
end
end
end
1 change: 1 addition & 0 deletions app/services/drug_stocks_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def call
protocol_drug_id: drug_stock[:protocol_drug_id],
received: drug_stock[:received].presence,
in_stock: drug_stock[:in_stock].presence,
redistributed: drug_stock[:redistributed].presence,
for_end_of_month: @for_end_of_month,
region: @region)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<span class='math'><%= report[:opening_balance] %></span>Opening balance<br>
<span class='math'>+<%= report[:received] %></span>Stock received<br>
<span class='math'>-<%= report[:closing_balance] %></span>Closing balance<br>
<span class='math'>-<%= report[:redistributed] %></span>Stock issued<br>
<span class='math'>=<%= report[:consumed] %></span>Tablets consumed
</div>
2 changes: 2 additions & 0 deletions app/views/my_facilities/drug_stocks/drug_consumption.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<span class="badge badge-light text-muted">STOCK RECEIVED THIS MONTH</span>
<small><i class="fas fa-minus"></i></small>
<span class="badge badge-light text-muted">CLOSING BALANCE OF THIS MONTH</span>
<small><i class="fas fa-minus"></i></small>
<span class="badge badge-light text-muted">STOCK ISSUED TO OTHER FACILITIES THIS MONTH</span>
</p>
<p>

Expand Down
9 changes: 6 additions & 3 deletions app/views/my_facilities/drug_stocks/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="card col-7" style="text-align: left">
<div class="card col-md-7" style="text-align: left">
<div class="modal-header">
<h2 class="modal-title">Drug stock report</h2>
</div>
Expand Down Expand Up @@ -31,10 +31,13 @@
<%= drug_stock_form.hidden_field :protocol_drug_id, value: protocol_drug.id %>
<div class="form-row">
<div class="col">
<%= drug_stock_form.number_field :received, class: :received, value: @drug_stocks[protocol_drug.id].try(&:received), hide_label: true, help: "Received during the month" %>
<%= drug_stock_form.number_field :received, value: @drug_stocks[protocol_drug.id].try(&:received), hide_label: true, help: "Received during the month" %>
</div>
<div class="col">
<%= drug_stock_form.number_field :in_stock, class: :in_stock, value: @drug_stocks[protocol_drug.id].try(&:in_stock), hide_label: true, help: "Stock at end of the month" %>
<%= drug_stock_form.number_field :redistributed, value: @drug_stocks[protocol_drug.id].try(&:redistributed), hide_label: true, help: "Issued to other facilities" %>
</div>
<div class="col">
<%= drug_stock_form.number_field :in_stock, value: @drug_stocks[protocol_drug.id].try(&:in_stock), hide_label: true, help: "Stock at end of the month" %>
</div>
</div>
</div>
Expand Down
56 changes: 49 additions & 7 deletions app/views/webview/drug_stocks/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
</div>
</div>

<% @protocol_drugs.each do |protocol_drug| %>
<%= form.fields_for "drug_stocks[]", DrugStock.new do |drug_stock_form| %>
<% @protocol_drugs.each_with_index do |protocol_drug, index| %>
<%= form.fields_for "drug_stocks[#{index}]", DrugStock.new do |drug_stock_form| %>
<%= drug_stock_form.hidden_field :protocol_drug_id, value: protocol_drug.id %>

<div class="form-group">
<label for=""><%= "#{protocol_drug.name} #{protocol_drug.dosage}"%> tablets</label>
<label><%= "#{protocol_drug.name} #{protocol_drug.dosage}"%> tablets</label>
<div class="form-row">
<div class="col">
<%= drug_stock_form.number_field :received, class: :received, value: @drug_stocks[protocol_drug.id].try(&:received), hide_label: true, help: "Received this month" %>
Expand All @@ -64,6 +64,36 @@
</div>
<% end %>
<% end %>
<div class="redistribution-question">
<div class="redistribution-switch-label">
<label for="redistribution-fields-toggle">Did your facility issue drugs to other facilities this
month?</label>
</div>
<div class="redistribution-switch-wrapper">
<input type="checkbox" id="redistribution-fields-toggle" class="switch" <%= "checked" if @drug_stocks.values.map(&:redistributed).any? %> />
</div>
</div>

<div id="redistribution-fields">
<% @protocol_drugs.each_with_index do |protocol_drug, index| %>
<%= form.fields_for "drug_stocks[#{index}]", DrugStock.new do |drug_stock_form| %>
<%= drug_stock_form.hidden_field :protocol_drug_id, value: protocol_drug.id %>

<div class="form-group">
<label><%= "#{protocol_drug.name} #{protocol_drug.dosage}"%> tablets</label>
<div class="form-row">
<div style="width: 95%">
<%= drug_stock_form.number_field :redistributed,
class: :redistributed,
value: @drug_stocks[protocol_drug.id].try(&:redistributed),
hide_label: true,
help: "Stock issued to other facilities" %>
</div>
</div>
</div>
<% end %>
<% end %>
</div>

<div class="button-fixed-bottom">
<%= form.button "SAVE", class: "button" %>
Expand All @@ -77,13 +107,25 @@

</body>

<script type="text/javascript" charset="utf-8">
<script type="text/javascript" charset="utf-8">
window.addEventListener("load", function() {
monthSelect = document.getElementById("for_end_of_month");
let monthSelect = document.getElementById("for_end_of_month");
monthSelect.addEventListener("change", function(e) {
window.location.href = location.origin + location.pathname + location.search + "&for_end_of_month=" + encodeURIComponent(monthSelect.value);
});
})
</script>

let redistributionFieldsToggle = document.getElementById("redistribution-fields-toggle")
let redistributionFields = document.getElementById("redistribution-fields")

function showRedistributionFields(show) {
redistributionFields.style.display = show ? "block" : "none";
}

redistributionFieldsToggle.onchange = function(e) {
showRedistributionFields(e.target.checked);
}

showRedistributionFields(redistributionFieldsToggle.checked);
})
</script>
</html>
5 changes: 5 additions & 0 deletions db/migrate/20210713134836_add_redistributed_to_drug_stock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddRedistributedToDrugStock < ActiveRecord::Migration[5.2]
def change
add_column :drug_stocks, :redistributed, :integer
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.uuid "region_id"
t.integer "redistributed"
t.index ["facility_id"], name: "index_drug_stocks_on_facility_id"
t.index ["protocol_drug_id"], name: "index_drug_stocks_on_protocol_drug_id"
t.index ["user_id"], name: "index_drug_stocks_on_user_id"
Expand Down
Loading

0 comments on commit 1d4450e

Please sign in to comment.