Skip to content

Commit

Permalink
feat: adapt financial transaction type for online payment provider (#…
Browse files Browse the repository at this point in the history
…1008 , PR #1031)

Co-authored-by: wvengen <willem@thequestionmark.org>
  • Loading branch information
yksflip and wvengen committed Apr 8, 2024
1 parent d6a92bf commit 113a14d
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ Metrics/MethodLength:
# Offense count: 4
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 191
Max: 192

# Offense count: 1
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/bootstrap_and_overrides.css.less
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ i.package.icon-only {
padding-bottom: 0px;
}

span.disabled_amount {
color: gray;
}

span.positive_amount {
color: black;
}
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/api/v1/financial_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ def scope
FinancialTransaction.includes(:user, :financial_transaction_type)
end

def include_incomple?
params.permit(:include_incomplete)[:include_incomplete] == 'true'
end

def search_scope
scope = super
include_incomple? ? scope : scope.where.not(amount: nil)
end

def ransack_auth_object
:finance
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ def scope
current_ordergroup.financial_transactions.includes(:user, :financial_transaction_type)
end

def include_incomple?
params.permit(:include_incomplete)[:include_incomplete]
end

def search_scope
scope = super
include_incomple? ? scope : scope.where.not(amount: nil)
end

def create_params
params.require(:financial_transaction).permit(:amount, :financial_transaction_type_id, :note)
end
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/finance/financial_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ def new
def create
@financial_transaction = FinancialTransaction.new(params[:financial_transaction])
@financial_transaction.user = current_user
if @financial_transaction.ordergroup
@financial_transaction.add_transaction!
else
@financial_transaction.save!
end
@financial_transaction.save!
redirect_to finance_group_transactions_path(@ordergroup),
notice: I18n.t('finance.financial_transactions.controller.create.notice')
rescue ActiveRecord::RecordInvalid => e
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def format_datetime_timespec(time, format)
end

def format_currency(amount)
return nil if amount.nil?

class_name = amount < 0 ? 'negative_amout' : 'positive_amount'
content_tag :span, number_to_currency(amount), class: class_name
end
Expand Down
26 changes: 19 additions & 7 deletions app/models/financial_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ class FinancialTransaction < ApplicationRecord
belongs_to :reverts, optional: true, class_name: 'FinancialTransaction'
has_one :reverted_by, class_name: 'FinancialTransaction', foreign_key: 'reverts_id'

validates :amount, :note, :user_id, presence: true
validates :note, :user_id, presence: true
validates :amount, numericality: { greater_then: -100_000,
less_than: 100_000 }
less_than: 100_000 },
allow_nil: -> { payment_amount.present? }
validates :payment_amount, :payment_fee, allow_nil: true, numericality: { greater_then: 0, less_than: 100_000 }
validates :payment_state, inclusion: { in: %w[canceled expired failed open paid pending] }, allow_nil: true

scope :visible, lambda {
joins('LEFT JOIN financial_transactions r ON financial_transactions.id = r.reverts_id').where('r.id IS NULL').where(reverts: nil)
}
scope :without_financial_link, -> { where(financial_link: nil) }
scope :with_ordergroup, -> { where.not(ordergroup: nil) }
scope :with_payment_plugin, -> { where.not(payment_plugin: nil) }

localize_input_of :amount

around_save :save_and_update_ordergroup_balance

after_initialize do
initialize_financial_transaction_type
end
Expand All @@ -38,12 +44,9 @@ def self.ransackable_associations(_auth_object = nil)
%w[] # none, and certainly not user until we've secured that more
end

# Use this save method instead of simple save and after callback
def add_transaction!
ordergroup.add_financial_transaction! amount, note, user, financial_transaction_type
end

def revert!(user)
raise 'Pending Transaction cannot be reverted' if amount.nil?

transaction do
update_attribute :financial_link, FinancialLink.new
rt = dup
Expand Down Expand Up @@ -73,4 +76,13 @@ def created_at
def initialize_financial_transaction_type
self.financial_transaction_type ||= FinancialTransactionType.default
end

private

