Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the account closing date information #76

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion assets/sql/migrations/v5.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ CREATE TABLE IF NOT EXISTS tags (
CREATE TABLE IF NOT EXISTS transactionTags (
transactionID TEXT NOT NULL REFERENCES transactions(id) ON DELETE CASCADE ON UPDATE CASCADE,
tagID TEXT NOT NULL REFERENCES tags(id) ON DELETE CASCADE ON UPDATE CASCADE
);
);

ALTER TABLE accounts ADD COLUMN closingDate TEXT;

UPDATE accounts
SET closingDate = strftime('%Y-%m-%dT%H:%M:%fZ', 'now', 'localtime')
WHERE isArchived == 1;

ALTER TABLE accounts DROP COLUMN isArchived;
238 changes: 192 additions & 46 deletions lib/app/accounts/account_details.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:drift/drift.dart' as drift;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
Expand All @@ -6,13 +7,17 @@ import 'package:monekin/app/transactions/transactions.page.dart';
import 'package:monekin/app/transactions/widgets/transaction_list.dart';
import 'package:monekin/core/database/services/account/account_service.dart';
import 'package:monekin/core/database/services/exchange-rate/exchange_rate_service.dart';
import 'package:monekin/core/database/services/transaction/transaction_service.dart';
import 'package:monekin/core/models/account/account.dart';
import 'package:monekin/core/models/transaction/transaction.dart';
import 'package:monekin/core/models/transaction/transaction_status.dart';
import 'package:monekin/core/presentation/widgets/bottomSheetFooter.dart';
import 'package:monekin/core/presentation/widgets/card_with_header.dart';
import 'package:monekin/core/presentation/widgets/inline_info_card.dart';
import 'package:monekin/core/presentation/widgets/monekin_quick_actions_buttons.dart';
import 'package:monekin/core/presentation/widgets/number_ui_formatters/currency_displayer.dart';
import 'package:monekin/core/presentation/widgets/transaction_filter/transaction_filters.dart';
import 'package:monekin/core/utils/date_time_picker.dart';
import 'package:monekin/core/utils/list_tile_action_item.dart';
import 'package:monekin/i18n/translations.g.dart';

Expand All @@ -28,8 +33,11 @@ class AccountDetailsPage extends StatefulWidget {
}

class _AccountDetailsPageState extends State<AccountDetailsPage> {
List<ListTileActionItem> getAccountDetailsActions(BuildContext context,
{required Account account, bool navigateBackOnDelete = false}) {
List<ListTileActionItem> getAccountDetailsActions(
BuildContext context, {
required Account account,
bool navigateBackOnDelete = false,
}) {
final t = Translations.of(context);

return [
Expand All @@ -46,7 +54,7 @@ class _AccountDetailsPageState extends State<AccountDetailsPage> {
ListTileActionItem(
label: t.transfer.create,
icon: TransactionType.transfer.icon,
onClick: account.isArchived
onClick: account.isClosed
? null
: () async {
showAccountsWarn() => showDialog(
Expand Down Expand Up @@ -86,34 +94,24 @@ class _AccountDetailsPageState extends State<AccountDetailsPage> {
}
}),
ListTileActionItem(
label: account.isArchived ? t.general.unarchive : t.general.archive,
icon: account.isArchived
label: account.isClosed
? t.account.reopen_short
: t.account.close.title_short,
icon: account.isClosed
? Icons.unarchive_rounded
: Icons.archive_rounded,
role: ListTileActionRole.warn,
onClick: () async {
if (account.isArchived) {
await AccountService.instance
.updateAccount(account.copyWith(isArchived: false))
.then((value) {
if (value) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(t.account.archive.unarchive_succes)),
);
}
}).catchError((err) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('$err')));
});

if (account.isClosed) {
showReopenAccountDialog(account);
return;
}

final currentBalance = await AccountService.instance
.getAccountMoney(account: account)
.first;

showArchiveWarnDialog(account, currentBalance);
await showCloseAccountDialog(account, currentBalance);
}),
ListTileActionItem(
label: t.general.delete,
Expand All @@ -127,44 +125,63 @@ class _AccountDetailsPageState extends State<AccountDetailsPage> {
];
}

showArchiveWarnDialog(Account account, double currentBalance) {
showReopenAccountDialog(Account account) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(currentBalance == 0 ? t.account.archive.title : 'Ops!'),
title: Text(t.account.reopen),
content: SingleChildScrollView(
child: Text(currentBalance == 0
? t.account.archive.warn
: t.account.archive.should_have_zero_balance)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(t.account.reopen_descr)],
),
),
actions: [
TextButton(
child: Text(
currentBalance == 0 ? t.general.confirm : t.general.understood),
child: Text(t.general.cancel),
onPressed: () {
Navigator.pop(context);
},
),
TextButton(
child: Text(t.general.confirm),
onPressed: () {
if (currentBalance != 0) {
Navigator.pop(context);
return;
}

AccountService.instance
.updateAccount(account.copyWith(isArchived: true))
.updateAccount(
account.copyWith(
closingDate: const drift.Value(null),
),
)
.then((value) {
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(t.account.archive.success)));
}).catchError((err) {
Navigator.pop(context);

ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('$err')));
});
if (value) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(t.account.close.unarchive_succes)),
);
}
})
.whenComplete(() => Navigator.pop(context))
.catchError((err) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('$err')));
});
},
),
],
),
);
}

