Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[32664] Fix trying to parse currency string in costs value

Oliver Günther54e30cad7d6f
diff --git a/modules/costs/app/controllers/cost_objects_controller.rb b/modules/costs/app/controllers/cost_objects_controller.rb
index 4c3c76f39e6..9287b1478c6 100644
--- a/modules/costs/app/controllers/cost_objects_controller.rb
+++ b/modules/costs/app/controllers/cost_objects_controller.rb
@@ -199,9 +199,13 @@ class CostObjectsController < ApplicationController
@unit = cost_type.try(:unit_plural) || ''
end
- response = { "#{@element_id}_unit_name" => h(@unit) }
+ response = {
+ "#{@element_id}_unit_name" => h(@unit),
+ "#{@element_id}_currency" => Setting.plugin_openproject_costs['costs_currency']
+ }
if current_user.allowed_to?(:view_cost_rates, @project)
response["#{@element_id}_costs"] = number_to_currency(@costs)
+ response["#{@element_id}_cost_value"] = @costs
end
respond_to do |format|
@@ -222,9 +226,13 @@ class CostObjectsController < ApplicationController
@costs = 0.0
end
- response = { "#{@element_id}_unit_name" => h(@unit) }
+ response = {
+ "#{@element_id}_unit_name" => h(@unit),
+ "#{@element_id}_currency" => Setting.plugin_openproject_costs['costs_currency']
+ }
if current_user.allowed_to?(:view_hourly_rates, @project)
response["#{@element_id}_costs"] = number_to_currency(@costs)
+ response["#{@element_id}_cost_value"] = @costs
end
respond_to do |format|
diff --git a/modules/costs/app/views/cost_objects/items/_labor_budget_item.html.erb b/modules/costs/app/views/cost_objects/items/_labor_budget_item.html.erb
index 49591ceb12c..b690c0a5e25 100644
--- a/modules/costs/app/views/cost_objects/items/_labor_budget_item.html.erb
+++ b/modules/costs/app/views/cost_objects/items/_labor_budget_item.html.erb
@@ -80,9 +80,16 @@ See docs/COPYRIGHT.rdoc for more details.
<% if templated == false && !labor_budget_item.new_record? && labor_budget_item.overridden_budget? %>
<%= cost_form.hidden_field :budget, value: labor_budget_item.budget %>
<% end %>
- <cost-unit-subform obj-id="<%= "#{id_prefix}_costs" %>" obj-name="<%= "#{name_prefix}[budget]" %>">
+
+ <% cost_value = labor_budget_item.budget || labor_budget_item.calculated_costs(@cost_object.fixed_date, @cost_object.project_id) %>
+ <%= cost_form.hidden_field :currency, index: id_or_index, value: Setting.plugin_openproject_costs['costs_currency'] %>
+ <cost-unit-subform obj-id="<%= id_prefix %>"
+ obj-name="<%= "#{name_prefix}[budget]" %>">
<a id="<%= "#{id_prefix}_costs" %>" class="costs--edit-planned-costs-btn icon-context icon-edit" title="<%= t(:help_click_to_edit) %>">
- <%= number_to_currency(labor_budget_item.budget || labor_budget_item.calculated_costs(@cost_object.fixed_date, @cost_object.project_id)) if labor_budget_item.costs_visible_by?(User.current) %>
+ <% if labor_budget_item.costs_visible_by?(User.current) %>
+ <%= cost_form.hidden_field :cost_value, index: id_or_index, value: cost_value %>
+ <%= number_to_currency(cost_value) %>
+ <% end %>
</a>
</cost-unit-subform>
</td>
diff --git a/modules/costs/app/views/cost_objects/items/_material_budget_item.html.erb b/modules/costs/app/views/cost_objects/items/_material_budget_item.html.erb
index 0566c6afb07..427e861f856 100644
--- a/modules/costs/app/views/cost_objects/items/_material_budget_item.html.erb
+++ b/modules/costs/app/views/cost_objects/items/_material_budget_item.html.erb
@@ -78,14 +78,17 @@ See docs/COPYRIGHT.rdoc for more details.
</td>
<% if User.current.allowed_to? :view_cost_rates, @project %>
<td class="currency budget-table--fields">
- <% obj_id = "#{id_prefix}_costs" %>
<%# Keep current budget as hidden field because otherwise they will be overridden %>
<% if templated == false && !material_budget_item.new_record? && material_budget_item.overridden_budget? %>
<%= cost_form.hidden_field :budget, value: material_budget_item.budget %>
<% end %>
- <cost-unit-subform obj-id="<%= obj_id %>" obj-name="<%= "#{name_prefix}[budget]" %>">
- <a id="<%= obj_id %>" class="costs--edit-planned-costs-btn icon-context icon-edit" role="button" title="<%= t(:help_click_to_edit) %>">
- <%= number_to_currency(material_budget_item.budget || material_budget_item.calculated_costs(@cost_object.fixed_date)) %>
+ <% cost_value = material_budget_item.budget || material_budget_item.calculated_costs(@cost_object.fixed_date) %>
+ <%= cost_form.hidden_field :currency, index: id_or_index, value: Setting.plugin_openproject_costs['costs_currency'] %>
+ <%= cost_form.hidden_field :cost_value, index: id_or_index, value: cost_value %>
+ <cost-unit-subform obj-id="<%= id_prefix %>"
+ obj-name="<%= "#{name_prefix}[budget]" %>">
+ <a id="<%= id_prefix %>_costs" class="costs--edit-planned-costs-btn icon-context icon-edit" role="button" title="<%= t(:help_click_to_edit) %>">
+ <%= number_to_currency(cost_value) %>
</a>
</cost-unit-subform>
</td>
diff --git a/modules/costs/app/views/costlog/edit.html.erb b/modules/costs/app/views/costlog/edit.html.erb
index b7d5a449e06..95c305bc172 100644
--- a/modules/costs/app/views/costlog/edit.html.erb
+++ b/modules/costs/app/views/costlog/edit.html.erb
@@ -113,7 +113,8 @@ See docs/COPYRIGHT.rdoc for more details.
<div class="form--field">
<label for="cost_entry_costs_edit" class="form--label"><%= CostEntry.human_attribute_name(:costs) %></label>
<span class="form--field-container">
- <cost-unit-subform obj-id="cost_entry_costs" obj-name="cost_entry[overridden_costs]">
+ <cost-unit-subform obj-id="cost_entry"
+ obj-name="cost_entry[overridden_costs]" %>
<% if User.current.allowed_to? :view_cost_rates, @cost_entry.project %>
<a href="#" id="cost_entry_costs" class="costs--edit-planned-costs-btn icon-context icon-edit" title="<%= t(:help_click_to_edit) %>">
<%= number_to_currency(@cost_entry.real_costs) %>
diff --git a/modules/costs/frontend/module/augment/cost-budget-subform.augment.service.ts b/modules/costs/frontend/module/augment/cost-budget-subform.augment.service.ts
index 74463967315..895fc35a8ab 100644
--- a/modules/costs/frontend/module/augment/cost-budget-subform.augment.service.ts
+++ b/modules/costs/frontend/module/augment/cost-budget-subform.augment.service.ts
@@ -90,7 +90,12 @@ export class CostBudgetSubformAugmentService {
.subscribe(
(data:any) => {
_.each(data, (val:string, selector:string) => {
- jQuery('#' + selector).html(val);
+ let element = document.getElementById(selector) as HTMLElement|HTMLInputElement|undefined;
+ if (element instanceof HTMLInputElement) {
+ element.value = val;
+ } else if (element) {
+ element.textContent = val;
+ }
});
},
(error:any) => this.halNotification.handleRawError(error)
diff --git a/modules/costs/frontend/module/augment/planned-costs-form.ts b/modules/costs/frontend/module/augment/planned-costs-form.ts
index 560c79852c7..def2085cf98 100644
--- a/modules/costs/frontend/module/augment/planned-costs-form.ts
+++ b/modules/costs/frontend/module/augment/planned-costs-form.ts
@@ -42,35 +42,36 @@ export class PlannedCostsFormAugment {
constructor(public $element:JQuery) {
this.objId = this.$element.attr('obj-id')!;
this.objName = this.$element.attr('obj-name')!;
- this.obj = jQuery(`#${this.objId}`) as any;
+ this.obj = jQuery(`#${this.objId}_costs`) as any;
- this.makeEditable('#' + this.objId, this.objName);
+ this.makeEditable();
}
- private getCurrencyValue(str:string) {
- var result = str.match(/^\s*(([0-9]+[.,])+[0-9]+) (.+)\s*/);
- return result ? new Array(result[1], result[3]) : new Array(str, "");
- }
-
- public makeEditable(id:string, name:string) {
+ public makeEditable() {
this.edit_and_focus();
}
private edit_and_focus() {
this.edit();
- jQuery('#' + this.objId + '_edit').trigger('focus');
- jQuery('#' + this.objId + '_edit').trigger('select');
+ jQuery('#' + this.objId + '_costs_edit').trigger('focus');
+ jQuery('#' + this.objId + '_costs_edit').trigger('select');
+ }
+
+ private getCurrency() {
+ return jQuery('#' + this.objId + '_currency').val();
+ }
+
+ private getValue() {
+ return jQuery('#' + this.objId + '_cost_value').val();
}
private edit() {
this.obj.hide();
- let obj_value = this.obj[0].innerHTML;
let id = this.obj[0].id;
- let parsed = this.getCurrencyValue(obj_value);
- let value = parsed[0];
- let currency = parsed[1];
+ let currency = this.getCurrency();
+ let value = this.getValue();
let name = this.objName;
let template = `
@@ -87,12 +88,11 @@ export class PlannedCostsFormAugment {
</section>
`;
-
jQuery(template).insertAfter(this.obj);
let that = this;
- jQuery('#' + id + '_cancel').on('click', function () {
- jQuery('#' + id + '_section').remove();
+ jQuery('#' + id + '_costs_cancel').on('click', function () {
+ jQuery('#' + id + '_costs_section').remove();
that.obj.show();
return false;
});
diff --git a/modules/costs/spec/features/budgets/update_budget_spec.rb b/modules/costs/spec/features/budgets/update_budget_spec.rb
index b0f53b2b9dd..030b9fd834c 100644
--- a/modules/costs/spec/features/budgets/update_budget_spec.rb
+++ b/modules/costs/spec/features/budgets/update_budget_spec.rb
@@ -29,7 +29,7 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
describe 'updating a budget', type: :feature, js: true do
- let(:project) { FactoryBot.create :project_with_types }
+ let(:project) { FactoryBot.create :project_with_types, enabled_module_names: %i[costs_module] }
let(:user) { FactoryBot.create :admin }
let(:budget) { FactoryBot.create :cost_object, author: user, project: project }
@@ -79,14 +79,14 @@ describe 'updating a budget', type: :feature, js: true do
let(:material_budget_item) do
FactoryBot.create :material_budget_item, units: 3,
- cost_type: cost_type,
- cost_object: budget
+ cost_type: cost_type,
+ cost_object: budget
end
let(:labor_budget_item) do
FactoryBot.create :labor_budget_item, hours: 5,
- user: user,
- cost_object: budget
+ user: user,
+ cost_object: budget
end
let(:budget_page) { Pages::EditBudget.new budget.id }
@@ -110,10 +110,10 @@ describe 'updating a budget', type: :feature, js: true do
budget_page.expect_planned_costs! type: :labor, row: 1, expected: '125.00 EUR'
budget_page.edit_unit_costs! material_budget_item.id, units: 5,
- comment: 'updated num stimpaks'
+ comment: 'updated num stimpaks'
budget_page.edit_labor_costs! labor_budget_item.id, hours: 3,
- user_name: user.name,
- comment: 'updated treatment duration'
+ user_name: user.name,
+ comment: 'updated treatment duration'
# Test for updated planned costs (Regression #31247)
budget_page.expect_planned_costs! type: :material, row: 1, expected: '250.00 EUR'
@@ -136,8 +136,8 @@ describe 'updating a budget', type: :feature, js: true do
context 'with two material budget items' do
let!(:material_budget_item_2) do
FactoryBot.create :material_budget_item, units: 5,
- cost_type: cost_type,
- cost_object: budget
+ cost_type: cost_type,
+ cost_object: budget
end
it 'keeps previous planned material costs (Regression test #27692)' do
@@ -169,13 +169,52 @@ describe 'updating a budget', type: :feature, js: true do
expect(material_budget_item_2.overridden_budget?).to be_truthy
expect(material_budget_item_2.costs).to eq(543.0)
end
+
+ context 'with a reversed currency format' do
+
+ before do
+ allow(Setting)
+ .to receive(:plugin_openproject_costs)
+ .and_return({costs_currency_format: '%u %n', costs_currency: 'USD'}.with_indifferent_access)
+ end
+
+ it 'can still update budgets (Regression test #32664)' do
+ budget_page.visit!
+ click_on 'Update'
+
+ # Update first element
+ budget_page.edit_planned_costs! material_budget_item.id, type: :material, costs: 123
+ expect(budget_page).to have_content('Successful update')
+ expect(page).to have_selector('tbody td.currency', text: 'USD 123.00')
+
+ click_on 'Update'
+
+ # Update second element
+ budget_page.edit_planned_costs! material_budget_item_2.id, type: :material, costs: 543
+ expect(budget_page).to have_content('Successful update')
+ expect(page).to have_selector('tbody td.currency', text: 'USD 123.00')
+ expect(page).to have_selector('tbody td.currency', text: 'USD 543.00')
+
+ # Expect overridden costs on both
+ material_budget_item.reload
+ material_budget_item_2.reload
+
+ # Expect budget == costs
+ expect(material_budget_item.budget).to eq(123.0)
+ expect(material_budget_item.overridden_budget?).to be_truthy
+ expect(material_budget_item.costs).to eq(123.0)
+ expect(material_budget_item_2.budget).to eq(543.0)
+ expect(material_budget_item_2.overridden_budget?).to be_truthy
+ expect(material_budget_item_2.costs).to eq(543.0)
+ end
+ end
end
context 'with two labor budget items' do
let!(:labor_budget_item_2) do
FactoryBot.create :labor_budget_item, hours: 5,
- user: user,
- cost_object: budget
+ user: user,
+ cost_object: budget
end
it 'keeps previous planned labor costs (Regression test #27692)' do
@@ -207,6 +246,45 @@ describe 'updating a budget', type: :feature, js: true do
expect(labor_budget_item_2.overridden_budget?).to be_truthy
expect(labor_budget_item_2.costs).to eq(987.0)
end
+
+ context 'with a reversed currency format' do
+
+ before do
+ allow(Setting)
+ .to receive(:plugin_openproject_costs)
+ .and_return({costs_currency_format: '%u %n', costs_currency: 'USD'}.with_indifferent_access)
+ end
+
+ it 'can still update budgets (Regression test #32664)' do
+ budget_page.visit!
+ click_on 'Update'
+
+ # Update first element
+ budget_page.edit_planned_costs! labor_budget_item.id, type: :labor, costs: 456
+ expect(budget_page).to have_content('Successful update')
+ expect(page).to have_selector('tbody td.currency', text: 'USD 456.00')
+
+ click_on 'Update'
+
+ # Update second element
+ budget_page.edit_planned_costs! labor_budget_item_2.id, type: :labor, costs: 987
+ expect(budget_page).to have_content('Successful update')
+ expect(page).to have_selector('tbody td.currency', text: 'USD 456.00')
+ expect(page).to have_selector('tbody td.currency', text: 'USD 987.00')
+
+ # Expect overridden costs on both
+ labor_budget_item.reload
+ labor_budget_item_2.reload
+
+ # Expect budget == costs
+ expect(labor_budget_item.budget).to eq(456.0)
+ expect(labor_budget_item.overridden_budget?).to be_truthy
+ expect(labor_budget_item.costs).to eq(456.0)
+ expect(labor_budget_item_2.budget).to eq(987.0)
+ expect(labor_budget_item_2.overridden_budget?).to be_truthy
+ expect(labor_budget_item_2.costs).to eq(987.0)
+ end
+ end
end
it 'removes existing cost items' do