Skip to content

Commit

Permalink
add invoice reference
Browse files Browse the repository at this point in the history
fixes #870
  • Loading branch information
senid231 committed Feb 18, 2021
1 parent fd63e07 commit 02448e9
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/admin/billing/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def find_resource
:customer_invoice_period_id, :vendor_invoice_period_id,
:autogenerate_vendor_invoices, :autogenerate_customer_invoices,
:vendor_invoice_template_id, :customer_invoice_template_id, :timezone_id,
:customer_invoice_ref_template, :vendor_invoice_ref_template,
send_invoices_to: [], send_balance_notifications_to: []

includes :customer_invoice_period, :vendor_invoice_period, :contractor, :timezone,
Expand Down Expand Up @@ -177,6 +178,8 @@ def find_resource
end
end
row :timezone
row :customer_invoice_ref_template
row :vendor_invoice_ref_template
end

panel 'Last Payments' do
Expand Down Expand Up @@ -242,6 +245,8 @@ def find_resource
f.input :send_invoices_to, as: :select, input_html: { class: 'chosen', multiple: true }, collection: Billing::Contact.collection
f.input :send_balance_notifications_to, as: :select, input_html: { class: 'chosen', multiple: true }, collection: Billing::Contact.collection
f.input :timezone_id, as: :select, input_html: { class: 'chosen' }, collection: System::Timezone.all
f.input :customer_invoice_ref_template
f.input :vendor_invoice_ref_template
f.input :uuid, as: :string
end
f.actions
Expand Down
3 changes: 3 additions & 0 deletions app/admin/billing/invoices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def build_new_resource
selectable_column
id_column
actions
column :reference
column :contractor, footer: lambda {
strong do
'Total:'
Expand Down Expand Up @@ -164,6 +165,7 @@ def build_new_resource
end

filter :id
filter :reference
filter :contractor,
input_html: { class: 'chosen-ajax', 'data-path': '/contractors/search' },
collection: proc {
Expand Down Expand Up @@ -193,6 +195,7 @@ def build_new_resource
tab 'Invoice' do
attributes_table_for s do
row :id
row :reference
row :contractor_id
row :account
row :state
Expand Down
4 changes: 3 additions & 1 deletion app/forms/account_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def self.policy_class
:send_invoices_to,
:send_balance_notifications_to,
:timezone_id,
:uuid
:uuid,
:customer_invoice_ref_template,
:vendor_invoice_ref_template

before_save :apply_vendor_invoice_period
before_save :apply_customer_invoice_period
Expand Down
27 changes: 27 additions & 0 deletions app/lib/invoice_ref_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class InvoiceRefTemplate
TEMPLATES = {
'$id': proc { invoice.id }
}.freeze

def self.call(invoice, template)
new(invoice, template).call
end

attr_reader :invoice, :template

def initialize(invoice, template)
@invoice = invoice
@template = template
end

def call
result = template
TEMPLATES.each do |key, value_block|
value = instance_exec(&value_block).to_s
result = result.gsub(key.to_s, value)
end
result
end
end
1 change: 1 addition & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Account < Yeti::ActiveRecord
validates :vat, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100, allow_nil: false } # this is percents
validates :destination_rate_limit, numericality: { greater_than_or_equal_to: 0, allow_nil: true }
validates :max_call_duration, numericality: { greater_than: 0, allow_nil: true }
validates :customer_invoice_ref_template, :vendor_invoice_ref_template, presence: true

after_initialize do
if new_record?
Expand Down
7 changes: 7 additions & 0 deletions app/services/billing_invoice/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def call
start_date: start_time,
end_date: end_time
)
invoice.update! reference: build_reference(invoice)
Worker::FillInvoiceJob.perform_later(invoice.id)
invoice
end
Expand All @@ -33,6 +34,12 @@ def call

private

def build_reference(invoice)
template_name = is_vendor ? :vendor_invoice_ref_template : :customer_invoice_ref_template
template = account.public_send(template_name)
InvoiceRefTemplate.call(invoice, template)
end

def validate!
start_date = start_time.in_time_zone(Time.zone)
end_date = end_time.in_time_zone(Time.zone)
Expand Down
3 changes: 3 additions & 0 deletions app/services/billing_invoice/generate_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class GenerateDocument < ApplicationService
# INV_DST_AMOUNT,
# INV_DST_FIRST_CALL_AT,
# INV_DST_LAST_CALL_AT
# INV_REF

