adapt financial transaction type for online payment provider
This commit is contained in:
parent
c59eefd219
commit
2ef17ceca4
10 changed files with 66 additions and 25 deletions
|
|
@ -50,11 +50,7 @@ class Finance::FinancialTransactionsController < ApplicationController
|
|||
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
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ class Finance::OrdergroupsController < Finance::BaseController
|
|||
@ordergroups = @ordergroups.page(params[:page]).per(@per_page)
|
||||
|
||||
@total_balances = FinancialTransactionClass.sorted.each_with_object({}) do |c, tmp|
|
||||
tmp[c.id] = c.financial_transactions.reduce(0) { |sum, t| sum + t.amount }
|
||||
tmp[c.id] = c.financial_transactions.reduce(0) { |sum, t| sum + (t.amount || 0) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -11,9 +11,11 @@ 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 }
|
||||
|
||||
scope :visible, lambda {
|
||||
joins('LEFT JOIN financial_transactions r ON financial_transactions.id = r.reverts_id').where('r.id IS NULL').where(reverts: nil)
|
||||
|
|
@ -23,6 +25,8 @@ class FinancialTransaction < ApplicationRecord
|
|||
|
||||
localize_input_of :amount
|
||||
|
||||
after_save :update_ordergroup_balance
|
||||
|
||||
after_initialize do
|
||||
initialize_financial_transaction_type
|
||||
end
|
||||
|
|
@ -38,16 +42,11 @@ class FinancialTransaction < ApplicationRecord
|
|||
%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)
|
||||
transaction do
|
||||
update_attribute :financial_link, FinancialLink.new
|
||||
rt = dup
|
||||
rt.amount = -rt.amount
|
||||
rt.amount = rt.amount ? -rt.amount : nil
|
||||
rt.reverts = self
|
||||
rt.user = user
|
||||
rt.save!
|
||||
|
|
@ -73,4 +72,12 @@ class FinancialTransaction < ApplicationRecord
|
|||
def initialize_financial_transaction_type
|
||||
self.financial_transaction_type ||= FinancialTransactionType.default
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_ordergroup_balance
|
||||
# @todo Make sure this transaction and the ordergroup update is in one database transaction.
|
||||
# It may be possible to use an around filter if needed.
|
||||
ordergroup.update_balance!
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -91,14 +91,12 @@ class Ordergroup < Group
|
|||
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) }
|
||||
|
|
@ -156,7 +154,7 @@ class Ordergroup < Group
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@
|
|||
= t.note
|
||||
- FinancialTransactionClass.sorted.each do |c|
|
||||
%td.numeric{style: 'width:5em'}
|
||||
- if t.financial_transaction_type.financial_transaction_class == c
|
||||
- if t.financial_transaction_type.financial_transaction_class == c && t.amount
|
||||
= format_currency t.amount
|
||||
- if with_hidden
|
||||
%td.actions{style: 'width:1em'}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@
|
|||
%td= format_date(ft.created_on)
|
||||
%td= ft.ordergroup_name
|
||||
%td= ft.note
|
||||
%td.numeric= format_currency ft.amount
|
||||
%td.numeric= ft.amount ? format_currency(ft.amount) : '-'
|
||||
.span6
|
||||
%h2
|
||||
= t('.open_transactions')
|
||||
|
|
@ -48,7 +48,7 @@
|
|||
%th= heading_helper Order, :name
|
||||
%th= t '.end'
|
||||
%th.numeric= t('.amount_fc')
|
||||
%th
|
||||
%th
|
||||
%tbody
|
||||
- @orders.each do |order|
|
||||
%tr
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@
|
|||
= link_to my_ordergroup_path do
|
||||
= t '.my_ordergroup.transactions.view'
|
||||
%i.icon.icon-chevron-right
|
||||
- '<dashboard_ordergroup_mark>'
|
||||
%table.table.table-striped
|
||||
%tr
|
||||
%th= heading_helper FinancialTransaction, :created_on
|
||||
|
|
@ -83,7 +84,7 @@
|
|||
- FinancialTransactionClass.sorted.each do |fc|
|
||||
%td.numeric{style: 'width:5em'}
|
||||
- if ft.financial_transaction_type.financial_transaction_class == fc
|
||||
= format_currency ft.amount
|
||||
= ft.amount ? format_currency(ft.amount) : '-'
|
||||
|
||||
-# placeholder deface to add content using erb[silent]:contains()
|
||||
- '<dashboard_bottom_mark>'
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue