diff --git a/app/models/budget.rb b/app/models/budget.rb index 2828265..5400f6d 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -78,7 +78,18 @@ class Budget return self.progress - self.budget_ratio end - # TODO def profit + return 0.0 unless self.deliverables.size > 0 + + profit = 0.0 + # Fixed profit + profit += self.deliverables.collect(&:profit).delete_if { |d| d.blank?}.inject { |sum, n| sum + n } || 0.0 + + # Percentage Rate + self.deliverables.delete_if { |d| d.profit_percent.blank?}.each do |deliverable| + profit += deliverable.budget * (deliverable.profit_percent.to_f / 100 ) + end + + return profit end end diff --git a/app/views/deliverables/index.html.erb b/app/views/deliverables/index.html.erb index dd1a337..a79f222 100644 --- a/app/views/deliverables/index.html.erb +++ b/app/views/deliverables/index.html.erb @@ -26,7 +26,7 @@ Completion: <%= h distance_of_time_in_words_to_now(@budget.final_due_date) if @budget.final_due_date %>
- Profit: TODO + Profit: <%= h number_to_currency(@budget.profit, :precision => 0) %>
diff --git a/spec/models/budget_spec.rb b/spec/models/budget_spec.rb index b78afeb..0bfb8b0 100644 --- a/spec/models/budget_spec.rb +++ b/spec/models/budget_spec.rb @@ -311,3 +311,42 @@ describe Budget, '.spent' do @budget.spent.should eql(0.0) end end + +describe Budget, '.profit' do + it 'should be calculated by the total profit of the deliverables' do + @project = mock_model(Project) + @deliverable1 = mock_model(HourlyDeliverable, :project_id => @project, :profit => 2000.00, :profit_percent => nil) + @deliverable2 = mock_model(HourlyDeliverable, :project_id => @project, :profit => 3000.00, :profit_percent => nil) + + Deliverable.stub!(:find_all_by_project_id).and_return([@deliverable1, @deliverable2]) + Project.stub!(:find).with(@project.id).and_return(@project) + + @budget = Budget.new(@project.id) + + @budget.profit.should eql(5000.0) + end + + it 'should be calculated by the total profit of the deliverables with % amounts' do + @project = mock_model(Project) + @deliverable1 = mock_model(HourlyDeliverable, :project_id => @project, :profit_percent => 10, :budget => 2000.0, :profit => nil) + @deliverable2 = mock_model(HourlyDeliverable, :project_id => @project, :profit_percent => 10, :budget => 1000.0, :profit => nil) + + Deliverable.stub!(:find_all_by_project_id).and_return([@deliverable1, @deliverable2]) + Project.stub!(:find).with(@project.id).and_return(@project) + + @budget = Budget.new(@project.id) + + @budget.profit.should eql(300.0) + end + + it 'should be 0 if there are no deliverables' do + Deliverable.stub!(:find_all_by_project_id).and_return([]) + + @project = mock_model(Project) + Project.stub!(:find).with(@project.id).and_return(@project) + + @budget = Budget.new(@project.id) + + @budget.profit.should eql(0.0) + end +end