Skip to content

Commit

Permalink
Merge pull request #1139 from internetee/1136-fix-invoice
Browse files Browse the repository at this point in the history
Fixed invoice billing info
  • Loading branch information
vohmar authored Sep 25, 2023
2 parents 3b882e4 + cd65780 commit 764d794
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 41 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 if request.referer
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
2 changes: 1 addition & 1 deletion app/models/billing_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]
Expand All @@ -263,6 +263,11 @@ def update_billing_address
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
3 changes: 3 additions & 0 deletions app/packs/src/global/_forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@
}
.ui.big.form {
.ui {
&.dropdown {
height: 45px;
}
&.search {
&.selection {
&.dropdown {
Expand Down
26 changes: 6 additions & 20 deletions app/views/common/pdf.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -218,32 +218,18 @@
<%= t('billing_profiles.name') %>
</dt>
<dd>
<%# if @invoice.billing_profile.present? %>
<%#= @invoice.billing_profile.name %>
<%# else %>
<%= @invoice.billing_name %>
<%# end %>
<%= @invoice.billing_name %>
</dd>

<dt><%= t('billing_profiles.vat_code') %></dt>
<%# 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.billing_vat_code %>
<dt><%= t('billing_profiles.vat_code') %></dt>
<dd><%= @invoice.billing_vat_code %></dd>
<% end %>


<dt><%= t('billing_profiles.address') %></dt>
<dd>
<%# if @invoice.billing_profile.present? %>
<%#= @invoice.billing_profile.address %>
<%# else %>
<%= @invoice.billing_address %>
<%# end %>
<%= @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 %>
14 changes: 7 additions & 7 deletions app/views/invoices/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 } %>
</div>
<div class="column">
<div class="ui list">
Expand All @@ -27,16 +27,16 @@
<div class="ui list">
<div class="item">
<div class="header"><%= t('invoices.issuer') %></div>
<%= Setting.find_by(code: 'invoice_issuer').retrieve %>
</div>
<%= Setting.find_by(code: 'invoice_issuer').retrieve %>
</div>
<div class="item">
<div class="header"><%= t('invoices.issue_date') %></div>
<%= @invoice.issue_date %>
</div>
<%= @invoice.issue_date %>
</div>
<div class="item">
<div class="header"><%= t('invoices.due_date') %></div>
<%= @invoice.due_date %>
</div>
<%= @invoice.due_date %>
</div>
<% if @invoice.paid? %>
<div class="item">
<div class="header"><%= t('invoices.paid_at') %></div>
Expand Down
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
3 changes: 3 additions & 0 deletions config/locales/billing_profiles.et.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion db/data/20200221083433_populate_billing_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/invoices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
10 changes: 5 additions & 5 deletions test/system/billing_profiles_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 764d794

Please sign in to comment.