diff --git a/app/controllers/costlog_controller.rb b/app/controllers/costlog_controller.rb index 2f257e9cdd..44e891dc72 100644 --- a/app/controllers/costlog_controller.rb +++ b/app/controllers/costlog_controller.rb @@ -91,6 +91,9 @@ class CostlogController < ApplicationController new_user ||= User.current @cost_entry = CostEntry.new(:project => @project, :issue => @issue, :user => new_user, :spent_on => Date.today) end + if params[:cost_entry].is_a?(Hash) && !params[:cost_entry][:costs] + params[:cost_entry][:costs] = nil + end @cost_entry.attributes = params[:cost_entry] @cost_entry.cost_type ||= CostType.default diff --git a/app/controllers/deliverables_controller.rb b/app/controllers/deliverables_controller.rb index 8621ba7078..0413192a2d 100644 --- a/app/controllers/deliverables_controller.rb +++ b/app/controllers/deliverables_controller.rb @@ -147,7 +147,6 @@ class DeliverablesController < ApplicationController def update_deliverable_cost element_id = params[:element_id] if params.has_key? :element_id - render_403 and return unless element_id =~ /^deliverable_(existing|new)_deliverable_cost_attributes_[0-9]+$/ cost_type = CostType.find(params[:cost_type_id]) if params.has_key? :cost_type_id @@ -168,8 +167,7 @@ class DeliverablesController < ApplicationController def update_deliverable_hour element_id = params[:element_id] if params.has_key? :element_id - render_403 and return unless element_id =~ /^deliverable_(existing|new)_deliverable_hour_attributes_[0-9]+$/ - + user = User.find(params[:user_id]) hours = params[:hours].to_hours diff --git a/app/models/cost_based_deliverable.rb b/app/models/cost_based_deliverable.rb index a8a290756c..94e9b8323c 100644 --- a/app/models/cost_based_deliverable.rb +++ b/app/models/cost_based_deliverable.rb @@ -98,12 +98,12 @@ class CostBasedDeliverable < Deliverable private def clean_currency(value) - if value + if value && value.is_a?(String) value = value.strip value.gsub!(l(:currency_delimiter), '') if value.include?(l(:currency_delimiter)) && value.include?(l(:currency_seperator)) value.gsub(',', '.') else - nil + value end end end \ No newline at end of file diff --git a/app/models/cost_entry.rb b/app/models/cost_entry.rb index cbf1c32cc0..1aaaaacdb9 100644 --- a/app/models/cost_entry.rb +++ b/app/models/cost_entry.rb @@ -33,8 +33,20 @@ class CostEntry < ActiveRecord::Base errors.add :user_id, :activerecord_error_invalid unless (user == User.current) || (User.current.allowed_to? :book_costs, project) end +# def overridden_costs=(value) +# self.attributes[:overridden_costs] = value +# end + def costs - @costs || units * cost_type.rate_at(self.spent_on).rate + self.overridden_costs || calculated_costs + end + + def costs=(value) + self.overridden_costs = value + end + + def calculated_costs + units * cost_type.rate_at(self.spent_on).rate rescue 0.0 end @@ -50,4 +62,14 @@ class CostEntry < ActiveRecord::Base end end +private + def clean_currency(value) + if value && value.is_a?(String) + value = value.strip + value.gsub!(l(:currency_delimiter), '') if value.include?(l(:currency_delimiter)) && value.include?(l(:currency_seperator)) + value.gsub(',', '.') + else + value + end + end end diff --git a/app/views/costlog/edit.rhtml b/app/views/costlog/edit.rhtml index 4d68ea2593..897b4d29ed 100644 --- a/app/views/costlog/edit.rhtml +++ b/app/views/costlog/edit.rhtml @@ -13,11 +13,26 @@
<%= f.select :cost_type_id, cost_types_collection_for_select_options, :required => true %>
<%= f.text_field :units, :size => 6, :required => true %> - <%= render :partial => "cost_type_unit_plural", :locals => {:cost_type => @cost_entry.cost_type} %> - <%= observe_field ('cost_entry_cost_type_id', :update => 'cost_type_unit_plural', :url => {:action => :get_cost_type_unit_plural}, :with => :cost_type_id) %> + <%= @cost_entry.units == 1 ? @cost_entry.cost_type.unit : @cost_entry.cost_type.unit_plural %> + <%= observe_field( "cost_entry_cost_type_id", :url => {:controller => :deliverables, :action => :update_deliverable_cost, :project_id => @cost_entry.project}, :with => "'cost_type_id=' + encodeURIComponent(value) + '&units=' + encodeURIComponent(document.getElementById('cost_entry_units').value) + '&fixed_date=' + encodeURIComponent(document.getElementById('cost_entry_spent_on').value) + '&element_id=cost_entry'") %> + <%= observe_field( "cost_entry_units", :frequency => 1, :url => {:controller => :deliverables, :action => :update_deliverable_cost, :project_id => @cost_entry.project}, :with => "'cost_type_id=' + encodeURIComponent(document.getElementById('cost_entry_cost_type_id').value) + '&units=' + encodeURIComponent(value) + '&fixed_date=' + encodeURIComponent(document.getElementById('cost_entry_spent_on').value) + '&element_id=cost_entry'") %> + <%= observe_field( "cost_entry_spent_on", :frequency => 1, :url => {:controller => :deliverables, :action => :update_deliverable_cost, :project_id => @cost_entry.project}, :with => "'cost_type_id=' + encodeURIComponent(document.getElementById('cost_entry_cost_type_id').value) + '&units=' + encodeURIComponent(document.getElementById('cost_entry_units').value) + '&fixed_date=' + encodeURIComponent(value) + '&element_id=cost_entry'") %> +
++ + <%= number_to_currency(@cost_entry.calculated_costs) %> + <%= update_page_tag do |page| + page << "makeEditable('cost_entry_costs', 'cost_entry[costs]');" + page << "edit($('cost_entry_costs'), 'cost_entry[budget]', '#{number_to_currency(@cost_entry.overridden_costs)}');" if @cost_entry.overridden_costs + end %> +
<%= f.text_field :comments, :size => 100 %>
<%= submit_tag l(:button_save) %> <% end %> + +<% content_for :header_tags do %> + <%= javascript_include_tag 'editinplace', :plugin => 'redmine_costs' %> +<% end %> diff --git a/app/views/deliverables/_show_cost_based_deliverable.rhtml b/app/views/deliverables/_show_cost_based_deliverable.rhtml index 4792331352..fdf3333ae6 100644 --- a/app/views/deliverables/_show_cost_based_deliverable.rhtml +++ b/app/views/deliverables/_show_cost_based_deliverable.rhtml @@ -36,10 +36,13 @@ cost_entries = issue.cost_entries.inject(Hash.new) do |results, entry| result = results[entry.cost_type.id.to_s] unless result - result = CostEntry.new(:cost_type => entry.cost_type, :deliverable => @deliverable, :costs => 0, :units => 0) + result = CostEntry.new(:cost_type => entry.cost_type, :deliverable => @deliverable, :overridden_costs => 0.0, :units => 0) results[entry.cost_type.id.to_s] = result end + p result.costs + p entry.costs + result.costs += entry.costs result.units += entry.units results diff --git a/db/migrate/010_add_free_text_to_budget_and_entries.rb b/db/migrate/010_add_free_text_to_budget_and_entries.rb index 79afde4fde..23d856324a 100644 --- a/db/migrate/010_add_free_text_to_budget_and_entries.rb +++ b/db/migrate/010_add_free_text_to_budget_and_entries.rb @@ -3,13 +3,13 @@ class AddFreeTextToBudgetAndEntries < ActiveRecord::Migration add_column :deliverable_costs, :budget, :decimal, :precision => 15, :scale => 2, :null => true add_column :deliverable_hours, :budget, :decimal, :precision => 15, :scale => 2, :null => true - add_column :cost_entries, :costs, :decimal, :precision => 15, :scale => 2, :null => true + add_column :cost_entries, :overridden_cost, :decimal, :precision => 15, :scale => 2, :null => true end def self.down remove_column :deliverable_costs, :budget remove_column :deliverable_hours, :budget - remove_column :cost_entries, :costs + remove_column :cost_entries, :overridden_cost end end \ No newline at end of file