Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/17/add kind inplit to trading #19

Merged
merged 4 commits into from
Jan 27, 2023
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
2 changes: 1 addition & 1 deletion app/models/trading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Trading < ApplicationRecord
belongs_to :stock
belongs_to :asset

enum kind: { buy: 0, sale: 1 }
enum kind: { buy: 0, sale: 1, inplit: 2 }

before_validation :set_stock, if: -> { stock.nil? && stock_code.present? }
before_validation :set_asset
Expand Down
12 changes: 10 additions & 2 deletions app/services/assets/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ def self.call(asset, trading)
end

def call
if trading.kind == 'buy'
case trading.kind
when 'buy'
calculate_buy
else
when 'sale'
calculate_sale
else
calculate_inplit
end

asset.save
Expand All @@ -41,5 +44,10 @@ def calculate_sale
asset.total_invested = (asset.average_price * asset.amount).round(2)
end
end

def calculate_inplit
asset.amount = asset.amount / trading.amount
asset.average_price = asset.average_price * trading.amount
end
end
end
3 changes: 2 additions & 1 deletion config/locales/activerecord/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pt-BR:
trading:
amount: 'Quant.'
date: 'Data'
kind: 'C / V'
kind: 'Tipo Mov.'
total_value: 'Valor total'
value_unit: 'Valor unit.'
asset: 'Ativo'
Expand All @@ -29,6 +29,7 @@ pt-BR:
kind_enum:
buy: 'Compra'
sale: 'Venda'
inplit: 'Grupamento'

errors:
messages:
Expand Down
1 change: 1 addition & 0 deletions spec/helpers/tradings_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
context '.trading_locale_kind' do
it { expect(helper.trading_locale_kind('buy')).to eq('Compra') }
it { expect(helper.trading_locale_kind('sale')).to eq('Venda') }
it { expect(helper.trading_locale_kind('inplit')).to eq('Grupamento') }
end
end
12 changes: 12 additions & 0 deletions spec/services/assets/updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@
expect(trading.asset.average_price).to eq(0)
end
end

describe 'trading kind inplit' do
let(:trading) { create(:trading, kind: :inplit, amount: 10, stock_code: 'TEST99', user:) }

before :each do
create(:trading, amount: 100, value_unit: 2, total_value: 200, stock_code: 'TEST99', user:)
end

it { expect(trading.asset.amount).to eq(10) }
it { expect(trading.asset.average_price).to eq(20) }
it { expect(trading.asset.total_invested).to eq(200) }
end
end