121 lines
3.6 KiB
Dart
121 lines
3.6 KiB
Dart
class TransactionItem {
|
|
final int id;
|
|
final String label;
|
|
final double amount;
|
|
final String type;
|
|
final int accountId;
|
|
final int? targetAccountId;
|
|
final int? categoryId;
|
|
final int? provisionId;
|
|
final DateTime date;
|
|
final String? note;
|
|
final bool isPending;
|
|
final int? localOperationId;
|
|
|
|
TransactionItem({
|
|
required this.id,
|
|
required this.label,
|
|
required this.amount,
|
|
required this.type,
|
|
required this.accountId,
|
|
required this.date,
|
|
this.targetAccountId,
|
|
this.categoryId,
|
|
this.provisionId,
|
|
this.note,
|
|
this.isPending = false,
|
|
this.localOperationId,
|
|
});
|
|
|
|
factory TransactionItem.fromJson(Map<String, dynamic> json) {
|
|
return TransactionItem(
|
|
id: json['id'] as int,
|
|
label: json['label'] as String,
|
|
amount: (json['amount'] as num? ?? 0).toDouble(),
|
|
type: json['type'] as String,
|
|
accountId: json['account_id'] as int,
|
|
targetAccountId: json['target_account_id'] as int?,
|
|
categoryId: json['category_id'] as int?,
|
|
provisionId: json['provision_id'] as int?,
|
|
date: DateTime.parse(json['date'] as String),
|
|
note: json['note'] as String?,
|
|
isPending: json['is_pending'] is int
|
|
? (json['is_pending'] as int) == 1
|
|
: json['is_pending'] as bool? ?? false,
|
|
localOperationId: json['local_operation_id'] as int?,
|
|
);
|
|
}
|
|
|
|
factory TransactionItem.fromPending({
|
|
required int localOperationId,
|
|
required Map<String, dynamic> payload,
|
|
}) {
|
|
return TransactionItem(
|
|
id: -localOperationId,
|
|
label: payload['label'] as String? ?? 'Opération mobile',
|
|
amount: (payload['amount'] as num? ?? 0).toDouble(),
|
|
type: payload['type'] as String? ?? 'expense',
|
|
accountId: payload['account_id'] as int,
|
|
targetAccountId: payload['target_account_id'] as int?,
|
|
categoryId: payload['category_id'] as int?,
|
|
provisionId: payload['provision_id'] as int?,
|
|
date: DateTime.tryParse(payload['date'] as String? ?? '') ?? DateTime.now(),
|
|
note: payload['note'] as String?,
|
|
isPending: true,
|
|
localOperationId: localOperationId,
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'id': id,
|
|
'label': label,
|
|
'amount': amount,
|
|
'type': type,
|
|
'account_id': accountId,
|
|
'target_account_id': targetAccountId,
|
|
'category_id': categoryId,
|
|
'provision_id': provisionId,
|
|
'date': date.toIso8601String(),
|
|
'note': note,
|
|
'is_pending': isPending,
|
|
'local_operation_id': localOperationId,
|
|
};
|
|
}
|
|
|
|
Map<String, dynamic> toDbMap() {
|
|
return {
|
|
'id': id,
|
|
'label': label,
|
|
'amount': amount,
|
|
'type': type,
|
|
'account_id': accountId,
|
|
'target_account_id': targetAccountId,
|
|
'category_id': categoryId,
|
|
'provision_id': provisionId,
|
|
'date': date.toIso8601String(),
|
|
'note': note,
|
|
'is_pending': isPending ? 1 : 0,
|
|
'local_operation_id': localOperationId,
|
|
'updated_at': DateTime.now().toIso8601String(),
|
|
};
|
|
}
|
|
|
|
factory TransactionItem.fromDbMap(Map<String, dynamic> map) {
|
|
return TransactionItem(
|
|
id: map['id'] as int,
|
|
label: map['label'] as String,
|
|
amount: (map['amount'] as num? ?? 0).toDouble(),
|
|
type: map['type'] as String,
|
|
accountId: map['account_id'] as int,
|
|
targetAccountId: map['target_account_id'] as int?,
|
|
categoryId: map['category_id'] as int?,
|
|
provisionId: map['provision_id'] as int?,
|
|
date: DateTime.parse(map['date'] as String),
|
|
note: map['note'] as String?,
|
|
isPending: (map['is_pending'] as int? ?? 0) == 1,
|
|
localOperationId: map['local_operation_id'] as int?,
|
|
);
|
|
}
|
|
}
|