Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ac jw subsidy fixes #563

Merged
merged 13 commits into from
Jul 13, 2016
Merged
12 changes: 6 additions & 6 deletions app/assets/javascripts/service_subsidy.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ $(document).ready ->
percent_subsidy = (recalculate_percent_subsidy(total_request_cost, pi_contribution) * 100).toFixed(2)
original_pi_contribution = recalculate_pi_contribution(total_request_cost, current_percent_subsidy)

if parseFloat(percent_subsidy) > parseFloat(max_percent)
message = "The Percent Subsidy cannot be greater than the max percent of #{max_percent}."
if parseFloat(percent_subsidy) > parseFloat(max_percent)
message = "The Percent Subsidy cannot be greater than the max percent of #{max_percent}."
current_cost = recalculate_current_cost(total_request_cost, current_percent_subsidy)
display_error_and_reset(subsidy_id, current_percent_subsidy, original_pi_contribution, current_cost, max_percent, message)
else if recalculate_current_cost(total_request_cost, (percent_subsidy / 100)) > max_dollar_cap
Expand All @@ -80,7 +80,7 @@ $(document).ready ->
'pi_contribution' : pi_contribution
'percent_subsidy' : percent_subsidy
$.ajax
type: 'PATCH'
type: 'PUT'
url: "/subsidies/#{subsidy_id}"
data: data
success: (data, textStatus, jqXHR) ->
Expand All @@ -100,7 +100,7 @@ $(document).ready ->
total_request_cost = parseFloat($(".request_cost[data-subsidy-id='#{subsidy_id}']").data("cost")) / 100.0
pi_contribution = recalculate_pi_contribution(total_request_cost, percent_subsidy)
original_subsidy = recalculate_percent_subsidy(total_request_cost, original_pi_contribution)

if (parseFloat(percent_subsidy * 100)) > parseFloat(max_percent)
message = "The Percent Subsidy cannot be greater than the max percent of #{max_percent}."
current_cost = recalculate_current_cost(total_request_cost, original_subsidy)
Expand All @@ -117,12 +117,12 @@ $(document).ready ->
percent_subsidy = 1.0
else if percent_subsidy < 0
percent_subsidy = 0

data = 'subsidy' :
'pi_contribution' : pi_contribution,
'percent_subsidy' : percent_subsidy
$.ajax
type: 'PATCH'
type: 'PUT'
url: "/subsidies/#{subsidy_id}"
data: data
success: (data, textStatus, jqXHR) ->
Expand Down
15 changes: 3 additions & 12 deletions app/controllers/dashboard/subsidies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def new
end

def create
format_pi_contribution_param
@subsidy = PendingSubsidy.new(params[:pending_subsidy])
format_percent_subsidy_param
@subsidy = PendingSubsidy.new(params[:pending_subsidy].except(:pi_contribution))
if @subsidy.valid?
@subsidy.save
@sub_service_request = @subsidy.sub_service_request
Expand All @@ -52,9 +52,8 @@ def edit
def update
@subsidy = PendingSubsidy.find(params[:id])
@sub_service_request = @subsidy.sub_service_request
format_pi_contribution_param
format_percent_subsidy_param
if @subsidy.update_attributes(params[:pending_subsidy])
if @subsidy.update_attributes(params[:pending_subsidy].except(:pi_contribution))
@admin = params[:admin] == 'true'
flash[:success] = t(:dashboard)[:subsidies][:updated]
unless @admin
Expand Down Expand Up @@ -85,14 +84,6 @@ def approve

private

def format_pi_contribution_param
# Refomat pi_contribution string to characters other than numbers and . delimiter,
# Convert to float, and multiply by 100 to get cents for db
if !params[:pending_subsidy].nil? && params[:pending_subsidy][:pi_contribution].present?
params[:pending_subsidy][:pi_contribution] = (params[:pending_subsidy][:pi_contribution].gsub(/[^\d^\.]/, '').to_f * 100)
end
end

def format_percent_subsidy_param
if !params[:pending_subsidy].nil? && params[:pending_subsidy][:percent_subsidy].present?
params[:pending_subsidy][:percent_subsidy] = ((params[:pending_subsidy][:percent_subsidy].gsub(/[^\d^\.]/, '').to_f) / 100)
Expand Down
13 changes: 2 additions & 11 deletions app/controllers/subsidies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ class SubsidiesController < ApplicationController

