Commit 93c2e264 authored by Robin Trioux's avatar Robin Trioux
Browse files

Refilling + history requests

parent e9abc9ef
......@@ -272,7 +272,7 @@ class Operation(Atom):
def getLabel(self):
return self.label
def getRefounded(self, refounded):
def getRefounded(self):
return self.refounded
def getCounterId(self):
......
......@@ -7,7 +7,7 @@ from QUIManager import QUIManager
from QNFCManager import QNFCManager
from Client import Client
from Euro import Eur
from QUtils import QQuantity, QDelButton
from QUtils import QQuantity, QDelButton, QWarningDialog, center
# ________/\\\___________/\\\\\\\\\_________________________________________________________________
# _____/\\\\/\\\\______/\\\\\\\\\\\\\_______________________________________________________________
......@@ -136,6 +136,7 @@ class QCounter(QAtom, Counter):
class QOperation(QAtom, Operation):
def __init__(self, operation):
super().__init__(operation)
......@@ -143,9 +144,10 @@ class QOperation(QAtom, Operation):
class QBuying(QOperation, Buying):
sRefounded = pyqtSignal() # The attribute "refounded" already exists
def __init__(self, buying):
def __init__(self, buying: Buying):
super().__init__(buying)
self.actionDict["Rembourser"] = {"fct": self.refound, "icon": "delete"}
if not buying.getRefounded():
self.actionDict["Rembourser"] = {"fct": self.refound, "icon": "delete"}
def refound(self):
client = Client()
......@@ -170,9 +172,30 @@ class QBuying(QOperation, Buying):
class QRefilling(QOperation, Refilling):
sCancelled = pyqtSignal()
def __init__(self, refilling):
super().__init__(refilling)
self.infoPannel = None
if not self.getRefounded():
self.actionDict["Annuler"] = {"fct": self.cancel, "icon": "delete"}
def cancel(self):
client = Client()
nfcm = QNFCManager()
if nfcm.getCardUID() == self.getCustomerId():
reply = QMessageBox.question(
None, "Annuler transaction", "Annuler cette transaction ?", QMessageBox.Yes, QMessageBox.No
)
if reply == QMessageBox.Yes:
if client.requestCancelRefilling(refilling_id=self.getId()) is None:
printE("Error during canceling")
else:
self.sCancelled.emit()
else:
warningDialog = QWarningDialog("Mauvais utilisateur", "L'utilisateur n'est pas concerné par cette transaction", "Veuillez présenter une carte concernée par la transaction")
center(warningDialog)
warningDialog.exec()
class QDistribution(QAtom, Distribution):
......
......@@ -130,7 +130,7 @@ class Client(metaclass=ClientSingleton):
uint64 counter_id
string customer_id
string device_uuid
uint64 max_history_size (0: no limit)
uint64 max_history_size (0: no limit)
"""
buyings = []
refillings = []
......@@ -164,13 +164,14 @@ class Client(metaclass=ClientSingleton):
refoundBuyingRequest = com_pb2.RefoundBuyingRequest(**kwargs)
refoundBuyingReply = self.stub.RefoundBuying(refoundBuyingRequest)
if refoundBuyingReply.status == com_pb2.RefoundBuyingReply.SUCCESS:
self.now = unpackTime(refoundBuyingReply.now)
return True
else:
printE("Unable to refound: {}".format(refoundBuyingReply.status))
return None
except RpcError:
printE("RPC: Unable to get product list")
printE("RPC: Unable to refound")
return None
def requestCounterProduct(self, **kwargs) -> [Product]:
......@@ -236,3 +237,21 @@ class Client(metaclass=ClientSingleton):
def requestTransfert(self, **kwargs):
pass
def requestCancelRefilling(self, **kwargs):
"""
uint64 refilling_id
"""
try:
cancelRefillingRequest = com_pb2.CancelRefillingRequest(**kwargs)
cancelRefillingReply = self.stub.CancelRefilling(cancelRefillingRequest)
if cancelRefillingReply.status == com_pb2.CancelRefillingReply.SUCCESS:
self.now = unpackTime(cancelRefillingReply.now)
return True
else:
printE("Unable to refound: {}".format(cancelRefillingReply.status))
return None
except RpcError:
printE("RPC: Unable to cancel refilling")
return None
\ No newline at end of file
......@@ -224,12 +224,12 @@ class QErrorDialog(QMessageBox):
# self.setWindowIcon()
self.setWindowTitle(title)
self.setBaseSize(QSize(800, 600))
self.setBaseSize(QSize(800, 650))
def resizeEvent(self, event):
super().resizeEvent(event)
self.setFixedWidth(600)
self.setFixedHeight(100)
self.setFixedHeight(200)
class QWarningDialog(QMessageBox):
......
......@@ -77,7 +77,7 @@ class QNFCInfo(QWidget):
class QBuyingInfo(QWidget):
def __init__(self, qBuying, parent=None):
def __init__(self, qBuying: QBuying, parent=None):
super().__init__(parent)
productList = qBuying.getBasketItems()
# Définitons
......@@ -146,7 +146,8 @@ class QUserInfo(QWidget):
self.setLayout(self.mainLayout)
# Window settings
self.setFixedSize(500, 500)
# self.setFixedSize(500, 500)
self.historyTree.treeView.expandAll()
QUtils.center(self)
self.setWindowTitle("Information utilisateur")
self.setWindowIcon(uim.getWindowIcon("group"))
......
......@@ -3,9 +3,8 @@ from QTree import *
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import *
# from Euro import *
from Client import Client
# Specialized models ...
......@@ -161,7 +160,18 @@ class QRefillingHistoryModel(QTreeModel):
def addRefilling(self, refilling):
qRefilling = QRefilling(refilling)
qRefilling.sCancelled.connect(self.removeRefilling)
self.insertQAtom(0, qRefilling)
def removeRefilling(self, qRefilling: QRefilling = None):
uim = QUIManager()
if qRefilling:
pass
else:
qRefilling = self.sender()
index, item, data = self.searchQAtom(qRefilling)
self.removeRow(index.row())
uim.balanceUpdated.emit()
def data(self, index: QModelIndex, role):
......@@ -192,6 +202,31 @@ class QUserHistoryModel(QTreeModel):
self.insertQAtom(0, buyingsRootQAtom)
self.insertQAtom(1, refillingRootQAtom)
self.user = user
client = Client()
buyings, refillings = client.requestHistory(type=0, customer_id=user.getId())
for buying in buyings:
qBuying = QBuying(buying)
qBuying.setTexts(["@label", "@price", str(qBuying.getRefounded())])
self.addOperation(qBuying)
# Looks like the server can't handle the case where you want both buyings AND refillings
# Hence this work around...
buyings, refillings = client.requestHistory(type=1, customer_id=user.getId())
for refilling in refillings:
qRefilling = QRefilling(refilling)
qRefilling.setTexts(["", "@amount", str(qRefilling.getRefounded())])
self.addOperation(qRefilling)
def addOperation(self, operation: QOperation):
if isinstance(operation, QBuying):
subRoot = self.index(0, 0, QModelIndex())
self.insertQAtom(0, operation, subRoot)
if isinstance(operation, QRefilling):
subRoot = self.index(1, 0, QModelIndex())
self.insertQAtom(0, operation, subRoot)
class QMultiUserModel(QTreeModel):
......
......@@ -201,7 +201,7 @@ class QBuyingHistory(QItemTree):
with open(GMC_DIR / "data" / "buyingHistory", "rb") as file: # open read/write/binary file
loadedBuyingHistory = pickle.load(file)
return loadedBuyingHistory
except:
except FileNotFoundError:
return None
......@@ -274,7 +274,7 @@ class QUserHistory(QItemTree):
# Definition
self.mainLayout = QHBoxLayout()
self.treeView = QSuperTreeView()
self.treeModel = QUserHistoryModel(["Transaction", "Montant"], user)
self.treeModel = QUserHistoryModel(["Transaction", "Montant", "Annulée"], user)
# Layout
self.treeView.setModel(self.treeModel)
......
......@@ -380,7 +380,7 @@ class QTreeModel(QAbstractItemModel):
def insertQAtom(self, position, qAtom, parent=QModelIndex()):
self.insertRow(position, parent)
newIndex = self.index(position, 0)
newIndex = self.index(position, 0, parent)
self.setData(newIndex, qAtom)
def searchQAtom(self, qAtom: QAtom, parent=QModelIndex()):
......
......@@ -7,5 +7,5 @@ pip install -r requirements.txt
./manage.py protoc # Generate protobuf files
./manage.py setup --import 2019.json # Generate database
./manage.py runserver
```
\ No newline at end of file
./manage.py runserver -h 127.0.0.1
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment