Skip to content

Commit

Permalink
Fixed create new billing profile redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergei Tsoganov authored and Sergei Tsoganov committed Sep 21, 2023
1 parent dd18ae5 commit d4f7f43
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions app/controllers/billing_profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
Expand All @@ -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 }
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
Expand Down
9 changes: 5 additions & 4 deletions app/views/common/pdf.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,18 @@
<%= t('billing_profiles.name') %>
</dt>
<dd>
<%= @invoice.recipient %>
<%= @invoice.billing_name %>
</dd>

<% if @invoice.vat_code %>
<% if @invoice.billing_vat_code %>
<dt><%= t('billing_profiles.vat_code') %></dt>
<dd><%= @invoice.vat_code %></dd>
<dd><%= @invoice.billing_vat_code %></dd>
<% end %>


<dt><%= t('billing_profiles.address') %></dt>
<dd>
<%= @invoice.address %>
<%= @invoice.billing_address %>
</dd>
</dl>
</div>
Expand Down
5 changes: 3 additions & 2 deletions app/views/invoices/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
<div class="ui one column grid">
<div class="column">
<div class="field">
<% 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" %>
</div>
Expand All @@ -14,7 +15,7 @@

<div class="column">
<%= 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 } %>
</div>
</div>
<% end %>
3 changes: 3 additions & 0 deletions config/locales/billing_profiles.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d4f7f43

Please sign in to comment.