From 955dc106930409c24b07e185e3a6bb9843efe446 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 20 Nov 2017 08:10:59 +0200 Subject: [PATCH 01/63] Extract partial #623 --- app/views/admin/registrars/_billing.html.erb | 17 +++++++++++++++++ app/views/admin/registrars/show.html.erb | 19 +------------------ config/locales/admin/registrars.en.yml | 3 +++ config/locales/en.yml | 4 +++- 4 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 app/views/admin/registrars/_billing.html.erb diff --git a/app/views/admin/registrars/_billing.html.erb b/app/views/admin/registrars/_billing.html.erb new file mode 100644 index 000000000..f3960233e --- /dev/null +++ b/app/views/admin/registrars/_billing.html.erb @@ -0,0 +1,17 @@ +
+
+ <%= t '.header' %> +
+
+
+
<%= Registrar.human_attribute_name :vat_no %>
+
<%= registrar.vat_no %>
+ +
<%= Registrar.human_attribute_name :accounting_customer_code %>
+
<%= registrar.accounting_customer_code %>
+ +
<%= Registrar.human_attribute_name :billing_email %>
+
<%= registrar.billing_email %>
+
+
+
diff --git a/app/views/admin/registrars/show.html.erb b/app/views/admin/registrars/show.html.erb index c09602985..b42d1b9f3 100644 --- a/app/views/admin/registrars/show.html.erb +++ b/app/views/admin/registrars/show.html.erb @@ -41,12 +41,6 @@
<%= @registrar.reg_no %>
-
- <%= t(:vat_no) %> -
-
- <%= @registrar.vat_no %> -
<%= t(:reference_no) %>
@@ -71,12 +65,6 @@
<%= @registrar.website %>
-
- <%= Registrar.human_attribute_name :accounting_customer_code %> -
-
- <%= @registrar.accounting_customer_code %> -
@@ -120,15 +108,10 @@
<%= @registrar.billing_address %>
-
- <%= t(:billing_email) %> -
-
- <%= @registrar.billing_email %> -
+ <%= render 'billing', registrar: @registrar %> <%= render 'admin/registrars/show/preferences', registrar: registrar %> diff --git a/config/locales/admin/registrars.en.yml b/config/locales/admin/registrars.en.yml index 3d269f64b..e805b19c6 100644 --- a/config/locales/admin/registrars.en.yml +++ b/config/locales/admin/registrars.en.yml @@ -12,6 +12,9 @@ en: preferences: header: Preferences + billing: + header: Billing + create: created: Registrar has been successfully created not_created: Unable to create registrar diff --git a/config/locales/en.yml b/config/locales/en.yml index 6b1dc7e31..89e7b0aad 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -199,7 +199,6 @@ en: alg: 'Algorithm' public_key: 'Public key' registrar: - billing_email: 'Billing e-mail' phone: 'Contact phone' email: 'Contact e-mail' state: 'State / Province' @@ -782,3 +781,6 @@ en: delimiter: " " precision: 2 unit: € + + attributes: + vat_no: VAT number From d2e84d28c6f9a82df583e80007259b5fc04f16ef Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 20 Nov 2017 08:12:40 +0200 Subject: [PATCH 02/63] Remove unused HTML #623 --- app/views/admin/registrars/show.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/admin/registrars/show.html.erb b/app/views/admin/registrars/show.html.erb index b42d1b9f3..c0a596b27 100644 --- a/app/views/admin/registrars/show.html.erb +++ b/app/views/admin/registrars/show.html.erb @@ -117,7 +117,7 @@
-
+
<%= t('.api_users') %> @@ -157,7 +157,7 @@
-
+
<%= t(:white_ips) %> From 4a41984a5c8da8287b5e93e57d510e4c28588474 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 21 Nov 2017 12:04:41 +0200 Subject: [PATCH 03/63] Use human_attribute_name #623 --- app/views/registrant/registrars/show.haml | 2 +- app/views/registrar/invoices/partials/_seller.haml | 2 +- app/views/registrar/invoices/pdf.haml | 2 +- config/locales/en.yml | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/views/registrant/registrars/show.haml b/app/views/registrant/registrars/show.haml index e0da73ee3..6d4aa9e79 100644 --- a/app/views/registrant/registrars/show.haml +++ b/app/views/registrant/registrars/show.haml @@ -19,7 +19,7 @@ %dt= t(:reg_no) %dd= @registrar.reg_no - %dt= t(:vat_no) + %dt= Registrar.human_attribute_name :vat_no %dd= @registrar.vat_no %dt= t(:id) diff --git a/app/views/registrar/invoices/partials/_seller.haml b/app/views/registrar/invoices/partials/_seller.haml index 0008c2f13..9c5387e16 100644 --- a/app/views/registrar/invoices/partials/_seller.haml +++ b/app/views/registrar/invoices/partials/_seller.haml @@ -16,7 +16,7 @@ %dt= t(:swift) %dd= @invoice.seller_swift - %dt= t(:vat_no) + %dt= Registrar.human_attribute_name :vat_no %dd= @invoice.seller_vat_no %dt= t(:address) diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index a74254a2a..61b4e5789 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -261,7 +261,7 @@ %br = "#{t('reg_no')} #{@invoice.seller_reg_no}" %br - = "#{t('vat_no')} #{@invoice.seller_vat_no}" + = "#{Registrar.human_attribute_name :vat_no} #{@invoice.seller_vat_no}" .col-md-3.left = @invoice.seller_phone diff --git a/config/locales/en.yml b/config/locales/en.yml index 89e7b0aad..4951f3b26 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -316,7 +316,6 @@ en: new_registrar: 'New registrar' registrar_details: 'Registrar details' - vat_no: 'VAT no' edit_registrar: 'Edit registrar' back_to_registrar: 'Back to registrar' registrar_deleted: 'Registrar deleted' From f86ab085acf5c1f637a300a76952cd08871cca07 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 08:38:26 +0200 Subject: [PATCH 04/63] Add `registrars.vat_rate` DB column #623 --- db/migrate/20180228055259_add_registrars_vat_rate.rb | 5 +++++ db/structure.sql | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180228055259_add_registrars_vat_rate.rb diff --git a/db/migrate/20180228055259_add_registrars_vat_rate.rb b/db/migrate/20180228055259_add_registrars_vat_rate.rb new file mode 100644 index 000000000..5965b65cf --- /dev/null +++ b/db/migrate/20180228055259_add_registrars_vat_rate.rb @@ -0,0 +1,5 @@ +class AddRegistrarsVatRate < ActiveRecord::Migration + def change + add_column :registrars, :vat_rate, :decimal, precision: 4, scale: 3 + end +end diff --git a/db/structure.sql b/db/structure.sql index 237c82e66..722aa4111 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2436,7 +2436,8 @@ CREATE TABLE registrars ( legacy_id integer, reference_no character varying, test_registrar boolean DEFAULT false, - language character varying NOT NULL + language character varying NOT NULL, + vat_rate numeric(4,3) ); @@ -5112,3 +5113,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180214213743'); INSERT INTO schema_migrations (version) VALUES ('20180218004148'); +INSERT INTO schema_migrations (version) VALUES ('20180228055259'); + From 7dee10c6835835769f8e92b43d5d1a59a2f5164d Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 08:46:36 +0200 Subject: [PATCH 05/63] Rename `invoices.var_prc` to `vat_rate` #623 --- app/models/directo.rb | 2 +- app/models/invoice.rb | 6 +++--- app/models/registrar.rb | 2 +- app/views/registrar/invoices/partials/_items.haml | 2 +- app/views/registrar/invoices/pdf.haml | 2 +- config/locales/en.yml | 2 +- ...20180228064342_rename_invoices_var_prc_to_vat_rate.rb | 5 +++++ db/structure.sql | 4 +++- doc/models_complete.svg | 2 +- spec/factories/invoice.rb | 2 +- spec/models/invoice_spec.rb | 2 +- test/fixtures/invoices.yml | 2 +- test/models/invoice_test.rb | 9 +++++++++ 13 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 db/migrate/20180228064342_rename_invoices_var_prc_to_vat_rate.rb create mode 100644 test/models/invoice_test.rb diff --git a/app/models/directo.rb b/app/models/directo.rb index 62cf43804..4a5f00dd9 100644 --- a/app/models/directo.rb +++ b/app/models/directo.rb @@ -34,7 +34,7 @@ class Directo < ActiveRecord::Base xml.line( "ProductID" => Setting.directo_receipt_product_name, "Quantity" => 1, - "UnitPriceWoVAT" => ActionController::Base.helpers.number_with_precision(invoice.sum_cache/(1+invoice.vat_prc), precision: 2, separator: "."), + "UnitPriceWoVAT" => ActionController::Base.helpers.number_with_precision(invoice.sum_cache/(1+invoice.vat_rate), precision: 2, separator: "."), "ProductName" => invoice.order ) } diff --git a/app/models/invoice.rb b/app/models/invoice.rb index ad478443d..37eb8a72c 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -28,7 +28,7 @@ class Invoice < ActiveRecord::Base validates :billing_email, email_format: { message: :invalid }, allow_blank: true validates :invoice_type, :due_date, :currency, :seller_name, - :seller_iban, :buyer_name, :invoice_items, :vat_prc, presence: true + :seller_iban, :buyer_name, :invoice_items, :vat_rate, presence: true before_create :set_invoice_number, :check_vat @@ -52,7 +52,7 @@ class Invoice < ActiveRecord::Base def check_vat if buyer.country_code != 'EE' && buyer.vat_no.present? - self.vat_prc = 0 + self.vat_rate = 0 end end @@ -157,7 +157,7 @@ class Invoice < ActiveRecord::Base end def vat - (sum_without_vat * vat_prc).round(2) + (sum_without_vat * vat_rate).round(2) end def sum diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 46022808f..5fb68fea2 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -81,7 +81,7 @@ class Registrar < ActiveRecord::Base payment_term: 'prepayment', description: description, currency: 'EUR', - vat_prc: Setting.registry_vat_prc, + vat_rate: Setting.registry_vat_prc, seller_name: Setting.registry_juridical_name, seller_reg_no: Setting.registry_reg_no, seller_iban: Setting.registry_iban, diff --git a/app/views/registrar/invoices/partials/_items.haml b/app/views/registrar/invoices/partials/_items.haml index 6ed5144a8..64fc71786 100644 --- a/app/views/registrar/invoices/partials/_items.haml +++ b/app/views/registrar/invoices/partials/_items.haml @@ -24,7 +24,7 @@ %td= currency(@invoice.sum_without_vat) %tr %th.no-border{colspan: 3} - %th= t('vat', vat_prc: (@invoice.vat_prc * 100).round) + %th= t('vat', rate: (@invoice.vat_rate * 100).round) %td= currency(@invoice.vat) %tr %th.no-border{colspan: 3} diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index 61b4e5789..6f92c4a8a 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -242,7 +242,7 @@ %td= "#{currency(@invoice.sum_without_vat)} #{@invoice.currency}" %tr %th.no-border{colspan: 3} - %th= t('vat', vat_prc: (@invoice.vat_prc * 100).round) + %th= t('vat', rate: (@invoice.vat_rate * 100).round) %td= "#{currency(@invoice.vat)} #{@invoice.currency}" %tr %th.no-border{colspan: 3} diff --git a/config/locales/en.yml b/config/locales/en.yml index 4951f3b26..58c4e4c69 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -553,7 +553,7 @@ en: invoice_number: Invoice no. seller: 'Seller' prepayment: 'Prepayment' - vat: 'VAT (%{vat_prc}%)' + vat: 'VAT (%{rate}%)' unpaid: 'Unpaid' your_current_account_balance_is: 'Your current account balance is %{balance} %{currency}' billing: 'Billing' diff --git a/db/migrate/20180228064342_rename_invoices_var_prc_to_vat_rate.rb b/db/migrate/20180228064342_rename_invoices_var_prc_to_vat_rate.rb new file mode 100644 index 000000000..b1d13de1d --- /dev/null +++ b/db/migrate/20180228064342_rename_invoices_var_prc_to_vat_rate.rb @@ -0,0 +1,5 @@ +class RenameInvoicesVarPrcToVatRate < ActiveRecord::Migration + def change + rename_column :invoices, :vat_prc, :vat_rate + end +end diff --git a/db/structure.sql b/db/structure.sql index 722aa4111..cb47a50c0 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1127,7 +1127,7 @@ CREATE TABLE invoices ( currency character varying NOT NULL, description character varying, reference_no character varying, - vat_prc numeric(10,2) NOT NULL, + vat_rate numeric(10,2) NOT NULL, paid_at timestamp without time zone, seller_id integer, seller_name character varying NOT NULL, @@ -5115,3 +5115,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180218004148'); INSERT INTO schema_migrations (version) VALUES ('20180228055259'); +INSERT INTO schema_migrations (version) VALUES ('20180228064342'); + diff --git a/doc/models_complete.svg b/doc/models_complete.svg index 148b12942..12fb078a4 100644 --- a/doc/models_complete.svg +++ b/doc/models_complete.svg @@ -1687,7 +1687,7 @@ currency :string description :string reference_no :string -vat_prc :decimal +vat_rate :decimal paid_at :datetime seller_id :integer seller_name :string diff --git a/spec/factories/invoice.rb b/spec/factories/invoice.rb index 7eeed32b9..0ea81d5e4 100644 --- a/spec/factories/invoice.rb +++ b/spec/factories/invoice.rb @@ -8,7 +8,7 @@ FactoryBot.define do seller_name { 'EIS' } seller_city { 'Tallinn' } seller_street { 'Paldiski mnt. 123' } - vat_prc 0.2 + vat_rate 0.2 buyer { FactoryBot.create(:registrar) } after :build do |invoice| diff --git a/spec/models/invoice_spec.rb b/spec/models/invoice_spec.rb index 49d15310f..eb8866d07 100644 --- a/spec/models/invoice_spec.rb +++ b/spec/models/invoice_spec.rb @@ -54,7 +54,7 @@ describe Invoice do it 'should calculate sums correctly' do @invoice = create(:invoice) - @invoice.vat_prc.should == BigDecimal.new('0.2') + @invoice.vat_rate.should == BigDecimal.new('0.2') @invoice.sum_without_vat.should == BigDecimal.new('300.0') @invoice.vat.should == BigDecimal.new('60.0') @invoice.sum.should == BigDecimal.new('360.0') diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index 7a1c85dd1..04cedc153 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -6,7 +6,7 @@ DEFAULTS: &DEFAULTS seller_name: John Doe seller_iban: 1234 buyer_name: Jane Doe - vat_prc: 0.2 + vat_rate: 0.2 valid: <<: *DEFAULTS diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb new file mode 100644 index 000000000..c89c37626 --- /dev/null +++ b/test/models/invoice_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class InvoiceTest < ActiveSupport::TestCase + def test_invalid_without_vat_rate + invoice = Invoice.new(vat_rate: nil) + invoice.validate + assert invoice.errors.added?(:vat_rate, :blank) + end +end From dc620e7780d3f2e70a6959a8df3195e4c5e2d9c7 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 08:47:54 +0200 Subject: [PATCH 06/63] Fix typo #623 --- ...rb => 20180228064342_rename_invoices_vat_prc_to_vat_rate.rb} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename db/migrate/{20180228064342_rename_invoices_var_prc_to_vat_rate.rb => 20180228064342_rename_invoices_vat_prc_to_vat_rate.rb} (53%) diff --git a/db/migrate/20180228064342_rename_invoices_var_prc_to_vat_rate.rb b/db/migrate/20180228064342_rename_invoices_vat_prc_to_vat_rate.rb similarity index 53% rename from db/migrate/20180228064342_rename_invoices_var_prc_to_vat_rate.rb rename to db/migrate/20180228064342_rename_invoices_vat_prc_to_vat_rate.rb index b1d13de1d..89b4de724 100644 --- a/db/migrate/20180228064342_rename_invoices_var_prc_to_vat_rate.rb +++ b/db/migrate/20180228064342_rename_invoices_vat_prc_to_vat_rate.rb @@ -1,4 +1,4 @@ -class RenameInvoicesVarPrcToVatRate < ActiveRecord::Migration +class RenameInvoicesVatPrcToVatRate < ActiveRecord::Migration def change rename_column :invoices, :vat_prc, :vat_rate end From fccc6eb9f0581a3ae299f4a11e5a38fac8cc573e Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 09:02:23 +0200 Subject: [PATCH 07/63] Change `invoices.vat_rate` DB column type #623 --- db/migrate/20180228070102_change_invoices_vat_rate_type.rb | 5 +++++ db/structure.sql | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180228070102_change_invoices_vat_rate_type.rb diff --git a/db/migrate/20180228070102_change_invoices_vat_rate_type.rb b/db/migrate/20180228070102_change_invoices_vat_rate_type.rb new file mode 100644 index 000000000..2c0790a18 --- /dev/null +++ b/db/migrate/20180228070102_change_invoices_vat_rate_type.rb @@ -0,0 +1,5 @@ +class ChangeInvoicesVatRateType < ActiveRecord::Migration + def change + change_column :invoices, :vat_rate, :decimal, precision: 4, scale: 3 + end +end diff --git a/db/structure.sql b/db/structure.sql index cb47a50c0..3f890fcd9 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1127,7 +1127,7 @@ CREATE TABLE invoices ( currency character varying NOT NULL, description character varying, reference_no character varying, - vat_rate numeric(10,2) NOT NULL, + vat_rate numeric(4,3) NOT NULL, paid_at timestamp without time zone, seller_id integer, seller_name character varying NOT NULL, @@ -5117,3 +5117,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180228055259'); INSERT INTO schema_migrations (version) VALUES ('20180228064342'); +INSERT INTO schema_migrations (version) VALUES ('20180228070102'); + From 6f9ea85a95dd9b55efd2ad5da4e676c4c9d7ea7c Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 09:05:41 +0200 Subject: [PATCH 08/63] Change `invoices.vat_rate` DB column to NULL #623 --- db/migrate/20180228070431_change_invoice_vat_rate_to_null.rb | 5 +++++ db/structure.sql | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180228070431_change_invoice_vat_rate_to_null.rb diff --git a/db/migrate/20180228070431_change_invoice_vat_rate_to_null.rb b/db/migrate/20180228070431_change_invoice_vat_rate_to_null.rb new file mode 100644 index 000000000..37b8394b5 --- /dev/null +++ b/db/migrate/20180228070431_change_invoice_vat_rate_to_null.rb @@ -0,0 +1,5 @@ +class ChangeInvoiceVatRateToNull < ActiveRecord::Migration + def change + change_column_null :invoices, :vat_rate, true + end +end diff --git a/db/structure.sql b/db/structure.sql index 3f890fcd9..ba040e7cd 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1127,7 +1127,7 @@ CREATE TABLE invoices ( currency character varying NOT NULL, description character varying, reference_no character varying, - vat_rate numeric(4,3) NOT NULL, + vat_rate numeric(4,3), paid_at timestamp without time zone, seller_id integer, seller_name character varying NOT NULL, @@ -5119,3 +5119,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180228064342'); INSERT INTO schema_migrations (version) VALUES ('20180228070102'); +INSERT INTO schema_migrations (version) VALUES ('20180228070431'); + From c47ca77ca655c3c1c35faecbf89ba2f3c3ac0944 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 23 Nov 2017 23:27:06 +0200 Subject: [PATCH 09/63] Add registrar VAT rate #623 --- .../admin/registrars_controller.rb | 3 +- app/models/registrar.rb | 20 ++++++++ app/views/admin/registrars/_billing.html.erb | 3 ++ .../admin/registrars/form/_billing.html.erb | 12 +++++ config/locales/en.yml | 1 + test/fixtures/registrars.yml | 7 +++ .../admin/registrars/show_registrar_test.rb | 8 +++ test/models/registrar_test.rb | 50 +++++++++++++++++++ test/test_helper.rb | 2 + 9 files changed, 105 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index 119abf85b..eb9ae9bd7 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -79,7 +79,8 @@ module Admin :code, :test_registrar, :accounting_customer_code, - :language) + :language, + :vat_rate,) end end end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 5fb68fea2..393bd56f2 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -17,10 +17,22 @@ class Registrar < ActiveRecord::Base validates :name, :reg_no, :reference_no, :code, uniqueness: true validates :accounting_customer_code, presence: true validates :language, presence: true + validates :vat_rate, :vat_no, absence: true, if: :local_vat_payer? + validates :vat_rate, presence: true, if: 'foreign_vat_payer? && vat_no.blank?' + validates :vat_rate, absence: true, if: 'foreign_vat_payer? && vat_no?' + validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 99 }, allow_nil: true validate :forbidden_codes after_initialize :set_defaults + before_save do + self.vat_rate = vat_rate / 100.0 if vat_rate + end + + after_find do |registrar| + registrar.vat_rate = registrar.vat_rate * 100 if registrar.vat_rate + end + def forbidden_codes return true unless ['CID'].include? code errors.add(:code, I18n.t(:forbidden_code)) @@ -163,4 +175,12 @@ class Registrar < ActiveRecord::Base def set_defaults self.language = Setting.default_language unless language end + + def local_vat_payer? + country == Registry.instance.legal_address_country + end + + def foreign_vat_payer? + !local_vat_payer? + end end diff --git a/app/views/admin/registrars/_billing.html.erb b/app/views/admin/registrars/_billing.html.erb index f3960233e..7ba778d08 100644 --- a/app/views/admin/registrars/_billing.html.erb +++ b/app/views/admin/registrars/_billing.html.erb @@ -7,6 +7,9 @@
<%= Registrar.human_attribute_name :vat_no %>
<%= registrar.vat_no %>
+
<%= Registrar.human_attribute_name :vat_rate %>
+
<%= number_to_percentage registrar.vat_rate, precision: 1 %>
+
<%= Registrar.human_attribute_name :accounting_customer_code %>
<%= registrar.accounting_customer_code %>
diff --git a/app/views/admin/registrars/form/_billing.html.erb b/app/views/admin/registrars/form/_billing.html.erb index a3adf1312..5bc87a93d 100644 --- a/app/views/admin/registrars/form/_billing.html.erb +++ b/app/views/admin/registrars/form/_billing.html.erb @@ -17,6 +17,18 @@
+
+
+ <%= f.label :vat_rate %> +
+
+
+ <%= f.number_field :vat_rate, class: 'form-control' %> +
%
+
+
+
+
<%= f.label :accounting_customer_code %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 58c4e4c69..e9b8316d6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -783,3 +783,4 @@ en: attributes: vat_no: VAT number + vat_rate: VAT rate diff --git a/test/fixtures/registrars.yml b/test/fixtures/registrars.yml index 0026f323b..b5715a0a5 100644 --- a/test/fixtures/registrars.yml +++ b/test/fixtures/registrars.yml @@ -16,3 +16,10 @@ goodnames: country_code: US accounting_customer_code: goodnames language: en + +foreign_vat_payer_with_rate: + <<: *DEFAULTS + name: 3 + reg_no: 3 + code: 3 + vat_rate: 0.2 diff --git a/test/integration/admin/registrars/show_registrar_test.rb b/test/integration/admin/registrars/show_registrar_test.rb index cb310317c..f13748191 100644 --- a/test/integration/admin/registrars/show_registrar_test.rb +++ b/test/integration/admin/registrars/show_registrar_test.rb @@ -16,4 +16,12 @@ class ShowRegistrarTest < ActionDispatch::IntegrationTest def test_language assert_text 'Language English' end + + def test_vat_number + assert_text 'US12345' + end + + def test_vat_rate + assert_text number_to_percentage(@registrar.vat_rate, precision: 1) + end end diff --git a/test/models/registrar_test.rb b/test/models/registrar_test.rb index 58a06b0ae..3ace52ba7 100644 --- a/test/models/registrar_test.rb +++ b/test/models/registrar_test.rb @@ -38,4 +38,54 @@ class RegistrarTest < ActiveSupport::TestCase registrar = Registrar.new(language: 'de') assert_equal 'de', registrar.language end + + def test_rejects_vat_number_when_local_vat_payer + Registry.instance.stub(:legal_address_country, Country.new('US')) do + @registrar.vat_no = 'US1' + @registrar.validate + assert @registrar.invalid? + end + end + + def test_rejects_vat_rate_when_local_vat_payer + Registry.instance.stub(:legal_address_country, Country.new('US')) do + @registrar.vat_rate = 20 + @registrar.validate + assert @registrar.invalid? + end + end + + def test_rejects_negative_vat_rate + @registrar.vat_rate = -1 + @registrar.validate + assert @registrar.invalid? + end + + def test_rejects_vat_rate_greater_than_max + @registrar.vat_rate = 100 + @registrar.validate + assert @registrar.invalid? + end + + def test_converts_integer_vat_rate_to_fractional + @registrar = registrars(:foreign_vat_payer_with_rate) + assert_equal 20, @registrar.vat_rate + end + + def test_requires_vat_rate_when_foreign_vat_payer_without_number + Registry.instance.stub(:legal_address_country, Country.new('GB')) do + @registrar.vat_no = nil + @registrar.validate + assert @registrar.invalid? + end + end + + def test_rejects_vat_rate_when_foreign_vat_payer_with_number + Registry.instance.stub(:legal_address_country, Country.new('GB')) do + @registrar.vat_no = 'US1' + @registrar.vat_rate = 1 + @registrar.validate + assert @registrar.invalid? + end + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 7bde0991d..64cef7e0f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,6 +14,8 @@ require 'webmock/minitest' Setting.address_processing = false +Setting.registry_country_code = 'US' + class ActiveSupport::TestCase include FactoryBot::Syntax::Methods From 6a0122613854abeee5f25c579a8abb292e60262d Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 27 Nov 2017 11:11:06 +0200 Subject: [PATCH 10/63] Update VAT #623 --- app/models/invoice.rb | 5 ++- app/models/registrar.rb | 38 +++++++++++++++---- test/fixtures/invoices.yml | 8 ++++ .../admin/invoices/invoice_details_test.rb | 19 ++++++++++ .../admin/registrars/show_registrar_test.rb | 2 +- .../invoices/invoice_details_test.rb | 19 ++++++++++ test/models/invoice_test.rb | 31 +++++++++++++-- test/models/registrar_test.rb | 24 +++++++++--- 8 files changed, 126 insertions(+), 20 deletions(-) create mode 100644 test/integration/admin/invoices/invoice_details_test.rb create mode 100644 test/integration/registrar/invoices/invoice_details_test.rb diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 37eb8a72c..90fa380e1 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -29,6 +29,7 @@ class Invoice < ActiveRecord::Base validates :invoice_type, :due_date, :currency, :seller_name, :seller_iban, :buyer_name, :invoice_items, :vat_rate, presence: true + validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 99 }, allow_nil: true before_create :set_invoice_number, :check_vat @@ -106,12 +107,12 @@ class Invoice < ActiveRecord::Base def buyer_country Country.new(buyer_country_code) end - + # order is used for directo/banklink description def order "Order nr. #{number}" end - + def pdf(html) kit = PDFKit.new(html) kit.to_pdf diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 393bd56f2..dd7a7a9f5 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -1,3 +1,21 @@ +class VATRateType < ActiveRecord::Type::Value + def type_cast_from_user(value) + if value.blank? + nil + else + super + end + end + + def type_cast_from_database(value) + BigDecimal.new(value) * 100 if value + end + + def type_cast_for_database(value) + BigDecimal.new(value) / 100 if value + end +end + class Registrar < ActiveRecord::Base include Versions # version/registrar_version.rb @@ -23,16 +41,9 @@ class Registrar < ActiveRecord::Base validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 99 }, allow_nil: true validate :forbidden_codes + attribute :vat_rate, VATRateType.new after_initialize :set_defaults - before_save do - self.vat_rate = vat_rate / 100.0 if vat_rate - end - - after_find do |registrar| - registrar.vat_rate = registrar.vat_rate * 100 if registrar.vat_rate - end - def forbidden_codes return true unless ['CID'].include? code errors.add(:code, I18n.t(:forbidden_code)) @@ -87,6 +98,16 @@ class Registrar < ActiveRecord::Base # rubocop:disable Metrics/MethodLength # rubocop:disable Metrics/AbcSize def issue_prepayment_invoice(amount, description = nil) + vat_rate = if local_vat_payer? + Setting.registry_vat_prc + else + if vat_no.blank? + self.vat_rate + else + nil + end + end + invoices.create( invoice_type: 'DEB', due_date: (Time.zone.now.to_date + Setting.days_to_keep_invoices_active.days).end_of_day, @@ -94,6 +115,7 @@ class Registrar < ActiveRecord::Base description: description, currency: 'EUR', vat_rate: Setting.registry_vat_prc, + vat_rate: vat_rate, seller_name: Setting.registry_juridical_name, seller_reg_no: Setting.registry_reg_no, seller_iban: Setting.registry_iban, diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index 04cedc153..bdd8ea033 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -30,3 +30,11 @@ outstanding: overdue: <<: *DEFAULTS due_date: <%= Date.parse '2010-07-03' %> + +with_vat: + <<: *DEFAULTS + vat_rate: 0.2 + +without_vat: + <<: *DEFAULTS + vat_rate: null diff --git a/test/integration/admin/invoices/invoice_details_test.rb b/test/integration/admin/invoices/invoice_details_test.rb new file mode 100644 index 000000000..8a67771ea --- /dev/null +++ b/test/integration/admin/invoices/invoice_details_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' + +class InvoiceDetailsTest < ActionDispatch::IntegrationTest + def setup + login_as users(:admin) + end + + def test_with_vat + invoice = invoices(:with_vat) + visit admin_invoice_path(invoice) + assert_selector '.total', text: 'VAT' + end + + def test_without_vat + invoice = invoices(:without_vat) + visit admin_invoice_path(invoice) + assert_no_selector '.total', text: 'VAT' + end +end diff --git a/test/integration/admin/registrars/show_registrar_test.rb b/test/integration/admin/registrars/show_registrar_test.rb index f13748191..d4ed927bd 100644 --- a/test/integration/admin/registrars/show_registrar_test.rb +++ b/test/integration/admin/registrars/show_registrar_test.rb @@ -17,7 +17,7 @@ class ShowRegistrarTest < ActionDispatch::IntegrationTest assert_text 'Language English' end - def test_vat_number + def test_vat_no assert_text 'US12345' end diff --git a/test/integration/registrar/invoices/invoice_details_test.rb b/test/integration/registrar/invoices/invoice_details_test.rb new file mode 100644 index 000000000..d3961faf3 --- /dev/null +++ b/test/integration/registrar/invoices/invoice_details_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' + +class InvoiceDetailsTest < ActionDispatch::IntegrationTest + def setup + login_as users(:api) + end + + def test_with_vat + invoice = invoices(:with_vat) + visit registrar_invoice_path(invoice) + assert_selector '.total', text: 'VAT' + end + + def test_without_vat + invoice = invoices(:without_vat) + visit registrar_invoice_path(invoice) + assert_no_selector '.total', text: 'VAT' + end +end diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index c89c37626..770a88590 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -1,9 +1,34 @@ require 'test_helper' class InvoiceTest < ActiveSupport::TestCase + def setup + @invoice = invoices(:valid) + end + + def test_valid + assert @invoice.valid? + end + def test_invalid_without_vat_rate - invoice = Invoice.new(vat_rate: nil) - invoice.validate - assert invoice.errors.added?(:vat_rate, :blank) + @invoice.vat_rate = nil + assert @invoice.invalid? + end + + def test_allows_absent_vat_rate + @invoice.vat_rate = nil + @invoice.validate + assert @invoice.valid? + end + + def test_rejects_negative_vat_rate + @invoice.vat_rate = -1 + @invoice.validate + assert @invoice.invalid? + end + + def test_rejects_vat_rate_greater_than_max + @invoice.vat_rate = 100 + @invoice.validate + assert @invoice.invalid? end end diff --git a/test/models/registrar_test.rb b/test/models/registrar_test.rb index 3ace52ba7..96328d38c 100644 --- a/test/models/registrar_test.rb +++ b/test/models/registrar_test.rb @@ -39,7 +39,7 @@ class RegistrarTest < ActiveSupport::TestCase assert_equal 'de', registrar.language end - def test_rejects_vat_number_when_local_vat_payer + def test_rejects_vat_no_when_local_vat_payer Registry.instance.stub(:legal_address_country, Country.new('US')) do @registrar.vat_no = 'US1' @registrar.validate @@ -67,11 +67,6 @@ class RegistrarTest < ActiveSupport::TestCase assert @registrar.invalid? end - def test_converts_integer_vat_rate_to_fractional - @registrar = registrars(:foreign_vat_payer_with_rate) - assert_equal 20, @registrar.vat_rate - end - def test_requires_vat_rate_when_foreign_vat_payer_without_number Registry.instance.stub(:legal_address_country, Country.new('GB')) do @registrar.vat_no = nil @@ -88,4 +83,21 @@ class RegistrarTest < ActiveSupport::TestCase assert @registrar.invalid? end end + + def test_serializes_and_deserializes_vat_rate + valid_attributes = registrars(:valid).attributes.except('id').merge({ name: 'uniq1', + reg_no: 'uniq1', + code: 'uniq1', + country_code: 'GB' }) + registrar = Registrar.new(valid_attributes) + registrar.vat_rate = 55 + registrar.save! + registrar.reload + assert_equal 55, registrar.vat_rate + end + + def test_treats_empty_vat_rate_as_absent + @registrar.vat_rate = '' + assert_nil @registrar.vat_rate + end end From 37cc05e44c3a10c49d710c86a53a7df24cdf4676 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 09:46:17 +0200 Subject: [PATCH 11/63] Remove `registrars.vat` DB column #623 --- db/migrate/20180228074442_remove_registrars_vat.rb | 5 +++++ db/structure.sql | 3 ++- doc/models_complete.svg | 1 - lib/tasks/import.rake | 1 - 4 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20180228074442_remove_registrars_vat.rb diff --git a/db/migrate/20180228074442_remove_registrars_vat.rb b/db/migrate/20180228074442_remove_registrars_vat.rb new file mode 100644 index 000000000..eb2bd44f1 --- /dev/null +++ b/db/migrate/20180228074442_remove_registrars_vat.rb @@ -0,0 +1,5 @@ +class RemoveRegistrarsVat < ActiveRecord::Migration + def change + remove_column :registrars, :vat, :boolean + end +end diff --git a/db/structure.sql b/db/structure.sql index ba040e7cd..46b433aa7 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2432,7 +2432,6 @@ CREATE TABLE registrars ( code character varying, website character varying, accounting_customer_code character varying NOT NULL, - vat boolean, legacy_id integer, reference_no character varying, test_registrar boolean DEFAULT false, @@ -5121,3 +5120,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180228070102'); INSERT INTO schema_migrations (version) VALUES ('20180228070431'); +INSERT INTO schema_migrations (version) VALUES ('20180228074442'); + diff --git a/doc/models_complete.svg b/doc/models_complete.svg index 12fb078a4..3985846b3 100644 --- a/doc/models_complete.svg +++ b/doc/models_complete.svg @@ -1556,7 +1556,6 @@ zip :string code :string url :string -vat :boolean legacy_id :integer reference_no :string diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index c3b67ba88..6c4be1e53 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -88,7 +88,6 @@ namespace :import do zip: x.postalcode.try(:strip), url: x.url.try(:strip), accounting_customer_code: x.directo_handle.try(:strip), - vat: x.vat, legacy_id: x.id, creator_str: user, updator_str: user, From ea305618a57bd503f1378f6a445964a946342d46 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 09:53:53 +0200 Subject: [PATCH 12/63] Add Registry model #623 --- app/models/registrar.rb | 3 +-- app/models/registry.rb | 11 +++++++++++ config/application.rb | 2 +- test/models/registry_test.rb | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 app/models/registry.rb create mode 100644 test/models/registry_test.rb diff --git a/app/models/registrar.rb b/app/models/registrar.rb index dd7a7a9f5..487bc513a 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -99,7 +99,7 @@ class Registrar < ActiveRecord::Base # rubocop:disable Metrics/AbcSize def issue_prepayment_invoice(amount, description = nil) vat_rate = if local_vat_payer? - Setting.registry_vat_prc + Registry.instance.vat_rate else if vat_no.blank? self.vat_rate @@ -114,7 +114,6 @@ class Registrar < ActiveRecord::Base payment_term: 'prepayment', description: description, currency: 'EUR', - vat_rate: Setting.registry_vat_prc, vat_rate: vat_rate, seller_name: Setting.registry_juridical_name, seller_reg_no: Setting.registry_reg_no, diff --git a/app/models/registry.rb b/app/models/registry.rb new file mode 100644 index 000000000..162ab4d71 --- /dev/null +++ b/app/models/registry.rb @@ -0,0 +1,11 @@ +class Registry + include Singleton + + def vat_rate + Setting.registry_vat_prc + end + + def legal_address_country + Country.new(Setting.registry_country_code) + end +end diff --git a/config/application.rb b/config/application.rb index 57106c8ea..04d697f55 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,7 +15,7 @@ require 'rails/all' # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) -module Registry +module DomainNameRegistry class Application < Rails::Application # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers diff --git a/test/models/registry_test.rb b/test/models/registry_test.rb new file mode 100644 index 000000000..2f25686c6 --- /dev/null +++ b/test/models/registry_test.rb @@ -0,0 +1,16 @@ +require 'test_helper' + +class RegistryTest < ActiveSupport::TestCase + def setup + @registry = Registry.send(:new) + end + + def test_implements_singleton + assert_equal Registry.instance.object_id, Registry.instance.object_id + end + + def test_vat_rate + Setting.registry_vat_prc = 0.2 + assert 0.2, @registry.vat_rate + end +end From 6393e4ba6fe8ea5e05abba95c6c8f8f48b626aee Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 10:42:41 +0200 Subject: [PATCH 13/63] Use standard page header #623 --- app/views/admin/registrars/edit.html.erb | 13 +++++--- app/views/admin/registrars/index.html.erb | 16 +++++++--- app/views/admin/registrars/new.html.erb | 9 +++++- app/views/admin/registrars/show.html.erb | 38 ++++++++++++++++------- config/locales/admin/registrars.en.yml | 10 ++++++ 5 files changed, 66 insertions(+), 20 deletions(-) diff --git a/app/views/admin/registrars/edit.html.erb b/app/views/admin/registrars/edit.html.erb index 71d6bd882..205ba6b28 100644 --- a/app/views/admin/registrars/edit.html.erb +++ b/app/views/admin/registrars/edit.html.erb @@ -1,5 +1,10 @@ -<% content_for :actions do %> - <%= link_to(t(:back_to_registrar), [:admin, @registrar], class: 'btn btn-default') %> -<% end %> -<%= render 'shared/title', name: "#{t(:edit)}: #{@registrar.name}" %> + + + + <%= render 'form' %> diff --git a/app/views/admin/registrars/index.html.erb b/app/views/admin/registrars/index.html.erb index 5533a6f07..49bf7eae8 100644 --- a/app/views/admin/registrars/index.html.erb +++ b/app/views/admin/registrars/index.html.erb @@ -1,7 +1,15 @@ -<% content_for :actions do %> - <%= link_to(t('.new_btn'), new_admin_registrar_path, class: 'btn btn-primary') %> -<% end %> -<%= render 'shared/title', name: t(:registrars) %> + +
diff --git a/app/views/admin/registrars/new.html.erb b/app/views/admin/registrars/new.html.erb index 70154d217..e813bdd57 100644 --- a/app/views/admin/registrars/new.html.erb +++ b/app/views/admin/registrars/new.html.erb @@ -1,2 +1,9 @@ -<%= render 'shared/title', name: t(:new_registrar) %> + + + + <%= render 'form' %> diff --git a/app/views/admin/registrars/show.html.erb b/app/views/admin/registrars/show.html.erb index c0a596b27..cff8ded47 100644 --- a/app/views/admin/registrars/show.html.erb +++ b/app/views/admin/registrars/show.html.erb @@ -1,15 +1,31 @@ <% registrar = RegistrarPresenter.new(registrar: @registrar, view: self) %> -<% content_for :actions do %> - <%= link_to(t(:edit), edit_admin_registrar_path(@registrar), class: 'btn btn-primary') %> - <%= link_to(t(:delete), admin_registrar_path(@registrar), method: :delete, data: { confirm: t(:are_you_sure) }, class: 'btn btn-danger') %> -<% end %> -<% content_for :page_name do %> - <%= @registrar.name %> - <% if @registrar.test_registrar? %> - (test) - <% end %> -<% end %> -<%= render 'shared/title', name: @registrar.name %> + + + + + <% if @registrar.errors.any? %> <% @registrar.errors.each do |attr, err| %> <%= err %> diff --git a/config/locales/admin/registrars.en.yml b/config/locales/admin/registrars.en.yml index e805b19c6..ec8d39b63 100644 --- a/config/locales/admin/registrars.en.yml +++ b/config/locales/admin/registrars.en.yml @@ -2,9 +2,16 @@ en: admin: registrars: index: + header: Registrars new_btn: New registrar + new: + header: New registrar + show: + edit_btn: Edit + delete_btn: Delete + delete_btn_confirm: Are you sure you want delete registrar? new_api_use_btn: New API user active: Active api_users: API users @@ -12,6 +19,9 @@ en: preferences: header: Preferences + edit: + header: Edit registrar + billing: header: Billing From 64325d7535a5b272c31d49eb3f4b1627c3556c75 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 10:43:15 +0200 Subject: [PATCH 14/63] Remove inline CSS #623 --- app/views/admin/registrars/show.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/admin/registrars/show.html.erb b/app/views/admin/registrars/show.html.erb index cff8ded47..ef75e4a64 100644 --- a/app/views/admin/registrars/show.html.erb +++ b/app/views/admin/registrars/show.html.erb @@ -10,7 +10,7 @@