def create
@sub_service_request = SubServiceRequest.find params[:subsidy][:sub_service_request_id]
@subsidy = PendingSubsidy.create(sub_service_request_id: @sub_service_request.id, pi_contribution: @sub_service_request.direct_cost_total)
@subsidy = PendingSubsidy.create(sub_service_request_id: @sub_service_request.id, percent_subsidy: 0)
end

def update
format_pi_contribution_param
format_percent_subsidy_param
unless @subsidy.update_attributes(params[:subsidy])
unless @subsidy.update_attributes(params[:subsidy].except(:pi_contribution))
@errors = @subsidy.errors.full_messages
end
end
Expand All @@ -45,14 +44,6 @@ def find_subsidy
@sub_service_request = @subsidy.sub_service_request
end

# Refomat pi_contribution string to characters other than numbers and . delimiter,
# Convert to float, and multiply by 100 to get cents for db
def format_pi_contribution_param
if !params[:subsidy].nil? && params[:subsidy][:pi_contribution].present?
params[:subsidy][:pi_contribution] = (params[:subsidy][:pi_contribution].gsub(/[^\d^\.]/, '').to_f * 100)
end
end

def format_percent_subsidy_param
if !params[:subsidy].nil? && params[:subsidy][:percent_subsidy].present?
params[:subsidy][:percent_subsidy] = (params[:subsidy][:percent_subsidy].gsub(/[^\d^\.]/, '').to_f)
Expand Down
8 changes: 7 additions & 1 deletion app/models/past_subsidy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ class PastSubsidy < ActiveRecord::Base

attr_accessible :sub_service_request_id
attr_accessible :total_at_approval
attr_accessible :pi_contribution
attr_accessible :percent_subsidy
attr_accessible :approved_by
attr_accessible :approved_at

default_scope { order('approved_at ASC') }

def pi_contribution
# This ensures that if pi_contribution is null (new record),
# then it will reflect the full cost of the request.
total_at_approval - (total_at_approval * percent_subsidy) || total_at_approval
end

def approved_cost
# Calculates cost of subsidy (amount subsidized)
# stored total - pi_contribution then convert from cents to dollars
Expand Down
11 changes: 2 additions & 9 deletions app/models/pending_subsidy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ def default_values
self.status ||= 'Pending'
end

def pi_contribution
# This ensures that if pi_contribution is null (new record),
# then it will reflect the full cost of the request.
total_request_cost - (total_request_cost * percent_subsidy) || total_request_cost
end

def current_cost
# Calculates cost of subsidy (amount subsidized)
# SSR direct_cost_total - pi_contribution then convert from cents to dollars
Expand All @@ -52,11 +46,10 @@ def grant_approval approver
# Creates a new ApprovedSubsidy from this PendingSubsidy
# Remove current approved subsidy if exists, save notes
current_approved_subsidy = sub_service_request.approved_subsidy

# log the past subsidy
PastSubsidy.create(current_approved_subsidy.attributes.except("id", "status", "created_at", "updated_at", "deleted_at", "overridden"))

if current_approved_subsidy.present?
# log the past subsidy
PastSubsidy.create(current_approved_subsidy.attributes.except("id", "status", "created_at", "updated_at", "deleted_at", "overridden"))
ApprovedSubsidy.where(sub_service_request_id: sub_service_request_id).destroy_all
end

Expand Down
20 changes: 11 additions & 9 deletions app/models/subsidy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class Subsidy < ActiveRecord::Base
has_many :notes, as: :notable

attr_accessible :sub_service_request_id
attr_accessible :pi_contribution
attr_accessible :overridden
attr_accessible :status
attr_accessible :percent_subsidy
Expand All @@ -41,18 +40,21 @@ class Subsidy < ActiveRecord::Base
validates_presence_of :pi_contribution
validate :contribution_caps

def pi_contribution
# This ensures that if pi_contribution is null (new record),
# then it will reflect the full cost of the request.
total_request_cost - (total_request_cost * percent_subsidy) || total_request_cost
end

# Generates error messages if user input is out of parameters
def contribution_caps
dollar_cap, percent_cap = max_dollar_cap, max_percentage
request_cost = total_request_cost()
subsidy_cost = (request_cost - pi_contribution)

