Created
March 25, 2014 17:30
-
-
Save guipdutra/9766913 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'spec_helper' | |
| describe YearlyPaymentIssuer do | |
| let(:issuer) { YearlyPaymentIssuer.new } | |
| it { should validate_presence_of(:city_hall_id) } | |
| it { should validate_presence_of(:incidence) } | |
| it { should validate_presence_of(:payment_type) } | |
| it { should validate_presence_of(:installments) } | |
| it { should validate_presence_of(:first_installment) } | |
| it { should validate_presence_of(:first_installment_number) } | |
| it { should validate_presence_of(:first_competence) } | |
| it { should validate_presence_of(:competences_type) } | |
| it { should validate_presence_of(:due_months_type) } | |
| it "should validate discount" do | |
| issuer.should_not allow_value(-10).for(:discount) | |
| issuer.should allow_value(0).for(:discount) | |
| issuer.should allow_value(99).for(:discount) | |
| issuer.should_not allow_value(100).for(:discount) | |
| issuer.should_not allow_value(101).for(:discount) | |
| end | |
| context ".registration_str" do | |
| it "should accept registration date via accessor" do | |
| issuer.registration_str = "03/2011" | |
| issuer.registration.should == Date.civil(2011, 3, 1) | |
| end | |
| it "should give back formated registration via attribute" do | |
| issuer.registration = Date.civil(2011, 3, 1) | |
| issuer.registration_str.should == "03/2011" | |
| end | |
| it { should allow_value("").for(:registration_str) } | |
| it { should allow_value("03/2010").for(:registration_str) } | |
| it { should allow_value("12/2011").for(:registration_str) } | |
| it { should_not allow_value("13/#{Time.zone.now.year}").for(:registration_str) } | |
| it { should_not allow_value("12/#{Time.zone.now.year+1}").for(:registration_str) } | |
| end | |
| it "should always require at least one installment to be generated" do | |
| issuer.installments = 6 | |
| issuer.first_installment = 7 | |
| issuer.should have(1).error_on(:first_installment) | |
| end | |
| context "custom_competences?" do | |
| it 'should return true, when competences_type == "custom"' do | |
| issuer.competences_type = "custom" | |
| issuer.custom_competences?.should be_true | |
| end | |
| it 'should return false, when competences_type == "consecutive"' do | |
| issuer.competences_type = "consecutive" | |
| issuer.custom_competences?.should be_false | |
| end | |
| end | |
| context ".find_taxpayers" do | |
| before do | |
| @issuer = issuer | |
| @issuer.city_hall = city_halls(:main) | |
| @issuer.incidence = "2011" | |
| end | |
| it "should return unfiltered without errors if no criteria is set" do | |
| expect { | |
| @issuer.find_taxpayers | |
| }.to_not raise_error | |
| end | |
| it "should return filtered results if criterias are supplied for yearly rate" do | |
| @issuer.payment_type = "yearly_rate" | |
| @issuer.installments = 6 | |
| @issuer.value_cents = 15000 | |
| @issuer.find_taxpayers.size.should == 1 | |
| end | |
| it "should return filtered results if criterias are supplied for estimative" do | |
| @issuer.payment_type = "estimative" | |
| @issuer.installments = 12 | |
| @issuer.value_cents = 10000 | |
| @issuer.find_taxpayers.size.should == 1 | |
| end | |
| context "when the city_hall is configured to apply diferent value to professional society" do | |
| before do | |
| city_hall = city_halls(:main) | |
| indicator = FactoryGirl.create(:monetary_indicator, :frequency => MonetaryIndicatorFrequency::FIXED, :city_hall => city_halls(:main)) | |
| indicator.monetary_indicator_values.create!(:year => Time.zone.now.year, :value => 107.86) | |
| Setting.update_all({:value => "1"}, {:kind => "yearly_payment_professional_society", :key => "professional_society_calculation"}) | |
| Setting.update_all({:value => indicator.id}, {:kind => "yearly_payment_professional_society", :key => "professional_society_calculation_indicator_id"}) | |
| Setting.update_all({:value => "0.5"}, {:kind => "yearly_payment_professional_society", :key => "professional_society_calculation_multiplier"}) | |
| @issuer.city_hall = city_hall | |
| taxpayer_cnpj = taxpayer() | |
| taxpayer_cnpj.update_attribute(:cpf_or_cnpj, true) | |
| taxpayer_cnpj.update_attribute(:special_regimen, 3) | |
| taxpayer_cnpj.update_attribute(:yearly_payment, true) | |
| taxpayer_cnpj.update_attribute(:yearly_value_cents, 15000) | |
| taxpayer_cnpj.update_attribute(:yearly_installments, 6) | |
| @issuer.city_hall.taxpayers << taxpayer_cnpj | |
| end | |
| it "returns only taxpayers who is not professional society " do | |
| @issuer.payment_type = "yearly_rate" | |
| expect(@issuer.find_taxpayers.size).to eq 1 | |
| expect(@issuer.find_taxpayers.first.special_regimen).not_to eq 3 | |
| expect(@issuer.find_taxpayers.first.cpf_or_cnpj).to be_false | |
| end | |
| end | |
| end | |
| context "processed_competences" do | |
| it "should give competences for custom list" do | |
| issuer.competences_type = "custom" | |
| issuer.competences = "1,3,7,9,1" | |
| issuer.processed_competences.should == [1, 3, 7, 9, 1] | |
| end | |
| it "should give competences for another custom list" do | |
| Timecop.travel(Time.local(2011, 3, 1, 12, 0, 0)) do | |
| issuer.competences_type = "custom" | |
| issuer.competences = "3,4,8,9" | |
| issuer.processed_competences.should == [3,4,8,9] | |
| end | |
| end | |
| it "should give competences for consecutive list" do | |
| issuer.competences_type = "consecutive" | |
| issuer.installments = 7 | |
| issuer.first_installment = 1 | |
| issuer.first_competence = 8 | |
| issuer.processed_competences.should == [8, 9, 10, 11, 12, 1, 2] | |
| end | |
| end | |
| context "processed_due_months" do | |
| it "should give due months for custom list" do | |
| issuer.competences_type = "consecutive" | |
| issuer.first_competence = 7 | |
| issuer.due_months_type = "custom" | |
| issuer.due_months = "1,3,7,9,1" | |
| issuer.processed_due_months.should == [1, 3, 7, 9, 1] | |
| end | |
| it "should give due months for same month list" do | |
| issuer.competences_type = "consecutive" | |
| issuer.first_competence = 7 | |
| issuer.due_months_type = "same" | |
| issuer.installments = 7 | |
| issuer.first_installment = 1 | |
| issuer.processed_due_months.should == [7, 8, 9, 10, 11, 12, 1] | |
| end | |
| it "should give due months for next month list" do | |
| issuer.competences_type = "consecutive" | |
| issuer.first_competence = 7 | |
| issuer.due_months_type = "next" | |
| issuer.installments = 7 | |
| issuer.first_installment = 1 | |
| issuer.processed_due_months.should == [8, 9, 10, 11, 12, 1, 2] | |
| end | |
| it "should give due months for same month and custom competences" do | |
| issuer.competences_type = "custom" | |
| issuer.competences = "3, 7, 12" | |
| issuer.due_months_type = "same" | |
| issuer.installments = 3 | |
| issuer.first_installment = 1 | |
| issuer.processed_due_months.should == [3, 7, 12] | |
| end | |
| it "should give due months for next month and custom competences" do | |
| issuer.competences_type = "custom" | |
| issuer.competences = "3, 7, 12" | |
| issuer.due_months_type = "next" | |
| issuer.installments = 3 | |
| issuer.first_installment = 1 | |
| issuer.processed_due_months.should == [4, 8, 1] | |
| end | |
| end | |
| context ".installments_data" do | |
| it "calculate installments for consecutive competences due in the same month" do | |
| issuer.city_hall = city_halls(:main) | |
| issuer.incidence = 2011 | |
| issuer.installments = 4 | |
| issuer.payment_type = "estimate" | |
| issuer.first_installment = 1 | |
| issuer.first_installment_number = 1 | |
| issuer.competences_type = "consecutive" | |
| issuer.first_competence = 2 | |
| issuer.due_months_type = "same" | |
| issuer.fine_and_interest = false | |
| ins = issuer.installments_data | |
| ins[0][:installment_number].should == 0 | |
| ins[0][:incidence].should == Date.civil(2011, 2, 1) | |
| ins[0][:expiration_date].should == Date.civil(2011, 2, 10) | |
| ins[0][:payment_date].should == Date.civil(2011, 2, 10) | |
| ins[1][:installment_number].should == 1 | |
| ins[1][:incidence].should == Date.civil(2011, 3, 1) | |
| ins[1][:expiration_date].should == Date.civil(2011, 3, 10) | |
| ins[1][:payment_date].should == Date.civil(2011, 3, 10) | |
| ins[2][:installment_number].should == 2 | |
| ins[2][:incidence].should == Date.civil(2011, 4, 1) | |
| ins[2][:expiration_date].should == Date.civil(2011, 4, 10) | |
| ins[2][:payment_date].should == Date.civil(2011, 4, 10) | |
| ins[3][:installment_number].should == 3 | |
| ins[3][:incidence].should == Date.civil(2011, 5, 1) | |
| ins[3][:expiration_date].should == Date.civil(2011, 5, 10) | |
| ins[3][:payment_date].should == Date.civil(2011, 5, 10) | |
| end | |
| it "calculate installments for consecutive competences due in the next month" do | |
| issuer.city_hall = city_halls(:main) | |
| issuer.incidence = 2011 | |
| issuer.installments = 4 | |
| issuer.payment_type = "estimate" | |
| issuer.first_installment = 1 | |
| issuer.first_installment_number = 1 | |
| issuer.competences_type = "consecutive" | |
| issuer.first_competence = 2 | |
| issuer.due_months_type = "next" | |
| issuer.fine_and_interest = false | |
| ins = issuer.installments_data | |
| ins[0][:installment_number].should == 0 | |
| ins[0][:incidence].should == Date.civil(2011, 2, 1) | |
| ins[0][:expiration_date].should == Date.civil(2011, 3, 10) | |
| ins[0][:payment_date].should == Date.civil(2011, 3, 10) | |
| ins[1][:installment_number].should == 1 | |
| ins[1][:incidence].should == Date.civil(2011, 3, 1) | |
| ins[1][:expiration_date].should == Date.civil(2011, 4, 10) | |
| ins[1][:payment_date].should == Date.civil(2011, 4, 10) | |
| ins[2][:installment_number].should == 2 | |
| ins[2][:incidence].should == Date.civil(2011, 4, 1) | |
| ins[2][:expiration_date].should == Date.civil(2011, 5, 10) | |
| ins[2][:payment_date].should == Date.civil(2011, 5, 10) | |
| ins[3][:installment_number].should == 3 | |
| ins[3][:incidence].should == Date.civil(2011, 5, 1) | |
| ins[3][:expiration_date].should == Date.civil(2011, 6, 10) | |
| ins[3][:payment_date].should == Date.civil(2011, 6, 10) | |
| end | |
| it "calculate installments for consecutive competences due in the same month with installments leaking for next incidence" do | |
| issuer.city_hall = city_halls(:main) | |
| issuer.incidence = 2011 | |
| issuer.installments = 3 | |
| issuer.payment_type = "estimate" | |
| issuer.first_installment = 1 | |
| issuer.first_installment_number = 1 | |
| issuer.competences_type = "consecutive" | |
| issuer.first_competence = 12 | |
| issuer.due_months_type = "same" | |
| issuer.fine_and_interest = false | |
| ins = issuer.installments_data | |
| ins[0][:installment_number].should == 0 | |
| ins[0][:incidence].should == Date.civil(2011, 12, 1) | |
| ins[0][:expiration_date].should == Date.civil(2011, 12, 10) | |
| ins[0][:payment_date].should == Date.civil(2011, 12, 10) | |
| ins[1][:installment_number].should == 1 | |
| ins[1][:incidence].should == Date.civil(2012, 1, 1) | |
| ins[1][:expiration_date].should == Date.civil(2012, 1, 10) | |
| ins[1][:payment_date].should == Date.civil(2012, 1, 10) | |
| ins[2][:installment_number].should == 2 | |
| ins[2][:incidence].should == Date.civil(2012, 2, 1) | |
| ins[2][:expiration_date].should == Date.civil(2012, 2, 10) | |
| ins[2][:payment_date].should == Date.civil(2012, 2, 10) | |
| end | |
| it "calculate installments for custom competences due in the same month" do | |
| issuer.city_hall = city_halls(:main) | |
| issuer.incidence = 2011 | |
| issuer.installments = 4 | |
| issuer.payment_type = "estimate" | |
| issuer.first_installment = 2 | |
| issuer.first_installment_number = 1 | |
| issuer.competences_type = "custom" | |
| issuer.competences = "3,7,12" | |
| issuer.due_months_type = "same" | |
| issuer.fine_and_interest = false | |
| ins = issuer.installments_data | |
| ins[0][:installment_number].should == 0 | |
| ins[0][:incidence].should == Date.civil(2011, 3, 1) | |
| ins[0][:expiration_date].should == Date.civil(2011, 3, 10) | |
| ins[0][:payment_date].should == Date.civil(2011, 3, 10) | |
| ins[1][:installment_number].should == 1 | |
| ins[1][:incidence].should == Date.civil(2011, 7, 1) | |
| ins[1][:expiration_date].should == Date.civil(2011, 7, 10) | |
| ins[1][:payment_date].should == Date.civil(2011, 7, 10) | |
| ins[2][:installment_number].should == 2 | |
| ins[2][:incidence].should == Date.civil(2011, 12, 1) | |
| ins[2][:expiration_date].should == Date.civil(2011, 12, 10) | |
| ins[2][:payment_date].should == Date.civil(2011, 12, 10) | |
| end | |
| it "calculate installments for custom competences due in the next month" do | |
| issuer.city_hall = city_halls(:main) | |
| issuer.incidence = 2011 | |
| issuer.installments = 4 | |
| issuer.payment_type = "estimate" | |
| issuer.first_installment = 2 | |
| issuer.first_installment_number = 1 | |
| issuer.competences_type = "custom" | |
| issuer.competences = "3,7,12" | |
| issuer.due_months_type = "next" | |
| issuer.fine_and_interest = false | |
| ins = issuer.installments_data | |
| ins[0][:installment_number].should == 0 | |
| ins[0][:incidence].should == Date.civil(2011, 3, 1) | |
| ins[0][:expiration_date].should == Date.civil(2011, 4, 10) | |
| ins[0][:payment_date].should == Date.civil(2011, 4, 10) | |
| ins[1][:installment_number].should == 1 | |
| ins[1][:incidence].should == Date.civil(2011, 7, 1) | |
| ins[1][:expiration_date].should == Date.civil(2011, 8, 10) | |
| ins[1][:payment_date].should == Date.civil(2011, 8, 10) | |
| ins[2][:installment_number].should == 2 | |
| ins[2][:incidence].should == Date.civil(2011, 12, 1) | |
| ins[2][:expiration_date].should == Date.civil(2012, 1, 10) | |
| ins[2][:payment_date].should == Date.civil(2012, 1, 10) | |
| end | |
| it "calculate installments for consecutive competences and custom due months" do | |
| issuer.city_hall = city_halls(:main) | |
| issuer.incidence = 2011 | |
| issuer.installments = 4 | |
| issuer.payment_type = "estimate" | |
| issuer.first_installment = 1 | |
| issuer.first_installment_number = 3 | |
| issuer.competences_type = "consecutive" | |
| issuer.first_competence = 5 | |
| issuer.due_months_type = "custom" | |
| issuer.due_months = "1,3,9,2" | |
| issuer.fine_and_interest = false | |
| ins = issuer.installments_data | |
| ins[0][:installment_number].should == 2 | |
| ins[0][:incidence].should == Date.civil(2011, 5, 1) | |
| ins[0][:expiration_date].should == Date.civil(2011, 1, 10) | |
| ins[0][:payment_date].should == Date.civil(2011, 1, 10) | |
| ins[1][:installment_number].should == 3 | |
| ins[1][:incidence].should == Date.civil(2011, 6, 1) | |
| ins[1][:expiration_date].should == Date.civil(2011, 3, 10) | |
| ins[1][:payment_date].should == Date.civil(2011, 3, 10) | |
| ins[2][:installment_number].should == 4 | |
| ins[2][:incidence].should == Date.civil(2011, 7, 1) | |
| ins[2][:expiration_date].should == Date.civil(2011, 9, 10) | |
| ins[2][:payment_date].should == Date.civil(2011, 9, 10) | |
| ins[3][:installment_number].should == 5 | |
| ins[3][:incidence].should == Date.civil(2011, 8, 1) | |
| ins[3][:expiration_date].should == Date.civil(2012, 2, 10) | |
| ins[3][:payment_date].should == Date.civil(2012, 2, 10) | |
| end | |
| context "with different due days" do | |
| before do | |
| @city_hall = city_halls(:main) | |
| Setting.create_due_days_for!(@city_hall, 10) | |
| s = @city_hall.settings.where(:kind => "due_days", :key => "taxed").first | |
| s.update_attribute(:value, 15) | |
| end | |
| it "calculate installments for custom competences and custom due months" do | |
| Timecop.travel(Time.local(2011, 3, 1, 10, 0, 0)) do | |
| issuer.city_hall = @city_hall | |
| issuer.incidence = 2011 | |
| issuer.installments = 4 | |
| issuer.payment_type = "yearly_rate" | |
| issuer.first_installment = 1 | |
| issuer.first_installment_number = 1 | |
| issuer.competences_type = "custom" | |
| issuer.competences = "3,4,8,9" | |
| issuer.due_months_type = "custom" | |
| issuer.due_months = "3,4,8,9" | |
| issuer.fine_and_interest = false | |
| issuer.installments_data.size.should be(4) | |
| ins = issuer.installments_data | |
| ins[0][:incidence].should == Date.civil(2011, 3, 1) | |
| ins[0][:expiration_date].should == Date.civil(2011, 3, 10) | |
| ins[0][:payment_date].should == Date.civil(2011, 3, 10) | |
| ins[1][:incidence].should == Date.civil(2011, 4, 1) | |
| ins[1][:expiration_date].should == Date.civil(2011, 4, 10) | |
| ins[1][:payment_date].should == Date.civil(2011, 4, 10) | |
| ins[2][:incidence].should == Date.civil(2011, 8, 1) | |
| ins[2][:expiration_date].should == Date.civil(2011, 8, 10) | |
| ins[2][:payment_date].should == Date.civil(2011, 8, 10) | |
| ins[3][:incidence].should == Date.civil(2011, 9, 1) | |
| ins[3][:expiration_date].should == Date.civil(2011, 9, 10) | |
| ins[3][:payment_date].should == Date.civil(2011, 9, 10) | |
| end | |
| end | |
| end | |
| it "calculate installments for custom competences and custom due months" do | |
| issuer.city_hall = city_halls(:main) | |
| issuer.incidence = 2011 | |
| issuer.installments = 4 | |
| issuer.payment_type = "estimate" | |
| issuer.first_installment = 1 | |
| issuer.first_installment_number = 3 | |
| issuer.competences_type = "custom" | |
| issuer.competences = "9,10,1,2" | |
| issuer.due_months_type = "custom" | |
| issuer.due_months = "1,3,9,2" | |
| issuer.fine_and_interest = false | |
| ins = issuer.installments_data | |
| ins[0][:installment_number].should == 2 | |
| ins[0][:incidence].should == Date.civil(2011, 9, 1) | |
| ins[0][:expiration_date].should == Date.civil(2011, 1, 10) | |
| ins[0][:payment_date].should == Date.civil(2011, 1, 10) | |
| ins[1][:installment_number].should == 3 | |
| ins[1][:incidence].should == Date.civil(2011, 10, 1) | |
| ins[1][:expiration_date].should == Date.civil(2011, 3, 10) | |
| ins[1][:payment_date].should == Date.civil(2011, 3, 10) | |
| ins[2][:installment_number].should == 4 | |
| ins[2][:incidence].should == Date.civil(2012, 1, 1) | |
| ins[2][:expiration_date].should == Date.civil(2011, 9, 10) | |
| ins[2][:payment_date].should == Date.civil(2011, 9, 10) | |
| ins[3][:installment_number].should == 5 | |
| ins[3][:incidence].should == Date.civil(2012, 2, 1) | |
| ins[3][:expiration_date].should == Date.civil(2012, 2, 10) | |
| ins[3][:payment_date].should == Date.civil(2012, 2, 10) | |
| end | |
| end | |
| context ".process_yearly_payment!" do | |
| before do | |
| issuer.city_hall = city_halls(:main) | |
| issuer.incidence = 2011 | |
| issuer.payment_type = "yearly_rate" | |
| issuer.installments = 6 | |
| issuer.value_cents = 15000 | |
| issuer.registration = Time.zone.now.to_date.beginning_of_month | |
| issuer.first_installment = 4 | |
| issuer.first_installment_number = 3 | |
| issuer.competences_type = "consecutive" | |
| issuer.first_competence = "1" | |
| issuer.due_months_type = "same" | |
| issuer.fine_and_interest = false | |
| end | |
| it "should process for taxpayer without yearly payment" do | |
| yp = nil | |
| expect { | |
| yp = issuer.process_yearly_payment!(taxpayer(:cpf_taxpayer), issuer.installments_data) | |
| }.to change(YearlyPayment, :count).by(1) | |
| yp.stubs.size.should == 3 | |
| stubs = yp.stubs | |
| stubs[0].installment_number.should == 2 | |
| stubs[0].iss_value_cents.should == 2500 | |
| stubs[0].total_value_cents.should == 2500 | |
| stubs[0].incidence.should == Date.civil(2011, 1, 1) | |
| stubs[0].expiration_date.should == Date.civil(2011, 1, 10) | |
| stubs[0].payment_date.should == Time.zone.now.to_date + 2.days | |
| stubs[0].yearly_payment_id == yp.id | |
| stubs[1].installment_number.should == 3 | |
| stubs[1].iss_value_cents.should == 2500 | |
| stubs[1].total_value_cents.should == 2500 | |
| stubs[1].incidence.should == Date.civil(2011, 2, 1) | |
| stubs[1].expiration_date.should == Date.civil(2011, 2, 10) | |
| stubs[1].payment_date.should == Time.zone.now.to_date + 2.days | |
| stubs[1].yearly_payment_id == yp.id | |
| stubs[2].installment_number.should == 4 | |
| stubs[2].iss_value_cents.should == 2500 | |
| stubs[2].total_value_cents.should == 2500 | |
| stubs[2].incidence.should == Date.civil(2011, 3, 1) | |
| stubs[2].expiration_date.should == Date.civil(2011, 3, 10) | |
| stubs[2].payment_date.should == Time.zone.now.to_date + 2.days | |
| stubs[2].yearly_payment_id == yp.id | |
| end | |
| it "process for taxpayer without yearly payment and with regular stubs in city_hall" do | |
| stub = FactoryGirl.create(:stub, :taxpayer => taxpayer(), :city_hall_id => taxpayer(:cpf_taxpayer).city_hall_id, :status => 'paid_off', :underwritings => [create_valid_underwriting]) | |
| yp = nil | |
| expect { | |
| yp = issuer.process_yearly_payment!(taxpayer(:cpf_taxpayer), issuer.installments_data) | |
| }.to change(YearlyPayment, :count).by(1) | |
| yp.stubs.size.should == 3 | |
| end | |
| it "should grant discount for taxpayer correctly" do | |
| yp = nil | |
| issuer.discount = 20 | |
| expect { | |
| yp = issuer.process_yearly_payment!(taxpayer(:cpf_taxpayer), issuer.installments_data) | |
| }.to change(YearlyPayment, :count).by(1) | |
| yp.stubs.size.should == 3 | |
| stubs = yp.stubs | |
| stubs[0].installment_number.should == 2 | |
| stubs[0].iss_value_cents.should == 2500 | |
| stubs[0].discount_cents == 500 | |
| stubs[0].total_value_cents.should == 2000 | |
| stubs[0].incidence.should == Date.civil(2011, 1, 1) | |
| stubs[0].expiration_date.should == Date.civil(2011, 1, 10) | |
| stubs[0].payment_date.should == Time.zone.now.to_date + 2.days | |
| stubs[0].yearly_payment_id == yp.id | |
| stubs[1].installment_number.should == 3 | |
| stubs[1].iss_value_cents.should == 2500 | |
| stubs[1].discount_cents == 500 | |
| stubs[1].total_value_cents.should == 2000 | |
| stubs[1].incidence.should == Date.civil(2011, 2, 1) | |
| stubs[1].expiration_date.should == Date.civil(2011, 2, 10) | |
| stubs[1].payment_date.should == Time.zone.now.to_date + 2.days | |
| stubs[1].yearly_payment_id == yp.id | |
| stubs[2].installment_number.should == 4 | |
| stubs[2].iss_value_cents.should == 2500 | |
| stubs[2].discount_cents == 500 | |
| stubs[2].total_value_cents.should == 2000 | |
| stubs[2].incidence.should == Date.civil(2011, 3, 1) | |
| stubs[2].expiration_date.should == Date.civil(2011, 3, 10) | |
| stubs[2].payment_date.should == Time.zone.now.to_date + 2.days | |
| stubs[2].yearly_payment_id == yp.id | |
| end | |
| it "should process for taxpayer with cancelled yearly payment" do | |
| yp = issuer.process_yearly_payment!(taxpayer(:cpf_taxpayer), issuer.installments_data) | |
| yp.cancel! | |
| expect { | |
| yp = issuer.process_yearly_payment!(taxpayer(:cpf_taxpayer), issuer.installments_data) | |
| }.to change(YearlyPayment, :count).by(0) | |
| yp.stubs.cancelled.size.should == 3 | |
| yp.stubs.pending.size.should == 3 | |
| end | |
| end | |
| context ".process_professional_society!" do | |
| let(:city_hall) { city_halls(:main) } | |
| before do | |
| indicator = FactoryGirl.create(:monetary_indicator, :frequency => MonetaryIndicatorFrequency::FIXED, :city_hall => city_halls(:main)) | |
| indicator.monetary_indicator_values.create!(:year => Time.zone.now.year, :value => 107.86) | |
| Setting.update_all({:value => "1"}, {:kind => "yearly_payment_professional_society", :key => "professional_society_calculation"}) | |
| Setting.update_all({:value => indicator.id}, {:kind => "yearly_payment_professional_society", :key => "professional_society_calculation_indicator_id"}) | |
| Setting.update_all({:value => "0.5"}, {:kind => "yearly_payment_professional_society", :key => "professional_society_calculation_multiplier"}) | |
| issuer.city_hall = city_hall | |
| taxpayer_cnpj = taxpayer() | |
| taxpayer_cnpj.update_attribute(:cpf_or_cnpj, true) | |
| taxpayer_cnpj.update_attribute(:special_regimen, 3) | |
| taxpayer_cnpj.update_attribute(:yearly_payment, true) | |
| taxpayer_cnpj.update_attribute(:yearly_value_cents, 15000) | |
| taxpayer_cnpj.update_attribute(:yearly_installments, 6) | |
| taxpayer_cnpj.partners << FactoryGirl.create(:partner) | |
| issuer.city_hall.taxpayers << taxpayer_cnpj | |
| end | |
| context "with same due months type" do | |
| context "simulate the 12 installments for professional society" do | |
| before do | |
| issuer.incidence = Time.now.year | |
| issuer.payment_type = "yearly_rate" | |
| issuer.installments = 1 | |
| issuer.value_cents = nil | |
| issuer.registration = nil | |
| issuer.discount = 0.0 | |
| issuer.first_installment = 1 | |
| issuer.first_installment_number = 1 | |
| issuer.competences_type = "consecutive" | |
| issuer.first_competence = "1" | |
| issuer.due_months_type = "same" | |
| issuer.fine_and_interest = false | |
| end | |
| it "creates yearly payment and the rest of the stubs for the year" do | |
| Timecop.travel(Time.local(Time.now.year, 1, 1)) | |
| expect { | |
| yp = issuer.process_professional_society!(city_admin()) | |
| }.to change(YearlyPayment, :count).by(1) | |
| attached_string = "CPF_CNPJ;inscricao_muncipal;nome_oficial;parcelas;valor_por_parcela_centavos;valor_total_centavos\n35130744000105;1234561;Company 1 LTDA;1;10786;10786\n" | |
| CityHallMailer.should have_queued(:yearly_payment, 1, 6, Time.now.year, 1, 1, attached_string, "resumo_2014_fixo_1_parcelas.csv") | |
| expect(YearlyPayment.last.taxpayer.cpf_or_cnpj).to be_true | |
| expect(YearlyPayment.last.taxpayer.special_regimen).to eq 3 | |
| expect(YearlyPayment.last.installments).to eq 1 | |
| expect(YearlyPayment.last.value_cents).to eq 10786 | |
| stubs = YearlyPayment.last.stubs | |
| expect(stubs.size).to eq 1 | |
| stubs[0].installment_number.should == 0 | |
| stubs[0].iss_value_cents.should == 10786 | |
| stubs[0].total_value_cents.should == 10786 | |
| stubs[0].incidence.should == Date.civil(Time.zone.now.year, 1, 1) | |
| stubs[0].expiration_date.should == Date.civil(Time.zone.now.year, 1, 10) | |
| stubs[0].payment_date.should == Date.civil(Time.zone.now.year, 1, 10) | |
| Timecop.travel(Time.local(Time.now.year, 2, 1)) | |
| expect { | |
| yp = issuer.process_professional_society!(city_admin()) | |
| }.not_to change(YearlyPayment, :count).by(1) | |
| attached_string = "CPF_CNPJ;inscricao_muncipal;nome_oficial;parcelas;valor_por_parcela_centavos;valor_total_centavos\n35130744000105;1234561;Company 1 LTDA;2;10786;21572\n" | |
| CityHallMailer.should have_queued(:yearly_payment, 1, 6, Time.now.year, 1, 1, attached_string, "resumo_2014_fixo_2_parcelas.csv") | |
| expect(YearlyPayment.last.taxpayer.cpf_or_cnpj).to be_true | |
| expect(YearlyPayment.last.taxpayer.special_regimen).to eq 3 | |
| expect(YearlyPayment.last.installments).to eq 2 | |
| expect(YearlyPayment.last.value_cents).to eq 21572 | |
| stubs = YearlyPayment.last.stubs | |
| expect(stubs.size).to eq 2 | |
| stubs[1].installment_number.should == 1 | |
| stubs[1].iss_value_cents.should == 10786 | |
| stubs[1].total_value_cents.should == 10786 | |
| stubs[1].incidence.should == Date.civil(Time.zone.now.year, 2, 1) | |
| stubs[1].expiration_date.should == Date.civil(Time.zone.now.year, 2, 10) | |
| stubs[1].payment_date.should == Date.civil(Time.zone.now.year, 2, 10) | |
| Timecop.travel(Time.local(Time.now.year, 3, 1)) | |
| expect { | |
| yp = issuer.process_professional_society!(city_admin()) | |
| }.not_to change(YearlyPayment, :count).by(1) | |
| attached_string = "CPF_CNPJ;inscricao_muncipal;nome_oficial;parcelas;valor_por_parcela_centavos;valor_total_centavos\n35130744000105;1234561;Company 1 LTDA;3;10786;32358\n" | |
| CityHallMailer.should have_queued(:yearly_payment, 1, 6, Time.now.year, 1, 1, attached_string, "resumo_2014_fixo_3_parcelas.csv") | |
| expect(YearlyPayment.last.taxpayer.cpf_or_cnpj).to be_true | |
| expect(YearlyPayment.last.taxpayer.special_regimen).to eq 3 | |
| expect(YearlyPayment.last.installments).to eq 3 | |
| expect(YearlyPayment.last.value_cents).to eq 32358 | |
| stubs = YearlyPayment.last.stubs | |
| expect(stubs.size).to eq 3 | |
| stubs[2].installment_number.should == 2 | |
| stubs[2].iss_value_cents.should == 10786 | |
| stubs[2].total_value_cents.should == 10786 | |
| stubs[2].incidence.should == Date.civil(Time.zone.now.year, 3, 1) | |
| stubs[2].expiration_date.should == Date.civil(Time.zone.now.year, 3, 10) | |
| stubs[2].payment_date.should == Date.civil(Time.zone.now.year, 3, 10) | |
| end | |
| end | |
| end | |
| end | |
| context ".process!" do | |
| before do | |
| issuer.city_hall = city_halls(:main) | |
| issuer.incidence = 2011 | |
| issuer.payment_type = "yearly_rate" | |
| issuer.installments = 6 | |
| issuer.value_cents = 15000 | |
| issuer.first_installment = 1 | |
| issuer.first_installment_number = 3 | |
| issuer.competences_type = "consecutive" | |
| issuer.first_competence = "1" | |
| issuer.due_months_type = "same" | |
| issuer.fine_and_interest = false | |
| end | |
| it "should process for taxpayer without yearly payment" do | |
| expect { | |
| yp = issuer.process!(city_admin()) | |
| }.to change(YearlyPayment, :count).by(1) | |
| end | |
| it 'should send a e-mail with the results' do | |
| taxpayer(:cpf_taxpayer).update_attributes :official_name => "Joaquim Silva" | |
| yp = issuer.process!(city_admin()) | |
| attached_string = "CPF_CNPJ;inscricao_muncipal;nome_oficial;parcelas;valor_por_parcela_centavos;valor_total_centavos\n68377086603;12345611;Joaquim Silva;6;2500;15000\n" | |
| CityHallMailer.should have_queued(:yearly_payment, 1, 6, 2011, 1, 1, attached_string, "resumo_2011_fixo_6_parcelas.csv") | |
| end | |
| it 'should send a e-mail when a monetary indicator is missing' do | |
| @city_hall = city_halls(:main) | |
| @city_hall.update_attributes(:monetary_adjustment => true, :monetary_adjustment_indicator => FactoryGirl.create(:monetary_indicator, :frequency => MonetaryIndicatorFrequency::MONTHLY, :city_hall => city_halls(:main))) | |
| issuer.fine_and_interest = true | |
| issuer.city_hall = @city_hall | |
| allow(Stub).to receive(:calculate_monetary_adjustment).and_raise(MonetaryAdjustmentCalculator::MissingMonetaryIndicatorError.new("Foo Message")) | |
| yp = issuer.process!(city_admin()) | |
| CityHallMailer.should have_queued(:yearly_payment_error, @city_hall.id, city_admin().id, 2011, 1, "Foo Message") | |
| end | |
| it 'should send a e-mail when a race condition happens' do | |
| allow(issuer).to receive(:process_yearly_payment!).and_raise(ActiveRecord::StatementInvalid) | |
| yp = issuer.process!(city_admin()) | |
| CityHallMailer.should have_queued(:yearly_payment_error, city_halls(:main).id, city_admin().id, 2011, 1, I18n.t("yearly_payment_issuer.race_condition_detected")) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment