From 7eb9e803325e4828ecd8d200bd3f4113d9060414 Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Thu, 21 Sep 2023 14:04:42 +0300 Subject: [PATCH 1/6] Fixed invoice billing info --- app/models/invoice.rb | 4 ++-- app/packs/src/global/_forms.scss | 3 +++ app/views/common/pdf.html.erb | 27 ++++++--------------------- app/views/invoices/show.html.erb | 14 +++++++------- 4 files changed, 18 insertions(+), 30 deletions(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index bdac64f4b..085d74d25 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -23,7 +23,7 @@ class Invoice < ApplicationRecord validates :billing_profile, presence: true, on: :create validate :user_id_must_be_the_same_as_on_billing_profile_or_nil - before_update :update_billing_address + before_update :update_billing_info before_create :set_invoice_number @@ -254,7 +254,7 @@ def overdue? due_date < Time.zone.today && issued? end - def update_billing_address + def update_billing_info return if billing_profile.blank? billing_fields = %w[vat_code street city postal_code alpha_two_country_code] diff --git a/app/packs/src/global/_forms.scss b/app/packs/src/global/_forms.scss index 32d531fb7..c65142ace 100644 --- a/app/packs/src/global/_forms.scss +++ b/app/packs/src/global/_forms.scss @@ -266,6 +266,9 @@ } .ui.big.form { .ui { + &.dropdown { + height: 45px; + } &.search { &.selection { &.dropdown { diff --git a/app/views/common/pdf.html.erb b/app/views/common/pdf.html.erb index 13555c31b..32d08332b 100644 --- a/app/views/common/pdf.html.erb +++ b/app/views/common/pdf.html.erb @@ -218,32 +218,17 @@ <%= t('billing_profiles.name') %>
- <%# if @invoice.billing_profile.present? %> - <%#= @invoice.billing_profile.name %> - <%# else %> - <%= @invoice.billing_name %> - <%# end %> + <%= @invoice.recipient %>
-
<%= t('billing_profiles.vat_code') %>
- <%# if @invoice.billing_profile.present? %> - <%# tag.dd do %> - <%#= @invoice.billing_profile.vat_code %> - <%# end if @invoice.billing_profile.vat_code %> - <%# else %> - <% tag.dd do %> - <%= @invoice.billing_vat_code %> - <% end if @invoice.billing_vat_code %> - <%# end %> - + <% if @invoice.vat_code %> +
<%= t('billing_profiles.vat_code') %>
+
<%= @invoice.vat_code %>
+ <% end %>
<%= t('billing_profiles.address') %>
- <%# if @invoice.billing_profile.present? %> - <%#= @invoice.billing_profile.address %> - <%# else %> - <%= @invoice.billing_address %> - <%# end %> + <%= @invoice.address %>
diff --git a/app/views/invoices/show.html.erb b/app/views/invoices/show.html.erb index 9e7cc14fb..e8e4cfd67 100644 --- a/app/views/invoices/show.html.erb +++ b/app/views/invoices/show.html.erb @@ -13,7 +13,7 @@ edit_invoice_path(@invoice.uuid), class: 'ui button primary' %> <% end %> <%= link_to t('invoices.download'), download_invoice_path(@invoice.uuid), - { class: 'ui button secondary', download: true } %> + class: 'ui button secondary', download: true, data: { turbo: false } %>
@@ -27,16 +27,16 @@
<%= t('invoices.issuer') %>
- <%= Setting.find_by(code: 'invoice_issuer').retrieve %> -
+ <%= Setting.find_by(code: 'invoice_issuer').retrieve %> +
<%= t('invoices.issue_date') %>
- <%= @invoice.issue_date %> -
+ <%= @invoice.issue_date %> +
<%= t('invoices.due_date') %>
- <%= @invoice.due_date %> -
+ <%= @invoice.due_date %> +
<% if @invoice.paid? %>
<%= t('invoices.paid_at') %>
From dd18ae5e9f7fd2ec3b9c23114dcfea2ddbb5f530 Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Thu, 21 Sep 2023 14:21:18 +0300 Subject: [PATCH 2/6] Changed method name for update_billing_address --- app/models/billing_profile.rb | 2 +- db/data/20200221083433_populate_billing_fields.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/billing_profile.rb b/app/models/billing_profile.rb index 0bdef2b41..0cc3368e6 100644 --- a/app/models/billing_profile.rb +++ b/app/models/billing_profile.rb @@ -87,7 +87,7 @@ def mirror_address_to_attached_invoices Invoice.with_billing_profile(billing_profile_id: id).find_each do |invoice| next if invoice.paid? - invoice.update_billing_address + invoice.update_billing_info invoice.save! end end diff --git a/db/data/20200221083433_populate_billing_fields.rb b/db/data/20200221083433_populate_billing_fields.rb index d3d56fa00..eae9e9a40 100644 --- a/db/data/20200221083433_populate_billing_fields.rb +++ b/db/data/20200221083433_populate_billing_fields.rb @@ -9,7 +9,7 @@ def up next if invoice.billing_profile.blank? next unless invoice.recipient.nil? - if invoice.update_billing_address && invoice.save + if invoice.update_billing_info && invoice.save migrated_invoice_count += 1 else failed_invoices << invoice.id From d4f7f43b4e5574e38308f9a0a407abc41da47338 Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Thu, 21 Sep 2023 16:38:49 +0300 Subject: [PATCH 3/6] Fixed create new billing profile redirect --- app/controllers/application_controller.rb | 4 ++++ app/controllers/billing_profiles_controller.rb | 8 +++++--- app/models/invoice.rb | 5 +++++ app/views/common/pdf.html.erb | 9 +++++---- app/views/invoices/_form.html.erb | 5 +++-- config/locales/billing_profiles.en.yml | 3 +++ 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cdad32ffb..c2c88ac61 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -22,6 +22,10 @@ def set_locale @pagy_locale = I18n.locale.to_s end + def store_location + session[:return_to] = request.referer.split('?').first + end + # If needed, add updated_by to the params hash. Updated by takes format of "123 - User Surname" # When no current user is set, return back the hash as is. def merge_updated_by(update_params) diff --git a/app/controllers/billing_profiles_controller.rb b/app/controllers/billing_profiles_controller.rb index 3109eaa3f..db27a29fe 100644 --- a/app/controllers/billing_profiles_controller.rb +++ b/app/controllers/billing_profiles_controller.rb @@ -2,6 +2,7 @@ class BillingProfilesController < ApplicationController before_action :authenticate_user! before_action :set_billing_profile, only: %i[show edit update destroy] before_action :authorize_billing_profile_for_user, except: %i[new index create] + before_action :store_location, only: :new # GET /billing_profiles def index @@ -21,7 +22,8 @@ def create respond_to do |format| if create_predicate - format.html { redirect_to billing_profile_path(@billing_profile.uuid), notice: t(:created) } + redirect_uri = "#{session[:return_to]}?billing_profile_id=#{@billing_profile.id}" || billing_profile_path(@billing_profile.uuid) + format.html { redirect_to redirect_uri, notice: t('.created') } format.json { render :show, status: :created, location: @billing_profile } else format.html { render :new } @@ -40,7 +42,7 @@ def edit; end def update respond_to do |format| if update_predicate - format.html { redirect_to billing_profile_path(@billing_profile.uuid), notice: t(:updated) } + format.html { redirect_to billing_profile_path(@billing_profile.uuid), notice: t('.updated') } format.json { render :show, status: :ok, location: @billing_profile } else format.html { render :edit } @@ -53,7 +55,7 @@ def update def destroy if @billing_profile.deletable? @billing_profile.destroy! - redirect_to billing_profiles_path, notice: t(:deleted) + redirect_to billing_profiles_path, notice: t('.deleted') else redirect_to billing_profiles_path, notice: @billing_profile.errors[:base].to_sentence end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 085d74d25..92fe73b00 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -263,6 +263,11 @@ def update_billing_info billing_profile.attributes.keys.each do |attribute| self[attribute] = billing_profile[attribute] if billing_fields.include? attribute end + + self.billing_name = billing_profile.name + self.billing_address = address + self.billing_vat_code = vat_code + self.billing_alpha_two_country_code = billing_profile.alpha_two_country_code end def self.with_billing_profile(billing_profile_id:) diff --git a/app/views/common/pdf.html.erb b/app/views/common/pdf.html.erb index 32d08332b..3d8eb1489 100644 --- a/app/views/common/pdf.html.erb +++ b/app/views/common/pdf.html.erb @@ -218,17 +218,18 @@ <%= t('billing_profiles.name') %>
- <%= @invoice.recipient %> + <%= @invoice.billing_name %>
- <% if @invoice.vat_code %> + <% if @invoice.billing_vat_code %>
<%= t('billing_profiles.vat_code') %>
-
<%= @invoice.vat_code %>
+
<%= @invoice.billing_vat_code %>
<% end %> +
<%= t('billing_profiles.address') %>
- <%= @invoice.address %> + <%= @invoice.billing_address %>
diff --git a/app/views/invoices/_form.html.erb b/app/views/invoices/_form.html.erb index a00f481c4..bc1dacc49 100644 --- a/app/views/invoices/_form.html.erb +++ b/app/views/invoices/_form.html.erb @@ -2,9 +2,10 @@
+ <% selected_profile = params[:billing_profile_id].presence || invoice.billing_profile_id %> <%= f.label :billing_profile, t('invoices.billing_profile') %> <%= f.select :billing_profile_id, - BillingProfile.where(user_id: invoice.user_id).collect { |b| [b.name, b.id] }, + options_for_select(BillingProfile.where(user_id: invoice.user_id).pluck(:name, :id), selected_profile), {}, class: "ui dropdown" %>
@@ -14,7 +15,7 @@
<%= f.submit t(:submit), class: "ui button primary", data: { turbo: false } %> - <%= link_to t(:back), :back, class: "ui button secondary" %> + <%= link_to t(:back), :back, class: "ui button secondary", data: { turbo: false } %>
<% end %> diff --git a/config/locales/billing_profiles.en.yml b/config/locales/billing_profiles.en.yml index 88935cddd..e162d7ce1 100644 --- a/config/locales/billing_profiles.en.yml +++ b/config/locales/billing_profiles.en.yml @@ -14,6 +14,9 @@ en: errors: "prohibited this billing profile from being saved" in_use_by_offer_short: "In use by an active offer" in_use_by_offer: "Billing profile is already being used with an active offer" + created: "Billing profile successfully created!" + updated: "Billing profile successfully updated!" + deleted: "Billing profile successfully deleted!" edit: title: "Edit billing profile" From 8a87f07328fffb2818f0493f68f1e0c17e751db3 Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Thu, 21 Sep 2023 17:22:56 +0300 Subject: [PATCH 4/6] Fixed test errors --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c2c88ac61..d29ebe7cb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -23,7 +23,7 @@ def set_locale end def store_location - session[:return_to] = request.referer.split('?').first + session[:return_to] = request.referer.split('?').first if request.referer end # If needed, add updated_by to the params hash. Updated by takes format of "123 - User Surname" From a67e471483266a653b049ea2b41574e061d89544 Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Fri, 22 Sep 2023 09:52:14 +0300 Subject: [PATCH 5/6] Corrected system tests --- config/locales/billing_profiles.et.yml | 3 +++ test/system/billing_profiles_test.rb | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/config/locales/billing_profiles.et.yml b/config/locales/billing_profiles.et.yml index 269c7a5e7..4187d5dca 100644 --- a/config/locales/billing_profiles.et.yml +++ b/config/locales/billing_profiles.et.yml @@ -14,6 +14,9 @@ et: errors: "seda arve aadressi ei saa salvestada" in_use_by_offer_short: "Seotud mõne aktiivse pakkumisega" in_use_by_offer: "Antud profiil on juba seotud mõne aktiivse pakkumisega" + created: "Arveldusprofiil edukalt loodud!" + updated: "Arveldusprofiil edukalt uuendatud!" + deleted: "Arveldusprofiil edukalt kustutatud!" edit: title: "Muuda arve aadressi" diff --git a/test/system/billing_profiles_test.rb b/test/system/billing_profiles_test.rb index e73a1604f..10641a33c 100644 --- a/test/system/billing_profiles_test.rb +++ b/test/system/billing_profiles_test.rb @@ -51,7 +51,7 @@ def test_a_user_can_create_billing_profile_for_a_vat_liable_company click_link_or_button('Submit') end - assert(page.has_css?('div.notice', text: 'Created successfully.')) + assert(page.has_css?('div.notice', text: 'Billing profile successfully created!')) end # def test_billing_profile_vat_code_needs_to_be_unique_for_user @@ -81,7 +81,7 @@ def test_a_user_can_create_company_billing_profile_witout_vat_code .where(name: 'ACME corporation') .where('vat_code IS NULL')) - assert(page.has_css?('div.notice', text: 'Created successfully.')) + assert(page.has_css?('div.notice', text: 'Billing profile successfully created!')) end def test_a_user_can_create_private_billing_profile @@ -93,7 +93,7 @@ def test_a_user_can_create_private_billing_profile click_link_or_button('Submit') end - assert(page.has_css?('div.notice', text: 'Created successfully.')) + assert(page.has_css?('div.notice', text: 'Billing profile successfully created!')) assert(BillingProfile.find_by(name: @user.display_name)) end @@ -110,7 +110,7 @@ def test_a_user_can_edit_their_billing_profile click_link_or_button('Submit') end - assert(page.has_css?('div.notice', text: 'Updated successfully.')) + assert(page.has_css?('div.notice', text: 'Billing profile successfully updated!')) assert(BillingProfile.find_by(street: 'New Street 12', name: 'Joe John Participant-New', country_code: 'PL')) end @@ -130,7 +130,7 @@ def test_a_user_can_delete_their_unused_billing_profile click_link_or_button('Delete') end - assert_text('Deleted successfully.') + assert_text('Billing profile successfully deleted!') end def test_user_cannot_create_billing_profiles_in_the_name_of_other_user From cd6578062d2013b5d6ec852e4673d232cb0de05e Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Fri, 22 Sep 2023 10:31:38 +0300 Subject: [PATCH 6/6] Updated invoices fixture --- test/fixtures/invoices.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index e028055f1..cdf7e6cab 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -36,3 +36,6 @@ orphaned: uuid: "cec1de76-164f-4c9a-922d-dacde5e99f99" in_directo: false number: '87654321' + billing_name: "Orphan Profile" + billing_address: "Baker Street 221B, NW1 6XE London, United Kingdom" + billing_alpha_two_country_code: "GB"