Line data Source code
1 : // Copyright 2024 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'package:app_finance/_classes/herald/app_design.dart';
5 : import 'package:app_finance/_classes/herald/app_locale.dart';
6 : import 'package:app_finance/_classes/storage/app_data.dart';
7 : import 'package:app_finance/_classes/structure/bill_app_data.dart';
8 : import 'package:app_finance/_classes/structure/invoice_app_data.dart';
9 : import 'package:app_finance/_classes/structure/navigation/app_route.dart';
10 : import 'package:app_finance/_classes/structure/payment_app_data.dart';
11 : import 'package:app_finance/_configs/automation_type.dart';
12 : import 'package:app_finance/_configs/theme_helper.dart';
13 : import 'package:app_finance/_ext/data_ext.dart';
14 : import 'package:app_finance/_ext/date_time_ext.dart';
15 : import 'package:app_finance/components/widgets/payment_list_widget.dart';
16 : import 'package:app_finance/design/generic/base_header_widget.dart';
17 : import 'package:app_finance/design/generic/base_swipe_widget.dart';
18 : import 'package:material_ui/material_ui.dart';
19 :
20 : class PaymentWidget extends StatelessWidget {
21 : final List<PaymentAppData> data;
22 : final AppData state;
23 : final EdgeInsets? margin;
24 : final double? width;
25 :
26 0 : PaymentWidget({
27 : super.key,
28 : required this.data,
29 : required this.state,
30 : this.margin,
31 : this.width,
32 : }) {
33 0 : data.sort((a, b) => a.updatedAt.compareTo(b.updatedAt));
34 : }
35 :
36 0 : double getMonthTotals() {
37 : double amount = 0.0;
38 0 : DateTime now = DateTime.now();
39 0 : DateTime nextMonth = DateTime(now.year, now.month + 1, 1);
40 0 : for (PaymentAppData item in data) {
41 0 : item = item.clone();
42 0 : while (item.updatedAt.isBefore(nextMonth)) {
43 0 : dynamic obj = item.data.toDataObject(state);
44 0 : if (obj is BillAppData) {
45 0 : amount -= obj.details;
46 0 : } else if (obj is InvoiceAppData && obj.accountFrom == null) {
47 0 : amount += obj.details;
48 : }
49 0 : if (item.title == AppAutomationType.days.name) {
50 0 : item.updatedAt = item.updatedAt.add(Duration(days: item.days > 0 ? item.days : 1));
51 0 : } else if (item.title == AppAutomationType.week.name) {
52 0 : item.updatedAt = item.updatedAt.add(const Duration(days: 7));
53 0 : } else if (item.title == AppAutomationType.year.name) {
54 0 : item.updatedAt = item.updatedAt.getNextYear();
55 : } else {
56 0 : item.updatedAt = item.updatedAt.getNextMonth();
57 : }
58 : }
59 : }
60 : return amount;
61 : }
62 :
63 0 : Widget buildListWidget(PaymentAppData item, BuildContext context, BoxConstraints constraints) {
64 0 : return BaseSwipeWidget(
65 : routePath: AppRoute.automationPaymentEditRoute,
66 0 : uuid: item.uuid ?? '',
67 0 : child: PaymentListWidget(item: item, state: state, width: width, constraints: constraints),
68 : );
69 : }
70 :
71 0 : @override
72 : Widget build(BuildContext context) {
73 0 : return Container(
74 0 : margin: margin,
75 0 : width: width,
76 0 : child: LayoutBuilder(
77 0 : builder: (context, constraints) {
78 0 : return Column(
79 0 : crossAxisAlignment: AppDesign.getAlignment(),
80 0 : children: [
81 0 : BaseHeaderWidget(
82 : route: AppRoute.automationRoute,
83 0 : width: width ?? constraints.maxWidth,
84 0 : total: getMonthTotals(),
85 0 : title: '${AppLocale.labels.paymentsHeadline}, ${DateTime.now().fullMonth()}',
86 0 : tooltip: AppLocale.labels.paymentsHeadline,
87 : ),
88 : ThemeHelper.hIndent,
89 0 : Expanded(
90 0 : child: ListView.builder(
91 : scrollDirection: Axis.vertical,
92 : shrinkWrap: true,
93 0 : itemCount: data.length + 1,
94 0 : itemBuilder: (BuildContext context, index) {
95 0 : if (index >= data.length) {
96 : return ThemeHelper.formEndBox;
97 : }
98 0 : return buildListWidget(data[index], context, constraints);
99 : },
100 : ),
101 : ),
102 : ],
103 : );
104 : },
105 : ),
106 : );
107 : }
108 : }
|