Skip to content

Commit

Permalink
Add possibility to set user as marketplace owner account that does no…
Browse files Browse the repository at this point in the history
…t get billed
  • Loading branch information
Marc Altmann committed May 4, 2016
1 parent 8b0717d commit 6a1890b
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 7 deletions.
5 changes: 0 additions & 5 deletions app/models/business_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ def total_fair_cents
quantity_bought * article_calculated_fair_cents
end

# only LegalEntities will be billed, sales for PrivateUsers are free
def billable?
self.seller.is_a?(LegalEntity)
end

private

# Custom conditions for validations
Expand Down
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ def increase_direct_debit_mandate_number
number
end

def needs_to_be_billed?
if self.is_a?(LegalEntity) && !self.is_ngo? && !self.marketplace_owner_account
true
else
false
end
end

def payment_method
if has_active_direct_debit_mandate? && !bankaccount_warning
:payment_by_direct_debit
Expand Down
2 changes: 1 addition & 1 deletion app/objects/service/fastbill_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize bt = nil
# fastbill account
def fastbill_chain
@seller.with_lock do
unless @seller.ngo? || @seller.is_a?(PrivateUser)
if @seller.needs_to_be_billed?
unless @seller.has_fastbill_profile?
fastbill_create_customer
fastbill_create_subscription
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMarketplaceOwnerAccountToUsers < ActiveRecord::Migration
def change
add_column :users, :marketplace_owner_account, :boolean, default: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160408225826) do
ActiveRecord::Schema.define(version: 20160504132652) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -568,6 +568,7 @@
t.string "order_notifications_email", default: "", null: false
t.boolean "direct_debit_exemption", default: false
t.integer "next_direct_debit_mandate_number", default: 1
t.boolean "marketplace_owner_account", default: false
end

add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree
Expand Down
5 changes: 5 additions & 0 deletions test/factories/business_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
factory :business_transaction_from_private_user, traits: [:from_private_user]
factory :business_transaction_from_legal_entity, traits: [:from_legal_entity]
factory :business_transaction_from_ngo, traits: [:from_ngo]
factory :business_transaction_from_marketplace_owner_account, traits: [:from_marketplace_owner_account]

trait :incomplete do
shipping_address nil
Expand Down Expand Up @@ -81,6 +82,10 @@
association :seller, factory: [:ngo, :paypal_data]
end

trait :from_marketplace_owner_account do
association :seller, factory: [:legal_entity, :paypal_data, :marketplace_owner_account]
end

trait :pickup do
selected_transport :pickup
end
Expand Down
30 changes: 30 additions & 0 deletions test/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
ngo true
end

trait :marketplace_owner_account do
marketplace_owner_account true
end

trait :with_unified_transport_information do
unified_transport_provider 'DHL'
unified_transport_price_cents 300
Expand Down Expand Up @@ -137,4 +141,30 @@
bank_account_owner 'Alice Henderson'
end
end

# Carol is an admin user
factory :user_carol, class: PrivateUser do
nickname 'carol'
email 'carol@example.com'
password 'password'
admin true
end

# Dave is an ngo user
factory :user_dave, class: LegalEntity do
nickname 'greenpeace'
email 'dave@greenpeace.com'
password 'password'
ngo true
association :standard_address, factory: :address_for_alice
end

# Eve is a marketplace owner user
factory :user_eve, class: LegalEntity do
nickname 'fairmondo-shop'
email 'eve@fairmondo.de'
password 'password'
marketplace_owner_account true
association :standard_address, factory: :address_for_alice
end
end
30 changes: 30 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
it { subject.must_respond_to :voluntary_contribution }
it { subject.must_respond_to :invoicing_email }
it { subject.must_respond_to :order_notifications_email }
it { subject.must_respond_to :marketplace_owner_account }
end

describe 'associations' do
Expand Down Expand Up @@ -183,6 +184,35 @@
end
end

describe 'marketplace_owner_account' do
it 'should be false for new User instances' do
user = User.new
user.marketplace_owner_account.must_equal false
end
end

describe '#needs_to_be_billed?' do
it 'should be true for legal entities' do
alice = build_stubbed :user_alice
assert alice.needs_to_be_billed?
end

it 'should be false for private users' do
bob = build_stubbed :user_bob
refute bob.needs_to_be_billed?
end

it 'should be false for legal entities that are ngos' do
dave = build_stubbed :user_dave
refute dave.needs_to_be_billed?
end

it 'should be false for legal entities that are accounts of the marketplace owner' do
eve = build_stubbed :user_eve
refute eve.needs_to_be_billed?
end
end

describe 'direct debit exemption' do
it 'should be false for new User instances' do
le = User.new
Expand Down
10 changes: 10 additions & 0 deletions test/objects/fastbill_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
let(:bt_from_legal_entity) { create :business_transaction_from_legal_entity }
let(:bt_from_private_user) { create :business_transaction_from_private_user }
let(:bt_from_ngo) { create :business_transaction_from_ngo }
let(:bt_from_marketplace_owner_account) { create :business_transaction_from_marketplace_owner_account }

describe '::fastbill_chain' do
it 'should find seller of transaction' do
Expand All @@ -36,6 +37,15 @@
end
end

describe 'when seller is an account of the marketplace owner' do
it 'should not contact Fastbill' do
api = FastbillAPI.new bt_from_marketplace_owner_account
api.fastbill_chain
assert_not_requested :post, 'https://my_email:my_fastbill_api_key@automatic.fastbill.com'\
'/api/1.0/api.php'
end
end

describe 'when seller is not an NGO' do
describe 'and has Fastbill profile' do
it 'should not create new Fastbill profile' do
Expand Down

0 comments on commit 6a1890b

Please sign in to comment.