subsidy_cost = (total_request_cost - pi_contribution)
if pi_contribution < 0
errors.add(:pi_contribution, "can not be less than 0")
elsif dollar_cap.present? and dollar_cap > 0 and (subsidy_cost / 100.0) > dollar_cap
errors.add(:requested_funding, "can not be greater than the cap of #{dollar_cap}")
elsif percent_cap.present? and percent_cap > 0 and percent_subsidy * 100 > percent_cap
errors.add(:percent_subsidy, "can not be greater than the cap of #{percent_cap}")
elsif max_dollar_cap.present? and max_dollar_cap > 0 and (subsidy_cost / 100.0) > max_dollar_cap
errors.add(:requested_funding, "can not be greater than the cap of #{max_dollar_cap}")
elsif max_percentage.present? and max_percentage > 0 and percent_subsidy * 100 > max_percentage
errors.add(:percent_subsidy, "can not be greater than the cap of #{max_percentage}")
elsif pi_contribution > total_request_cost
errors.add(:pi_contribution, "can not be greater than the total request cost")
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
json.(@subsidies) do |subsidy|

json.request_cost number_to_currency(subsidy.total_at_approval/100.0)
json.percent subsidy.approved_percent_of_total
json.percent subsidy.percent_subsidy
json.pi_contribution number_to_currency(subsidy.pi_contribution/100.0)
json.subsidy_cost number_to_currency(subsidy.approved_cost)
json.approved_by subsidy.approver.try(:full_name)
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ en:
pi_contribution: "PI Contribution"
cost: "Subsidy Current Cost"
approve: "Approve"
add: "Add a Subsidy"
add: "Request a Subsidy"
form:
request_cost: "Current Request Cost:"
percent: "Percent Subsidy:"
Expand Down
13 changes: 7 additions & 6 deletions db/migrate/20160627160134_remove_nexus_tag.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Tag < ActiveRecord::Base
audited
attr_accessible :name
has_many :taggings, dependent: :destroy, class_name: '::ActsAsTaggableOn::Tagging'
end

class RemoveNexusTag < ActiveRecord::Migration

class Tag < ActiveRecord::Base
audited
attr_accessible :name
has_many :taggings, dependent: :destroy, class_name: '::ActsAsTaggableOn::Tagging'
end

def change
Tag.where(name: 'ctrc_clinical_services').destroy_all
end
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20160629155935_create_past_subsidies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class CreatePastSubsidies < ActiveRecord::Migration

class PastSubsidy < ActiveRecord::Base
attr_accessible :pi_contribution
attr_accessible :sub_service_request_id
attr_accessible :total_at_approval
attr_accessible :approved_by
attr_accessible :approved_at
end

def change
create_table :past_subsidies do |t|
t.integer :sub_service_request_id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Percent subsidy is held.
# Calculating percent subsidy based on pi contribution for both
# PastSubsidy and Subsidy tables
class UpdatePercentSubsidyForPastSubsidyAndSubsidy < ActiveRecord::Migration
class Subsidy < ActiveRecord::Base
attr_accessible :percent_subsidy
attr_accessible :pi_contribution
attr_accessible :total_at_approval
end

class PastSubsidy < ActiveRecord::Base
attr_accessible :percent_subsidy
attr_accessible :pi_contribution
attr_accessible :total_at_approval
end

def change
add_column :past_subsidies, :percent_subsidy, :float, default: 0

PastSubsidy.reset_column_information

PastSubsidy.all.each do |subsidy|
pi_contribution = subsidy.pi_contribution
request_cost = subsidy.total_at_approval
if request_cost && pi_contribution && !request_cost.zero?
percent_subsidy = (request_cost - pi_contribution).to_f / request_cost.to_f
subsidy.update_attribute(:percent_subsidy, percent_subsidy)
else
puts "Percent Subsidy for #{subsidy.id} wasn't updated in PastSubsidy table"
end
end

Subsidy.all.each do |subsidy|
pi_contribution = subsidy.pi_contribution
request_cost = subsidy.total_at_approval
if request_cost && pi_contribution && !request_cost.zero?
percent_subsidy = (request_cost - pi_contribution).to_f / request_cost.to_f
subsidy.update_attribute(:percent_subsidy, percent_subsidy)
elsif request_cost && pi_contribution && request_cost.zero? && pi_contribution.zero?
puts "Request cost and pi contribution are both 0 for Subsidy #{subsidy.id}"
else
puts "Percent Subsidy for #{subsidy.id} wasn't updated in Subsidy table"
end
end
remove_column :past_subsidies, :pi_contribution
remove_column :subsidies, :pi_contribution
end
end
5 changes: 2 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160711161202) do
ActiveRecord::Schema.define(version: 20160712191814) do

