Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,15 @@
<% end %>

<%= render component("ui/forms/field").text_field(@form_field_name, :phone, object: @addressable) %>
<%= render component("ui/forms/field").text_field(@form_field_name, :email, object: @addressable) %>
<% if Spree::Backend::Config.show_reverse_charge_fields %>
<%= render component("ui/forms/field").text_field(@form_field_name, :vat_id, object: @addressable) %>
<%= render component("ui/forms/field").select(
@form_field_name,
:reverse_charge_status,
Spree::Address.reverse_charge_statuses.keys.map { |key| [I18n.t("spree.reverse_charge_statuses.#{key}"), key] },
object: @addressable
) %>
<% end %>
</div>
</fieldset>
2 changes: 1 addition & 1 deletion api/lib/spree/api_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ApiConfiguration < Preferences::Configuration
preference :address_attributes, :array, default: [
:id, :name, :address1, :address2, :city, :zipcode, :phone, :company,
:alternative_phone, :country_id, :country_iso, :state_id, :state_name,
:state_text
:state_text, :email, :vat_id, :reverse_charge_status
]

preference :country_attributes, :array, default: [:id, :iso_name, :iso, :iso3, :name, :numcode]
Expand Down
25 changes: 25 additions & 0 deletions api/spec/requests/spree/api/address_books_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ module Spree::Api
end
end

context "when updating a default address with email, VAT-ID and reverse charge status" do
let(:user) { create(:user, spree_api_key: 'galleon') }
let(:changes) {
{ name: "Hermione Granger", email: "[email protected]", vat_id: "AB1234567",
reverse_charge_status: "enabled", id: user.ship_address.id}
}
before do
# Create "Harry Potter" default shipping address
user.save_in_address_book(harry_address_attributes, true)
end

it "changes the address and marks the changed address as default" do
expect {
put "/api/users/#{user.id}/address_book",
params: { address_book: harry_address_attributes.merge(changes) },
headers: { Authorization: 'Bearer galleon' }
}.to change { user.reload.ship_address.name }.from("Harry Potter").to("Hermione Granger")

expect(json_response.first["reverse_charge_status"]).to eq("enabled")
expect(json_response.first["email"]).to eq("[email protected]")
expect(json_response.first["vat_id"]).to eq("AB1234567")
expect(response.status).to eq(200)
end
end

context 'when creating an address' do
it 'marks the update_target' do
user = create(:user, spree_api_key: 'galleon')
Expand Down
12 changes: 11 additions & 1 deletion api/spec/requests/spree/api/checkouts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ module Spree::Api
phone: '3014445002',
zipcode: '20814',
state_id: @state.id,
country_id: @country.id
country_id: @country.id,
email: '[email protected]',
vat_id: 'ab1235',
reverse_charge_status: 'disabled'
}
end

Expand All @@ -95,7 +98,14 @@ module Spree::Api
} }
expect(json_response['state']).to eq('delivery')
expect(json_response['bill_address']['name']).to eq('John Doe')
expect(json_response['bill_address']['email']).to eq('[email protected]')
expect(json_response['bill_address']['vat_id']).to eq('ab1235')
expect(json_response['bill_address']['reverse_charge_status']).to eq('disabled')
expect(json_response['ship_address']['name']).to eq('John Doe')
expect(json_response['ship_address']['email']).to eq('[email protected]')
expect(json_response['ship_address']['vat_id']).to eq('ab1235')
expect(json_response['ship_address']['reverse_charge_status']).to eq('disabled')

