Class: Ordergroup

Inherits:
Group
  • Object
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


117
118
119
120
# File 'app/models/ordergroup.rb', line 117

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

.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


122
123
124
# File 'app/models/ordergroup.rb', line 122

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

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

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


70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/ordergroup.rb', line 70

def add_financial_transaction!(amount, note, user, transaction_type, link = nil)
  transaction do
    t = FinancialTransaction.new(ordergroup: self, amount: amount, note: note, user: user, financial_transaction_type: transaction_type, financial_link: link)
    t.save!
    self. = financial_transactions.sum('amount')
    save!
    # 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


99
100
101
# File 'app/models/ordergroup.rb', line 99

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

#avg_jobs_per_euroObject


93
94
95
# File 'app/models/ordergroup.rb', line 93

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

#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


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

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


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

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

#last_user_activityObject


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

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)

107
108
109
110
111
112
113
114
# File 'app/models/ordergroup.rb', line 107

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_stats!Object


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

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


58
59
60
# File 'app/models/ordergroup.rb', line 58

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


54
55
56
# File 'app/models/ordergroup.rb', line 54

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