cirandas.net

ref: master

plugins/orders_cycle/lib/ext/profile.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require_dependency 'profile'
require_dependency 'community'

([Profile] + Profile.descendants).each do |subclass|
subclass.class_eval do

  has_many :orders_cycles, -> {
    order('created_at DESC').
    where "orders_cycle_plugin_cycles.status <> 'new'"
  }, foreign_key: :profile_id, class_name: 'OrdersCyclePlugin::Cycle', dependent: :destroy

  has_many :orders_cycles_without_order, -> {
    where "orders_cycle_plugin_cycles.status <> 'new'"
  }, foreign_key: :profile_id, class_name: 'OrdersCyclePlugin::Cycle'

  has_many :orders_cycles_sales, through: :orders_cycles, source: :sales
  has_many :orders_cycles_purchases, through: :orders_cycles, source: :purchases

  has_many :offered_products, -> { reorder 'products.name ASC' }, class_name: 'OrdersCyclePlugin::OfferedProduct'

end
end

class Profile

  def orders_cycles_closed_date_range
    list = self.orders_cycles.closing.order('start ASC').all
    return DateTime.now..DateTime.now if list.blank?
    list.first.start.to_date..list.last.finish.to_date
  end

  def orders_cycles_products_default_margins
    self.class.transaction do
      self.orders_cycles.opened.each do |cycle|
        cycle.products.each do |product|
          product.margin_percentage = margin_percentage
          product.save!
        end
      end
    end
  end

end