• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

foodcoops / foodsoft / 1925

pending completion
1925

Pull #683

travis-ci

web-flow
Change deprecated *_filter methods to *_action
Pull Request #683: Change deprecated *_filter methods to *_action

8 of 8 new or added lines in 6 files covered. (100.0%)

26728 of 43903 relevant lines covered (60.88%)

1.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.89
/app/models/ordergroup.rb
1
# encoding: utf-8
2
#
3
# Ordergroups can order, they are "children" of the class Group
4
# 
5
# Ordergroup have the following attributes, in addition to Group
6
# * account_balance (decimal)
7
class Ordergroup < Group
1✔
8
  include CustomFields
1✔
9

10
  APPLE_MONTH_AGO = 6                 # How many month back we will count tasks and orders sum
1✔
11

12
  serialize :stats
1✔
13

14
  has_many :financial_transactions
1✔
15
  has_many :group_orders
1✔
16
  has_many :orders, :through => :group_orders
1✔
17

18
  validates_numericality_of :account_balance, :message => I18n.t('ordergroups.model.invalid_balance')
1✔
19
  validate :uniqueness_of_name, :uniqueness_of_members
1✔
20

21
  after_create :update_stats!
1✔
22

23
  def contact
1✔
24
    "#{contact_phone} (#{contact_person})"
×
25
  end
26
  def non_members
1✔
27
    User.natural_order.all.reject { |u| (users.include?(u) || u.ordergroup) }
×
28
  end
29

30
  def self.include_transaction_class_sum
1✔
31
    columns = ['groups.*']
×
32
    FinancialTransactionClass.all.each do |c|
×
33
      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}"
×
34
    end
35

36
    select(columns.join(', '))
37
      .joins('LEFT JOIN financial_transactions ON groups.id = financial_transactions.ordergroup_id')
38
      .joins('LEFT JOIN financial_transaction_types ON financial_transaction_types.id = financial_transactions.financial_transaction_type_id')
39
      .group('groups.id')
×
40
  end
41

42
  def self.custom_fields
1✔
43
    fields = FoodsoftConfig[:custom_fields] && FoodsoftConfig[:custom_fields][:ordergroup]
×
44
    return [] unless fields
×
45
    fields.map(&:deep_symbolize_keys)
×
46
  end
47

48
  def last_user_activity
1✔
49
    last_active_user = users.order('users.last_activity DESC').first
×
50
    if last_active_user
×
51
      last_active_user.last_activity
×
52
    end
53
  end
54

55
  # the most recent order this ordergroup was participating in
56
  def last_order
1✔
57
    orders.order('orders.starts DESC').first
×
58
  end
59

60
  def value_of_open_orders(exclude = nil)
1✔
61
    group_orders.in_open_orders.reject{|go| go == exclude}.collect(&:price).sum
×
62
  end
63
  
64
  def value_of_finished_orders(exclude = nil)
1✔
65
    group_orders.in_finished_orders.reject{|go| go == exclude}.collect(&:price).sum
×
66
  end
67

68
  # Returns the available funds for this order group (the account_balance minus price of all non-closed GroupOrders of this group).
69
  # * exclude (GroupOrder): exclude this GroupOrder from the calculation
70
  def get_available_funds(exclude = nil)
1✔
71
    account_balance - value_of_open_orders(exclude) - value_of_finished_orders(exclude)
×
72
  end
73

74
  def financial_transaction_class_balance(klass)
1✔
75
    financial_transactions
76
      .joins(:financial_transaction_type)
77
      .where(financial_transaction_types: {financial_transaction_class_id: klass})
78
      .sum(:amount)
×
79
  end
80

81
  # Creates a new FinancialTransaction for this Ordergroup and updates the account_balance accordingly.
82
  # Throws an exception if it fails.
83
  def add_financial_transaction!(amount, note, user, transaction_type, link = nil)
1✔
84
    transaction do
×
85
      t = FinancialTransaction.new(ordergroup: self, amount: amount, note: note, user: user, financial_transaction_type: transaction_type, financial_link: link)
×
86
      t.save!
×
87
      self.account_balance = financial_transactions.sum('amount')
×
88
      save!
×
89
      # Notify only when order group had a positive balance before the last transaction:
90
      if t.amount < 0 && self.account_balance < 0 && self.account_balance - t.amount >= 0
×
91
        Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'negative_balance', self.id, t.id)
×
92
      end
93
    end
94
  end
95

96
  def update_stats!
1✔
97
    # Get hours for every job of each user in period
98
    jobs = users.to_a.sum { |u| u.tasks.done.where('updated_on > ?', APPLE_MONTH_AGO.month.ago).sum(:duration) }
6✔
99
    # Get group_order.price for every finished order in this period
100
    orders_sum = group_orders.includes(:order).merge(Order.finished).where('orders.ends >= ?', APPLE_MONTH_AGO.month.ago).references(:orders).sum(:price)
4✔
101

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

106
  def avg_jobs_per_euro
1✔
107
    stats[:jobs_size].to_f / stats[:orders_sum].to_f rescue 0
×
108
  end
109

110
  # This is the ordergroup job per euro performance 
111
  # in comparison to the hole foodcoop average
112
  def apples
1✔
113
    ((avg_jobs_per_euro / Ordergroup.avg_jobs_per_euro) * 100).to_i rescue 0
×
114
  end
115

116
  # If the the option stop_ordering_under is set, the ordergroup is only allowed to participate in an order,
117
  # when the apples value is above the configured amount.
118
  # The restriction can be deactivated for each ordergroup.
119
  # Only ordergroups, which have participated in more than 5 orders in total and more than 2 orders in apple time period
120
  def not_enough_apples?
1✔
121
    FoodsoftConfig[:use_apple_points] &&
122
        FoodsoftConfig[:stop_ordering_under].present? &&
×
123
        !ignore_apple_restriction &&
124
        apples < FoodsoftConfig[:stop_ordering_under] &&
125
        group_orders.count > 5 &&
126
        group_orders.joins(:order).merge(Order.finished).where('orders.ends >= ?', APPLE_MONTH_AGO.month.ago).count > 2
127
  end
128

129
  # Global average
130
  def self.avg_jobs_per_euro
1✔
131
    stats = Ordergroup.pluck(:stats)
×
132
    stats.sum {|s| s[:jobs_size].to_f } / stats.sum {|s| s[:orders_sum].to_f } rescue 0
×
133
  end
134

135
  def account_updated
1✔
136
    financial_transactions.last.try(:created_on) || created_on
×
137
  end
138
  
139
  private
1✔
140

141
  # Make sure, that a user can only be in one ordergroup
142
  def uniqueness_of_members
1✔
143
    users.each do |user|
×
144
      errors.add :user_tokens, I18n.t('ordergroups.model.error_single_group', :user => user.display) if user.groups.where(:type => 'Ordergroup').size > 1
×
145
    end
146
  end
147

148
  # Make sure, the name is uniq, add usefull message if uniq group is already deleted
149
  def uniqueness_of_name
1✔
150
    group = Ordergroup.where(name: name)
×
151
    group = group.where.not(id: self.id) unless new_record?
×
152
    if group.exists?
×
153
      message = group.first.deleted? ? :taken_with_deleted : :taken
×
154
      errors.add :name, message
×
155
    end
156
  end
157
 
158
end
159

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2024 Coveralls, Inc