Allow deletion of financial transactions

This commit is contained in:
Patrick Gansterer 2019-11-01 19:30:23 +01:00
parent 260ef90f6b
commit ff76fa60c0
14 changed files with 79 additions and 8 deletions

View file

@ -537,3 +537,7 @@ span.positive_amount {
span.negative_amout {
color: red;
}
.deleted_row {
text-decoration: line-through;
}

View file

@ -21,6 +21,7 @@ class Finance::FinancialTransactionsController < ApplicationController
@q = FinancialTransaction.search(params[:q])
@financial_transactions_all = @q.result(distinct: true).includes(:user).order(sort)
@financial_transactions_all = @financial_transactions_all.visible unless params[:show_hidden]
@financial_transactions_all = @financial_transactions_all.where(ordergroup_id: @ordergroup.id) if @ordergroup
@financial_transactions = @financial_transactions_all.page(params[:page]).per(@per_page)
@ -50,6 +51,12 @@ class Finance::FinancialTransactionsController < ApplicationController
render :action => :new
end
def destroy
transaction = FinancialTransaction.find(params[:id])
transaction.revert!(current_user)
redirect_to finance_ordergroup_transactions_url(transaction.ordergroup), notice: t('finance.financial_transactions.controller.destroy.notice')
end
def new_collection
end

View file

@ -45,7 +45,7 @@ class HomeController < ApplicationController
sort = "created_on DESC"
end
@financial_transactions = @ordergroup.financial_transactions.page(params[:page]).per(@per_page).order(sort)
@financial_transactions = @ordergroup.financial_transactions.visible.page(params[:page]).per(@per_page).order(sort)
@financial_transactions = @financial_transactions.where("note LIKE ?", "%#{params[:query]}%") if params[:query].present?
else

View file

@ -5,11 +5,14 @@ class FinancialTransaction < ApplicationRecord
belongs_to :user
belongs_to :financial_link
belongs_to :financial_transaction_type
belongs_to :reverts, class_name: 'FinancialTransaction', foreign_key: 'reverts_id'
has_one :reverted_by, class_name: 'FinancialTransaction', foreign_key: 'reverts_id'
validates_presence_of :amount, :note, :user_id, :ordergroup_id
validates_numericality_of :amount, greater_then: -100_000,
less_than: 100_000
scope :visible, -> { 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) }
localize_input_of :amount
@ -23,6 +26,21 @@ class FinancialTransaction < ApplicationRecord
ordergroup.add_financial_transaction! amount, note, user, financial_transaction_type
end
def revert!(user)
transaction do
rt = dup
rt.amount = -rt.amount
rt.reverts = self
rt.user = user
rt.save!
ordergroup.update_balance!
end
end
def hidden?
reverts.present? || reverted_by.present?
end
protected
def initialize_financial_transaction_type

View file

@ -84,8 +84,7 @@ class Ordergroup < Group
transaction do
t = FinancialTransaction.new(ordergroup: self, amount: amount, note: note, user: user, financial_transaction_type: transaction_type, financial_link: link)
t.save!
self.account_balance = financial_transactions.sum('amount')
save!
update_balance!
# Notify only when order group had a positive balance before the last transaction:
if t.amount < 0 && self.account_balance < 0 && self.account_balance - t.amount >= 0
Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'negative_balance', self.id, t.id)
@ -103,6 +102,10 @@ class Ordergroup < Group
update_attribute(:stats, {:jobs_size => jobs, :orders_sum => orders_sum})
end
def update_balance!
update_attribute :account_balance, financial_transactions.sum('amount')
end
def avg_jobs_per_euro
stats[:jobs_size].to_f / stats[:orders_sum].to_f rescue 0
end

View file

@ -1,4 +1,5 @@
- with_ordergroup = local_assigns[:with_ordergroup]
- with_hidden = local_assigns[:with_hidden]
- with_csv = local_assigns[:with_csv]
.pull-right
- if with_csv
@ -22,9 +23,11 @@
- FinancialTransactionClass.sorted.each do |c|
%th
= sort_link_helper c.display, "amount"
- if with_hidden
%th
%tbody
- @financial_transactions.each do |t|
%tr
%tr{class: "#{'deleted_row' if t.hidden?}"}
%td
- if t.financial_link
= link_to format_time(t.created_on), finance_link_path(t.financial_link)
@ -40,3 +43,10 @@
%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?
= link_to finance_ordergroup_transaction_path(t.ordergroup, t), method: :delete,
data: {confirm: t('.confirm_revert', name: t.note)}, title: t('.revert_title'),
class: 'btn btn-danger btn-mini' do
= glyph :remove

View file

@ -19,3 +19,8 @@
= f.text_field :amount_gteq, class: 'input-mini'
%span.add-on -
= f.text_field :amount_lteq, class: 'input-mini search-query'
&nbsp;
%label{for: 'show_hidden'}
= check_box_tag 'show_hidden', 1, params[:show_hidden]
= t '.show_hidden'

View file

@ -22,4 +22,4 @@
= render 'transactions_search', url: finance_ordergroup_transactions_path(@ordergroup)
#transactions= render 'transactions', with_csv: true
#transactions= render 'transactions', with_csv: true, with_hidden: true

View file

@ -1 +1 @@
$('#transactions').html('<%= escape_javascript(render("transactions", with_csv: true)) %>');
$('#transactions').html('<%= escape_javascript(render("transactions", with_csv: true, with_hidden: true)) %>');

View file

@ -73,7 +73,7 @@
- FinancialTransactionClass.sorted.each do |fc|
%th
= fc.display
- for ft in current_user.ordergroup.financial_transactions.includes(:financial_transaction_type, :user).limit(5).order('created_on DESC')
- for ft in current_user.ordergroup.financial_transactions.visible.includes(:financial_transaction_type, :user).limit(5).order(created_on: :desc)
%tr
%td= format_time(ft.created_on)
%td= h(show_user(ft.user))