feat: complete sell

This commit is contained in:
alzalia1 2025-08-15 14:48:27 +02:00
parent b4375fbaa2
commit bee5e05296
5 changed files with 144 additions and 38 deletions

View file

@ -23,4 +23,12 @@ class BookInstanceRepository {
) async {
return await _apiClient.sendBook(book, owner, bal, price);
}
Future<Result<void>> sellBooks(List<BookInstance> books) async {
Map<String, double?> res = {};
for (BookInstance instance in books) {
res[instance.id.toString()] = instance.soldPrice;
}
return await _apiClient.sellBooks(res);
}
}

View file

@ -310,6 +310,31 @@ class ApiClient {
}
}
Future<Result<void>> sellBooks(Map<String, double?> books) async {
final client = Client();
try {
final headers = await _getHeaders({"Content-Type": "application/json"});
debugPrint("\n\n\n\nMAP: $books\n\n\n\n");
final body = jsonEncode(books);
debugPrint("\n\n\n\nSENT: $body\n\n\n\n");
final response = await client.post(
Uri.parse("https://$apiBasePath/book_instance/sell/bulk"),
headers: headers,
body: body,
);
if (response.statusCode == 200) {
return Result.ok(response.statusCode);
} else {
throw "Unknown error";
}
} catch (e) {
debugPrint("\n\n\n\nERROR : ${e.toString()}\n\n\n\n");
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<BookInstance>> sendBook(
Book book,
Owner owner,

View file

@ -50,14 +50,41 @@ class SellViewModel extends ChangeNotifier {
List<BookStack> get scannedBooks => _scannedBooks;
bool isScanLoaded = false;
bool isSendingSell = false;
double minimumAmount = 0;
void sellBook(BookStack addedBook) {
minimumAmount += addedBook.instance.price;
_soldBooks.add(addedBook);
notifyListeners();
}
void sendSell() {
void sendSell(double givenAmount) async {
isSendingSell = true;
notifyListeners();
List<BookInstance> toSend = [];
int nbOfPl = 0;
for (BookStack book in _soldBooks) {
if (book.instance.price != 0) {
book.instance.soldPrice = book.instance.price;
givenAmount -= book.instance.price;
toSend.add(book.instance);
} else {
nbOfPl++;
}
}
if (nbOfPl != 0) {
double amountPerPl = givenAmount / nbOfPl;
for (BookStack book in _soldBooks) {
if (book.instance.price == 0) {
book.instance.soldPrice = amountPerPl;
toSend.add(book.instance);
}
}
}
await _bookInstanceRepository.sellBooks(toSend);
_soldBooks.clear();
isSendingSell = false;
notifyListeners();
}
@ -75,6 +102,9 @@ class SellViewModel extends ChangeNotifier {
switch (result) {
case Ok():
for (BookInstance instance in result.value) {
if (instance.available == false) {
continue;
}
if (_soldBooks
.where((book) => book.instance.id == instance.id)
.isNotEmpty) {

View file

@ -25,7 +25,7 @@ class SellChoicePopup extends StatelessWidget {
children: [
(viewModel.scannedBooks.isEmpty)
? Text(
"Ce livre n'a jamais été rentré, ou vous l'avez déjà mis dans cette vente.",
"Ce livre n'a jamais été rentré, il a déjà été vendu ou vous l'avez déjà mis dans cette vente.",
)
: SizedBox(),
for (BookStack book in viewModel.scannedBooks)

View file

@ -17,6 +17,8 @@ class SellPage extends StatefulWidget {
}
class _SellPageState extends State<SellPage> {
TextEditingController price = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
@ -70,12 +72,15 @@ class _SellPageState extends State<SellPage> {
),
SizedBox(height: 15),
Expanded(
child: ListView(
child: (widget.viewModel.isSendingSell)
? Center(child: CircularProgressIndicator())
: ListView(
children: [
(widget.viewModel.scannedBooks.isEmpty)
? Center(child: Text("Aucun"))
: SizedBox(),
for (BookStack book in widget.viewModel.soldBooks)
for (BookStack book
in widget.viewModel.soldBooks)
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 15,
@ -107,15 +112,20 @@ class _SellPageState extends State<SellPage> {
),
),
SizedBox(height: 40),
Text("Montant minimum à payer : 20€"),
Text(
"Montant minimum à payer : ${widget.viewModel.minimumAmount.toString()}",
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 60.0),
child: SizedBox(
child: TextField(
controller: price,
decoration: InputDecoration(
labelText: "Argent reçu",
hintText:
"Utilisez un point (.) pour les virgules (,)",
helperText:
"L'argent reçu sera réparti automatiquement",
"L'argent reçu sera réparti automatiquement.",
suffixText: "",
border: OutlineInputBorder(),
),
@ -129,7 +139,40 @@ class _SellPageState extends State<SellPage> {
children: [
IconButton(
onPressed: () {
widget.viewModel.sendSell();
if (double.tryParse(price.text) == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
"Veuillez indiquer un prix de vente valide",
),
behavior: SnackBarBehavior.floating,
),
);
return;
} else if (double.parse(price.text) <
widget.viewModel.minimumAmount) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
"Le prix de vente est inférieur au montant minimum",
),
behavior: SnackBarBehavior.floating,
),
);
return;
}
widget.viewModel.sendSell(
double.parse(price.text),
);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
"La vente a bien été enregistrée.",
),
behavior: SnackBarBehavior.floating,
),
);
return;
},
icon: Icon(Icons.check),
style: ButtonStyle(