@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Bundle bundle = getArguments(); // TODO strict mode violation mTransaction = Transaction.getInstanceFromDb(bundle.getLong(KEY_ROWID)); }
public Transaction toTransaction(Account a) { Transaction t; Money m = new Money(a.currency, amount); if (isSplit()) { t = new SplitTransaction(a.getId(), m); } else if (isTransfer()) { t = new Transfer(a.getId(), m); } else { t = new Transaction(a.getId(), m); } if (date != null) { t.setDate(date); } t.comment = memo; t.crStatus = Transaction.CrStatus.fromQifName(status); t.referenceNumber = number; return t; }
@Override public Loader<Cursor> onCreateLoader(int id, Bundle arg1) { if (getActivity() == null) { return null; } switch (id) { case MyExpenses.SPLIT_PART_CURSOR: CursorLoader cursorLoader = new CursorLoader( getActivity(), TransactionProvider.TRANSACTIONS_URI, null, "parent_id = ?", new String[] {String.valueOf(mTransaction.getId())}, null); return cursorLoader; } return null; }
@Override public void onClick(DialogInterface dialog, int which) { MyExpenses ctx = (MyExpenses) getActivity(); if (ctx != null && which == AlertDialog.BUTTON_POSITIVE) { if (mTransaction.transfer_peer != null && DbUtils.hasParent(mTransaction.transfer_peer)) { Toast.makeText( getActivity(), getString(R.string.warning_splitpartcategory_context), Toast.LENGTH_LONG) .show(); return; } Intent i = new Intent(ctx, ExpenseEdit.class); i.putExtra(KEY_ROWID, mTransaction.getId()); i.putExtra(DatabaseConstants.KEY_TRANSFER_ENABLED, ctx.transferEnabled()); // i.putExtra("operationType", operationType); ctx.startActivityForResult(i, MyExpenses.EDIT_TRANSACTION_REQUEST); } else { this.dismiss(); } }
public void testAccount() throws RemoteException, OperationApplicationException { Account account, restored = null; Long openingBalance = (long) 100; account = new Account("TestAccount", openingBalance, "Testing with Junit"); account.setCurrency("EUR"); assertEquals("EUR", account.currency.getCurrencyCode()); account.save(); assertTrue(account.getId() > 0); restored = Account.getInstanceFromDb(account.getId()); assertEquals(account, restored); Long trAmount = (long) 100; Transaction op1 = Transaction.getNewInstance(account.getId()); op1.setAmount(new Money(account.currency, trAmount)); op1.comment = "test transaction"; op1.save(); assertEquals(account.getTotalBalance().getAmountMinor().longValue(), openingBalance + trAmount); Account.delete(account.getId()); assertNull( "Account deleted, but can still be retrieved", Account.getInstanceFromDb(account.getId())); assertNull( "Account delete should delete transaction, but operation can still be retrieved", Transaction.getInstanceFromDb(op1.getId())); }
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { final MyExpenses ctx = (MyExpenses) getActivity(); Context wrappedCtx = DialogUtils.wrapContext2(ctx); if (mTransaction == null) { return new AlertDialog.Builder(wrappedCtx) .setMessage("Transaction has been deleted") .create(); } final LayoutInflater li = LayoutInflater.from(wrappedCtx); View view = li.inflate(R.layout.transaction_detail, null); int title; boolean type = mTransaction.amount.getAmountMinor() > 0 ? ExpenseEdit.INCOME : ExpenseEdit.EXPENSE; if (mTransaction instanceof SplitTransaction) { // TODO: refactor duplicated code with SplitPartList title = R.string.split_transaction; View emptyView = view.findViewById(R.id.empty); Resources.Theme theme = ctx.getTheme(); TypedValue color = new TypedValue(); theme.resolveAttribute(R.attr.colorExpense, color, true); final int colorExpense = color.data; theme.resolveAttribute(R.attr.colorIncome, color, true); final int colorIncome = color.data; ListView lv = (ListView) view.findViewById(R.id.list); // Create an array to specify the fields we want to display in the list String[] from = new String[] {KEY_LABEL_MAIN, KEY_AMOUNT}; // and an array of the fields we want to bind those fields to int[] to = new int[] {R.id.category, R.id.amount}; final String categorySeparator, commentSeparator; categorySeparator = " : "; commentSeparator = " / "; // Now create a simple cursor adapter and set it to display mAdapter = new SimpleCursorAdapter(ctx, R.layout.split_part_row, null, from, to, 0) { /* (non-Javadoc) * calls {@link #convText for formatting the values retrieved from the cursor} * @see android.widget.SimpleCursorAdapter#setViewText(android.widget.TextView, java.lang.String) */ @Override public void setViewText(TextView v, String text) { switch (v.getId()) { case R.id.amount: text = Utils.convAmount(text, mTransaction.amount.getCurrency()); } super.setViewText(v, text); } /* (non-Javadoc) * manipulates the view for amount (setting expenses to red) and * category (indicate transfer direction with => or <= * @see android.widget.CursorAdapter#getView(int, android.view.View, android.view.ViewGroup) */ @Override public View getView(int position, View convertView, ViewGroup parent) { View row = super.getView(position, convertView, parent); TextView tv1 = (TextView) row.findViewById(R.id.amount); Cursor c = getCursor(); c.moveToPosition(position); int col = c.getColumnIndex(KEY_AMOUNT); long amount = c.getLong(col); if (amount < 0) { tv1.setTextColor(colorExpense); // Set the background color of the text. } else { tv1.setTextColor(colorIncome); } TextView tv2 = (TextView) row.findViewById(R.id.category); if (Build.VERSION.SDK_INT < 11) tv2.setTextColor(Color.WHITE); String catText = tv2.getText().toString(); if (DbUtils.getLongOrNull(c, KEY_TRANSFER_PEER) != null) { catText = ((amount < 0) ? "=> " : "<= ") + catText; } else { Long catId = DbUtils.getLongOrNull(c, KEY_CATID); if (catId == null) { catText = getString(R.string.no_category_assigned); } else { col = c.getColumnIndex(KEY_LABEL_SUB); String label_sub = c.getString(col); if (label_sub != null && label_sub.length() > 0) { catText += categorySeparator + label_sub; } } } col = c.getColumnIndex(KEY_COMMENT); String comment = c.getString(col); if (comment != null && comment.length() > 0) { catText += (catText.equals("") ? "" : commentSeparator) + "<i>" + comment + "</i>"; } tv2.setText(Html.fromHtml(catText)); return row; } }; lv.setAdapter(mAdapter); lv.setEmptyView(emptyView); LoaderManager manager = ctx.getSupportLoaderManager(); if (manager.getLoader(MyExpenses.SPLIT_PART_CURSOR) != null && !manager.getLoader(MyExpenses.SPLIT_PART_CURSOR).isReset()) manager.restartLoader(MyExpenses.SPLIT_PART_CURSOR, null, this); else manager.initLoader(MyExpenses.SPLIT_PART_CURSOR, null, this); } else { view.findViewById(R.id.SplitContainer).setVisibility(View.GONE); if (mTransaction instanceof Transfer) { title = R.string.transfer; ((TextView) view.findViewById(R.id.AccountLabel)).setText(R.string.transfer_from_account); ((TextView) view.findViewById(R.id.CategoryLabel)).setText(R.string.transfer_to_account); } else title = type ? R.string.income : R.string.expense; } String accountLabel = Account.getInstanceFromDb(mTransaction.accountId).label; if (mTransaction instanceof Transfer) { ((TextView) view.findViewById(R.id.Account)) .setText(type ? mTransaction.label : accountLabel); ((TextView) view.findViewById(R.id.Category)) .setText(type ? accountLabel : mTransaction.label); } else { ((TextView) view.findViewById(R.id.Account)).setText(accountLabel); if ((mTransaction.catId != null && mTransaction.catId > 0)) { ((TextView) view.findViewById(R.id.Category)).setText(mTransaction.label); } else { view.findViewById(R.id.CategoryRow).setVisibility(View.GONE); } } ((TextView) view.findViewById(R.id.Date)) .setText( DateFormat.getDateInstance(DateFormat.FULL).format(mTransaction.getDate()) + " " + DateFormat.getTimeInstance(DateFormat.SHORT).format(mTransaction.getDate())); ((TextView) view.findViewById(R.id.Amount)) .setText( Utils.formatCurrency( new Money( mTransaction.amount.getCurrency(), Math.abs(mTransaction.amount.getAmountMinor())))); if (!mTransaction.comment.equals("")) ((TextView) view.findViewById(R.id.Comment)).setText(mTransaction.comment); else view.findViewById(R.id.CommentRow).setVisibility(View.GONE); if (!mTransaction.referenceNumber.equals("")) ((TextView) view.findViewById(R.id.Number)).setText(mTransaction.referenceNumber); else view.findViewById(R.id.NumberRow).setVisibility(View.GONE); if (!mTransaction.payee.equals("")) ((TextView) view.findViewById(R.id.Payee)).setText(mTransaction.payee); else view.findViewById(R.id.PayeeRow).setVisibility(View.GONE); if (mTransaction.methodId != null) ((TextView) view.findViewById(R.id.Method)) .setText(PaymentMethod.getInstanceFromDb(mTransaction.methodId).getDisplayLabel()); else view.findViewById(R.id.MethodRow).setVisibility(View.GONE); if (Account.getInstanceFromDb(mTransaction.accountId).type.equals(Type.CASH)) view.findViewById(R.id.StatusRow).setVisibility(View.GONE); else { TextView tv = (TextView) view.findViewById(R.id.Status); tv.setBackgroundColor(mTransaction.crStatus.color); tv.setText(mTransaction.crStatus.toString()); } return new AlertDialog.Builder(getActivity()) .setTitle(title) .setView(view) .setNegativeButton(android.R.string.ok, this) .setPositiveButton(R.string.menu_edit, this) .create(); }
private void insertData() { Transaction op; account1 = new Account("Account 1", openingBalance, "Account 1"); account1.save(); account2 = new Account("Account 2", openingBalance, "Account 2"); account2.save(); catId = Category.write(0, TEST_CAT, null); op = Transaction.getNewInstance(account1.getId()); op.setAmount(new Money(account1.currency, -expense1)); op.crStatus = CrStatus.CLEARED; op.save(); op.setAmount(new Money(account1.currency, -expense2)); op.saveAsNew(); op.setAmount(new Money(account1.currency, income1)); op.saveAsNew(); op.setAmount(new Money(account1.currency, income2)); op.setCatId(catId); op.saveAsNew(); Transfer op1 = Transfer.getNewInstance(account1.getId(), account2.getId()); op1.setAmount(new Money(account1.currency, transferP)); op1.save(); op1.setAmount(new Money(account1.currency, -transferN)); op1.saveAsNew(); }