Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix invoice xls generation #1269

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ gem 'jrpc', github: 'didww/jrpc'
gem 'active_admin_sidebar', '1.1.0'

# XLS generation
gem 'excelinator', github: 'livingsocial/excelinator'
# can be switched back to the original repo after ruby 3 fix PR merged
# https://github.com/livingsocial/excelinator/pull/19
gem 'excelinator', github: 'senid231/excelinator', branch: 'ruby3-fix'

# REST API
gem 'jsonapi-resources', '~> 0.9.12'
Expand Down
21 changes: 11 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ GIT
specs:
prometheus_exporter (0.5.1)

GIT
remote: https://github.com/livingsocial/excelinator.git
revision: 51cd9dbdbcc0e610ff5bd31b85e4744ac379e8c5
specs:
excelinator (1.3.1)
spreadsheet

GIT
remote: https://github.com/nsarno/knock.git
revision: 37e403a7c6d44f585b56a086245e41566a8d6fe1
Expand All @@ -72,6 +65,14 @@ GIT
jwt (~> 2.2.1)
rails (>= 5)

GIT
remote: https://github.com/senid231/excelinator.git
revision: 25afcc544a6f287a2ace9018adbe6fc96ea29cb2
branch: ruby3-fix
specs:
excelinator (1.3.1)
spreadsheet

GIT
remote: https://github.com/workgena/active_admin_date_range_preset.git
revision: 1bfb64ceb9639bb76dcad2c8e2df4c5199b138f3
Expand Down Expand Up @@ -545,7 +546,7 @@ GEM
rubocop (>= 1.33.0, < 2.0)
rubocop-rspec (1.39.0)
rubocop (>= 0.68.1)
ruby-ole (1.2.12.1)
ruby-ole (1.2.12.2)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.4)
rubyzip (1.3.0)
Expand Down Expand Up @@ -585,8 +586,8 @@ GEM
simplecov (~> 0.19)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
spreadsheet (1.1.7)
ruby-ole (>= 1.0)
spreadsheet (1.3.0)
ruby-ole
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand Down
17 changes: 15 additions & 2 deletions spec/factories/billing/invoice_destination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,21 @@
invoice

trait :filled do
country
network
country { FactoryBot.create(:country, :uniq_name) }
network { FactoryBot.create(:network, :uniq_name) }
end

trait :success do
filled
sequence(:dst_prefix, &:to_s)
rate { rand + rand(5) }
successful_calls_count { rand(1..1000) }
calls_duration { successful_calls_count + (successful_calls_count * rand(100)) }
amount { (successful_calls_count * rand(5)).round(6) }
first_call_at { 25.hours.ago }
first_successful_call_at { 24.hours.ago }
last_successful_call_at { 1.minute.ago }
last_call_at { 1.second.ago }
end
end
end
Binary file added spec/fixtures/files/invoice_template.odt
Binary file not shown.
50 changes: 50 additions & 0 deletions spec/services/billing_invoice/generate_document_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

RSpec.describe BillingInvoice::GenerateDocument do
subject do
described_class.call(service_params)
end

let(:service_params) { { invoice: invoice } }

let!(:contractor) { FactoryBot.create(:vendor) }
let(:odt_fixture_binary) do
IO.binread Rails.root.join('spec/fixtures/files/invoice_template.odt')
end
let!(:invoice_template) do
FactoryBot.create(:invoice_template, data: odt_fixture_binary)
end
let!(:account) { FactoryBot.create(:account, account_attrs) }
let(:account_attrs) do
{
contractor: contractor,
vendor_invoice_template: invoice_template
}
end
let!(:invoice) { FactoryBot.create(:invoice, invoice_attrs) }
let(:invoice_attrs) do
{
account: account,
vendor_invoice: true,
type_id: Billing::InvoiceType::MANUAL,
state_id: Billing::InvoiceState::NEW,
start_date: Time.zone.parse('2020-01-01 00:00:00'),
end_date: Time.zone.parse('2020-02-01 00:00:00')
}
end
let!(:destinations) do
FactoryBot.create_list(:invoice_destination, 20, :success, invoice: invoice)
end

it 'creates invoice document' do
expect { subject }.to change { Billing::InvoiceDocument.count }.by(1)
doc = Billing::InvoiceDocument.last!
expect(doc).to have_attributes(
invoice: invoice,
filename: invoice.file_name.to_s,
data: be_present,
csv_data: a_kind_of(String),
xls_data: a_kind_of(String)
)
end
end