<%= @registrar.name %> <% if @registrar.test_registrar? %> - (test) + (test) <% end %>

From 8dcac194e27c1431109a2842f122737a951f39a4 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 10:43:30 +0200 Subject: [PATCH 15/63] Remove unused translations #623 --- config/locales/en.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index e9b8316d6..f92c525ce 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -314,9 +314,6 @@ en: edit_statuses: 'Edit statuses' history: 'History' - new_registrar: 'New registrar' - registrar_details: 'Registrar details' - edit_registrar: 'Edit registrar' back_to_registrar: 'Back to registrar' registrar_deleted: 'Registrar deleted' failed_to_delete_registrar: 'Failed to delete registrar' From 70fb91c812196dc46a01bcf5f54e4a3698d3a6cf Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 10:44:59 +0200 Subject: [PATCH 16/63] Add HTML validation #623 --- app/views/admin/registrars/form/_billing.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/admin/registrars/form/_billing.html.erb b/app/views/admin/registrars/form/_billing.html.erb index 5bc87a93d..ff67232de 100644 --- a/app/views/admin/registrars/form/_billing.html.erb +++ b/app/views/admin/registrars/form/_billing.html.erb @@ -23,7 +23,7 @@
- <%= f.number_field :vat_rate, class: 'form-control' %> + <%= f.number_field :vat_rate, min: 0, max: 99, class: 'form-control' %>
%
From f449f1c107746018041c8d25b9e12dfbe89cf062 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 10:49:20 +0200 Subject: [PATCH 17/63] Change `registrars.country_code` DB column to NOT NULL #623 --- ...80228084818_change_registrars_country_code_to_not_null.rb | 5 +++++ db/structure.sql | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180228084818_change_registrars_country_code_to_not_null.rb diff --git a/db/migrate/20180228084818_change_registrars_country_code_to_not_null.rb b/db/migrate/20180228084818_change_registrars_country_code_to_not_null.rb new file mode 100644 index 000000000..16a66e989 --- /dev/null +++ b/db/migrate/20180228084818_change_registrars_country_code_to_not_null.rb @@ -0,0 +1,5 @@ +class ChangeRegistrarsCountryCodeToNotNull < ActiveRecord::Migration + def change + change_column_null :registrars, :country_code, false + end +end diff --git a/db/structure.sql b/db/structure.sql index 46b433aa7..00eac536d 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2424,7 +2424,7 @@ CREATE TABLE registrars ( phone character varying, email character varying, billing_email character varying, - country_code character varying, + country_code character varying NOT NULL, state character varying, city character varying, street character varying, @@ -5122,3 +5122,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180228070431'); INSERT INTO schema_migrations (version) VALUES ('20180228074442'); +INSERT INTO schema_migrations (version) VALUES ('20180228084818'); + From 9e4fd48c70225b2229ccdba596f3a32b44b268a6 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 11:10:08 +0200 Subject: [PATCH 18/63] Fix style #623 --- app/controllers/admin/registrars_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index eb9ae9bd7..df2a43e1b 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -80,7 +80,7 @@ module Admin :test_registrar, :accounting_customer_code, :language, - :vat_rate,) + :vat_rate) end end end From b2a38ae4e9e5decd9a215bd36a3be694f5044317 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 11:36:49 +0200 Subject: [PATCH 19/63] Re-order arguments #623 --- app/controllers/admin/registrars_controller.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index df2a43e1b..34eb49598 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -65,7 +65,6 @@ module Admin def registrar_params params.require(:registrar).permit(:name, :reg_no, - :vat_no, :street, :city, :state, @@ -78,9 +77,11 @@ module Admin :billing_email, :code, :test_registrar, + :vat_no, + :vat_rate, :accounting_customer_code, - :language, - :vat_rate) + :billing_email, + :language) end end end From b81263ee3d54c0f2e356a34b1461c033a19fc5b6 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 11:48:19 +0200 Subject: [PATCH 20/63] Remove unused fixture #623 --- test/fixtures/registrars.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/fixtures/registrars.yml b/test/fixtures/registrars.yml index b5715a0a5..0026f323b 100644 --- a/test/fixtures/registrars.yml +++ b/test/fixtures/registrars.yml @@ -16,10 +16,3 @@ goodnames: country_code: US accounting_customer_code: goodnames language: en - -foreign_vat_payer_with_rate: - <<: *DEFAULTS - name: 3 - reg_no: 3 - code: 3 - vat_rate: 0.2 From e4b730caf715235e5536f59492bf5d88b7f4914d Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 11:50:52 +0200 Subject: [PATCH 21/63] Fix invalid fixture #623 --- test/models/registrar_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/models/registrar_test.rb b/test/models/registrar_test.rb index 96328d38c..5d0894e0e 100644 --- a/test/models/registrar_test.rb +++ b/test/models/registrar_test.rb @@ -85,7 +85,7 @@ class RegistrarTest < ActiveSupport::TestCase end def test_serializes_and_deserializes_vat_rate - valid_attributes = registrars(:valid).attributes.except('id').merge({ name: 'uniq1', + valid_attributes = registrars(:bestnames).attributes.except('id').merge({ name: 'uniq1', reg_no: 'uniq1', code: 'uniq1', country_code: 'GB' }) From 07d58d5b9a5fd6dc49872c466e343c633258cb8e Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 28 Feb 2018 11:52:20 +0200 Subject: [PATCH 22/63] Convert spec to test #623 --- spec/models/registrar_spec.rb | 1 - test/models/registrar_test.rb | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/models/registrar_spec.rb b/spec/models/registrar_spec.rb index 4a8110d45..301d59bcb 100644 --- a/spec/models/registrar_spec.rb +++ b/spec/models/registrar_spec.rb @@ -10,7 +10,6 @@ describe Registrar do @registrar.valid? @registrar.errors.full_messages.should include(*[ 'Contact e-mail is missing', - 'Country code is missing', 'Name is missing', 'Reg no is missing', 'Code is missing' diff --git a/test/models/registrar_test.rb b/test/models/registrar_test.rb index 5d0894e0e..057c34f62 100644 --- a/test/models/registrar_test.rb +++ b/test/models/registrar_test.rb @@ -15,9 +15,8 @@ class RegistrarTest < ActiveSupport::TestCase assert @registrar.invalid? end - def test_requires_country_code + def test_invalid_without_country_code @registrar.country_code = nil - @registrar.validate assert @registrar.invalid? end From d53a7f9c47587e5592483aea3a9b1d9b78cb998d Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 8 Mar 2018 14:22:28 +0200 Subject: [PATCH 23/63] Revert "Extract partial" This reverts commit 955dc10 --- app/views/admin/registrars/_billing.html.erb | 20 -------------------- app/views/admin/registrars/show.html.erb | 19 ++++++++++++++++++- config/locales/admin/registrars.en.yml | 3 --- config/locales/en.yml | 2 +- 4 files changed, 19 insertions(+), 25 deletions(-) delete mode 100644 app/views/admin/registrars/_billing.html.erb diff --git a/app/views/admin/registrars/_billing.html.erb b/app/views/admin/registrars/_billing.html.erb deleted file mode 100644 index 7ba778d08..000000000 --- a/app/views/admin/registrars/_billing.html.erb +++ /dev/null @@ -1,20 +0,0 @@ -
-
- <%= t '.header' %> -
-
-
-
<%= Registrar.human_attribute_name :vat_no %>
-
<%= registrar.vat_no %>
- -
<%= Registrar.human_attribute_name :vat_rate %>
-
<%= number_to_percentage registrar.vat_rate, precision: 1 %>
- -
<%= Registrar.human_attribute_name :accounting_customer_code %>
-
<%= registrar.accounting_customer_code %>
- -
<%= Registrar.human_attribute_name :billing_email %>
-
<%= registrar.billing_email %>
-
-
-
diff --git a/app/views/admin/registrars/show.html.erb b/app/views/admin/registrars/show.html.erb index ef75e4a64..2b1515fa0 100644 --- a/app/views/admin/registrars/show.html.erb +++ b/app/views/admin/registrars/show.html.erb @@ -57,6 +57,12 @@
<%= @registrar.reg_no %>
+
+ <%= t(:vat_no) %> +
+
+ <%= @registrar.vat_no %> +
<%= t(:reference_no) %>
@@ -81,6 +87,12 @@
<%= @registrar.website %>
+
+ <%= Registrar.human_attribute_name :accounting_customer_code %> +
+
+ <%= @registrar.accounting_customer_code %> +
@@ -124,10 +136,15 @@
<%= @registrar.billing_address %>
+
+ <%= t(:billing_email) %> +
+
+ <%= @registrar.billing_email %> +
- <%= render 'billing', registrar: @registrar %> <%= render 'admin/registrars/show/preferences', registrar: registrar %>
diff --git a/config/locales/admin/registrars.en.yml b/config/locales/admin/registrars.en.yml index ec8d39b63..249a3b70a 100644 --- a/config/locales/admin/registrars.en.yml +++ b/config/locales/admin/registrars.en.yml @@ -22,9 +22,6 @@ en: edit: header: Edit registrar - billing: - header: Billing - create: created: Registrar has been successfully created not_created: Unable to create registrar diff --git a/config/locales/en.yml b/config/locales/en.yml index 4845d7309..65c3d658d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -199,6 +199,7 @@ en: alg: 'Algorithm' public_key: 'Public key' registrar: + billing_email: 'Billing e-mail' phone: 'Contact phone' email: 'Contact e-mail' state: 'State / Province' @@ -777,5 +778,4 @@ en: unit: € attributes: - vat_no: VAT number vat_rate: VAT rate From 6949b684eb524640e193e9b84458b64f4b0b3a0b Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 8 Mar 2018 14:22:39 +0200 Subject: [PATCH 24/63] Revert "Remove unused HTML" This reverts commit d2e84d2 --- app/views/admin/registrars/show.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/admin/registrars/show.html.erb b/app/views/admin/registrars/show.html.erb index 2b1515fa0..755143ff4 100644 --- a/app/views/admin/registrars/show.html.erb +++ b/app/views/admin/registrars/show.html.erb @@ -150,7 +150,7 @@
-
+
<%= t('.api_users') %> @@ -190,7 +190,7 @@
-
+
<%= t(:white_ips) %> From fcc89769cf3fbdcf8c949e8fd39eac094d629073 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 8 Mar 2018 14:23:26 +0200 Subject: [PATCH 25/63] Revert "Use human_attribute_name" This reverts commit 4a41984 --- app/views/registrant/registrars/show.haml | 2 +- app/views/registrar/invoices/partials/_seller.haml | 2 +- app/views/registrar/invoices/pdf.haml | 2 +- config/locales/en.yml | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/views/registrant/registrars/show.haml b/app/views/registrant/registrars/show.haml index 6d4aa9e79..e0da73ee3 100644 --- a/app/views/registrant/registrars/show.haml +++ b/app/views/registrant/registrars/show.haml @@ -19,7 +19,7 @@ %dt= t(:reg_no) %dd= @registrar.reg_no - %dt= Registrar.human_attribute_name :vat_no + %dt= t(:vat_no) %dd= @registrar.vat_no %dt= t(:id) diff --git a/app/views/registrar/invoices/partials/_seller.haml b/app/views/registrar/invoices/partials/_seller.haml index 9c5387e16..0008c2f13 100644 --- a/app/views/registrar/invoices/partials/_seller.haml +++ b/app/views/registrar/invoices/partials/_seller.haml @@ -16,7 +16,7 @@ %dt= t(:swift) %dd= @invoice.seller_swift - %dt= Registrar.human_attribute_name :vat_no + %dt= t(:vat_no) %dd= @invoice.seller_vat_no %dt= t(:address) diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index 6f92c4a8a..1622613f5 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -261,7 +261,7 @@ %br = "#{t('reg_no')} #{@invoice.seller_reg_no}" %br - = "#{Registrar.human_attribute_name :vat_no} #{@invoice.seller_vat_no}" + = "#{t('vat_no')} #{@invoice.seller_vat_no}" .col-md-3.left = @invoice.seller_phone diff --git a/config/locales/en.yml b/config/locales/en.yml index 65c3d658d..099a9fbbf 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -315,6 +315,7 @@ en: edit_statuses: 'Edit statuses' history: 'History' + vat_no: 'VAT no' back_to_registrar: 'Back to registrar' registrar_deleted: 'Registrar deleted' failed_to_delete_registrar: 'Failed to delete registrar' From 6406e6116e421334a59f0fd4a5ed4516e12cf9f6 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 8 Mar 2018 14:25:54 +0200 Subject: [PATCH 26/63] Revert "Use standard page header" This reverts commit 6393e4b --- app/views/admin/registrars/edit.html.erb | 13 +++----- app/views/admin/registrars/index.html.erb | 16 +++------- app/views/admin/registrars/new.html.erb | 9 +----- app/views/admin/registrars/show.html.erb | 38 +++++++---------------- config/locales/admin/registrars.en.yml | 10 ------ 5 files changed, 20 insertions(+), 66 deletions(-) diff --git a/app/views/admin/registrars/edit.html.erb b/app/views/admin/registrars/edit.html.erb index 205ba6b28..71d6bd882 100644 --- a/app/views/admin/registrars/edit.html.erb +++ b/app/views/admin/registrars/edit.html.erb @@ -1,10 +1,5 @@ - - - - +<% content_for :actions do %> + <%= link_to(t(:back_to_registrar), [:admin, @registrar], class: 'btn btn-default') %> +<% end %> +<%= render 'shared/title', name: "#{t(:edit)}: #{@registrar.name}" %> <%= render 'form' %> diff --git a/app/views/admin/registrars/index.html.erb b/app/views/admin/registrars/index.html.erb index 49bf7eae8..5533a6f07 100644 --- a/app/views/admin/registrars/index.html.erb +++ b/app/views/admin/registrars/index.html.erb @@ -1,15 +1,7 @@ - - +<% content_for :actions do %> + <%= link_to(t('.new_btn'), new_admin_registrar_path, class: 'btn btn-primary') %> +<% end %> +<%= render 'shared/title', name: t(:registrars) %>
diff --git a/app/views/admin/registrars/new.html.erb b/app/views/admin/registrars/new.html.erb index e813bdd57..70154d217 100644 --- a/app/views/admin/registrars/new.html.erb +++ b/app/views/admin/registrars/new.html.erb @@ -1,9 +1,2 @@ - - - - +<%= render 'shared/title', name: t(:new_registrar) %> <%= render 'form' %> diff --git a/app/views/admin/registrars/show.html.erb b/app/views/admin/registrars/show.html.erb index 755143ff4..c09602985 100644 --- a/app/views/admin/registrars/show.html.erb +++ b/app/views/admin/registrars/show.html.erb @@ -1,31 +1,15 @@ <% registrar = RegistrarPresenter.new(registrar: @registrar, view: self) %> - - - - - +<% content_for :actions do %> + <%= link_to(t(:edit), edit_admin_registrar_path(@registrar), class: 'btn btn-primary') %> + <%= link_to(t(:delete), admin_registrar_path(@registrar), method: :delete, data: { confirm: t(:are_you_sure) }, class: 'btn btn-danger') %> +<% end %> +<% content_for :page_name do %> + <%= @registrar.name %> + <% if @registrar.test_registrar? %> + (test) + <% end %> +<% end %> +<%= render 'shared/title', name: @registrar.name %> <% if @registrar.errors.any? %> <% @registrar.errors.each do |attr, err| %> <%= err %> diff --git a/config/locales/admin/registrars.en.yml b/config/locales/admin/registrars.en.yml index 249a3b70a..3d269f64b 100644 --- a/config/locales/admin/registrars.en.yml +++ b/config/locales/admin/registrars.en.yml @@ -2,16 +2,9 @@ en: admin: registrars: index: - header: Registrars new_btn: New registrar - new: - header: New registrar - show: - edit_btn: Edit - delete_btn: Delete - delete_btn_confirm: Are you sure you want delete registrar? new_api_use_btn: New API user active: Active api_users: API users @@ -19,9 +12,6 @@ en: preferences: header: Preferences - edit: - header: Edit registrar - create: created: Registrar has been successfully created not_created: Unable to create registrar From bdeb266d279ba3021f725c92c3d81dd1d06d467f Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 8 Mar 2018 14:28:21 +0200 Subject: [PATCH 27/63] Revert "Remove unused translations" This reverts commit 8dcac19 --- config/locales/en.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/locales/en.yml b/config/locales/en.yml index 099a9fbbf..6feb4c353 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -315,7 +315,10 @@ en: edit_statuses: 'Edit statuses' history: 'History' + new_registrar: 'New registrar' + registrar_details: 'Registrar details' vat_no: 'VAT no' + edit_registrar: 'Edit registrar' back_to_registrar: 'Back to registrar' registrar_deleted: 'Registrar deleted' failed_to_delete_registrar: 'Failed to delete registrar' From 810c34ffaf020f01400de7ce157ff255a070e889 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Fri, 9 Mar 2018 08:24:24 +0200 Subject: [PATCH 28/63] Revert "Change `registrars.country_code` DB column to NOT NULL" This reverts commit f449f1c --- ...80228084818_change_registrars_country_code_to_not_null.rb | 5 ----- db/structure.sql | 4 +--- 2 files changed, 1 insertion(+), 8 deletions(-) delete mode 100644 db/migrate/20180228084818_change_registrars_country_code_to_not_null.rb diff --git a/db/migrate/20180228084818_change_registrars_country_code_to_not_null.rb b/db/migrate/20180228084818_change_registrars_country_code_to_not_null.rb deleted file mode 100644 index 16a66e989..000000000 --- a/db/migrate/20180228084818_change_registrars_country_code_to_not_null.rb +++ /dev/null @@ -1,5 +0,0 @@ -class ChangeRegistrarsCountryCodeToNotNull < ActiveRecord::Migration - def change - change_column_null :registrars, :country_code, false - end -end diff --git a/db/structure.sql b/db/structure.sql index 15f92fe2b..fbbb4a7d0 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2147,7 +2147,7 @@ CREATE TABLE registrars ( phone character varying, email character varying, billing_email character varying, - country_code character varying NOT NULL, + country_code character varying, state character varying, city character varying, street character varying, @@ -4670,8 +4670,6 @@ INSERT INTO schema_migrations (version) VALUES ('20180228070431'); INSERT INTO schema_migrations (version) VALUES ('20180228074442'); -INSERT INTO schema_migrations (version) VALUES ('20180228084818'); - INSERT INTO schema_migrations (version) VALUES ('20180306180401'); INSERT INTO schema_migrations (version) VALUES ('20180306181538'); From a5869e0ecad1927b47d4e12188cc57508870029e Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 20 Nov 2017 08:10:59 +0200 Subject: [PATCH 29/63] Refactor registrars #765 --- .../admin/registrars_controller.rb | 25 ++-- app/models/registrar.rb | 56 ++++----- app/views/admin/registrars/_billing.html.erb | 17 +++ app/views/admin/registrars/_form.html.erb | 7 +- app/views/admin/registrars/edit.html.erb | 13 +- app/views/admin/registrars/index.html.erb | 16 ++- app/views/admin/registrars/new.html.erb | 9 +- app/views/admin/registrars/show.html.erb | 61 +++++---- app/views/registrant/registrars/show.haml | 2 +- .../registrar/invoices/partials/_seller.haml | 2 +- app/views/registrar/invoices/pdf.haml | 2 +- config/locales/admin/registrars.en.yml | 18 ++- config/locales/en.yml | 11 +- config/locales/registrars.en.yml | 8 ++ ...53424_add_registrars_unique_constraints.rb | 19 +++ ...0180309053921_remove_registrars_indexes.rb | 6 + ...510_add_registrars_not_null_constraints.rb | 9 ++ db/structure.sql | 62 ++++++--- lib/tasks/import.rake | 2 +- spec/models/registrar_spec.rb | 118 ------------------ spec/requests/admin/registrars/update_spec.rb | 42 ------- spec/routing/admin/registrars_routing_spec.rb | 33 ----- .../admin/registrars/_form.html.erb_spec.rb | 19 --- spec/views/admin/registrars/show.haml_spec.rb | 19 --- .../admin/registrars/update_test.rb | 30 ----- test/fixtures/accounts.yml | 6 + test/fixtures/registrars.yml | 13 ++ .../admin/registrars/delete_test.rb | 18 +++ .../admin/registrars/edit_registrar_test.rb | 17 --- .../integration/admin/registrars/edit_test.rb | 69 ++++++++++ .../admin/registrars/new_registrar_test.rb | 23 ---- test/integration/admin/registrars/new_test.rb | 49 ++++++++ test/models/registrar/code_test.rb | 29 +++++ test/models/registrar/delete_test.rb | 37 ++++++ test/models/registrar_test.rb | 31 ++++- 35 files changed, 471 insertions(+), 427 deletions(-) create mode 100644 app/views/admin/registrars/_billing.html.erb create mode 100644 config/locales/registrars.en.yml create mode 100644 db/migrate/20180309053424_add_registrars_unique_constraints.rb create mode 100644 db/migrate/20180309053921_remove_registrars_indexes.rb create mode 100644 db/migrate/20180309054510_add_registrars_not_null_constraints.rb delete mode 100644 spec/models/registrar_spec.rb delete mode 100644 spec/requests/admin/registrars/update_spec.rb delete mode 100644 spec/routing/admin/registrars_routing_spec.rb delete mode 100644 spec/views/admin/registrars/_form.html.erb_spec.rb delete mode 100644 spec/views/admin/registrars/show.haml_spec.rb delete mode 100644 test/controllers/admin/registrars/update_test.rb create mode 100644 test/integration/admin/registrars/delete_test.rb delete mode 100644 test/integration/admin/registrars/edit_registrar_test.rb create mode 100644 test/integration/admin/registrars/edit_test.rb delete mode 100644 test/integration/admin/registrars/new_registrar_test.rb create mode 100644 test/integration/admin/registrars/new_test.rb create mode 100644 test/models/registrar/code_test.rb create mode 100644 test/models/registrar/delete_test.rb diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index 119abf85b..dd31e1c3d 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -19,17 +19,15 @@ module Admin def create @registrar = Registrar.new(registrar_params) - begin + if @registrar.valid? @registrar.transaction do @registrar.save! @registrar.accounts.create!(account_type: Account::CASH, currency: 'EUR') end - flash[:notice] = t('.created') - redirect_to [:admin, @registrar] - rescue ActiveRecord::RecordInvalid - flash.now[:alert] = t('.not_created') - render 'new' + redirect_to [:admin, @registrar], notice: t('.created') + else + render :new end end @@ -38,22 +36,15 @@ module Admin def update if @registrar.update(registrar_params) - flash[:notice] = t('.updated') - redirect_to [:admin, @registrar] + redirect_to [:admin, @registrar], notice: t('.updated') else - flash.now[:alert] = t('.not_updated') - render 'edit' + render :edit end end def destroy - if @registrar.destroy - flash[:notice] = I18n.t('registrar_deleted') - redirect_to admin_registrars_path - else - flash.now[:alert] = I18n.t('failed_to_delete_registrar') - render 'show' - end + @registrar.destroy! + redirect_to admin_registrars_url, notice: t('.deleted') end private diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 6a10983d2..de030b81b 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -6,7 +6,7 @@ class Registrar < ActiveRecord::Base has_many :api_users, dependent: :restrict_with_error has_many :messages has_many :invoices, foreign_key: 'buyer_id' - has_many :accounts + has_many :accounts, dependent: :destroy has_many :nameservers, through: :domains has_many :whois_records has_many :white_ips, dependent: :destroy @@ -17,37 +17,10 @@ class Registrar < ActiveRecord::Base validates :name, :reg_no, :reference_no, :code, uniqueness: true validates :accounting_customer_code, presence: true validates :language, presence: true - validate :forbidden_codes + validate :forbid_special_code after_initialize :set_defaults - - def forbidden_codes - return true unless ['CID'].include? code - errors.add(:code, I18n.t(:forbidden_code)) - false - end - before_validation :generate_iso_11649_reference_no - def generate_iso_11649_reference_no - return if reference_no.present? - - loop do - base = nil - loop do - base = SecureRandom.random_number.to_s.last(8) - break if base.to_i != 0 && base.length == 8 - end - - control_base = (base + '2715' + '00').to_i - reminder = control_base % 97 - check_digits = 98 - reminder - - check_digits = check_digits < 10 ? "0#{check_digits}" : check_digits.to_s - - self.reference_no = "RF#{check_digits}#{base}" - break unless self.class.exists?(reference_no: reference_no) - end - end validates :email, :billing_email, email_format: { message: :invalid }, @@ -177,4 +150,29 @@ class Registrar < ActiveRecord::Base def set_defaults self.language = Setting.default_language unless language end + + def forbid_special_code + errors.add(:code, :forbidden) if code == 'CID' + end + + def generate_iso_11649_reference_no + return if reference_no.present? + + loop do + base = nil + loop do + base = SecureRandom.random_number.to_s.last(8) + break if base.to_i != 0 && base.length == 8 + end + + control_base = (base + '2715' + '00').to_i + reminder = control_base % 97 + check_digits = 98 - reminder + + check_digits = check_digits < 10 ? "0#{check_digits}" : check_digits.to_s + + self.reference_no = "RF#{check_digits}#{base}" + break unless self.class.exists?(reference_no: reference_no) + end + end end diff --git a/app/views/admin/registrars/_billing.html.erb b/app/views/admin/registrars/_billing.html.erb new file mode 100644 index 000000000..f3960233e --- /dev/null +++ b/app/views/admin/registrars/_billing.html.erb @@ -0,0 +1,17 @@ +
+
+ <%= t '.header' %> +
+
+
+
<%= Registrar.human_attribute_name :vat_no %>
+
<%= registrar.vat_no %>
+ +
<%= Registrar.human_attribute_name :accounting_customer_code %>
+
<%= registrar.accounting_customer_code %>
+ +
<%= Registrar.human_attribute_name :billing_email %>
+
<%= registrar.billing_email %>
+
+
+
diff --git a/app/views/admin/registrars/_form.html.erb b/app/views/admin/registrars/_form.html.erb index 65a5351ad..71c4fb68f 100644 --- a/app/views/admin/registrars/_form.html.erb +++ b/app/views/admin/registrars/_form.html.erb @@ -1,5 +1,6 @@ <%= form_for([:admin, @registrar], html: { class: 'form-horizontal' }) do |f| %> - <%= render 'shared/full_errors', object: @registrar %> + <%= render 'form_errors', target: @registrar %> +
@@ -133,7 +134,9 @@ <%= f.label :code %>
- <%= f.text_field :code, required: f.object.new_record?, class: 'form-control', disabled: !f.object.new_record? %> + <%= f.text_field :code, required: f.object.new_record?, + disabled: f.object.persisted?, + class: 'form-control' %>
diff --git a/app/views/admin/registrars/edit.html.erb b/app/views/admin/registrars/edit.html.erb index 71d6bd882..205ba6b28 100644 --- a/app/views/admin/registrars/edit.html.erb +++ b/app/views/admin/registrars/edit.html.erb @@ -1,5 +1,10 @@ -<% content_for :actions do %> - <%= link_to(t(:back_to_registrar), [:admin, @registrar], class: 'btn btn-default') %> -<% end %> -<%= render 'shared/title', name: "#{t(:edit)}: #{@registrar.name}" %> + + + + <%= render 'form' %> diff --git a/app/views/admin/registrars/index.html.erb b/app/views/admin/registrars/index.html.erb index 5533a6f07..49bf7eae8 100644 --- a/app/views/admin/registrars/index.html.erb +++ b/app/views/admin/registrars/index.html.erb @@ -1,7 +1,15 @@ -<% content_for :actions do %> - <%= link_to(t('.new_btn'), new_admin_registrar_path, class: 'btn btn-primary') %> -<% end %> -<%= render 'shared/title', name: t(:registrars) %> + +
diff --git a/app/views/admin/registrars/new.html.erb b/app/views/admin/registrars/new.html.erb index 70154d217..e813bdd57 100644 --- a/app/views/admin/registrars/new.html.erb +++ b/app/views/admin/registrars/new.html.erb @@ -1,2 +1,9 @@ -<%= render 'shared/title', name: t(:new_registrar) %> + + + + <%= render 'form' %> diff --git a/app/views/admin/registrars/show.html.erb b/app/views/admin/registrars/show.html.erb index c09602985..ef75e4a64 100644 --- a/app/views/admin/registrars/show.html.erb +++ b/app/views/admin/registrars/show.html.erb @@ -1,15 +1,31 @@ <% registrar = RegistrarPresenter.new(registrar: @registrar, view: self) %> -<% content_for :actions do %> - <%= link_to(t(:edit), edit_admin_registrar_path(@registrar), class: 'btn btn-primary') %> - <%= link_to(t(:delete), admin_registrar_path(@registrar), method: :delete, data: { confirm: t(:are_you_sure) }, class: 'btn btn-danger') %> -<% end %> -<% content_for :page_name do %> - <%= @registrar.name %> - <% if @registrar.test_registrar? %> - (test) - <% end %> -<% end %> -<%= render 'shared/title', name: @registrar.name %> + + + + + <% if @registrar.errors.any? %> <% @registrar.errors.each do |attr, err| %> <%= err %> @@ -41,12 +57,6 @@
<%= @registrar.reg_no %>
-
- <%= t(:vat_no) %> -
-
- <%= @registrar.vat_no %> -
<%= t(:reference_no) %>
@@ -71,12 +81,6 @@
<%= @registrar.website %>
-
- <%= Registrar.human_attribute_name :accounting_customer_code %> -
-
- <%= @registrar.accounting_customer_code %> -
@@ -120,21 +124,16 @@
<%= @registrar.billing_address %>
-
- <%= t(:billing_email) %> -
-
- <%= @registrar.billing_email %> -
+ <%= render 'billing', registrar: @registrar %> <%= render 'admin/registrars/show/preferences', registrar: registrar %>
-
+
<%= t('.api_users') %> @@ -174,7 +173,7 @@
-
+
<%= t(:white_ips) %> diff --git a/app/views/registrant/registrars/show.haml b/app/views/registrant/registrars/show.haml index e0da73ee3..6d4aa9e79 100644 --- a/app/views/registrant/registrars/show.haml +++ b/app/views/registrant/registrars/show.haml @@ -19,7 +19,7 @@ %dt= t(:reg_no) %dd= @registrar.reg_no - %dt= t(:vat_no) + %dt= Registrar.human_attribute_name :vat_no %dd= @registrar.vat_no %dt= t(:id) diff --git a/app/views/registrar/invoices/partials/_seller.haml b/app/views/registrar/invoices/partials/_seller.haml index 0008c2f13..9c5387e16 100644 --- a/app/views/registrar/invoices/partials/_seller.haml +++ b/app/views/registrar/invoices/partials/_seller.haml @@ -16,7 +16,7 @@ %dt= t(:swift) %dd= @invoice.seller_swift - %dt= t(:vat_no) + %dt= Registrar.human_attribute_name :vat_no %dd= @invoice.seller_vat_no %dt= t(:address) diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index a74254a2a..61b4e5789 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -261,7 +261,7 @@ %br = "#{t('reg_no')} #{@invoice.seller_reg_no}" %br - = "#{t('vat_no')} #{@invoice.seller_vat_no}" + = "#{Registrar.human_attribute_name :vat_no} #{@invoice.seller_vat_no}" .col-md-3.left = @invoice.seller_phone diff --git a/config/locales/admin/registrars.en.yml b/config/locales/admin/registrars.en.yml index 3d269f64b..5d212f219 100644 --- a/config/locales/admin/registrars.en.yml +++ b/config/locales/admin/registrars.en.yml @@ -2,9 +2,16 @@ en: admin: registrars: index: + header: Registrars new_btn: New registrar + new: + header: New registrar + show: + edit_btn: Edit + delete_btn: Delete + delete_btn_confirm: Are you sure you want delete registrar? new_api_use_btn: New API user active: Active api_users: API users @@ -12,13 +19,20 @@ en: preferences: header: Preferences + edit: + header: Edit registrar + + billing: + header: Billing + create: created: Registrar has been successfully created - not_created: Unable to create registrar update: updated: Registrar has been successfully updated - not_updated: Unable to update registrar + + destroy: + deleted: Registrar has been successfully deleted form: misc: Miscellaneous diff --git a/config/locales/en.yml b/config/locales/en.yml index 00923faf2..a093243b2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -199,7 +199,6 @@ en: alg: 'Algorithm' public_key: 'Public key' registrar: - billing_email: 'Billing e-mail' phone: 'Contact phone' email: 'Contact e-mail' state: 'State / Province' @@ -315,13 +314,7 @@ en: edit_statuses: 'Edit statuses' history: 'History' - new_registrar: 'New registrar' - registrar_details: 'Registrar details' - vat_no: 'VAT no' - edit_registrar: 'Edit registrar' back_to_registrar: 'Back to registrar' - registrar_deleted: 'Registrar deleted' - failed_to_delete_registrar: 'Failed to delete registrar' users: 'Users' user_details: 'User details' @@ -652,7 +645,6 @@ en: due_date_until: 'Due date until' minimum_total: 'Minimum total' maximum_total: 'Maximum total' - forbidden_code: 'is forbidden to use' unimplemented_object_service: 'Unimplemented object service' contact_email_update_subject: 'Teie domeenide kontakt epostiaadress on muutunud / Contact e-mail addresses of your domains have changed' object_status_prohibits_operation: 'Object status prohibits operation' @@ -780,3 +772,6 @@ en: delimiter: " " precision: 2 unit: € + + attributes: + vat_no: VAT number diff --git a/config/locales/registrars.en.yml b/config/locales/registrars.en.yml new file mode 100644 index 000000000..60cb9d5c5 --- /dev/null +++ b/config/locales/registrars.en.yml @@ -0,0 +1,8 @@ +en: + activerecord: + errors: + models: + registrar: + attributes: + code: + forbidden: is forbidden diff --git a/db/migrate/20180309053424_add_registrars_unique_constraints.rb b/db/migrate/20180309053424_add_registrars_unique_constraints.rb new file mode 100644 index 000000000..1c2d3c1b8 --- /dev/null +++ b/db/migrate/20180309053424_add_registrars_unique_constraints.rb @@ -0,0 +1,19 @@ +class AddRegistrarsUniqueConstraints < ActiveRecord::Migration + def up + execute <<-SQL + ALTER TABLE registrars ADD CONSTRAINT unique_name UNIQUE (name); + ALTER TABLE registrars ADD CONSTRAINT unique_reg_no UNIQUE (reg_no); + ALTER TABLE registrars ADD CONSTRAINT unique_reference_no UNIQUE (reference_no); + ALTER TABLE registrars ADD CONSTRAINT unique_code UNIQUE (code); + SQL + end + + def down + execute <<-SQL + ALTER TABLE registrars DROP CONSTRAINT unique_name; + ALTER TABLE registrars DROP CONSTRAINT unique_reg_no; + ALTER TABLE registrars DROP CONSTRAINT unique_reference_no; + ALTER TABLE registrars DROP CONSTRAINT unique_code; + SQL + end +end diff --git a/db/migrate/20180309053921_remove_registrars_indexes.rb b/db/migrate/20180309053921_remove_registrars_indexes.rb new file mode 100644 index 000000000..0613d9fc6 --- /dev/null +++ b/db/migrate/20180309053921_remove_registrars_indexes.rb @@ -0,0 +1,6 @@ +class RemoveRegistrarsIndexes < ActiveRecord::Migration + def change + remove_index :registrars, name: :index_registrars_on_code + remove_index :registrars, name: :index_registrars_on_legacy_id + end +end diff --git a/db/migrate/20180309054510_add_registrars_not_null_constraints.rb b/db/migrate/20180309054510_add_registrars_not_null_constraints.rb new file mode 100644 index 000000000..33d464356 --- /dev/null +++ b/db/migrate/20180309054510_add_registrars_not_null_constraints.rb @@ -0,0 +1,9 @@ +class AddRegistrarsNotNullConstraints < ActiveRecord::Migration + def change + change_column_null :registrars, :name, false + change_column_null :registrars, :reg_no, false + change_column_null :registrars, :country_code, false + change_column_null :registrars, :email, false + change_column_null :registrars, :code, false + end +end diff --git a/db/structure.sql b/db/structure.sql index b303afbe7..194bc8111 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2136,8 +2136,8 @@ ALTER SEQUENCE registrant_verifications_id_seq OWNED BY registrant_verifications CREATE TABLE registrars ( id integer NOT NULL, - name character varying, - reg_no character varying, + name character varying NOT NULL, + reg_no character varying NOT NULL, vat_no character varying, billing_address character varying, created_at timestamp without time zone, @@ -2145,14 +2145,14 @@ CREATE TABLE registrars ( creator_str character varying, updator_str character varying, phone character varying, - email character varying, + email character varying NOT NULL, billing_email character varying, - country_code character varying, + country_code character varying NOT NULL, state character varying, city character varying, street character varying, zip character varying, - code character varying, + code character varying NOT NULL, website character varying, accounting_customer_code character varying NOT NULL, vat boolean, @@ -3244,6 +3244,14 @@ ALTER TABLE ONLY settings ADD CONSTRAINT settings_pkey PRIMARY KEY (id); +-- +-- Name: unique_code; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- + +ALTER TABLE ONLY registrars + ADD CONSTRAINT unique_code UNIQUE (code); + + -- -- Name: unique_contact_code; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: -- @@ -3252,6 +3260,30 @@ ALTER TABLE ONLY contacts ADD CONSTRAINT unique_contact_code UNIQUE (code); +-- +-- Name: unique_name; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- + +ALTER TABLE ONLY registrars + ADD CONSTRAINT unique_name UNIQUE (name); + + +-- +-- Name: unique_reference_no; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- + +ALTER TABLE ONLY registrars + ADD CONSTRAINT unique_reference_no UNIQUE (reference_no); + + +-- +-- Name: unique_reg_no; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- + +ALTER TABLE ONLY registrars + ADD CONSTRAINT unique_reg_no UNIQUE (reg_no); + + -- -- Name: unique_session_id; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: -- @@ -3854,20 +3886,6 @@ CREATE INDEX index_registrant_verifications_on_created_at ON registrant_verifica CREATE INDEX index_registrant_verifications_on_domain_id ON registrant_verifications USING btree (domain_id); --- --- Name: index_registrars_on_code; Type: INDEX; Schema: public; Owner: -; Tablespace: --- - -CREATE INDEX index_registrars_on_code ON registrars USING btree (code); - - --- --- Name: index_registrars_on_legacy_id; Type: INDEX; Schema: public; Owner: -; Tablespace: --- - -CREATE INDEX index_registrars_on_legacy_id ON registrars USING btree (legacy_id); - - -- -- Name: index_settings_on_thing_type_and_thing_id_and_var; Type: INDEX; Schema: public; Owner: -; Tablespace: -- @@ -4678,3 +4696,9 @@ INSERT INTO schema_migrations (version) VALUES ('20180306183540'); INSERT INTO schema_migrations (version) VALUES ('20180306183549'); +INSERT INTO schema_migrations (version) VALUES ('20180309053424'); + +INSERT INTO schema_migrations (version) VALUES ('20180309053921'); + +INSERT INTO schema_migrations (version) VALUES ('20180309054510'); + diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index c3b67ba88..2b9909f68 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -101,7 +101,7 @@ namespace :import do puts "-----> Generating reference numbers" Registrar.all.each do |x| - x.generate_iso_11649_reference_no + x.send(:generate_iso_11649_reference_no) x.save(validate: false) end diff --git a/spec/models/registrar_spec.rb b/spec/models/registrar_spec.rb deleted file mode 100644 index 4a8110d45..000000000 --- a/spec/models/registrar_spec.rb +++ /dev/null @@ -1,118 +0,0 @@ -require 'rails_helper' - -describe Registrar do - context 'with invalid attribute' do - before :all do - @registrar = Registrar.new - end - - it 'is not valid' do - @registrar.valid? - @registrar.errors.full_messages.should include(*[ - 'Contact e-mail is missing', - 'Country code is missing', - 'Name is missing', - 'Reg no is missing', - 'Code is missing' - ]) - end - - it 'returns an error with invalid email' do - @registrar.email = 'bla' - @registrar.billing_email = 'bla' - - @registrar.valid? - @registrar.errors[:email].should == ['is invalid'] - @registrar.errors[:billing_email].should == ['is invalid'] - end - - it 'should not have valid code' do - @registrar.code.should == nil - end - - it 'should generate reference number' do - @registrar.generate_iso_11649_reference_no - @registrar.reference_no.should_not be_blank - @registrar.reference_no.last(10).to_i.should_not == 0 - end - end - - context 'with valid attributes' do - before :all do - @registrar = create(:registrar) - end - - it 'should be valid' do - @registrar.valid? - @registrar.errors.full_messages.should match_array([]) - end - - it 'should be valid twice' do - @registrar = create(:registrar) - @registrar.valid? - @registrar.errors.full_messages.should match_array([]) - end - - it 'should remove blank from code' do - registrar = build(:registrar, code: 'with blank') - registrar.valid? - registrar.errors.full_messages.should match_array([ - ]) - registrar.code.should == 'WITHBLANK' - end - - it 'should remove colon from code' do - registrar = build(:registrar, code: 'with colon:and:blank') - registrar.valid? - registrar.errors.full_messages.should match_array([ - ]) - registrar.code.should == 'WITHCOLONANDBLANK' - end - - it 'should allow dot in code' do - registrar = build(:registrar, code: 'with.dot') - registrar.valid? - registrar.errors.full_messages.should match_array([ - ]) - registrar.code.should == 'WITH.DOT' - end - - it 'should have one version' do - with_versioning do - @registrar.versions.should == [] - @registrar.name = 'New name' - @registrar.save - @registrar.errors.full_messages.should match_array([]) - @registrar.versions.size.should == 1 - end - end - - it 'should return full address' do - registrar = described_class.new(street: 'Street 999', city: 'Town', state: 'County', zip: 'Postal') - registrar.address.should == 'Street 999, Town, County, Postal' - end - - it 'should not be able to change code' do - registrar = create(:registrar, code: 'TEST') - registrar.code = 'new-code' - expect(registrar.code).to eq('TEST') - end - - it 'should be able to issue a prepayment invoice' do - Setting.days_to_keep_invoices_active = 30 - create(:registrar, name: 'EIS', reg_no: '90010019') - @registrar.issue_prepayment_invoice(200, 'add some money') - @registrar.invoices.count.should == 1 - i = @registrar.invoices.first - i.sum.should == BigDecimal.new('240.0') - i.due_date.should be_within(0.1).of((Time.zone.now + 30.days).end_of_day) - i.description.should == 'add some money' - end - - it 'should not allaw to use CID as code for leagcy reasons' do - registrar = build(:registrar, code: 'CID') - registrar.valid? - registrar.errors.full_messages.should == ['Code is forbidden to use'] - end - end -end diff --git a/spec/requests/admin/registrars/update_spec.rb b/spec/requests/admin/registrars/update_spec.rb deleted file mode 100644 index 7bffb99d0..000000000 --- a/spec/requests/admin/registrars/update_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -require 'rails_helper' - -RSpec.describe 'admin registrar update' do - before :example do - sign_in_to_admin_area - end - - it 'updates website' do - registrar = create(:registrar, website: 'test') - - patch admin_registrar_path(registrar), registrar: attributes_for(:registrar, website: 'new-website') - registrar.reload - - expect(registrar.website).to eq('new-website') - end - - it 'updates email' do - registrar = create(:registrar, email: 'test@test.com') - - patch admin_registrar_path(registrar), registrar: attributes_for(:registrar, email: 'new-test@test.com') - registrar.reload - - expect(registrar.email).to eq('new-test@test.com') - end - - it 'updates billing email' do - registrar = create(:registrar, billing_email: 'test@test.com') - - patch admin_registrar_path(registrar), registrar: attributes_for(:registrar, billing_email: 'new-test@test.com') - registrar.reload - - expect(registrar.billing_email).to eq('new-test@test.com') - end - - it 'redirects to :show' do - registrar = create(:registrar) - - patch admin_registrar_path(registrar), { registrar: attributes_for(:registrar) } - - expect(response).to redirect_to admin_registrar_path(registrar) - end -end diff --git a/spec/routing/admin/registrars_routing_spec.rb b/spec/routing/admin/registrars_routing_spec.rb deleted file mode 100644 index 14ec765e9..000000000 --- a/spec/routing/admin/registrars_routing_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'rails_helper' - -RSpec.describe Admin::RegistrarsController do - describe 'routing' do - it 'routes to #index' do - expect(get: '/admin/registrars').to route_to('admin/registrars#index') - end - - it 'routes to #new' do - expect(get: '/admin/registrars/new').to route_to('admin/registrars#new') - end - - it 'routes to #show' do - expect(get: '/admin/registrars/1').to route_to('admin/registrars#show', id: '1') - end - - it 'routes to #edit' do - expect(get: '/admin/registrars/1/edit').to route_to('admin/registrars#edit', id: '1') - end - - it 'routes to #create' do - expect(post: '/admin/registrars').to route_to('admin/registrars#create') - end - - it 'routes to #update' do - expect(patch: '/admin/registrars/1').to route_to('admin/registrars#update', id: '1') - end - - it 'routes to #destroy' do - expect(delete: '/admin/registrars/1').to route_to('admin/registrars#destroy', id: '1') - end - end -end diff --git a/spec/views/admin/registrars/_form.html.erb_spec.rb b/spec/views/admin/registrars/_form.html.erb_spec.rb deleted file mode 100644 index 83b727a30..000000000 --- a/spec/views/admin/registrars/_form.html.erb_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'rails_helper' - -RSpec.describe 'admin/registrars/_form' do - let(:registrar) { build_stubbed(:registrar) } - - before :example do - assign(:registrar, registrar) - stub_template 'shared/_full_errors' => '' - - without_partial_double_verification do - allow(view).to receive(:available_languages).and_return({}) - end - end - - it 'has website' do - render - expect(rendered).to have_css('[name="registrar[website]"]') - end -end diff --git a/spec/views/admin/registrars/show.haml_spec.rb b/spec/views/admin/registrars/show.haml_spec.rb deleted file mode 100644 index 4def10e8c..000000000 --- a/spec/views/admin/registrars/show.haml_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'rails_helper' - -RSpec.describe 'admin/registrars/show' do - let(:registrar) { build_stubbed(:registrar, website: 'test website') } - - before :example do - assign(:registrar, registrar) - stub_template 'shared/_title' => '' - - without_partial_double_verification do - allow(view).to receive(:available_languages).and_return({}) - end - end - - it 'has website' do - render - expect(rendered).to have_text('test website') - end -end diff --git a/test/controllers/admin/registrars/update_test.rb b/test/controllers/admin/registrars/update_test.rb deleted file mode 100644 index 2c996ef44..000000000 --- a/test/controllers/admin/registrars/update_test.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'test_helper' - -class RegistrarsControllerTest < ActionDispatch::IntegrationTest - def setup - login_as users(:admin) - @registrar = registrars(:bestnames) - end - - def test_updates_website - patch admin_registrar_path(@registrar), registrar: @registrar.attributes.merge(website: 'new.example.com') - @registrar.reload - - assert_equal 'new.example.com', @registrar.website - end - - def test_updates_email - patch admin_registrar_path(@registrar), registrar: @registrar.attributes.merge(email: 'new@example.com') - @registrar.reload - - assert_equal 'new@example.com', @registrar.email - end - - def test_updates_billing_email - patch admin_registrar_path(@registrar), - registrar: @registrar.attributes.merge(billing_email: 'new-billing@example.com') - @registrar.reload - - assert_equal 'new-billing@example.com', @registrar.billing_email - end -end diff --git a/test/fixtures/accounts.yml b/test/fixtures/accounts.yml index ea51ff7eb..a7060c249 100644 --- a/test/fixtures/accounts.yml +++ b/test/fixtures/accounts.yml @@ -3,3 +3,9 @@ cash: balance: 100 currency: EUR registrar: bestnames + +not_in_use_cash: + account_type: cash + balance: 0 + currency: EUR + registrar: not_in_use diff --git a/test/fixtures/registrars.yml b/test/fixtures/registrars.yml index 0026f323b..b0cc371a7 100644 --- a/test/fixtures/registrars.yml +++ b/test/fixtures/registrars.yml @@ -3,6 +3,10 @@ bestnames: reg_no: 1234 code: bestnames email: info@bestnames.test + street: Main Street + zip: 12345 + city: New York + state: New York country_code: US accounting_customer_code: bestnames language: en @@ -16,3 +20,12 @@ goodnames: country_code: US accounting_customer_code: goodnames language: en + +not_in_use: + name: any + reg_no: any + code: any + email: any@example.com + country_code: US + accounting_customer_code: any + language: en diff --git a/test/integration/admin/registrars/delete_test.rb b/test/integration/admin/registrars/delete_test.rb new file mode 100644 index 000000000..22e561664 --- /dev/null +++ b/test/integration/admin/registrars/delete_test.rb @@ -0,0 +1,18 @@ +require 'test_helper' + +class AdminAreaDeleteRegistrarTest < ActionDispatch::IntegrationTest + def setup + login_as users(:admin) + end + + def test_can_be_deleted_if_not_in_use + visit admin_registrar_url(registrars(:not_in_use)) + + assert_difference 'Registrar.count', -1 do + click_link_or_button 'Delete' + end + + assert_current_path admin_registrars_path + assert_text 'Registrar has been successfully deleted' + end +end diff --git a/test/integration/admin/registrars/edit_registrar_test.rb b/test/integration/admin/registrars/edit_registrar_test.rb deleted file mode 100644 index ada794032..000000000 --- a/test/integration/admin/registrars/edit_registrar_test.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'test_helper' - -class EditRegistrarTest < ActionDispatch::IntegrationTest - def setup - login_as users(:admin) - @registrar = registrars(:bestnames) - end - - def test_updates_registrar - visit admin_registrar_path(@registrar) - click_link_or_button 'Edit' - click_link_or_button 'Update registrar' - - assert_current_path admin_registrar_path(@registrar) - assert_text 'Registrar has been successfully updated' - end -end diff --git a/test/integration/admin/registrars/edit_test.rb b/test/integration/admin/registrars/edit_test.rb new file mode 100644 index 000000000..af420b179 --- /dev/null +++ b/test/integration/admin/registrars/edit_test.rb @@ -0,0 +1,69 @@ +require 'test_helper' + +class AdminAreaEditRegistrarTest < ActionDispatch::IntegrationTest + def setup + login_as users(:admin) + @registrar = registrars(:bestnames) + end + + def test_attributes_update + visit admin_registrar_path(@registrar) + click_link_or_button 'Edit' + + fill_in 'Name', with: 'new name' + fill_in 'Reg no', with: '4727673' + fill_in 'Contact phone', with: '2570937' + fill_in 'Website', with: 'http://new.example.com' + fill_in 'Contact e-mail', with: 'new@example.com' + + fill_in 'Street', with: 'new street' + fill_in 'Zip', with: 'new zip' + fill_in 'City', with: 'new city' + fill_in 'State / Province', with: 'new state' + select 'Germany', from: 'Country' + + fill_in 'VAT number', with: '2386449' + fill_in 'Accounting customer code', with: '866477' + fill_in 'Billing email', with: 'new-billing@example.com' + + select 'Estonian', from: 'Language' + click_link_or_button 'Update registrar' + + @registrar.reload + assert_equal 'new name', @registrar.name + assert_equal '4727673', @registrar.reg_no + assert_equal '2570937', @registrar.phone + assert_equal 'http://new.example.com', @registrar.website + assert_equal 'new@example.com', @registrar.email + + assert_equal 'new street', @registrar.street + assert_equal 'new zip', @registrar.zip + assert_equal 'new city', @registrar.city + assert_equal 'new state', @registrar.state + assert_equal Country.new('DE'), @registrar.country + + assert_equal '2386449', @registrar.vat_no + assert_equal '866477', @registrar.accounting_customer_code + assert_equal 'new-billing@example.com', @registrar.billing_email + + assert_equal 'et', @registrar.language + assert_current_path admin_registrar_path(@registrar) + assert_text 'Registrar has been successfully updated' + end + + def test_code_cannot_be_changed + visit admin_registrar_path(@registrar) + click_link_or_button 'Edit' + assert_no_field 'Code' + end + + def test_fails_gracefully + visit admin_registrar_path(@registrar) + click_link_or_button 'Edit' + fill_in 'Name', with: 'Good Names' + click_link_or_button 'Update registrar' + + assert_field 'Name', with: 'Good Names' + assert_text 'Name has already been taken' + end +end diff --git a/test/integration/admin/registrars/new_registrar_test.rb b/test/integration/admin/registrars/new_registrar_test.rb deleted file mode 100644 index 33deea9e8..000000000 --- a/test/integration/admin/registrars/new_registrar_test.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'test_helper' - -class NewRegistrarTest < ActionDispatch::IntegrationTest - def setup - login_as users(:admin) - end - - def test_creates_registrar - visit admin_registrars_path - click_link_or_button 'New registrar' - - fill_in 'registrar[name]', with: 'John Doe' - fill_in 'registrar[reg_no]', with: '1234567' - fill_in 'registrar[email]', with: 'test@test.com' - fill_in 'registrar[code]', with: 'test' - fill_in 'registrar[accounting_customer_code]', with: 'test' - click_link_or_button 'Create registrar' - - assert_current_path admin_registrar_path(Registrar.last) - assert_text 'Registrar has been successfully created' - assert_text 'John Doe' - end -end diff --git a/test/integration/admin/registrars/new_test.rb b/test/integration/admin/registrars/new_test.rb new file mode 100644 index 000000000..61a7a43be --- /dev/null +++ b/test/integration/admin/registrars/new_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class AdminAreaNewRegistrarTest < ActionDispatch::IntegrationTest + def setup + login_as users(:admin) + end + + def test_new_registrar_creation_with_required_params + visit admin_registrars_url + click_link_or_button 'New registrar' + + fill_in 'Name', with: 'Brand new names' + fill_in 'Reg no', with: '55555555' + fill_in 'Contact e-mail', with: 'test@example.com' + fill_in 'Accounting customer code', with: 'test' + fill_in 'Code', with: 'test' + + assert_difference 'Registrar.count' do + click_link_or_button 'Create registrar' + end + + assert_current_path admin_registrar_path(Registrar.last) + assert_text 'Registrar has been successfully created' + end + + def test_fails_gracefully + visit admin_registrars_url + click_link_or_button 'New registrar' + + fill_in 'Name', with: 'Best Names' + fill_in 'Reg no', with: '55555555' + fill_in 'Contact e-mail', with: 'test@example.com' + fill_in 'Accounting customer code', with: 'test' + fill_in 'Code', with: 'test' + + assert_no_difference 'Registrar.count' do + click_link_or_button 'Create registrar' + end + assert_field 'Name', with: 'Best Names' + assert_text 'Name has already been taken' + end + + def test_pre_populated_default_language + Setting.default_language = 'en' + visit admin_registrars_url + click_link_or_button 'New registrar' + assert_field 'Language', with: 'en' + end +end diff --git a/test/models/registrar/code_test.rb b/test/models/registrar/code_test.rb new file mode 100644 index 000000000..7b5188878 --- /dev/null +++ b/test/models/registrar/code_test.rb @@ -0,0 +1,29 @@ +require 'test_helper' + +class RegistrarCodeTest < ActiveSupport::TestCase + def setup + @registrar = registrars(:bestnames).dup + end + + def test_registrar_is_invalid_without_code + @registrar.code = '' + assert @registrar.invalid? + end + + def test_special_code_validation + @registrar.code = 'CID' + assert @registrar.invalid? + assert_includes @registrar.errors.full_messages, 'Code is forbidden' + end + + def test_cannot_be_changed_once_registrar_is_created + registrar = registrars(:bestnames) + registrar.update!(code: 'new-code') + refute_equal 'new-code', registrar.code + end + + def test_normalization + @registrar.code = 'with spaces:and:colon.' + assert_equal 'WITHSPACESANDCOLON.', @registrar.code + end +end diff --git a/test/models/registrar/delete_test.rb b/test/models/registrar/delete_test.rb new file mode 100644 index 000000000..2020c76dc --- /dev/null +++ b/test/models/registrar/delete_test.rb @@ -0,0 +1,37 @@ +require 'test_helper' + +class DeleteRegistrarTest < ActiveSupport::TestCase + def setup + @registrar = registrars(:not_in_use) + end + + def test_can_be_deleted_if_not_in_use + assert_difference 'Registrar.count', -1 do + @registrar.destroy + end + end + + def test_cannot_be_deleted_if_has_at_least_one_user + users(:api_bestnames).update!(registrar: @registrar) + + assert_no_difference 'Registrar.count' do + @registrar.destroy + end + end + + def test_cannot_be_deleted_if_has_at_least_one_contact + contacts(:john).update!(registrar: @registrar) + + assert_no_difference 'Registrar.count' do + @registrar.destroy + end + end + + def test_cannot_be_deleted_if_has_at_least_one_domain + domains(:shop).update!(registrar: @registrar) + + assert_no_difference 'Registrar.count' do + @registrar.destroy + end + end +end diff --git a/test/models/registrar_test.rb b/test/models/registrar_test.rb index 58a06b0ae..0901e83f0 100644 --- a/test/models/registrar_test.rb +++ b/test/models/registrar_test.rb @@ -9,21 +9,33 @@ class RegistrarTest < ActiveSupport::TestCase assert @registrar.valid? end + def test_invalid_without_name + @registrar.name = '' + assert @registrar.invalid? + end + + def test_invalid_without_reg_no + @registrar.reg_no = '' + assert @registrar.invalid? + end + + def test_invalid_without_email + @registrar.email = '' + assert @registrar.invalid? + end + def test_rejects_absent_accounting_customer_code @registrar.accounting_customer_code = nil - @registrar.validate assert @registrar.invalid? end def test_requires_country_code @registrar.country_code = nil - @registrar.validate assert @registrar.invalid? end - def test_requires_language - @registrar.language = nil - @registrar.validate + def test_invalid_without_language + @registrar.language = '' assert @registrar.invalid? end @@ -38,4 +50,13 @@ class RegistrarTest < ActiveSupport::TestCase registrar = Registrar.new(language: 'de') assert_equal 'de', registrar.language end + + def test_full_address + assert_equal 'Main Street, New York, New York, 12345', @registrar.address + end + + def test_reference_number_generation + @registrar.validate + refute_empty @registrar.reference_no + end end From d6d20a81a95e1810989b20f5e9ab9b1766542163 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Fri, 9 Mar 2018 13:08:36 +0200 Subject: [PATCH 30/63] Add registrar vat rate #623 --- app/models/registrar.rb | 33 +++------ app/models/type/vat_rate.rb | 19 +++++ app/views/admin/registrars/_billing.html.erb | 3 + .../admin/registrars/form/_billing.html.erb | 3 +- test/models/registrar/vat_test.rb | 73 +++++++++++++++++++ 5 files changed, 108 insertions(+), 23 deletions(-) create mode 100644 app/models/type/vat_rate.rb create mode 100644 test/models/registrar/vat_test.rb diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 1d5c4eba3..87915e4ba 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -1,21 +1,3 @@ -class VATRateType < ActiveRecord::Type::Value - def type_cast_from_user(value) - if value.blank? - nil - else - super - end - end - - def type_cast_from_database(value) - BigDecimal.new(value) * 100 if value - end - - def type_cast_for_database(value) - BigDecimal.new(value) / 100 if value - end -end - class Registrar < ActiveRecord::Base include Versions # version/registrar_version.rb @@ -35,13 +17,16 @@ class Registrar < ActiveRecord::Base validates :name, :reg_no, :reference_no, :code, uniqueness: true validates :accounting_customer_code, presence: true validates :language, presence: true - validates :vat_rate, :vat_no, absence: true, if: :local_vat_payer? - validates :vat_rate, presence: true, if: 'foreign_vat_payer? && vat_no.blank?' + + validates :vat_rate, presence: true, if: :vat_rate_required? + validates :vat_rate, absence: true, if: :local_vat_payer? validates :vat_rate, absence: true, if: 'foreign_vat_payer? && vat_no?' - validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 99 }, allow_nil: true + validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 99.9 }, + allow_nil: true + validate :forbid_special_code - attribute :vat_rate, VATRateType.new + attribute :vat_rate, ::Type::VATRate.new after_initialize :set_defaults before_validation :generate_iso_11649_reference_no @@ -216,4 +201,8 @@ class Registrar < ActiveRecord::Base def foreign_vat_payer? !local_vat_payer? end + + def vat_rate_required? + foreign_vat_payer? && vat_no.blank? + end end diff --git a/app/models/type/vat_rate.rb b/app/models/type/vat_rate.rb new file mode 100644 index 000000000..466b076f8 --- /dev/null +++ b/app/models/type/vat_rate.rb @@ -0,0 +1,19 @@ +module Type + class VATRate < ActiveRecord::Type::Value + def type_cast_from_user(value) + if value.blank? + nil + else + super + end + end + + def type_cast_from_database(value) + BigDecimal(value) * 100 if value + end + + def type_cast_for_database(value) + BigDecimal(value) / 100.0 if value + end + end +end diff --git a/app/views/admin/registrars/_billing.html.erb b/app/views/admin/registrars/_billing.html.erb index f3960233e..7ba778d08 100644 --- a/app/views/admin/registrars/_billing.html.erb +++ b/app/views/admin/registrars/_billing.html.erb @@ -7,6 +7,9 @@
<%= Registrar.human_attribute_name :vat_no %>
<%= registrar.vat_no %>
+
<%= Registrar.human_attribute_name :vat_rate %>
+
<%= number_to_percentage registrar.vat_rate, precision: 1 %>
+
<%= Registrar.human_attribute_name :accounting_customer_code %>
<%= registrar.accounting_customer_code %>
diff --git a/app/views/admin/registrars/form/_billing.html.erb b/app/views/admin/registrars/form/_billing.html.erb index ff67232de..e644085cf 100644 --- a/app/views/admin/registrars/form/_billing.html.erb +++ b/app/views/admin/registrars/form/_billing.html.erb @@ -23,7 +23,8 @@
- <%= f.number_field :vat_rate, min: 0, max: 99, class: 'form-control' %> + <%= f.number_field :vat_rate, min: 0, max: 99, step: 0.1, + class: 'form-control' %>
%
diff --git a/test/models/registrar/vat_test.rb b/test/models/registrar/vat_test.rb new file mode 100644 index 000000000..cf36b6266 --- /dev/null +++ b/test/models/registrar/vat_test.rb @@ -0,0 +1,73 @@ +require 'test_helper' + +class RegistrarVATTest < ActiveSupport::TestCase + def setup + @registrar = registrars(:bestnames) + end + + def test_optional_vat_no + @registrar.vat_no = '' + assert @registrar.valid? + + @registrar.vat_no = 'any' + assert @registrar.valid? + end + + def test_requires_vat_rate_when_registrar_is_foreign_vat_payer_and_vat_no_is_absent + @registrar.vat_no = '' + + Registry.instance.stub(:legal_address_country, Country.new('GB')) do + @registrar.vat_rate = '' + assert @registrar.invalid? + assert @registrar.errors.added?(:vat_rate, :blank) + + @registrar.vat_rate = -1 + assert @registrar.invalid? + + @registrar.vat_rate = 1 + assert @registrar.valid? + + @registrar.vat_rate = 99.9 + assert @registrar.valid? + + @registrar.vat_rate = 100 + assert @registrar.invalid? + end + end + + def test_vat_is_not_applied_when_registrar_is_local_vat_payer + @registrar.vat_rate = 1 + assert @registrar.invalid? + + @registrar.vat_rate = nil + assert @registrar.valid? + end + + def test_vat_is_not_applied_when_registrar_is_foreign_vat_payer_and_vat_no_is_present + @registrar.vat_no = 'valid' + + Registry.instance.stub(:legal_address_country, Country.new('GB')) do + @registrar.vat_rate = 1 + assert @registrar.invalid? + + @registrar.vat_rate = nil + assert @registrar.valid? + end + end + + def test_serializes_and_deserializes_vat_rate + @registrar.vat_rate = '25.5' + + Registry.instance.stub(:legal_address_country, Country.new('GB')) do + @registrar.save! + end + + @registrar.reload + assert_equal 25.5, @registrar.vat_rate + end + + def test_treats_empty_vat_rate_as_absent + @registrar.vat_rate = '' + assert_nil @registrar.vat_rate + end +end From 9ae08a1e6f2215db847805ce497de794514480b5 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Sat, 4 Nov 2017 21:33:01 +0200 Subject: [PATCH 31/63] Remove dead code #623 (cherry picked from commit 49af8cc) --- app/models/registrar.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 87915e4ba..1f1bfaea9 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -123,11 +123,6 @@ class Registrar < ActiveRecord::Base cash_account.account_activities.create!(args) end - def credit!(args) - args[:currency] = 'EUR' - cash_account.account_activities.create!(args) - end - def address [street, city, state, zip].reject(&:blank?).compact.join(', ') end From efaac16ceeec2ead3349522d15d90ba76b629290 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Sat, 10 Mar 2018 16:33:35 +0200 Subject: [PATCH 32/63] Remove unused attribute #772 --- app/models/directo.rb | 2 +- app/models/invoice.rb | 2 +- app/models/registrar.rb | 1 - ...0180310142630_remove_invoices_invoice_type.rb | 5 +++++ db/structure.sql | 3 ++- doc/models_complete.svg | 1 - spec/factories/invoice.rb | 1 - spec/models/invoice_spec.rb | 1 - test/fixtures/invoices.yml | 1 - .../registrar/billing/balance_top_up_test.rb | 16 ++++++++++++++++ 10 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20180310142630_remove_invoices_invoice_type.rb create mode 100644 test/integration/registrar/billing/balance_top_up_test.rb diff --git a/app/models/directo.rb b/app/models/directo.rb index 62cf43804..e0301e3dc 100644 --- a/app/models/directo.rb +++ b/app/models/directo.rb @@ -3,7 +3,7 @@ class Directo < ActiveRecord::Base belongs_to :item, polymorphic: true def self.send_receipts - new_trans = Invoice.where(invoice_type: "DEB", in_directo: false).where(cancelled_at: nil) + new_trans = Invoice.where(in_directo: false).where(cancelled_at: nil) total = new_trans.count counter = 0 Rails.logger.info("[DIRECTO] Will try to send #{total} invoices") diff --git a/app/models/invoice.rb b/app/models/invoice.rb index ad478443d..5dcf9ba78 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -27,7 +27,7 @@ class Invoice < ActiveRecord::Base attr_accessor :billing_email validates :billing_email, email_format: { message: :invalid }, allow_blank: true - validates :invoice_type, :due_date, :currency, :seller_name, + validates :due_date, :currency, :seller_name, :seller_iban, :buyer_name, :invoice_items, :vat_prc, presence: true before_create :set_invoice_number, :check_vat diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 6a10983d2..3c8300eb5 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -76,7 +76,6 @@ class Registrar < ActiveRecord::Base # rubocop:disable Metrics/AbcSize def issue_prepayment_invoice(amount, description = nil) invoices.create( - invoice_type: 'DEB', due_date: (Time.zone.now.to_date + Setting.days_to_keep_invoices_active.days).end_of_day, payment_term: 'prepayment', description: description, diff --git a/db/migrate/20180310142630_remove_invoices_invoice_type.rb b/db/migrate/20180310142630_remove_invoices_invoice_type.rb new file mode 100644 index 000000000..f8cddadf9 --- /dev/null +++ b/db/migrate/20180310142630_remove_invoices_invoice_type.rb @@ -0,0 +1,5 @@ +class RemoveInvoicesInvoiceType < ActiveRecord::Migration + def change + remove_column :invoices, :invoice_type, :string + end +end diff --git a/db/structure.sql b/db/structure.sql index a9257ffa3..65a686a97 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -998,7 +998,6 @@ CREATE TABLE invoices ( id integer NOT NULL, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, - invoice_type character varying NOT NULL, due_date timestamp without time zone NOT NULL, payment_term character varying, currency character varying NOT NULL, @@ -4679,3 +4678,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180306183549'); INSERT INTO schema_migrations (version) VALUES ('20180308123240'); +INSERT INTO schema_migrations (version) VALUES ('20180310142630'); + diff --git a/doc/models_complete.svg b/doc/models_complete.svg index 296d20c67..1cbefea90 100644 --- a/doc/models_complete.svg +++ b/doc/models_complete.svg @@ -1614,7 +1614,6 @@ id :integer created_at :datetime updated_at :datetime -invoice_type :string due_date :datetime payment_term :string currency :string diff --git a/spec/factories/invoice.rb b/spec/factories/invoice.rb index 7eeed32b9..3bd0c5f08 100644 --- a/spec/factories/invoice.rb +++ b/spec/factories/invoice.rb @@ -3,7 +3,6 @@ FactoryBot.define do buyer_name 'Registrar 1' currency { 'EUR' } due_date { Time.zone.now.to_date + 1.day } - invoice_type 'DEB' seller_iban { '123' } seller_name { 'EIS' } seller_city { 'Tallinn' } diff --git a/spec/models/invoice_spec.rb b/spec/models/invoice_spec.rb index 49d15310f..95b603f85 100644 --- a/spec/models/invoice_spec.rb +++ b/spec/models/invoice_spec.rb @@ -13,7 +13,6 @@ describe Invoice do "Currency is missing", "Due date is missing", "Invoice items is missing", - "Invoice type is missing", "Seller iban is missing", "Seller name is missing", "Vat prc is missing" diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index 7a1c85dd1..cc9f829d0 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -1,7 +1,6 @@ DEFAULTS: &DEFAULTS created_at: <%= Date.parse '2010-07-05' %> due_date: <%= Date.parse '2010-07-06' %> - invoice_type: DEB currency: EUR seller_name: John Doe seller_iban: 1234 diff --git a/test/integration/registrar/billing/balance_top_up_test.rb b/test/integration/registrar/billing/balance_top_up_test.rb new file mode 100644 index 000000000..c4dd1cf5d --- /dev/null +++ b/test/integration/registrar/billing/balance_top_up_test.rb @@ -0,0 +1,16 @@ +require 'test_helper' + +class BalanceTopUpTest < ActionDispatch::IntegrationTest + def setup + login_as users(:api_bestnames) + end + + def test_registrar_balance_top_up + visit registrar_invoices_url + click_link_or_button 'Add deposit' + fill_in 'Amount', with: 100 + click_link_or_button 'Add' + + assert_text 'Please pay the following invoice' + end +end From c129598d2b443a03b473bf3ae3acfe825e08774b Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Sat, 10 Mar 2018 16:51:11 +0200 Subject: [PATCH 33/63] Improve VAT rate validation #623 --- app/models/invoice.rb | 5 +++-- app/models/registrar.rb | 2 +- test/models/invoice_test.rb | 20 ++++++++------------ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index dfdafee6d..165b7f614 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -28,8 +28,9 @@ class Invoice < ActiveRecord::Base validates :billing_email, email_format: { message: :invalid }, allow_blank: true validates :due_date, :currency, :seller_name, - :seller_iban, :buyer_name, :invoice_items, :vat_rate, presence: true - validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 99 }, allow_nil: true + :seller_iban, :buyer_name, :invoice_items, presence: true + validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 }, + allow_nil: true before_create :set_invoice_number, :check_vat diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 0c7559ce8..0f9cdae06 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -21,7 +21,7 @@ class Registrar < ActiveRecord::Base validates :vat_rate, presence: true, if: :vat_rate_required? validates :vat_rate, absence: true, if: :local_vat_payer? validates :vat_rate, absence: true, if: 'foreign_vat_payer? && vat_no?' - validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 99.9 }, + validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 }, allow_nil: true validate :forbid_special_code diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index 770a88590..4a2457be3 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -9,26 +9,22 @@ class InvoiceTest < ActiveSupport::TestCase assert @invoice.valid? end - def test_invalid_without_vat_rate + def test_valid_without_vat_rate @invoice.vat_rate = nil - assert @invoice.invalid? - end - - def test_allows_absent_vat_rate - @invoice.vat_rate = nil - @invoice.validate assert @invoice.valid? end - def test_rejects_negative_vat_rate + def test_vat_rate_validation @invoice.vat_rate = -1 - @invoice.validate assert @invoice.invalid? - end - def test_rejects_vat_rate_greater_than_max + @invoice.vat_rate = 1 + assert @invoice.valid? + + @invoice.vat_rate = 99.9 + assert @invoice.valid? + @invoice.vat_rate = 100 - @invoice.validate assert @invoice.invalid? end end From cf2aa76cc62b3d14ef46241822a5eb3dbe4a1c87 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Sat, 10 Mar 2018 16:57:24 +0200 Subject: [PATCH 34/63] Regenerate structure.sql #623 --- db/structure.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/db/structure.sql b/db/structure.sql index 70cb7e5b2..d54caef91 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -3243,7 +3243,7 @@ ALTER TABLE ONLY settings -- --- Name: unique_code; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- Name: unique_code; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: -- ALTER TABLE ONLY registrars @@ -3259,7 +3259,7 @@ ALTER TABLE ONLY contacts -- --- Name: unique_name; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- Name: unique_name; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: -- ALTER TABLE ONLY registrars @@ -3267,7 +3267,7 @@ ALTER TABLE ONLY registrars -- --- Name: unique_reference_no; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- Name: unique_reference_no; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: -- ALTER TABLE ONLY registrars @@ -3275,7 +3275,7 @@ ALTER TABLE ONLY registrars -- --- Name: unique_reg_no; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- Name: unique_reg_no; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: -- ALTER TABLE ONLY registrars From 026c36d066f2d50216dc4414655263dab0e4fde7 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Sun, 11 Mar 2018 13:35:33 +0200 Subject: [PATCH 35/63] Fix vat rate form field constraints #623 --- app/views/admin/registrars/form/_billing.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/admin/registrars/form/_billing.html.erb b/app/views/admin/registrars/form/_billing.html.erb index e644085cf..c87da8b6c 100644 --- a/app/views/admin/registrars/form/_billing.html.erb +++ b/app/views/admin/registrars/form/_billing.html.erb @@ -23,7 +23,7 @@
- <%= f.number_field :vat_rate, min: 0, max: 99, step: 0.1, + <%= f.number_field :vat_rate, min: 0, max: 99.9, step: 0.1, class: 'form-control' %>
%
From 7d72d9cb34fc05dbf95dded17837e3a187f7f3b8 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Sun, 11 Mar 2018 19:33:21 +0200 Subject: [PATCH 36/63] Fix vat rate #623 --- app/models/invoice.rb | 19 ++++--- app/models/registrar.rb | 31 +++++------ app/models/type/vat_rate.rb | 14 ++--- test/fixtures/invoices.yml | 1 + test/fixtures/registrars.yml | 3 +- test/models/invoice/vat_rate_test.rb | 51 ++++++++++++++++++ test/models/invoice_test.rb | 19 ------- test/models/registrar/vat_test.rb | 77 +++++++++++++++------------- 8 files changed, 121 insertions(+), 94 deletions(-) create mode 100644 test/models/invoice/vat_rate_test.rb diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 165b7f614..d78c24d07 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -32,9 +32,11 @@ class Invoice < ActiveRecord::Base validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 }, allow_nil: true - before_create :set_invoice_number, :check_vat + after_initialize :apply_defaults + before_create :set_invoice_number - before_save :check_vat + attribute :vat_rate, ::Type::VATRate.new + attr_readonly :vat_rate def set_invoice_number last_no = Invoice.order(number: :desc).where('number IS NOT NULL').limit(1).pluck(:number).first @@ -52,12 +54,6 @@ class Invoice < ActiveRecord::Base false end - def check_vat - if buyer.country_code != 'EE' && buyer.vat_no.present? - self.vat_rate = 0 - end - end - before_save -> { self.sum_cache = sum } class << self @@ -159,10 +155,17 @@ class Invoice < ActiveRecord::Base end def vat + return 0 unless vat_rate (sum_without_vat * vat_rate).round(2) end def sum (sum_without_vat + vat).round(2) end + + private + + def apply_defaults + self.vat_rate = buyer.effective_vat_rate unless vat_rate + end end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 0f9cdae06..9e7d84a7b 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -18,8 +18,8 @@ class Registrar < ActiveRecord::Base validates :accounting_customer_code, presence: true validates :language, presence: true - validates :vat_rate, presence: true, if: :vat_rate_required? - validates :vat_rate, absence: true, if: :local_vat_payer? + validates :vat_rate, presence: true, if: 'foreign_vat_payer? && vat_no.blank?' + validates :vat_rate, absence: true, if: :home_vat_payer? validates :vat_rate, absence: true, if: 'foreign_vat_payer? && vat_no?' validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 }, allow_nil: true @@ -56,22 +56,11 @@ class Registrar < ActiveRecord::Base # rubocop:disable Metrics/MethodLength # rubocop:disable Metrics/AbcSize def issue_prepayment_invoice(amount, description = nil) - vat_rate = if local_vat_payer? - Registry.instance.vat_rate - else - if vat_no.blank? - self.vat_rate - else - nil - end - end - invoices.create( due_date: (Time.zone.now.to_date + Setting.days_to_keep_invoices_active.days).end_of_day, payment_term: 'prepayment', description: description, currency: 'EUR', - vat_rate: vat_rate, seller_name: Setting.registry_juridical_name, seller_reg_no: Setting.registry_reg_no, seller_iban: Setting.registry_iban, @@ -157,6 +146,14 @@ class Registrar < ActiveRecord::Base end end + def effective_vat_rate + if home_vat_payer? + Registry.instance.vat_rate + else + vat_rate + end + end + private def set_defaults @@ -188,15 +185,11 @@ class Registrar < ActiveRecord::Base end end - def local_vat_payer? + def home_vat_payer? country == Registry.instance.legal_address_country end def foreign_vat_payer? - !local_vat_payer? - end - - def vat_rate_required? - foreign_vat_payer? && vat_no.blank? + !home_vat_payer? end end diff --git a/app/models/type/vat_rate.rb b/app/models/type/vat_rate.rb index 466b076f8..5ee993211 100644 --- a/app/models/type/vat_rate.rb +++ b/app/models/type/vat_rate.rb @@ -1,19 +1,11 @@ module Type - class VATRate < ActiveRecord::Type::Value - def type_cast_from_user(value) - if value.blank? - nil - else - super - end - end - + class VATRate < ActiveRecord::Type::Decimal def type_cast_from_database(value) - BigDecimal(value) * 100 if value + super * 100 if value end def type_cast_for_database(value) - BigDecimal(value) / 100.0 if value + super / 100.0 if value end end end diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index 6fbfa282b..ffb53f938 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -4,6 +4,7 @@ DEFAULTS: &DEFAULTS currency: EUR seller_name: John Doe seller_iban: 1234 + buyer: bestnames buyer_name: Jane Doe vat_rate: 0.2 diff --git a/test/fixtures/registrars.yml b/test/fixtures/registrars.yml index b0cc371a7..a0faee8b4 100644 --- a/test/fixtures/registrars.yml +++ b/test/fixtures/registrars.yml @@ -17,7 +17,8 @@ goodnames: reg_no: 12345 code: goodnames email: info@goodnames.test - country_code: US + country_code: DE + vat_no: DE123456789 accounting_customer_code: goodnames language: en diff --git a/test/models/invoice/vat_rate_test.rb b/test/models/invoice/vat_rate_test.rb new file mode 100644 index 000000000..cb2bd9f19 --- /dev/null +++ b/test/models/invoice/vat_rate_test.rb @@ -0,0 +1,51 @@ +require 'test_helper' + +class InvoiceVATRateTest < ActiveSupport::TestCase + def setup + @invoice = invoices(:valid) + end + + def test_valid_without_vat_rate + @invoice.vat_rate = nil + assert @invoice.valid? + end + + def test_vat_rate_validation + @invoice.vat_rate = -1 + assert @invoice.invalid? + + @invoice.vat_rate = 0 + assert @invoice.valid? + + @invoice.vat_rate = 99.9 + assert @invoice.valid? + + @invoice.vat_rate = 100 + assert @invoice.invalid? + end + + def test_serializes_and_deserializes_vat_rate + invoice = @invoice.dup + invoice.invoice_items = @invoice.invoice_items + invoice.vat_rate = BigDecimal('25.5') + invoice.save! + invoice.reload + assert_equal BigDecimal('25.5'), invoice.vat_rate + end + + def test_vat_rate_defaults_to_effective_vat_rate_of_a_registrar + registrar = registrars(:bestnames) + + registrar.stub(:effective_vat_rate, 55) do + invoice = Invoice.new(buyer: registrar) + assert_equal 55, invoice.vat_rate + end + end + + def test_vat_rate_cannot_be_updated + @invoice.vat_rate = 21 + @invoice.save! + @invoice.reload + refute_equal 21, @invoice.vat_rate + end +end diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index 4a2457be3..29af6fab8 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -8,23 +8,4 @@ class InvoiceTest < ActiveSupport::TestCase def test_valid assert @invoice.valid? end - - def test_valid_without_vat_rate - @invoice.vat_rate = nil - assert @invoice.valid? - end - - def test_vat_rate_validation - @invoice.vat_rate = -1 - assert @invoice.invalid? - - @invoice.vat_rate = 1 - assert @invoice.valid? - - @invoice.vat_rate = 99.9 - assert @invoice.valid? - - @invoice.vat_rate = 100 - assert @invoice.invalid? - end end diff --git a/test/models/registrar/vat_test.rb b/test/models/registrar/vat_test.rb index cf36b6266..802a0d987 100644 --- a/test/models/registrar/vat_test.rb +++ b/test/models/registrar/vat_test.rb @@ -13,28 +13,6 @@ class RegistrarVATTest < ActiveSupport::TestCase assert @registrar.valid? end - def test_requires_vat_rate_when_registrar_is_foreign_vat_payer_and_vat_no_is_absent - @registrar.vat_no = '' - - Registry.instance.stub(:legal_address_country, Country.new('GB')) do - @registrar.vat_rate = '' - assert @registrar.invalid? - assert @registrar.errors.added?(:vat_rate, :blank) - - @registrar.vat_rate = -1 - assert @registrar.invalid? - - @registrar.vat_rate = 1 - assert @registrar.valid? - - @registrar.vat_rate = 99.9 - assert @registrar.valid? - - @registrar.vat_rate = 100 - assert @registrar.invalid? - end - end - def test_vat_is_not_applied_when_registrar_is_local_vat_payer @registrar.vat_rate = 1 assert @registrar.invalid? @@ -43,30 +21,57 @@ class RegistrarVATTest < ActiveSupport::TestCase assert @registrar.valid? end + def test_requires_vat_rate_when_registrar_is_foreign_vat_payer_and_vat_no_is_absent + @registrar.country_code = 'DE' + @registrar.vat_no = '' + + @registrar.vat_rate = '' + assert @registrar.invalid? + assert @registrar.errors.added?(:vat_rate, :blank) + end + def test_vat_is_not_applied_when_registrar_is_foreign_vat_payer_and_vat_no_is_present + @registrar.country_code = 'DE' @registrar.vat_no = 'valid' - Registry.instance.stub(:legal_address_country, Country.new('GB')) do - @registrar.vat_rate = 1 - assert @registrar.invalid? + @registrar.vat_rate = 1 + assert @registrar.invalid? - @registrar.vat_rate = nil - assert @registrar.valid? - end + @registrar.vat_rate = nil + assert @registrar.valid? + end + + def test_vat_rate_validation + @registrar.country_code = 'DE' + @registrar.vat_no = '' + + @registrar.vat_rate = -1 + assert @registrar.invalid? + + @registrar.vat_rate = 0 + assert @registrar.valid? + + @registrar.vat_rate = 99.9 + assert @registrar.valid? + + @registrar.vat_rate = 100 + assert @registrar.invalid? end def test_serializes_and_deserializes_vat_rate - @registrar.vat_rate = '25.5' - - Registry.instance.stub(:legal_address_country, Country.new('GB')) do - @registrar.save! - end - + @registrar.country_code = 'DE' + @registrar.vat_rate = BigDecimal('25.5') + @registrar.save! @registrar.reload - assert_equal 25.5, @registrar.vat_rate + assert_equal BigDecimal('25.5'), @registrar.vat_rate end - def test_treats_empty_vat_rate_as_absent + def test_parses_vat_rate_as_a_string + @registrar.vat_rate = '25.5' + assert_equal BigDecimal('25.5'), @registrar.vat_rate + end + + def test_treats_empty_vat_rate_as_nil @registrar.vat_rate = '' assert_nil @registrar.vat_rate end From 4a63a60433360bbe7eb8c065d31795ca23b81586 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 12 Mar 2018 10:17:15 +0200 Subject: [PATCH 37/63] Return VAT rate as BigDecimal #623 --- app/models/registry.rb | 2 +- test/models/registry_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/registry.rb b/app/models/registry.rb index 162ab4d71..5e5c8cd93 100644 --- a/app/models/registry.rb +++ b/app/models/registry.rb @@ -2,7 +2,7 @@ class Registry include Singleton def vat_rate - Setting.registry_vat_prc + Setting.registry_vat_prc.to_d * 100 end def legal_address_country diff --git a/test/models/registry_test.rb b/test/models/registry_test.rb index 2f25686c6..70bc037c6 100644 --- a/test/models/registry_test.rb +++ b/test/models/registry_test.rb @@ -10,7 +10,7 @@ class RegistryTest < ActiveSupport::TestCase end def test_vat_rate - Setting.registry_vat_prc = 0.2 - assert 0.2, @registry.vat_rate + Setting.registry_vat_prc = 0.25 + assert_equal BigDecimal(25), @registry.vat_rate end end From 57ab19d86adc88c74bd04f84c834754e4228abbe Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 12 Mar 2018 11:34:41 +0200 Subject: [PATCH 38/63] Improve readability #623 --- test/models/invoice/vat_rate_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/models/invoice/vat_rate_test.rb b/test/models/invoice/vat_rate_test.rb index cb2bd9f19..27bc4b705 100644 --- a/test/models/invoice/vat_rate_test.rb +++ b/test/models/invoice/vat_rate_test.rb @@ -5,7 +5,7 @@ class InvoiceVATRateTest < ActiveSupport::TestCase @invoice = invoices(:valid) end - def test_valid_without_vat_rate + def test_optional_vat_rate @invoice.vat_rate = nil assert @invoice.valid? end From 7cc479c3fe34b4a9a3ccaaa3e9f73852ae7a95bf Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 12 Mar 2018 11:35:34 +0200 Subject: [PATCH 39/63] Update fixtures #623 --- test/fixtures/invoice_items.yml | 18 +++++++++--------- test/fixtures/invoices.yml | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/fixtures/invoice_items.yml b/test/fixtures/invoice_items.yml index 417203a51..9c37ece9a 100644 --- a/test/fixtures/invoice_items.yml +++ b/test/fixtures/invoice_items.yml @@ -1,13 +1,13 @@ -DEFAULTS: &DEFAULTS +one: description: Acme services + price: 5 + amount: 1 + unit: pc + invoice: valid + +two: + description: Acme services + price: 5 amount: 2 unit: pc - price: 5 - -valid: - <<: *DEFAULTS - invoice: valid - -another: - <<: *DEFAULTS invoice: valid diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index ffb53f938..7d532cd87 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -6,7 +6,7 @@ DEFAULTS: &DEFAULTS seller_iban: 1234 buyer: bestnames buyer_name: Jane Doe - vat_rate: 0.2 + vat_rate: 0.1 valid: <<: *DEFAULTS From 8d4b2d902b4878376425c51cb6d5833ddc4034b4 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 12 Mar 2018 11:36:07 +0200 Subject: [PATCH 40/63] Convert specs to tests #623 --- spec/models/invoice_spec.rb | 14 -------------- test/models/invoice_test.rb | 12 ++++++++++++ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/spec/models/invoice_spec.rb b/spec/models/invoice_spec.rb index d6cdc2725..23f91f741 100644 --- a/spec/models/invoice_spec.rb +++ b/spec/models/invoice_spec.rb @@ -51,20 +51,6 @@ describe Invoice do @invoice.seller_address.should == 'Paldiski mnt. 123, Tallinn' end - it 'should calculate sums correctly' do - @invoice = create(:invoice) - @invoice.vat_rate.should == BigDecimal.new('0.2') - @invoice.sum_without_vat.should == BigDecimal.new('300.0') - @invoice.vat.should == BigDecimal.new('60.0') - @invoice.sum.should == BigDecimal.new('360.0') - - ii = @invoice.items.first - ii.item_sum_without_vat.should == BigDecimal.new('150.0') - - ii = @invoice.items.last - ii.item_sum_without_vat.should == BigDecimal.new('150.0') - end - it 'should cancel overdue invoices' do create(:invoice, created_at: Time.zone.now - 35.days, due_date: Time.zone.now - 30.days) Invoice.cancel_overdue_invoices diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index 29af6fab8..f7f2900aa 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -8,4 +8,16 @@ class InvoiceTest < ActiveSupport::TestCase def test_valid assert @invoice.valid? end + + def test_calculates_subtotal + assert_equal BigDecimal('15'), @invoice.sum_without_vat + end + + def test_calculates_vat_amount + assert_equal BigDecimal('1.5'), @invoice.vat + end + + def test_calculates_total + assert_equal BigDecimal('16.5'), @invoice.sum + end end From 8de70735f628f2075d3216f5cd763026ff98ac9f Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 12 Mar 2018 12:19:06 +0200 Subject: [PATCH 41/63] Fix spec #623 --- spec/models/invoice_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/models/invoice_spec.rb b/spec/models/invoice_spec.rb index 23f91f741..14747c9c1 100644 --- a/spec/models/invoice_spec.rb +++ b/spec/models/invoice_spec.rb @@ -15,7 +15,6 @@ describe Invoice do "Invoice items is missing", "Seller iban is missing", "Seller name is missing", - "Vat prc is missing" ]) end From 6b54dd520d9c7cbc93fbdd3f12e522d23e2f629f Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 12 Mar 2018 12:19:20 +0200 Subject: [PATCH 42/63] Enhance test #623 --- .../registrar/billing/balance_top_up_test.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test/integration/registrar/billing/balance_top_up_test.rb b/test/integration/registrar/billing/balance_top_up_test.rb index c4dd1cf5d..36109e4c4 100644 --- a/test/integration/registrar/billing/balance_top_up_test.rb +++ b/test/integration/registrar/billing/balance_top_up_test.rb @@ -5,12 +5,20 @@ class BalanceTopUpTest < ActionDispatch::IntegrationTest login_as users(:api_bestnames) end - def test_registrar_balance_top_up + def test_creates_new_invoice visit registrar_invoices_url click_link_or_button 'Add deposit' - fill_in 'Amount', with: 100 - click_link_or_button 'Add' + fill_in 'Amount', with: '25.5' + Registry.instance.stub(:vat_rate, 10) do + assert_difference 'Invoice.count' do + click_link_or_button 'Add' + end + end + + invoice = Invoice.last + + assert_equal BigDecimal('28.05'), invoice.sum_cache assert_text 'Please pay the following invoice' end end From 6dfd57ee2e979b4b42deddd4a74ff6dbd8a22bb6 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 13 Mar 2018 08:47:08 +0200 Subject: [PATCH 43/63] Remove tests #623 --- test/fixtures/invoices.yml | 8 -------- .../admin/invoices/invoice_details_test.rb | 19 ------------------- .../invoices/invoice_details_test.rb | 19 ------------------- 3 files changed, 46 deletions(-) delete mode 100644 test/integration/admin/invoices/invoice_details_test.rb delete mode 100644 test/integration/registrar/invoices/invoice_details_test.rb diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index 7d532cd87..6d1e0fc73 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -30,11 +30,3 @@ outstanding: overdue: <<: *DEFAULTS due_date: <%= Date.parse '2010-07-03' %> - -with_vat: - <<: *DEFAULTS - vat_rate: 0.2 - -without_vat: - <<: *DEFAULTS - vat_rate: null diff --git a/test/integration/admin/invoices/invoice_details_test.rb b/test/integration/admin/invoices/invoice_details_test.rb deleted file mode 100644 index 8a67771ea..000000000 --- a/test/integration/admin/invoices/invoice_details_test.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'test_helper' - -class InvoiceDetailsTest < ActionDispatch::IntegrationTest - def setup - login_as users(:admin) - end - - def test_with_vat - invoice = invoices(:with_vat) - visit admin_invoice_path(invoice) - assert_selector '.total', text: 'VAT' - end - - def test_without_vat - invoice = invoices(:without_vat) - visit admin_invoice_path(invoice) - assert_no_selector '.total', text: 'VAT' - end -end diff --git a/test/integration/registrar/invoices/invoice_details_test.rb b/test/integration/registrar/invoices/invoice_details_test.rb deleted file mode 100644 index d3961faf3..000000000 --- a/test/integration/registrar/invoices/invoice_details_test.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'test_helper' - -class InvoiceDetailsTest < ActionDispatch::IntegrationTest - def setup - login_as users(:api) - end - - def test_with_vat - invoice = invoices(:with_vat) - visit registrar_invoice_path(invoice) - assert_selector '.total', text: 'VAT' - end - - def test_without_vat - invoice = invoices(:without_vat) - visit registrar_invoice_path(invoice) - assert_no_selector '.total', text: 'VAT' - end -end From a73a8a0b89116579f91be50913b77cf31a1de91a Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 13 Mar 2018 09:02:08 +0200 Subject: [PATCH 44/63] Fix VAT rate #623 --- app/models/invoice.rb | 8 ++++---- app/views/registrar/invoices/partials/_items.haml | 2 +- app/views/registrar/invoices/pdf.haml | 2 +- config/locales/en.yml | 2 +- test/models/invoice/vat_rate_test.rb | 15 ++++++++++----- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index d78c24d07..7dd8bbe67 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -32,8 +32,8 @@ class Invoice < ActiveRecord::Base validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 }, allow_nil: true - after_initialize :apply_defaults before_create :set_invoice_number + before_create :apply_default_vat_rate, unless: :vat_rate? attribute :vat_rate, ::Type::VATRate.new attr_readonly :vat_rate @@ -156,7 +156,7 @@ class Invoice < ActiveRecord::Base def vat return 0 unless vat_rate - (sum_without_vat * vat_rate).round(2) + sum_without_vat * vat_rate / 100 end def sum @@ -165,7 +165,7 @@ class Invoice < ActiveRecord::Base private - def apply_defaults - self.vat_rate = buyer.effective_vat_rate unless vat_rate + def apply_default_vat_rate + self.vat_rate = buyer.effective_vat_rate end end diff --git a/app/views/registrar/invoices/partials/_items.haml b/app/views/registrar/invoices/partials/_items.haml index 64fc71786..eef06c1ce 100644 --- a/app/views/registrar/invoices/partials/_items.haml +++ b/app/views/registrar/invoices/partials/_items.haml @@ -24,7 +24,7 @@ %td= currency(@invoice.sum_without_vat) %tr %th.no-border{colspan: 3} - %th= t('vat', rate: (@invoice.vat_rate * 100).round) + %th= t('vat', rate: number_to_percentage(@invoice.vat_rate, precision: 1)) %td= currency(@invoice.vat) %tr %th.no-border{colspan: 3} diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index 6f92c4a8a..a5f035fe0 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -242,7 +242,7 @@ %td= "#{currency(@invoice.sum_without_vat)} #{@invoice.currency}" %tr %th.no-border{colspan: 3} - %th= t('vat', rate: (@invoice.vat_rate * 100).round) + %th= t('vat', rate: number_to_percentage(@invoice.vat_rate, precision: 1)) %td= "#{currency(@invoice.vat)} #{@invoice.currency}" %tr %th.no-border{colspan: 3} diff --git a/config/locales/en.yml b/config/locales/en.yml index 19f62fe3f..73045367b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -543,7 +543,7 @@ en: invoice_number: Invoice no. seller: 'Seller' prepayment: 'Prepayment' - vat: 'VAT (%{rate}%)' + vat: 'VAT (%{rate})' unpaid: 'Unpaid' your_current_account_balance_is: 'Your current account balance is %{balance} %{currency}' billing: 'Billing' diff --git a/test/models/invoice/vat_rate_test.rb b/test/models/invoice/vat_rate_test.rb index 27bc4b705..d1e892b72 100644 --- a/test/models/invoice/vat_rate_test.rb +++ b/test/models/invoice/vat_rate_test.rb @@ -35,17 +35,22 @@ class InvoiceVATRateTest < ActiveSupport::TestCase def test_vat_rate_defaults_to_effective_vat_rate_of_a_registrar registrar = registrars(:bestnames) + invoice = @invoice.dup + invoice.vat_rate = nil + invoice.buyer = registrar + invoice.invoice_items = @invoice.invoice_items - registrar.stub(:effective_vat_rate, 55) do - invoice = Invoice.new(buyer: registrar) - assert_equal 55, invoice.vat_rate + registrar.stub(:effective_vat_rate, BigDecimal(55)) do + invoice.save! end + + assert_equal BigDecimal(55), invoice.vat_rate end def test_vat_rate_cannot_be_updated - @invoice.vat_rate = 21 + @invoice.vat_rate = BigDecimal(21) @invoice.save! @invoice.reload - refute_equal 21, @invoice.vat_rate + refute_equal BigDecimal(21), @invoice.vat_rate end end From 3d107bd198b7d566d0388cef283837ef77772a07 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 13 Mar 2018 09:39:32 +0200 Subject: [PATCH 45/63] Set association instead of foreign key #623 --- app/models/registrar.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 9e7d84a7b..9cba29faf 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -76,7 +76,7 @@ class Registrar < ActiveRecord::Base seller_url: Setting.registry_url, seller_email: Setting.registry_email, seller_contact_name: Setting.registry_invoice_contact, - buyer_id: id, + buyer: self, buyer_name: name, buyer_reg_no: reg_no, buyer_country_code: country_code, From b68fe6e3124b28f12d805247d48d91212e5c41c3 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 13 Mar 2018 11:32:15 +0200 Subject: [PATCH 46/63] Rename attributes #623 --- .../admin/bank_statements_controller.rb | 2 +- .../registrar/invoices_controller.rb | 4 +- app/models/bank_link.rb | 4 +- app/models/bank_transaction.rb | 6 +- app/models/directo.rb | 4 +- app/models/invoice.rb | 15 ++-- app/views/registrar/invoices/index.haml | 6 +- .../registrar/invoices/partials/_items.haml | 4 +- app/views/registrar/invoices/pdf.haml | 4 +- ...0437_rename_invoices_sum_cache_to_total.rb | 5 ++ db/structure.sql | 4 +- doc/models_complete.svg | 2 +- test/fixtures/invoices.yml | 3 +- .../registrar/billing/balance_top_up_test.rb | 11 +-- test/models/invoice/vat_rate_test.rb | 56 --------------- test/models/invoice_test.rb | 72 ++++++++++++++++++- 16 files changed, 110 insertions(+), 92 deletions(-) create mode 100644 db/migrate/20180313090437_rename_invoices_sum_cache_to_total.rb delete mode 100644 test/models/invoice/vat_rate_test.rb diff --git a/app/controllers/admin/bank_statements_controller.rb b/app/controllers/admin/bank_statements_controller.rb index d7b6edae2..d7bb81fb1 100644 --- a/app/controllers/admin/bank_statements_controller.rb +++ b/app/controllers/admin/bank_statements_controller.rb @@ -24,7 +24,7 @@ module Admin @invoice = Invoice.find_by(id: params[:invoice_id]) @bank_transaction = @bank_statement.bank_transactions.build( description: @invoice.to_s, - sum: @invoice.sum, + sum: @invoice.amount, reference_no: @invoice.reference_no, paid_at: Time.zone.now.to_date, currency: 'EUR' diff --git a/app/controllers/registrar/invoices_controller.rb b/app/controllers/registrar/invoices_controller.rb index ac762e712..735df91a3 100644 --- a/app/controllers/registrar/invoices_controller.rb +++ b/app/controllers/registrar/invoices_controller.rb @@ -55,8 +55,8 @@ class Registrar end def normalize_search_parameters - params[:q][:sum_cache_gteq].gsub!(',', '.') if params[:q][:sum_cache_gteq] - params[:q][:sum_cache_lteq].gsub!(',', '.') if params[:q][:sum_cache_lteq] + params[:q][:total_gteq].gsub!(',', '.') if params[:q][:total_gteq] + params[:q][:total_lteq].gsub!(',', '.') if params[:q][:total_lteq] ca_cache = params[:q][:due_date_lteq] begin diff --git a/app/models/bank_link.rb b/app/models/bank_link.rb index 24c94a771..e388a0f8b 100644 --- a/app/models/bank_link.rb +++ b/app/models/bank_link.rb @@ -29,7 +29,7 @@ class BankLink hash["VK_VERSION"] = "008" hash["VK_SND_ID"] = ENV["payments_#{type}_seller_account"] hash["VK_STAMP"] = invoice.number - hash["VK_AMOUNT"] = number_with_precision(invoice.sum_cache, :precision => 2, :separator => ".") + hash["VK_AMOUNT"] = number_with_precision(invoice.total, :precision => 2, :separator => ".") hash["VK_CURR"] = invoice.currency hash["VK_REF"] = "" hash["VK_MSG"] = invoice.order @@ -140,7 +140,7 @@ class BankLink def validate_amount source = number_with_precision(BigDecimal.new(params["VK_AMOUNT"].to_s), precision: 2, separator: ".") - target = number_with_precision(invoice.sum_cache, precision: 2, separator: ".") + target = number_with_precision(invoice.total, precision: 2, separator: ".") source == target end diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index daf6abc29..3749f92b5 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -47,7 +47,7 @@ class BankTransaction < ActiveRecord::Base return if invoice.binded? return if invoice.cancelled? - return if invoice.sum != sum + return if invoice.total != sum create_activity(registrar, invoice) end # rubocop: enable Metrics/PerceivedComplexity @@ -76,7 +76,7 @@ class BankTransaction < ActiveRecord::Base return end - if invoice.sum != sum + if invoice.total != sum errors.add(:base, I18n.t('invoice_and_transaction_sums_do_not_match')) return end @@ -88,7 +88,7 @@ class BankTransaction < ActiveRecord::Base create_account_activity( account: registrar.cash_account, invoice: invoice, - sum: invoice.sum_without_vat, + sum: invoice.subtotal, currency: currency, description: description, activity_type: AccountActivity::ADD_CREDIT diff --git a/app/models/directo.rb b/app/models/directo.rb index f3a506ff3..2b794b0e4 100644 --- a/app/models/directo.rb +++ b/app/models/directo.rb @@ -15,7 +15,7 @@ class Directo < ActiveRecord::Base group.each do |invoice| if invoice.account_activity.nil? || invoice.account_activity.bank_transaction.nil? || - invoice.account_activity.bank_transaction.sum.nil? || invoice.account_activity.bank_transaction.sum != invoice.sum_cache + invoice.account_activity.bank_transaction.sum.nil? || invoice.account_activity.bank_transaction.sum != invoice.total Rails.logger.info("[DIRECTO] Invoice #{invoice.number} has been skipped") next end @@ -34,7 +34,7 @@ class Directo < ActiveRecord::Base xml.line( "ProductID" => Setting.directo_receipt_product_name, "Quantity" => 1, - "UnitPriceWoVAT" => ActionController::Base.helpers.number_with_precision(invoice.sum_cache/(1+invoice.vat_rate), precision: 2, separator: "."), + "UnitPriceWoVAT" => ActionController::Base.helpers.number_with_precision(invoice.total/(1+invoice.vat_rate), precision: 2, separator: "."), "ProductName" => invoice.order ) } diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 7dd8bbe67..19e25d5c9 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -34,6 +34,7 @@ class Invoice < ActiveRecord::Base before_create :set_invoice_number before_create :apply_default_vat_rate, unless: :vat_rate? + before_create :save_total attribute :vat_rate, ::Type::VATRate.new attr_readonly :vat_rate @@ -54,8 +55,6 @@ class Invoice < ActiveRecord::Base false end - before_save -> { self.sum_cache = sum } - class << self def cancel_overdue_invoices STDOUT << "#{Time.zone.now.utc} - Cancelling overdue invoices\n" unless Rails.env.test? @@ -150,17 +149,13 @@ class Invoice < ActiveRecord::Base invoice_items end - def sum_without_vat + def subtotal (items.map(&:item_sum_without_vat).sum).round(2) end def vat return 0 unless vat_rate - sum_without_vat * vat_rate / 100 - end - - def sum - (sum_without_vat + vat).round(2) + subtotal * vat_rate / 100 end private @@ -168,4 +163,8 @@ class Invoice < ActiveRecord::Base def apply_default_vat_rate self.vat_rate = buyer.effective_vat_rate end + + def calculate_total + # (sum_without_vat + vat).round(2) + end end diff --git a/app/views/registrar/invoices/index.haml b/app/views/registrar/invoices/index.haml index b8c825c36..61d955708 100644 --- a/app/views/registrar/invoices/index.haml +++ b/app/views/registrar/invoices/index.haml @@ -33,11 +33,11 @@ .col-md-3 .form-group = f.label t(:minimum_total) - = f.search_field :sum_cache_gteq, class: 'form-control', placeholder: t(:minimum_total), autocomplete: 'off' + = f.search_field :total_gteq, class: 'form-control', placeholder: t(:minimum_total), autocomplete: 'off' .col-md-3 .form-group = f.label t(:maximum_total) - = f.search_field :sum_cache_lteq, class: 'form-control', placeholder: t(:maximum_total), autocomplete: 'off' + = f.search_field :total_lteq, class: 'form-control', placeholder: t(:maximum_total), autocomplete: 'off' .col-md-3{style: 'padding-top: 25px;'} %button.btn.btn-default   @@ -67,7 +67,7 @@ %td{class: 'text-danger'}= t(:unpaid) %td= l(x.due_date, format: :date_long) - %td= currency(x.sum) + %td= currency(x.total) .row .col-md-12 = paginate @invoices diff --git a/app/views/registrar/invoices/partials/_items.haml b/app/views/registrar/invoices/partials/_items.haml index eef06c1ce..560b3e1f6 100644 --- a/app/views/registrar/invoices/partials/_items.haml +++ b/app/views/registrar/invoices/partials/_items.haml @@ -21,7 +21,7 @@ %tr %th{colspan: 3} %th= t(:total_without_vat) - %td= currency(@invoice.sum_without_vat) + %td= currency(@invoice.subtotal) %tr %th.no-border{colspan: 3} %th= t('vat', rate: number_to_percentage(@invoice.vat_rate, precision: 1)) @@ -29,4 +29,4 @@ %tr %th.no-border{colspan: 3} %th= t(:total) - %td= currency(@invoice.sum) + %td= currency(@invoice.total) diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index a5f035fe0..e59ca618b 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -239,7 +239,7 @@ %tr %th{colspan: 3} %th= t(:total_without_vat) - %td= "#{currency(@invoice.sum_without_vat)} #{@invoice.currency}" + %td= "#{currency(@invoice.subtotal)} #{@invoice.currency}" %tr %th.no-border{colspan: 3} %th= t('vat', rate: number_to_percentage(@invoice.vat_rate, precision: 1)) @@ -247,7 +247,7 @@ %tr %th.no-border{colspan: 3} %th= t(:total) - %td= "#{currency(@invoice.sum)} #{@invoice.currency}" + %td= "#{currency(@invoice.total)} #{@invoice.currency}" #footer %hr diff --git a/db/migrate/20180313090437_rename_invoices_sum_cache_to_total.rb b/db/migrate/20180313090437_rename_invoices_sum_cache_to_total.rb new file mode 100644 index 000000000..43e827dfc --- /dev/null +++ b/db/migrate/20180313090437_rename_invoices_sum_cache_to_total.rb @@ -0,0 +1,5 @@ +class RenameInvoicesSumCacheToTotal < ActiveRecord::Migration + def change + rename_column :invoices, :sum_cache, :total + end +end diff --git a/db/structure.sql b/db/structure.sql index d54caef91..6df03a91a 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1036,7 +1036,7 @@ CREATE TABLE invoices ( updator_str character varying, number integer, cancelled_at timestamp without time zone, - sum_cache numeric(10,2), + total numeric(10,2), in_directo boolean DEFAULT false ); @@ -4714,3 +4714,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180309054510'); INSERT INTO schema_migrations (version) VALUES ('20180310142630'); +INSERT INTO schema_migrations (version) VALUES ('20180313090437'); + diff --git a/doc/models_complete.svg b/doc/models_complete.svg index b3194a90a..235aa433b 100644 --- a/doc/models_complete.svg +++ b/doc/models_complete.svg @@ -1651,7 +1651,7 @@ updator_str :string number :integer cancelled_at :datetime -sum_cache :decimal +total :decimal Registrar->Invoice diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index 6d1e0fc73..c395da425 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -7,6 +7,7 @@ DEFAULTS: &DEFAULTS buyer: bestnames buyer_name: Jane Doe vat_rate: 0.1 + total: 16.50 valid: <<: *DEFAULTS @@ -21,7 +22,7 @@ cancelled: paid: <<: *DEFAULTS - sum_cache: 1 + total: 1 outstanding: <<: *DEFAULTS diff --git a/test/integration/registrar/billing/balance_top_up_test.rb b/test/integration/registrar/billing/balance_top_up_test.rb index 36109e4c4..3c36b5f42 100644 --- a/test/integration/registrar/billing/balance_top_up_test.rb +++ b/test/integration/registrar/billing/balance_top_up_test.rb @@ -6,19 +6,20 @@ class BalanceTopUpTest < ActionDispatch::IntegrationTest end def test_creates_new_invoice + Setting.registry_vat_prc = 0.1 + visit registrar_invoices_url click_link_or_button 'Add deposit' fill_in 'Amount', with: '25.5' - Registry.instance.stub(:vat_rate, 10) do - assert_difference 'Invoice.count' do - click_link_or_button 'Add' - end + assert_difference 'Invoice.count' do + click_link_or_button 'Add' end invoice = Invoice.last - assert_equal BigDecimal('28.05'), invoice.sum_cache + assert_equal BigDecimal(10), invoice.vat_rate + assert_equal BigDecimal('28.05'), invoice.total assert_text 'Please pay the following invoice' end end diff --git a/test/models/invoice/vat_rate_test.rb b/test/models/invoice/vat_rate_test.rb deleted file mode 100644 index d1e892b72..000000000 --- a/test/models/invoice/vat_rate_test.rb +++ /dev/null @@ -1,56 +0,0 @@ -require 'test_helper' - -class InvoiceVATRateTest < ActiveSupport::TestCase - def setup - @invoice = invoices(:valid) - end - - def test_optional_vat_rate - @invoice.vat_rate = nil - assert @invoice.valid? - end - - def test_vat_rate_validation - @invoice.vat_rate = -1 - assert @invoice.invalid? - - @invoice.vat_rate = 0 - assert @invoice.valid? - - @invoice.vat_rate = 99.9 - assert @invoice.valid? - - @invoice.vat_rate = 100 - assert @invoice.invalid? - end - - def test_serializes_and_deserializes_vat_rate - invoice = @invoice.dup - invoice.invoice_items = @invoice.invoice_items - invoice.vat_rate = BigDecimal('25.5') - invoice.save! - invoice.reload - assert_equal BigDecimal('25.5'), invoice.vat_rate - end - - def test_vat_rate_defaults_to_effective_vat_rate_of_a_registrar - registrar = registrars(:bestnames) - invoice = @invoice.dup - invoice.vat_rate = nil - invoice.buyer = registrar - invoice.invoice_items = @invoice.invoice_items - - registrar.stub(:effective_vat_rate, BigDecimal(55)) do - invoice.save! - end - - assert_equal BigDecimal(55), invoice.vat_rate - end - - def test_vat_rate_cannot_be_updated - @invoice.vat_rate = BigDecimal(21) - @invoice.save! - @invoice.reload - refute_equal BigDecimal(21), @invoice.vat_rate - end -end diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index f7f2900aa..fb7e66821 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -9,15 +9,81 @@ class InvoiceTest < ActiveSupport::TestCase assert @invoice.valid? end - def test_calculates_subtotal - assert_equal BigDecimal('15'), @invoice.sum_without_vat + def test_optional_vat_rate + @invoice.vat_rate = nil + assert @invoice.valid? + end + + def test_vat_rate_validation + @invoice.vat_rate = -1 + assert @invoice.invalid? + + @invoice.vat_rate = 0 + assert @invoice.valid? + + @invoice.vat_rate = 99.9 + assert @invoice.valid? + + @invoice.vat_rate = 100 + assert @invoice.invalid? + end + + def test_serializes_and_deserializes_vat_rate + invoice = @invoice.dup + invoice.invoice_items = @invoice.invoice_items + invoice.vat_rate = BigDecimal('25.5') + invoice.save! + invoice.reload + assert_equal BigDecimal('25.5'), invoice.vat_rate + end + + def test_vat_rate_defaults_to_effective_vat_rate_of_a_registrar + registrar = registrars(:bestnames) + invoice = @invoice.dup + invoice.vat_rate = nil + invoice.buyer = registrar + invoice.invoice_items = @invoice.invoice_items + + registrar.stub(:effective_vat_rate, BigDecimal(55)) do + invoice.save! + end + + assert_equal BigDecimal(55), invoice.vat_rate + end + + def test_vat_rate_cannot_be_updated + @invoice.vat_rate = BigDecimal(21) + @invoice.save! + @invoice.reload + refute_equal BigDecimal(21), @invoice.vat_rate end def test_calculates_vat_amount assert_equal BigDecimal('1.5'), @invoice.vat end + def test_vat_amount_is_zero_when_vat_rate_is_blank + @invoice.vat_rate = nil + assert_equal 0, @invoice.vat + end + + def test_calculates_subtotal + line_item = InvoiceItem.new + invoice = Invoice.new(invoice_items: [line_item, line_item]) + + line_item.stub(:item_sum_without_vat, BigDecimal('2.5')) do + assert_equal BigDecimal(5), invoice.subtotal + end + end + def test_calculates_total - assert_equal BigDecimal('16.5'), @invoice.sum + line_item = InvoiceItem.new + invoice = Invoice.new + invoice.vat_rate = 10 + invoice.invoice_items = [line_item, line_item] + + line_item.stub(:item_sum_without_vat, BigDecimal('2.5')) do + assert_equal BigDecimal('5.50'), invoice.total + end end end From b3c4f794d0ac48cff609e02ce03c333ebdd6c3be Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 13 Mar 2018 11:32:35 +0200 Subject: [PATCH 47/63] Reformat #623 --- test/test_helper.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 64cef7e0f..868cd39c0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,7 +13,6 @@ require 'capybara/minitest' require 'webmock/minitest' Setting.address_processing = false - Setting.registry_country_code = 'US' class ActiveSupport::TestCase From 3ed6416e5064dca0a52c72c106adfebfef35cd86 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 13 Mar 2018 11:32:44 +0200 Subject: [PATCH 48/63] Fix test #623 --- test/integration/admin/registrars/new_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/integration/admin/registrars/new_test.rb b/test/integration/admin/registrars/new_test.rb index 61a7a43be..88ff76288 100644 --- a/test/integration/admin/registrars/new_test.rb +++ b/test/integration/admin/registrars/new_test.rb @@ -9,9 +9,10 @@ class AdminAreaNewRegistrarTest < ActionDispatch::IntegrationTest visit admin_registrars_url click_link_or_button 'New registrar' - fill_in 'Name', with: 'Brand new names' + fill_in 'Name', with: 'Brand new name' fill_in 'Reg no', with: '55555555' fill_in 'Contact e-mail', with: 'test@example.com' + select 'United States', from: 'Country' fill_in 'Accounting customer code', with: 'test' fill_in 'Code', with: 'test' From beba81bc6dbef7b249caa5f248a25922f156d679 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 13 Mar 2018 11:39:18 +0200 Subject: [PATCH 49/63] Introduce helper method #623 --- app/helpers/application_helper.rb | 4 ++++ app/views/admin/registrars/_billing.html.erb | 2 +- app/views/registrar/invoices/partials/_items.haml | 2 +- app/views/registrar/invoices/pdf.haml | 2 +- test/integration/admin/registrars/show_registrar_test.rb | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8203a630f..f0e42344b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -89,4 +89,8 @@ module ApplicationHelper types.delete('ddoc') ".#{types.join(',.')}" end + + def vat_rate(value) + number_to_percentage(value, precision: 1) + end end diff --git a/app/views/admin/registrars/_billing.html.erb b/app/views/admin/registrars/_billing.html.erb index 7ba778d08..929487afe 100644 --- a/app/views/admin/registrars/_billing.html.erb +++ b/app/views/admin/registrars/_billing.html.erb @@ -8,7 +8,7 @@
<%= registrar.vat_no %>
<%= Registrar.human_attribute_name :vat_rate %>
-
<%= number_to_percentage registrar.vat_rate, precision: 1 %>
+
<%= vat_rate registrar.vat_rate %>
<%= Registrar.human_attribute_name :accounting_customer_code %>
<%= registrar.accounting_customer_code %>
diff --git a/app/views/registrar/invoices/partials/_items.haml b/app/views/registrar/invoices/partials/_items.haml index 560b3e1f6..2d485f0ab 100644 --- a/app/views/registrar/invoices/partials/_items.haml +++ b/app/views/registrar/invoices/partials/_items.haml @@ -24,7 +24,7 @@ %td= currency(@invoice.subtotal) %tr %th.no-border{colspan: 3} - %th= t('vat', rate: number_to_percentage(@invoice.vat_rate, precision: 1)) + %th= t('vat', rate: vat_rate(@invoice.vat_rate)) %td= currency(@invoice.vat) %tr %th.no-border{colspan: 3} diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index e59ca618b..fcc68f128 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -242,7 +242,7 @@ %td= "#{currency(@invoice.subtotal)} #{@invoice.currency}" %tr %th.no-border{colspan: 3} - %th= t('vat', rate: number_to_percentage(@invoice.vat_rate, precision: 1)) + %th= t('vat', rate: vat_rate(@invoice.vat_rate)) %td= "#{currency(@invoice.vat)} #{@invoice.currency}" %tr %th.no-border{colspan: 3} diff --git a/test/integration/admin/registrars/show_registrar_test.rb b/test/integration/admin/registrars/show_registrar_test.rb index d4ed927bd..94252705e 100644 --- a/test/integration/admin/registrars/show_registrar_test.rb +++ b/test/integration/admin/registrars/show_registrar_test.rb @@ -22,6 +22,6 @@ class ShowRegistrarTest < ActionDispatch::IntegrationTest end def test_vat_rate - assert_text number_to_percentage(@registrar.vat_rate, precision: 1) + assert_text vat_rate(@registrar.vat_rate) end end From a2c919b0fd1cc993797d6eb92b243b9196e38873 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 13 Mar 2018 11:45:04 +0200 Subject: [PATCH 50/63] Use standard translation #623 --- app/views/registrar/invoices/partials/_items.haml | 4 ++-- app/views/registrar/invoices/pdf.haml | 4 ++-- config/locales/en.yml | 2 -- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/app/views/registrar/invoices/partials/_items.haml b/app/views/registrar/invoices/partials/_items.haml index 2d485f0ab..3bf4f9a84 100644 --- a/app/views/registrar/invoices/partials/_items.haml +++ b/app/views/registrar/invoices/partials/_items.haml @@ -20,11 +20,11 @@ %tfoot %tr %th{colspan: 3} - %th= t(:total_without_vat) + %th= Invoice.human_attribute_name :subtotal %td= currency(@invoice.subtotal) %tr %th.no-border{colspan: 3} - %th= t('vat', rate: vat_rate(@invoice.vat_rate)) + %th= "VAT #{vat_rate(@invoice.vat_rate)}" %td= currency(@invoice.vat) %tr %th.no-border{colspan: 3} diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index fcc68f128..9a2dd80f2 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -238,11 +238,11 @@ %tfoot %tr %th{colspan: 3} - %th= t(:total_without_vat) + %th= Invoice.human_attribute_name :subtotal %td= "#{currency(@invoice.subtotal)} #{@invoice.currency}" %tr %th.no-border{colspan: 3} - %th= t('vat', rate: vat_rate(@invoice.vat_rate)) + %th= "VAT #{vat_rate(@invoice.vat_rate)}" %td= "#{currency(@invoice.vat)} #{@invoice.currency}" %tr %th.no-border{colspan: 3} diff --git a/config/locales/en.yml b/config/locales/en.yml index 73045367b..2ca4a207b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -543,7 +543,6 @@ en: invoice_number: Invoice no. seller: 'Seller' prepayment: 'Prepayment' - vat: 'VAT (%{rate})' unpaid: 'Unpaid' your_current_account_balance_is: 'Your current account balance is %{balance} %{currency}' billing: 'Billing' @@ -561,7 +560,6 @@ en: unit: 'Unit' price: 'Price' total: 'Total' - total_without_vat: 'Total without VAT' paid_at: 'Paid at' invoice: 'Invoice' bank_statements: 'Bank statements' From ef6f4825288956d55e0633ff074338402822fa7d Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 13 Mar 2018 14:32:17 +0200 Subject: [PATCH 51/63] Format inline #623 --- app/helpers/application_helper.rb | 4 ---- app/views/admin/registrars/_billing.html.erb | 2 +- app/views/registrar/invoices/partials/_items.haml | 8 ++++---- app/views/registrar/invoices/pdf.haml | 8 ++++---- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f0e42344b..8203a630f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -89,8 +89,4 @@ module ApplicationHelper types.delete('ddoc') ".#{types.join(',.')}" end - - def vat_rate(value) - number_to_percentage(value, precision: 1) - end end diff --git a/app/views/admin/registrars/_billing.html.erb b/app/views/admin/registrars/_billing.html.erb index 929487afe..7a7e49fea 100644 --- a/app/views/admin/registrars/_billing.html.erb +++ b/app/views/admin/registrars/_billing.html.erb @@ -8,7 +8,7 @@
<%= registrar.vat_no %>
<%= Registrar.human_attribute_name :vat_rate %>
-
<%= vat_rate registrar.vat_rate %>
+
<%= number_to_percentage(registrar.vat_rate, precision: 1) %>
<%= Registrar.human_attribute_name :accounting_customer_code %>
<%= registrar.accounting_customer_code %>
diff --git a/app/views/registrar/invoices/partials/_items.haml b/app/views/registrar/invoices/partials/_items.haml index 3bf4f9a84..ebd94be53 100644 --- a/app/views/registrar/invoices/partials/_items.haml +++ b/app/views/registrar/invoices/partials/_items.haml @@ -21,12 +21,12 @@ %tr %th{colspan: 3} %th= Invoice.human_attribute_name :subtotal - %td= currency(@invoice.subtotal) + %td= number_to_currency @invoice.subtotal %tr %th.no-border{colspan: 3} - %th= "VAT #{vat_rate(@invoice.vat_rate)}" - %td= currency(@invoice.vat) + %th= "VAT #{number_to_percentage(@invoice.vat_rate, precision: 1)}" + %td= number_to_currency @invoice.vat_amount %tr %th.no-border{colspan: 3} %th= t(:total) - %td= currency(@invoice.total) + %td= number_to_currency @invoice.total diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index 9a2dd80f2..4f3b499df 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -239,15 +239,15 @@ %tr %th{colspan: 3} %th= Invoice.human_attribute_name :subtotal - %td= "#{currency(@invoice.subtotal)} #{@invoice.currency}" + %td= number_to_currency @invoice.subtotal %tr %th.no-border{colspan: 3} - %th= "VAT #{vat_rate(@invoice.vat_rate)}" - %td= "#{currency(@invoice.vat)} #{@invoice.currency}" + %th= "VAT #{number_to_percentage(@invoice.vat_rate, precision: 1)}" + %td= number_to_currency @invoice.vat_amount %tr %th.no-border{colspan: 3} %th= t(:total) - %td= "#{currency(@invoice.total)} #{@invoice.currency}" + %td= number_to_currency @invoice.total #footer %hr From 9a4380fd96c3e003c5ff3cba6dd32c12788a9813 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 13 Mar 2018 14:32:38 +0200 Subject: [PATCH 52/63] Fix invoice total calculation #623 --- app/models/invoice.rb | 12 ++++++++---- test/models/invoice_test.rb | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 19e25d5c9..a5c3f3f85 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -34,7 +34,7 @@ class Invoice < ActiveRecord::Base before_create :set_invoice_number before_create :apply_default_vat_rate, unless: :vat_rate? - before_create :save_total + before_create :calculate_total, unless: :total? attribute :vat_rate, ::Type::VATRate.new attr_readonly :vat_rate @@ -150,14 +150,18 @@ class Invoice < ActiveRecord::Base end def subtotal - (items.map(&:item_sum_without_vat).sum).round(2) + invoice_items.collect { |line_item| line_item.item_sum_without_vat }.reduce(:+) end - def vat + def vat_amount return 0 unless vat_rate subtotal * vat_rate / 100 end + def total + calculate_total unless total? + end + private def apply_default_vat_rate @@ -165,6 +169,6 @@ class Invoice < ActiveRecord::Base end def calculate_total - # (sum_without_vat + vat).round(2) + self.total = subtotal + vat_amount end end diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index fb7e66821..9222f83b8 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -59,12 +59,12 @@ class InvoiceTest < ActiveSupport::TestCase end def test_calculates_vat_amount - assert_equal BigDecimal('1.5'), @invoice.vat + assert_equal BigDecimal('1.5'), @invoice.vat_amount end def test_vat_amount_is_zero_when_vat_rate_is_blank @invoice.vat_rate = nil - assert_equal 0, @invoice.vat + assert_equal 0, @invoice.vat_amount end def test_calculates_subtotal From fb35ec5cbb6dbc2ea9848733923447165c591ec8 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Tue, 13 Mar 2018 14:49:36 +0200 Subject: [PATCH 53/63] Require `invoices.total` #623 --- .../20180313124751_change_invoices_total_to_not_null.rb | 5 +++++ db/structure.sql | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180313124751_change_invoices_total_to_not_null.rb diff --git a/db/migrate/20180313124751_change_invoices_total_to_not_null.rb b/db/migrate/20180313124751_change_invoices_total_to_not_null.rb new file mode 100644 index 000000000..fb31f4084 --- /dev/null +++ b/db/migrate/20180313124751_change_invoices_total_to_not_null.rb @@ -0,0 +1,5 @@ +class ChangeInvoicesTotalToNotNull < ActiveRecord::Migration + def change + change_column_null :invoices, :total, false + end +end diff --git a/db/structure.sql b/db/structure.sql index 6df03a91a..32a7b3891 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1036,7 +1036,7 @@ CREATE TABLE invoices ( updator_str character varying, number integer, cancelled_at timestamp without time zone, - total numeric(10,2), + total numeric(10,2) NOT NULL, in_directo boolean DEFAULT false ); @@ -4716,3 +4716,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180310142630'); INSERT INTO schema_migrations (version) VALUES ('20180313090437'); +INSERT INTO schema_migrations (version) VALUES ('20180313124751'); + From e1b0f3044e875888158650d4e630573102070cee Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 14 Mar 2018 12:19:44 +0200 Subject: [PATCH 54/63] Fix invoice #623 --- app/models/invoice.rb | 1 + test/models/invoice_test.rb | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index a5c3f3f85..edd85108d 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -160,6 +160,7 @@ class Invoice < ActiveRecord::Base def total calculate_total unless total? + read_attribute(:total) end private diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index 9222f83b8..c9e1f9f11 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -76,6 +76,10 @@ class InvoiceTest < ActiveSupport::TestCase end end + def test_returns_persisted_total + assert_equal BigDecimal('16.50'), @invoice.total + end + def test_calculates_total line_item = InvoiceItem.new invoice = Invoice.new From 094c06bee712c9bac4021c2dd732179df90b6f74 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 14 Mar 2018 12:58:49 +0200 Subject: [PATCH 55/63] Fix directo #623 --- app/models/directo.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/directo.rb b/app/models/directo.rb index 2b794b0e4..53fa9b36f 100644 --- a/app/models/directo.rb +++ b/app/models/directo.rb @@ -34,7 +34,7 @@ class Directo < ActiveRecord::Base xml.line( "ProductID" => Setting.directo_receipt_product_name, "Quantity" => 1, - "UnitPriceWoVAT" => ActionController::Base.helpers.number_with_precision(invoice.total/(1+invoice.vat_rate), precision: 2, separator: "."), + "UnitPriceWoVAT" => ActionController::Base.helpers.number_with_precision(invoice.subtotal, precision: 2, separator: "."), "ProductName" => invoice.order ) } From 1107dc67b87607ef3e56069a294c239433fe42c2 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 14 Mar 2018 14:30:20 +0200 Subject: [PATCH 56/63] Simplify method #623 --- app/models/invoice.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index edd85108d..8381ccbac 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -150,7 +150,7 @@ class Invoice < ActiveRecord::Base end def subtotal - invoice_items.collect { |line_item| line_item.item_sum_without_vat }.reduce(:+) + invoice_items.map(&:item_sum_without_vat).reduce(:+) end def vat_amount From 169c67839e37a77e6a8dc584f31ab54e6bd46647 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 14 Mar 2018 14:48:20 +0200 Subject: [PATCH 57/63] Add "VATCode" and "TotalVAT" fields to Directo #623 --- app/models/directo.rb | 6 ++++-- app/models/invoice.rb | 5 +++++ .../20180314122722_add_invoices_buyer_vat_no.rb | 5 +++++ db/structure.sql | 5 ++++- test/models/invoice_test.rb | 16 ++++++++++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20180314122722_add_invoices_buyer_vat_no.rb diff --git a/app/models/directo.rb b/app/models/directo.rb index 53fa9b36f..9352c9356 100644 --- a/app/models/directo.rb +++ b/app/models/directo.rb @@ -29,12 +29,14 @@ class Directo < ActiveRecord::Base "InvoiceDate" => invoice.created_at.strftime("%Y-%m-%dT%H:%M:%S"), "PaymentTerm" => Setting.directo_receipt_payment_term, "Currency" => invoice.currency, - "CustomerCode"=> invoice.buyer.accounting_customer_code + "CustomerCode"=> invoice.buyer.accounting_customer_code, + 'TotalVAT' => ActionController::Base.helpers.number_with_precision(invoice.vat_amount, precision: 2, separator: '.') ){ xml.line( "ProductID" => Setting.directo_receipt_product_name, "Quantity" => 1, - "UnitPriceWoVAT" => ActionController::Base.helpers.number_with_precision(invoice.subtotal, precision: 2, separator: "."), + "UnitPriceWoVAT" => ActionController::Base.helpers.number_with_precision(invoice.subtotal, precision: 2, separator: '.'), + 'VATCode' => invoice.buyer_vat_no, "ProductName" => invoice.order ) } diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 8381ccbac..a2e469ca0 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -35,6 +35,7 @@ class Invoice < ActiveRecord::Base before_create :set_invoice_number before_create :apply_default_vat_rate, unless: :vat_rate? before_create :calculate_total, unless: :total? + before_create :apply_default_buyer_vat_no, unless: :buyer_vat_no? attribute :vat_rate, ::Type::VATRate.new attr_readonly :vat_rate @@ -169,6 +170,10 @@ class Invoice < ActiveRecord::Base self.vat_rate = buyer.effective_vat_rate end + def apply_default_buyer_vat_no + self.buyer_vat_no = buyer.vat_no + end + def calculate_total self.total = subtotal + vat_amount end diff --git a/db/migrate/20180314122722_add_invoices_buyer_vat_no.rb b/db/migrate/20180314122722_add_invoices_buyer_vat_no.rb new file mode 100644 index 000000000..83f9f9abd --- /dev/null +++ b/db/migrate/20180314122722_add_invoices_buyer_vat_no.rb @@ -0,0 +1,5 @@ +class AddInvoicesBuyerVatNo < ActiveRecord::Migration + def change + add_column :invoices, :buyer_vat_no, :string + end +end diff --git a/db/structure.sql b/db/structure.sql index 32a7b3891..96a9637fe 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1037,7 +1037,8 @@ CREATE TABLE invoices ( number integer, cancelled_at timestamp without time zone, total numeric(10,2) NOT NULL, - in_directo boolean DEFAULT false + in_directo boolean DEFAULT false, + buyer_vat_no character varying ); @@ -4718,3 +4719,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180313090437'); INSERT INTO schema_migrations (version) VALUES ('20180313124751'); +INSERT INTO schema_migrations (version) VALUES ('20180314122722'); + diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index c9e1f9f11..5ae54abd0 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -90,4 +90,20 @@ class InvoiceTest < ActiveSupport::TestCase assert_equal BigDecimal('5.50'), invoice.total end end + + def test_valid_without_buyer_vat_no + @invoice.buyer_vat_no = '' + assert @invoice.valid? + end + + def test_buyer_vat_no_is_taken_from_registrar_by_default + registrar = registrars(:bestnames) + registrar.vat_no = 'US1234' + invoice = @invoice.dup + invoice.buyer_vat_no = nil + invoice.buyer = registrar + invoice.invoice_items = @invoice.invoice_items + invoice.save! + assert_equal 'US1234', invoice.buyer_vat_no + end end From f8088df79800a6947403eeb03413a22bd1c569b0 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 14 Mar 2018 23:26:54 +0200 Subject: [PATCH 58/63] Fix tests #623 --- test/models/registrar/vat_test.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/test/models/registrar/vat_test.rb b/test/models/registrar/vat_test.rb index 802a0d987..123eed671 100644 --- a/test/models/registrar/vat_test.rb +++ b/test/models/registrar/vat_test.rb @@ -13,7 +13,16 @@ class RegistrarVATTest < ActiveSupport::TestCase assert @registrar.valid? end - def test_vat_is_not_applied_when_registrar_is_local_vat_payer + def test_apply_vat_rate_from_registry_when_registrar_is_local_vat_payer + Setting.registry_country_code = 'US' + @registrar.country_code = 'US' + + Registry.instance.stub(:vat_rate, BigDecimal('5.5')) do + assert_equal BigDecimal('5.5'), @registrar.effective_vat_rate + end + end + + def test_require_no_vat_rate_when_registrar_is_local_vat_payer @registrar.vat_rate = 1 assert @registrar.invalid? @@ -21,7 +30,14 @@ class RegistrarVATTest < ActiveSupport::TestCase assert @registrar.valid? end - def test_requires_vat_rate_when_registrar_is_foreign_vat_payer_and_vat_no_is_absent + def test_apply_vat_rate_from_registrar_when_registrar_is_foreign_vat_payer + Setting.registry_country_code = 'US' + @registrar.country_code = 'DE' + @registrar.vat_rate = BigDecimal('5.6') + assert_equal BigDecimal('5.6'), @registrar.effective_vat_rate + end + + def test_require_vat_rate_when_registrar_is_foreign_vat_payer_and_vat_no_is_absent @registrar.country_code = 'DE' @registrar.vat_no = '' @@ -30,7 +46,7 @@ class RegistrarVATTest < ActiveSupport::TestCase assert @registrar.errors.added?(:vat_rate, :blank) end - def test_vat_is_not_applied_when_registrar_is_foreign_vat_payer_and_vat_no_is_present + def test_require_no_vat_rate_when_registrar_is_foreign_vat_payer_and_vat_no_is_present @registrar.country_code = 'DE' @registrar.vat_no = 'valid' From 69d0a2011512db57238308d7c2906ef8358534eb Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Wed, 14 Mar 2018 23:53:53 +0200 Subject: [PATCH 59/63] Fix tests #623 --- test/fixtures/registrars.yml | 11 ++++++++ .../admin/registrars/details_test.rb | 18 +++++++++++++ .../admin/registrars/show_registrar_test.rb | 27 ------------------- test/models/registrar/vat_test.rb | 3 +++ 4 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 test/integration/admin/registrars/details_test.rb delete mode 100644 test/integration/admin/registrars/show_registrar_test.rb diff --git a/test/fixtures/registrars.yml b/test/fixtures/registrars.yml index a0faee8b4..e54d79fad 100644 --- a/test/fixtures/registrars.yml +++ b/test/fixtures/registrars.yml @@ -30,3 +30,14 @@ not_in_use: country_code: US accounting_customer_code: any language: en + +complete: + name: Complete Names + reg_no: 123456 + code: completenames + email: completenames@example.com + country_code: US + accounting_customer_code: US0001 + language: en + vat_no: US12345 + vat_rate: 0.05 diff --git a/test/integration/admin/registrars/details_test.rb b/test/integration/admin/registrars/details_test.rb new file mode 100644 index 000000000..3ea7fa187 --- /dev/null +++ b/test/integration/admin/registrars/details_test.rb @@ -0,0 +1,18 @@ +require 'test_helper' + +class AdminAreaRegistrarDetailsTest < ActionDispatch::IntegrationTest + include ActionView::Helpers::NumberHelper + + def setup + login_as users(:admin) + @registrar = registrars(:complete) + end + + def test_registrar_details + visit admin_registrar_path(@registrar) + assert_text 'Accounting customer code US0001' + assert_text 'VAT number US12345' + assert_text 'VAT rate 5.0%' + assert_text 'Language English' + end +end diff --git a/test/integration/admin/registrars/show_registrar_test.rb b/test/integration/admin/registrars/show_registrar_test.rb deleted file mode 100644 index 94252705e..000000000 --- a/test/integration/admin/registrars/show_registrar_test.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'test_helper' - -class ShowRegistrarTest < ActionDispatch::IntegrationTest - include ActionView::Helpers::NumberHelper - - def setup - login_as users(:admin) - @registrar = registrars(:bestnames) - visit admin_registrar_path(@registrar) - end - - def test_accounting_customer_code - assert_text 'bestnames' - end - - def test_language - assert_text 'Language English' - end - - def test_vat_no - assert_text 'US12345' - end - - def test_vat_rate - assert_text vat_rate(@registrar.vat_rate) - end -end diff --git a/test/models/registrar/vat_test.rb b/test/models/registrar/vat_test.rb index 123eed671..6ba16f8e4 100644 --- a/test/models/registrar/vat_test.rb +++ b/test/models/registrar/vat_test.rb @@ -44,6 +44,9 @@ class RegistrarVATTest < ActiveSupport::TestCase @registrar.vat_rate = '' assert @registrar.invalid? assert @registrar.errors.added?(:vat_rate, :blank) + + @registrar.vat_rate = 5 + assert @registrar.valid? end def test_require_no_vat_rate_when_registrar_is_foreign_vat_payer_and_vat_no_is_present From bee233e00bc7c7c87155c9060baeb9ea1dae998e Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 12 Apr 2018 12:06:42 +0300 Subject: [PATCH 60/63] Fix incorrect method #623 --- app/controllers/admin/bank_statements_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/admin/bank_statements_controller.rb b/app/controllers/admin/bank_statements_controller.rb index d7bb81fb1..a70387317 100644 --- a/app/controllers/admin/bank_statements_controller.rb +++ b/app/controllers/admin/bank_statements_controller.rb @@ -24,7 +24,7 @@ module Admin @invoice = Invoice.find_by(id: params[:invoice_id]) @bank_transaction = @bank_statement.bank_transactions.build( description: @invoice.to_s, - sum: @invoice.amount, + sum: @invoice.total, reference_no: @invoice.reference_no, paid_at: Time.zone.now.to_date, currency: 'EUR' From a8046ce9cffa8592a4fd198b89e7061dd4cd545b Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 12 Apr 2018 17:27:39 +0300 Subject: [PATCH 61/63] Remove global setting for tests #623 --- test/integration/admin/registrars/new_test.rb | 7 +++++++ test/test_helper.rb | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/test/integration/admin/registrars/new_test.rb b/test/integration/admin/registrars/new_test.rb index 47b7aded6..cadb513e9 100644 --- a/test/integration/admin/registrars/new_test.rb +++ b/test/integration/admin/registrars/new_test.rb @@ -3,6 +3,12 @@ require 'test_helper' class AdminAreaNewRegistrarTest < ActionDispatch::IntegrationTest setup do login_as users(:admin) + @original_registry_country_code = Setting.registry_country_code + Setting.registry_country_code = 'DE' + end + + teardown do + Setting.registry_country_code = @original_registry_country_code end def test_new_registrar_creation_with_required_params @@ -12,6 +18,7 @@ class AdminAreaNewRegistrarTest < ActionDispatch::IntegrationTest fill_in 'Name', with: 'Brand new names' fill_in 'Reg no', with: '55555555' fill_in 'Contact e-mail', with: 'test@example.com' + select 'Germany', from: 'Country' fill_in 'Accounting customer code', with: 'test' fill_in 'Code', with: 'test' diff --git a/test/test_helper.rb b/test/test_helper.rb index e319656be..931a0d21b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,7 +14,6 @@ require 'webmock/minitest' require 'support/rails5_assetions' # Remove once upgraded to Rails 5 Setting.address_processing = false -Setting.registry_country_code = 'US' class ActiveSupport::TestCase include FactoryBot::Syntax::Methods From 963f6f87cf9338c24f6f5021e5f01bc395218ce3 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 12 Apr 2018 17:31:04 +0300 Subject: [PATCH 62/63] Use setup block instead of method in tests #623 --- test/integration/admin/mail_templates/new_test.rb | 2 +- test/integration/admin/registrars/details_test.rb | 2 +- test/integration/registrar/billing/balance_top_up_test.rb | 2 +- test/models/invoice_test.rb | 2 +- test/models/registrar/vat_test.rb | 2 +- test/models/registry_test.rb | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/integration/admin/mail_templates/new_test.rb b/test/integration/admin/mail_templates/new_test.rb index c7b9dff41..42d50ef43 100644 --- a/test/integration/admin/mail_templates/new_test.rb +++ b/test/integration/admin/mail_templates/new_test.rb @@ -1,7 +1,7 @@ require 'test_helper' class AdminAreaNewMailTemplateTest < ActionDispatch::IntegrationTest - def setup + setup do login_as users(:admin) end diff --git a/test/integration/admin/registrars/details_test.rb b/test/integration/admin/registrars/details_test.rb index 3ea7fa187..3c2f39072 100644 --- a/test/integration/admin/registrars/details_test.rb +++ b/test/integration/admin/registrars/details_test.rb @@ -3,7 +3,7 @@ require 'test_helper' class AdminAreaRegistrarDetailsTest < ActionDispatch::IntegrationTest include ActionView::Helpers::NumberHelper - def setup + setup do login_as users(:admin) @registrar = registrars(:complete) end diff --git a/test/integration/registrar/billing/balance_top_up_test.rb b/test/integration/registrar/billing/balance_top_up_test.rb index 3c36b5f42..c77c9ff37 100644 --- a/test/integration/registrar/billing/balance_top_up_test.rb +++ b/test/integration/registrar/billing/balance_top_up_test.rb @@ -1,7 +1,7 @@ require 'test_helper' class BalanceTopUpTest < ActionDispatch::IntegrationTest - def setup + setup do login_as users(:api_bestnames) end diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index 5ae54abd0..45ca57417 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -1,7 +1,7 @@ require 'test_helper' class InvoiceTest < ActiveSupport::TestCase - def setup + setup do @invoice = invoices(:valid) end diff --git a/test/models/registrar/vat_test.rb b/test/models/registrar/vat_test.rb index 6ba16f8e4..2dc5de3ce 100644 --- a/test/models/registrar/vat_test.rb +++ b/test/models/registrar/vat_test.rb @@ -1,7 +1,7 @@ require 'test_helper' class RegistrarVATTest < ActiveSupport::TestCase - def setup + setup do @registrar = registrars(:bestnames) end diff --git a/test/models/registry_test.rb b/test/models/registry_test.rb index 70bc037c6..90a603e15 100644 --- a/test/models/registry_test.rb +++ b/test/models/registry_test.rb @@ -1,7 +1,7 @@ require 'test_helper' class RegistryTest < ActiveSupport::TestCase - def setup + setup do @registry = Registry.send(:new) end From 5061962c40836fabb7ca4ef1d8056b2ab7b4236d Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 12 Apr 2018 17:48:33 +0300 Subject: [PATCH 63/63] Fix test #623 --- test/integration/admin/registrars/new_test.rb | 8 +------- test/test_helper.rb | 1 + 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/test/integration/admin/registrars/new_test.rb b/test/integration/admin/registrars/new_test.rb index cadb513e9..d2efc31a2 100644 --- a/test/integration/admin/registrars/new_test.rb +++ b/test/integration/admin/registrars/new_test.rb @@ -3,12 +3,6 @@ require 'test_helper' class AdminAreaNewRegistrarTest < ActionDispatch::IntegrationTest setup do login_as users(:admin) - @original_registry_country_code = Setting.registry_country_code - Setting.registry_country_code = 'DE' - end - - teardown do - Setting.registry_country_code = @original_registry_country_code end def test_new_registrar_creation_with_required_params @@ -18,7 +12,7 @@ class AdminAreaNewRegistrarTest < ActionDispatch::IntegrationTest fill_in 'Name', with: 'Brand new names' fill_in 'Reg no', with: '55555555' fill_in 'Contact e-mail', with: 'test@example.com' - select 'Germany', from: 'Country' + select 'United States', from: 'Country' fill_in 'Accounting customer code', with: 'test' fill_in 'Code', with: 'test' diff --git a/test/test_helper.rb b/test/test_helper.rb index 931a0d21b..e319656be 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,6 +14,7 @@ require 'webmock/minitest' require 'support/rails5_assetions' # Remove once upgraded to Rails 5 Setting.address_processing = false +Setting.registry_country_code = 'US' class ActiveSupport::TestCase include FactoryBot::Syntax::Methods