Added Budget.profit. #1137
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
Completion: <%= h distance_of_time_in_words_to_now(@budget.final_due_date) if @budget.final_due_date %>
|
||||
</p>
|
||||
<p>
|
||||
Profit: TODO
|
||||
Profit: <%= h number_to_currency(@budget.profit, :precision => 0) %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user