class TemplateUndefined < Error
def initialize(invoice_id)
Expand All @@ -50,6 +51,7 @@ def self.replaces_list
contractor_address
contractor_phones
inv_id
inv_ref
inv_created_at
inv_start_date
inv_end_date
Expand Down Expand Up @@ -125,6 +127,7 @@ def replaces
contractor_address: invoice.contractor.address,
contractor_phones: invoice.contractor.phones,
inv_id: invoice.id,
inv_ref: invoice.reference,
inv_created_at: invoice.created_at,
inv_start_date: invoice.start_date,
inv_end_date: invoice.end_date,
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20210218092245_add_account_invoice_reference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddAccountInvoiceReference < ActiveRecord::Migration[5.2]
def change
add_column :accounts, :customer_invoice_ref_template, :string, default: '$id', null: false
add_column :accounts, :vendor_invoice_ref_template, :string, default: '$id', null: false
end
end
15 changes: 15 additions & 0 deletions db/secondbase/migrate/20210218095038_add_invoice_reference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class AddInvoiceReference < ActiveRecord::Migration[5.2]
def up
add_column 'billing.invoices', :reference, :string

execute <<-SQL
UPDATE billing.invoices SET reference = id::varchar
SQL

add_index 'billing.invoices', :reference
end

def down
remove_column 'billing.invoices', :reference
end
end
13 changes: 11 additions & 2 deletions db/secondbase/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,8 @@ CREATE TABLE billing.invoices (
last_successful_call_at timestamp with time zone,
successful_calls_count bigint,
type_id smallint NOT NULL,
billing_duration bigint NOT NULL
billing_duration bigint NOT NULL,
reference character varying
);


Expand Down Expand Up @@ -4070,6 +4071,13 @@ CREATE INDEX auth_log_id_idx ON ONLY auth_log.auth_log USING btree (id);
CREATE INDEX auth_log_request_time_idx ON ONLY auth_log.auth_log USING btree (request_time);


--
-- Name: index_billing.invoices_on_reference; Type: INDEX; Schema: billing; Owner: -
--

CREATE INDEX "index_billing.invoices_on_reference" ON billing.invoices USING btree (reference);


--
-- Name: invoice_destinations_invoice_id_idx; Type: INDEX; Schema: billing; Owner: -
--
Expand Down Expand Up @@ -4399,6 +4407,7 @@ INSERT INTO "public"."schema_migrations" (version) VALUES
('20200803201602'),
('20201128134302'),
('20210116150950'),
('20210212102105');
('20210212102105'),
('20210218095038');


5 changes: 4 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,8 @@ CREATE TABLE billing.accounts (
total_capacity smallint,
destination_rate_limit numeric,
max_call_duration integer,
customer_invoice_ref_template character varying DEFAULT '$id'::character varying NOT NULL,
vendor_invoice_ref_template character varying DEFAULT '$id'::character varying NOT NULL,
CONSTRAINT positive_max_call_duration CHECK ((max_call_duration > 0)),
CONSTRAINT positive_origination_capacity CHECK ((origination_capacity > 0)),
CONSTRAINT positive_termination_capacity CHECK ((termination_capacity > 0)),
Expand Down Expand Up @@ -23004,6 +23006,7 @@ INSERT INTO "public"."schema_migrations" (version) VALUES
('20201103092516'),
('20201111201356'),
('20201226161344'),
('20210116152459');
('20210116152459'),
('20210218092245');


4 changes: 4 additions & 0 deletions spec/factories/billing/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@
trait :with_vendor_account do
account { FactoryBot.create(:account, contractor: FactoryBot.create(:vendor)) }
end

after(:create) do |record|
record.update!(reference: record.id.to_s) if record.reference.blank?
end
end
end
21 changes: 21 additions & 0 deletions spec/features/billing/accounts/edit_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,25 @@
)
end
end

context 'with invoice ref templates change' do
before do
fill_in 'Customer invoice ref template', with: 'cust-$id'
fill_in 'Vendor invoice ref template', with: 'vend-$id'
end

it 'updates account' do
expect {
subject
expect(page).to have_flash_message('Account was successfully updated.', type: :notice)
}.to change { Account.count }.by(0)

expect(page).to have_current_path account_path(account.id)

