Class: Ordergroup

Inherits:
Group show all
Includes:
CustomFields
Defined in:
app/models/ordergroup.rb

Overview

Ordergroups can order, they are “children” of the class Group

Ordergroup have the following attributes, in addition to Group

  • account_balance (decimal)

Constant Summary collapse

APPLE_MONTH_AGO =

How many month back we will count tasks and orders sum

6

Instance Attribute Summary

Attributes included from CustomFields

#custom_fields

Attributes inherited from Group

#user_tokens

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Group

#deleted?, #mark_as_deleted, #member?

Methods included from MarkAsDeletedWithName

#mark_as_deleted

Class Method Details

.avg_jobs_per_euroObject

Global average


133
134
135
136
# File 'app/models/ordergroup.rb', line 133

def self.avg_jobs_per_euro
  stats = Ordergroup.pluck(:stats)
  stats.sum {|s| s[:jobs_size].to_f } / stats.sum {|s| s[:orders_sum].to_f } rescue 0
end

.custom_fieldsObject


42
43
44
45
46
# File 'app/models/ordergroup.rb', line 42

def self.custom_fields
  fields = FoodsoftConfig[:custom_fields] && FoodsoftConfig[:custom_fields][:ordergroup]
  return [] unless fields
  fields.map(&:deep_symbolize_keys)
end

.include_transaction_class_sumObject


30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/ordergroup.rb', line 30

def self.include_transaction_class_sum
  columns = ['groups.*']
  FinancialTransactionClass.all.each do |c|
    columns << "sum(CASE financial_transaction_types.financial_transaction_class_id WHEN #{c.id} THEN financial_transactions.amount ELSE 0 END) AS sum_of_class_#{c.id}"
  end

  select(columns.join(', '))
    .joins('LEFT JOIN financial_transactions ON groups.id = financial_transactions.ordergroup_id')
    .joins('LEFT JOIN financial_transaction_types ON financial_transaction_types.id = financial_transactions.financial_transaction_type_id')
    .group('groups.id')
end

Instance Method Details

#account_updatedObject


138
139
140
# File 'app/models/ordergroup.rb', line 138

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

#add_financial_transaction!(amount, note, user, transaction_type, link = nil, group_order = nil) ⇒ Object

Creates a new FinancialTransaction for this Ordergroup and updates the account_balance accordingly. Throws an exception if it fails.


83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/ordergroup.rb', line 83

def add_financial_transaction!(amount, note, user, transaction_type, link = nil, group_order = nil)
  transaction do
    t = FinancialTransaction.new(ordergroup: self, amount: amount, note: note, user: user, financial_transaction_type: transaction_type, financial_link: link, group_order: group_order)
    t.save!
    update_balance!
    # Notify only when order group had a positive balance before the last transaction:
    if t.amount < 0 && self. < 0 && self. - t.amount >= 0
      Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'negative_balance', self.id, t.id)
    end
  end
end

#applesObject

This is the ordergroup job per euro performance in comparison to the hole foodcoop average


115
116
117
# File 'app/models/ordergroup.rb', line 115

def apples
  ((avg_jobs_per_euro / Ordergroup.avg_jobs_per_euro) * 100).to_i rescue 0
end

#avg_jobs_per_euroObject


109
110
111
# File 'app/models/ordergroup.rb', line 109

def avg_jobs_per_euro
  stats[:jobs_size].to_f / stats[:orders_sum].to_f rescue 0
end

#contactObject


23
24
25
# File 'app/models/ordergroup.rb', line 23

def contact
  "#{contact_phone} (#{contact_person})"
end

#financial_transaction_class_balance(klass) ⇒ Object


74
75
76
77
78
79
# File 'app/models/ordergroup.rb', line 74

def financial_transaction_class_balance(klass)
  financial_transactions
    .joins(:financial_transaction_type)
    .where(financial_transaction_types: {financial_transaction_class_id: klass})
    .sum(:amount)
end

#get_available_funds(exclude = nil) ⇒ Object

Returns the available funds for this order group (the account_balance minus price of all non-closed GroupOrders of this group).

  • exclude (GroupOrder): exclude this GroupOrder from the calculation


70
71
72
# File 'app/models/ordergroup.rb', line 70

def get_available_funds(exclude = nil)
   - value_of_open_orders(exclude) - value_of_finished_orders(exclude)
end

#last_orderObject

the most recent order this ordergroup was participating in


56
57
58
# File 'app/models/ordergroup.rb', line 56

def last_order
  orders.order('orders.starts DESC').first
end

#last_user_activityObject


48
49
50
51
52
53
# File 'app/models/ordergroup.rb', line 48

def last_user_activity
  last_active_user = users.order('users.last_activity DESC').first
  if last_active_user
    last_active_user.last_activity
  end
end

#non_membersObject


26
27
28
# File 'app/models/ordergroup.rb', line 26

def non_members
  User.natural_order.all.reject { |u| (users.include?(u) || u.ordergroup) }
end

#not_enough_apples?Boolean

If the the option stop_ordering_under is set, the ordergroup is only allowed to participate in an order, when the apples value is above the configured amount. The restriction can be deactivated for each ordergroup. Only ordergroups, which have participated in more than 5 orders in total and more than 2 orders in apple time period

Returns:

  • (Boolean)

123
124
125
126
127
128
129
130
# File 'app/models/ordergroup.rb', line 123

def not_enough_apples?
  FoodsoftConfig[:use_apple_points] &&
      FoodsoftConfig[:stop_ordering_under].present? &&
      !ignore_apple_restriction &&
      apples < FoodsoftConfig[:stop_ordering_under] &&
      group_orders.count > 5 &&
      group_orders.joins(:order).merge(Order.finished).where('orders.ends >= ?', APPLE_MONTH_AGO.month.ago).count > 2
end

#update_balance!Object


105
106
107
# File 'app/models/ordergroup.rb', line 105

def update_balance!
  update_attribute :account_balance, financial_transactions.sum('amount')
end

#update_stats!Object


95
96
97
98
99
100
101
102
103
# File 'app/models/ordergroup.rb', line 95

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) }
  # Get group_order.price for every finished order in this period
  orders_sum = group_orders.includes(:order).merge(Order.finished).where('orders.ends >= ?', APPLE_MONTH_AGO.month.ago).references(:orders).sum(:price)

  @readonly = false # Dirty hack, avoid getting RecordReadOnly exception when called in task after_save callback. A rails bug?
  update_attribute(:stats, {:jobs_size => jobs, :orders_sum => orders_sum})
end

#value_of_finished_orders(exclude = nil) ⇒ Object


64
65
66
# File 'app/models/ordergroup.rb', line 64

def value_of_finished_orders(exclude = nil)
  group_orders.in_finished_orders.reject{|go| go == exclude}.collect(&:price).sum
end

#value_of_open_orders(exclude = nil) ⇒ Object


60
61
62
# File 'app/models/ordergroup.rb', line 60

def value_of_open_orders(exclude = nil)
  group_orders.in_open_orders.reject{|go| go == exclude}.collect(&:price).sum
end