Future<bool?> showCloseAccountDialog(Account account, double currentBalance) {
return showModalBottomSheet<bool>(
context: context,
isScrollControlled: true,
showDragHandle: true,
builder: (context) =>
ArchiveWarnDialog(currentBalance: currentBalance, account: account),
);
}

deleteTransactionWithAlertAndSnackBar(BuildContext context,
{required String transactionId, required bool navigateBack}) {
showDialog(
Expand Down Expand Up @@ -296,7 +313,8 @@ class _AccountDetailsPageState extends State<AccountDetailsPage> {
ConnectionState.waiting ||
currencySnapshot.data != 0 &&
currencySnapshot.data! ==
snapshot.data) {
snapshot.data ||
snapshot.data! == 0) {
return Container();
}

Expand Down Expand Up @@ -352,6 +370,17 @@ class _AccountDetailsPageState extends State<AccountDetailsPage> {
),
),
const Divider(indent: 12),
if (account.isClosed) ...[
ListTile(
title: Text(t.account.close_date),
subtitle: Text(
DateFormat.yMMMMEEEEd()
.add_Hm()
.format(account.closingDate!),
),
),
const Divider(indent: 12),
],
ListTile(
title: Text(t.account.types.title),
subtitle: Text(account.type.title(context)),
Expand Down Expand Up @@ -412,9 +441,10 @@ class _AccountDetailsPageState extends State<AccountDetailsPage> {
),
const SizedBox(height: 16),
CardWithHeader(
title: t.general.quick_actions,
body: MonekinQuickActionsButton(
actions: accountDetailsActions)),
title: t.general.quick_actions,
body: MonekinQuickActionsButton(
actions: accountDetailsActions),
),
],
),
),
Expand All @@ -426,3 +456,119 @@ class _AccountDetailsPageState extends State<AccountDetailsPage> {
});
}
}

class ArchiveWarnDialog extends StatefulWidget {
const ArchiveWarnDialog({
super.key,
required this.currentBalance,
required this.account,
});

final double currentBalance;
final Account account;

@override
State<ArchiveWarnDialog> createState() => _ArchiveWarnDialogState();
}

class _ArchiveWarnDialogState extends State<ArchiveWarnDialog> {
DateTime date = DateTime.now();

@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: TransactionService.instance.countTransactions(
predicate: TransactionFilters(
accountsIDs: [widget.account.id], minDate: date),
convertToPreferredCurrency: false,
),
builder: (context, snapshot) {
final hasNoTransactions =
!snapshot.hasData || snapshot.data!.numberOfRes == 0;

return Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 24),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.account.close.title,
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 22),
Text(t.account.close.warn),
const SizedBox(height: 22),
TextFormField(
controller: TextEditingController(
text: DateFormat.yMMMMd().add_Hm().format(date)),
decoration: InputDecoration(
labelText: '${t.account.close_date} *'),
enabled: widget.currentBalance == 0,
readOnly: true,
onTap: () async {
DateTime? pickedDate = await openDateTimePicker(
context,
initialDate: date,
firstDate: widget.account.date,
lastDate: DateTime.now(),
showTimePickerAfterDate: true,
);
if (pickedDate == null) return;

setState(() {
date = pickedDate;
});
},
),
if (!hasNoTransactions ||
widget.currentBalance != 0) ...[
const SizedBox(height: 12),
InlineInfoCard(
mode: InlineInfoCardMode.warn,
text: widget.currentBalance != 0
? t.account.close.should_have_zero_balance
: t.account.close.should_have_no_transactions,
)
]
],
)),
BottomSheetFooter(
submitText: t.general.continue_text,
submitIcon: Icons.check,
onSaved: !hasNoTransactions || widget.currentBalance != 0
? null
: () {
AccountService.instance
.updateAccount(
widget.account.copyWith(
closingDate: drift.Value(date),
),
)
.then((value) {
Navigator.pop(context, true);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(t.account.close.success)));
}).catchError(
(err) {
Navigator.pop(context);

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('$err')),
);
},
);
},
)
],
),
);
});
}
}
Loading