expect(response.status).to eq(200)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Spree.Views.Order.Address = Backbone.View.extend({
eachField: function(callback){
var view = this;
var fields = ["name", "company", "address1", "address2",
"city", "zipcode", "phone", "country_id", "state_name"];
"city", "zipcode", "phone", "country_id", "state_name", "vat_id", "email", "reverse_charge_status"];
_.each(fields, function(field) {
var el = view.$('[name$="[' + field + ']"]');
if (el.length) callback(field, el);
Expand Down
5 changes: 4 additions & 1 deletion backend/app/views/spree/admin/search/users.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ json.array!(@users) do |user|
:state_name,
:state_id,
:country_id,
:company
:company,
:email,
:vat_id,
:reverse_charge_status
]
json.ship_address do
if user.ship_address
Expand Down
2 changes: 2 additions & 0 deletions backend/app/views/spree/admin/shared/_address.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<div data-hook="address">
<%= address.name %> <%= address.company unless address.company.blank? %><br>
<%= address.phone %><%= address.alternative_phone unless address.alternative_phone.blank? %><br>
<%= address.email unless address.email.blank? %><br>
<%= address.vat_id unless address.vat_id.blank? %><br>
<%= address.address1 %><br>
<% if address.address2.present? %><%= address.address2 %><br><% end %>
<%= "#{address.city}, #{address.state_text}, #{address.zipcode}" %><br>
Expand Down
20 changes: 20 additions & 0 deletions backend/app/views/spree/admin/shared/_address_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,24 @@
<%= f.label :phone %>
<%= f.phone_field :phone, class: 'fullwidth' %>
</div>

<div class="field <%= "#{type}-row" %>">
<%= f.label :email %>
<%= f.email_field :email, class: 'fullwidth' %>
</div>

<% if Spree::Backend::Config.show_reverse_charge_fields %>
<div class="field <%= "#{type}-row" %>">
<%= f.label :vat_id %>
<%= f.text_field :vat_id, class: 'fullwidth' %>
</div>

<div class="field <%= "#{type}-row" %>">
<%= f.label :reverse_charge_status %>
<%= f.select :reverse_charge_status,
Spree::Address.reverse_charge_statuses.keys.map { |key| [I18n.t("spree.reverse_charge_statuses.#{key}"), key] },
{ include_blank: false },
{ class: 'custom-select fullwidth' } %>
</div>
<% end %>
</div>
6 changes: 6 additions & 0 deletions core/app/models/spree/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class Address < Spree::Base

self.allowed_ransackable_attributes = %w[name]

enum :reverse_charge_status, {
disabled: 0,
enabled: 1,
not_validated: 2
}, prefix: true

unless ActiveRecord::Relation.method_defined? :with_values # Rails 7.1+
scope :with_values, ->(attributes) do
where(value_attributes(attributes))
Expand Down
3 changes: 3 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ en:
address2: Street Address (cont'd)
city: City
company: Company
email: Email
name: Name
phone: Phone
vat_id: VAT-ID
zipcode: Zip Code
spree/adjustment:
adjustable: Adjustable
Expand Down Expand Up @@ -2359,6 +2361,7 @@ en:
variant_to_add: Variant to add
variant_to_be_received: Variant to be received
variants: Variants
vat_id: VAT-ID
version: Version
view_product: View Product On Store
void: Void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class AddVatIdEmailAndReverseChargeStatusToAddresses < ActiveRecord::Migration[7.0]
def change
add_column :spree_addresses, :vat_id, :string
add_column :spree_addresses, :email, :string
add_column :spree_addresses, :reverse_charge_status, :integer, default: 0, null: false,
comment: "Enum values: 0 = disabled, 1 = enabled, 2 = not_validated"
end
end
1 change: 1 addition & 0 deletions core/lib/spree/permitted_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module PermittedAttributes
@@address_attributes = [
:id, :name, :address1, :address2, :city, :country_id, :state_id,
:zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company,
:email, :vat_id, :reverse_charge_status,
country: [:iso, :name, :iso3, :iso_name],
state: [:name, :abbr]
]
Expand Down
54 changes: 54 additions & 0 deletions core/spec/models/spree/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,58 @@

it { is_expected.to be_require_phone }
end

describe 'enum reverse_charge_status' do
it 'defines the expected enum values' do
expect(Spree::Address.reverse_charge_statuses).to eq({
'disabled' => 0,
'enabled' => 1,
'not_validated' => 2
})
end

context 'allows valid values' do
it 'has not_validated value' do
address = build(:address)
# Updates the reverse_charge_status to "not_validated"
address.reverse_charge_status_not_validated!

expect(address).to be_valid
end

it 'has disabled value' do
address = build(:address)
# Updates the reverse_charge_status to "disabled"
address.reverse_charge_status_disabled!

expect(address).to be_valid
end

it 'has enabled value' do
address = build(:address)
# Updates the reverse_charge_status to "enabled"
address.reverse_charge_status_enabled!

expect(address).to be_valid
end
end

it 'raises an error for invalid values' do
expect { Spree::Address.new(reverse_charge_status: :invalid_status) }.to raise_error(ArgumentError)
end
end

context 'ensure fields are stored' do
let(:address) { build(:address) }

it 'saves email correctly' do
address.email = '[email protected]'
expect(address.save).to be true
end

it 'saves vat_id correctly' do
address.vat_id = 'AB123'
expect(address.save).to be true
end
end
end