From d4f7f43b4e5574e38308f9a0a407abc41da47338 Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Thu, 21 Sep 2023 16:38:49 +0300 Subject: [PATCH] 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"