|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright (c) 2006-2017, Puzzle ITC GmbH. This file is part of |
| 4 | +# PuzzleTime and licensed under the Affero General Public License version 3 |
| 5 | +# or later. See the COPYING file at the top-level directory or at |
| 6 | +# https://github.com/puzzle/puzzletime. |
| 7 | + |
| 8 | +module ProgressBarHelper |
| 9 | + # returns the percentage of the budget that is already used, defaulting to 0 for orders with no budget |
| 10 | + def get_order_budget_used_percentage(order) |
| 11 | + order_progress(order)[:percent_title] || 0 |
| 12 | + end |
| 13 | + |
| 14 | + def order_progress_bar(order) |
| 15 | + progress = order_progress(order) |
| 16 | + |
| 17 | + order_progress_bar_link(order.order, progress) do |
| 18 | + ''.html_safe.tap do |content| |
| 19 | + if (progress[:percent]).positive? |
| 20 | + content << content_tag( |
| 21 | + :div, |
| 22 | + nil, |
| 23 | + class: 'progress-bar progress-bar-success', |
| 24 | + style: "width:#{f(progress[:percent])}%" |
| 25 | + ) |
| 26 | + end |
| 27 | + |
| 28 | + if (progress[:over_budget_percent]).positive? |
| 29 | + content << content_tag( |
| 30 | + :div, |
| 31 | + nil, |
| 32 | + class: 'progress-bar progress-bar-danger', |
| 33 | + style: "width:#{f(progress[:over_budget_percent])}%" |
| 34 | + ) |
| 35 | + end |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + private |
| 41 | + |
| 42 | + def order_progress_bar_link(order, progress, &) |
| 43 | + title = "#{f((progress[:percent_title]) || 0)}% geleistet" |
| 44 | + |
| 45 | + if can?(:show, order) |
| 46 | + link_to(order_order_controlling_url(order.id), |
| 47 | + { class: 'progress', title: }, |
| 48 | + &) |
| 49 | + else |
| 50 | + content_tag(:div, yield, class: 'progress', title:) |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + def order_progress(order) |
| 55 | + progress = order_progress_hash |
| 56 | + return progress unless order.offered_amount.positive? |
| 57 | + |
| 58 | + calculate_order_progress(order, progress) |
| 59 | + progress |
| 60 | + end |
| 61 | + |
| 62 | + def order_progress_hash |
| 63 | + { |
| 64 | + percent: 0, |
| 65 | + over_budget_percent: 0 |
| 66 | + } |
| 67 | + end |
| 68 | + |
| 69 | + def calculate_order_progress(order, progress) |
| 70 | + progress[:percent] = 100 / |
| 71 | + order.offered_amount.to_f * |
| 72 | + order.supplied_amount.to_f |
| 73 | + progress[:percent_title] = progress[:percent] |
| 74 | + |
| 75 | + return unless order.supplied_amount.to_f > order.offered_amount.to_f |
| 76 | + |
| 77 | + progress[:over_budget_percent] = |
| 78 | + (order.supplied_amount.to_f - order.offered_amount.to_f) / |
| 79 | + order.supplied_amount.to_f * |
| 80 | + 100 |
| 81 | + progress[:percent] = (100 - progress[:over_budget_percent]) |
| 82 | + end |
| 83 | +end |
0 commit comments