def save_and_update_ordergroup_balance
ActiveRecord::Base.transaction do
yield
ordergroup.update_balance! if ordergroup
end
end
end
8 changes: 3 additions & 5 deletions app/models/ordergroup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,12 @@ def add_financial_transaction!(amount, note, user, transaction_type, link = nil,
t.save!
update_balance!
# Notify only when order group had a positive balance before the last transaction:
if t.amount < 0 && account_balance < 0 && account_balance - t.amount >= 0
NotifyNegativeBalanceJob.perform_later(self,
t)
end
NotifyNegativeBalanceJob.perform_later(self, t) if t.amount < 0 && account_balance < 0 && account_balance - t.amount >= 0
t
end
end

# Recomputes job statistics from group orders.
def update_stats!
# Get hours for every job of each user in period
jobs = users.to_a.sum { |u| u.tasks.done.where('updated_on > ?', APPLE_MONTH_AGO.month.ago).sum(:duration) }
Expand Down Expand Up @@ -156,7 +154,7 @@ def self.avg_jobs_per_euro
end

def account_updated
financial_transactions.last.try(:created_on) || created_on
financial_transactions.last.try(:updated_on) || created_on
end

def self.sort_by_param(param)
Expand Down
15 changes: 14 additions & 1 deletion app/views/finance/financial_transactions/_transactions.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- with_ordergroup = local_assigns[:with_ordergroup]
- with_hidden = local_assigns[:with_hidden]
- with_csv = local_assigns[:with_csv]
- with_payment = @financial_transactions.with_payment_plugin.any?
.pull-right
- if with_csv
.btn-group
Expand All @@ -20,6 +21,12 @@
- if FinancialTransactionType.has_multiple_types
%th= heading_helper FinancialTransaction, :financial_transaction_type
%th= sort_link_helper heading_helper(FinancialTransaction, :note), "note"
- if with_payment
%th= heading_helper FinancialTransaction, :payment_plugin
%th= heading_helper FinancialTransaction, :payment_method
%th= heading_helper FinancialTransaction, :payment_amount
%th= heading_helper FinancialTransaction, :payment_fee
%th= heading_helper FinancialTransaction, :payment_state
- FinancialTransactionClass.sorted.each do |c|
%th
= sort_link_helper c.display, "amount"
Expand All @@ -43,13 +50,19 @@
= link_to t.note, t.group_order
- else
= t.note
- if with_payment
%td= t.payment_plugin
%td= t.payment_method
%td= format_currency t.payment_amount
%td= format_currency t.payment_fee
%td= t.payment_state
- FinancialTransactionClass.sorted.each do |c|
%td.numeric{style: 'width:5em'}
- if t.financial_transaction_type.financial_transaction_class == c
= format_currency t.amount
- if with_hidden
%td.actions{style: 'width:1em'}
- unless t.hidden?
- unless t.hidden? || t.amount.nil?
= link_to finance_transaction_path(t), method: :delete,
data: {confirm: t('.confirm_revert', name: t.note)}, title: t('.revert_title'),
class: 'btn btn-danger btn-mini' do
Expand Down
9 changes: 7 additions & 2 deletions app/views/finance/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- title t('.title')
- with_payment = @financial_transactions.with_payment_plugin.any?

.row-fluid
.span6
Expand Down Expand Up @@ -29,14 +30,18 @@
%th= heading_helper FinancialTransaction, :created_on
%th= heading_helper FinancialTransaction, :ordergroup
%th= heading_helper FinancialTransaction, :note
- if with_payment
%th= heading_helper FinancialTransaction, :payment_state
%th.numeric= heading_helper FinancialTransaction, :amount
%tbody
- @financial_transactions.each do |ft|
%tr
%td= format_date(ft.created_on)
%td= ft.ordergroup_name
%td= ft.note
%td.numeric= format_currency ft.amount
- if with_payment
%td= ft.payment_state
%td.numeric= format_currency(ft.amount)
.span6
%h2
= t('.open_transactions')
Expand All @@ -48,7 +53,7 @@
%th= heading_helper Order, :name
%th= t '.end'
%th.numeric= t('.amount_fc')
%th
%th
%tbody
- @orders.each do |order|
%tr
Expand Down
12 changes: 10 additions & 2 deletions app/views/home/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- '<dashboard_middle_mark>'

- if current_user.ordergroup

// Ordergroup overview
%section
%h2= t '.my_ordergroup.title'
Expand All @@ -63,27 +64,34 @@
= link_to my_ordergroup_path do
= t '.my_ordergroup.transactions.view'
%i.icon.icon-chevron-right
- '<dashboard_ordergroup_mark>'
%table.table.table-striped
- financial_transactions = current_user.ordergroup.financial_transactions.visible.includes(:financial_transaction_type, :user).limit(5).order(created_on: :desc)
- with_payment = financial_transactions.with_payment_plugin.any?
%tr
%th= heading_helper FinancialTransaction, :created_on
%th= heading_helper FinancialTransaction, :user
- if FinancialTransactionType.has_multiple_types
%th= heading_helper FinancialTransaction, :financial_transaction_type
%th= heading_helper FinancialTransaction, :note
- if with_payment
%th= heading_helper FinancialTransaction, :payment_state
- FinancialTransactionClass.sorted.each do |fc|
%th
= fc.display
- for ft in current_user.ordergroup.financial_transactions.visible.includes(:financial_transaction_type, :user).limit(5).order(created_on: :desc)
- for ft in financial_transactions
%tr
%td= format_time(ft.created_on)
%td= h(show_user(ft.user))
- if FinancialTransactionType.has_multiple_types
%td= h(ft.financial_transaction_type.name)
%td= h(ft.note)
- if with_payment
%td= h(ft.payment_state)
- FinancialTransactionClass.sorted.each do |fc|
%td.numeric{style: 'width:5em'}
- if ft.financial_transaction_type.financial_transaction_class == fc
= format_currency ft.amount
= format_currency(ft.amount)

-# placeholder deface to add content using erb[silent]:contains()
- '<dashboard_bottom_mark>'
1 change: 1 addition & 0 deletions app/views/home/ordergroup.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
= @ordergroup.memberships.map{|m| show_user m.user}.join(', ')
- unless FoodsoftConfig[:disable_invite]
= link_to t('.invite'), new_invite_path(:id => @ordergroup), :remote => true, class: 'btn btn-primary'
- '<home_ordergroup_well_mark>'
.span8
%h2= t('.account_summary')
.well.well-small
Expand Down
23 changes: 23 additions & 0 deletions db/migrate/20230915093041_add_payment_to_financial_transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class AddPaymentToFinancialTransaction < ActiveRecord::Migration[7.0]
def change
reversible do |dir|
dir.up do
change_column :financial_transactions, :amount, :decimal, precision: 8, scale: 2, default: nil, null: true
end
dir.down do
change_column :financial_transactions, :amount, :decimal, precision: 8, scale: 2, default: 0, null: false
end
end

add_column :financial_transactions, :updated_on, :timestamp
add_column :financial_transactions, :payment_method, :string
add_column :financial_transactions, :payment_plugin, :string
add_column :financial_transactions, :payment_id, :string
add_column :financial_transactions, :payment_amount, :decimal, precision: 8, scale: 3
add_column :financial_transactions, :payment_currency, :string
add_column :financial_transactions, :payment_state, :string
add_column :financial_transactions, :payment_fee, :decimal, precision: 8, scale: 3

add_index :financial_transactions, %i[payment_plugin payment_id]
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,24 @@

create_table "financial_transactions", id: :integer, charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t|
t.integer "ordergroup_id"
t.decimal "amount", precision: 8, scale: 2, default: "0.0", null: false
t.decimal "amount", precision: 8, scale: 2
t.text "note", null: false
t.integer "user_id", default: 0, null: false
t.datetime "created_on", precision: nil, null: false
t.integer "financial_transaction_type_id", null: false
t.integer "financial_link_id"
t.integer "reverts_id"
t.integer "group_order_id"
t.timestamp "updated_on"
t.string "payment_method"
t.string "payment_plugin"
t.string "payment_id"
t.decimal "payment_amount", precision: 8, scale: 3
t.string "payment_currency"
t.string "payment_state"
t.decimal "payment_fee", precision: 8, scale: 3
t.index ["ordergroup_id"], name: "index_financial_transactions_on_ordergroup_id"
t.index ["payment_plugin", "payment_id"], name: "index_financial_transactions_on_payment_plugin_and_payment_id"
t.index ["reverts_id"], name: "index_financial_transactions_on_reverts_id", unique: true
end

Expand Down
Loading

0 comments on commit 113a14d

Please sign in to comment.