Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mortbauer committed May 23, 2024
2 parents 72c4242 + ad05870 commit e6da86f
Show file tree
Hide file tree
Showing 64 changed files with 1,504 additions and 112 deletions.
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Metrics/BlockNesting:
# Offense count: 18
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 294
Max: 304

# Offense count: 51
# Configuration parameters: AllowedMethods, AllowedPatterns.
Expand All @@ -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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* incompatible uri gem ([bae984b](https://github.com/foodcoops/foodsoft/commit/bae984bb3e760bb863410374216c7f6ef50fe93a))
* update ruby image, fix bundler version ([c450368](https://github.com/foodcoops/foodsoft/commit/c45036810eeced044a58dac615a629a7b1fb78d0))
* bug on saving config, fixes [#801](https://github.com/foodcoops/foodsoft/issues/801) ([8b152bf](https://github.com/foodcoops/foodsoft/commit/8b152bfbc0ef375f1f180c92006b62d241c83a2d))
* active storage links for attachments ([#1045](https://github.com/foodcoops/foodsoft/issues/1045)) ([4b7356e](https://github.com/foodcoops/foodsoft/commit/4b7356e7e4993f44590cd62dc9f24642cb94c89a))
* allow_other_host for discourse plugin redirects ([#1043](https://github.com/foodcoops/foodsoft/issues/1043)) ([8836697](https://github.com/foodcoops/foodsoft/commit/8836697ed7d9d3137d7797067a2db9e9208a6440))
* **finance:** ordergroup overview total balances ([#1051](https://github.com/foodcoops/foodsoft/issues/1051)) ([a5861f5](https://github.com/foodcoops/foodsoft/commit/a5861f55855dfbe5ce343d4d1896121ccf14471d))

### Features

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ gem 'foodsoft_wiki', path: 'plugins/wiki'
# gem 'foodsoft_current_orders', path: 'plugins/current_orders'
gem 'foodsoft_printer', path: 'plugins/printer'
# gem 'foodsoft_uservoice', path: 'plugins/uservoice'
# gem 'foodsoft_mollie', path: 'plugins/mollie'

group :development do
gem 'listen'
Expand All @@ -99,6 +100,7 @@ group :development do
end

group :development, :test do
gem 'rails-erd', '~> 1.7'
gem 'rubocop', require: false
gem 'rubocop-rails', require: false
gem 'rubocop-rspec', require: false
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ GEM
xpath (~> 3.2)
case_transform (0.2)
activesupport
choice (0.2.0)
chronic (0.10.2)
coderay (1.1.3)
commonjs (0.2.7)
Expand Down Expand Up @@ -403,6 +404,11 @@ GEM
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-erd (1.7.2)
activerecord (>= 4.2)
activesupport (>= 4.2)
choice (~> 0.2.0)
ruby-graphviz (~> 1.2)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
Expand Down Expand Up @@ -515,6 +521,8 @@ GEM
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
ruby-filemagic (0.7.3)
ruby-graphviz (1.2.5)
rexml
ruby-ole (1.2.12.2)
ruby-prof (1.6.3)
ruby-progressbar (1.13.0)
Expand Down Expand Up @@ -681,6 +689,7 @@ DEPENDENCIES
rack-cors
rails (~> 7.0.8)
rails-assets-listjs (= 0.2.0.beta.4)
rails-erd (~> 1.7)
rails-i18n
rails-settings-cached (= 0.4.3)
rails_tokeninput
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
3 changes: 2 additions & 1 deletion app/controllers/finance/balancing_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def confirm
def close
@order = Order.find(params[:id])
@type = FinancialTransactionType.find_by_id(params.permit(:type)[:type])
@order.close!(@current_user, @type)
@link = FinancialLink.new if params[:create_financial_link]
@order.close!(@current_user, @type, @link, create_foodcoop_transaction: params[:create_foodcoop_transaction])
redirect_to finance_order_index_url, notice: t('.notice')
rescue StandardError => e
redirect_to new_finance_order_url(order_id: @order.id), alert: t('.alert', message: e.message)
Expand Down
11 changes: 8 additions & 3 deletions app/controllers/finance/financial_links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ def show
}
end
@items += @financial_link.financial_transactions.map do |ft|
ft_note =
if ft.group_order
view_context.link_to ft.note, new_finance_order_path(order_id: ft.group_order.order.id)
else
ft.note
end
{
date: ft.created_on,
type: t('activerecord.models.financial_transaction'),
description: "#{ft.ordergroup_name}: #{ft.note}",
description: "#{ft.ordergroup_name}: #{ft_note}",
amount: ft.amount,
link_to: finance_group_transactions_path(ft.ordergroup),
remove_path: remove_financial_transaction_finance_link_path(@financial_link, ft)
Expand Down Expand Up @@ -80,8 +86,7 @@ def create_financial_transaction
end

def index_financial_transaction
@financial_transactions = FinancialTransaction.without_financial_link.includes(:financial_transaction_type,
:ordergroup)
@financial_transactions = FinancialTransaction.without_financial_link.includes(:financial_transaction_type, :ordergroup, :group_order)
end

def add_financial_transaction
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/finance/financial_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def create

respond_to do |format|
format.js
format.html { redirect_to finance_group_transactions_path(@ordergroup), notice: I18n.t('finance.financial_transactions.controller.create.notice') }
redirect_to finance_group_transactions_path(@ordergroup),
notice: I18n.t('finance.financial_transactions.controller.create.notice')
end
rescue ActiveRecord::RecordInvalid => error
@error = error
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/finance/ordergroups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def index
@ordergroups = @ordergroups.where('groups.name LIKE ?', "%#{params[:query]}%") unless params[:query].nil?
@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 }
@total_balances = FinancialTransactionClass.sorted.each_with_object({}) do |transaction_class, total_balances|
total_balances[transaction_class.id] = @ordergroups.reduce(0) { |sum, o| o["sum_of_class_#{transaction_class.id}"] + sum }
end
end
end
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
17 changes: 13 additions & 4 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,13 @@ def finish!(user)
end

# Sets order.status to 'close' and updates all Ordergroup.account_balances
def close!(user, transaction_type = nil)
def close!(user, transaction_type = nil, financial_link = nil, create_foodcoop_transaction: false)
raise I18n.t('orders.model.error_closed') if closed?

update_price_of_group_orders!

transaction do # Start updating account balances
charge_group_orders!(user, transaction_type)
charge_group_orders!(user, transaction_type, financial_link)

if stockit? # Decreases the quantity of stock_articles
for oa in order_articles.includes(:article)
Expand All @@ -289,6 +289,15 @@ def close!(user, transaction_type = nil)
end
end

if create_foodcoop_transaction
ft = FinancialTransaction.new({ financial_transaction_type: transaction_type,
user: user,
amount: sum(:groups),
note: transaction_note,
financial_link: financial_link })
ft.save!
end

update!(state: 'closed', updated_by: user, foodcoop_result: profit)
end
end
Expand Down Expand Up @@ -400,12 +409,12 @@ def update_price_of_group_orders!
group_orders.each(&:update_price!)
end

def charge_group_orders!(user, transaction_type = nil)
def charge_group_orders!(user, transaction_type = nil, financial_link = nil)
note = transaction_note
group_orders.includes(:ordergroup).find_each do |group_order|
if group_order.ordergroup
price = group_order.total * -1 # decrease! account balance
group_order.ordergroup.add_financial_transaction!(price, note, user, transaction_type, nil, group_order)
group_order.ordergroup.add_financial_transaction!(price, note, user, transaction_type, financial_link, group_order)
end
end
end
Expand Down
8 changes: 3 additions & 5 deletions app/models/ordergroup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,14 @@ 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

t
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 @@ -159,7 +157,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
1 change: 1 addition & 0 deletions app/views/admin/configs/_tab_payment.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
= config_input_field form, :minimum_balance, as: :decimal, class: 'input-small'
= config_input form, :charge_members_manually, as: :boolean
= config_input form, :use_iban, as: :boolean
= config_input form, :use_financial_links, as: :boolean
= config_input form, :use_self_service, as: :boolean

%h4= t '.schedule_title'
Expand Down
8 changes: 8 additions & 0 deletions app/views/finance/balancing/confirm.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
%tr{:class => cycle('even', 'odd')}
%td= group_order.ordergroup_name
%td.numeric= number_to_currency(group_order.total)
- if FoodsoftConfig[:use_financial_links]
%p
%label
= check_box_tag :create_financial_link, true, params.fetch(:create_financial_link, true)
= t('.create_financial_link')
%label
= check_box_tag :create_foodcoop_transaction, true, params.fetch(:create_foodcoop_transaction, true)
= t('.create_foodcoop_transaction', sum: number_to_currency(@order.sum(:groups)))
.form-actions
= submit_tag t('.clear'), class: 'btn btn-primary'
= link_to t('.or_cancel'), new_finance_order_path(order_id: @order.id)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
%tr
%th= heading_helper FinancialTransaction, :created_on
%th= heading_helper FinancialTransaction, :ordergroup
%th= heading_helper FinancialTransaction, :note
- if FinancialTransactionType.has_multiple_types
%th= heading_helper FinancialTransaction, :financial_transaction_type
%th= heading_helper FinancialTransaction, :amount
Expand All @@ -15,6 +16,7 @@
%tr
%td= link_to format_time(t.created_on), add_financial_transaction_finance_link_path(@financial_link, financial_transaction: t.id), method: :put
%td= t.ordergroup_name
%td= render 'finance/financial_transactions/order_note', financial_transaction:t, with_grouporder_link: false
- if FinancialTransactionType.has_multiple_types
%td= h t.financial_transaction_type.name
%td= number_to_currency t.amount
Expand Down
4 changes: 2 additions & 2 deletions app/views/finance/financial_links/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
- else
= format_date item[:date]
%td= item[:type]
%td= item[:description]
%td= sanitize item[:description]
%td.numeric{style: 'width:5em'}= format_currency item[:amount]
%td= link_to t('ui.delete'), item[:remove_path], :data => {:confirm => t('ui.confirm_delete', name: item[:description])}, :method => :delete,
%td= link_to t('.remove_from_link'), item[:remove_path], :data => {:confirm => t('.remove_from_link_confirm')}, :method => :delete,
class: 'btn btn-danger btn-mini'

%p
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- financial_transaction = local_assigns[:financial_transaction]
- with_grouporder_link = local_assigns[:with_grouporder_link]
- if financial_transaction.group_order
- if with_grouporder_link
= link_to financial_transaction.note, financial_transaction.group_order
- else
= link_to financial_transaction.note, new_finance_order_path(order_id: financial_transaction.group_order.order.id)
- else
= financial_transaction.note
Loading

0 comments on commit e6da86f

Please sign in to comment.