Sha256: 3b4277034c2c558838978a1d7b95ea58413afde2a55a7ce3dffcbdcb71bb2396

Contents?: true

Size: 1.38 KB

Versions: 9

Compression:

Stored size: 1.38 KB

Contents

module Logistics
	module Core
	  class Currency < Lookup
      has_many :currency_rates, class_name: 'Logistics::Core::CurrencyRate'
      def current_selling_rate
        current_rate = self.currency_rates.where('rate_date <= ? AND currency_id = ?', Time.zone.now, self.id)
                           .order('rate_date DESC').first
        current_rate.rate_to_base_selling
      end

      def current_buying_rate
        current_rate = self.currency_rates.where('rate_date <= ? AND currency_id = ?', Time.zone.now, self.id)
                           .order('rate_date DESC').first
        current_rate.rate_to_base_buying
      end

      def self.convert_currency(suggested_currency, default_currency, unit_price)
        result = []
        if not suggested_currency.nil? and not suggested_currency == ''
          if suggested_currency != default_currency
            d_currency = Currency.find(default_currency)
            s_currency = Currency.find(suggested_currency)
            if s_currency.current_selling_rate == 1
              unit_price = d_currency.current_selling_rate * unit_price
            else
              unit_price = (d_currency.current_buying_rate/s_currency.current_buying_rate)*unit_price
            end
            return result.push(unit_price, suggested_currency)
          end
        end
        return result.push(unit_price, default_currency)
      end
	  end
	end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
logistics_core-21.11.2 app/models/logistics/core/currency.rb
logistics_core-21.11.1 app/models/logistics/core/currency.rb
logistics_core-21.08.1 app/models/logistics/core/currency.rb
logistics_core-21.03.1 app/models/logistics/core/currency.rb
logistics_core-20.10.3 app/models/logistics/core/currency.rb
logistics_core-20.10.2 app/models/logistics/core/currency.rb
logistics_core-20.10.1 app/models/logistics/core/currency.rb
logistics_core-20.9.1 app/models/logistics/core/currency.rb
logistics_core-20.8.1 app/models/logistics/core/currency.rb