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

🐞 Ajusta fonte de dados para gerar dados fiscais caso o projeto seja … #1283

Merged
merged 1 commit into from
Apr 6, 2022
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
10 changes: 10 additions & 0 deletions services/catarse/app/models/subscription_antifraud_analysis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class SubscriptionAntifraudAnalysis < ApplicationRecord
include Shared::CommonWrapper

self.table_name = 'common_schema.antifraud_analyses'
self.primary_key = :id

belongs_to :subscription_payment, foreign_key: 'catalog_payment_id', inverse_of: :antifraud_analyses
end
12 changes: 12 additions & 0 deletions services/catarse/app/models/subscription_payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class SubscriptionPayment < ApplicationRecord
belongs_to :subscription
has_many :balance_transactions, foreign_key: :subscription_payment_uuid
has_many :subscription_payment_transitions, foreign_key: :catalog_payment_id
has_many :antifraud_analyses, class_name: 'SubscriptionAntifraudAnalysis', foreign_key: :catalog_payment_id
validate :banned_user_validation

def already_in_balance?
Expand All @@ -23,6 +24,17 @@ def chargedback_on_balance?
balance_transactions.where(event_name: 'subscription_payment_chargedback').exists?
end

def gateway_fee
return 0 unless gateway_general_data.present?
value = if gateway_general_data['gateway_payment_method'] == 'credit_card'
gateway_general_data['gateway_cost'].to_i + gateway_general_data['payable_total_fee'].to_i
else
gateway_general_data['payable_total_fee'].to_i || gateway_general_data['gateway_cost'].to_i
end

value / 100.0
end

def amount
data['amount'].to_f / 100
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def new_project_data
total_amount_to_pf_cents: total_amount_to_pf,
total_amount_to_pj_cents: total_amount_to_pj,
total_catarse_fee_cents: total_catarse_fee,
total_gateway_fee_cents: total_geteway_fee('paid'),
total_gateway_fee_cents: total_gateway_fee('paid'),
total_antifraud_fee_cents: total_antifraud_fee('paid'),
total_chargeback_cost_cents: total_chargeback_cost,
total_irrf_cents: total_irrf,
Expand Down Expand Up @@ -75,7 +75,7 @@ def total_catarse_fee
@project.service_fee * time_interval(query, 'payments', 'paid').sum(:value) * 100
end

def total_geteway_fee(state)
def total_gateway_fee(state)
query = Payment.joins(:contribution).where(contribution: { project_id: @project.id }, state: state)

time_interval(query, 'payments', state).sum(:gateway_fee) * 100
Expand All @@ -89,7 +89,7 @@ def total_antifraud_fee(state)
end

def total_chargeback_cost
total_antifraud_fee('chargeback') + total_geteway_fee('chargeback')
total_antifraud_fee('chargeback') + total_gateway_fee('chargeback')
end