create_table "admin_rates", force: :cascade do |t|
t.integer "line_item_id", limit: 4
Expand Down Expand Up @@ -529,11 +529,11 @@
create_table "past_subsidies", force: :cascade do |t|
t.integer "sub_service_request_id", limit: 4
t.integer "total_at_approval", limit: 4
t.integer "pi_contribution", limit: 4
t.integer "approved_by", limit: 4
t.datetime "approved_at"
t.datetime "created_at"
t.datetime "updated_at"
t.float "percent_subsidy", limit: 24, default: 0.0
end

add_index "past_subsidies", ["approved_by"], name: "index_past_subsidies_on_approved_by", using: :btree
Expand Down Expand Up @@ -999,7 +999,6 @@
add_index "submission_emails", ["organization_id"], name: "index_submission_emails_on_organization_id", using: :btree

create_table "subsidies", force: :cascade do |t|
t.integer "pi_contribution", limit: 4
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/subsidy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

factory :subsidy do
sub_service_request nil
pi_contribution 0
percent_subsidy 0

trait :without_validations do
to_create { |instance| instance.save(validate: false) }
Expand Down
2 changes: 1 addition & 1 deletion spec/features/service_subsidy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
wait_for_javascript_to_finish
find('#pi_contribution').set("6000\n")
wait_for_javascript_to_finish

subsidy_cost = @direct_cost - 6000
percent_subsidy = ((subsidy_cost / @direct_cost) * 100).round(2)
percent_field_value = find('#percent_subsidy').value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
context "subsidy percentage" do

it "should return the correct subsidy percentage" do
expect(sub_service_request.subsidy_percentage).to eq(80)
expect(sub_service_request.subsidy_percentage).to eq(45)
end
end

Expand Down
5 changes: 3 additions & 2 deletions spec/models/subsidy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
describe "#contribution_caps" do
context "pi contribution is less than 0" do
it "should return an error message" do
subsidy.update_attribute(:pi_contribution, -1)
subsidy.update_attribute(:percent_subsidy, 1.02)
expect(subsidy.contribution_caps).to eq(["can not be less than 0"])
end
end
Expand All @@ -39,6 +39,7 @@
# (subsidy_cost / 100.0) > dollar_cap
# (4000 / 100) > 30
it "should return an error message" do
subsidy.update_attribute(:percent_subsidy, 0.7)
subsidy_map.update_attribute(:max_dollar_cap, 30)
expect(subsidy.contribution_caps).to eq(["can not be greater than the cap of 30.0"])
end
Expand All @@ -56,7 +57,7 @@
# pi_contribution > total_request_cost
# 1000 > 5000
it "should return an error message" do
subsidy.update_attribute(:pi_contribution, 6000)
subsidy.update_attribute(:percent_subsidy, -1)
expect(subsidy.contribution_caps).to eq(["can not be greater than the total request cost"])
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/support/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def build_per_patient_per_visit_services
let!(:clinical_provider) { create(:clinical_provider, organization_id: program.id, identity_id: jug2.id) }
let!(:available_status) { create(:available_status, organization_id: program.id, status: 'submitted')}
let!(:available_status2) { create(:available_status, organization_id: program.id, status: 'draft')}
let!(:subsidy) { Subsidy.auditing_enabled = false; create(:subsidy_without_validations, pi_contribution: 1000, sub_service_request_id: sub_service_request.id)}
let!(:subsidy) { Subsidy.auditing_enabled = false; create(:subsidy_without_validations, percent_subsidy: 0.45, sub_service_request_id: sub_service_request.id)}
let!(:subsidy_map) { create(:subsidy_map, organization_id: program.id) }
end

Expand All @@ -171,7 +171,7 @@ def build_service_request
let!(:core_62) { create(:core, parent_id: program.id, abbreviation: "PWF Services") }
let!(:sub_service_request) { create(:sub_service_request, ssr_id: "0001", service_request_id: service_request.id, organization_id: program.id,status: "draft")}


before :each do
program.tag_list.add("ctrc")

Expand Down