# Price type calculation # 1. :static # 2. :perpage # 3. :onepage # 4. :percentage_base # 5. :percentage_total # class PriceType @STATIC: 1 @PERCENTAGE: 2 @PERCENTAGE_MIN_AMOUNT: 3 @RANGE: '1': min: 0 max: 10000 '2': min: 0 max: 100 '3': min: 0 max: 100 @in_range: (type_id, value) -> type = parseInt(type_id, 10) number = parseFloat(value) return (@RANGE[type].min >= value) && (@RANGE[type].max <= value) if @RANGE[type]? false @calc: (type_id, value, options) -> type = parseInt(type_id, 10) number = parseFloat(value) switch type when 1 then @calc_static(number, options) when 2 then @calc_percentage(number, options) when 3 then @calc_percentage_min_amount(number, options) else 0 @calc_static: (value, options) -> value @calc_percentage: (value, options) -> ((options?['base'] || 0) * value) * 0.01 @calc_percentage_min_amount: (price, options)-> checkpoint = parseFloat(options?['checkpoint_amount']) _base = parseFloat(options?['base']) checkpoint ||= (options['min_amount'] * @RANGE[@PERCENTAGE_MIN_AMOUNT]['max'] / price) if checkpoint >= _base options['min_amount'] else @calc_percentage(price, options) @format: (type_id, value, rate, prefix = '', _base = 0, options = {}) -> type = parseInt(type_id, 10) number = parseFloat(value) || 0 if number is 0.0 return "FREE" result = switch type when 1 if rate?.symbol? && rate?.value > 0 numeral(number * rate.value).format("$0,0.00").replace("$", rate.symbol) else numeral(number).format("$0,0.00").replace("$", rate) when 2 then "#{number}%" when 3 if (_base = parseFloat(_base)) > 0 checkpoint = parseFloat(options?['checkpoint_amount']) checkpoint ||= (options['min_amount'] * @RANGE[@PERCENTAGE_MIN_AMOUNT]['max'] / number) if checkpoint >= _base number = parseFloat(options['min_amount']) numeral(number * rate.value).format("$0,0.00").replace("$", rate.symbol) else "#{number}%" else "#{number}%" else "" "#{prefix}#{result}" angular.module('EssayApp.services').factory('PriceType', [() -> return PriceType ])