-
Notifications
You must be signed in to change notification settings - Fork 30
[OSU-242] Public estimates #6458
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
Open
diebas
wants to merge
4
commits into
master
Choose a base branch
from
OSU-242/public-estimates
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PublicEstimatesController < ApplicationController | ||
|
|
||
| skip_before_action :authenticate_user! | ||
|
|
||
| def show | ||
| @facilities = Facility.active.alphabetized | ||
| @facility = @facilities.find_by(id: params[:facility_id]) | ||
| @products = @facility ? public_products : Product.none | ||
| @customer_type_options = customer_type_options | ||
| @price_group = PriceGroup.for_public_estimate(params[:customer_type] || "internal") | ||
| @priced_product_ids = priced_product_ids | ||
| @estimate = build_estimate if @price_group && requested_quantities.any? | ||
| @total = @estimate.estimate_details.sum { |estimate_detail| estimate_detail.cost || 0 } if @estimate | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def customer_type_options | ||
| return [[t(".internal"), "internal"], [t(".external"), "external"]] if PriceGroup.secondary_external.blank? | ||
|
|
||
| [ | ||
| [t(".internal"), "internal"], | ||
| [t(".external_for_profit"), "external"], | ||
| [t(".external_non_profit"), "external_non_profit"], | ||
| ] | ||
| end | ||
|
|
||
| def priced_product_ids | ||
| return [] if @price_group.blank? || @products.empty? | ||
|
|
||
| PricePolicy.current_for_date(Time.current).purchaseable | ||
| .where(product_id: @products.map(&:id), price_group: @price_group) | ||
| .distinct.pluck(:product_id) | ||
| end | ||
|
|
||
| def public_products | ||
| @facility.products.active.available_for_estimates.where.not(type: "Bundle").alphabetized | ||
| end | ||
|
|
||
| def requested_quantities | ||
| @requested_quantities ||= | ||
| params[:quantities].presence&.to_unsafe_h&.select { |_id, quantity| quantity.to_i.positive? } || {} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's use strong parameters instead of to_unsafe_h |
||
| end | ||
|
|
||
| def build_estimate | ||
| estimate = Estimate.new(facility: @facility, price_group: @price_group) | ||
|
|
||
| requested_quantities.each do |product_id, quantity| | ||
| product = @products.find_by(id: product_id) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a chance we can make a single db query? |
||
| next if product.blank? | ||
|
|
||
| estimate.estimate_details.build( | ||
| product:, | ||
| quantity: quantity.to_i, | ||
| duration: params.dig(:durations, product_id).presence, | ||
| duration_unit: product.time_unit, | ||
| ) | ||
| end | ||
|
|
||
| estimate.estimate_details.each(&:assign_price_policy_and_cost) | ||
| estimate | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| = content_for :h1 do | ||
| = t(".title") | ||
|
|
||
| %p= t(".intro") | ||
|
|
||
| = form_tag estimate_path, method: :get do | ||
| .inline-form-controls | ||
| %div | ||
| = label_tag :customer_type, t(".customer_type") | ||
| = select_tag :customer_type, options_for_select(@customer_type_options, params[:customer_type]), class: "form-control", onchange: "this.form.submit();" | ||
| .margin_x | ||
| = label_tag :facility_id, Facility.model_name.human | ||
| = select_tag :facility_id, options_from_collection_for_select(@facilities, :id, :name, params[:facility_id]), include_blank: true, class: "form-control", onchange: "this.form.submit();" | ||
|
|
||
| - if @facility.present? | ||
| %h3= t(".choose_products") | ||
| %table.table | ||
| %thead | ||
| %tr | ||
| %th= Product.model_name.human | ||
| %th= EstimateDetail.human_attribute_name(:quantity) | ||
| %th= EstimateDetail.human_attribute_name(:duration) | ||
| %tbody | ||
| - @products.each do |product| | ||
| %tr | ||
| %td= product.name | ||
| - if @priced_product_ids.include?(product.id) | ||
| %td= number_field_tag "quantities[#{product.id}]", params.dig(:quantities, product.id.to_s), min: 0, style: "width: 6em;" | ||
| %td | ||
| - if product.time_unit.present? | ||
| = number_field_tag "durations[#{product.id}]", params.dig(:durations, product.id.to_s), min: 1, style: "width: 6em;" | ||
| = EstimateDetail.human_attribute_name("duration_unit.#{product.time_unit}", count: 2) | ||
| - else | ||
| %td.text-muted{ colspan: 2 }= t(".no_public_rate") | ||
| = submit_tag t(".calculate"), class: "btn btn-primary", name: nil | ||
|
|
||
| - if @estimate.present? | ||
| %h3= t(".results") | ||
| %table.table.table-striped | ||
| %thead | ||
| %tr | ||
| %th= Product.model_name.human | ||
| %th= EstimateDetail.human_attribute_name(:quantity) | ||
| %th= EstimateDetail.human_attribute_name(:duration) | ||
| %th.text-right= EstimateDetail.human_attribute_name(:cost) | ||
| %tbody | ||
| - @estimate.estimate_details.map { |detail| EstimateDetailPresenter.new(detail) }.each do |estimate_detail| | ||
| %tr | ||
| %td= estimate_detail.product_display | ||
| %td= estimate_detail.quantity | ||
| %td= estimate_detail.duration_display | ||
| %td.text-right= estimate_detail.cost ? estimate_detail.cost_display : t(".no_public_rate") | ||
| .text-right | ||
| %strong= t(".total") | ||
| %span= number_to_currency(@total) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - if SettingsHelper.feature_on?(:public_estimates) | ||
| %li= link_to t("pages.public_estimate"), estimate_path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rails_helper" | ||
|
|
||
| RSpec.describe EstimateDetail do | ||
| let(:facility) { create(:setup_facility) } | ||
| let(:price_group) { facility.price_groups.first } | ||
| let!(:item) { create(:setup_item, facility:) } | ||
| let!(:item_price_policy) do | ||
| create(:item_price_policy, product: item, price_group:, unit_cost: 25, unit_subsidy: 5) | ||
| end | ||
|
|
||
| describe "#assign_price_policy_and_cost without a user" do | ||
| let(:persisted_detail) do | ||
| estimate = create(:estimate, facility:, price_group:) | ||
| estimate.estimate_details.create!(product: item, quantity: 3) | ||
| end | ||
|
|
||
| let(:anonymous_detail) do | ||
| estimate = Estimate.new(facility:, price_group:) | ||
| estimate.estimate_details.build(product: item, quantity: 3) | ||
| end | ||
|
|
||
| it "resolves the price policy on an unsaved record" do | ||
| expect(anonymous_detail.assign_price_policy_and_cost).to be true | ||
| expect(anonymous_detail.price_policy).to eq(item_price_policy) | ||
| end | ||
|
|
||
| it "computes the same cost as the persisted equivalent" do | ||
| anonymous_detail.assign_price_policy_and_cost | ||
|
|
||
| expect(anonymous_detail.cost).to eq(persisted_detail.cost) | ||
| expect(anonymous_detail.cost).to eq(60) | ||
| end | ||
|
|
||
| it "persists nothing" do | ||
| expect { anonymous_detail.assign_price_policy_and_cost }.not_to change(EstimateDetail, :count) | ||
| expect(anonymous_detail).not_to be_persisted | ||
| end | ||
|
|
||
| it "returns false when no price policy matches the price group" do | ||
| other_item = create(:setup_item, facility:) | ||
| detail = Estimate.new(facility:, price_group:).estimate_details.build(product: other_item, quantity: 1) | ||
|
|
||
| expect(detail.assign_price_policy_and_cost).to be false | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have this predefined to base and external and allow each school override it? what do you think?