-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1555 from yeti-switch/1554-invoice-auto-approve
1554, Invoice auto approve configuration
- Loading branch information
Showing
16 changed files
with
212 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module BillingInvoice | ||
class Approve < ApplicationService | ||
parameter :invoice, required: true | ||
|
||
Error = Class.new(ApplicationService::Error) | ||
|
||
def call | ||
Billing::Invoice.transaction do | ||
raise_if_invalid! | ||
|
||
approve_invoice! | ||
send_email | ||
end | ||
end | ||
|
||
private | ||
|
||
def approve_invoice! | ||
invoice.update!(state_id: Billing::InvoiceState::APPROVED) | ||
rescue ActiveRecord::RecordInvalid => e | ||
raise Error, e.message | ||
end | ||
|
||
def send_email | ||
if invoice.invoice_document.present? | ||
invoice.invoice_document.send_invoice | ||
end | ||
end | ||
|
||
def raise_if_invalid! | ||
raise Error, 'Invoice already approved' if invoice.state_id == Billing::InvoiceState::APPROVED | ||
raise Error, "Invoice can't be approved" unless invoice.approvable? | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,3 +53,5 @@ cryptomus: | |
|
||
routing_simulation_default_interface: internal | ||
|
||
invoice: | ||
auto_approve: false |
This file contains 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
This file contains 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
This file contains 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
37 changes: 37 additions & 0 deletions
37
spec/features/billing/invoices/approve_invoice_feature_spec.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe 'Approve invoide feature', type: :feature do | ||
subject { click_action_item 'Approve' } | ||
|
||
include_context :login_as_admin | ||
|
||
context 'when valid data' do | ||
let!(:invoice) { FactoryBot.create(:invoice, :manual, :pending, :with_vendor_account) } | ||
let(:approved_state_id) { Billing::InvoiceState::APPROVED } | ||
|
||
before { visit invoice_path(invoice) } | ||
|
||
it 'should approve invoice', :js do | ||
subject | ||
|
||
expect(page).to have_flash_message 'Invoice was successful approved', type: :notice | ||
expect(invoice.reload).to have_attributes(state_id: approved_state_id) | ||
end | ||
end | ||
|
||
context 'when invalid data', :js do | ||
let!(:invoice) { FactoryBot.create(:invoice, :pending, :with_vendor_account) } | ||
|
||
before do | ||
allow(BillingInvoice::Approve).to receive(:call).and_raise(BillingInvoice::Approve::Error, 'error') | ||
visit invoice_path(invoice) | ||
end | ||
|
||
it 'should render error' do | ||
subject | ||
|
||
expect(page).to have_flash_message 'error', type: :error | ||
expect(BillingInvoice::Approve).to have_received(:call) | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe BillingInvoice::Approve, type: :service do | ||
subject { described_class.call(service_params) } | ||
|
||
let(:service_params) { { invoice: } } | ||
|
||
context 'when valid data' do | ||
let!(:contractor) { FactoryBot.create(:customer) } | ||
let!(:contact) { FactoryBot.create(:contact, contractor:) } | ||
let!(:account) { FactoryBot.create(:account, contractor:, send_invoices_to: [contact.id]) } | ||
let(:invoice_attrs) { { account:, contractor: } } | ||
let!(:invoice) { FactoryBot.create(:invoice, :pending, invoice_attrs) } | ||
let(:invoice_document_attrs) { { invoice:, filename: "#{invoice.id}_#{invoice.start_date}_#{invoice.end_date}" } } | ||
let!(:invoice_document) { FactoryBot.create(:invoice_document, :filled, invoice_document_attrs) } | ||
|
||
before { FactoryBot.create(:smtp_connection, global: true) } | ||
|
||
it 'approves invoice' do | ||
expect { subject }.to change { invoice.state_id }.to(Billing::InvoiceState::APPROVED) | ||
end | ||
|
||
it 'enqueues email worker' do | ||
expect { subject }.to have_enqueued_job(Worker::SendEmailLogJob) | ||
end | ||
end | ||
|
||
context 'when invoice already approved' do | ||
let!(:invoice) { FactoryBot.build_stubbed(:invoice, :approved) } | ||
|
||
it 'raises error' do | ||
expect { subject }.to raise_error(BillingInvoice::Approve::Error, 'Invoice already approved') | ||
end | ||
end | ||
|
||
context 'when invoice is not approvable' do | ||
let!(:invoice) { FactoryBot.build_stubbed(:invoice, :new) } | ||
|
||
it 'raises error' do | ||
expect { subject }.to raise_error(BillingInvoice::Approve::Error, "Invoice can't be approved") | ||
end | ||
end | ||
|
||
context 'when validation error' do | ||
let!(:invoice) { FactoryBot.create(:invoice, :pending, :with_vendor_account) } | ||
|
||
before { allow_any_instance_of(Billing::Invoice).to receive(:update!).and_raise(ActiveRecord::RecordInvalid) } | ||
|
||
it 'raises error' do | ||
expect { subject }.to raise_error(BillingInvoice::Approve::Error, 'Record invalid') | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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