<commit_before>class ChargeBookingTemplate < BookingTemplate
  # Charge Rates
  def charge_rate(date = nil)
    ChargeRate.current(charge_rate_code, date)
  end
  
  def amount(date = nil)
    return 0.0 unless charge_rate(date)
    
    charge_rate(date).rate / 100
  end
  
  def booking_parameters(params = {})
    # Prepare parameters set by template
    booking_params = attributes.reject!{|key, value| !["title", "comments", "credit_account_id", "debit_account_id"].include?(key)}

    # Calculate amount
    booking_amount = self.amount(params['value_date'])

    if ref_type = params['reference_type'] and ref_id = params['reference_id']
      reference = ref_type.constantize.find(ref_id)

      case self.amount_relates_to
        when 'reference_amount'
          booking_amount *= reference.amount
        when 'reference_balance'
          booking_amount *= reference.balance
      end
    end

    booking_params['amount'] = booking_amount
    
    # Override by passed in parameters
    booking_params.merge!(params)
  end
end<commit_msg>Set and use value date from reference in charge_booking_template.<commit_after>class ChargeBookingTemplate < BookingTemplate
  # Charge Rates
  def charge_rate(date = nil)
    ChargeRate.current(charge_rate_code, date)
  end
  
  def amount(date = nil)
    return 0.0 unless charge_rate(date)
    
    charge_rate(date).rate / 100
  end
  
  def booking_parameters(params = {})
    # Prepare parameters set by template
    booking_params = attributes.reject!{|key, value| !["title", "comments", "credit_account_id", "debit_account_id"].include?(key)}

    if ref_type = params['reference_type'] and ref_id = params['reference_id']
      reference = ref_type.constantize.find(ref_id)

      # Calculate amount
      booking_params['value_date'] = reference.value_date
      booking_amount = self.amount(reference.value_date)

      case self.amount_relates_to
        when 'reference_amount'
          booking_amount *= reference.amount
        when 'reference_balance'
          booking_amount *= reference.balance
      end
    end

    booking_params['amount'] = booking_amount
    
    # Override by passed in parameters
    booking_params.merge!(params)
  end
end
{'fraction_non_alphanumeric': 0.09099099099099099, 'fraction_numerical': 0.0045045045045045045, 'mean_word_length': 3.4868686868686867, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1993525', 'n_tokens_mistral': 668, 'n_tokens_neox': 656, 'n_words': 147}