From d62e55e6a179a98a269644fbdcc3ec79774fac15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 10 Sep 2020 12:00:13 +0300 Subject: [PATCH] Change Invoice::Item price scale to 3 places --- app/models/invoice.rb | 7 +++---- app/models/invoice_item.rb | 6 +++--- app/models/registrar.rb | 3 +-- ...ange_invoice_item_price_scale_to_three_places.rb | 5 +++++ db/structure.sql | 13 ++++++------- 5 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 db/migrate/20200910085157_change_invoice_item_price_scale_to_three_places.rb diff --git a/app/models/invoice.rb b/app/models/invoice.rb index adaad87d6..2f4be2e60 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -82,7 +82,7 @@ class Invoice < ApplicationRecord end def vat_amount - subtotal * vat_rate / 100 + (subtotal * vat_rate / 100) end def total @@ -118,9 +118,8 @@ class Invoice < ApplicationRecord vat = VatRateCalculator.new(registrar: registrar_user).calculate wo_vat = (transaction.sum / (1 + (vat / 100))) - registrar_user.issue_prepayment_invoice(wo_vat, 'Direct top-up via bank transfer', - payable: false, total: transaction.sum) + payable: false) end private @@ -130,6 +129,6 @@ class Invoice < ApplicationRecord end def calculate_total - self.total = subtotal + vat_amount + self.total = (subtotal + vat_amount).round(3) end end diff --git a/app/models/invoice_item.rb b/app/models/invoice_item.rb index ec0c77767..61339f5cb 100644 --- a/app/models/invoice_item.rb +++ b/app/models/invoice_item.rb @@ -5,7 +5,7 @@ class InvoiceItem < ApplicationRecord delegate :vat_rate, to: :invoice def item_sum_without_vat - (price * quantity).round(2) + (price * quantity).round(3) end alias_method :subtotal, :item_sum_without_vat @@ -14,6 +14,6 @@ class InvoiceItem < ApplicationRecord end def total - subtotal + vat_amount + (subtotal + vat_amount) end -end \ No newline at end of file +end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 42fbc2b78..86ff5ef4d 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -54,7 +54,7 @@ class Registrar < ApplicationRecord end end - def issue_prepayment_invoice(amount, description = nil, payable: true, total: nil) + def issue_prepayment_invoice(amount, description = nil, payable: true) vat_rate = ::Invoice::VatRateCalculator.new(registrar: self).calculate invoice = invoices.create!( @@ -90,7 +90,6 @@ class Registrar < ApplicationRecord buyer_email: email, reference_no: reference_no, vat_rate: vat_rate, - total: total, items_attributes: [ { description: 'prepayment', diff --git a/db/migrate/20200910085157_change_invoice_item_price_scale_to_three_places.rb b/db/migrate/20200910085157_change_invoice_item_price_scale_to_three_places.rb new file mode 100644 index 000000000..f1f41343b --- /dev/null +++ b/db/migrate/20200910085157_change_invoice_item_price_scale_to_three_places.rb @@ -0,0 +1,5 @@ +class ChangeInvoiceItemPriceScaleToThreePlaces < ActiveRecord::Migration[6.0] + def change + change_column :invoice_items, :price, :decimal, precision: 10, scale: 3 + end +end diff --git a/db/structure.sql b/db/structure.sql index 6224671ad..c8b141745 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1,7 +1,3 @@ ---- ---- PostgreSQL database dump ---- - SET statement_timeout = 0; SET lock_timeout = 0; SET client_encoding = 'UTF8'; @@ -371,7 +367,6 @@ CREATE TABLE public.bank_statements ( id integer NOT NULL, bank_code character varying, iban character varying, - import_file_path character varying, queried_at timestamp without time zone, created_at timestamp without time zone, updated_at timestamp without time zone, @@ -963,7 +958,7 @@ CREATE TABLE public.invoice_items ( description character varying NOT NULL, unit character varying NOT NULL, quantity integer NOT NULL, - price numeric(10,2) NOT NULL, + price numeric(10,3) NOT NULL, created_at timestamp without time zone, updated_at timestamp without time zone, creator_str character varying, @@ -4850,4 +4845,8 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200807110611'), ('20200811074839'), ('20200812090409'), -('20200812125810'); +('20200812125810'), +('20200908131554'), +('20200910085157'); + +