<commit_before>module Claims::Calculations
  def calculate_fees_total(category=nil)
    fees.reload
    if category.blank?
      fees.map(&:amount).compact.sum
    else
      __send__("#{category.downcase}_fees").map(&:amount).compact.sum
    end
  end

  def calculate_expenses_total
    # #reload prevents cloning
    Expense.where(claim_id: self.id).pluck(:amount).sum
  end

  def calculate_disbursements_total
    # #reload prevents cloning
    Disbursement.where(claim_id: self.id).pluck(:net_amount).sum
  end

  def calculate_total
    calculate_fees_total + calculate_expenses_total + calculate_disbursements_total
  end

  def update_fees_total
    update_column(:fees_total, calculate_fees_total)
  end

  def update_expenses_total
    update_column(:expenses_total, calculate_expenses_total)
  end

  def update_disbursements_total
    update_column(:disbursements_total, calculate_disbursements_total)
  end

  def update_total
    update_column(:total, calculate_total)
  end

  def calculate_expenses_vat
    if lgfs?
      Expense.where(claim_id: self.id).pluck(:vat_amount).sum<commit_msg>Guard against trying to sum nil amounts (#1540)<commit_after>module Claims::Calculations
  def calculate_fees_total(category=nil)
    fees.reload
    if category.blank?
      fees.map(&:amount).compact.sum
    else
      __send__("#{category.downcase}_fees").map(&:amount).compact.sum
    end
  end

  def calculate_expenses_total
    # #reload prevents cloning
    Expense.where(claim_id: self.id).where.not(amount: nil).pluck(:amount).sum
  end

  def calculate_disbursements_total
    # #reload prevents cloning
    Disbursement.where(claim_id: self.id).where.not(net_amount: nil).pluck(:net_amount).sum
  end

  def calculate_total
    calculate_fees_total + calculate_expenses_total + calculate_disbursements_total
  end

  def update_fees_total
    update_column(:fees_total, calculate_fees_total)
  end

  def update_expenses_total
    update_column(:expenses_total, calculate_expenses_total)
  end

  def update_disbursements_total
    update_column(:disbursements_total, calculate_disbursements_total)
  end

  def update_total
    update_column(:total, calculate_total)
  end

  def calculate_expenses_vat
    if lgfs?
      Expense.where(claim_id: self.id).pluck(:vat_amount).sum
{'fraction_non_alphanumeric': 0.08057491289198607, 'fraction_numerical': 0.0017421602787456446, 'mean_word_length': 4.548309178743962, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4311519', 'n_tokens_mistral': 782, 'n_tokens_neox': 751, 'n_words': 128}