2626
2727import android .widget .*;
2828import org .gnucash .android .R ;
29+ import org .gnucash .android .data .Account ;
2930import org .gnucash .android .data .Money ;
3031import org .gnucash .android .data .Transaction ;
3132import org .gnucash .android .data .Transaction .TransactionType ;
@@ -173,6 +174,12 @@ public class NewTransactionFragment extends SherlockFragment implements
173174 */
174175 boolean mAmountManuallyEdited = false ;
175176
177+ /**
178+ * The AccountType of the account to which this transaction belongs.
179+ * Used for determining the accounting rules for credits and debits
180+ */
181+ Account .AccountType mAccountType ;
182+
176183 /**
177184 * Create the view and retrieve references to the UI elements
178185 */
@@ -215,8 +222,12 @@ public void onActivityCreated(Bundle savedInstanceState) {
215222 long transactionId = getArguments ().getLong (SELECTED_TRANSACTION_ID );
216223 mTransactionsDbAdapter = new TransactionsDbAdapter (getActivity ());
217224 mTransaction = mTransactionsDbAdapter .getTransaction (transactionId );
218-
219- setListeners ();
225+
226+ final long accountId = getArguments ().getLong (TransactionsListFragment .SELECTED_ACCOUNT_ID );
227+ mAccountType = mAccountsDbAdapter .getAccountType (accountId );
228+ toggleTransactionTypeState ();
229+
230+ setListeners ();
220231 if (mTransaction == null )
221232 initalizeViews ();
222233 else {
@@ -229,6 +240,22 @@ public void onActivityCreated(Bundle savedInstanceState) {
229240 initTransactionNameAutocomplete ();
230241 }
231242
243+ private void toggleTransactionTypeState () {
244+ switch (mAccountType ) {
245+ case ASSET :
246+ case EXPENSE :
247+ mTransactionTypeButton .setTextOff (getString (R .string .label_debit ));
248+ mTransactionTypeButton .setTextOn (getString (R .string .label_credit ));
249+ break ;
250+
251+ default :
252+ mTransactionTypeButton .setTextOff (getString (R .string .label_credit ));
253+ mTransactionTypeButton .setTextOn (getString (R .string .label_debit ));
254+ break ;
255+ }
256+ mTransactionTypeButton .invalidate ();
257+ }
258+
232259 /**
233260 * Initializes the transaction name field for autocompletion with existing transaction names in the database
234261 */
@@ -276,7 +303,6 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
276303 * This method is called if the fragment is used for editing a transaction
277304 */
278305 private void initializeViewsWithTransaction (){
279-
280306 mNameEditText .setText (mTransaction .getName ());
281307 mTransactionTypeButton .setChecked (mTransaction .getTransactionType () == TransactionType .DEBIT );
282308 if (!mAmountManuallyEdited ){
@@ -315,11 +341,19 @@ private void initalizeViews() {
315341 mDateTextView .setText (DATE_FORMATTER .format (time ));
316342 mTimeTextView .setText (TIME_FORMATTER .format (time ));
317343 mTime = mDate = Calendar .getInstance ();
318-
344+
319345 String typePref = PreferenceManager .getDefaultSharedPreferences (getActivity ()).getString (getString (R .string .key_default_transaction_type ), "DEBIT" );
320346 if (typePref .equals ("CREDIT" )){
321- mTransactionTypeButton .setChecked (false );
322- }
347+ if (mAccountType == Account .AccountType .ASSET || mAccountType == Account .AccountType .EXPENSE )
348+ mTransactionTypeButton .setChecked (false );
349+ else
350+ mTransactionTypeButton .setChecked (true );
351+ } else {
352+ if (mAccountType == Account .AccountType .ASSET || mAccountType == Account .AccountType .EXPENSE )
353+ mTransactionTypeButton .setChecked (true );
354+ else
355+ mTransactionTypeButton .setChecked (false );
356+ }
323357
324358 final long accountId = getArguments ().getLong (TransactionsListFragment .SELECTED_ACCOUNT_ID );
325359 String code = Money .DEFAULT_CURRENCY_CODE ;
@@ -455,7 +489,16 @@ public void onAccountChanged(long newAccountId){
455489 String currencyCode = accountsDbAdapter .getCurrencyCode (newAccountId );
456490 Currency currency = Currency .getInstance (currencyCode );
457491 mCurrencyTextView .setText (currency .getSymbol (Locale .getDefault ()));
458-
492+
493+ Account .AccountType previousAccountType = mAccountType ;
494+ mAccountType = accountsDbAdapter .getAccountType (newAccountId );
495+ toggleTransactionTypeState ();
496+
497+ //if the new account has a different credit/debit philosophy as the previous one, then toggle the button
498+ if (mAccountType .hasInvertedCredit () != previousAccountType .hasInvertedCredit ()){
499+ mTransactionTypeButton .toggle ();
500+ }
501+
459502 updateTransferAccountsList ();
460503 }
461504
@@ -478,7 +521,11 @@ private void saveNewTransaction() {
478521 long accountID = ((TransactionsActivity ) getSherlockActivity ()).getCurrentAccountID ();
479522 Currency currency = Currency .getInstance (mTransactionsDbAdapter .getCurrencyCode (accountID ));
480523 Money amount = new Money (amountBigd , currency );
481- TransactionType type = mTransactionTypeButton .isChecked () ? TransactionType .DEBIT : TransactionType .CREDIT ;
524+ TransactionType type ;
525+ if (mAccountType .hasInvertedCredit ()){
526+ type = amount .isNegative () ? TransactionType .CREDIT : TransactionType .DEBIT ;
527+ } else
528+ type = amount .isNegative () ? TransactionType .DEBIT : TransactionType .CREDIT ;
482529 if (mTransaction != null ){
483530 mTransaction .setAmount (amount );
484531 mTransaction .setName (name );
0 commit comments