Example #1
0
  /**
   * Builds a QIF entry representing this transaction
   *
   * @return String QIF representation of this transaction
   */
  public String toQIF() {
    final String newLine = "\n";

    AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(GnuCashApplication.getAppContext());

    // all transactions are double transactions
    String splitAccountFullName = QifHelper.getImbalanceAccountName(mAmount.getCurrency());
    if (mDoubleEntryAccountUID != null && mDoubleEntryAccountUID.length() > 0) {
      splitAccountFullName = accountsDbAdapter.getFullyQualifiedAccountName(mDoubleEntryAccountUID);
    }

    StringBuffer transactionQifBuffer = new StringBuffer();
    transactionQifBuffer
        .append(QifHelper.DATE_PREFIX)
        .append(QifHelper.formatDate(mTimestamp))
        .append(newLine);
    transactionQifBuffer.append(QifHelper.MEMO_PREFIX).append(mName).append(newLine);

    transactionQifBuffer
        .append(QifHelper.SPLIT_CATEGORY_PREFIX)
        .append(splitAccountFullName)
        .append(newLine);
    if (mDescription != null && mDescription.length() > 0) {
      transactionQifBuffer.append(QifHelper.SPLIT_MEMO_PREFIX).append(mDescription).append(newLine);
    }
    transactionQifBuffer
        .append(QifHelper.SPLIT_AMOUNT_PREFIX)
        .append(mAmount.asString())
        .append(newLine);
    transactionQifBuffer.append(QifHelper.ENTRY_TERMINATOR).append(newLine);

    accountsDbAdapter.close();
    return transactionQifBuffer.toString();
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.i("TransactionRecorder", "Received transaction recording intent");
    Bundle args = intent.getExtras();
    String name = args.getString(Intent.EXTRA_TITLE);
    String note = args.getString(Intent.EXTRA_TEXT);
    BigDecimal amountBigDecimal = (BigDecimal) args.getSerializable(Transaction.EXTRA_AMOUNT);
    String currencyCode = args.getString(Account.EXTRA_CURRENCY_CODE);
    if (currencyCode == null) currencyCode = Money.DEFAULT_CURRENCY_CODE;

    String accountUID = args.getString(Transaction.EXTRA_ACCOUNT_UID);
    if (accountUID == null) // if no account was assigned, throw an exception
    throw new IllegalArgumentException("No account specified for the transaction");

    String doubleAccountUID = args.getString(Transaction.EXTRA_DOUBLE_ACCOUNT_UID);
    if (doubleAccountUID == null || doubleAccountUID.length() == 0)
      doubleAccountUID =
          QifHelper.getImbalanceAccountName(Currency.getInstance(Money.DEFAULT_CURRENCY_CODE));
    Transaction.TransactionType type =
        Transaction.TransactionType.valueOf(args.getString(Transaction.EXTRA_TRANSACTION_TYPE));

    Money amount = new Money(amountBigDecimal, Currency.getInstance(currencyCode));
    Transaction transaction = new Transaction(amount, name);
    transaction.setTime(System.currentTimeMillis());
    transaction.setDescription(note);
    transaction.setAccountUID(accountUID);
    transaction.setDoubleEntryAccountUID(doubleAccountUID);
    transaction.setTransactionType(type);

    TransactionsDbAdapter transacionsDbAdapter = new TransactionsDbAdapter(context);
    transacionsDbAdapter.addTransaction(transaction);

    WidgetConfigurationActivity.updateAllWidgets(context);

    transacionsDbAdapter.close();
  }