From be960457e6ed11430c2342cb09f6fbff404a4d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 7 Sep 2020 16:58:27 +0300 Subject: [PATCH 01/16] Process payments: Create new invoice by transaction to allow direct top ups --- app/models/bank_transaction.rb | 14 ++++++++++---- app/models/invoice.rb | 9 +++++++++ app/models/registrar.rb | 4 ++-- lib/tasks/invoices/process_payments.rake | 2 ++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index f53a286ba..51895ae5e 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -31,12 +31,18 @@ class BankTransaction < ApplicationRecord @registrar ||= Invoice.find_by(reference_no: parsed_ref_number)&.buyer end + def autobindable? + return false if binded? + return false unless registrar + return false unless invoice + return false unless invoice.payable? + + true + end + # For successful binding, reference number, invoice id and sum must match with the invoice def autobind_invoice(manual: false) - return if binded? - return unless registrar - return unless invoice - return unless invoice.payable? + return unless autobindable? channel = if manual 'admin_payment' diff --git a/app/models/invoice.rb b/app/models/invoice.rb index a130a90ff..28fd60f00 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -112,6 +112,15 @@ class Invoice < ApplicationRecord e_invoice_sent_at.present? end + def self.create_from_transaction!(transaction) + registrar_user = Registrar.find_by(reference_no: transasction.parsed_ref_number) + return unless registrar_user + + registrar_user.issue_prepayment_invoice(amount: transaction.sum, + description: 'Direct top-up via bank transfer', + paid: true) + end + private def apply_default_buyer_vat_no diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 470d768b7..25380b4ef 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -54,7 +54,7 @@ class Registrar < ApplicationRecord end end - def issue_prepayment_invoice(amount, description = nil) + def issue_prepayment_invoice(amount, description = nil, paid: false) vat_rate = ::Invoice::VatRateCalculator.new(registrar: self).calculate invoice = invoices.create!( @@ -99,7 +99,7 @@ class Registrar < ApplicationRecord } ] ) - SendEInvoiceJob.enqueue(invoice.id) + SendEInvoiceJob.enqueue(invoice.id) unless paid invoice end diff --git a/lib/tasks/invoices/process_payments.rake b/lib/tasks/invoices/process_payments.rake index 340aba187..3e02a8838 100644 --- a/lib/tasks/invoices/process_payments.rake +++ b/lib/tasks/invoices/process_payments.rake @@ -36,6 +36,8 @@ namespace :invoices do reference_no: incoming_transaction.payment_reference_number, description: incoming_transaction.payment_description } transaction = bank_statement.bank_transactions.create!(transaction_attributes) + Invoice.create_from_transaction!(transaction) unless transaction.autobindable? + transaction.autobind_invoice end end From 5991ea6be7e13dbe2caadf72d4f837149e7d450c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 7 Sep 2020 17:06:36 +0300 Subject: [PATCH 02/16] Reduce complexity of autobindable?() conditional --- app/models/bank_transaction.rb | 17 +++++++---------- app/models/invoice.rb | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index 51895ae5e..fa4d63bff 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -32,12 +32,9 @@ class BankTransaction < ApplicationRecord end def autobindable? - return false if binded? - return false unless registrar - return false unless invoice - return false unless invoice.payable? - - true + binded? && registrar.present && invoice.payable? ? true : false + rescue NoMethodError + false end # For successful binding, reference number, invoice id and sum must match with the invoice @@ -113,6 +110,10 @@ class BankTransaction < ApplicationRecord end end + def parsed_ref_number + reference_no || ref_number_from_description + end + private def reset_pending_registrar_balance_reload @@ -122,10 +123,6 @@ class BankTransaction < ApplicationRecord registrar.save! end - def parsed_ref_number - reference_no || ref_number_from_description - end - def ref_number_from_description /(\d{7})/.match(description)[0] end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 28fd60f00..9e4f43dd8 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -113,7 +113,7 @@ class Invoice < ApplicationRecord end def self.create_from_transaction!(transaction) - registrar_user = Registrar.find_by(reference_no: transasction.parsed_ref_number) + registrar_user = Registrar.find_by(reference_no: transaction.parsed_ref_number) return unless registrar_user registrar_user.issue_prepayment_invoice(amount: transaction.sum, From c7d768fbed2ed01c54ecf59f2004835cbfb9b71f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 8 Sep 2020 11:14:26 +0300 Subject: [PATCH 03/16] Fix autobindable? condition --- app/models/bank_transaction.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index fa4d63bff..0fe6b63af 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -32,7 +32,7 @@ class BankTransaction < ApplicationRecord end def autobindable? - binded? && registrar.present && invoice.payable? ? true : false + !binded? && registrar && invoice.payable? ? true : false rescue NoMethodError false end From 20e3b2c09fae8633252a64d21897c7d057daebe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 8 Sep 2020 11:19:33 +0300 Subject: [PATCH 04/16] Fix some CC issues --- app/models/bank_transaction.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index 0fe6b63af..18e6e8c57 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -41,11 +41,7 @@ class BankTransaction < ApplicationRecord def autobind_invoice(manual: false) return unless autobindable? - channel = if manual - 'admin_payment' - else - 'system_payment' - end + channel = manual ? 'admin_payment' : 'system_payment' create_internal_payment_record(channel: channel, invoice: invoice, registrar: registrar) end From 8cc24b828f37ead38acd772f5dd756f79ee81945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 8 Sep 2020 11:53:37 +0300 Subject: [PATCH 05/16] Submit VAT-exclusive sum to prepayment invoice Prepayment invoices are subject to VAT, which is calculated from before-taxes sum --- app/models/invoice.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 9e4f43dd8..8122e46dd 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -116,7 +116,9 @@ class Invoice < ApplicationRecord registrar_user = Registrar.find_by(reference_no: transaction.parsed_ref_number) return unless registrar_user - registrar_user.issue_prepayment_invoice(amount: transaction.sum, + vat = VatRateCalculator.new(registrar: registrar_user).calculate + wo_vat = transaction.sum / (1 + (vat / 100)) + registrar_user.issue_prepayment_invoice(amount: wo_vat, description: 'Direct top-up via bank transfer', paid: true) end From 6b2aaf305f3225f47ed4e15004d6e97763de3ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 8 Sep 2020 13:08:19 +0300 Subject: [PATCH 06/16] Reference e_invoice development branch in Gemfile --- Gemfile | 2 +- Gemfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 12b826e3c..95abfc56a 100644 --- a/Gemfile +++ b/Gemfile @@ -64,7 +64,7 @@ gem 'jquery-ui-rails', '5.0.5' gem 'airbrake' gem 'company_register', github: 'internetee/company_register', branch: :master -gem 'e_invoice', github: 'internetee/e_invoice', branch: :master +gem 'e_invoice', github: 'internetee/e_invoice', branch: 'make-payable-configurable' gem 'lhv', github: 'internetee/lhv', branch: 'master' gem 'domain_name' gem 'haml', '~> 5.0' diff --git a/Gemfile.lock b/Gemfile.lock index 4144c9ed3..8a154953e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -18,8 +18,8 @@ GIT GIT remote: https://github.com/internetee/e_invoice.git - revision: b374ffd7be77b559b30c7a0210dc0df5ac3ed723 - branch: master + revision: 8a1aea2a1f0d46e1a8f3145b7b46a1810318d229 + branch: make-payable-configurable specs: e_invoice (0.1.0) builder (~> 3.2) From 17188ec6351637c2784b6baf07f9380907424eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 8 Sep 2020 13:09:12 +0300 Subject: [PATCH 07/16] Send unpayable e-invoice when invoice has already been paid via wire transfer --- app/jobs/send_e_invoice_job.rb | 10 +++++----- app/models/invoice.rb | 6 +++--- app/models/invoice/e_invoice_generator.rb | 7 +++++-- app/models/registrar.rb | 4 ++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/jobs/send_e_invoice_job.rb b/app/jobs/send_e_invoice_job.rb index e281db14d..91e068b9f 100644 --- a/app/jobs/send_e_invoice_job.rb +++ b/app/jobs/send_e_invoice_job.rb @@ -1,8 +1,8 @@ class SendEInvoiceJob < Que::Job - def run(invoice_id) - invoice = run_condition(Invoice.find_by(id: invoice_id)) + def run(invoice_id, payable: true) + invoice = run_condition(Invoice.find_by(id: invoice_id), payable: payable) - invoice.to_e_invoice.deliver + invoice.to_e_invoice(payable: payable).deliver ActiveRecord::Base.transaction do invoice.update(e_invoice_sent_at: Time.zone.now) log_success(invoice) @@ -15,9 +15,9 @@ class SendEInvoiceJob < Que::Job private - def run_condition(invoice) + def run_condition(invoice, payable: true) destroy unless invoice - destroy if invoice.do_not_send_e_invoice? + destroy if invoice.do_not_send_e_invoice? && payable invoice end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 8122e46dd..87f27af36 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -99,8 +99,8 @@ class Invoice < ApplicationRecord generator.as_pdf end - def to_e_invoice - generator = Invoice::EInvoiceGenerator.new(self) + def to_e_invoice(payable: true) + generator = Invoice::EInvoiceGenerator.new(self, payable: payable) generator.generate end @@ -120,7 +120,7 @@ class Invoice < ApplicationRecord wo_vat = transaction.sum / (1 + (vat / 100)) registrar_user.issue_prepayment_invoice(amount: wo_vat, description: 'Direct top-up via bank transfer', - paid: true) + payable: false) end private diff --git a/app/models/invoice/e_invoice_generator.rb b/app/models/invoice/e_invoice_generator.rb index 9a2ab2e01..d2963b93e 100644 --- a/app/models/invoice/e_invoice_generator.rb +++ b/app/models/invoice/e_invoice_generator.rb @@ -1,9 +1,11 @@ class Invoice class EInvoiceGenerator attr_reader :invoice + attr_reader :payable - def initialize(invoice) + def initialize(invoice, payable) @invoice = invoice + @payable = payable end def generate @@ -70,9 +72,10 @@ class Invoice i.total = invoice.total i.currency = invoice.currency i.delivery_channel = %i[internet_bank portal] + i.payable = payable end EInvoice::EInvoice.new(date: Time.zone.today, invoice: e_invoice_invoice) end end -end \ No newline at end of file +end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 25380b4ef..86ff5ef4d 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -54,7 +54,7 @@ class Registrar < ApplicationRecord end end - def issue_prepayment_invoice(amount, description = nil, paid: false) + def issue_prepayment_invoice(amount, description = nil, payable: true) vat_rate = ::Invoice::VatRateCalculator.new(registrar: self).calculate invoice = invoices.create!( @@ -99,7 +99,7 @@ class Registrar < ApplicationRecord } ] ) - SendEInvoiceJob.enqueue(invoice.id) unless paid + SendEInvoiceJob.enqueue(invoice.id, payable: payable) invoice end From 1a56fcf179e499805b6ffba274d5a510bb420936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 8 Sep 2020 13:49:20 +0300 Subject: [PATCH 08/16] Test balance reloading after bank transfer without invoice --- test/tasks/invoices/process_payments_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/tasks/invoices/process_payments_test.rb b/test/tasks/invoices/process_payments_test.rb index bd447be29..38c2e8fdc 100644 --- a/test/tasks/invoices/process_payments_test.rb +++ b/test/tasks/invoices/process_payments_test.rb @@ -82,6 +82,18 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase assert payment_order.failed? end + def test_credits_registrar_account_without_invoice_beforehand + registrar = registrars(:bestnames) + + assert_changes -> { registrar.accounts.first.balance } do + run_task + end + + assert_changes -> { registrar.invoices.count } do + run_task + end + end + def test_output assert_output "Transactions processed: 1\n" do run_task From 3decec8b02e479ba48f5a4f688aa7d0385586515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 8 Sep 2020 14:24:52 +0300 Subject: [PATCH 09/16] Test valid invoice sums when created by bank transaction --- app/models/invoice.rb | 8 ++++---- app/models/registrar.rb | 3 ++- test/models/invoice_test.rb | 12 ++++++++++++ test/tasks/invoices/process_payments_test.rb | 7 +++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 87f27af36..adaad87d6 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -117,10 +117,10 @@ class Invoice < ApplicationRecord return unless registrar_user vat = VatRateCalculator.new(registrar: registrar_user).calculate - wo_vat = transaction.sum / (1 + (vat / 100)) - registrar_user.issue_prepayment_invoice(amount: wo_vat, - description: 'Direct top-up via bank transfer', - payable: false) + wo_vat = (transaction.sum / (1 + (vat / 100))) + + registrar_user.issue_prepayment_invoice(wo_vat, 'Direct top-up via bank transfer', + payable: false, total: transaction.sum) end private diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 86ff5ef4d..42fbc2b78 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -54,7 +54,7 @@ class Registrar < ApplicationRecord end end - def issue_prepayment_invoice(amount, description = nil, payable: true) + def issue_prepayment_invoice(amount, description = nil, payable: true, total: nil) vat_rate = ::Invoice::VatRateCalculator.new(registrar: self).calculate invoice = invoices.create!( @@ -90,6 +90,7 @@ class Registrar < ApplicationRecord buyer_email: email, reference_no: reference_no, vat_rate: vat_rate, + total: total, items_attributes: [ { description: 'prepayment', diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index 9c1c45610..6ee2dc85c 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -109,4 +109,16 @@ class InvoiceTest < ActiveSupport::TestCase seller_zip: nil) assert_equal 'street, city, state', invoice.seller_address end + + def test_assumes_correct_sum_amount_when_created_by_transaction + registrar = registrars(:bestnames) + + bank_transaction = bank_transactions(:one).dup + bank_transaction.reference_no = registrar.reference_no + bank_transaction.sum = 5 + bank_transaction.save + + invoice = Invoice.create_from_transaction!(bank_transaction) + assert_equal 5, invoice.total + end end diff --git a/test/tasks/invoices/process_payments_test.rb b/test/tasks/invoices/process_payments_test.rb index 38c2e8fdc..eeaf411cc 100644 --- a/test/tasks/invoices/process_payments_test.rb +++ b/test/tasks/invoices/process_payments_test.rb @@ -94,6 +94,13 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase end end + def test_topup_creates_invoice_with_total_of_transactioned_amount + registrar = registrars(:bestnames) + run_task + + assert_equal 0.1, registrar.invoices.last.total + end + def test_output assert_output "Transactions processed: 1\n" do run_task From d62e55e6a179a98a269644fbdcc3ec79774fac15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 10 Sep 2020 12:00:13 +0300 Subject: [PATCH 10/16] Change Invoice::Item price scale to 3 places --- app/models/invoice.rb | 7 +++---- app/models/invoice_item.rb | 6 +++--- app/models/registrar.rb | 3 +-- ...ange_invoice_item_price_scale_to_three_places.rb | 5 +++++ db/structure.sql | 13 ++++++------- 5 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 db/migrate/20200910085157_change_invoice_item_price_scale_to_three_places.rb diff --git a/app/models/invoice.rb b/app/models/invoice.rb index adaad87d6..2f4be2e60 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -82,7 +82,7 @@ class Invoice < ApplicationRecord end def vat_amount - subtotal * vat_rate / 100 + (subtotal * vat_rate / 100) end def total @@ -118,9 +118,8 @@ class Invoice < ApplicationRecord vat = VatRateCalculator.new(registrar: registrar_user).calculate wo_vat = (transaction.sum / (1 + (vat / 100))) - registrar_user.issue_prepayment_invoice(wo_vat, 'Direct top-up via bank transfer', - payable: false, total: transaction.sum) + payable: false) end private @@ -130,6 +129,6 @@ class Invoice < ApplicationRecord end def calculate_total - self.total = subtotal + vat_amount + self.total = (subtotal + vat_amount).round(3) end end diff --git a/app/models/invoice_item.rb b/app/models/invoice_item.rb index ec0c77767..61339f5cb 100644 --- a/app/models/invoice_item.rb +++ b/app/models/invoice_item.rb @@ -5,7 +5,7 @@ class InvoiceItem < ApplicationRecord delegate :vat_rate, to: :invoice def item_sum_without_vat - (price * quantity).round(2) + (price * quantity).round(3) end alias_method :subtotal, :item_sum_without_vat @@ -14,6 +14,6 @@ class InvoiceItem < ApplicationRecord end def total - subtotal + vat_amount + (subtotal + vat_amount) end -end \ No newline at end of file +end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 42fbc2b78..86ff5ef4d 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -54,7 +54,7 @@ class Registrar < ApplicationRecord end end - def issue_prepayment_invoice(amount, description = nil, payable: true, total: nil) + def issue_prepayment_invoice(amount, description = nil, payable: true) vat_rate = ::Invoice::VatRateCalculator.new(registrar: self).calculate invoice = invoices.create!( @@ -90,7 +90,6 @@ class Registrar < ApplicationRecord buyer_email: email, reference_no: reference_no, vat_rate: vat_rate, - total: total, items_attributes: [ { description: 'prepayment', diff --git a/db/migrate/20200910085157_change_invoice_item_price_scale_to_three_places.rb b/db/migrate/20200910085157_change_invoice_item_price_scale_to_three_places.rb new file mode 100644 index 000000000..f1f41343b --- /dev/null +++ b/db/migrate/20200910085157_change_invoice_item_price_scale_to_three_places.rb @@ -0,0 +1,5 @@ +class ChangeInvoiceItemPriceScaleToThreePlaces < ActiveRecord::Migration[6.0] + def change + change_column :invoice_items, :price, :decimal, precision: 10, scale: 3 + end +end diff --git a/db/structure.sql b/db/structure.sql index 6224671ad..c8b141745 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1,7 +1,3 @@ ---- ---- PostgreSQL database dump ---- - SET statement_timeout = 0; SET lock_timeout = 0; SET client_encoding = 'UTF8'; @@ -371,7 +367,6 @@ CREATE TABLE public.bank_statements ( id integer NOT NULL, bank_code character varying, iban character varying, - import_file_path character varying, queried_at timestamp without time zone, created_at timestamp without time zone, updated_at timestamp without time zone, @@ -963,7 +958,7 @@ CREATE TABLE public.invoice_items ( description character varying NOT NULL, unit character varying NOT NULL, quantity integer NOT NULL, - price numeric(10,2) NOT NULL, + price numeric(10,3) NOT NULL, created_at timestamp without time zone, updated_at timestamp without time zone, creator_str character varying, @@ -4850,4 +4845,8 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200807110611'), ('20200811074839'), ('20200812090409'), -('20200812125810'); +('20200812125810'), +('20200908131554'), +('20200910085157'); + + From b3fd31f679bdd9a84d31e9e642db0a0fe938e257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 10 Sep 2020 12:24:38 +0300 Subject: [PATCH 11/16] Send invoice to Registrar's billing email after topup --- app/models/invoice.rb | 7 +++---- app/models/registrar.rb | 4 ++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 2f4be2e60..8e82bbea6 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -82,7 +82,7 @@ class Invoice < ApplicationRecord end def vat_amount - (subtotal * vat_rate / 100) + subtotal * vat_rate / 100 end def total @@ -117,9 +117,8 @@ class Invoice < ApplicationRecord return unless registrar_user vat = VatRateCalculator.new(registrar: registrar_user).calculate - wo_vat = (transaction.sum / (1 + (vat / 100))) - registrar_user.issue_prepayment_invoice(wo_vat, 'Direct top-up via bank transfer', - payable: false) + net = (transaction.sum / (1 + (vat / 100))) + registrar_user.issue_prepayment_invoice(net, 'Direct top-up via bank transfer', payable: false) end private diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 86ff5ef4d..4d3d9f926 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -101,6 +101,10 @@ class Registrar < ApplicationRecord ) SendEInvoiceJob.enqueue(invoice.id, payable: payable) + unless payable + InvoiceMailer.invoice_email(invoice: invoice, recipient: billing_email).deliver_now + end + invoice end From 58079c6955aaef35d21fc87a7be50c76db8df6a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 10 Sep 2020 13:00:05 +0300 Subject: [PATCH 12/16] Improve topup tests --- app/models/registrar.rb | 3 ++- test/models/invoice_test.rb | 33 ++++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 4d3d9f926..e5020d83f 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -99,12 +99,13 @@ class Registrar < ApplicationRecord } ] ) - SendEInvoiceJob.enqueue(invoice.id, payable: payable) unless payable InvoiceMailer.invoice_email(invoice: invoice, recipient: billing_email).deliver_now end + SendEInvoiceJob.enqueue(invoice.id, payable: payable) + invoice end diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index 6ee2dc85c..300de3a58 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -1,6 +1,8 @@ require 'test_helper' class InvoiceTest < ActiveSupport::TestCase + include ActionMailer::TestHelper + setup do @invoice = invoices(:one) end @@ -110,15 +112,32 @@ class InvoiceTest < ActiveSupport::TestCase assert_equal 'street, city, state', invoice.seller_address end - def test_assumes_correct_sum_amount_when_created_by_transaction + def test_creates_invoice_with_bank_transaction_total registrar = registrars(:bestnames) + transaction = bank_transactions(:one).dup + transaction.reference_no = registrar.reference_no + transaction.sum = 250 - bank_transaction = bank_transactions(:one).dup - bank_transaction.reference_no = registrar.reference_no - bank_transaction.sum = 5 - bank_transaction.save + invoice = Invoice.create_from_transaction!(transaction) + assert_equal 250, invoice.total - invoice = Invoice.create_from_transaction!(bank_transaction) - assert_equal 5, invoice.total + transaction.sum = 146.88 + invoice = Invoice.create_from_transaction!(transaction) + assert_equal 146.88, invoice.total + + transaction.sum = 0.99 + invoice = Invoice.create_from_transaction!(transaction) + assert_equal 0.99, invoice.total + end + + def test_emails_invoice_after_creating_topup_invoice + registrar = registrars(:bestnames) + transaction = bank_transactions(:one).dup + transaction.reference_no = registrar.reference_no + transaction.sum = 250 + + Invoice.create_from_transaction!(transaction) + + assert_emails 1 end end From 18aa7e3d63ba21892724eacee1ba6f16035c1db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 10 Sep 2020 14:14:42 +0300 Subject: [PATCH 13/16] Fix CC issues --- app/models/bank_transaction.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index 5a70a4380..e5ae76505 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -42,8 +42,7 @@ class BankTransaction < ApplicationRecord return unless autobindable? channel = manual ? 'admin_payment' : 'system_payment' - create_internal_payment_record(channel: channel, invoice: invoice, - registrar: registrar) + create_internal_payment_record(channel: channel, invoice: invoice, registrar: registrar) end def create_internal_payment_record(channel: nil, invoice:, registrar:) @@ -92,12 +91,11 @@ class BankTransaction < ApplicationRecord end def create_activity(registrar, invoice) - activity = AccountActivity.new( - account: registrar.cash_account, bank_transaction: self, - invoice: invoice, sum: invoice.subtotal, - currency: currency, description: description, - activity_type: AccountActivity::ADD_CREDIT - ) + activity = AccountActivity.new(account: registrar.cash_account, bank_transaction: self, + invoice: invoice, sum: invoice.subtotal, + currency: currency, description: description, + activity_type: AccountActivity::ADD_CREDIT) + if activity.save reset_pending_registrar_balance_reload true From 8a5208057a271fe74212f10998f2424f007e5153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 10 Sep 2020 16:38:27 +0300 Subject: [PATCH 14/16] Fix parsing from ref number --- app/models/bank_transaction.rb | 7 +++---- test/models/invoice_test.rb | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index e5ae76505..24bf51e0c 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -118,12 +118,11 @@ class BankTransaction < ApplicationRecord end def ref_number_from_description - (Billing::ReferenceNo::MULTI_REGEXP.match(description) || []).captures.each do |match| - break match if match.length == 7 || valid_ref_no?(match) - end + matches = description.to_s.scan(Billing::ReferenceNo::MULTI_REGEXP).flatten + matches.detect { |m| break m if m.length == 7 || valid_ref_no?(m) } end def valid_ref_no?(match) - return true if Billing::ReferenceNo.valid?(match) && Registrar.find_by(reference_no: match).any? + return true if Billing::ReferenceNo.valid?(match) && Registrar.find_by(reference_no: match) end end diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index 300de3a58..150e8032c 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -136,8 +136,8 @@ class InvoiceTest < ActiveSupport::TestCase transaction.reference_no = registrar.reference_no transaction.sum = 250 - Invoice.create_from_transaction!(transaction) - - assert_emails 1 + assert_emails 1 do + Invoice.create_from_transaction!(transaction) + end end end From 90b5baa29dfc7efc287ce130818a4dd6f1735baa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 10 Sep 2020 17:30:39 +0300 Subject: [PATCH 15/16] Test reference_no parsing from transaction description --- test/models/bank_transaction_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/models/bank_transaction_test.rb b/test/models/bank_transaction_test.rb index 944b47573..9a9b02a74 100644 --- a/test/models/bank_transaction_test.rb +++ b/test/models/bank_transaction_test.rb @@ -156,6 +156,24 @@ class BankTransactionTest < ActiveSupport::TestCase assert transaction.errors.full_messages.include?('Cannot bind cancelled invoice') end + def test_assumes_7_digit_number_is_reference_no_in_desc + statement = BankTransaction.new + statement.description = 'number 1234567 defo valid' + assert_equal '1234567', statement.parsed_ref_number + end + + def test_determines_correct_ref_no_from_description + statement = BankTransaction.new + ref_no = registrars(:bestnames).reference_no + statement.description = "invoice 123 125 55 4521 #{ref_no} 7541 defo valid" + assert_equal ref_no.to_s, statement.parsed_ref_number + end + + def test_parsed_ref_no_returns_nil_if_ref_not_found + statement = BankTransaction.new + statement.description = "all invalid 12 123 55 77777 --" + assert_nil statement.parsed_ref_number + end private def create_payable_invoice(attributes) From a2b26a0c820276b4e13b383004643b0ed25e6dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Fri, 11 Sep 2020 11:39:49 +0300 Subject: [PATCH 16/16] Reference master git branch of e_invoice gem --- Gemfile | 2 +- Gemfile.lock | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 95abfc56a..12b826e3c 100644 --- a/Gemfile +++ b/Gemfile @@ -64,7 +64,7 @@ gem 'jquery-ui-rails', '5.0.5' gem 'airbrake' gem 'company_register', github: 'internetee/company_register', branch: :master -gem 'e_invoice', github: 'internetee/e_invoice', branch: 'make-payable-configurable' +gem 'e_invoice', github: 'internetee/e_invoice', branch: :master gem 'lhv', github: 'internetee/lhv', branch: 'master' gem 'domain_name' gem 'haml', '~> 5.0' diff --git a/Gemfile.lock b/Gemfile.lock index 8a154953e..a49c9becb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -18,8 +18,8 @@ GIT GIT remote: https://github.com/internetee/e_invoice.git - revision: 8a1aea2a1f0d46e1a8f3145b7b46a1810318d229 - branch: make-payable-configurable + revision: 5f8d0029bf1affdbf2bd6e3d1ce87d34066add4d + branch: master specs: e_invoice (0.1.0) builder (~> 3.2) @@ -238,7 +238,7 @@ GEM http-cookie (1.0.3) domain_name (~> 0.5) httpclient (2.8.3) - httpi (2.4.4) + httpi (2.4.5) rack socksify i18n (1.8.5) @@ -467,7 +467,8 @@ GEM i18n warden (1.2.8) rack (>= 2.0.6) - wasabi (3.5.0) + wasabi (3.6.1) + addressable httpi (~> 2.0) nokogiri (>= 1.4.2) webdrivers (4.4.1)