Skip to content

Instantly share code, notes, and snippets.

@EvgenyKungurov
Created October 23, 2019 08:07
Show Gist options
  • Save EvgenyKungurov/309823e2a752e7ec5dc768e59cf2442e to your computer and use it in GitHub Desktop.
Save EvgenyKungurov/309823e2a752e7ec5dc768e59cf2442e to your computer and use it in GitHub Desktop.
salary_income_spec.rb
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Salary::Income::Invoice::Create do
subject { described_class.call(invoice) }
let!(:employee) { create :employee, team: team }
let!(:invoice) { create :invoice, act_from: period_begin, act_to: period_end }
let!(:redmine_project) { create :redmine_project, client: invoice.client }
let!(:team) { create :team, redmine_project: redmine_project }
let(:amount_by_days) { invoice.total_price_rub / BigDecimal(invoice.period.to_a.size) }
let(:period_begin) { Date.new(2019, 1, 1) }
let(:period_end) { Date.new(2019, 3, 31) }
context 'when multiple contracts' do
let!(:project_contract) { create(:project_contract, redmine_project: team.redmine_project, team: team, date_from: period_begin, date_to: period_end) }
let!(:salary_contract_one) { create :salary_contract, employee: employee, invoice_quota: 3, period: Date.new(2019, 1, 1)..Date.new(2019, 1, 31) }
let!(:salary_contract_two) { create :salary_contract, employee: employee, invoice_quota: 4, period: Date.new(2019, 2, 1)..Date.new(2019, 2, 15) }
let!(:salary_contract_three) { create :salary_contract, employee: employee, invoice_quota: 5, period: Date.new(2019, 2, 16)..Date.new(2019, 2, 28) }
let(:january_amount) { amount_by_days * (salary_contract_one.period.begin..salary_contract_one.period.end).to_a.size }
let(:february_amount) { amount_by_days * (salary_contract_two.period.begin..salary_contract_two.period.end).to_a.size }
let(:march_amount) { amount_by_days * (salary_contract_three.period.begin..salary_contract_three.period.end).to_a.size }
let(:amount_one) { january_amount * salary_contract_one.invoice_quota / 100 }
let(:amount_two) { february_amount * salary_contract_two.invoice_quota / 100 }
let(:amount_three) { march_amount * salary_contract_three.invoice_quota / 100 }
it 'should create income for all contracts' do
expect(Salary::Income).to receive(:create!)
.with(employee: employee, owner: invoice, contract: salary_contract_one, amount: amount_one, date: salary_contract_one.period.end.end_of_month)
expect(Salary::Income).to receive(:create!)
.with(employee: employee, owner: invoice, contract: salary_contract_two, amount: amount_two, date: salary_contract_two.period.end.end_of_month)
expect(Salary::Income).to receive(:create!)
.with(employee: employee, owner: invoice, contract: salary_contract_three, amount: amount_three, date: salary_contract_three.period.end.end_of_month)
subject
end
end
context 'when contract one and period long than 1 month' do
let!(:project_contract) { create(:project_contract, redmine_project: team.redmine_project, team: team, date_from: period_begin, date_to: period_end) }
let!(:salary_contract) { create(:salary_contract, employee: employee, invoice_quota: 3, period: period_begin..Date.new(2019, 3, 15)) }
let(:january_amount) { amount_by_days * 31 }
let(:february_amount) { amount_by_days * 28 }
let(:march_amount) { amount_by_days * 15 }
let(:amount_one) { january_amount * salary_contract.invoice_quota / 100 }
let(:amount_two) { february_amount * salary_contract.invoice_quota / 100 }
let(:amount_three) { march_amount * salary_contract.invoice_quota / 100 }
it 'should create partial incomes' do
expect(Salary::Income).to receive(:create!)
.with(employee: employee, owner: invoice, contract: salary_contract, amount: amount_one, date: Date.new(2019, 1, 31))
expect(Salary::Income).to receive(:create!)
.with(employee: employee, owner: invoice, contract: salary_contract, amount: amount_two, date: Date.new(2019, 2, 28))
expect(Salary::Income).to receive(:create!)
.with(employee: employee, owner: invoice, contract: salary_contract, amount: amount_three, date: Date.new(2019, 3, 31))
subject
end
end
context 'when invoice period includes multiple project contracts' do
let!(:salary_contract_one) { create(:salary_contract, invoice_quota: 3, period: period_begin..period_end, employee: create(:employee, team: team_one)) }
let!(:salary_contract_two) { create(:salary_contract, invoice_quota: 4, period: period_begin..period_end, employee: create(:employee, team: team_two)) }
let!(:team_one) { create :team, redmine_project: redmine_project }
let!(:team_two) { create :team, redmine_project: redmine_project }
let!(:project_contract_one) { create(:project_contract, redmine_project: team_one.redmine_project, team: team_one, date_from: period_begin, date_to: period_begin + 5.days) }
let!(:project_contract_two) { create(:project_contract, redmine_project: team_two.redmine_project, team: team_two, date_from: period_begin + 6.days, date_to: period_end) }
let(:january_amount_one) { amount_by_days * 6 }
let(:january_amount_two) { amount_by_days * (31 - 6) }
let(:february_amount) { amount_by_days * 28 }
let(:march_amount) { amount_by_days * 31 }
let(:amount_one) { january_amount_one * salary_contract_one.invoice_quota / 100 }
let(:amount_two) { january_amount_two * salary_contract_two.invoice_quota / 100 }
let(:amount_three) { february_amount * salary_contract_two.invoice_quota / 100 }
let(:amount_four) { march_amount * salary_contract_two.invoice_quota / 100 }
it 'should create income for two team' do
expect(Salary::Income).to receive(:create!)
.with(employee: salary_contract_one.employee, owner: invoice, contract: salary_contract_one, amount: amount_one, date: Date.new(2019, 1, 31))
expect(Salary::Income).to receive(:create!)
.with(employee: salary_contract_two.employee, owner: invoice, contract: salary_contract_two, amount: amount_two, date: Date.new(2019, 1, 31))
expect(Salary::Income).to receive(:create!)
.with(employee: salary_contract_two.employee, owner: invoice, contract: salary_contract_two, amount: amount_three, date: Date.new(2019, 2, 28))
expect(Salary::Income).to receive(:create!)
.with(employee: salary_contract_two.employee, owner: invoice, contract: salary_contract_two, amount: amount_four, date: Date.new(2019, 3, 31))
subject
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment