feat: complete sell
This commit is contained in:
parent
b4375fbaa2
commit
bee5e05296
5 changed files with 144 additions and 38 deletions
|
|
@ -23,4 +23,12 @@ class BookInstanceRepository {
|
||||||
) async {
|
) async {
|
||||||
return await _apiClient.sendBook(book, owner, bal, price);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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(
|
Future<Result<BookInstance>> sendBook(
|
||||||
Book book,
|
Book book,
|
||||||
Owner owner,
|
Owner owner,
|
||||||
|
|
|
||||||
|
|
@ -50,14 +50,41 @@ class SellViewModel extends ChangeNotifier {
|
||||||
List<BookStack> get scannedBooks => _scannedBooks;
|
List<BookStack> get scannedBooks => _scannedBooks;
|
||||||
|
|
||||||
bool isScanLoaded = false;
|
bool isScanLoaded = false;
|
||||||
|
bool isSendingSell = false;
|
||||||
|
double minimumAmount = 0;
|
||||||
|
|
||||||
void sellBook(BookStack addedBook) {
|
void sellBook(BookStack addedBook) {
|
||||||
|
minimumAmount += addedBook.instance.price;
|
||||||
_soldBooks.add(addedBook);
|
_soldBooks.add(addedBook);
|
||||||
notifyListeners();
|
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();
|
_soldBooks.clear();
|
||||||
|
isSendingSell = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,6 +102,9 @@ class SellViewModel extends ChangeNotifier {
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case Ok():
|
case Ok():
|
||||||
for (BookInstance instance in result.value) {
|
for (BookInstance instance in result.value) {
|
||||||
|
if (instance.available == false) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (_soldBooks
|
if (_soldBooks
|
||||||
.where((book) => book.instance.id == instance.id)
|
.where((book) => book.instance.id == instance.id)
|
||||||
.isNotEmpty) {
|
.isNotEmpty) {
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ class SellChoicePopup extends StatelessWidget {
|
||||||
children: [
|
children: [
|
||||||
(viewModel.scannedBooks.isEmpty)
|
(viewModel.scannedBooks.isEmpty)
|
||||||
? Text(
|
? 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(),
|
: SizedBox(),
|
||||||
for (BookStack book in viewModel.scannedBooks)
|
for (BookStack book in viewModel.scannedBooks)
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ class SellPage extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _SellPageState extends State<SellPage> {
|
class _SellPageState extends State<SellPage> {
|
||||||
|
TextEditingController price = TextEditingController();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
|
@ -70,12 +72,15 @@ class _SellPageState extends State<SellPage> {
|
||||||
),
|
),
|
||||||
SizedBox(height: 15),
|
SizedBox(height: 15),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: ListView(
|
child: (widget.viewModel.isSendingSell)
|
||||||
|
? Center(child: CircularProgressIndicator())
|
||||||
|
: ListView(
|
||||||
children: [
|
children: [
|
||||||
(widget.viewModel.scannedBooks.isEmpty)
|
(widget.viewModel.scannedBooks.isEmpty)
|
||||||
? Center(child: Text("Aucun"))
|
? Center(child: Text("Aucun"))
|
||||||
: SizedBox(),
|
: SizedBox(),
|
||||||
for (BookStack book in widget.viewModel.soldBooks)
|
for (BookStack book
|
||||||
|
in widget.viewModel.soldBooks)
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.symmetric(
|
padding: const EdgeInsets.symmetric(
|
||||||
horizontal: 15,
|
horizontal: 15,
|
||||||
|
|
@ -107,15 +112,20 @@ class _SellPageState extends State<SellPage> {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
SizedBox(height: 40),
|
SizedBox(height: 40),
|
||||||
Text("Montant minimum à payer : 20€"),
|
Text(
|
||||||
|
"Montant minimum à payer : ${widget.viewModel.minimumAmount.toString()}€",
|
||||||
|
),
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 60.0),
|
padding: const EdgeInsets.symmetric(horizontal: 60.0),
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
child: TextField(
|
child: TextField(
|
||||||
|
controller: price,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
labelText: "Argent reçu",
|
labelText: "Argent reçu",
|
||||||
|
hintText:
|
||||||
|
"Utilisez un point (.) pour les virgules (,)",
|
||||||
helperText:
|
helperText:
|
||||||
"L'argent reçu sera réparti automatiquement",
|
"L'argent reçu sera réparti automatiquement.",
|
||||||
suffixText: "€",
|
suffixText: "€",
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
),
|
),
|
||||||
|
|
@ -129,7 +139,40 @@ class _SellPageState extends State<SellPage> {
|
||||||
children: [
|
children: [
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
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),
|
icon: Icon(Icons.check),
|
||||||
style: ButtonStyle(
|
style: ButtonStyle(
|
||||||
|
|
|
||||||
Reference in a new issue