# frozen_string_literal: true module Account::DiscountsHelper 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 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 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 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 def lifetime_item_class(value, account = 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 account && account.try(:lifetime_discount).to_i == value.to_i css.join(' ') end 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 end