Line data Source code
1 : // Copyright 2023 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/structure/abstract_app_data.dart';
5 : import 'package:app_finance/_classes/structure/account_app_data.dart';
6 : import 'package:app_finance/_classes/structure/budget_app_data.dart';
7 : import 'package:app_finance/_classes/storage/app_data.dart';
8 : import 'package:app_finance/_ext/date_time_ext.dart';
9 : import 'package:app_finance/_mixins/storage_mixin.dart';
10 : import 'package:material_ui/material_ui.dart';
11 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
12 :
13 : class BillAppData extends AbstractAppData with StorageMixin {
14 : String account;
15 : String category;
16 : double exchangeAccount;
17 : double exchangeCategory;
18 : bool hasChild;
19 : String? childOf;
20 :
21 4 : BillAppData({
22 : required this.account,
23 : required this.category,
24 : super.uuid,
25 : super.title = '',
26 : super.details,
27 : super.currency,
28 : super.updatedAt,
29 : super.createdAt,
30 : super.createdAtFormatted,
31 : super.hidden,
32 : super.payment,
33 : this.exchangeAccount = 1.0,
34 : this.exchangeCategory = 1.0,
35 : this.hasChild = false,
36 : this.childOf,
37 : });
38 :
39 2 : @override
40 : String getClassName() => 'BillAppData';
41 :
42 2 : @override
43 : AppDataType getType() => AppDataType.bills;
44 :
45 2 : @override
46 : BillAppData clone() {
47 2 : return BillAppData(
48 2 : uuid: super.uuid,
49 2 : account: account,
50 2 : category: category,
51 2 : title: super.title,
52 2 : details: super.details,
53 2 : currency: super.currency,
54 2 : createdAt: super.createdAt,
55 2 : hidden: super.hidden,
56 2 : payment: super.payment,
57 2 : exchangeAccount: exchangeAccount,
58 2 : exchangeCategory: exchangeCategory,
59 2 : hasChild: hasChild,
60 2 : childOf: childOf,
61 : );
62 : }
63 :
64 0 : factory BillAppData.fromJson(Map<String, dynamic> json) {
65 0 : return BillAppData(
66 0 : uuid: json['uuid'],
67 0 : account: json['account'],
68 0 : category: json['category'],
69 0 : title: json['title'],
70 0 : details: 0.0 + json['details'],
71 0 : currency: CurrencyProvider.find(json['currency']),
72 0 : updatedAt: DateTime.parse(json['updatedAt']),
73 0 : createdAt: DateTime.parse(json['createdAt']),
74 0 : hidden: json['hidden'],
75 0 : payment: json['payment'],
76 0 : hasChild: json['hasChild'] ?? false,
77 0 : childOf: json['childOf'],
78 : );
79 : }
80 :
81 2 : @override
82 2 : Map<String, dynamic> toJson() => {
83 2 : ...super.toJson(),
84 4 : 'account': account,
85 4 : 'category': category,
86 4 : 'hasChild': hasChild,
87 4 : 'childOf': childOf,
88 : };
89 :
90 8 : String get detailsFormatted => (super.details as double).toCurrency(currency: currency, withPattern: false);
91 :
92 2 : @override
93 : String get description {
94 6 : AccountAppData? type = getState().getByUuid(account);
95 10 : return super.createdAt.monthDay() + (type != null ? ' (${type.title})' : '');
96 : }
97 :
98 0 : String get accountNamed => getState().getByUuid(account)?.title ?? '?';
99 :
100 2 : @override
101 : MaterialColor? get color {
102 6 : BudgetAppData? budget = getState().getByUuid(category);
103 2 : return budget?.color;
104 : }
105 :
106 2 : @override
107 : IconData? get icon {
108 6 : BudgetAppData? budget = getState().getByUuid(category);
109 2 : return budget?.icon;
110 : }
111 : }
|