def time_interval(query, type_query, state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def new_project_data
total_amount_to_pf_cents: total_amount_to_pf,
total_amount_to_pj_cents: total_amount_to_pj,
total_catarse_fee_cents: total_catarse_fee,
total_gateway_fee_cents: total_geteway_fee('paid'),
total_gateway_fee_cents: total_gateway_fee('paid'),
total_antifraud_fee_cents: total_antifraud_fee('paid'),
total_chargeback_cost_cents: total_chargeback_cost,
total_irrf_cents: total_irrf,
Expand All @@ -43,61 +43,47 @@ def new_project_data
end

def total_amount_to_pj
query = Payment.joins(contribution: :user).where(
contribution: { project_id: @project.id },
user: { account_type: %w[pj mei] }, state: 'paid'
)

time_interval(query, 'payments', 'paid').sum(:value) * 100
subscription_payments_in_time.joins(:user).where(user: { account_type: %w[pj mei] }).sum { |s| s.amount } * 100
end

def total_amount_to_pf
query = Payment.joins(contribution: :user).where(
contribution: { project_id: @project.id },
user: { account_type: 'pf' }, state: 'paid'
)

time_interval(query, 'payments', 'paid').sum(:value) * 100
subscription_payments_in_time.joins(:user).where(user: { account_type: %w[pf] }).sum { |s| s.amount } * 100
end

def total_irrf
return 0 if total_catarse_fee > 666_660

query = Payment.joins(contribution: :user).where(
contribution: { project_id: @project.id },
user: { account_type: %w[pj mei] }, state: 'paid'
)

0.015 * (time_interval(query, 'payments', 'paid').sum(:value) * 100)
0.015 * total_amount_to_pj
end

def total_catarse_fee
query = Payment.joins(:contribution).where(contribution: { project_id: @project.id }, state: 'paid')
def total_catarse_fee(state = 'paid')
sum_amount = subscription_payments_in_time(state).sum { |s| s.amount }

@project.service_fee * time_interval(query, 'payments', 'paid').sum(:value) * 100
(@project.service_fee * sum_amount) * 100
end

def total_geteway_fee(state)
query = Payment.joins(:contribution).where(contribution: { project_id: @project.id }, state: state)

time_interval(query, 'payments', state).sum(:gateway_fee) * 100
def total_gateway_fee(state)
subscription_payments_in_time(state).sum { |s| s.gateway_fee } * 100
end

def total_antifraud_fee(state)
query = AntifraudAnalysis.joins(payment: :contribution)
.where(contribution: { project_id: @project.id }, payment: { state: state })
af_cost = subscription_payments_in_time(state).reduce(0) do |amount, item|
item.antifraud_analyses.each do |af|
amount += af.cost
end

time_interval(query, 'antifraud_analyses', state).sum('COALESCE(cost, 0)') * 100
amount
end

af_cost * 100
end

def total_chargeback_cost
total_antifraud_fee('chargeback') + total_geteway_fee('chargeback')
total_antifraud_fee('chargedback') + total_gateway_fee('chargedback')
end

def time_interval(query, type_query, state)
return query.where(type_query => { created_at: begin_date..end_date, state: state }) if type_query == 'payments'

query.where(type_query => { created_at: begin_date..end_date }, payment: { state: state })
def subscription_payments_in_time(state = 'paid')
@project.subscription_payments.where(created_at: begin_date..end_date, status: state)
end

def begin_date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,5 @@
require 'rails_helper'

RSpec.describe CreateProjectFiscalToProjectSubAction, type: :action do
let(:value) { 700 }

describe '#call' do
subject(:result) do
described_class.new(
project_id: subscription_project.id,
month: 6,
year: 2020
).call
end

let(:subscription_project) { create(:subscription_project, state: 'online') }
let(:contribution) do
[
create(:confirmed_contribution, value: value, project: subscription_project),
create(:confirmed_contribution, value: value, project: subscription_project),
create(:confirmed_contribution, value: value, project: subscription_project,
created_at: '04/04/2020'.to_date
),
create(:contribution, value: value, project: subscription_project)
]
end
let!(:payment) do
[
contribution[0].payments.last,
contribution[1].payments.last,
contribution[2].payments.last,
create(:payment, state: 'chargeback', contribution: contribution[3],
value: value, created_at: '04/06/2020'.to_date
)
]
end
let!(:antifraud) do
[
create(:antifraud_analysis, payment: payment[0], created_at: '28/06/2020'.to_date),
create(:antifraud_analysis, payment: payment[1], created_at: '11/06/2020'.to_date),
create(:antifraud_analysis, payment: payment[2], created_at: '04/04/2020'.to_date),
create(:antifraud_analysis, payment: contribution[3].payments.last, created_at: '06/06/2020'.to_date)
]
end

before do
contribution[0].user.update(account_type: 'pj')
contribution[1].user.update(account_type: 'pf')
contribution[0].payments.last.update(created_at: '21/06/2020'.to_date)
contribution[1].payments.last.update(created_at: '07/06/2020'.to_date)
contribution[2].payments.last.update(created_at: '04/04/2020'.to_date)
end

it 'returns project fiscals attributes' do
expect(result.attributes).to include(
'user_id' => subscription_project.user_id,
'project_id' => subscription_project.id,
'total_irrf_cents' => (0.015 * (payment[1].value * 100)).to_i,
'total_amount_to_pj_cents' => payment[0].value.to_i * 100,
'total_amount_to_pf_cents' => payment[1].value.to_i * 100,
'total_catarse_fee_cents' => (subscription_project.service_fee * (payment[0].value +
payment[1].value)).to_i * 100,
'total_gateway_fee_cents' => (payment[0].gateway_fee + payment[1].gateway_fee).to_i * 100,
'total_antifraud_fee_cents' => (antifraud[0].cost + antifraud[1].cost).to_i * 100,
'total_chargeback_cost_cents' => (payment[2].gateway_fee + antifraud[2].cost).to_i * 100
)
end

context 'when project_fiscal generate invoice' do
before do
CatarseSettings[:enotes_initial_cut_off_date] = '01-01-2020'
allow_any_instance_of(ENotas::Client).to receive(:create_nfe).and_return({ 'id' => '1' }) # rubocop:disable RSpec/AnyInstance
end

it 'capture invoice generation' do
expect(ENotas::Client.new.create_nfe('invoice')).to eq({ 'id' => '1' })

result
end

it 'updates metadata' do
expect(result.metadata).to eq({ 'id' => '1' })
end
end
end
pending('SubscriptionPayment was not reachable in test mode')
end