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 : List<String> tags;
21 :
22 18 : BillAppData({
23 : required this.account,
24 : required this.category,
25 : super.uuid,
26 : super.title = '',
27 : super.details,
28 : super.currency,
29 : super.updatedAt,
30 : super.createdAt,
31 : super.createdAtFormatted,
32 : super.hidden,
33 : super.payment,
34 : this.exchangeAccount = 1.0,
35 : this.exchangeCategory = 1.0,
36 : this.hasChild = false,
37 : this.childOf,
38 : List<String>? tags,
39 18 : }) : tags = tags ?? [];
40 :
41 0 : @override
42 : String getClassName() => 'BillAppData';
43 :
44 0 : @override
45 : AppDataType getType() => AppDataType.bills;
46 :
47 2 : @override
48 : BillAppData clone() {
49 2 : return BillAppData(
50 2 : uuid: super.uuid,
51 2 : account: account,
52 2 : category: category,
53 2 : title: super.title,
54 2 : details: super.details,
55 2 : currency: super.currency,
56 2 : createdAt: super.createdAt,
57 2 : hidden: super.hidden,
58 2 : payment: super.payment,
59 2 : exchangeAccount: exchangeAccount,
60 2 : exchangeCategory: exchangeCategory,
61 2 : hasChild: hasChild,
62 2 : childOf: childOf,
63 4 : tags: [...tags],
64 : );
65 : }
66 :
67 1 : factory BillAppData.fromJson(Map<String, dynamic> json) {
68 1 : return BillAppData(
69 1 : uuid: json['uuid'],
70 1 : account: json['account'],
71 1 : category: json['category'],
72 1 : title: json['title'],
73 2 : details: 0.0 + json['details'],
74 2 : currency: CurrencyProvider.find(json['currency']),
75 2 : updatedAt: DateTime.parse(json['updatedAt']),
76 2 : createdAt: DateTime.parse(json['createdAt']),
77 1 : hidden: json['hidden'],
78 1 : payment: json['payment'],
79 1 : hasChild: json['hasChild'] ?? false,
80 1 : childOf: json['childOf'],
81 2 : tags: (json['tags'] as List<dynamic>?)?.cast<String>(),
82 : );
83 : }
84 :
85 0 : @override
86 0 : Map<String, dynamic> toJson() => {
87 0 : ...super.toJson(),
88 0 : 'account': account,
89 0 : 'category': category,
90 0 : 'hasChild': hasChild,
91 0 : 'childOf': childOf,
92 0 : 'tags': tags,
93 : };
94 :
95 0 : String get detailsFormatted => (super.details as double).toCurrency(currency: currency, withPattern: false);
96 :
97 0 : @override
98 : String get description {
99 0 : AccountAppData? type = getState().getByUuid(account);
100 0 : return super.createdAt.monthDay() + (type != null ? ' (${type.title})' : '');
101 : }
102 :
103 0 : String get accountNamed => getState().getByUuid(account)?.title ?? '?';
104 :
105 0 : @override
106 : MaterialColor? get color {
107 0 : BudgetAppData? budget = getState().getByUuid(category);
108 0 : return budget?.color;
109 : }
110 :
111 0 : @override
112 : IconData? get icon {
113 0 : BudgetAppData? budget = getState().getByUuid(category);
114 0 : return budget?.icon;
115 : }
116 : }
|