expect(account.reload).to have_attributes(
customer_invoice_ref_template: 'cust-$id',
vendor_invoice_ref_template: 'vend-$id'
)
end
end
end
54 changes: 51 additions & 3 deletions spec/features/billing/accounts/new_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,51 @@
next_vendor_invoice_type_id: nil,
customer_invoice_period_id: nil,
next_customer_invoice_at: nil,
next_customer_invoice_type_id: nil
next_customer_invoice_type_id: nil,
customer_invoice_ref_template: '$id',
vendor_invoice_ref_template: '$id'
)
end
end

context 'with invoice ref templates' do
let(:form_params) do
super().merge customer_invoice_ref_template: 'cust-$id',
vendor_invoice_ref_template: 'vend-$id'
end
before do
fill_in 'Customer invoice ref template', with: form_params[:customer_invoice_ref_template]
fill_in 'Vendor invoice ref template', with: form_params[:vendor_invoice_ref_template]
end

it 'creates new account successfully' do
subject

expect(page).to have_flash_message('Account was successfully created.', type: :notice)
account = Account.last!
expect(page).to have_current_path account_path(account.id)

expect(account).to have_attributes(
name: form_params[:name],
contractor: form_params[:contractor],
max_balance: form_params[:max_balance],
min_balance: form_params[:min_balance],
balance_low_threshold: form_params[:balance_low_threshold],
balance_high_threshold: form_params[:balance_high_threshold],
destination_rate_limit: form_params[:destination_rate_limit],
max_call_duration: form_params[:max_call_duration],
origination_capacity: form_params[:origination_capacity],
termination_capacity: form_params[:termination_capacity],
total_capacity: form_params[:total_capacity],
timezone: form_params[:timezone],
vendor_invoice_period_id: nil,
next_vendor_invoice_at: nil,
next_vendor_invoice_type_id: nil,
customer_invoice_period_id: nil,
next_customer_invoice_at: nil,
next_customer_invoice_type_id: nil,
customer_invoice_ref_template: form_params[:customer_invoice_ref_template],
vendor_invoice_ref_template: form_params[:vendor_invoice_ref_template]
)
end
end
Expand Down Expand Up @@ -118,7 +162,9 @@
next_vendor_invoice_type_id: nil,
customer_invoice_period_id: Billing::InvoicePeriod::WEEKLY_ID,
next_customer_invoice_at: account_time_zone.parse('2020-01-06 00:00:00'),
next_customer_invoice_type_id: Billing::InvoiceType::AUTO_FULL
next_customer_invoice_type_id: Billing::InvoiceType::AUTO_FULL,
customer_invoice_ref_template: '$id',
vendor_invoice_ref_template: '$id'
)
end
end
Expand Down Expand Up @@ -163,7 +209,9 @@
next_vendor_invoice_type_id: Billing::InvoiceType::AUTO_PARTIAL,
customer_invoice_period_id: nil,
next_customer_invoice_at: nil,
next_customer_invoice_type_id: nil
next_customer_invoice_type_id: nil,
customer_invoice_ref_template: '$id',
vendor_invoice_ref_template: '$id'
)
end
end
Expand Down
47 changes: 47 additions & 0 deletions spec/lib/invoice_ref_template_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

RSpec.describe InvoiceRefTemplate, '.call' do
subject do
InvoiceRefTemplate.call(invoice, template)
end

let!(:contractor) { FactoryBot.create(:vendor) }
let!(:account) { FactoryBot.create(:account, account_attrs) }
let(:account_attrs) do
{ contractor: contractor }
end
let(:invoice) { FactoryBot.create(:invoice, :vendor, :manual, invoice_attrs) }
let(:invoice_attrs) do
{ account: account }
end

context 'when template is "$id"' do
let(:template) { '$id' }

it { is_expected.to eq invoice.id.to_s }
end

context 'when template is "test_$id"' do
let(:template) { 'test_$id' }

it { is_expected.to eq "test_#{invoice.id}" }
end

context 'when template is "$id_test"' do
let(:template) { '$id_test' }

it { is_expected.to eq "#{invoice.id}_test" }
end

context 'when template is "$id_test_$id"' do
let(:template) { '$id_test_$id' }

it { is_expected.to eq "#{invoice.id}_test_#{invoice.id}" }
end

context 'when template is "test"' do
let(:template) { 'test' }

it { is_expected.to eq 'test' }
end
end
Loading

0 comments on commit 02448e9

Please sign in to comment.