feat: complete sell
This commit is contained in:
parent
b4375fbaa2
commit
bee5e05296
5 changed files with 144 additions and 38 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
Reference in a new issue