Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change loan history order #310

Merged
merged 5 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 64 additions & 52 deletions lib/loan/class/loan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import 'package:myecl/tools/functions.dart';
import 'package:myecl/user/class/list_users.dart';

class Loan {
final String id;
final Loaner loaner;
final SimpleUser borrower;
final String notes;
final DateTime start;
final DateTime end;
final String caution;
final List<ItemQuantity> itemsQuantity;
final bool returned;
final DateTime? returnedDate;

Loan({
required this.id,
required this.loaner,
Expand All @@ -14,29 +25,23 @@ class Loan {
required this.caution,
required this.itemsQuantity,
required this.returned,
this.returnedDate,
});
late final String id;
late final Loaner loaner;
late final SimpleUser borrower;
late final String notes;
late final DateTime start;
late final DateTime end;
late final String caution;
late final List<ItemQuantity> itemsQuantity;
late final bool returned;

Loan.fromJson(Map<String, dynamic> json) {
id = json['id'];
borrower = SimpleUser.fromJson(json['borrower']);
loaner = Loaner.fromJson(json['loaner']);
notes = json['notes'];
start = processDateFromAPIWithoutHour(json['start']);
end = processDateFromAPIWithoutHour(json['end']);
caution = json['caution'];
itemsQuantity = List<ItemQuantity>.from(
json['items_qty'].map((x) => ItemQuantity.fromJson(x)));
returned = json['returned'];
}
Loan.fromJson(Map<String, dynamic> json)
: id = json['id'],
borrower = SimpleUser.fromJson(json['borrower']),
loaner = Loaner.fromJson(json['loaner']),
notes = json['notes'],
start = processDateFromAPIWithoutHour(json['start']),
end = processDateFromAPIWithoutHour(json['end']),
caution = json['caution'],
itemsQuantity = List<ItemQuantity>.from(
json['items_qty'].map((x) => ItemQuantity.fromJson(x))),
returned = json['returned'],
returnedDate = json['returned_date'] != null
? DateTime.parse(json['returned_date'])
: null;

Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
Expand All @@ -48,45 +53,52 @@ class Loan {
data['end'] = processDateToAPIWithoutHour(end);
data['caution'] = caution;
data['items_borrowed'] = itemsQuantity.map((x) => x.toJson()).toList();
data['returned_date'] = returnedDate != null
? processDateToAPIWithoutHour(returnedDate!)
: null;
return data;
}

Loan copyWith(
{String? id,
Loaner? loaner,
SimpleUser? borrower,
String? notes,
DateTime? start,
DateTime? end,
String? caution,
List<ItemQuantity>? itemsQuantity,
bool? returned}) {
Loan copyWith({
String? id,
Loaner? loaner,
SimpleUser? borrower,
String? notes,
DateTime? start,
DateTime? end,
String? caution,
List<ItemQuantity>? itemsQuantity,
bool? returned,
DateTime? returnedDate,
}) {
return Loan(
id: id ?? this.id,
loaner: loaner ?? this.loaner,
borrower: borrower ?? this.borrower,
notes: notes ?? this.notes,
start: start ?? this.start,
end: end ?? this.end,
caution: caution ?? this.caution,
itemsQuantity: itemsQuantity ?? this.itemsQuantity,
returned: returned ?? this.returned);
id: id ?? this.id,
loaner: loaner ?? this.loaner,
borrower: borrower ?? this.borrower,
notes: notes ?? this.notes,
start: start ?? this.start,
end: end ?? this.end,
caution: caution ?? this.caution,
itemsQuantity: itemsQuantity ?? this.itemsQuantity,
returned: returned ?? this.returned,
returnedDate: returnedDate ?? this.returnedDate,
);
}

Loan.empty() {
id = '';
borrower = SimpleUser.empty();
loaner = Loaner.empty();
notes = '';
start = DateTime.now();
end = DateTime.now();
caution = '';
itemsQuantity = [];
returned = false;
}
Loan.empty()
: id = '',
borrower = SimpleUser.empty(),
loaner = Loaner.empty(),
notes = '',
start = DateTime.now(),
end = DateTime.now(),
caution = '',
itemsQuantity = [],
returned = false,
returnedDate = null;

@override
String toString() {
return 'Loan(id: $id, loaner: $loaner, borrower: $borrower, notes: $notes, start: $start, end: $end, caution: $caution, itemsQuantity: $itemsQuantity, returned: $returned)';
return 'Loan(id: $id, loaner: $loaner, borrower: $borrower, notes: $notes, start: $start, end: $end, caution: $caution, itemsQuantity: $itemsQuantity, returned: $returned, returnedDate: $returnedDate)';
}
}
1 change: 1 addition & 0 deletions lib/loan/tools/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class LoanTextConstants {
static const String noItems = "Aucun objet";
static const String noItemSelected = "Aucun objet sélectionné";
static const String noLoan = "Aucun prêt";
static const String noReturnedDate = "Pas de date de retour";
static const String quantity = "Quantité";
static const String none = "Aucun";
static const String note = "Note";
Expand Down
20 changes: 13 additions & 7 deletions lib/loan/ui/pages/admin_page/loan_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,19 @@ class LoanCard extends StatelessWidget {
color: shouldReturn
? LoanColorConstants.urgentRed
: Colors.grey.shade400)),
Text(processDate(loan.end),
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.bold,
color: shouldReturn
? Colors.white.withOpacity(0.8)
: Colors.grey.shade400)),
Text(
(loan.returned)
? loan.returnedDate != null
? processDate(loan.returnedDate!)
: LoanTextConstants.noReturnedDate
: processDate(loan.end),
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.bold,
color: shouldReturn
? Colors.white.withOpacity(0.8)
: Colors.grey.shade400),
),
],
),
const Spacer(),
Expand Down
2 changes: 1 addition & 1 deletion lib/loan/ui/pages/admin_page/loan_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class HistoryLoan extends HookConsumerWidget {
value: loan,
builder: (context, data) {
if (data.isNotEmpty) {
data.sort((a, b) => b.end.compareTo(a.end));
data.sort((a, b) => b.returnedDate!.compareTo(a.returnedDate!));
}
return Column(
children: [
Expand Down
7 changes: 5 additions & 2 deletions test/loan/loan_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ void main() {
nickname: '',
),
returned: true,
returnedDate: DateTime.parse('2020-01-01'),
caution: '',
end: DateTime.parse('2020-01-01'),
loaner: Loaner.empty(),
Expand All @@ -215,7 +216,7 @@ void main() {
);
expect(
loan.toString(),
'Loan(id: 1, loaner: Loaner(name: , groupManagerId: , id: ), borrower: SimpleUser {name: name, firstname: , nickname: , id: 1}, notes: , start: 2020-01-01 00:00:00.000, end: 2020-01-01 00:00:00.000, caution: , itemsQuantity: [ItemQuantity(itemSimple: ItemSimple(id: 1, name: name, quantity: 2)], returned: true)',
'Loan(id: 1, loaner: Loaner(name: , groupManagerId: , id: ), borrower: SimpleUser {name: name, firstname: , nickname: , id: 1}, notes: , start: 2020-01-01 00:00:00.000, end: 2020-01-01 00:00:00.000, caution: , itemsQuantity: [ItemQuantity(itemSimple: ItemSimple(id: 1, name: name, quantity: 2)], returned: true, returnedDate: 2020-01-01 00:00:00.000)',
);
});

Expand Down Expand Up @@ -275,6 +276,7 @@ void main() {
nickname: '',
),
returned: true,
returnedDate: DateTime.parse('2020-01-01'),
caution: '',
end: DateTime.parse('2020-01-01'),
loaner: Loaner.empty(),
Expand All @@ -291,7 +293,8 @@ void main() {
'caution': '',
'items_borrowed': [
{'item_id': '1', 'quantity': 2}
]
],
'returned_date': '2020-01-01',
});
});
});
Expand Down