<commit_before>#encoding: utf-8
module Plugins::Ecommerce::EcommerceFunctionsHelper
  # return the visitor key which is used to relate the cart until login/register
  def ecommerce_get_visitor_key
    cookies[:e_cart_id] ||= cama_get_session_id unless cama_current_user.present?
  end

  # return the current cart for current user
  def e_current_cart(custom_visitor_key = nil)
    @_cache_e_current_cart ||= current_site.carts.set_user(custom_visitor_key || cama_current_user || ecommerce_get_visitor_key).active_cart.first_or_create(name: "Cart by #{Time.current.to_s}").decorate
  end

  # return all shipping country codes supported for shipping
  def e_shipping_countries
    current_site.e_settings[:shipping_countries] || ISO3166::Country.codes
  end

  # return (Array) all the currencies for visitors
  def e_visitor_unit_currencies
    current_site.e_settings[:visitor_unit_currencies] || ['USD']
  end

  # draw a select dropdown with all frontend currencies and actions to change current currency
  # long_mode: (Boolean, default false)
  # attrs = (Hash) attributes of the select drodown, sample: {class: 'form-control'}
  def e_draw_ecommerce_currencies(long_mode = false, attrs = {})
    return '' if e_visitor_unit_currencies.count <= 1
    res = '<select '+attrs.collect{|k, v| "#{k}='#{v}'" }.join(" ")+' onchange="window.location.href=window.location.href.split(\'cama_change_currency\')[0]+(window.location.href.search(\'\\\?\') > 1 ? \'&\' : \'?\')+\'cama_change_currency=\'+this.value">'
    e_visitor_unit_currencies.each do |unit|
      cur = e_get_currency_by_code(unit)
      res << "<option value='#{unit}' #{'selected' if e_current_visitor_currency == unit}>#{long_mode ? "#{cur[:label]} (#{cur[:symbol]})": cur[:symbol]}</option>"
    end
    res << '</select>'
  end

  # set a new currency if new_currency is present
  # return current currency defined by the current visitor
  def e_current_visitor_currency(new_currency = nil)
    session[:e_current_visitor_currency] = new_currency if new_currency.present?
    session[:e_current_visitor_currency] || 'USD'
  end

  # return all currency weights supported by the plugin
  def e_get_currency_weights
    @_cache_e_get_currency_weights ||= lambda{
      weights = {kg: t('plugins.ecommerce.select.kilogram'), lb: t('plugins.ecommerce.select.pound'), dr: t('plugins.ecommerce.select.dram'), gr: t('plugins.ecommerce.select.grain'), g: t('plugins.ecommerce.select.gram'), mg: t('plugins.ecommerce.select.milligram'), oz: t('plugins.ecommerce.select.ounce'), t: t('plugins.ecommerce.select.tonne'), UK: t('plugins.ecommerce.select.hundredweight')}
      hooks_run('ecommerce_weights', weights)
      weights
    }.call
  end

  # return the currency details with code = code
  def e_get_currency_by_code(code)
    e_get_all_currencies[code.to_s.upcase] || {}
  end

  # return the currency defined for admin panel
  def e_system_currency
    current_site.e_settings[:current_unit] || 'USD'
  end

  # render price formatted of a product with current currency
  def e_parse_price(price)
    currency = cama_is_admin_request? ? e_system_currency : e_current_visitor_currency
    args = {currency: currency, price: price, data: number_to_currency(e_parse_to_current_currency(price), unit: currency)}; hooks_run('ecommerce_parse_price', args) # permit to customize price format
    args[:data]
  end

  # convert the amount of system currency into visitor current currency
  # amount: (Number) which will be exchanged into current visitor's currency
  # return the modified amount (Integer)
  def e_parse_to_current_currency(amount)
    amount = amount.to_f
    args = {amount: amount, from: e_system_currency, to: e_current_visitor_currency, exchange: nil}
    return amount if cama_is_admin_request? || args[:from] == args[:to] || amount.to_f.round(2) == 0.00

    currency = cama_ecommerce_post_type.get_option(e_current_visitor_currency, {}, 'currencies')
    return (currency[:exchange] * amount).round(2) if currency[:date] == Date.today.to_s && currency[:base] == e_system_currency

    hooks_run('ecommerce_calculate_exchange', args) # permit to use custom exchange app by setting the value in :exchange attribute
    return (args[:exchange] * amount).round(2) if args[:exchange].present?

    exchange = e_finance_exchange_converter(args)
    cama_ecommerce_post_type.set_option(e_current_visitor_currency, {date: Date.today.to_s, exchange: exchange, base: e_system_currency}, 'currencies')
    (exchange * amount).round(2)
  end

  # helper to calculate exchanges
  def e_finance_exchange_converter(args)
    # request to google's finance converter site
    res = open("https://finance.google.com/finance/converter?a=1&from=#{args[:from]}&to=#{args[:to]}").read
    res = res.scan(/\<span class=bld\>(.+?)\<\/span\>/).first.first.split(' ') # => ["69.3000", "BOB"]
    res.first.to_f.round(4)
  end

  # return all currencies to use as a base currency
  def e_get_all_currencies
    @_cache_e_get_all_currencies ||= lambda{
      currencies = {
        'AED' => {symbol: 'AED', label: t('plugins.ecommerce.currencies.aed', default: 'United Arab Emirates Dirham')},
        'AFN' => {symbol: 'AFN', label: t('plugins.ecommerce.currencies.afn', default: 'Afghan Afghani')},
        'ALL' => {symbol: 'ALL', label: t('plugins.ecommerce.currencies.all', default: 'Albanian Lek')},
        'AMD' => {symbol: 'AMD', label: t('plugins.ecommerce.currencies.amd', default: 'Armenian Dram')},
        'ANG' => {symbol: 'ANG', label: t('plugins.ecommerce.currencies.ang', default: 'Netherlands Antillean Guilder')},
        'AOA' => {symbol: 'AOA', label: t('plugins.ecommerce.currencies.aoa', default: 'Angolan Kwanza')},
        'ARS' => {symbol: 'ARS', label: t('plugins.ecommerce.currencies.ars', default: 'Argentine Peso')},
        'AUD' => {symbol: 'A$', label: t('plugins.ecommerce.currencies.aud', default: 'Australian Dollar')},
        'AWG' => {symbol: 'AWG', label: t('plugins.ecommerce.currencies.awg', default: 'Aruban Florin')},
        'AZN' => {symbol: 'AZN', label: t('plugins.ecommerce.currencies.azn', default: 'Azerbaijani Manat')},
        'BAM' => {symbol: 'BAM', label: t('plugins.ecommerce.currencies.bam', default: 'Bosnia-Herzegovina Convertible Mark')},
        'BBD' => {symbol: 'BBD', label: t('plugins.ecommerce.currencies.bbd', default: 'Barbadian Dollar')},<commit_msg>Update ecommerce_functions_helper.rb<commit_after>#encoding: utf-8
module Plugins::Ecommerce::EcommerceFunctionsHelper
  # return the visitor key which is used to relate the cart until login/register
  def ecommerce_get_visitor_key
    cookies[:e_cart_id] ||= cama_get_session_id unless cama_current_user.present?
  end

  # return the current cart for current user
  def e_current_cart(custom_visitor_key = nil)
    @_cache_e_current_cart ||= current_site.carts.set_user(custom_visitor_key || cama_current_user || ecommerce_get_visitor_key).active_cart.first_or_create(name: "Cart by #{Time.current.to_s}").decorate
  end

  # return all shipping country codes supported for shipping
  def e_shipping_countries
    current_site.e_settings[:shipping_countries] || ISO3166::Country.codes
  end

  # return (Array) all the currencies for visitors
  def e_visitor_unit_currencies
    current_site.e_settings[:visitor_unit_currencies] || ['USD']
  end

  # draw a select dropdown with all frontend currencies and actions to change current currency
  # long_mode: (Boolean, default false)
  # attrs = (Hash) attributes of the select drodown, sample: {class: 'form-control'}
  def e_draw_ecommerce_currencies(long_mode = false, attrs = {})
    return '' if e_visitor_unit_currencies.count <= 1
    res = '<select '+attrs.collect{|k, v| "#{k}='#{v}'" }.join(" ")+' onchange="window.location.href=window.location.href.split(\'cama_change_currency\')[0]+(window.location.href.search(\'\\\?\') > 1 ? \'&\' : \'?\')+\'cama_change_currency=\'+this.value">'
    e_visitor_unit_currencies.each do |unit|
      cur = e_get_currency_by_code(unit)
      res << "<option value='#{unit}' #{'selected' if e_current_visitor_currency == unit}>#{long_mode ? "#{cur[:label]} (#{cur[:symbol]})": cur[:symbol]}</option>"
    end
    res << '</select>'
  end

  # set a new currency if new_currency is present
  # return current currency defined by the current visitor
  def e_current_visitor_currency(new_currency = nil)
    session[:e_current_visitor_currency] = new_currency if new_currency.present?
    session[:e_current_visitor_currency] || 'USD'
  end

  # return all currency weights supported by the plugin
  def e_get_currency_weights
    @_cache_e_get_currency_weights ||= lambda{
      weights = {kg: t('plugins.ecommerce.select.kilogram'), lb: t('plugins.ecommerce.select.pound'), dr: t('plugins.ecommerce.select.dram'), gr: t('plugins.ecommerce.select.grain'), g: t('plugins.ecommerce.select.gram'), mg: t('plugins.ecommerce.select.milligram'), oz: t('plugins.ecommerce.select.ounce'), t: t('plugins.ecommerce.select.tonne'), UK: t('plugins.ecommerce.select.hundredweight')}
      hooks_run('ecommerce_weights', weights)
      weights
    }.call
  end

  # return the currency details with code = code
  def e_get_currency_by_code(code)
    e_get_all_currencies[code.to_s.upcase] || {}
  end

  # return the currency defined for admin panel
  def e_system_currency
    current_site.e_settings[:current_unit] || 'USD'
  end

  # render price formatted of a product with current currency
  def e_parse_price(price)
    currency = cama_is_admin_request? ? e_system_currency : e_current_visitor_currency
    args = {currency: currency, price: price, data: number_to_currency(e_parse_to_current_currency(price), unit: currency)}; hooks_run('ecommerce_parse_price', args) # permit to customize price format
    args[:data]
  end

  # convert the amount of system currency into visitor current currency
  # amount: (Number) which will be exchanged into current visitor's currency
  # return the modified amount (Integer)
  def e_parse_to_current_currency(amount)
    amount = amount.to_f
    args = {amount: amount, from: e_system_currency, to: e_current_visitor_currency, exchange: nil}
    return amount if cama_is_admin_request? || args[:from] == args[:to] || amount.to_f.round(2) == 0.00

    currency = cama_ecommerce_post_type.get_option(e_current_visitor_currency, {}, 'currencies')
    return (currency[:exchange] * amount).round(2) if currency[:date] == Date.today.to_s && currency[:base] == e_system_currency

    hooks_run('ecommerce_calculate_exchange', args) # permit to use custom exchange app by setting the value in :exchange attribute
    return (args[:exchange] * amount).round(2) if args[:exchange].present?

    exchange = e_finance_exchange_converter(args)
    cama_ecommerce_post_type.set_option(e_current_visitor_currency, {date: Date.today.to_s, exchange: exchange, base: e_system_currency}, 'currencies')
    (exchange * amount).round(2)
  end

  # helper to calculate exchanges
  def e_finance_exchange_converter(args)
    # request to google's finance converter site
    res = open("https://finance.google.com/bctzjpnsun/converter?a=1&from=#{args[:from]}&to=#{args[:to]}").read
    res = res.scan(/\<span class=bld\>(.+?)\<\/span\>/).first.first.split(' ') # => ["69.3000", "BOB"]
    res.first.to_f.round(4)
  end

  # return all currencies to use as a base currency
  def e_get_all_currencies
    @_cache_e_get_all_currencies ||= lambda{
      currencies = {
        'AED' => {symbol: 'AED', label: t('plugins.ecommerce.currencies.aed', default: 'United Arab Emirates Dirham')},
        'AFN' => {symbol: 'AFN', label: t('plugins.ecommerce.currencies.afn', default: 'Afghan Afghani')},
        'ALL' => {symbol: 'ALL', label: t('plugins.ecommerce.currencies.all', default: 'Albanian Lek')},
        'AMD' => {symbol: 'AMD', label: t('plugins.ecommerce.currencies.amd', default: 'Armenian Dram')},
        'ANG' => {symbol: 'ANG', label: t('plugins.ecommerce.currencies.ang', default: 'Netherlands Antillean Guilder')},
        'AOA' => {symbol: 'AOA', label: t('plugins.ecommerce.currencies.aoa', default: 'Angolan Kwanza')},
        'ARS' => {symbol: 'ARS', label: t('plugins.ecommerce.currencies.ars', default: 'Argentine Peso')},
        'AUD' => {symbol: 'A$', label: t('plugins.ecommerce.currencies.aud', default: 'Australian Dollar')},
        'AWG' => {symbol: 'AWG', label: t('plugins.ecommerce.currencies.awg', default: 'Aruban Florin')},
        'AZN' => {symbol: 'AZN', label: t('plugins.ecommerce.currencies.azn', default: 'Azerbaijani Manat')},
        'BAM' => {symbol: 'BAM', label: t('plugins.ecommerce.currencies.bam', default: 'Bosnia-Herzegovina Convertible Mark')},
        'BBD' => {symbol: 'BBD', label: t('plugins.ecommerce.currencies.bbd', default: 'Barbadian Dollar')},
{'fraction_non_alphanumeric': 0.14107981220657276, 'fraction_numerical': 0.0035993740219092333, 'mean_word_length': 4.908922792417938, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6538759', 'n_tokens_mistral': 4324, 'n_tokens_neox': 4138, 'n_words': 948}