<commit_before>Template.expenseList.expenses = ->
  Expenses.find().fetch()

Template.expenseList.editingExpense = ->
  expense = Expenses.findOne(Session.get 'editingExpense') if Session.get 'editingExpense'
  expense

Template.expense.thisRowBeingEdited = ->
  Session.equals('editingExpense', this._id)
  
Template.expense.amount = ->
  accounting.formatMoney @amount

Template.expense.amountRemaining = ->
  accounting.formatMoney @amountRemaining

Template.expense.destinationAccount = ->
  getVirtualAccountName(@destinationAccountId)

Template.expense.payFromAccounts = ->
  massaged = []
  getVirtualAccounts(undefined, undefined, {
    type: 'payFrom'
    _id: {
      $in: Object.keys(@payFromAccounts || {})
    }
  }).forEach((pfa) =>
    pfa.accountName = getVirtualAccountName(pfa._id)
    massaged.push pfa
  )
  massaged

Template.expense.events {
  'click .edit-expense': (event) ->
    expenseId = recordIdFromRow event
    Session.set 'editingExpense', expenseId

  'click .remove-expense': (event) ->
    expenseId = recordIdFromRow event
    description = Expenses.findOne(expenseId).description

    alertify.confirm "Are you sure you want to remove <em>#{description}</em>?", (event) ->
      if event
        Expenses.remove expenseId, (error) ->
          if not error
            showNavSuccess "Expense removed."
          else
            showNavError "I couldn't remove the expense for some reason. Try again, and contact us if the problems persist."
            console.log error
}

Template.newExpenseForm.expensesCount = ->
  !! Expenses.find().count()

Template.expenseForm.rendered = ->
  $context = $ @firstNode
  $dueDate = (elementByName 'dueDate', $context)
  $dueDate.datepicker()

Template.expenseForm.destinationAccounts = ->
  virtualAccounts = getVirtualAccounts undefined, undefined, {
    type: {
      $in: [ "payFrom", "payTo" ]
    }
  }
  getAccountSelector virtualAccounts, this.destinationAccountId
  
Template.expenseForm.payFromAccounts = ->
  massaged = []<commit_msg>Allow paying back bank accounts.<commit_after>Template.expenseList.expenses = ->
  Expenses.find().fetch()

Template.expenseList.editingExpense = ->
  expense = Expenses.findOne(Session.get 'editingExpense') if Session.get 'editingExpense'
  expense

Template.expense.thisRowBeingEdited = ->
  Session.equals('editingExpense', this._id)
  
Template.expense.amount = ->
  accounting.formatMoney @amount

Template.expense.amountRemaining = ->
  accounting.formatMoney @amountRemaining

Template.expense.destinationAccount = ->
  getVirtualAccountName(@destinationAccountId)

Template.expense.payFromAccounts = ->
  massaged = []
  getVirtualAccounts(undefined, undefined, {
    type: 'payFrom'
    _id: {
      $in: Object.keys(@payFromAccounts || {})
    }
  }).forEach((pfa) =>
    pfa.accountName = getVirtualAccountName(pfa._id)
    massaged.push pfa
  )
  massaged

Template.expense.events {
  'click .edit-expense': (event) ->
    expenseId = recordIdFromRow event
    Session.set 'editingExpense', expenseId

  'click .remove-expense': (event) ->
    expenseId = recordIdFromRow event
    description = Expenses.findOne(expenseId).description

    alertify.confirm "Are you sure you want to remove <em>#{description}</em>?", (event) ->
      if event
        Expenses.remove expenseId, (error) ->
          if not error
            showNavSuccess "Expense removed."
          else
            showNavError "I couldn't remove the expense for some reason. Try again, and contact us if the problems persist."
            console.log error
}

Template.newExpenseForm.expensesCount = ->
  !! Expenses.find().count()

Template.expenseForm.rendered = ->
  $context = $ @firstNode
  $dueDate = (elementByName 'dueDate', $context)
  $dueDate.datepicker()

Template.expenseForm.destinationAccounts = ->
  virtualAccounts = getVirtualAccounts undefined, undefined, {
    type: {
      $in: [ "payFrom", "payTo", "bank" ]
    }
  }
  getAccountSelector virtualAccounts, this.destinationAccountId
  
Template.expenseForm.payFromAccounts = ->
  massaged = []
{'fraction_non_alphanumeric': 0.11790714812085483, 'fraction_numerical': 0.0, 'mean_word_length': 3.632536973833902, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, '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': '5654474', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1243, 'n_words': 245}