Line data Source code
1 : // Copyright 2025 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/controller/focus_controller.dart';
5 : import 'package:app_finance/_classes/herald/app_design.dart';
6 : import 'package:app_finance/_classes/herald/app_locale.dart';
7 : import 'package:app_finance/_classes/storage/app_data.dart';
8 : import 'package:app_finance/_classes/structure/invoice_app_data.dart';
9 : import 'package:app_finance/_configs/screen_helper.dart';
10 : import 'package:app_finance/_configs/theme_helper.dart';
11 : import 'package:app_finance/_ext/build_context_ext.dart';
12 : import 'package:app_finance/_ext/tags_ext.dart';
13 : import 'package:app_finance/design/form/date_range_input.dart';
14 : import 'package:app_finance/design/form/list_account_selector.dart';
15 : import 'package:app_finance/design/form/simple_input.dart';
16 : import 'package:app_finance/design/form/tags_input.dart';
17 : import 'package:app_finance/design/wrapper/input_wrapper.dart';
18 : import 'package:app_finance/design/wrapper/row_widget.dart';
19 : import 'package:app_finance/design/wrapper/single_scroll_wrapper.dart';
20 : import 'package:app_finance/design/wrapper/text_wrapper.dart';
21 : import 'package:app_finance/pages/invoice/invoice_transfer_page.dart';
22 : import 'package:material_ui/material_ui.dart';
23 :
24 : class InvoiceTransferSearchPage extends StatefulWidget {
25 0 : const InvoiceTransferSearchPage({super.key});
26 :
27 0 : @override
28 0 : InvoiceTransferSearchPageState createState() => InvoiceTransferSearchPageState();
29 : }
30 :
31 : class InvoiceTransferSearchPageState extends InvoiceTransferPageState<InvoiceTransferSearchPage> {
32 : String? accountFrom;
33 : String? accountTo;
34 : DateTime? startDate;
35 : DateTime? endDate;
36 : late TextEditingController description;
37 : late TextEditingController amountFrom;
38 : late TextEditingController amountTo;
39 : late ValueNotifier<List<String>> tags;
40 : late FocusController focus;
41 :
42 : late List<ListAccountSelectorItem> accountList =
43 : state.getList(AppDataType.accounts).map((item) => ListAccountSelectorItem(item: item)).toList();
44 :
45 0 : @override
46 : void initState() {
47 0 : description = TextEditingController();
48 0 : amountFrom = TextEditingController();
49 0 : amountTo = TextEditingController();
50 0 : tags = ValueNotifier([]);
51 0 : tags.addListener(() => setState(clearState));
52 0 : focus = FocusController();
53 0 : super.initState();
54 : }
55 :
56 0 : @override
57 : dispose() {
58 0 : description.dispose();
59 0 : amountFrom.dispose();
60 0 : amountTo.dispose();
61 0 : tags.dispose();
62 0 : focus.dispose();
63 0 : super.dispose();
64 : }
65 :
66 0 : @override
67 0 : String getTitle() => AppLocale.labels.searchTooltip;
68 :
69 0 : @override
70 0 : String getButtonName() => AppLocale.labels.searchTooltip;
71 :
72 0 : @override
73 : Widget buildButton(BuildContext context, BoxConstraints constraints) {
74 0 : return FloatingActionButton(
75 : heroTag: 'invoice_transfer_search_page',
76 0 : onPressed: () => setState(clearState),
77 0 : tooltip: getButtonName(),
78 : child: const Icon(Icons.search),
79 : );
80 : }
81 :
82 0 : @override
83 : bool getContentFilter(InvoiceAppData o) {
84 0 : final descriptionMatch = o.title.toLowerCase().contains(description.text.toLowerCase());
85 0 : final tagsMatch = o.tags.hasTags(tags.value);
86 0 : final accountFromMatch = accountFrom == null || o.accountFrom == accountFrom;
87 0 : final accountToMatch = accountTo == null || o.account == accountTo;
88 0 : final startDateMatch = startDate == null || o.createdAt.isAfter(startDate!);
89 0 : final endDateMatch = endDate == null || o.createdAt.isBefore(endDate!);
90 0 : final amountFromMatch = amountFrom.text.isEmpty || o.details >= double.tryParse(amountFrom.text)!;
91 0 : final amountToMatch = amountTo.text.isEmpty || o.details <= double.tryParse(amountTo.text)!;
92 : return !(descriptionMatch &&
93 : tagsMatch &&
94 : accountFromMatch &&
95 : accountToMatch &&
96 : startDateMatch &&
97 : endDateMatch &&
98 : amountFromMatch &&
99 : amountToMatch) ||
100 0 : o.accountFrom == null;
101 : }
102 :
103 0 : @override
104 : Widget addHeaderWidget() {
105 0 : final width = ScreenHelper.state().width - ThemeHelper.getIndent(4);
106 0 : final indent = ThemeHelper.getIndent(2);
107 0 : return SliverToBoxAdapter(
108 0 : child: SingleScrollWrapper(
109 0 : controller: focus,
110 0 : child: Column(
111 0 : crossAxisAlignment: AppDesign.getAlignment(),
112 0 : children: [
113 0 : InputWrapper.text(
114 0 : title: AppLocale.labels.description,
115 0 : controller: description,
116 0 : tooltip: AppLocale.labels.descriptionTooltip,
117 : ),
118 0 : TagsInput(
119 0 : title: AppLocale.labels.tags,
120 0 : tooltip: AppLocale.labels.tagsTooltip,
121 0 : controller: tags,
122 : ),
123 0 : RowWidget(
124 : indent: indent,
125 0 : maxWidth: width + indent,
126 : chunk: const [0.5, 0.5],
127 0 : children: [
128 0 : [
129 0 : InputWrapper.text(
130 0 : title: AppLocale.labels.amountFrom,
131 0 : tooltip: AppLocale.labels.billSetTooltip,
132 0 : controller: amountFrom,
133 : inputType: const TextInputType.numberWithOptions(decimal: true),
134 0 : formatter: [
135 0 : SimpleInputFormatter.filterDouble,
136 : ],
137 : ),
138 : ],
139 0 : [
140 0 : InputWrapper.text(
141 0 : title: AppLocale.labels.amountTo,
142 0 : tooltip: AppLocale.labels.billSetTooltip,
143 0 : controller: amountTo,
144 : inputType: const TextInputType.numberWithOptions(decimal: true),
145 0 : formatter: [
146 0 : SimpleInputFormatter.filterDouble,
147 : ],
148 : ),
149 : ],
150 : ],
151 : ),
152 0 : InputWrapper(
153 : type: NamedInputType.accountSelector,
154 0 : value: accountTo != null ? state.getByUuid(accountTo!) : null,
155 0 : title: AppLocale.labels.accountTo,
156 0 : tooltip: AppLocale.labels.titleAccountTooltip,
157 0 : state: state,
158 0 : options: accountList,
159 0 : onChange: (value) => setState(() {
160 0 : if (value?.uuid != accountTo) clearState();
161 0 : accountTo = value?.uuid;
162 : }),
163 : width: width,
164 : ),
165 0 : InputWrapper(
166 : type: NamedInputType.accountSelector,
167 0 : value: accountFrom != null ? state.getByUuid(accountFrom!) : null,
168 0 : title: AppLocale.labels.accountFrom,
169 0 : tooltip: AppLocale.labels.titleAccountTooltip,
170 0 : state: state,
171 0 : options: accountList,
172 0 : onChange: (value) => setState(() {
173 0 : if (value?.uuid != accountFrom) clearState();
174 0 : accountFrom = value?.uuid;
175 : }),
176 : width: width,
177 : ),
178 0 : TextWrapper(
179 0 : AppLocale.labels.dateRange,
180 0 : style: context.textTheme.bodyLarge,
181 : ),
182 0 : DateRangeInput(
183 0 : valueFrom: startDate,
184 0 : value: endDate,
185 0 : setState: (start, end) => setState(() {
186 0 : startDate = start;
187 0 : endDate = end;
188 0 : clearState();
189 : }),
190 : ),
191 : ],
192 : ),
193 : ),
194 : );
195 : }
196 : }
|