Module: Account::DiscountsHelper

Defined in:
app/helpers/account/discounts_helper.rb

Instance Method Summary collapse

Instance Method Details

#discount_lifetime_notes(lifetimes, value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/account/discounts_helper.rb', line 88

def discount_lifetime_notes(lifetimes, value)
  lifetimes = lifetimes.sort { |x, y| y.lifetime_type_id <=> x.lifetime_type_id }

  if lifetimes.size == 1 && lifetimes.first.present?
    item = lifetimes.first
    t(item.lifetime_type.code, scope: [:discount_notes], total: pretty_number(item.checkpoint.to_f),
                               words: pretty_number(item.checkpoint.to_f), value: pretty_number(value.to_f),
                               orders: pretty_number(item.checkpoint.to_f))
  elsif lifetimes.present?
    total = lifetimes.detect { |i| i.lifetime_type_id == LifetimeType.orders_total.id }
    words = lifetimes.detect { |i| i.lifetime_type_id == LifetimeType.words_count.id }
    t(:both, scope: [:discount_notes], total: satellite_price(total.checkpoint),
             words: number_with_delimiter(words.checkpoint.to_i), value: pretty_number(value.to_f))
  end
end

#discount_lifetime_title(lifetimes, _options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/helpers/account/discounts_helper.rb', line 69

def discount_lifetime_title(lifetimes, _options = {})
  return if lifetimes.blank?

  if lifetimes.size == 1 && item = lifetimes.first
    t(item.lifetime_type.code,
      scope: [:discount_title],
      total: satellite_price(item.checkpoint),
      words: number_with_delimiter(item.checkpoint.to_i),
      orders: number_with_delimiter(item.checkpoint.to_i))
  else
    total = lifetimes.select { |i| i.lifetime_type_id == LifetimeType.orders_total.id }.first
    words = lifetimes.select { |i| i.lifetime_type_id == LifetimeType.words_count.id }.first
    t(:both,
      scope: [:discount_title],
      total: satellite_price(total.checkpoint),
      words: number_with_delimiter(words.checkpoint.to_i))
  end
end

#discount_progress_percent(current, lifetimes, custom_mask = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/helpers/account/discounts_helper.rb', line 47

def discount_progress_percent(current, lifetimes, custom_mask = nil)
  return 0 if lifetimes.blank?

  current = current.to_f

  next_lifetime = lifetimes.detect { |l| l.checkpoint >= current }
  total = ((current / lifetimes.last.checkpoint) * 100).to_i

  return 100 if total > 100

  next_index = lifetimes.index(next_lifetime)
  prev_checkpoint = if next_index && next_index > 0
                      discounts_mask_progress(next_index - 1, custom_mask)
                    else
                      0
                    end

  progress = current.to_f / next_lifetime.checkpoint
  maxwidth = discounts_mask_progress(next_index, custom_mask) - prev_checkpoint
  (prev_checkpoint + progress * maxwidth).round(4)
end

#discount_progress_title(finance, lifetimes, value = nil) ⇒ Object



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
44
45
# File 'app/helpers/account/discounts_helper.rb', line 4

def discount_progress_title(finance, lifetimes, value = nil)
  completed_total = finance.try(:completed_payments_total).to_f || 0
  completed_words = finance.try(:completed_words_count).to_f || 0

  if value.nil?
    next_total = lifetimes.detect do |item|
      item.lifetime_type.orders_total? && item.checkpoint > completed_total
    end
    next_words = lifetimes.detect do |item|
      item.lifetime_type.words_count? && item.checkpoint > completed_words
    end
  else
    next_total = lifetimes.detect do |item|
      item.lifetime_type.orders_total? && item.try(:value).to_f == value
    end
    next_words = lifetimes.detect do |item|
      item.lifetime_type.words_count? && item.try(:value).to_f == value
    end
  end
  return if next_total.nil? && next_words.nil?

  diff_total = (next_total.try(:checkpoint).to_f - completed_total).round(2)
  diff_words = (next_words.try(:checkpoint).to_i - completed_words).to_i

  if next_total.try(:value) == next_words.try(:value)
    t(:both,
      scope: [:discount_progress],
      total: satellite_price(diff_total),
      words: number_with_delimiter(diff_words.to_i),
      value: next_total.value.to_i)
  elsif next_total.try(:value).to_i > next_words.try(:value).to_i
    t(:total,
      scope: [:discount_progress],
      total: satellite_price(diff_total),
      value: next_total.value.to_i)
  else
    t(:words,
      scope: [:discount_progress],
      words: number_with_delimiter(diff_words.to_i),
      value: next_words.value.to_i)
  end
end

#discounts_mask_progress(index, custom_mask = nil) ⇒ Object



116
117
118
119
120
# File 'app/helpers/account/discounts_helper.rb', line 116

def discounts_mask_progress(index, custom_mask = nil)
  custom_mask ||= {}
  custom_mask.reverse_merge('0' => 16.6, '1' => 50, '2' => 83.5)
  custom_mask[index.to_s]
end

#lifetime_item_class(value, account = nil, discounts = [5, 10, 15]) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'app/helpers/account/discounts_helper.rb', line 104

def lifetime_item_class(value,  = nil, discounts = [5, 10, 15])
  css = []
  css << case value.to_i
         when discounts[0] then 'first'
         when discounts[1] then 'second'
         when discounts[2] then 'third'
         end
  css << 'active' if  && .try(:lifetime_discount).to_i == value.to_i

  css.join(' ')
end